1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master;
21
22 import static org.junit.Assert.assertEquals;
23
24 import org.apache.hadoop.fs.FileStatus;
25 import org.junit.After;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Ignore;
30 import org.junit.Test;
31
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.fs.FileSystem;
36 import org.apache.hadoop.conf.Configuration;
37
38 import java.net.URLEncoder;
39 import java.util.concurrent.atomic.AtomicBoolean;
40
41 public class TestOldLogsCleaner {
42
43 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44
45
46
47
48
49 @BeforeClass
50 public static void setUpBeforeClass() throws Exception {
51 }
52
53
54
55
56 @AfterClass
57 public static void tearDownAfterClass() throws Exception {
58 }
59
60
61
62
63 @Before
64 public void setUp() throws Exception {
65 }
66
67
68
69
70 @After
71 public void tearDown() throws Exception {
72 }
73
74 @Test
75 public void testLogCleaning() throws Exception{
76 Configuration c = TEST_UTIL.getConfiguration();
77 Path oldLogDir = new Path(TEST_UTIL.getTestDir(),
78 HConstants.HREGION_OLDLOGDIR_NAME);
79 String fakeMachineName = URLEncoder.encode("regionserver:60020", "UTF8");
80
81 FileSystem fs = FileSystem.get(c);
82 AtomicBoolean stop = new AtomicBoolean(false);
83 OldLogsCleaner cleaner = new OldLogsCleaner(1000, stop,c, fs, oldLogDir);
84
85
86 long now = System.currentTimeMillis();
87 fs.delete(oldLogDir, true);
88 fs.mkdirs(oldLogDir);
89 fs.createNewFile(new Path(oldLogDir, "a"));
90 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + "a"));
91 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + now));
92 System.out.println("Now is: " + now);
93 for (int i = 0; i < 30; i++) {
94 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now - 6000000 - i) ));
95 }
96 for (FileStatus stat : fs.listStatus(oldLogDir)) {
97 System.out.println(stat.getPath().toString());
98 }
99
100 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now + 10000) ));
101
102 assertEquals(34, fs.listStatus(oldLogDir).length);
103
104
105 cleaner.chore();
106
107 assertEquals(14, fs.listStatus(oldLogDir).length);
108
109
110 cleaner.chore();
111
112
113 assertEquals(2, fs.listStatus(oldLogDir).length);
114 }
115
116 }