1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.snapshot;
20
21 import java.io.IOException;
22 import java.util.HashSet;
23 import java.util.TreeMap;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import org.apache.hadoop.classification.InterfaceAudience;
33 import org.apache.hadoop.fs.Path;
34 import org.apache.hadoop.fs.FileSystem;
35 import org.apache.hadoop.fs.FileStatus;
36
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.io.Reference;
39 import org.apache.hadoop.hbase.io.HFileLink;
40 import org.apache.hadoop.hbase.regionserver.wal.HLog;
41 import org.apache.hadoop.hbase.util.FSUtils;
42 import org.apache.hadoop.hbase.util.FSVisitor;
43
44
45
46
47 @InterfaceAudience.Private
48 public final class SnapshotReferenceUtil {
49 public interface FileVisitor extends FSVisitor.StoreFileVisitor,
50 FSVisitor.RecoveredEditsVisitor, FSVisitor.LogFileVisitor {
51 }
52
53 private SnapshotReferenceUtil() {
54
55 }
56
57
58
59
60
61
62
63
64 public static Path getLogsDir(Path snapshotDir, String serverName) {
65 return new Path(snapshotDir, HLog.getHLogDirectoryName(serverName));
66 }
67
68
69
70
71
72
73
74
75 public static Path getRecoveredEditsDir(Path snapshotDir, String regionName) {
76 return HLog.getRegionDirRecoveredEditsDir(new Path(snapshotDir, regionName));
77 }
78
79
80
81
82
83
84
85
86
87 public static Path getRecoveredEdits(Path snapshotDir, String regionName, String logfile) {
88 return new Path(getRecoveredEditsDir(snapshotDir, regionName), logfile);
89 }
90
91
92
93
94
95
96
97
98
99 public static void visitReferencedFiles(final FileSystem fs, final Path snapshotDir,
100 final FileVisitor visitor) throws IOException {
101 visitTableStoreFiles(fs, snapshotDir, visitor);
102 visitRecoveredEdits(fs, snapshotDir, visitor);
103 visitLogFiles(fs, snapshotDir, visitor);
104 }
105
106
107
108
109
110
111
112
113
114 public static void visitTableStoreFiles(final FileSystem fs, final Path snapshotDir,
115 final FSVisitor.StoreFileVisitor visitor) throws IOException {
116 FSVisitor.visitTableStoreFiles(fs, snapshotDir, visitor);
117 }
118
119
120
121
122
123
124
125
126
127 public static void visitRegionStoreFiles(final FileSystem fs, final Path regionDir,
128 final FSVisitor.StoreFileVisitor visitor) throws IOException {
129 FSVisitor.visitRegionStoreFiles(fs, regionDir, visitor);
130 }
131
132
133
134
135
136
137
138
139
140 public static void visitRecoveredEdits(final FileSystem fs, final Path snapshotDir,
141 final FSVisitor.RecoveredEditsVisitor visitor) throws IOException {
142 FSVisitor.visitTableRecoveredEdits(fs, snapshotDir, visitor);
143 }
144
145
146
147
148
149
150
151
152
153 public static void visitLogFiles(final FileSystem fs, final Path snapshotDir,
154 final FSVisitor.LogFileVisitor visitor) throws IOException {
155 FSVisitor.visitLogFiles(fs, snapshotDir, visitor);
156 }
157
158
159
160
161
162
163
164
165
166 public static Set<String> getSnapshotRegionNames(final FileSystem fs, final Path snapshotDir)
167 throws IOException {
168 FileStatus[] regionDirs = FSUtils.listStatus(fs, snapshotDir, new FSUtils.RegionDirFilter(fs));
169 if (regionDirs == null) return null;
170
171 Set<String> regions = new HashSet<String>();
172 for (FileStatus regionDir: regionDirs) {
173 regions.add(regionDir.getPath().getName());
174 }
175 return regions;
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189 public static Map<String, List<String>> getRegionHFileReferences(final FileSystem fs,
190 final Path snapshotRegionDir) throws IOException {
191 final Map<String, List<String>> familyFiles = new TreeMap<String, List<String>>();
192
193 visitRegionStoreFiles(fs, snapshotRegionDir,
194 new FSVisitor.StoreFileVisitor() {
195 public void storeFile (final String region, final String family, final String hfile)
196 throws IOException {
197 List<String> hfiles = familyFiles.get(family);
198 if (hfiles == null) {
199 hfiles = new LinkedList<String>();
200 familyFiles.put(family, hfiles);
201 }
202 hfiles.add(hfile);
203 }
204 });
205
206 return familyFiles;
207 }
208
209
210
211
212
213
214
215
216
217 public static Set<String> getHFileNames(final FileSystem fs, final Path snapshotDir)
218 throws IOException {
219 final Set<String> names = new HashSet<String>();
220 visitTableStoreFiles(fs, snapshotDir, new FSVisitor.StoreFileVisitor() {
221 public void storeFile (final String region, final String family, final String hfile)
222 throws IOException {
223 if (HFileLink.isHFileLink(hfile)) {
224 names.add(HFileLink.getReferencedHFileName(hfile));
225 } else {
226 names.add(hfile);
227 }
228 }
229 });
230 return names;
231 }
232
233
234
235
236
237
238
239
240
241 public static Set<String> getHLogNames(final FileSystem fs, final Path snapshotDir)
242 throws IOException {
243 final Set<String> names = new HashSet<String>();
244 visitLogFiles(fs, snapshotDir, new FSVisitor.LogFileVisitor() {
245 public void logFile (final String server, final String logfile) throws IOException {
246 names.add(logfile);
247 }
248 });
249 return names;
250 }
251 }