Skip to content

Commit 7f08b78

Browse files
coeuvrecopybara-github
authored andcommitted
Remote: Async upload (Part 7)
Add --experimental_remote_cache_async flag, which when enabled, makes the uploads happen in the background. Part of #13655. PiperOrigin-RevId: 395020967
1 parent 862fd5e commit 7f08b78

15 files changed

Lines changed: 356 additions & 219 deletions

‎src/main/java/com/google/devtools/build/lib/remote/BUILD‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ java_library(
5656
"//src/main/java/com/google/devtools/build/lib/authandtls",
5757
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader",
5858
"//src/main/java/com/google/devtools/build/lib/buildeventstream",
59-
"//src/main/java/com/google/devtools/build/lib/collect",
6059
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",
6160
"//src/main/java/com/google/devtools/build/lib/concurrent",
6261
"//src/main/java/com/google/devtools/build/lib/events",
6362
"//src/main/java/com/google/devtools/build/lib/exec:abstract_spawn_strategy",
6463
"//src/main/java/com/google/devtools/build/lib/exec:execution_options",
6564
"//src/main/java/com/google/devtools/build/lib/exec:executor_builder",
66-
"//src/main/java/com/google/devtools/build/lib/exec:executor_lifecycle_listener",
6765
"//src/main/java/com/google/devtools/build/lib/exec:module_action_context_registry",
6866
"//src/main/java/com/google/devtools/build/lib/exec:remote_local_fallback_registry",
6967
"//src/main/java/com/google/devtools/build/lib/exec:spawn_cache",

‎src/main/java/com/google/devtools/build/lib/remote/RemoteActionContextProvider.java‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
import com.google.common.base.Preconditions;
1919
import com.google.common.collect.ImmutableSet;
2020
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
21-
import com.google.devtools.build.lib.actions.ActionGraph;
2221
import com.google.devtools.build.lib.actions.ActionInput;
23-
import com.google.devtools.build.lib.actions.Artifact;
2422
import com.google.devtools.build.lib.exec.ExecutionOptions;
25-
import com.google.devtools.build.lib.exec.ExecutorLifecycleListener;
2623
import com.google.devtools.build.lib.exec.ModuleActionContextRegistry;
2724
import com.google.devtools.build.lib.exec.SpawnCache;
2825
import com.google.devtools.build.lib.exec.SpawnStrategyRegistry;
@@ -35,11 +32,10 @@
3532
import com.google.devtools.build.lib.remote.util.DigestUtil;
3633
import com.google.devtools.build.lib.runtime.CommandEnvironment;
3734
import com.google.devtools.build.lib.vfs.Path;
38-
import java.util.function.Supplier;
3935
import javax.annotation.Nullable;
4036

4137
/** Provides a remote execution context. */
42-
final class RemoteActionContextProvider implements ExecutorLifecycleListener {
38+
final class RemoteActionContextProvider {
4339

4440
private final CommandEnvironment env;
4541
@Nullable private final RemoteCache cache;
@@ -120,8 +116,12 @@ private RemoteExecutionService getRemoteExecutionService() {
120116
workingDirectory.getRelative(remoteOptions.remoteCaptureCorruptedOutputs);
121117
}
122118

119+
boolean verboseFailures =
120+
checkNotNull(env.getOptions().getOptions(ExecutionOptions.class)).verboseFailures;
123121
remoteExecutionService =
124122
new RemoteExecutionService(
123+
env.getReporter(),
124+
verboseFailures,
125125
env.getExecRoot(),
126126
createRemotePathResolver(),
127127
env.getBuildRequestId(),
@@ -132,6 +132,7 @@ private RemoteExecutionService getRemoteExecutionService() {
132132
executor,
133133
filesToDownload,
134134
captureCorruptedOutputsDir);
135+
env.getEventBus().register(remoteExecutionService);
135136
}
136137

137138
return remoteExecutionService;
@@ -189,20 +190,16 @@ void setFilesToDownload(ImmutableSet<ActionInput> topLevelOutputs) {
189190
this.filesToDownload = Preconditions.checkNotNull(topLevelOutputs, "filesToDownload");
190191
}
191192

192-
@Override
193-
public void executorCreated() {}
194-
195-
@Override
196-
public void executionPhaseStarting(
197-
ActionGraph actionGraph, Supplier<ImmutableSet<Artifact>> topLevelArtifacts) {}
198-
199-
@Override
200-
public void executionPhaseEnding() {
201-
if (cache != null) {
202-
cache.close();
203-
}
204-
if (executor != null) {
205-
executor.close();
193+
public void afterCommand() {
194+
if (remoteExecutionService != null) {
195+
remoteExecutionService.shutdown();
196+
} else {
197+
if (cache != null) {
198+
cache.release();
199+
}
200+
if (executor != null) {
201+
executor.close();
202+
}
206203
}
207204
}
208205
}

‎src/main/java/com/google/devtools/build/lib/remote/RemoteCache.java‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,36 @@
5151
import com.google.devtools.build.lib.vfs.FileSystemUtils;
5252
import com.google.devtools.build.lib.vfs.Path;
5353
import com.google.protobuf.ByteString;
54+
import io.netty.util.AbstractReferenceCounted;
5455
import io.reactivex.rxjava3.core.Completable;
5556
import java.io.ByteArrayOutputStream;
5657
import java.io.IOException;
5758
import java.io.OutputStream;
5859
import java.util.ArrayList;
5960
import java.util.List;
61+
import java.util.concurrent.CountDownLatch;
6062
import java.util.concurrent.atomic.AtomicLong;
6163
import java.util.regex.Matcher;
6264
import java.util.regex.Pattern;
6365

64-
/** A cache for storing artifacts (input and output) as well as the output of running an action. */
66+
/**
67+
* A cache for storing artifacts (input and output) as well as the output of running an action.
68+
*
69+
* <p>The cache is reference counted. Initially, the reference count is 1. Use {@link #retain()} to
70+
* increase and {@link #release()} to decrease the reference count respectively. Once the reference
71+
* count is reached to 0, the underlying resources will be released (after network I/Os finished).
72+
*
73+
* <p>Use {@link #awaitTermination()} to wait for the underlying network I/Os to finish. Use {@link
74+
* #shutdownNow()} to cancel all active network I/Os and reject new requests.
75+
*/
6576
@ThreadSafety.ThreadSafe
66-
public class RemoteCache implements AutoCloseable {
77+
public class RemoteCache extends AbstractReferenceCounted {
6778
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
6879

6980
private static final ListenableFuture<Void> COMPLETED_SUCCESS = immediateFuture(null);
7081
private static final ListenableFuture<byte[]> EMPTY_BYTES = immediateFuture(new byte[0]);
7182

83+
private final CountDownLatch closeCountDownLatch = new CountDownLatch(1);
7284
protected final AsyncTaskCache.NoResult<Digest> casUploadCache = AsyncTaskCache.NoResult.create();
7385

7486
protected final RemoteCacheClient cacheProtocol;
@@ -95,7 +107,7 @@ public CachedActionResult downloadActionResult(
95107
public ListenableFuture<ImmutableSet<Digest>> findMissingDigests(
96108
RemoteActionExecutionContext context, Iterable<Digest> digests) {
97109
if (Iterables.isEmpty(digests)) {
98-
return Futures.immediateFuture(ImmutableSet.of());
110+
return immediateFuture(ImmutableSet.of());
99111
}
100112
return cacheProtocol.findMissingDigests(context, digests);
101113
}
@@ -426,10 +438,34 @@ public final List<ListenableFuture<Void>> downloadOutErr(
426438
return downloads;
427439
}
428440

429-
/** Release resources associated with the cache. The cache may not be used after calling this. */
430441
@Override
431-
public void close() {
442+
protected void deallocate() {
443+
casUploadCache.shutdown();
432444
cacheProtocol.close();
445+
446+
closeCountDownLatch.countDown();
447+
}
448+
449+
@Override
450+
public RemoteCache touch(Object o) {
451+
return this;
452+
}
453+
454+
@Override
455+
public RemoteCache retain() {
456+
super.retain();
457+
return this;
458+
}
459+
460+
/** Waits for active network I/Os to finish. */
461+
public void awaitTermination() throws InterruptedException {
462+
casUploadCache.awaitTermination();
463+
closeCountDownLatch.await();
464+
}
465+
466+
/** Shuts the cache down and cancels active network I/Os. */
467+
public void shutdownNow() {
468+
casUploadCache.shutdownNow();
433469
}
434470

435471
public static FailureDetail createFailureDetail(String message, Code detailedCode) {

0 commit comments

Comments
 (0)