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 */ 018 019package org.apache.hadoop.hbase.master.cleaner; 020 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertTrue; 023 024import java.util.List; 025import java.util.Map; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.replication.ReplicationFactory; 030import org.apache.hadoop.hbase.replication.ReplicationQueues; 031import org.apache.hadoop.hbase.replication.ReplicationQueuesArguments; 032import org.apache.hadoop.hbase.replication.ReplicationQueuesZKImpl; 033import org.apache.hadoop.hbase.testclassification.MasterTests; 034import org.apache.hadoop.hbase.testclassification.MediumTests; 035import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 036import org.junit.AfterClass; 037import org.junit.BeforeClass; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041 042@Category({ MasterTests.class, MediumTests.class }) 043public class TestReplicationZKNodeCleaner { 044 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestReplicationZKNodeCleaner.class); 048 049 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 050 051 private final String ID_ONE = "1"; 052 private final String SERVER_ONE = "server1"; 053 private final String ID_TWO = "2"; 054 private final String SERVER_TWO = "server2"; 055 056 private final Configuration conf; 057 private final ZKWatcher zkw; 058 private final ReplicationQueues repQueues; 059 060 public TestReplicationZKNodeCleaner() throws Exception { 061 conf = TEST_UTIL.getConfiguration(); 062 zkw = new ZKWatcher(conf, "TestReplicationZKNodeCleaner", null); 063 repQueues = ReplicationFactory.getReplicationQueues(new ReplicationQueuesArguments(conf, null, 064 zkw)); 065 assertTrue(repQueues instanceof ReplicationQueuesZKImpl); 066 } 067 068 @BeforeClass 069 public static void setUpBeforeClass() throws Exception { 070 TEST_UTIL.getConfiguration().setInt("hbase.master.cleaner.interval", 10000); 071 TEST_UTIL.startMiniCluster(); 072 } 073 074 @AfterClass 075 public static void tearDownAfterClass() throws Exception { 076 TEST_UTIL.shutdownMiniCluster(); 077 } 078 079 @Test 080 public void testReplicationZKNodeCleaner() throws Exception { 081 repQueues.init(SERVER_ONE); 082 // add queue for ID_ONE which isn't exist 083 repQueues.addLog(ID_ONE, "file1"); 084 085 ReplicationZKNodeCleaner cleaner = new ReplicationZKNodeCleaner(conf, zkw, null); 086 Map<String, List<String>> undeletedQueues = cleaner.getUnDeletedQueues(); 087 assertEquals(1, undeletedQueues.size()); 088 assertTrue(undeletedQueues.containsKey(SERVER_ONE)); 089 assertEquals(1, undeletedQueues.get(SERVER_ONE).size()); 090 assertTrue(undeletedQueues.get(SERVER_ONE).contains(ID_ONE)); 091 092 // add a recovery queue for ID_TWO which isn't exist 093 repQueues.addLog(ID_TWO + "-" + SERVER_TWO, "file2"); 094 095 undeletedQueues = cleaner.getUnDeletedQueues(); 096 assertEquals(1, undeletedQueues.size()); 097 assertTrue(undeletedQueues.containsKey(SERVER_ONE)); 098 assertEquals(2, undeletedQueues.get(SERVER_ONE).size()); 099 assertTrue(undeletedQueues.get(SERVER_ONE).contains(ID_ONE)); 100 assertTrue(undeletedQueues.get(SERVER_ONE).contains(ID_TWO + "-" + SERVER_TWO)); 101 102 cleaner.removeQueues(undeletedQueues); 103 undeletedQueues = cleaner.getUnDeletedQueues(); 104 assertEquals(0, undeletedQueues.size()); 105 } 106 107 @Test 108 public void testReplicationZKNodeCleanerChore() throws Exception { 109 repQueues.init(SERVER_ONE); 110 // add queue for ID_ONE which isn't exist 111 repQueues.addLog(ID_ONE, "file1"); 112 // add a recovery queue for ID_TWO which isn't exist 113 repQueues.addLog(ID_TWO + "-" + SERVER_TWO, "file2"); 114 115 // Wait the cleaner chore to run 116 Thread.sleep(20000); 117 118 ReplicationZKNodeCleaner cleaner = new ReplicationZKNodeCleaner(conf, zkw, null); 119 assertEquals(0, cleaner.getUnDeletedQueues().size()); 120 } 121}