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.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.SnapshotCleanupProtos; 034 035@Tag(MasterTests.TAG) 036@Tag(MediumTests.TAG) 037public class TestSnapshotCleanupStateStore extends MasterStateStoreTestBase { 038 039 @AfterEach 040 public void tearDown() throws Exception { 041 cleanup(); 042 ZKUtil.deleteNodeFailSilent(UTIL.getZooKeeperWatcher(), 043 UTIL.getZooKeeperWatcher().getZNodePaths().snapshotCleanupZNode); 044 } 045 046 @Test 047 public void testReadWrite() throws Exception { 048 SnapshotCleanupStateStore store = 049 new SnapshotCleanupStateStore(REGION, UTIL.getZooKeeperWatcher()); 050 assertTrue(store.get()); 051 store.set(false); 052 assertFalse(store.get()); 053 054 // restart 055 store = new SnapshotCleanupStateStore(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 snapshot cleanup enabled to false, since the default value is 064 // true 065 byte[] zkData = ProtobufUtil.prependPBMagic(SnapshotCleanupProtos.SnapshotCleanupState 066 .newBuilder().setSnapshotCleanupEnabled(false).build().toByteArray()); 067 ZKUtil.createSetData(UTIL.getZooKeeperWatcher(), 068 UTIL.getZooKeeperWatcher().getZNodePaths().snapshotCleanupZNode, zkData); 069 070 SnapshotCleanupStateStore store = 071 new SnapshotCleanupStateStore(REGION, UTIL.getZooKeeperWatcher()); 072 assertFalse(store.get()); 073 // should have deleted the node on zk 074 assertEquals(-1, ZKUtil.checkExists(UTIL.getZooKeeperWatcher(), 075 UTIL.getZooKeeperWatcher().getZNodePaths().snapshotCleanupZNode)); 076 } 077}