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
22 import java.io.IOException;
23 import java.net.URLEncoder;
24
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileStatus;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.*;
30 import org.apache.hadoop.hbase.catalog.CatalogTracker;
31 import org.apache.hadoop.hbase.replication.ReplicationFactory;
32 import org.apache.hadoop.hbase.replication.ReplicationQueues;
33 import org.apache.hadoop.hbase.replication.regionserver.Replication;
34 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 @Category(MediumTests.class)
41 public class TestLogsCleaner {
42
43 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44
45
46
47
48 @BeforeClass
49 public static void setUpBeforeClass() throws Exception {
50 TEST_UTIL.startMiniZKCluster();
51 }
52
53
54
55
56 @AfterClass
57 public static void tearDownAfterClass() throws Exception {
58 TEST_UTIL.shutdownMiniZKCluster();
59 }
60
61 @Test
62 public void testLogCleaning() throws Exception{
63 Configuration conf = TEST_UTIL.getConfiguration();
64
65 long ttl = 10000;
66 conf.setLong("hbase.master.logcleaner.ttl", ttl);
67 conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
68 Replication.decorateMasterConfiguration(conf);
69 Server server = new DummyServer();
70 ReplicationQueues repQueues =
71 ReplicationFactory.getReplicationQueues(server.getZooKeeper(), conf, server);
72 repQueues.init(server.getServerName().toString());
73 Path oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
74 HConstants.HREGION_OLDLOGDIR_NAME);
75 String fakeMachineName =
76 URLEncoder.encode(server.getServerName().toString(), "UTF8");
77
78 FileSystem fs = FileSystem.get(conf);
79 LogCleaner cleaner = new LogCleaner(1000, server, conf, fs, oldLogDir);
80
81
82 long now = System.currentTimeMillis();
83 fs.delete(oldLogDir, true);
84 fs.mkdirs(oldLogDir);
85
86 fs.createNewFile(new Path(oldLogDir, "a"));
87 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + "a"));
88
89
90 System.out.println("Now is: " + now);
91 for (int i = 1; i < 31; i++) {
92
93
94 Path fileName = new Path(oldLogDir, fakeMachineName + "." + (now - i) );
95 fs.createNewFile(fileName);
96
97
98
99
100 if (i % (30/3) == 1) {
101 repQueues.addLog(fakeMachineName, fileName.getName());
102 System.out.println("Replication log file: " + fileName);
103 }
104 }
105
106
107 Thread.sleep(ttl);
108 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + now));
109
110
111
112 fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now + 10000) ));
113
114 for (FileStatus stat : fs.listStatus(oldLogDir)) {
115 System.out.println(stat.getPath().toString());
116 }
117
118 assertEquals(34, fs.listStatus(oldLogDir).length);
119
120 cleaner.chore();
121
122
123
124 assertEquals(5, fs.listStatus(oldLogDir).length);
125
126 for (FileStatus file : fs.listStatus(oldLogDir)) {
127 System.out.println("Kept log files: " + file.getPath().getName());
128 }
129 }
130
131 static class DummyServer implements Server {
132
133 @Override
134 public Configuration getConfiguration() {
135 return TEST_UTIL.getConfiguration();
136 }
137
138 @Override
139 public ZooKeeperWatcher getZooKeeper() {
140 try {
141 return new ZooKeeperWatcher(getConfiguration(), "dummy server", this);
142 } catch (IOException e) {
143 e.printStackTrace();
144 }
145 return null;
146 }
147
148 @Override
149 public CatalogTracker getCatalogTracker() {
150 return null;
151 }
152
153 @Override
154 public ServerName getServerName() {
155 return ServerName.valueOf("regionserver,60020,000000");
156 }
157
158 @Override
159 public void abort(String why, Throwable e) {}
160
161 @Override
162 public boolean isAborted() {
163 return false;
164 }
165
166 @Override
167 public void stop(String why) {}
168
169 @Override
170 public boolean isStopped() {
171 return false;
172 }
173 }
174
175 }
176