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