1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.backup.example;
19
20 import java.io.IOException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileStatus;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
30 import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
31 import org.apache.hadoop.hbase.util.FSUtils;
32 import org.apache.zookeeper.KeeperException;
33
34
35
36
37
38
39
40
41
42 @InterfaceAudience.Private
43 public class LongTermArchivingHFileCleaner extends BaseHFileCleanerDelegate {
44
45 private static final Log LOG = LogFactory.getLog(LongTermArchivingHFileCleaner.class);
46
47 TableHFileArchiveTracker archiveTracker;
48 private FileSystem fs;
49
50 @Override
51 public boolean isFileDeletable(Path file) {
52 try {
53
54 if (!fs.isFile(file)) return true;
55
56
57 FileStatus[] deleteStatus = FSUtils.listStatus(this.fs, file, null);
58
59
60 if (deleteStatus == null) return true;
61
62
63 Path family = file.getParent();
64 Path region = family.getParent();
65 Path table = region.getParent();
66
67 String tableName = table.getName();
68 boolean ret = !archiveTracker.keepHFiles(tableName);
69 LOG.debug("Archiver says to [" + (ret ? "delete" : "keep") + "] files for table:" + tableName);
70 return ret;
71 } catch (IOException e) {
72 LOG.error("Failed to lookup status of:" + file + ", keeping it just incase.", e);
73 return false;
74 }
75 }
76
77 @Override
78 public void setConf(Configuration config) {
79
80
81
82 Configuration conf = new Configuration(config);
83 super.setConf(conf);
84 try {
85 this.fs = FileSystem.get(conf);
86 this.archiveTracker = TableHFileArchiveTracker.create(conf);
87 this.archiveTracker.start();
88 } catch (KeeperException e) {
89 LOG.error("Error while configuring " + this.getClass().getName(), e);
90 } catch (IOException e) {
91 LOG.error("Error while configuring " + this.getClass().getName(), e);
92 }
93 }
94
95 @Override
96 public void stop(String reason) {
97 if (this.isStopped()) return;
98 super.stop(reason);
99 if (this.archiveTracker != null) {
100 LOG.info("Stopping " + this.archiveTracker);
101 this.archiveTracker.stop();
102 }
103
104 }
105
106 }