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