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