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(FileStatus fStat) {
52 try {
53
54 if (fStat.isDir()) return true;
55
56 Path file = fStat.getPath();
57
58 FileStatus[] deleteStatus = FSUtils.listStatus(this.fs, file, null);
59
60
61 if (deleteStatus == null) return true;
62
63
64 Path family = file.getParent();
65 Path region = family.getParent();
66 Path table = region.getParent();
67
68 String tableName = table.getName();
69 boolean ret = !archiveTracker.keepHFiles(tableName);
70 LOG.debug("Archiver says to [" + (ret ? "delete" : "keep") + "] files for table:" + tableName);
71 return ret;
72 } catch (IOException e) {
73 LOG.error("Failed to lookup status of:" + fStat.getPath() + ", keeping it just incase.", e);
74 return false;
75 }
76 }
77
78 @Override
79 public void setConf(Configuration config) {
80
81
82
83 Configuration conf = new Configuration(config);
84 super.setConf(conf);
85 try {
86 this.fs = FileSystem.get(conf);
87 this.archiveTracker = TableHFileArchiveTracker.create(conf);
88 this.archiveTracker.start();
89 } catch (KeeperException e) {
90 LOG.error("Error while configuring " + this.getClass().getName(), e);
91 } catch (IOException e) {
92 LOG.error("Error while configuring " + this.getClass().getName(), e);
93 }
94 }
95
96 @Override
97 public void stop(String reason) {
98 if (this.isStopped()) return;
99 super.stop(reason);
100 if (this.archiveTracker != null) {
101 LOG.info("Stopping " + this.archiveTracker);
102 this.archiveTracker.stop();
103 }
104
105 }
106
107 }