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