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.BaseHFileCleanerDelegate;
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 SnapshotHFileCleaner extends BaseHFileCleanerDelegate {
43 private static final Log LOG = LogFactory.getLog(SnapshotHFileCleaner.class);
44
45
46
47
48
49 public static final String HFILE_CACHE_REFRESH_PERIOD_CONF_KEY =
50 "hbase.master.hfilecleaner.plugins.snapshot.period";
51
52
53 private static final long DEFAULT_HFILE_CACHE_REFRESH_PERIOD = 300000;
54
55
56 private SnapshotFileCache cache;
57
58 @Override
59 public synchronized boolean isFileDeletable(FileStatus fStat) {
60 try {
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 @Override
70 public void setConf(final Configuration conf) {
71 super.setConf(conf);
72 try {
73 long cacheRefreshPeriod = conf.getLong(HFILE_CACHE_REFRESH_PERIOD_CONF_KEY,
74 DEFAULT_HFILE_CACHE_REFRESH_PERIOD);
75 final FileSystem fs = FSUtils.getCurrentFileSystem(conf);
76 Path rootDir = FSUtils.getRootDir(conf);
77 cache = new SnapshotFileCache(fs, rootDir, cacheRefreshPeriod, cacheRefreshPeriod,
78 "snapshot-hfile-cleaner-cache-refresher", new SnapshotFileCache.SnapshotFileInspector() {
79 public Collection<String> filesUnderSnapshot(final Path snapshotDir)
80 throws IOException {
81 return SnapshotReferenceUtil.getHFileNames(conf, fs, snapshotDir);
82 }
83 });
84 } catch (IOException e) {
85 LOG.error("Failed to create cleaner util", e);
86 }
87 }
88
89 @Override
90 public void stop(String why) {
91 this.cache.stop(why);
92 }
93
94 @Override
95 public boolean isStopped() {
96 return this.cache.isStopped();
97 }
98
99
100
101
102
103 public SnapshotFileCache getFileCacheForTesting() {
104 return this.cache;
105 }
106 }