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 java.io.IOException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.classification.InterfaceAudience;
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.util.EnvironmentEdgeManager;
30
31
32
33
34
35 @InterfaceAudience.Private
36 public class TimeToLiveHFileCleaner extends BaseHFileCleanerDelegate {
37
38 public static final Log LOG = LogFactory.getLog(TimeToLiveHFileCleaner.class.getName());
39 public static final String TTL_CONF_KEY = "hbase.master.hfilecleaner.ttl";
40
41 private static final long DEFAULT_TTL = 60000 * 5;
42
43 private long ttl;
44 private FileSystem fs;
45
46 @Override
47 public void setConf(Configuration conf) {
48 this.ttl = conf.getLong(TTL_CONF_KEY, DEFAULT_TTL);
49 super.setConf(conf);
50 }
51
52 @Override
53 public boolean isFileDeletable(Path filePath) {
54 if (!instantiateFS()) {
55 return false;
56 }
57 long time = 0;
58 long currentTime = EnvironmentEdgeManager.currentTimeMillis();
59 try {
60 FileStatus fStat = fs.getFileStatus(filePath);
61 time = fStat.getModificationTime();
62 } catch (IOException e) {
63 LOG.error("Unable to get modification time of file " + filePath.getName()
64 + ", not deleting it.", e);
65 return false;
66 }
67 long life = currentTime - time;
68 if (LOG.isTraceEnabled()) {
69 LOG.trace("HFile life:" + life + ", ttl:" + ttl + ", current:" + currentTime + ", from: "
70 + time);
71 }
72 if (life < 0) {
73 LOG.warn("Found a log (" + filePath + ") newer than current time (" + currentTime + " < "
74 + time + "), probably a clock skew");
75 return false;
76 }
77 return life > ttl;
78 }
79
80
81
82
83 private synchronized boolean instantiateFS() {
84 if (this.fs == null) {
85 try {
86 this.fs = FileSystem.get(this.getConf());
87 } catch (IOException e) {
88 LOG.error("Couldn't instantiate the file system, not deleting file, just incase");
89 return false;
90 }
91 }
92 return true;
93 }
94 }