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