From d48e3915ff292786b5a2196c8e296d04445a3745 Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 27 Sep 2024 02:03:26 -0700 Subject: [PATCH] Use a dedicated thread pool for disk cache garbage collection. The ExecutorService created in RemoteModule#beforeCommand is intended to have command scope, so it won't be available during idle periods. (The current state doesn't work, either, because we pass it into the DiskCacheGarbageCollectorIdleTask before initializing it. Oops.) PiperOrigin-RevId: 679497012 Change-Id: I8e791580c2db078c6d39cb760936b8fbcba4ed3d --- .../google/devtools/build/lib/remote/RemoteModule.java | 3 +-- .../remote/disk/DiskCacheGarbageCollectorIdleTask.java | 8 +++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java index 63ff81af1d72ea..8557bcad155e19 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java @@ -344,8 +344,7 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException { if (enableDiskCache) { var gcIdleTask = - DiskCacheGarbageCollectorIdleTask.create( - remoteOptions, env.getWorkingDirectory(), executorService); + DiskCacheGarbageCollectorIdleTask.create(remoteOptions, env.getWorkingDirectory()); if (gcIdleTask != null) { env.addIdleTask(gcIdleTask); } diff --git a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheGarbageCollectorIdleTask.java b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheGarbageCollectorIdleTask.java index 8f9f68e3805436..ddccdbacd5f120 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheGarbageCollectorIdleTask.java +++ b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheGarbageCollectorIdleTask.java @@ -17,6 +17,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.flogger.GoogleLogger; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.devtools.build.lib.remote.disk.DiskCacheGarbageCollector.CollectionPolicy; import com.google.devtools.build.lib.remote.disk.DiskCacheGarbageCollector.CollectionStats; import com.google.devtools.build.lib.remote.options.RemoteOptions; @@ -26,6 +27,7 @@ import java.time.Duration; import java.util.Optional; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import javax.annotation.Nullable; /** An {@link IdleTask} to run a {@link DiskCacheGarbageCollector}. */ @@ -35,6 +37,10 @@ public final class DiskCacheGarbageCollectorIdleTask implements IdleTask { private final Duration delay; private final DiskCacheGarbageCollector gc; + private static final ExecutorService executorService = + Executors.newCachedThreadPool( + new ThreadFactoryBuilder().setNameFormat("disk-cache-gc-%d").build()); + private DiskCacheGarbageCollectorIdleTask(Duration delay, DiskCacheGarbageCollector gc) { this.delay = delay; this.gc = gc; @@ -50,7 +56,7 @@ private DiskCacheGarbageCollectorIdleTask(Duration delay, DiskCacheGarbageCollec */ @Nullable public static DiskCacheGarbageCollectorIdleTask create( - RemoteOptions remoteOptions, Path workingDirectory, ExecutorService executorService) { + RemoteOptions remoteOptions, Path workingDirectory) { if (remoteOptions.diskCache == null || remoteOptions.diskCache.isEmpty()) { return null; }