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 org.apache.hadoop.fs.Path;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28
29
30
31 public class TimeToLiveLogCleaner implements LogCleanerDelegate {
32 static final Log LOG = LogFactory.getLog(TimeToLiveLogCleaner.class.getName());
33 private Configuration conf;
34
35 private long ttl;
36 private boolean stopped = false;
37
38 @Override
39 public boolean isLogDeletable(Path filePath) {
40 long time = 0;
41 long currentTime = System.currentTimeMillis();
42 String[] parts = filePath.getName().split("\\.");
43 try {
44 time = Long.parseLong(parts[parts.length-1]);
45 } catch (NumberFormatException e) {
46 LOG.error("Unable to parse the timestamp in " + filePath.getName() +
47 ", deleting it since it's invalid and may not be a hlog", e);
48 return true;
49 }
50 long life = currentTime - time;
51 if (life < 0) {
52 LOG.warn("Found a log newer than current time, " +
53 "probably a clock skew");
54 return false;
55 }
56 return life > ttl;
57 }
58
59 @Override
60 public void setConf(Configuration conf) {
61 this.conf = conf;
62 this.ttl = conf.getLong("hbase.master.logcleaner.ttl", 600000);
63 }
64
65 @Override
66 public Configuration getConf() {
67 return conf;
68 }
69
70 @Override
71 public void stop(String why) {
72 this.stopped = true;
73 }
74
75 @Override
76 public boolean isStopped() {
77 return this.stopped;
78 }
79 }