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 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FileStatus;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.ChoreService;
31 import org.apache.hadoop.hbase.CoordinatedStateManager;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HRegionInfo;
34 import org.apache.hadoop.hbase.Server;
35 import org.apache.hadoop.hbase.ServerName;
36 import org.apache.hadoop.hbase.TableName;
37 import org.apache.hadoop.hbase.client.ClusterConnection;
38 import org.apache.hadoop.hbase.io.HFileLink;
39 import org.apache.hadoop.hbase.testclassification.SmallTests;
40 import org.apache.hadoop.hbase.util.FSUtils;
41 import org.apache.hadoop.hbase.util.HFileArchiveUtil;
42 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
43 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47
48
49
50
51 @Category(SmallTests.class)
52 public class TestHFileLinkCleaner {
53
54 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55
56 @Test
57 public void testHFileLinkCleaning() throws Exception {
58 Configuration conf = TEST_UTIL.getConfiguration();
59 FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
60 conf.set(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, HFileLinkCleaner.class.getName());
61 Path rootDir = FSUtils.getRootDir(conf);
62 FileSystem fs = FileSystem.get(conf);
63
64 final TableName tableName = TableName.valueOf("test-table");
65 final TableName tableLinkName = TableName.valueOf("test-link");
66 final String hfileName = "1234567890";
67 final String familyName = "cf";
68
69 HRegionInfo hri = new HRegionInfo(tableName);
70 HRegionInfo hriLink = new HRegionInfo(tableLinkName);
71
72 Path archiveDir = HFileArchiveUtil.getArchivePath(conf);
73 Path archiveStoreDir = HFileArchiveUtil.getStoreArchivePath(conf,
74 tableName, hri.getEncodedName(), familyName);
75 Path archiveLinkStoreDir = HFileArchiveUtil.getStoreArchivePath(conf,
76 tableLinkName, hriLink.getEncodedName(), familyName);
77
78
79 Path familyPath = getFamilyDirPath(archiveDir, tableName, hri.getEncodedName(), familyName);
80 fs.mkdirs(familyPath);
81 Path hfilePath = new Path(familyPath, hfileName);
82 fs.createNewFile(hfilePath);
83
84
85 Path familyLinkPath = getFamilyDirPath(rootDir, tableLinkName,
86 hriLink.getEncodedName(), familyName);
87 fs.mkdirs(familyLinkPath);
88 HFileLink.create(conf, fs, familyLinkPath, hri, hfileName);
89 Path linkBackRefDir = HFileLink.getBackReferencesDir(archiveStoreDir, hfileName);
90 assertTrue(fs.exists(linkBackRefDir));
91 FileStatus[] backRefs = fs.listStatus(linkBackRefDir);
92 assertEquals(1, backRefs.length);
93 Path linkBackRef = backRefs[0].getPath();
94
95
96 final long ttl = 1000;
97 conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, ttl);
98 Server server = new DummyServer();
99 CleanerChore.initChorePool(conf);
100 HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archiveDir);
101
102
103 cleaner.chore();
104 assertTrue(fs.exists(linkBackRef));
105 assertTrue(fs.exists(hfilePath));
106
107
108 fs.rename(FSUtils.getTableDir(rootDir, tableLinkName),
109 FSUtils.getTableDir(archiveDir, tableLinkName));
110 cleaner.chore();
111 assertFalse("Link should be deleted", fs.exists(linkBackRef));
112
113
114 Thread.sleep(ttl * 2);
115 cleaner.chore();
116 assertFalse("HFile should be deleted", fs.exists(hfilePath));
117
118
119 for (int i = 0; i < 4; ++i) {
120 Thread.sleep(ttl * 2);
121 cleaner.chore();
122 }
123 assertFalse("HFile should be deleted", fs.exists(FSUtils.getTableDir(archiveDir, tableName)));
124 assertFalse("Link should be deleted", fs.exists(FSUtils.getTableDir(archiveDir, tableLinkName)));
125 }
126
127 private static Path getFamilyDirPath (final Path rootDir, final TableName table,
128 final String region, final String family) {
129 return new Path(new Path(FSUtils.getTableDir(rootDir, table), region), family);
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 CoordinatedStateManager getCoordinatedStateManager() {
151 return null;
152 }
153
154 @Override
155 public ClusterConnection getConnection() {
156 return null;
157 }
158
159 @Override
160 public MetaTableLocator getMetaTableLocator() {
161 return null;
162 }
163
164 @Override
165 public ServerName getServerName() {
166 return ServerName.valueOf("regionserver,60020,000000");
167 }
168
169 @Override
170 public void abort(String why, Throwable e) {}
171
172 @Override
173 public boolean isAborted() {
174 return false;
175 }
176
177 @Override
178 public void stop(String why) {}
179
180 @Override
181 public boolean isStopped() {
182 return false;
183 }
184
185 @Override
186 public ChoreService getChoreService() {
187 return null;
188 }
189 }
190 }