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