1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master.cleaner;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.replication.ReplicationFactory;
30 import org.apache.hadoop.hbase.replication.ReplicationQueues;
31 import org.apache.hadoop.hbase.replication.ReplicationQueuesZKImpl;
32 import org.apache.hadoop.hbase.testclassification.MasterTests;
33 import org.apache.hadoop.hbase.testclassification.MediumTests;
34 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 @Category({ MasterTests.class, MediumTests.class })
41 public class TestReplicationZKNodeCleaner {
42 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
43
44 private final String ID_ONE = "1";
45 private final String SERVER_ONE = "server1";
46 private final String ID_TWO = "2";
47 private final String SERVER_TWO = "server2";
48
49 private final Configuration conf;
50 private final ZooKeeperWatcher zkw;
51 private final ReplicationQueues repQueues;
52
53 public TestReplicationZKNodeCleaner() throws Exception {
54 conf = TEST_UTIL.getConfiguration();
55 zkw = new ZooKeeperWatcher(conf, "TestReplicationZKNodeCleaner", null);
56 repQueues = ReplicationFactory.getReplicationQueues(zkw, conf, null);
57 assertTrue(repQueues instanceof ReplicationQueuesZKImpl);
58 }
59
60 @BeforeClass
61 public static void setUpBeforeClass() throws Exception {
62 TEST_UTIL.getConfiguration().setInt("hbase.master.cleaner.interval", 10000);
63 TEST_UTIL.startMiniCluster();
64 }
65
66 @AfterClass
67 public static void tearDownAfterClass() throws Exception {
68 TEST_UTIL.shutdownMiniCluster();
69 }
70
71 @Test
72 public void testReplicationZKNodeCleaner() throws Exception {
73 repQueues.init(SERVER_ONE);
74
75 repQueues.addLog(ID_ONE, "file1");
76
77 ReplicationZKNodeCleaner cleaner = new ReplicationZKNodeCleaner(conf, zkw, null);
78 Map<String, List<String>> undeletedQueues = cleaner.getUnDeletedQueues();
79 assertEquals(1, undeletedQueues.size());
80 assertTrue(undeletedQueues.containsKey(SERVER_ONE));
81 assertEquals(1, undeletedQueues.get(SERVER_ONE).size());
82 assertTrue(undeletedQueues.get(SERVER_ONE).contains(ID_ONE));
83
84
85 repQueues.addLog(ID_TWO + "-" + SERVER_TWO, "file2");
86
87 undeletedQueues = cleaner.getUnDeletedQueues();
88 assertEquals(1, undeletedQueues.size());
89 assertTrue(undeletedQueues.containsKey(SERVER_ONE));
90 assertEquals(2, undeletedQueues.get(SERVER_ONE).size());
91 assertTrue(undeletedQueues.get(SERVER_ONE).contains(ID_ONE));
92 assertTrue(undeletedQueues.get(SERVER_ONE).contains(ID_TWO + "-" + SERVER_TWO));
93
94 cleaner.removeQueues(undeletedQueues);
95 undeletedQueues = cleaner.getUnDeletedQueues();
96 assertEquals(0, undeletedQueues.size());
97 }
98
99 @Test
100 public void testReplicationZKNodeCleanerChore() throws Exception {
101 repQueues.init(SERVER_ONE);
102
103 repQueues.addLog(ID_ONE, "file1");
104
105 repQueues.addLog(ID_TWO + "-" + SERVER_TWO, "file2");
106
107
108 Thread.sleep(20000);
109
110 ReplicationZKNodeCleaner cleaner = new ReplicationZKNodeCleaner(conf, zkw, null);
111 assertEquals(0, cleaner.getUnDeletedQueues().size());
112 }
113 }