1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.cleaner;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.doThrow;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.doAnswer;
26
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.net.URLEncoder;
30 import java.util.Iterator;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Random;
34 import java.util.concurrent.atomic.AtomicBoolean;
35
36 import com.google.common.collect.Lists;
37 import org.apache.hadoop.conf.Configuration;
38 import org.apache.hadoop.fs.FSDataOutputStream;
39 import org.apache.hadoop.fs.FileStatus;
40 import org.apache.hadoop.fs.FileSystem;
41 import org.apache.hadoop.fs.Path;
42 import org.apache.hadoop.hbase.Abortable;
43 import org.apache.hadoop.hbase.ChoreService;
44 import org.apache.hadoop.hbase.CoordinatedStateManager;
45 import org.apache.hadoop.hbase.HBaseTestingUtility;
46 import org.apache.hadoop.hbase.HConstants;
47 import org.apache.hadoop.hbase.testclassification.MediumTests;
48 import org.apache.hadoop.hbase.Server;
49 import org.apache.hadoop.hbase.ServerName;
50 import org.apache.hadoop.hbase.Waiter;
51 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
52 import org.apache.hadoop.hbase.client.ClusterConnection;
53 import org.apache.hadoop.hbase.replication.ReplicationFactory;
54 import org.apache.hadoop.hbase.replication.ReplicationQueues;
55 import org.apache.hadoop.hbase.replication.ReplicationQueuesClient;
56 import org.apache.hadoop.hbase.replication.master.ReplicationLogCleaner;
57 import org.apache.hadoop.hbase.replication.regionserver.Replication;
58 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
59 import org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper;
60 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
61 import org.apache.zookeeper.KeeperException;
62 import org.junit.AfterClass;
63 import org.junit.BeforeClass;
64 import org.junit.Test;
65 import org.junit.experimental.categories.Category;
66 import org.mockito.Mockito;
67 import org.mockito.invocation.InvocationOnMock;
68 import org.mockito.stubbing.Answer;
69
70 @Category(MediumTests.class)
71 public class TestLogsCleaner {
72
73 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
74 private static DirScanPool POOL;
75
76
77
78
79 @BeforeClass
80 public static void setUpBeforeClass() throws Exception {
81 TEST_UTIL.startMiniZKCluster();
82 TEST_UTIL.startMiniDFSCluster(1);
83 POOL = new DirScanPool(TEST_UTIL.getConfiguration());
84 }
85
86
87
88
89 @AfterClass
90 public static void tearDownAfterClass() throws Exception {
91 TEST_UTIL.shutdownMiniZKCluster();
92 TEST_UTIL.shutdownMiniDFSCluster();
93 POOL.shutdownNow();
94 }
95
96 @Test
97 public void testLogCleaning() throws Exception{
98 Configuration conf = TEST_UTIL.getConfiguration();
99
100 long ttl = 10000;
101 conf.setLong("hbase.master.logcleaner.ttl", ttl);
102 conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
103 Replication.decorateMasterConfiguration(conf);
104 Server server = new DummyServer();
105 ReplicationQueues repQueues =
106 ReplicationFactory.getReplicationQueues(server.getZooKeeper(), conf, server);
107 repQueues.init(server.getServerName().toString());
108 final Path oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
109 HConstants.HREGION_OLDLOGDIR_NAME);
110 String fakeMachineName =
111 URLEncoder.encode(server.getServerName().toString(), "UTF8");
112
113 final FileSystem fs = FileSystem.get(conf);
114
115
116 long now = System.currentTimeMillis();
117 fs.delete(oldLogDir, true);
118 fs.mkdirs(oldLogDir);
119
120 fs.createNewFile(new Path(oldLogDir, "a"));
121 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + "a"));
122
123
124 System.out.println("Now is: " + now);
125 for (int i = 1; i < 31; i++) {
126
127
128 Path fileName = new Path(oldLogDir, fakeMachineName + "." + (now - i) );
129 fs.createNewFile(fileName);
130
131
132
133
134 if (i % (30/3) == 1) {
135 repQueues.addLog(fakeMachineName, fileName.getName());
136 System.out.println("Replication log file: " + fileName);
137 }
138 }
139
140
141 Thread.sleep(ttl);
142 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + now));
143
144
145
146 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now + 10000) ));
147
148 for (FileStatus stat : fs.listStatus(oldLogDir)) {
149 System.out.println(stat.getPath().toString());
150 }
151
152 assertEquals(34, fs.listStatus(oldLogDir).length);
153
154 LogCleaner cleaner = new LogCleaner(1000, server, conf, fs, oldLogDir, POOL);
155
156 cleaner.chore();
157
158
159
160 TEST_UTIL.waitFor(1000, new Waiter.Predicate<Exception>() {
161 @Override
162 public boolean evaluate() throws Exception {
163 return 5 == fs.listStatus(oldLogDir).length;
164 }
165 });
166
167 for (FileStatus file : fs.listStatus(oldLogDir)) {
168 System.out.println("Kept log files: " + file.getPath().getName());
169 }
170 }
171
172 @Test(timeout=5000)
173 public void testZnodeCversionChange() throws Exception {
174 Configuration conf = TEST_UTIL.getConfiguration();
175 ReplicationLogCleaner cleaner = new ReplicationLogCleaner();
176 cleaner.setConf(conf);
177
178 ReplicationQueuesClient rqcMock = Mockito.mock(ReplicationQueuesClient.class);
179 Mockito.when(rqcMock.getQueuesZNodeCversion()).thenReturn(1, 2, 3, 4);
180
181 Field rqc = ReplicationLogCleaner.class.getDeclaredField("replicationQueues");
182 rqc.setAccessible(true);
183
184 rqc.set(cleaner, rqcMock);
185
186
187 cleaner.getDeletableFiles(new LinkedList<FileStatus>());
188 }
189
190 @Test(timeout=10000)
191 public void testZooKeeperAbortDuringGetListOfReplicators() throws Exception {
192 Configuration conf = TEST_UTIL.getConfiguration();
193
194 ReplicationLogCleaner cleaner = new ReplicationLogCleaner();
195
196 List<FileStatus> dummyFiles = Lists.newArrayList(
197 new FileStatus(100, false, 3, 100, System.currentTimeMillis(), new Path("log1")),
198 new FileStatus(100, false, 3, 100, System.currentTimeMillis(), new Path("log2"))
199 );
200
201 FaultyZooKeeperWatcher faultyZK =
202 new FaultyZooKeeperWatcher(conf, "testZooKeeperAbort-faulty", null);
203 final AtomicBoolean getListOfReplicatorsFailed = new AtomicBoolean(false);
204
205 try {
206 faultyZK.init();
207 ReplicationQueuesClient replicationQueuesClient = spy(ReplicationFactory.getReplicationQueuesClient(
208 faultyZK, conf, new ReplicationLogCleaner.WarnOnlyAbortable()));
209 doAnswer(new Answer<Object>() {
210 @Override
211 public Object answer(InvocationOnMock invocation) throws Throwable {
212 try {
213 return invocation.callRealMethod();
214 } catch (KeeperException.ConnectionLossException e) {
215 getListOfReplicatorsFailed.set(true);
216 throw e;
217 }
218 }
219 }).when(replicationQueuesClient).getListOfReplicators();
220 replicationQueuesClient.init();
221
222 cleaner.setConf(conf, faultyZK, replicationQueuesClient);
223
224 Iterable<FileStatus> toDelete = cleaner.getDeletableFiles(dummyFiles);
225
226 assertTrue(getListOfReplicatorsFailed.get());
227 assertFalse(toDelete.iterator().hasNext());
228 assertFalse(cleaner.isStopped());
229 } finally {
230 faultyZK.close();
231 }
232 }
233
234
235
236
237
238 @Test(timeout=10000)
239 public void testZooKeeperNormal() throws Exception {
240 Configuration conf = TEST_UTIL.getConfiguration();
241 ReplicationLogCleaner cleaner = new ReplicationLogCleaner();
242
243 List<FileStatus> dummyFiles = Lists.newArrayList(
244 new FileStatus(100, false, 3, 100, System.currentTimeMillis(), new Path("log1")),
245 new FileStatus(100, false, 3, 100, System.currentTimeMillis(), new Path("log2"))
246 );
247
248 ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf, "testZooKeeperAbort-normal", null);
249 try {
250 cleaner.setConf(conf, zkw);
251 Iterable<FileStatus> filesToDelete = cleaner.getDeletableFiles(dummyFiles);
252 Iterator<FileStatus> iter = filesToDelete.iterator();
253 assertTrue(iter.hasNext());
254 assertEquals(new Path("log1"), iter.next().getPath());
255 assertTrue(iter.hasNext());
256 assertEquals(new Path("log2"), iter.next().getPath());
257 assertFalse(iter.hasNext());
258 } finally {
259 zkw.close();
260 }
261 }
262
263 @Test
264 public void testOnConfigurationChange() throws Exception {
265 Configuration conf = TEST_UTIL.getConfiguration();
266 conf.setInt(LogCleaner.OLD_WALS_CLEANER_THREAD_SIZE,
267 LogCleaner.DEFAULT_OLD_WALS_CLEANER_THREAD_SIZE);
268 conf.setLong(LogCleaner.OLD_WALS_CLEANER_THREAD_TIMEOUT_MSEC,
269 LogCleaner.DEFAULT_OLD_WALS_CLEANER_THREAD_TIMEOUT_MSEC);
270 conf.setLong(LogCleaner.OLD_WALS_CLEANER_THREAD_CHECK_INTERVAL_MSEC,
271 LogCleaner.DEFAULT_OLD_WALS_CLEANER_THREAD_CHECK_INTERVAL_MSEC);
272
273 Server server = new DummyServer();
274 Path oldWALsDir = new Path(TEST_UTIL.getDefaultRootDirPath(),
275 HConstants.HREGION_OLDLOGDIR_NAME);
276 FileSystem fs = TEST_UTIL.getDFSCluster().getFileSystem();
277 final LogCleaner cleaner = new LogCleaner(3000, server, conf, fs, oldWALsDir, POOL);
278 assertEquals(LogCleaner.DEFAULT_OLD_WALS_CLEANER_THREAD_SIZE, cleaner.getSizeOfCleaners());
279 assertEquals(LogCleaner.DEFAULT_OLD_WALS_CLEANER_THREAD_TIMEOUT_MSEC,
280 cleaner.getCleanerThreadTimeoutMsec());
281 assertEquals(LogCleaner.DEFAULT_OLD_WALS_CLEANER_THREAD_CHECK_INTERVAL_MSEC,
282 cleaner.getCleanerThreadCheckIntervalMsec());
283
284 fs.delete(oldWALsDir, true);
285 fs.mkdirs(oldWALsDir);
286 int numOfFiles = 10;
287 createFiles(fs, oldWALsDir, numOfFiles);
288 FileStatus[] status = fs.listStatus(oldWALsDir);
289 assertEquals(numOfFiles, status.length);
290
291 Thread thread = new Thread(new Runnable() {
292 @Override
293 public void run() {
294 cleaner.chore();
295 }
296 });
297 thread.setDaemon(true);
298 thread.start();
299
300 int sizeToChange = 4;
301 long threadTimeoutToChange = 30 * 1000L;
302 long threadCheckIntervalToChange = 250L;
303 conf.setInt(LogCleaner.OLD_WALS_CLEANER_THREAD_SIZE, sizeToChange);
304 conf.setLong(LogCleaner.OLD_WALS_CLEANER_THREAD_TIMEOUT_MSEC, threadTimeoutToChange);
305 conf.setLong(LogCleaner.OLD_WALS_CLEANER_THREAD_CHECK_INTERVAL_MSEC,
306 threadCheckIntervalToChange);
307 cleaner.onConfigurationChange(conf);
308 assertEquals(sizeToChange, cleaner.getSizeOfCleaners());
309 assertEquals(threadTimeoutToChange, cleaner.getCleanerThreadTimeoutMsec());
310 assertEquals(threadCheckIntervalToChange, cleaner.getCleanerThreadCheckIntervalMsec());
311
312 thread.join();
313 status = fs.listStatus(oldWALsDir);
314 assertEquals(0, status.length);
315 }
316
317 private void createFiles(FileSystem fs, Path parentDir, int numOfFiles) throws IOException {
318 Random random = new Random();
319 for (int i = 0; i < numOfFiles; i++) {
320 int xMega = 1 + random.nextInt(3);
321 try (FSDataOutputStream fsdos = fs.create(new Path(parentDir, "file-" + i))) {
322 for (int m = 0; m < xMega; m++) {
323 byte[] M = new byte[1024 * 1024];
324 random.nextBytes(M);
325 fsdos.write(M);
326 }
327 }
328 }
329 }
330
331 static class DummyServer implements Server {
332
333 @Override
334 public Configuration getConfiguration() {
335 return TEST_UTIL.getConfiguration();
336 }
337
338 @Override
339 public ZooKeeperWatcher getZooKeeper() {
340 try {
341 return new ZooKeeperWatcher(getConfiguration(), "dummy server", this);
342 } catch (IOException e) {
343 e.printStackTrace();
344 }
345 return null;
346 }
347
348 @Override
349 public CoordinatedStateManager getCoordinatedStateManager() {
350 return null;
351 }
352
353 @Override
354 public ClusterConnection getConnection() {
355 return null;
356 }
357
358 @Override
359 public MetaTableLocator getMetaTableLocator() {
360 return null;
361 }
362
363 @Override
364 public ServerName getServerName() {
365 return ServerName.valueOf("regionserver,60020,000000");
366 }
367
368 @Override
369 public void abort(String why, Throwable e) {}
370
371 @Override
372 public boolean isAborted() {
373 return false;
374 }
375
376 @Override
377 public void stop(String why) {}
378
379 @Override
380 public boolean isStopped() {
381 return false;
382 }
383
384 @Override
385 public ChoreService getChoreService() {
386 return null;
387 }
388 }
389
390 static class FaultyZooKeeperWatcher extends ZooKeeperWatcher {
391 private RecoverableZooKeeper zk;
392
393 public FaultyZooKeeperWatcher(Configuration conf, String identifier, Abortable abortable)
394 throws ZooKeeperConnectionException, IOException {
395 super(conf, identifier, abortable);
396 }
397
398 public void init() throws Exception {
399 this.zk = spy(super.getRecoverableZooKeeper());
400 doThrow(new KeeperException.ConnectionLossException())
401 .when(zk).getChildren("/hbase/replication/rs", null);
402 }
403
404 public RecoverableZooKeeper getRecoverableZooKeeper() {
405 return zk;
406 }
407 }
408 }