Skip to content

Commit c366f86

Browse files
committed
no face threading
1 parent e817ac0 commit c366f86

File tree

4 files changed

+46
-96
lines changed

4 files changed

+46
-96
lines changed

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

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

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package com.lowtuna.jsonblob.core;
22

3-
import com.codahale.metrics.Gauge;
4-
import com.codahale.metrics.MetricRegistry;
3+
import com.fasterxml.jackson.core.JsonParseException;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.google.common.base.Optional;
57
import com.google.common.base.Stopwatch;
68
import io.dropwizard.util.Duration;
79
import lombok.extern.slf4j.Slf4j;
810
import org.apache.commons.io.DirectoryWalker;
11+
import org.joda.time.DateTime;
912

1013
import java.io.File;
1114
import java.io.IOException;
1215
import java.nio.file.Files;
1316
import java.nio.file.Path;
1417
import java.time.LocalDate;
1518
import java.util.Collection;
16-
import java.util.concurrent.BlockingQueue;
1719
import java.util.concurrent.TimeUnit;
1820
import java.util.concurrent.atomic.AtomicInteger;
1921

@@ -24,14 +26,15 @@
2426
public class BlobCleanupProducer extends DirectoryWalker<Void> implements Runnable {
2527
private final Path dataDirectoryPath;
2628
private final Duration blobAccessTtl;
27-
private final BlockingQueue<File> filesToProcess;
29+
private final FileSystemJsonBlobManager fileSystemJsonBlobManager;
30+
private final ObjectMapper om;
2831

29-
public BlobCleanupProducer(Path dataDirectoryPath, Duration blobAccessTtl, BlockingQueue<File> filesToProcess, MetricRegistry metricRegistry) {
32+
public BlobCleanupProducer(Path dataDirectoryPath, Duration blobAccessTtl, FileSystemJsonBlobManager fileSystemJsonBlobManager, ObjectMapper om) {
3033
super(null, 3);
3134
this.dataDirectoryPath = dataDirectoryPath;
3235
this.blobAccessTtl = blobAccessTtl;
33-
this.filesToProcess = filesToProcess;
34-
metricRegistry.register(MetricRegistry.name(getClass(), "filesToProcessCount"), (Gauge<Integer>) () -> filesToProcess.size());
36+
this.fileSystemJsonBlobManager = fileSystemJsonBlobManager;
37+
this.om = om;
3538
}
3639

3740

@@ -51,10 +54,41 @@ protected boolean handleDirectory(File directory, int depth, Collection<Void> re
5154
if (file.getName().startsWith(FileSystemJsonBlobManager.BLOB_METADATA_FILE_NAME)) {
5255
return;
5356
}
57+
5458
try {
55-
filesToProcess.put(file);
56-
} catch (InterruptedException e) {
57-
log.warn("Interrupted while trying to add file to be processed at {}", file.getAbsolutePath(), e);
59+
log.debug("Processing {}", file.getAbsolutePath());
60+
String blobId = file.getName().split("\\.", 2)[0];
61+
File metadataFile = fileSystemJsonBlobManager.getMetaDataFile(file.getParentFile());
62+
63+
if (file.equals(metadataFile)) {
64+
return;
65+
}
66+
67+
BlobMetadataContainer metadataContainer = metadataFile.exists() ? om.readValue(fileSystemJsonBlobManager.readFile(metadataFile), BlobMetadataContainer.class) : new BlobMetadataContainer();
68+
69+
Optional<DateTime> lastAccessed = fileSystemJsonBlobManager.resolveTimestamp(blobId);
70+
if (metadataContainer.getLastAccessedByBlobId().containsKey(blobId)) {
71+
lastAccessed = Optional.of(metadataContainer.getLastAccessedByBlobId().get(blobId));
72+
}
73+
74+
if (!lastAccessed.isPresent()) {
75+
log.warn("Couldn't get last accessed timestamp for blob {}", blobId);
76+
return;
77+
}
78+
79+
log.debug("Blob {} was last accessed {}", blobId, lastAccessed.get());
80+
81+
if (lastAccessed.get().plusMillis((int) blobAccessTtl.toMilliseconds()).isBefore(DateTime.now())) {
82+
if (file.delete()) {
83+
log.info("Blob {} hasn't been accessed in {} (last accessed {}), so it's going to be deleted", blobId, blobAccessTtl, lastAccessed.get());
84+
}
85+
}
86+
} catch (JsonParseException e) {
87+
log.warn("Couldn't parse JSON from BlobMetadataContainer", e);
88+
} catch (JsonMappingException e) {
89+
log.warn("Couldn't map JSON from BlobMetadataContainer", e);
90+
} catch (IOException e) {
91+
log.warn("Couldn't read json for BlobMetadataContainer file", e);
5892
}
5993
});
6094

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import java.util.Map;
3333
import java.util.TimeZone;
3434
import java.util.UUID;
35-
import java.util.concurrent.ArrayBlockingQueue;
36-
import java.util.concurrent.BlockingQueue;
3735
import java.util.concurrent.ConcurrentMap;
3836
import java.util.concurrent.ScheduledExecutorService;
3937
import java.util.concurrent.TimeUnit;
@@ -50,7 +48,6 @@
5048
public class FileSystemJsonBlobManager implements JsonBlobManager, Runnable, Managed {
5149
static final String BLOB_METADATA_FILE_NAME = "blobMetadata";
5250

53-
private static final int CONSUMER_COUNT = 7;
5451
private static final DateTimeFormatter DIRECTORY_FORMAT = DateTimeFormat.forPattern("yyyy/MM/dd");
5552

5653
@GuardedBy("lastAccessedLock")
@@ -313,13 +310,7 @@ public void start() throws Exception {
313310
log.info("Scheduling the updating of blob last accessed timestamps");
314311
scheduledExecutorService.scheduleWithFixedDelay(this, 1, 1, TimeUnit.MINUTES);
315312

316-
log.info("Scheduling blob cleanup job");
317-
BlockingQueue<File> filesToProcess = new ArrayBlockingQueue<>(1024);
318-
for (int i = 0; i < CONSUMER_COUNT; i++) {
319-
cleanupScheduledExecutorService.scheduleAtFixedRate(new BlobCleanupConsumer(filesToProcess, blobAccessTtl, this, objectMapper), 0, 1, TimeUnit.SECONDS);
320-
}
321-
322-
BlobCleanupProducer dataDirectoryCleaner = new BlobCleanupProducer(blobDataDirectory.toPath(), blobAccessTtl, filesToProcess, metricRegistry);
313+
BlobCleanupProducer dataDirectoryCleaner = new BlobCleanupProducer(blobDataDirectory.toPath(), blobAccessTtl, this, objectMapper);
323314
cleanupScheduledExecutorService.scheduleWithFixedDelay(dataDirectoryCleaner, 0, 1, TimeUnit.DAYS);
324315
}
325316

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void run() {
3636
updatesByMetadataFile.put(metadataFile, Pair.of(entry.getKey(), entry.getValue()));
3737
}
3838

39-
updatesByMetadataFile.keySet().parallelStream().forEach(metadataFile -> {
39+
updatesByMetadataFile.keySet().stream().forEach(metadataFile -> {
4040
try {
4141
log.info(metadataFile.exists() ? "Reading metadata file at {}" : "No metadata file exists yet, so creating a new one", metadataFile.getAbsolutePath());
4242
BlobMetadataContainer metadataContainer = metadataFile.exists() ? om.readValue(fileSystemJsonBlobManager.readFile(metadataFile), BlobMetadataContainer.class) : new BlobMetadataContainer();

0 commit comments

Comments
 (0)