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.normalizer;
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.RegionNormalizerProtos;
034
035@Tag(MasterTests.TAG)
036@Tag(MediumTests.TAG)
037public class TestRegionNormalizerStateStore extends MasterStateStoreTestBase {
038
039  @AfterEach
040  public void tearDown() throws Exception {
041    cleanup();
042    ZKUtil.deleteNodeFailSilent(UTIL.getZooKeeperWatcher(),
043      UTIL.getZooKeeperWatcher().getZNodePaths().regionNormalizerZNode);
044  }
045
046  @Test
047  public void testReadWrite() throws Exception {
048    RegionNormalizerStateStore store =
049      new RegionNormalizerStateStore(REGION, UTIL.getZooKeeperWatcher());
050    assertTrue(store.get());
051    store.set(false);
052    assertFalse(store.get());
053
054    // restart
055    store = new RegionNormalizerStateStore(REGION, UTIL.getZooKeeperWatcher());
056    assertFalse(store.get());
057    store.set(true);
058    assertTrue(store.get());
059  }
060
061  @Test
062  public void testMigrate() throws Exception {
063    // prepare data on zk which set normalizer on to false, since the default value is true
064    byte[] zkData = ProtobufUtil.prependPBMagic(RegionNormalizerProtos.RegionNormalizerState
065      .newBuilder().setNormalizerOn(false).build().toByteArray());
066    ZKUtil.createSetData(UTIL.getZooKeeperWatcher(),
067      UTIL.getZooKeeperWatcher().getZNodePaths().regionNormalizerZNode, zkData);
068
069    RegionNormalizerStateStore store =
070      new RegionNormalizerStateStore(REGION, UTIL.getZooKeeperWatcher());
071    assertFalse(store.get());
072    // should have deleted the node on zk
073    assertEquals(-1, ZKUtil.checkExists(UTIL.getZooKeeperWatcher(),
074      UTIL.getZooKeeperWatcher().getZNodePaths().regionNormalizerZNode));
075  }
076}