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.hbase.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.wal.WALSplitter;
33
34
35
36
37 @InterfaceAudience.Private
38 public final class FSVisitor {
39 private static final Log LOG = LogFactory.getLog(FSVisitor.class);
40
41 public interface RegionVisitor {
42 void region(final String region) throws IOException;
43 }
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 visitRegions(final FileSystem fs, final Path tableDir,
73 final RegionVisitor visitor) throws IOException {
74 FileStatus[] regions = FSUtils.listStatus(fs, tableDir, new FSUtils.RegionDirFilter(fs));
75 if (regions == null) {
76 if (LOG.isTraceEnabled()) {
77 LOG.trace("No regions under directory:" + tableDir);
78 }
79 return;
80 }
81
82 for (FileStatus region: regions) {
83 visitor.region(region.getPath().getName());
84 }
85 }
86
87
88
89
90
91
92
93
94
95 public static void visitTableStoreFiles(final FileSystem fs, final Path tableDir,
96 final StoreFileVisitor visitor) throws IOException {
97 FileStatus[] regions = FSUtils.listStatus(fs, tableDir, new FSUtils.RegionDirFilter(fs));
98 if (regions == null) {
99 if (LOG.isTraceEnabled()) {
100 LOG.trace("No regions under directory:" + tableDir);
101 }
102 return;
103 }
104
105 for (FileStatus region: regions) {
106 visitRegionStoreFiles(fs, region.getPath(), visitor);
107 }
108 }
109
110
111
112
113
114
115
116
117
118 public static void visitRegionStoreFiles(final FileSystem fs, final Path regionDir,
119 final StoreFileVisitor visitor) throws IOException {
120 FileStatus[] families = FSUtils.listStatus(fs, regionDir, new FSUtils.FamilyDirFilter(fs));
121 if (families == null) {
122 if (LOG.isTraceEnabled()) {
123 LOG.trace("No families under region directory:" + regionDir);
124 }
125 return;
126 }
127
128 PathFilter fileFilter = new FSUtils.FileFilter(fs);
129 for (FileStatus family: families) {
130 Path familyDir = family.getPath();
131 String familyName = familyDir.getName();
132
133
134 FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir, fileFilter);
135 if (storeFiles == null) {
136 if (LOG.isTraceEnabled()) {
137 LOG.trace("No hfiles found for family: " + familyDir + ", skipping.");
138 }
139 continue;
140 }
141
142 for (FileStatus hfile: storeFiles) {
143 Path hfilePath = hfile.getPath();
144 visitor.storeFile(regionDir.getName(), familyName, hfilePath.getName());
145 }
146 }
147 }
148
149
150
151
152
153
154
155
156
157 public static void visitTableRecoveredEdits(final FileSystem fs, final Path tableDir,
158 final FSVisitor.RecoveredEditsVisitor visitor) throws IOException {
159 FileStatus[] regions = FSUtils.listStatus(fs, tableDir, new FSUtils.RegionDirFilter(fs));
160 if (regions == null) {
161 if (LOG.isTraceEnabled()) {
162 LOG.trace("No recoveredEdits regions under directory:" + tableDir);
163 }
164 return;
165 }
166
167 for (FileStatus region: regions) {
168 visitRegionRecoveredEdits(fs, region.getPath(), visitor);
169 }
170 }
171
172
173
174
175
176
177
178
179
180 public static void visitRegionRecoveredEdits(final FileSystem fs, final Path regionDir,
181 final FSVisitor.RecoveredEditsVisitor visitor) throws IOException {
182 NavigableSet<Path> files = WALSplitter.getSplitEditFilesSorted(fs, regionDir);
183 if (files == null || files.size() == 0) return;
184
185 for (Path source: files) {
186
187 FileStatus stat = fs.getFileStatus(source);
188 if (stat.getLen() <= 0) continue;
189
190 visitor.recoveredEdits(regionDir.getName(), source.getName());
191 }
192 }
193
194
195
196
197
198
199
200
201
202 public static void visitLogFiles(final FileSystem fs, final Path rootDir,
203 final LogFileVisitor visitor) throws IOException {
204 Path logsDir = new Path(rootDir, HConstants.HREGION_LOGDIR_NAME);
205 FileStatus[] logServerDirs = FSUtils.listStatus(fs, logsDir);
206 if (logServerDirs == null) {
207 if (LOG.isTraceEnabled()) {
208 LOG.trace("No logs under directory:" + logsDir);
209 }
210 return;
211 }
212
213 for (FileStatus serverLogs: logServerDirs) {
214 String serverName = serverLogs.getPath().getName();
215
216 FileStatus[] wals = FSUtils.listStatus(fs, serverLogs.getPath());
217 if (wals == null) {
218 if (LOG.isTraceEnabled()) {
219 LOG.trace("No wals found for server: " + serverName + ", skipping.");
220 }
221 continue;
222 }
223
224 for (FileStatus walRef: wals) {
225 visitor.logFile(serverName, walRef.getPath().getName());
226 }
227 }
228 }
229 }