View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  import java.util.Random;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FSDataOutputStream;
31  import org.apache.hadoop.fs.FileStatus;
32  import org.apache.hadoop.fs.FileSystem;
33  import org.apache.hadoop.fs.Path;
34  import org.apache.hadoop.hbase.ChoreService;
35  import org.apache.hadoop.hbase.CoordinatedStateManager;
36  import org.apache.hadoop.hbase.HBaseTestingUtility;
37  import org.apache.hadoop.hbase.HConstants;
38  import org.apache.hadoop.hbase.testclassification.MediumTests;
39  import org.apache.hadoop.hbase.Server;
40  import org.apache.hadoop.hbase.ServerName;
41  import org.apache.hadoop.hbase.client.ClusterConnection;
42  import org.apache.hadoop.hbase.util.EnvironmentEdge;
43  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
44  import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
45  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
46  import org.junit.AfterClass;
47  import org.junit.Assert;
48  import org.junit.BeforeClass;
49  import org.junit.Test;
50  import org.junit.experimental.categories.Category;
51  
52  @Category(MediumTests.class)
53  public class TestHFileCleaner {
54    private static final Log LOG = LogFactory.getLog(TestHFileCleaner.class);
55  
56    private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
57  
58    @BeforeClass
59    public static void setupCluster() throws Exception {
60      // have to use a minidfs cluster because the localfs doesn't modify file times correctly
61      UTIL.startMiniDFSCluster(1);
62    }
63  
64    @AfterClass
65    public static void shutdownCluster() throws IOException {
66      UTIL.shutdownMiniDFSCluster();
67    }
68  
69    @Test
70    public void testTTLCleaner() throws IOException, InterruptedException {
71      FileSystem fs = UTIL.getDFSCluster().getFileSystem();
72      Path root = UTIL.getDataTestDirOnTestFS();
73      Path file = new Path(root, "file");
74      fs.createNewFile(file);
75      long createTime = System.currentTimeMillis();
76      assertTrue("Test file not created!", fs.exists(file));
77      TimeToLiveHFileCleaner cleaner = new TimeToLiveHFileCleaner();
78      // update the time info for the file, so the cleaner removes it
79      fs.setTimes(file, createTime - 100, -1);
80      Configuration conf = UTIL.getConfiguration();
81      conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, 100);
82      cleaner.setConf(conf);
83      assertTrue("File not set deletable - check mod time:" + getFileStats(file, fs)
84          + " with create time:" + createTime, cleaner.isFileDeletable(fs.getFileStatus(file)));
85    }
86  
87    /**
88     * @param file to check
89     * @return loggable information about the file
90     */
91    private String getFileStats(Path file, FileSystem fs) throws IOException {
92      FileStatus status = fs.getFileStatus(file);
93      return "File" + file + ", mtime:" + status.getModificationTime() + ", atime:"
94          + status.getAccessTime();
95    }
96  
97    @Test(timeout = 60 *1000)
98    public void testHFileCleaning() throws Exception {
99      final EnvironmentEdge originalEdge = EnvironmentEdgeManager.getDelegate();
100     String prefix = "someHFileThatWouldBeAUUID";
101     Configuration conf = UTIL.getConfiguration();
102     // set TTL
103     long ttl = 2000;
104     conf.set(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS,
105       "org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner");
106     conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, ttl);
107     Server server = new DummyServer();
108     Path archivedHfileDir = new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
109     FileSystem fs = FileSystem.get(conf);
110     HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
111 
112     // Create 2 invalid files, 1 "recent" file, 1 very new file and 30 old files
113     final long createTime = System.currentTimeMillis();
114     fs.delete(archivedHfileDir, true);
115     fs.mkdirs(archivedHfileDir);
116     // Case 1: 1 invalid file, which should be deleted directly
117     fs.createNewFile(new Path(archivedHfileDir, "dfd-dfd"));
118     // Case 2: 1 "recent" file, not even deletable for the first log cleaner
119     // (TimeToLiveLogCleaner), so we are not going down the chain
120     LOG.debug("Now is: " + createTime);
121     for (int i = 1; i < 32; i++) {
122       // Case 3: old files which would be deletable for the first log cleaner
123       // (TimeToLiveHFileCleaner),
124       Path fileName = new Path(archivedHfileDir, (prefix + "." + (createTime + i)));
125       fs.createNewFile(fileName);
126       // set the creation time past ttl to ensure that it gets removed
127       fs.setTimes(fileName, createTime - ttl - 1, -1);
128       LOG.debug("Creating " + getFileStats(fileName, fs));
129     }
130 
131     // Case 2: 1 newer file, not even deletable for the first log cleaner
132     // (TimeToLiveLogCleaner), so we are not going down the chain
133     Path saved = new Path(archivedHfileDir, prefix + ".00000000000");
134     fs.createNewFile(saved);
135     // set creation time within the ttl
136     fs.setTimes(saved, createTime - ttl / 2, -1);
137     LOG.debug("Creating " + getFileStats(saved, fs));
138     for (FileStatus stat : fs.listStatus(archivedHfileDir)) {
139       LOG.debug(stat.getPath().toString());
140     }
141 
142     assertEquals(33, fs.listStatus(archivedHfileDir).length);
143 
144     // set a custom edge manager to handle time checking
145     EnvironmentEdge setTime = new EnvironmentEdge() {
146       @Override
147       public long currentTime() {
148         return createTime;
149       }
150     };
151     EnvironmentEdgeManager.injectEdge(setTime);
152 
153     // run the chore
154     cleaner.chore();
155 
156     // ensure we only end up with the saved file
157     assertEquals(1, fs.listStatus(archivedHfileDir).length);
158 
159     for (FileStatus file : fs.listStatus(archivedHfileDir)) {
160       LOG.debug("Kept hfiles: " + file.getPath().getName());
161     }
162 
163     // reset the edge back to the original edge
164     EnvironmentEdgeManager.injectEdge(originalEdge);
165   }
166 
167   @Test
168   public void testRemovesEmptyDirectories() throws Exception {
169     Configuration conf = UTIL.getConfiguration();
170     // no cleaner policies = delete all files
171     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
172     Server server = new DummyServer();
173     Path archivedHfileDir = new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
174 
175     // setup the cleaner
176     FileSystem fs = UTIL.getDFSCluster().getFileSystem();
177     HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
178 
179     // make all the directories for archiving files
180     Path table = new Path(archivedHfileDir, "table");
181     Path region = new Path(table, "regionsomthing");
182     Path family = new Path(region, "fam");
183     Path file = new Path(family, "file12345");
184     fs.mkdirs(family);
185     if (!fs.exists(family)) throw new RuntimeException("Couldn't create test family:" + family);
186     fs.create(file).close();
187     if (!fs.exists(file)) throw new RuntimeException("Test file didn't get created:" + file);
188 
189     // run the chore to cleanup the files (and the directories above it)
190     cleaner.chore();
191 
192     // make sure all the parent directories get removed
193     assertFalse("family directory not removed for empty directory", fs.exists(family));
194     assertFalse("region directory not removed for empty directory", fs.exists(region));
195     assertFalse("table directory not removed for empty directory", fs.exists(table));
196     assertTrue("archive directory", fs.exists(archivedHfileDir));
197   }
198 
199   static class DummyServer implements Server {
200 
201     @Override
202     public Configuration getConfiguration() {
203       return UTIL.getConfiguration();
204     }
205 
206     @Override
207     public ZooKeeperWatcher getZooKeeper() {
208       try {
209         return new ZooKeeperWatcher(getConfiguration(), "dummy server", this);
210       } catch (IOException e) {
211         e.printStackTrace();
212       }
213       return null;
214     }
215 
216     @Override
217     public CoordinatedStateManager getCoordinatedStateManager() {
218       return null;
219     }
220 
221     @Override
222     public ClusterConnection getConnection() {
223       return null;
224     }
225 
226     @Override
227     public MetaTableLocator getMetaTableLocator() {
228       return null;
229     }
230 
231     @Override
232     public ServerName getServerName() {
233       return ServerName.valueOf("regionserver,60020,000000");
234     }
235 
236     @Override
237     public void abort(String why, Throwable e) {
238     }
239 
240     @Override
241     public boolean isAborted() {
242       return false;
243     }
244 
245     @Override
246     public void stop(String why) {
247     }
248 
249     @Override
250     public boolean isStopped() {
251       return false;
252     }
253 
254     @Override
255     public ChoreService getChoreService() {
256       return null;
257     }
258   }
259 
260   @Test
261   public void testThreadCleanup() throws Exception {
262     Configuration conf = UTIL.getConfiguration();
263     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
264     Server server = new DummyServer();
265     Path archivedHfileDir =
266         new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
267 
268     // setup the cleaner
269     FileSystem fs = UTIL.getDFSCluster().getFileSystem();
270     HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
271     // clean up archive directory
272     fs.delete(archivedHfileDir, true);
273     fs.mkdirs(archivedHfileDir);
274     // create some file to delete
275     fs.createNewFile(new Path(archivedHfileDir, "dfd-dfd"));
276     // launch the chore
277     cleaner.chore();
278     // call cleanup
279     cleaner.cleanup();
280     // wait awhile for thread to die
281     Thread.sleep(100);
282     for (Thread thread : cleaner.getCleanerThreads()) {
283       Assert.assertFalse(thread.isAlive());
284     }
285   }
286 
287   @Test
288   public void testLargeSmallIsolation() throws Exception {
289     Configuration conf = UTIL.getConfiguration();
290     // no cleaner policies = delete all files
291     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
292     conf.setInt(HFileCleaner.HFILE_DELETE_THROTTLE_THRESHOLD, 512 * 1024);
293     Server server = new DummyServer();
294     Path archivedHfileDir =
295         new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
296 
297     // setup the cleaner
298     FileSystem fs = UTIL.getDFSCluster().getFileSystem();
299     HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
300     // clean up archive directory
301     fs.delete(archivedHfileDir, true);
302     fs.mkdirs(archivedHfileDir);
303     // necessary set up
304     final int LARGE_FILE_NUM = 5;
305     final int SMALL_FILE_NUM = 20;
306     createFilesForTesting(LARGE_FILE_NUM, SMALL_FILE_NUM, fs, archivedHfileDir);
307     // call cleanup
308     cleaner.chore();
309 
310     Assert.assertEquals(LARGE_FILE_NUM, cleaner.getNumOfDeletedLargeFiles());
311     Assert.assertEquals(SMALL_FILE_NUM, cleaner.getNumOfDeletedSmallFiles());
312   }
313 
314   @Test(timeout = 60 * 1000)
315   public void testOnConfigurationChange() throws Exception {
316     // constants
317     final int ORIGINAL_THROTTLE_POINT = 512 * 1024;
318     final int ORIGINAL_QUEUE_INIT_SIZE = 512;
319     final int UPDATE_THROTTLE_POINT = 1024;// small enough to change large/small check
320     final int UPDATE_QUEUE_INIT_SIZE = 1024;
321     final int LARGE_FILE_NUM = 5;
322     final int SMALL_FILE_NUM = 20;
323 
324     Configuration conf = UTIL.getConfiguration();
325     // no cleaner policies = delete all files
326     conf.setStrings(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
327     conf.setInt(HFileCleaner.HFILE_DELETE_THROTTLE_THRESHOLD, ORIGINAL_THROTTLE_POINT);
328     conf.setInt(HFileCleaner.LARGE_HFILE_QUEUE_INIT_SIZE, ORIGINAL_QUEUE_INIT_SIZE);
329     conf.setInt(HFileCleaner.SMALL_HFILE_QUEUE_INIT_SIZE, ORIGINAL_QUEUE_INIT_SIZE);
330     Server server = new DummyServer();
331     Path archivedHfileDir =
332         new Path(UTIL.getDataTestDirOnTestFS(), HConstants.HFILE_ARCHIVE_DIRECTORY);
333 
334     // setup the cleaner
335     FileSystem fs = UTIL.getDFSCluster().getFileSystem();
336     final HFileCleaner cleaner = new HFileCleaner(1000, server, conf, fs, archivedHfileDir);
337     Assert.assertEquals(ORIGINAL_THROTTLE_POINT, cleaner.getThrottlePoint());
338     Assert.assertEquals(ORIGINAL_QUEUE_INIT_SIZE, cleaner.getLargeQueueInitSize());
339     Assert.assertEquals(ORIGINAL_QUEUE_INIT_SIZE, cleaner.getSmallQueueInitSize());
340 
341     // clean up archive directory and create files for testing
342     fs.delete(archivedHfileDir, true);
343     fs.mkdirs(archivedHfileDir);
344     createFilesForTesting(LARGE_FILE_NUM, SMALL_FILE_NUM, fs, archivedHfileDir);
345 
346     // call cleaner, run as daemon to test the interrupt-at-middle case
347     Thread t = new Thread() {
348       @Override
349       public void run() {
350         cleaner.chore();
351       }
352     };
353     t.setDaemon(true);
354     t.start();
355     // wait until file clean started
356     while (cleaner.getNumOfDeletedSmallFiles() == 0) {
357       Thread.yield();
358     }
359 
360     // trigger configuration change
361     Configuration newConf = new Configuration(conf);
362     newConf.setInt(HFileCleaner.HFILE_DELETE_THROTTLE_THRESHOLD, UPDATE_THROTTLE_POINT);
363     newConf.setInt(HFileCleaner.LARGE_HFILE_QUEUE_INIT_SIZE, UPDATE_QUEUE_INIT_SIZE);
364     newConf.setInt(HFileCleaner.SMALL_HFILE_QUEUE_INIT_SIZE, UPDATE_QUEUE_INIT_SIZE);
365     LOG.debug("File deleted from large queue: " + cleaner.getNumOfDeletedLargeFiles()
366         + "; from small queue: " + cleaner.getNumOfDeletedSmallFiles());
367     cleaner.onConfigurationChange(newConf);
368 
369     // check values after change
370     Assert.assertEquals(UPDATE_THROTTLE_POINT, cleaner.getThrottlePoint());
371     Assert.assertEquals(UPDATE_QUEUE_INIT_SIZE, cleaner.getLargeQueueInitSize());
372     Assert.assertEquals(UPDATE_QUEUE_INIT_SIZE, cleaner.getSmallQueueInitSize());
373     Assert.assertEquals(2, cleaner.getCleanerThreads().size());
374 
375     // wait until clean done and check
376     t.join();
377     LOG.debug("File deleted from large queue: " + cleaner.getNumOfDeletedLargeFiles()
378         + "; from small queue: " + cleaner.getNumOfDeletedSmallFiles());
379     Assert.assertTrue("Should delete more than " + LARGE_FILE_NUM
380         + " files from large queue but actually " + cleaner.getNumOfDeletedLargeFiles(),
381       cleaner.getNumOfDeletedLargeFiles() > LARGE_FILE_NUM);
382     Assert.assertTrue("Should delete less than " + SMALL_FILE_NUM
383         + " files from small queue but actually " + cleaner.getNumOfDeletedSmallFiles(),
384       cleaner.getNumOfDeletedSmallFiles() < SMALL_FILE_NUM);
385   }
386 
387   private void createFilesForTesting(int largeFileNum, int smallFileNum, FileSystem fs,
388       Path archivedHfileDir) throws IOException {
389     final Random rand = new Random();
390     final byte[] large = new byte[1024 * 1024];
391     for (int i = 0; i < large.length; i++) {
392       large[i] = (byte) rand.nextInt(128);
393     }
394     final byte[] small = new byte[1024];
395     for (int i = 0; i < small.length; i++) {
396       small[i] = (byte) rand.nextInt(128);
397     }
398     // create large and small files
399     for (int i = 1; i <= largeFileNum; i++) {
400       FSDataOutputStream out = fs.create(new Path(archivedHfileDir, "large-file-" + i));
401       out.write(large);
402       out.close();
403     }
404     for (int i = 1; i <= smallFileNum; i++) {
405       FSDataOutputStream out = fs.create(new Path(archivedHfileDir, "small-file-" + i));
406       out.write(small);
407       out.close();
408     }
409   }
410 }