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