1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.io.IOException;
22 import java.util.NavigableSet;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.fs.FileStatus;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.fs.PathFilter;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.io.Reference;
34 import org.apache.hadoop.hbase.regionserver.HRegion;
35 import org.apache.hadoop.hbase.regionserver.wal.HLog;
36 import org.apache.hadoop.hbase.util.FSUtils;
37
38
39
40
41 @InterfaceAudience.Private
42 public final class FSVisitor {
43 private static final Log LOG = LogFactory.getLog(FSVisitor.class);
44
45 public interface StoreFileVisitor {
46 void storeFile(final String region, final String family, final String hfileName)
47 throws IOException;
48 }
49
50 public interface RecoveredEditsVisitor {
51 void recoveredEdits (final String region, final String logfile)
52 throws IOException;
53 }
54
55 public interface LogFileVisitor {
56 void logFile (final String server, final String logfile)
57 throws IOException;
58 }
59
60 private FSVisitor() {
61
62 }
63
64
65
66
67
68
69
70
71
72 public static void visitTableStoreFiles(final FileSystem fs, final Path tableDir,
73 final StoreFileVisitor visitor) throws IOException {
74 FileStatus[] regions = FSUtils.listStatus(fs, tableDir, new FSUtils.RegionDirFilter(fs));
75 if (regions == null) {
76 LOG.info("No regions under directory:" + tableDir);
77 return;
78 }
79
80 for (FileStatus region: regions) {
81 visitRegionStoreFiles(fs, region.getPath(), visitor);
82 }
83 }
84
85
86
87
88
89
90
91
92
93 public static void visitRegionStoreFiles(final FileSystem fs, final Path regionDir,
94 final StoreFileVisitor visitor) throws IOException {
95 FileStatus[] families = FSUtils.listStatus(fs, regionDir, new FSUtils.FamilyDirFilter(fs));
96 if (families == null) {
97 LOG.info("No families under region directory:" + regionDir);
98 return;
99 }
100
101 PathFilter fileFilter = new FSUtils.FileFilter(fs);
102 for (FileStatus family: families) {
103 Path familyDir = family.getPath();
104 String familyName = familyDir.getName();
105
106
107 FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir, fileFilter);
108 if (storeFiles == null) {
109 LOG.debug("No hfiles found for family: " + familyDir + ", skipping.");
110 continue;
111 }
112
113 for (FileStatus hfile: storeFiles) {
114 Path hfilePath = hfile.getPath();
115 visitor.storeFile(regionDir.getName(), familyName, hfilePath.getName());
116 }
117 }
118 }
119
120
121
122
123
124
125
126
127
128 public static void visitTableRecoveredEdits(final FileSystem fs, final Path tableDir,
129 final FSVisitor.RecoveredEditsVisitor visitor) throws IOException {
130 FileStatus[] regions = FSUtils.listStatus(fs, tableDir, new FSUtils.RegionDirFilter(fs));
131 if (regions == null) {
132 LOG.info("No regions under directory:" + tableDir);
133 return;
134 }
135
136 for (FileStatus region: regions) {
137 visitRegionRecoveredEdits(fs, region.getPath(), visitor);
138 }
139 }
140
141
142
143
144
145
146
147
148
149 public static void visitRegionRecoveredEdits(final FileSystem fs, final Path regionDir,
150 final FSVisitor.RecoveredEditsVisitor visitor) throws IOException {
151 NavigableSet<Path> files = HLog.getSplitEditFilesSorted(fs, regionDir);
152 if (files == null || files.size() == 0) return;
153
154 for (Path source: files) {
155
156 FileStatus stat = fs.getFileStatus(source);
157 if (stat.getLen() <= 0) continue;
158
159 visitor.recoveredEdits(regionDir.getName(), source.getName());
160 }
161 }
162
163
164
165
166
167
168
169
170
171 public static void visitLogFiles(final FileSystem fs, final Path rootDir,
172 final LogFileVisitor visitor) throws IOException {
173 Path logsDir = new Path(rootDir, HConstants.HREGION_LOGDIR_NAME);
174 FileStatus[] logServerDirs = FSUtils.listStatus(fs, logsDir);
175 if (logServerDirs == null) {
176 LOG.info("No logs under directory:" + logsDir);
177 return;
178 }
179
180 for (FileStatus serverLogs: logServerDirs) {
181 String serverName = serverLogs.getPath().getName();
182
183 FileStatus[] hlogs = FSUtils.listStatus(fs, serverLogs.getPath());
184 if (hlogs == null) {
185 LOG.debug("No hfiles found for server: " + serverName + ", skipping.");
186 continue;
187 }
188
189 for (FileStatus hlogRef: hlogs) {
190 visitor.logFile(serverName, hlogRef.getPath().getName());
191 }
192 }
193 }
194 }