View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
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      // add queue for ID_ONE which isn't exist
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      // add a recovery queue for ID_TWO which isn't exist
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     // add queue for ID_ONE which isn't exist
103     repQueues.addLog(ID_ONE, "file1");
104     // add a recovery queue for ID_TWO which isn't exist
105     repQueues.addLog(ID_TWO + "-" + SERVER_TWO, "file2");
106 
107     // Wait the cleaner chore to run
108     Thread.sleep(20000);
109 
110     ReplicationZKNodeCleaner cleaner = new ReplicationZKNodeCleaner(conf, zkw, null);
111     assertEquals(0, cleaner.getUnDeletedQueues().size());
112   }
113 }