Skip to content

Commit 5766108

Browse files
committed
try a file walker
1 parent 17978ac commit 5766108

File tree

5 files changed

+99
-192
lines changed

5 files changed

+99
-192
lines changed

src/main/java/com/lowtuna/jsonblob/core/BlobCleanupJob.java

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.lowtuna.jsonblob.core;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.google.common.base.Optional;
5+
import com.google.common.base.Stopwatch;
6+
import com.google.common.collect.Lists;
7+
import io.dropwizard.util.Duration;
8+
import lombok.AllArgsConstructor;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.apache.commons.io.DirectoryWalker;
11+
import org.apache.commons.lang3.time.StopWatch;
12+
import org.joda.time.DateTime;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.nio.file.Path;
17+
import java.time.LocalDate;
18+
import java.util.Collection;
19+
import java.util.List;
20+
import java.util.concurrent.TimeUnit;
21+
22+
/**
23+
* Created by tburch on 8/18/17.
24+
*/
25+
@AllArgsConstructor
26+
@Slf4j
27+
public class BlobDataDirectoryCleaner extends DirectoryWalker<String> implements Runnable {
28+
private final Path dataDirectoryPath;
29+
private final Duration blobAccessTtl;
30+
private final FileSystemJsonBlobManager fileSystemJsonBlobManager;
31+
private final ObjectMapper om;
32+
33+
@Override
34+
protected void handleFile(File file, int depth, Collection<String> results) throws IOException {
35+
log.debug("Processing {}", file.getAbsolutePath());
36+
String blobId = file.getName().split("\\.", 2)[0];
37+
File metadataFile = fileSystemJsonBlobManager.getMetaDataFile(file.getParentFile());
38+
try {
39+
BlobMetadataContainer metadataContainer = metadataFile.exists() ? om.readValue(fileSystemJsonBlobManager.readFile(metadataFile), BlobMetadataContainer.class) : new BlobMetadataContainer();
40+
41+
Optional<DateTime> lastAccessed = fileSystemJsonBlobManager.resolveTimestamp(blobId);
42+
if (metadataContainer.getLastAccessedByBlobId().containsKey(blobId)) {
43+
lastAccessed = Optional.of(metadataContainer.getLastAccessedByBlobId().get(blobId));
44+
}
45+
46+
if (!lastAccessed.isPresent()) {
47+
log.warn("Couldn't get last accessed timestamp for blob {}", blobId);
48+
return;
49+
}
50+
51+
log.debug("Blob {} was last accessed {}", blobId, lastAccessed.get());
52+
53+
if (lastAccessed.get().plusMillis((int) blobAccessTtl.toMilliseconds()).isBefore(DateTime.now())) {
54+
log.debug("Blob {} is older than {}, so it's going to be deleted", blobId, blobAccessTtl);
55+
file.delete();
56+
results.add(blobId);
57+
}
58+
59+
} catch (IOException e) {
60+
log.warn("Couldn't load metadata file from {}", file.getParentFile().getAbsolutePath(), e);
61+
}
62+
}
63+
64+
@Override
65+
protected boolean handleDirectory(File directory, int depth, Collection<String> results) throws IOException {
66+
if (directory.listFiles() != null && directory.listFiles().length == 0) {
67+
log.info("{} has no files, so it's being deleted", directory.getAbsolutePath());
68+
directory.delete();
69+
return false;
70+
}
71+
72+
if (isDataDir(directory.getAbsolutePath())) {
73+
String[] dateParts = directory.getAbsolutePath().replace(dataDirectoryPath.toFile().getAbsolutePath(), "").split("/", 4);
74+
LocalDate localDate = LocalDate.of(Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[2]), Integer.parseInt(dateParts[3]));
75+
return localDate.isBefore(LocalDate.now().minusDays(blobAccessTtl.toDays()));
76+
}
77+
78+
return true;
79+
}
80+
81+
private boolean isDataDir(String path) {
82+
return path.replace(dataDirectoryPath.toFile().getAbsolutePath(), "").split("/").length == 4;
83+
}
84+
85+
@Override
86+
public void run() {
87+
Stopwatch stopwatch = new Stopwatch().start();
88+
try {
89+
List<String> removedBlobs = Lists.newArrayList();
90+
walk(dataDirectoryPath.toFile(), removedBlobs);
91+
log.info("Completed cleaning up {} un-accessed blobs in {}ms", removedBlobs.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
92+
} catch (Exception e) {
93+
e.printStackTrace();
94+
}
95+
}
96+
}

src/main/java/com/lowtuna/jsonblob/core/BulkBlobDeleteJob.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/java/com/lowtuna/jsonblob/core/DataDirectoryCleanupJob.java

Lines changed: 0 additions & 93 deletions
This file was deleted.

src/main/java/com/lowtuna/jsonblob/core/FileSystemJsonBlobManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ public void start() throws Exception {
306306
scheduledExecutorService.scheduleWithFixedDelay(this, 1, 1, TimeUnit.MINUTES);
307307

308308
log.info("Scheduling blob cleanup job");
309-
cleanupScheduledExecutorService.scheduleWithFixedDelay(new BlobCleanupJob(blobDataDirectory.toPath(), blobAccessTtl, this, objectMapper, deleteEnabled, scheduledExecutorService), 0, 1, TimeUnit.DAYS);
309+
BlobDataDirectoryCleaner dataDirectoryCleaner = new BlobDataDirectoryCleaner(blobDataDirectory.toPath(), blobAccessTtl, this,objectMapper);
310+
311+
cleanupScheduledExecutorService.scheduleWithFixedDelay(dataDirectoryCleaner, 0, 1, TimeUnit.DAYS);
310312
}
311313

312314
@Override

0 commit comments

Comments
 (0)