Skip to content

Commit

Permalink
[7.4.0] Implement disk cache garbage collection. (bazelbuild#23833)
Browse files Browse the repository at this point in the history
By cherry-picking the following changes:

* 33aca3f
* 85b100b
* 08cd046
* ca802d6
* 1690067
* 6096b5e
* 041faf5
* 46d5502
* d48e391
* c43cf67
* db4a400
* 6922734
* 544e235
* b09cd65
* c72af54
* 5997fde
* caa2c41
  • Loading branch information
tjgq authored Oct 2, 2024
1 parent f5fa97d commit 00ed3e2
Show file tree
Hide file tree
Showing 23 changed files with 1,316 additions and 17 deletions.
1 change: 1 addition & 0 deletions .bazelci/postsubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tasks:
- "//src:bazel_jdk_minimal"
- "//src:test_repos"
- "//src/main/java/..."
- "//src/tools/diskcache/..."
- "//src/tools/execlog/..."
test_flags:
- "--config=ci-linux"
Expand Down
1 change: 1 addition & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tasks:
- "//src:bazel_jdk_minimal"
- "//src:test_repos"
- "//src/main/java/..."
- "//src/tools/diskcache/..."
- "//src/tools/execlog/..."
test_flags:
- "--config=ci-linux"
Expand Down
17 changes: 14 additions & 3 deletions site/en/remote/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,8 @@ This feature is unsupported on Windows.

Bazel can use a directory on the file system as a remote cache. This is
useful for sharing build artifacts when switching branches and/or working
on multiple workspaces of the same project, such as multiple checkouts. Since
Bazel does not garbage-collect the directory, you might want to automate a
periodic cleanup of this directory. Enable the disk cache as follows:
on multiple workspaces of the same project, such as multiple checkouts.
Enable the disk cache as follows:

```posix-terminal
build --disk_cache={{ '<var>' }}path/to/build/cache{{ '</var>' }}
Expand All @@ -305,6 +304,18 @@ You can pass a user-specific path to the `--disk_cache` flag using the `~` alias
when enabling the disk cache for all developers of a project via the project's
checked in `.bazelrc` file.

### Garbage collection {:#disk-cache-gc}

Starting with Bazel 7.4, you can use `--experimental_disk_cache_gc_max_size` and
`--experimental_disk_cache_gc_max_age` to set a maximum size for the disk cache
or for the age of individual cache entries. Bazel will automatically garbage
collect the disk cache while idling between builds; the idle timer can be set
with `--experimental_disk_cache_gc_idle_delay` (defaulting to 5 minutes).

As an alternative to automatic garbage collection, we also provide a [tool](
https://github.com/bazelbuild/bazel/tree/master/src/tools/diskcache) to run a
garbage collection on demand.

## Known issues {:#known-issues}

**Input file modification during a build**
Expand Down
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ filegroup(
"//src/tools/android:srcs",
"//src/tools/android/java/com/google/devtools/build/android:srcs",
"//src/tools/bzlmod:srcs",
"//src/tools/diskcache:srcs",
"//src/tools/execlog:srcs",
"//src/tools/launcher:srcs",
"//src/tools/remote:srcs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import com.google.devtools.build.lib.remote.circuitbreaker.CircuitBreakerFactory;
import com.google.devtools.build.lib.remote.common.RemoteCacheClient;
import com.google.devtools.build.lib.remote.common.RemoteExecutionClient;
import com.google.devtools.build.lib.remote.disk.DiskCacheClient;
import com.google.devtools.build.lib.remote.disk.DiskCacheGarbageCollectorIdleTask;
import com.google.devtools.build.lib.remote.downloader.GrpcRemoteDownloader;
import com.google.devtools.build.lib.remote.http.DownloadTimeoutException;
import com.google.devtools.build.lib.remote.http.HttpException;
Expand Down Expand Up @@ -335,6 +337,14 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
}
}

if (enableDiskCache) {
var gcIdleTask =
DiskCacheGarbageCollectorIdleTask.create(remoteOptions, env.getWorkingDirectory());
if (gcIdleTask != null) {
env.addIdleTask(gcIdleTask);
}
}

if (!enableDiskCache && !enableHttpCache && !enableGrpcCache && !enableRemoteExecution) {
// Quit if no remote caching or execution was enabled.
actionContextProvider =
Expand Down Expand Up @@ -954,9 +964,9 @@ public void afterCommand() {
}

private static void afterCommandTask(
RemoteActionContextProvider actionContextProvider,
TempPathGenerator tempPathGenerator,
AsynchronousMessageOutputStream<LogEntry> rpcLogFile)
@Nullable RemoteActionContextProvider actionContextProvider,
@Nullable TempPathGenerator tempPathGenerator,
@Nullable AsynchronousMessageOutputStream<LogEntry> rpcLogFile)
throws AbruptExitException {
if (actionContextProvider != null) {
actionContextProvider.afterCommand();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/google/devtools/build/lib/remote/disk/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ java_library(
name = "disk",
srcs = glob(["*.java"]),
deps = [
"//src/main/java/com/google/devtools/build/lib/concurrent",
"//src/main/java/com/google/devtools/build/lib/exec:spawn_runner",
"//src/main/java/com/google/devtools/build/lib/remote:store",
"//src/main/java/com/google/devtools/build/lib/remote/common",
"//src/main/java/com/google/devtools/build/lib/remote/common:cache_not_found_exception",
"//src/main/java/com/google/devtools/build/lib/remote/options",
"//src/main/java/com/google/devtools/build/lib/remote/util",
"//src/main/java/com/google/devtools/build/lib/server:idle_task",
"//src/main/java/com/google/devtools/build/lib/util:string",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//third_party:flogger",
"//third_party:guava",
"//third_party:jsr305",
"//third_party/protobuf:protobuf_java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@
* when they collide.
*
* <p>The mtime of an entry reflects the most recent time the entry was stored *or* retrieved. This
* property may be used to trim the disk cache to the most recently used entries. However, it's not
* safe to trim the cache at the same time a Bazel process is accessing it.
* property may be used to garbage collect the disk cache by deleting the least recently accessed
* entries. This may be done by Bazel itself (see {@link DiskCacheGarbageCollectorIdleTask}), by
* another Bazel process sharing the disk cache, or by an external process. Although we could have
* arranged for an ongoing garbage collection to block a concurrent build, we judge it to not be
* worth the extra complexity; assuming that the collection policy is not overly aggressive, the
* likelihood of a race condition is fairly small, and an affected build is able to automatically
* recover by retrying.
*/
public class DiskCacheClient implements RemoteCacheClient {

Expand Down
Loading

0 comments on commit 00ed3e2

Please sign in to comment.