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;
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.client.MasterSwitchType;
026import org.apache.hadoop.hbase.testclassification.MasterTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.zookeeper.ZKUtil;
029import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
030import org.junit.After;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
036import org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos;
037
038@Category({ MasterTests.class, MediumTests.class })
039public class TestSplitOrMergeStateStore extends MasterStateStoreTestBase {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestSplitOrMergeStateStore.class);
044
045  @After
046  public void tearDown() throws Exception {
047    cleanup();
048    ZKUtil.deleteNodeRecursively(UTIL.getZooKeeperWatcher(),
049      UTIL.getZooKeeperWatcher().getZNodePaths().switchZNode);
050  }
051
052  @Test
053  public void testSplit() throws Exception {
054    testReadWrite(MasterSwitchType.SPLIT);
055  }
056
057  @Test
058  public void testMerge() throws Exception {
059    testReadWrite(MasterSwitchType.MERGE);
060  }
061
062  @Test
063  public void testSplitMigrate() throws Exception {
064    testMigrate(MasterSwitchType.SPLIT,
065      ZNodePaths.joinZNode(UTIL.getZooKeeperWatcher().getZNodePaths().switchZNode,
066        UTIL.getConfiguration().get("zookeeper.znode.switch.split", "split")));
067  }
068
069  @Test
070  public void testMergeMigrate() throws Exception {
071    testMigrate(MasterSwitchType.MERGE,
072      ZNodePaths.joinZNode(UTIL.getZooKeeperWatcher().getZNodePaths().switchZNode,
073        UTIL.getConfiguration().get("zookeeper.znode.switch.merge", "merge")));
074  }
075
076  private void testReadWrite(MasterSwitchType type) throws Exception {
077    SplitOrMergeStateStore store =
078      new SplitOrMergeStateStore(REGION, UTIL.getZooKeeperWatcher(), UTIL.getConfiguration());
079    assertTrue(store.isSplitOrMergeEnabled(type));
080    store.setSplitOrMergeEnabled(false, type);
081    assertFalse(store.isSplitOrMergeEnabled(type));
082
083    // restart
084    store = new SplitOrMergeStateStore(REGION, UTIL.getZooKeeperWatcher(), UTIL.getConfiguration());
085    assertFalse(store.isSplitOrMergeEnabled(type));
086    store.setSplitOrMergeEnabled(true, type);
087    assertTrue(store.isSplitOrMergeEnabled(type));
088  }
089
090  private void testMigrate(MasterSwitchType type, String zkPath) throws Exception {
091    // prepare data on zk which set snapshot cleanup enabled to false, since the default value is
092    // true
093    byte[] zkData = ProtobufUtil.prependPBMagic(
094      ZooKeeperProtos.SwitchState.newBuilder().setEnabled(false).build().toByteArray());
095    ZKUtil.createSetData(UTIL.getZooKeeperWatcher(), zkPath, zkData);
096
097    SplitOrMergeStateStore store =
098      new SplitOrMergeStateStore(REGION, UTIL.getZooKeeperWatcher(), UTIL.getConfiguration());
099    assertFalse(store.isSplitOrMergeEnabled(type));
100    // should have deleted the node on zk
101    assertEquals(-1, ZKUtil.checkExists(UTIL.getZooKeeperWatcher(), zkPath));
102  }
103}