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