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.Test;
30
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.regionserver.HRegionServer;
34 import org.apache.hadoop.hbase.replication.ReplicationZookeeperWrapper;
35 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper;
36 import org.apache.hadoop.fs.Path;
37 import org.apache.hadoop.fs.FileSystem;
38 import org.apache.hadoop.conf.Configuration;
39
40 import java.net.URLEncoder;
41 import java.util.concurrent.atomic.AtomicBoolean;
42
43 public class TestLogsCleaner {
44
45 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46
47 private ReplicationZookeeperWrapper zkHelper;
48
49
50
51
52 @BeforeClass
53 public static void setUpBeforeClass() throws Exception {
54 TEST_UTIL.startMiniZKCluster();
55 }
56
57
58
59
60 @AfterClass
61 public static void tearDownAfterClass() throws Exception {
62 TEST_UTIL.shutdownMiniZKCluster();
63 }
64
65
66
67
68 @Before
69 public void setUp() throws Exception {
70 Configuration conf = TEST_UTIL.getConfiguration();
71 zkHelper = new ReplicationZookeeperWrapper(
72 ZooKeeperWrapper.createInstance(conf, HRegionServer.class.getName()),
73 conf, new AtomicBoolean(true), "test-cluster");
74 }
75
76
77
78
79 @After
80 public void tearDown() throws Exception {
81 }
82
83 @Test
84 public void testLogCleaning() throws Exception{
85 Configuration c = TEST_UTIL.getConfiguration();
86 Path oldLogDir = new Path(TEST_UTIL.getTestDir(),
87 HConstants.HREGION_OLDLOGDIR_NAME);
88 String fakeMachineName = URLEncoder.encode("regionserver:60020", "UTF8");
89
90 FileSystem fs = FileSystem.get(c);
91 AtomicBoolean stop = new AtomicBoolean(false);
92 LogsCleaner cleaner = new LogsCleaner(1000, stop,c, fs, oldLogDir);
93
94
95 long now = System.currentTimeMillis();
96 fs.delete(oldLogDir, true);
97 fs.mkdirs(oldLogDir);
98
99 fs.createNewFile(new Path(oldLogDir, "a"));
100 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + "a"));
101
102
103 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + now));
104 System.out.println("Now is: " + now);
105 for (int i = 0; i < 30; i++) {
106
107
108 Path fileName = new Path(oldLogDir, fakeMachineName + "." +
109 (now - 6000000 - i) );
110 fs.createNewFile(fileName);
111
112
113
114
115 if (i % (30/3) == 0) {
116 zkHelper.addLogToList(fileName.getName(), fakeMachineName);
117 System.out.println("Replication log file: " + fileName);
118 }
119 }
120 for (FileStatus stat : fs.listStatus(oldLogDir)) {
121 System.out.println(stat.getPath().toString());
122 }
123
124
125
126 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now + 10000) ));
127
128 assertEquals(34, fs.listStatus(oldLogDir).length);
129
130
131 cleaner.chore();
132
133 assertEquals(14, fs.listStatus(oldLogDir).length);
134
135
136
137 cleaner.chore();
138
139
140
141 assertEquals(5, fs.listStatus(oldLogDir).length);
142
143 for (FileStatus file : fs.listStatus(oldLogDir)) {
144 System.out.println("Keeped log files: " + file.getPath().getName());
145 }
146 }
147
148 }