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