Skip to content

Commit d0eed45

Browse files
committed
Remove CompletableFuture from TempLocationManager
1 parent 8bcee06 commit d0eed45

File tree

1 file changed

+40
-25
lines changed
  • dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller

1 file changed

+40
-25
lines changed

dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datadog.trace.api.config.ProfilingConfig;
66
import datadog.trace.bootstrap.config.provider.ConfigProvider;
7+
import datadog.trace.util.AgentTaskScheduler;
78
import datadog.trace.util.PidHelper;
89
import java.io.IOException;
910
import java.nio.file.FileVisitResult;
@@ -17,10 +18,8 @@
1718
import java.time.Instant;
1819
import java.time.temporal.ChronoUnit;
1920
import java.util.Set;
20-
import java.util.concurrent.CompletableFuture;
21-
import java.util.concurrent.ExecutionException;
21+
import java.util.concurrent.CountDownLatch;
2222
import java.util.concurrent.TimeUnit;
23-
import java.util.concurrent.TimeoutException;
2423
import org.slf4j.Logger;
2524
import org.slf4j.LoggerFactory;
2625

@@ -64,7 +63,7 @@ default FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOE
6463
default void onCleanupStart(boolean selfCleanup, long timeout, TimeUnit unit) {}
6564
}
6665

67-
private class CleanupVisitor implements FileVisitor<Path> {
66+
private final class CleanupVisitor implements FileVisitor<Path> {
6867
private boolean shouldClean;
6968

7069
private final Set<String> pidSet = PidHelper.getJavaPids();
@@ -175,12 +174,37 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
175174
}
176175
}
177176

177+
private final class CleanupTask implements Runnable {
178+
private final CountDownLatch latch = new CountDownLatch(1);
179+
private volatile Throwable throwable = null;
180+
181+
@Override
182+
public void run() {
183+
try {
184+
cleanup(false);
185+
} catch (OutOfMemoryError oom) {
186+
throw oom;
187+
} catch (Throwable t) {
188+
throwable = t;
189+
} finally {
190+
latch.countDown();
191+
}
192+
}
193+
194+
boolean await(long timeout, TimeUnit unit) throws Throwable {
195+
boolean ret = latch.await(timeout, unit);
196+
if (throwable != null) {
197+
throw throwable;
198+
}
199+
return ret;
200+
}
201+
}
202+
178203
private final Path baseTempDir;
179204
private final Path tempDir;
180205
private final long cutoffSeconds;
181206

182-
private final CompletableFuture<Void> cleanupTask;
183-
207+
private final CleanupTask cleanupTask = new CleanupTask();
184208
private final CleanupHook cleanupTestHook;
185209

186210
/**
@@ -202,11 +226,7 @@ public static TempLocationManager getInstance() {
202226
static TempLocationManager getInstance(boolean waitForCleanup) {
203227
TempLocationManager instance = SingletonHolder.INSTANCE;
204228
if (waitForCleanup) {
205-
try {
206-
instance.waitForCleanup(5, TimeUnit.SECONDS);
207-
} catch (TimeoutException ignored) {
208-
209-
}
229+
instance.waitForCleanup(5, TimeUnit.SECONDS);
210230
}
211231
return instance;
212232
}
@@ -259,20 +279,17 @@ private TempLocationManager() {
259279
baseTempDir.toFile().deleteOnExit();
260280

261281
tempDir = baseTempDir.resolve("pid_" + pid);
262-
cleanupTask = CompletableFuture.runAsync(() -> cleanup(false));
282+
AgentTaskScheduler.INSTANCE.execute(() -> cleanup(false));
263283

264284
Thread selfCleanup =
265285
new Thread(
266286
() -> {
267-
try {
268-
waitForCleanup(1, TimeUnit.SECONDS);
269-
} catch (TimeoutException e) {
287+
if (!waitForCleanup(1, TimeUnit.SECONDS)) {
270288
log.info(
271289
"Cleanup task timed out. {} temp directory might not have been cleaned up properly",
272290
tempDir);
273-
} finally {
274-
cleanup(true);
275291
}
292+
cleanup(true);
276293
},
277294
"Temp Location Manager Cleanup");
278295
Runtime.getRuntime().addShutdownHook(selfCleanup);
@@ -362,21 +379,19 @@ boolean cleanup(boolean cleanSelf, long timeout, TimeUnit unit) {
362379
}
363380

364381
// accessible for tests
365-
void waitForCleanup(long timeout, TimeUnit unit) throws TimeoutException {
382+
boolean waitForCleanup(long timeout, TimeUnit unit) {
366383
try {
367-
cleanupTask.get(timeout, unit);
384+
return cleanupTask.await(timeout, unit);
368385
} catch (InterruptedException e) {
369-
cleanupTask.cancel(true);
386+
log.debug("Temp directory cleanup was interrupted");
370387
Thread.currentThread().interrupt();
371-
} catch (TimeoutException e) {
372-
cleanupTask.cancel(true);
373-
throw e;
374-
} catch (ExecutionException e) {
388+
} catch (Throwable t) {
375389
if (log.isDebugEnabled()) {
376-
log.debug("Failed to cleanup temp directory: {}", tempDir, e);
390+
log.debug("Failed to cleanup temp directory: {}", tempDir, t);
377391
} else {
378392
log.debug("Failed to cleanup temp directory: {}", tempDir);
379393
}
380394
}
395+
return false;
381396
}
382397
}

0 commit comments

Comments
 (0)