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.BaseLogCleanerDelegate;
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 SnapshotLogCleaner extends BaseLogCleanerDelegate {
44 private static final Log LOG = LogFactory.getLog(SnapshotLogCleaner.class);
45
46
47
48
49
50 static final String WAL_CACHE_REFRESH_PERIOD_CONF_KEY =
51 "hbase.master.hlogcleaner.plugins.snapshot.period";
52
53
54 private static final long DEFAULT_WAL_CACHE_REFRESH_PERIOD = 300000;
55
56 private SnapshotFileCache cache;
57
58 @Override
59 public synchronized Iterable<FileStatus> getDeletableFiles(Iterable<FileStatus> files) {
60 try {
61 return cache.getUnreferencedFiles(files);
62 } catch (IOException e) {
63 LOG.error("Exception while checking if files were valid, keeping them just in case.", e);
64 return Collections.emptyList();
65 }
66 }
67
68
69
70
71
72
73
74 @Override
75 public void setConf(Configuration conf) {
76 super.setConf(conf);
77 try {
78 long cacheRefreshPeriod = conf.getLong(
79 WAL_CACHE_REFRESH_PERIOD_CONF_KEY, DEFAULT_WAL_CACHE_REFRESH_PERIOD);
80 final FileSystem fs = FSUtils.getCurrentFileSystem(conf);
81 Path rootDir = FSUtils.getRootDir(conf);
82 cache = new SnapshotFileCache(fs, rootDir, cacheRefreshPeriod, cacheRefreshPeriod,
83 "snapshot-log-cleaner-cache-refresher", new SnapshotFileCache.SnapshotFileInspector() {
84 public Collection<String> filesUnderSnapshot(final Path snapshotDir)
85 throws IOException {
86 return SnapshotReferenceUtil.getWALNames(fs, snapshotDir);
87 }
88 });
89 } catch (IOException e) {
90 LOG.error("Failed to create snapshot log cleaner", e);
91 }
92 }
93
94 @Override
95 public void stop(String why) {
96 this.cache.stop(why);
97 }
98
99 @Override
100 public boolean isStopped() {
101 return this.cache.isStopped();
102 }
103 }