1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.snapshot;
19
20 import java.io.IOException;
21 import java.util.Collection;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.classification.InterfaceStability;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileStatus;
29 import org.apache.hadoop.fs.FileSystem;
30 import org.apache.hadoop.fs.Path;
31 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
32 import org.apache.hadoop.hbase.master.cleaner.BaseLogCleanerDelegate;
33 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
34 import org.apache.hadoop.hbase.util.FSUtils;
35
36
37
38
39
40 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
41 @InterfaceStability.Evolving
42 public class SnapshotLogCleaner extends BaseLogCleanerDelegate {
43 private static final Log LOG = LogFactory.getLog(SnapshotLogCleaner.class);
44
45
46
47
48
49 static final String HLOG_CACHE_REFRESH_PERIOD_CONF_KEY =
50 "hbase.master.hlogcleaner.plugins.snapshot.period";
51
52
53 private static final long DEFAULT_HLOG_CACHE_REFRESH_PERIOD = 300000;
54
55 private SnapshotFileCache cache;
56
57 @Override
58 public synchronized boolean isFileDeletable(FileStatus fStat) {
59 try {
60 if (null == cache) return false;
61 return !cache.contains(fStat.getPath().getName());
62 } catch (IOException e) {
63 LOG.error("Exception while checking if:" + fStat.getPath()
64 + " was valid, keeping it just in case.", e);
65 return false;
66 }
67 }
68
69
70
71
72
73
74
75 @Override
76 public void setConf(Configuration conf) {
77 super.setConf(conf);
78 try {
79 long cacheRefreshPeriod = conf.getLong(
80 HLOG_CACHE_REFRESH_PERIOD_CONF_KEY, DEFAULT_HLOG_CACHE_REFRESH_PERIOD);
81 final FileSystem fs = FSUtils.getCurrentFileSystem(conf);
82 Path rootDir = FSUtils.getRootDir(conf);
83 cache = new SnapshotFileCache(fs, rootDir, cacheRefreshPeriod, cacheRefreshPeriod,
84 "snapshot-log-cleaner-cache-refresher", new SnapshotFileCache.SnapshotFileInspector() {
85 public Collection<String> filesUnderSnapshot(final Path snapshotDir)
86 throws IOException {
87 return SnapshotReferenceUtil.getHLogNames(fs, snapshotDir);
88 }
89 });
90 } catch (IOException e) {
91 LOG.error("Failed to create snapshot log cleaner", e);
92 }
93 }
94
95 @Override
96 public void stop(String why) {
97 this.cache.stop(why);
98 }
99
100 @Override
101 public boolean isStopped() {
102 return this.cache.isStopped();
103 }
104 }