001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.balancer;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.master.MasterStateStoreTestBase;
026import org.apache.hadoop.hbase.testclassification.MasterTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.zookeeper.ZKUtil;
029import org.junit.After;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
035import org.apache.hadoop.hbase.shaded.protobuf.generated.LoadBalancerProtos;
036
037@Category({ MasterTests.class, MediumTests.class })
038public class TestLoadBalancerStateStore extends MasterStateStoreTestBase {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestLoadBalancerStateStore.class);
043
044  @After
045  public void tearDown() throws Exception {
046    cleanup();
047    ZKUtil.deleteNodeFailSilent(UTIL.getZooKeeperWatcher(),
048      UTIL.getZooKeeperWatcher().getZNodePaths().balancerZNode);
049  }
050
051  @Test
052  public void testReadWrite() throws Exception {
053    LoadBalancerStateStore store = new LoadBalancerStateStore(REGION, UTIL.getZooKeeperWatcher());
054    assertTrue(store.get());
055    store.set(false);
056    assertFalse(store.get());
057
058    // restart
059    store = new LoadBalancerStateStore(REGION, UTIL.getZooKeeperWatcher());
060    assertFalse(store.get());
061    store.set(true);
062    assertTrue(store.get());
063  }
064
065  @Test
066  public void testMigrate() throws Exception {
067    // prepare data on zk which set balancer on to false, since the default value is true
068    byte[] zkData = ProtobufUtil.prependPBMagic(
069      LoadBalancerProtos.LoadBalancerState.newBuilder().setBalancerOn(false).build().toByteArray());
070    ZKUtil.createSetData(UTIL.getZooKeeperWatcher(),
071      UTIL.getZooKeeperWatcher().getZNodePaths().balancerZNode, zkData);
072
073    LoadBalancerStateStore store = new LoadBalancerStateStore(REGION, UTIL.getZooKeeperWatcher());
074    assertFalse(store.get());
075    // should have deleted the node on zk
076    assertEquals(-1, ZKUtil.checkExists(UTIL.getZooKeeperWatcher(),
077      UTIL.getZooKeeperWatcher().getZNodePaths().balancerZNode));
078  }
079}