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
24 import java.io.IOException;
25 import java.util.Random;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FSDataOutputStream;
31 import org.apache.hadoop.fs.FileStatus;
32 import org.apache.hadoop.fs.FileSystem;
33 import org.apache.hadoop.fs.Path;
34 import org.apache.hadoop.hbase.ChoreService;
35 import org.apache.hadoop.hbase.CoordinatedStateManager;
36 import org.apache.hadoop.hbase.HBaseTestingUtility;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.testclassification.MediumTests;
39 import org.apache.hadoop.hbase.Server;
40 import org.apache.hadoop.hbase.ServerName;
41 import org.apache.hadoop.hbase.client.ClusterConnection;
42 import org.apache.hadoop.hbase.util.EnvironmentEdge;
43 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
44 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
45 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
46 import org.junit.AfterClass;
47 import org.junit.Assert;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.experimental.categories.Category;
51
52 @Category(MediumTests.class)
53 public class TestHFileCleaner {
54 private static final Log LOG = LogFactory.getLog(TestHFileCleaner.class);
55
56 private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
57
58 @BeforeClass
59 public static void setupCluster() throws Exception {
60
61 UTIL.startMiniDFSCluster(1);
62 }
63
64 @AfterClass
65 public static void shutdownCluster() throws IOException {
66 UTIL.shutdownMiniDFSCluster();
67 }
68
69 @Test
70 public void testTTLCleaner() throws IOException, InterruptedException {
71 FileSystem fs = UTIL.getDFSCluster().getFileSystem();
72 Path root = UTIL.getDataTestDirOnTestFS();
73 Path file = new Path(root, "file");
74 fs.createNewFile(file);
75 long createTime = System.currentTimeMillis();
76 assertTrue("Test file not created!", fs.exists(file));
77 TimeToLiveHFileCleaner cleaner = new TimeToLiveHFileCleaner();
78
79 fs.setTimes(file, createTime - 100, -1);
80 Configuration conf = UTIL.getConfiguration();
81 conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, 100);
82 cleaner.setConf(conf);
83 assertTrue("File not set deletable - check mod time:" + getFileStats(file, fs)
84 + " with create time:" + createTime, cleaner.isFileDeletable(fs.getFileStatus(file)));
85 }
86
87
88
89
90
91 private String getFileStats(Path file, FileSystem fs) throws IOException {
92 FileStatus status = fs.getFileStatus(file);
93 return "File" + file + ", mtime:" + status.getModificationTime() + ", atime:"
94 + status.getAccessTime();
95 }
96
97 @Test(timeout = 60 *1000)
98 public void testHFileCleaning() throws Exception {
99 final EnvironmentEdge originalEdge = EnvironmentEdgeManager.getDelegate();
100 String prefix = "someHFileThatWouldBeAUUID";
101 Configuration conf = UTIL.getConfiguration();
102
103 long ttl = 2000;
104 conf.set(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS,
105 "org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner");
106 conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, ttl);
107 Server server = new DummyServer();
108 Path archivedHfileDir = new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
109 FileSystem fs = FileSystem.get(conf);
110 HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
111
112
113 final long createTime = System.currentTimeMillis();
114 fs.delete(archivedHfileDir, true);
115 fs.mkdirs(archivedHfileDir);
116
117 fs.createNewFile(new Path(archivedHfileDir, "dfd-dfd"));
118
119
120 LOG.debug("Now is: " + createTime);
121 for (int i = 1; i < 32; i++) {
122
123
124 Path fileName = new Path(archivedHfileDir, (prefix + "." + (createTime + i)));
125 fs.createNewFile(fileName);
126
127 fs.setTimes(fileName, createTime - ttl - 1, -1);
128 LOG.debug("Creating " + getFileStats(fileName, fs));
129 }
130
131
132
133 Path saved = new Path(archivedHfileDir, prefix + ".00000000000");
134 fs.createNewFile(saved);
135
136 fs.setTimes(saved, createTime - ttl / 2, -1);
137 LOG.debug("Creating " + getFileStats(saved, fs));
138 for (FileStatus stat : fs.listStatus(archivedHfileDir)) {
139 LOG.debug(stat.getPath().toString());
140 }
141
142 assertEquals(33, fs.listStatus(archivedHfileDir).length);
143
144
145 EnvironmentEdge setTime = new EnvironmentEdge() {
146 @Override
147 public long currentTime() {
148 return createTime;
149 }
150 };
151 EnvironmentEdgeManager.injectEdge(setTime);
152
153
154 cleaner.chore();
155
156
157 assertEquals(1, fs.listStatus(archivedHfileDir).length);
158
159 for (FileStatus file : fs.listStatus(archivedHfileDir)) {
160 LOG.debug("Kept hfiles: " + file.getPath().getName());
161 }
162
163
164 EnvironmentEdgeManager.injectEdge(originalEdge);
165 }
166
167 @Test
168 public void testRemovesEmptyDirectories() throws Exception {
169 Configuration conf = UTIL.getConfiguration();
170
171 conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
172 Server server = new DummyServer();
173 Path archivedHfileDir = new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
174
175
176 FileSystem fs = UTIL.getDFSCluster().getFileSystem();
177 HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
178
179
180 Path table = new Path(archivedHfileDir, "table");
181 Path region = new Path(table, "regionsomthing");
182 Path family = new Path(region, "fam");
183 Path file = new Path(family, "file12345");
184 fs.mkdirs(family);
185 if (!fs.exists(family)) throw new RuntimeException("Couldn't create test family:" + family);
186 fs.create(file).close();
187 if (!fs.exists(file)) throw new RuntimeException("Test file didn't get created:" + file);
188
189
190 cleaner.chore();
191
192
193 assertFalse("family directory not removed for empty directory", fs.exists(family));
194 assertFalse("region directory not removed for empty directory", fs.exists(region));
195 assertFalse("table directory not removed for empty directory", fs.exists(table));
196 assertTrue("archive directory", fs.exists(archivedHfileDir));
197 }
198
199 static class DummyServer implements Server {
200
201 @Override
202 public Configuration getConfiguration() {
203 return UTIL.getConfiguration();
204 }
205
206 @Override
207 public ZooKeeperWatcher getZooKeeper() {
208 try {
209 return new ZooKeeperWatcher(getConfiguration(), "dummy server", this);
210 } catch (IOException e) {
211 e.printStackTrace();
212 }
213 return null;
214 }
215
216 @Override
217 public CoordinatedStateManager getCoordinatedStateManager() {
218 return null;
219 }
220
221 @Override
222 public ClusterConnection getConnection() {
223 return null;
224 }
225
226 @Override
227 public MetaTableLocator getMetaTableLocator() {
228 return null;
229 }
230
231 @Override
232 public ServerName getServerName() {
233 return ServerName.valueOf("regionserver,60020,000000");
234 }
235
236 @Override
237 public void abort(String why, Throwable e) {
238 }
239
240 @Override
241 public boolean isAborted() {
242 return false;
243 }
244
245 @Override
246 public void stop(String why) {
247 }
248
249 @Override
250 public boolean isStopped() {
251 return false;
252 }
253
254 @Override
255 public ChoreService getChoreService() {
256 return null;
257 }
258 }
259
260 @Test
261 public void testThreadCleanup() throws Exception {
262 Configuration conf = UTIL.getConfiguration();
263 conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
264 Server server = new DummyServer();
265 Path archivedHfileDir =
266 new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
267
268
269 FileSystem fs = UTIL.getDFSCluster().getFileSystem();
270 HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
271
272 fs.delete(archivedHfileDir, true);
273 fs.mkdirs(archivedHfileDir);
274
275 fs.createNewFile(new Path(archivedHfileDir, "dfd-dfd"));
276
277 cleaner.chore();
278
279 cleaner.cleanup();
280
281 Thread.sleep(100);
282 for (Thread thread : cleaner.getCleanerThreads()) {
283 Assert.assertFalse(thread.isAlive());
284 }
285 }
286
287 @Test
288 public void testLargeSmallIsolation() throws Exception {
289 Configuration conf = UTIL.getConfiguration();
290
291 conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
292 conf.setInt(HFileCleaner.HFILE_DELETE_THROTTLE_THRESHOLD, 512 * 1024);
293 Server server = new DummyServer();
294 Path archivedHfileDir =
295 new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
296
297
298 FileSystem fs = UTIL.getDFSCluster().getFileSystem();
299 HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
300
301 fs.delete(archivedHfileDir, true);
302 fs.mkdirs(archivedHfileDir);
303
304 final int LARGE_FILE_NUM = 5;
305 final int SMALL_FILE_NUM = 20;
306 createFilesForTesting(LARGE_FILE_NUM, SMALL_FILE_NUM, fs, archivedHfileDir);
307
308 cleaner.chore();
309
310 Assert.assertEquals(LARGE_FILE_NUM, cleaner.getNumOfDeletedLargeFiles());
311 Assert.assertEquals(SMALL_FILE_NUM, cleaner.getNumOfDeletedSmallFiles());
312 }
313
314 @Test(timeout = 60 * 1000)
315 public void testOnConfigurationChange() throws Exception {
316
317 final int ORIGINAL_THROTTLE_POINT = 512 * 1024;
318 final int ORIGINAL_QUEUE_INIT_SIZE = 512;
319 final int UPDATE_THROTTLE_POINT = 1024;
320 final int UPDATE_QUEUE_INIT_SIZE = 1024;
321 final int LARGE_FILE_NUM = 5;
322 final int SMALL_FILE_NUM = 20;
323
324 Configuration conf = UTIL.getConfiguration();
325
326 conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
327 conf.setInt(HFileCleaner.HFILE_DELETE_THROTTLE_THRESHOLD, ORIGINAL_THROTTLE_POINT);
328 conf.setInt(HFileCleaner.LARGE_HFILE_QUEUE_INIT_SIZE, ORIGINAL_QUEUE_INIT_SIZE);
329 conf.setInt(HFileCleaner.SMALL_HFILE_QUEUE_INIT_SIZE, ORIGINAL_QUEUE_INIT_SIZE);
330 Server server = new DummyServer();
331 Path archivedHfileDir =
332 new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
333
334
335 FileSystem fs = UTIL.getDFSCluster().getFileSystem();
336 final HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
337 Assert.assertEquals(ORIGINAL_THROTTLE_POINT, cleaner.getThrottlePoint());
338 Assert.assertEquals(ORIGINAL_QUEUE_INIT_SIZE, cleaner.getLargeQueueInitSize());
339 Assert.assertEquals(ORIGINAL_QUEUE_INIT_SIZE, cleaner.getSmallQueueInitSize());
340
341
342 fs.delete(archivedHfileDir, true);
343 fs.mkdirs(archivedHfileDir);
344 createFilesForTesting(LARGE_FILE_NUM, SMALL_FILE_NUM, fs, archivedHfileDir);
345
346
347 Thread t = new Thread() {
348 @Override
349 public void run() {
350 cleaner.chore();
351 }
352 };
353 t.setDaemon(true);
354 t.start();
355
356 while (cleaner.getNumOfDeletedSmallFiles() == 0) {
357 Thread.yield();
358 }
359
360
361 Configuration newConf = new Configuration(conf);
362 newConf.setInt(HFileCleaner.HFILE_DELETE_THROTTLE_THRESHOLD, UPDATE_THROTTLE_POINT);
363 newConf.setInt(HFileCleaner.LARGE_HFILE_QUEUE_INIT_SIZE, UPDATE_QUEUE_INIT_SIZE);
364 newConf.setInt(HFileCleaner.SMALL_HFILE_QUEUE_INIT_SIZE, UPDATE_QUEUE_INIT_SIZE);
365 LOG.debug("File deleted from large queue: " + cleaner.getNumOfDeletedLargeFiles()
366 + "; from small queue: " + cleaner.getNumOfDeletedSmallFiles());
367 cleaner.onConfigurationChange(newConf);
368
369
370 Assert.assertEquals(UPDATE_THROTTLE_POINT, cleaner.getThrottlePoint());
371 Assert.assertEquals(UPDATE_QUEUE_INIT_SIZE, cleaner.getLargeQueueInitSize());
372 Assert.assertEquals(UPDATE_QUEUE_INIT_SIZE, cleaner.getSmallQueueInitSize());
373 Assert.assertEquals(2, cleaner.getCleanerThreads().size());
374
375
376 t.join();
377 LOG.debug("File deleted from large queue: " + cleaner.getNumOfDeletedLargeFiles()
378 + "; from small queue: " + cleaner.getNumOfDeletedSmallFiles());
379 Assert.assertTrue("Should delete more than " + LARGE_FILE_NUM
380 + " files from large queue but actually " + cleaner.getNumOfDeletedLargeFiles(),
381 cleaner.getNumOfDeletedLargeFiles() > LARGE_FILE_NUM);
382 Assert.assertTrue("Should delete less than " + SMALL_FILE_NUM
383 + " files from small queue but actually " + cleaner.getNumOfDeletedSmallFiles(),
384 cleaner.getNumOfDeletedSmallFiles() < SMALL_FILE_NUM);
385 }
386
387 private void createFilesForTesting(int largeFileNum, int smallFileNum, FileSystem fs,
388 Path archivedHfileDir) throws IOException {
389 final Random rand = new Random();
390 final byte[] large = new byte[1024 * 1024];
391 for (int i = 0; i < large.length; i++) {
392 large[i] = (byte) rand.nextInt(128);
393 }
394 final byte[] small = new byte[1024];
395 for (int i = 0; i < small.length; i++) {
396 small[i] = (byte) rand.nextInt(128);
397 }
398
399 for (int i = 1; i <= largeFileNum; i++) {
400 FSDataOutputStream out = fs.create(new Path(archivedHfileDir, "large-file-" + i));
401 out.write(large);
402 out.close();
403 }
404 for (int i = 1; i <= smallFileNum; i++) {
405 FSDataOutputStream out = fs.create(new Path(archivedHfileDir, "small-file-" + i));
406 out.write(small);
407 out.close();
408 }
409 }
410 }