Skip to content

Commit 4c52628

Browse files
committed
add gauge for queue size
1 parent 4232d2e commit 4c52628

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

src/main/java/com/lowtuna/jsonblob/JsonBlobApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void run(JsonBlobConfiguration configuration, Environment environment) th
7575
ScheduledExecutorService scheduledExecutorService = configuration.getBlobManagerConfig().getScheduledExecutorService().instance(environment);
7676
ScheduledExecutorService cleanupScheduledExecutorService = configuration.getBlobManagerConfig().getCleanupScheduledExecutorService().instance(environment);
7777

78-
FileSystemJsonBlobManager fileSystemBlobManager = new FileSystemJsonBlobManager(configuration.getBlobManagerConfig().getFileSystemBlogDataDirectory(), scheduledExecutorService, cleanupScheduledExecutorService, environment.getObjectMapper(), configuration.getBlobManagerConfig().getBlobAccessTtl(), configuration.getBlobManagerConfig().isDeleteEnabled());
78+
FileSystemJsonBlobManager fileSystemBlobManager = new FileSystemJsonBlobManager(configuration.getBlobManagerConfig().getFileSystemBlogDataDirectory(), scheduledExecutorService, cleanupScheduledExecutorService, environment.getObjectMapper(), configuration.getBlobManagerConfig().getBlobAccessTtl(), configuration.getBlobManagerConfig().isDeleteEnabled(), environment.metrics());
7979
environment.lifecycle().manage(fileSystemBlobManager);
8080

8181
environment.healthChecks().register("freeSpace", new BlobDirectoryFreeSpaceHealthcheck(configuration.getBlobManagerConfig().getFileSystemBlogDataDirectory(), 5242880));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
@Slf4j
2020
@RequiredArgsConstructor
2121
public class BlobCleanupConsumer implements Runnable {
22+
private static final Duration QUEUE_TIMEOUT = Duration.seconds(15);
23+
2224
private final BlockingQueue<File> filesToProcess;
2325
private final Duration blobAccessTtl;
2426
private final FileSystemJsonBlobManager fileSystemJsonBlobManager;
@@ -27,7 +29,7 @@ public class BlobCleanupConsumer implements Runnable {
2729
@Override
2830
public void run() {
2931
try {
30-
File file = filesToProcess.take();
32+
File file = filesToProcess.poll(QUEUE_TIMEOUT.getQuantity(), QUEUE_TIMEOUT.getUnit());
3133
log.debug("Processing {}", file.getAbsolutePath());
3234
String blobId = file.getName().split("\\.", 2)[0];
3335
File metadataFile = fileSystemJsonBlobManager.getMetaDataFile(file.getParentFile());

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.lowtuna.jsonblob.core;
22

3+
import com.codahale.metrics.Gauge;
4+
import com.codahale.metrics.MetricRegistry;
35
import com.google.common.base.Stopwatch;
46
import io.dropwizard.util.Duration;
5-
import lombok.AllArgsConstructor;
67
import lombok.extern.slf4j.Slf4j;
78
import org.apache.commons.io.DirectoryWalker;
89

@@ -17,13 +18,20 @@
1718
/**
1819
* Created by tburch on 8/18/17.
1920
*/
20-
@AllArgsConstructor
2121
@Slf4j
2222
public class BlobCleanupProducer extends DirectoryWalker<Void> implements Runnable {
2323
private final Path dataDirectoryPath;
2424
private final Duration blobAccessTtl;
2525
private final BlockingQueue<File> filesToProcess;
2626

27+
public BlobCleanupProducer(Path dataDirectoryPath, Duration blobAccessTtl, BlockingQueue<File> filesToProcess, MetricRegistry metricRegistry) {
28+
this.dataDirectoryPath = dataDirectoryPath;
29+
this.blobAccessTtl = blobAccessTtl;
30+
this.filesToProcess = filesToProcess;
31+
metricRegistry.register(MetricRegistry.name(getClass(), "filesToProcessCount"), (Gauge<Integer>) () -> filesToProcess.size());
32+
}
33+
34+
2735
@Override
2836
protected boolean handleDirectory(File directory, int depth, Collection<Void> results) throws IOException {
2937
if (directory.listFiles() != null && directory.listFiles().length == 0) {
@@ -44,7 +52,7 @@ protected boolean handleDirectory(File directory, int depth, Collection<Void> re
4452
LocalDate localDate = LocalDate.of(Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[2]), Integer.parseInt(dateParts[3]));
4553
process = localDate.isBefore(LocalDate.now().minusDays(blobAccessTtl.toDays()));
4654
if (process) {
47-
log.info("Processing {} with {} blobs for un-accessed blobs", directory.getAbsolutePath(), directory.listFiles().length - 1);
55+
log.info("Processing {} blobs for un-accessed blobs", directory.getAbsolutePath());
4856
for (File file: directory.listFiles()) {
4957
try {
5058
filesToProcess.put(file);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lowtuna.jsonblob.core;
22

3+
import com.codahale.metrics.MetricRegistry;
34
import com.fasterxml.jackson.databind.ObjectMapper;
45
import com.fasterxml.uuid.Generators;
56
import com.google.common.base.Optional;
@@ -64,14 +65,16 @@ public class FileSystemJsonBlobManager implements JsonBlobManager, Runnable, Man
6465
private final Duration blobAccessTtl;
6566
@Getter
6667
private final boolean deleteEnabled;
68+
private final MetricRegistry metricRegistry;
6769

68-
public FileSystemJsonBlobManager(File blobDataDirectory, ScheduledExecutorService scheduledExecutorService, ScheduledExecutorService cleanupScheduledExecutorService, ObjectMapper objectMapper, Duration blobAccessTtl, boolean deleteEnabled) {
70+
public FileSystemJsonBlobManager(File blobDataDirectory, ScheduledExecutorService scheduledExecutorService, ScheduledExecutorService cleanupScheduledExecutorService, ObjectMapper objectMapper, Duration blobAccessTtl, boolean deleteEnabled, MetricRegistry metricRegistry) {
6971
this.blobDataDirectory = blobDataDirectory;
7072
this.scheduledExecutorService = scheduledExecutorService;
7173
this.cleanupScheduledExecutorService = cleanupScheduledExecutorService;
7274
this.objectMapper = objectMapper;
7375
this.blobAccessTtl = blobAccessTtl;
7476
this.deleteEnabled = deleteEnabled;
77+
this.metricRegistry = metricRegistry;
7578

7679
blobDataDirectory.mkdirs();
7780
}
@@ -316,7 +319,7 @@ public void start() throws Exception {
316319
cleanupScheduledExecutorService.scheduleAtFixedRate(new BlobCleanupConsumer(filesToProcess, blobAccessTtl, this, objectMapper), 0, 250, TimeUnit.MILLISECONDS);
317320
}
318321

319-
BlobCleanupProducer dataDirectoryCleaner = new BlobCleanupProducer(blobDataDirectory.toPath(), blobAccessTtl, filesToProcess);
322+
BlobCleanupProducer dataDirectoryCleaner = new BlobCleanupProducer(blobDataDirectory.toPath(), blobAccessTtl, filesToProcess, metricRegistry);
320323
cleanupScheduledExecutorService.scheduleWithFixedDelay(dataDirectoryCleaner, 0, 1, TimeUnit.DAYS);
321324
}
322325

src/test/java/com/lowtuna/jsonblob/core/TryBlobCleanupJob.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lowtuna.jsonblob.core;
22

3+
import com.codahale.metrics.MetricRegistry;
34
import com.fasterxml.jackson.databind.ObjectMapper;
45
import io.dropwizard.util.Duration;
56
import lombok.extern.java.Log;
@@ -36,7 +37,7 @@ public class TryBlobCleanupJob {
3637

3738
@Before
3839
public void initBlobManage() {
39-
this.blobManager = new FileSystemJsonBlobManager(TEMP, Executors.newSingleThreadScheduledExecutor(), Executors.newScheduledThreadPool(10), new ObjectMapper(), blobTtl, true);
40+
this.blobManager = new FileSystemJsonBlobManager(TEMP, Executors.newSingleThreadScheduledExecutor(), Executors.newScheduledThreadPool(10), new ObjectMapper(), blobTtl, true, new MetricRegistry());
4041
}
4142

4243
@Test

0 commit comments

Comments
 (0)