1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.cleaner;
19
20 import java.io.IOException;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.fs.FileStatus;
25 import org.apache.hadoop.fs.FileSystem;
26 import org.apache.hadoop.fs.Path;
27 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.io.HFileLink;
31 import org.apache.hadoop.hbase.util.FSUtils;
32
33
34
35
36
37
38
39
40
41 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
42 public class HFileLinkCleaner extends BaseHFileCleanerDelegate {
43 private static final Log LOG = LogFactory.getLog(HFileLinkCleaner.class);
44
45 private FileSystem fs = null;
46
47 @Override
48 public synchronized boolean isFileDeletable(FileStatus fStat) {
49 if (this.fs == null) return false;
50 Path filePath = fStat.getPath();
51
52 if (HFileLink.isHFileLink(filePath)) return true;
53
54
55
56 Path parentDir = filePath.getParent();
57 if (HFileLink.isBackReferencesDir(parentDir)) {
58 Path hfilePath = null;
59 try {
60
61
62 hfilePath = HFileLink.getHFileFromBackReference(
63 new Path(FSUtils.getRootDir(getConf()), HConstants.HBASE_TEMP_DIRECTORY), filePath);
64 if (fs.exists(hfilePath)) {
65 return false;
66 }
67 hfilePath = HFileLink.getHFileFromBackReference(FSUtils.getRootDir(getConf()), filePath);
68 return !fs.exists(hfilePath);
69 } catch (IOException e) {
70 if (LOG.isDebugEnabled()) {
71 LOG.debug("Couldn't verify if the referenced file still exists, keep it just in case: "
72 + hfilePath);
73 }
74 return false;
75 }
76 }
77
78
79 Path backRefDir = null;
80 try {
81 backRefDir = HFileLink.getBackReferencesDir(parentDir, filePath.getName());
82 return FSUtils.listStatus(fs, backRefDir) == null;
83 } catch (IOException e) {
84 if (LOG.isDebugEnabled()) {
85 LOG.debug("Couldn't get the references, not deleting file, just in case. filePath="
86 + filePath + ", backRefDir=" + backRefDir);
87 }
88 return false;
89 }
90 }
91
92 @Override
93 public synchronized void setConf(Configuration conf) {
94 super.setConf(conf);
95
96
97 try {
98 this.fs = FileSystem.get(this.getConf());
99 } catch (IOException e) {
100 if (LOG.isDebugEnabled()) {
101 LOG.debug("Couldn't instantiate the file system, not deleting file, just in case. "
102 + FileSystem.FS_DEFAULT_NAME_KEY + "="
103 + getConf().get(FileSystem.FS_DEFAULT_NAME_KEY, FileSystem.DEFAULT_FS));
104 }
105 }
106 }
107 }