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