1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.snapshot;
19
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.Timer;
29 import java.util.TimerTask;
30
31 import com.google.common.annotations.VisibleForTesting;
32 import com.google.common.collect.Lists;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.hbase.classification.InterfaceAudience;
36 import org.apache.hadoop.hbase.classification.InterfaceStability;
37 import org.apache.hadoop.conf.Configuration;
38 import org.apache.hadoop.fs.FileStatus;
39 import org.apache.hadoop.fs.FileSystem;
40 import org.apache.hadoop.fs.Path;
41 import org.apache.hadoop.hbase.Stoppable;
42 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
43 import org.apache.hadoop.hbase.util.FSUtils;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @InterfaceAudience.Private
75 @InterfaceStability.Evolving
76 public class SnapshotFileCache implements Stoppable {
77 interface SnapshotFileInspector {
78
79
80
81
82
83 Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException;
84 }
85
86 private static final Log LOG = LogFactory.getLog(SnapshotFileCache.class);
87 private volatile boolean stop = false;
88 private final FileSystem fs;
89 private final SnapshotFileInspector fileInspector;
90 private final Path snapshotDir;
91 private final Set<String> cache = new HashSet<String>();
92
93
94
95
96 private final Map<String, SnapshotDirectoryInfo> snapshots =
97 new HashMap<String, SnapshotDirectoryInfo>();
98 private final Timer refreshTimer;
99
100 private long lastModifiedTime = Long.MIN_VALUE;
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public SnapshotFileCache(Configuration conf, long cacheRefreshPeriod, String refreshThreadName,
115 SnapshotFileInspector inspectSnapshotFiles) throws IOException {
116 this(FSUtils.getCurrentFileSystem(conf), FSUtils.getRootDir(conf), 0, cacheRefreshPeriod,
117 refreshThreadName, inspectSnapshotFiles);
118 }
119
120
121
122
123
124
125
126
127
128
129
130 public SnapshotFileCache(FileSystem fs, Path rootDir, long cacheRefreshPeriod,
131 long cacheRefreshDelay, String refreshThreadName, SnapshotFileInspector inspectSnapshotFiles) {
132 this.fs = fs;
133 this.fileInspector = inspectSnapshotFiles;
134 this.snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
135
136 this.refreshTimer = new Timer(refreshThreadName, true);
137 this.refreshTimer.scheduleAtFixedRate(new RefreshCacheTask(), cacheRefreshDelay,
138 cacheRefreshPeriod);
139 }
140
141
142
143
144
145
146
147
148
149 public void triggerCacheRefreshForTesting() {
150 try {
151 SnapshotFileCache.this.refreshCache();
152 } catch (IOException e) {
153 LOG.warn("Failed to refresh snapshot hfile cache!", e);
154 }
155 LOG.debug("Current cache:" + cache);
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public synchronized Iterable<FileStatus> getUnreferencedFiles(Iterable<FileStatus> files)
180 throws IOException {
181 List<FileStatus> unReferencedFiles = Lists.newArrayList();
182 List<String> snapshotsInProgress = null;
183 boolean refreshed = false;
184 for (FileStatus file : files) {
185 String fileName = file.getPath().getName();
186 if (!refreshed && !cache.contains(fileName)) {
187 refreshCache();
188 refreshed = true;
189 }
190 if (cache.contains(fileName)) {
191 continue;
192 }
193 if (snapshotsInProgress == null) {
194 snapshotsInProgress = getSnapshotsInProgress();
195 }
196 if (snapshotsInProgress.contains(fileName)) {
197 continue;
198 }
199 unReferencedFiles.add(file);
200 }
201 return unReferencedFiles;
202 }
203
204 private synchronized void refreshCache() throws IOException {
205 long lastTimestamp = Long.MAX_VALUE;
206 boolean hasChanges = false;
207
208
209 try {
210 FileStatus dirStatus = fs.getFileStatus(snapshotDir);
211 lastTimestamp = dirStatus.getModificationTime();
212 hasChanges |= (lastTimestamp >= lastModifiedTime);
213 } catch (FileNotFoundException e) {
214 if (this.cache.size() > 0) {
215 LOG.error("Snapshot directory: " + snapshotDir + " doesn't exist");
216 }
217 return;
218 }
219
220
221
222 try {
223 Path snapshotTmpDir = new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOT_TMP_DIR_NAME);
224 FileStatus tempDirStatus = fs.getFileStatus(snapshotTmpDir);
225 lastTimestamp = Math.min(lastTimestamp, tempDirStatus.getModificationTime());
226 hasChanges |= (lastTimestamp >= lastModifiedTime);
227 if (!hasChanges) {
228 FileStatus[] tmpSnapshots = FSUtils.listStatus(fs, snapshotDir);
229 if (tmpSnapshots != null) {
230 for (FileStatus dirStatus: tmpSnapshots) {
231 lastTimestamp = Math.min(lastTimestamp, dirStatus.getModificationTime());
232 }
233 hasChanges |= (lastTimestamp >= lastModifiedTime);
234 }
235 }
236 } catch (FileNotFoundException e) {
237
238 }
239
240
241 if (!hasChanges) {
242 return;
243 }
244
245
246
247
248
249
250
251 this.lastModifiedTime = lastTimestamp;
252
253
254 this.cache.clear();
255 Map<String, SnapshotDirectoryInfo> known = new HashMap<String, SnapshotDirectoryInfo>();
256
257
258 FileStatus[] snapshots = FSUtils.listStatus(fs, snapshotDir);
259 if (snapshots == null) {
260
261 if (LOG.isDebugEnabled() && this.snapshots.size() > 0) {
262 LOG.debug("No snapshots on-disk, cache empty");
263 }
264 this.snapshots.clear();
265 return;
266 }
267
268
269 for (FileStatus snapshot : snapshots) {
270 String name = snapshot.getPath().getName();
271
272 if (!name.equals(SnapshotDescriptionUtils.SNAPSHOT_TMP_DIR_NAME)) {
273 SnapshotDirectoryInfo files = this.snapshots.remove(name);
274
275
276
277
278
279 if (files == null || files.hasBeenModified(snapshot.getModificationTime())) {
280
281 Collection<String> storedFiles = fileInspector.filesUnderSnapshot(snapshot.getPath());
282 files = new SnapshotDirectoryInfo(snapshot.getModificationTime(), storedFiles);
283 }
284
285 this.cache.addAll(files.getFiles());
286 known.put(name, files);
287 }
288 }
289
290
291 this.snapshots.clear();
292 this.snapshots.putAll(known);
293 }
294
295 @VisibleForTesting List<String> getSnapshotsInProgress() throws IOException {
296 List<String> snapshotInProgress = Lists.newArrayList();
297
298 Path snapshotTmpDir = new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOT_TMP_DIR_NAME);
299
300 FileStatus[] running = FSUtils.listStatus(fs, snapshotTmpDir);
301 if (running != null) {
302 for (FileStatus run : running) {
303 snapshotInProgress.addAll(fileInspector.filesUnderSnapshot(run.getPath()));
304 }
305 }
306 return snapshotInProgress;
307 }
308
309
310
311
312 public class RefreshCacheTask extends TimerTask {
313 @Override
314 public void run() {
315 try {
316 SnapshotFileCache.this.refreshCache();
317 } catch (IOException e) {
318 LOG.warn("Failed to refresh snapshot hfile cache!", e);
319 }
320 }
321 }
322
323 @Override
324 public void stop(String why) {
325 if (!this.stop) {
326 this.stop = true;
327 this.refreshTimer.cancel();
328 }
329
330 }
331
332 @Override
333 public boolean isStopped() {
334 return this.stop;
335 }
336
337
338
339
340 private static class SnapshotDirectoryInfo {
341 long lastModified;
342 Collection<String> files;
343
344 public SnapshotDirectoryInfo(long mtime, Collection<String> files) {
345 this.lastModified = mtime;
346 this.files = files;
347 }
348
349
350
351
352 public Collection<String> getFiles() {
353 return this.files;
354 }
355
356
357
358
359
360
361
362 public boolean hasBeenModified(long mtime) {
363 return this.lastModified < mtime;
364 }
365 }
366 }