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