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 import java.util.Collections;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.classification.InterfaceStability;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.fs.FileStatus;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
33 import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
34 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
35 import org.apache.hadoop.hbase.util.FSUtils;
36
37
38
39
40
41 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
42 @InterfaceStability.Evolving
43 public class SnapshotHFileCleaner extends BaseHFileCleanerDelegate {
44 private static final Log LOG = LogFactory.getLog(SnapshotHFileCleaner.class);
45
46
47
48
49
50 public static final String HFILE_CACHE_REFRESH_PERIOD_CONF_KEY =
51 "hbase.master.hfilecleaner.plugins.snapshot.period";
52
53
54 private static final long DEFAULT_HFILE_CACHE_REFRESH_PERIOD = 300000;
55
56
57 private SnapshotFileCache cache;
58
59 @Override
60 public synchronized Iterable<FileStatus> getDeletableFiles(Iterable<FileStatus> files) {
61 try {
62 return cache.getUnreferencedFiles(files);
63 } catch (IOException e) {
64 LOG.error("Exception while checking if files were valid, keeping them just in case.", e);
65 return Collections.emptyList();
66 }
67 }
68
69 @Override
70 protected boolean isFileDeletable(FileStatus fStat) {
71 return false;
72 }
73
74 public void setConf(final Configuration conf) {
75 super.setConf(conf);
76 try {
77 long cacheRefreshPeriod = conf.getLong(HFILE_CACHE_REFRESH_PERIOD_CONF_KEY,
78 DEFAULT_HFILE_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-hfile-cleaner-cache-refresher", new SnapshotFileCache.SnapshotFileInspector() {
83 public Collection<String> filesUnderSnapshot(final Path snapshotDir)
84 throws IOException {
85 return SnapshotReferenceUtil.getHFileNames(conf, fs, snapshotDir);
86 }
87 });
88 } catch (IOException e) {
89 LOG.error("Failed to create cleaner util", 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
103
104
105
106
107 public SnapshotFileCache getFileCacheForTesting() {
108 return this.cache;
109 }
110 }