1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.snapshot;
19
20 import java.io.IOException;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.fs.FileStatus;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.fs.PathFilter;
29 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
30 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
31 import org.apache.hadoop.hbase.util.FSUtils;
32
33
34
35
36
37
38
39 public class ReferenceRegionHFilesTask extends SnapshotTask {
40
41 public static final Log LOG = LogFactory.getLog(ReferenceRegionHFilesTask.class);
42 private final Path regiondir;
43 private final FileSystem fs;
44 private final PathFilter fileFilter;
45 private final Path snapshotDir;
46
47
48
49
50
51
52
53
54
55 public ReferenceRegionHFilesTask(final SnapshotDescription snapshot,
56 ForeignExceptionDispatcher monitor, Path regionDir, final FileSystem fs, Path regionSnapshotDir) {
57 super(snapshot, monitor);
58 this.regiondir = regionDir;
59 this.fs = fs;
60
61 this.fileFilter = new PathFilter() {
62 @Override
63 public boolean accept(Path path) {
64 try {
65 return fs.isFile(path);
66 } catch (IOException e) {
67 LOG.error("Failed to reach fs to check file:" + path + ", marking as not file");
68 ReferenceRegionHFilesTask.this.snapshotFailure("Failed to reach fs to check file status",
69 e);
70 return false;
71 }
72 }
73 };
74 this.snapshotDir = regionSnapshotDir;
75 }
76
77 @Override
78 public Void call() throws IOException {
79 FileStatus[] families = FSUtils.listStatus(fs, regiondir, new FSUtils.FamilyDirFilter(fs));
80
81
82 if (families == null || families.length == 0) {
83 LOG.info("No families under region directory:" + regiondir
84 + ", not attempting to add references.");
85 return null;
86 }
87
88
89 List<Path> snapshotFamilyDirs = TakeSnapshotUtils.getFamilySnapshotDirectories(snapshot,
90 snapshotDir, families);
91
92 LOG.debug("Add hfile references to snapshot directories:" + snapshotFamilyDirs);
93 for (int i = 0; i < families.length; i++) {
94 FileStatus family = families[i];
95 Path familyDir = family.getPath();
96
97 FileStatus[] hfiles = FSUtils.listStatus(fs, familyDir, fileFilter);
98
99
100 if (hfiles == null || hfiles.length == 0) {
101 LOG.debug("Not hfiles found for family: " + familyDir + ", skipping.");
102 continue;
103 }
104
105
106 Path snapshotFamilyDir = snapshotFamilyDirs.get(i);
107 fs.mkdirs(snapshotFamilyDir);
108
109
110 for (FileStatus hfile : hfiles) {
111
112 Path referenceFile = new Path(snapshotFamilyDir, hfile.getPath().getName());
113 LOG.debug("Creating reference for:" + hfile.getPath() + " at " + referenceFile);
114 if (!fs.createNewFile(referenceFile)) {
115 throw new IOException("Failed to create reference file:" + referenceFile);
116 }
117 }
118 }
119 if (LOG.isDebugEnabled()) {
120 LOG.debug("Finished referencing hfiles, current region state:");
121 FSUtils.logFileSystemState(fs, regiondir, LOG);
122 LOG.debug("and the snapshot directory:");
123 FSUtils.logFileSystemState(fs, snapshotDir, LOG);
124 }
125 return null;
126 }
127 }