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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import org.apache.hadoop.hbase.master.MasterStateStoreTestBase;
025import org.apache.hadoop.hbase.testclassification.MasterTests;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.zookeeper.ZKUtil;
028import org.junit.jupiter.api.AfterEach;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.LoadBalancerProtos;
034
035@Tag(MasterTests.TAG)
036@Tag(MediumTests.TAG)
037public class TestLoadBalancerStateStore extends MasterStateStoreTestBase {
038
039  @AfterEach
040  public void tearDown() throws Exception {
041    cleanup();
042    ZKUtil.deleteNodeFailSilent(UTIL.getZooKeeperWatcher(),
043      UTIL.getZooKeeperWatcher().getZNodePaths().balancerZNode);
044  }
045
046  @Test
047  public void testReadWrite() throws Exception {
048    LoadBalancerStateStore store = new LoadBalancerStateStore(REGION, UTIL.getZooKeeperWatcher());
049    assertTrue(store.get());
050    store.set(false);
051    assertFalse(store.get());
052
053    // restart
054    store = new LoadBalancerStateStore(REGION, UTIL.getZooKeeperWatcher());
055    assertFalse(store.get());
056    store.set(true);
057    assertTrue(store.get());
058  }
059
060  @Test
061  public void testMigrate() throws Exception {
062    // prepare data on zk which set balancer on to false, since the default value is true
063    byte[] zkData = ProtobufUtil.prependPBMagic(
064      LoadBalancerProtos.LoadBalancerState.newBuilder().setBalancerOn(false).build().toByteArray());
065    ZKUtil.createSetData(UTIL.getZooKeeperWatcher(),
066      UTIL.getZooKeeperWatcher().getZNodePaths().balancerZNode, zkData);
067
068    LoadBalancerStateStore store = new LoadBalancerStateStore(REGION, UTIL.getZooKeeperWatcher());
069    assertFalse(store.get());
070    // should have deleted the node on zk
071    assertEquals(-1, ZKUtil.checkExists(UTIL.getZooKeeperWatcher(),
072      UTIL.getZooKeeperWatcher().getZNodePaths().balancerZNode));
073  }
074}