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.hbase.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.HBaseInterfaceAudience;
30 import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
31 import org.apache.hadoop.hbase.util.FSUtils;
32 import org.apache.zookeeper.KeeperException;
33
34
35
36
37
38
39
40
41
42
43 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
44 public class LongTermArchivingHFileCleaner extends BaseHFileCleanerDelegate {
45
46 private static final Log LOG = LogFactory.getLog(LongTermArchivingHFileCleaner.class);
47
48 TableHFileArchiveTracker archiveTracker;
49 private FileSystem fs;
50
51 @Override
52 public boolean isFileDeletable(FileStatus fStat) {
53 try {
54
55 if (fStat.isDirectory()) return true;
56
57 Path file = fStat.getPath();
58
59 FileStatus[] deleteStatus = FSUtils.listStatus(this.fs, file, null);
60
61
62 if (deleteStatus == null) return true;
63
64
65 Path family = file.getParent();
66 Path region = family.getParent();
67 Path table = region.getParent();
68
69 String tableName = table.getName();
70 boolean ret = !archiveTracker.keepHFiles(tableName);
71 LOG.debug("Archiver says to [" + (ret ? "delete" : "keep") + "] files for table:" + tableName);
72 return ret;
73 } catch (IOException e) {
74 LOG.error("Failed to lookup status of:" + fStat.getPath() + ", keeping it just incase.", e);
75 return false;
76 }
77 }
78
79 @Override
80 public void setConf(Configuration config) {
81
82
83
84 Configuration conf = new Configuration(config);
85 super.setConf(conf);
86 try {
87 this.fs = FileSystem.get(conf);
88 this.archiveTracker = TableHFileArchiveTracker.create(conf);
89 this.archiveTracker.start();
90 } catch (KeeperException e) {
91 LOG.error("Error while configuring " + this.getClass().getName(), e);
92 } catch (IOException e) {
93 LOG.error("Error while configuring " + this.getClass().getName(), e);
94 }
95 }
96
97 @Override
98 public void stop(String reason) {
99 if (this.isStopped()) return;
100 super.stop(reason);
101 if (this.archiveTracker != null) {
102 LOG.info("Stopping " + this.archiveTracker);
103 this.archiveTracker.stop();
104 }
105
106 }
107
108 }