HBASE-23723 Ensure MOB compaction works in optimized mode after snapshot clone (...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / util / TestHBaseFsckReplication.java
blob316a321f2b7baeada47bedfb6b4bb5d78c22e79f
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.util;
20 import static org.junit.Assert.assertEquals;
22 import java.util.List;
23 import java.util.stream.Stream;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.ServerName;
27 import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
28 import org.apache.hadoop.hbase.replication.ReplicationPeerStorage;
29 import org.apache.hadoop.hbase.replication.ReplicationQueueStorage;
30 import org.apache.hadoop.hbase.replication.ReplicationStorageFactory;
31 import org.apache.hadoop.hbase.replication.SyncReplicationState;
32 import org.apache.hadoop.hbase.testclassification.MediumTests;
33 import org.apache.hadoop.hbase.testclassification.ReplicationTests;
34 import org.apache.hadoop.hbase.util.HbckErrorReporter.ERROR_CODE;
35 import org.apache.hadoop.hbase.util.hbck.HbckTestingUtil;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.ClassRule;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
42 @Category({ ReplicationTests.class, MediumTests.class })
43 public class TestHBaseFsckReplication {
45 @ClassRule
46 public static final HBaseClassTestRule CLASS_RULE =
47 HBaseClassTestRule.forClass(TestHBaseFsckReplication.class);
49 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
51 @BeforeClass
52 public static void setUp() throws Exception {
53 UTIL.getConfiguration().setBoolean("hbase.write.hbck1.lock.file", false);
54 UTIL.startMiniCluster(1);
57 @AfterClass
58 public static void tearDown() throws Exception {
59 UTIL.shutdownMiniCluster();
62 @Test
63 public void test() throws Exception {
64 ReplicationPeerStorage peerStorage = ReplicationStorageFactory
65 .getReplicationPeerStorage(UTIL.getZooKeeperWatcher(), UTIL.getConfiguration());
66 ReplicationQueueStorage queueStorage = ReplicationStorageFactory
67 .getReplicationQueueStorage(UTIL.getZooKeeperWatcher(), UTIL.getConfiguration());
69 String peerId1 = "1";
70 String peerId2 = "2";
71 peerStorage.addPeer(peerId1, ReplicationPeerConfig.newBuilder().setClusterKey("key").build(),
72 true, SyncReplicationState.NONE);
73 peerStorage.addPeer(peerId2, ReplicationPeerConfig.newBuilder().setClusterKey("key").build(),
74 true, SyncReplicationState.NONE);
75 for (int i = 0; i < 10; i++) {
76 queueStorage.addWAL(ServerName.valueOf("localhost", 10000 + i, 100000 + i), peerId1,
77 "file-" + i);
79 queueStorage.addWAL(ServerName.valueOf("localhost", 10000, 100000), peerId2, "file");
80 HBaseFsck fsck = HbckTestingUtil.doFsck(UTIL.getConfiguration(), true);
81 HbckTestingUtil.assertNoErrors(fsck);
83 // should not remove anything since the replication peer is still alive
84 assertEquals(10, queueStorage.getListOfReplicators().size());
85 peerStorage.removePeer(peerId1);
86 // there should be orphan queues
87 assertEquals(10, queueStorage.getListOfReplicators().size());
88 fsck = HbckTestingUtil.doFsck(UTIL.getConfiguration(), false);
89 HbckTestingUtil.assertErrors(fsck, Stream.generate(() -> {
90 return ERROR_CODE.UNDELETED_REPLICATION_QUEUE;
91 }).limit(10).toArray(ERROR_CODE[]::new));
93 // should not delete anything when fix is false
94 assertEquals(10, queueStorage.getListOfReplicators().size());
96 fsck = HbckTestingUtil.doFsck(UTIL.getConfiguration(), true);
97 HbckTestingUtil.assertErrors(fsck, Stream.generate(() -> {
98 return ERROR_CODE.UNDELETED_REPLICATION_QUEUE;
99 }).limit(10).toArray(ERROR_CODE[]::new));
101 List<ServerName> replicators = queueStorage.getListOfReplicators();
102 // should not remove the server with queue for peerId2
103 assertEquals(1, replicators.size());
104 assertEquals(ServerName.valueOf("localhost", 10000, 100000), replicators.get(0));
105 for (String queueId : queueStorage.getAllQueues(replicators.get(0))) {
106 assertEquals(peerId2, queueId);