Skip to content

Commit 3b39ab8

Browse files
coeuvrecopybara-github
authored andcommitted
Add ActionExecutionMetadata as a parameter to ActionInputPrefetcher#prefetchFiles.
Due to recent overhual of `prefetchFiles`, the call sites now always have the reference to `ActionExecutionMetadata`, which makes it possible to pass it to `prefetchFiles`. The reason why we want to access `ActionExecutionMetadata` inside `prefetchFiles` is to associate downloads with actions. For example: 1. When downloading from remote cache, we want to provide action metadata to the RPCs so that remote server can associate them with actions. `actionId` was removed from remote file metadata by f62a8b9 because it is not the right action to associate with for downloads inside this context. We left the action metadata empty for RPCs until this change. #17724 (comment) 2. When building with the bytes, we are able to report download progress to UI. However, `prefetchFiles` is the major way to download when building without the bytes and we can't report download progress on action basis. #17604 added the download report for background downloads, but now, we no longer download toplevel artifacts in background (a5dde12). With this change, we are able to report download progress for each action, either when they download outputs, or prefetch inputs for building without the bytes. PiperOrigin-RevId: 539635790 Change-Id: Ic9cc1024f5d3560ac46bc462bd549dd81712d92f
1 parent 0573eee commit 3b39ab8

11 files changed

Lines changed: 96 additions & 82 deletions

‎src/main/java/com/google/devtools/build/lib/actions/ActionInputPrefetcher.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface MetadataSupplier {
3434
new ActionInputPrefetcher() {
3535
@Override
3636
public ListenableFuture<Void> prefetchFiles(
37+
ActionExecutionMetadata action,
3738
Iterable<? extends ActionInput> inputs,
3839
MetadataSupplier metadataSupplier,
3940
Priority priority) {
@@ -79,7 +80,10 @@ public enum Priority {
7980
* @return future success if prefetch is finished or {@link IOException}.
8081
*/
8182
ListenableFuture<Void> prefetchFiles(
82-
Iterable<? extends ActionInput> inputs, MetadataSupplier metadataSupplier, Priority priority);
83+
ActionExecutionMetadata action,
84+
Iterable<? extends ActionInput> inputs,
85+
MetadataSupplier metadataSupplier,
86+
Priority priority);
8387

8488
/**
8589
* Whether the prefetcher requires the metadata for a tree artifact to be available whenever one

‎src/main/java/com/google/devtools/build/lib/exec/AbstractSpawnStrategy.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ public ListenableFuture<Void> prefetchInputs()
252252
actionExecutionContext
253253
.getActionInputPrefetcher()
254254
.prefetchFiles(
255+
spawn.getResourceOwner(),
255256
getInputMapping(PathFragment.EMPTY_FRAGMENT, /* willAccessRepeatedly= */ true)
256257
.values(),
257258
getInputMetadataProvider()::getInputMetadata,

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.common.flogger.GoogleLogger;
3030
import com.google.common.util.concurrent.ListenableFuture;
3131
import com.google.devtools.build.lib.actions.Action;
32+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
3233
import com.google.devtools.build.lib.actions.ActionInput;
3334
import com.google.devtools.build.lib.actions.ActionInputPrefetcher;
3435
import com.google.devtools.build.lib.actions.Artifact;
@@ -263,6 +264,7 @@ private boolean shouldDownloadFile(Path path, FileArtifactValue metadata) {
263264
* @param tempPath the temporary path which the input should be written to.
264265
*/
265266
protected abstract ListenableFuture<Void> doDownloadFile(
267+
ActionExecutionMetadata action,
266268
Reporter reporter,
267269
Path tempPath,
268270
PathFragment execPath,
@@ -285,6 +287,7 @@ protected void prefetchVirtualActionInput(VirtualActionInput input) throws IOExc
285287
*/
286288
@Override
287289
public ListenableFuture<Void> prefetchFiles(
290+
ActionExecutionMetadata action,
288291
Iterable<? extends ActionInput> inputs,
289292
MetadataSupplier metadataSupplier,
290293
Priority priority) {
@@ -308,7 +311,8 @@ public ListenableFuture<Void> prefetchFiles(
308311

309312
Flowable<TransferResult> transfers =
310313
Flowable.fromIterable(files)
311-
.flatMapSingle(input -> prefetchFile(dirCtx, metadataSupplier, input, priority));
314+
.flatMapSingle(
315+
input -> prefetchFile(action, dirCtx, metadataSupplier, input, priority));
312316

313317
Completable prefetch =
314318
Completable.using(
@@ -318,6 +322,7 @@ public ListenableFuture<Void> prefetchFiles(
318322
}
319323

320324
private Single<TransferResult> prefetchFile(
325+
ActionExecutionMetadata action,
321326
DirectoryContext dirCtx,
322327
MetadataSupplier metadataSupplier,
323328
ActionInput input,
@@ -347,6 +352,7 @@ private Single<TransferResult> prefetchFile(
347352

348353
Completable result =
349354
downloadFileNoCheckRx(
355+
action,
350356
dirCtx,
351357
execRoot.getRelative(execPath),
352358
treeRootExecPath != null ? execRoot.getRelative(treeRootExecPath) : null,
@@ -421,6 +427,7 @@ private Symlink maybeGetSymlink(
421427
}
422428

423429
private Completable downloadFileNoCheckRx(
430+
ActionExecutionMetadata action,
424431
DirectoryContext dirCtx,
425432
Path path,
426433
@Nullable Path treeRoot,
@@ -445,6 +452,7 @@ private Completable downloadFileNoCheckRx(
445452
toCompletable(
446453
() ->
447454
doDownloadFile(
455+
action,
448456
reporter,
449457
tempPath,
450458
finalPath.relativeTo(execRoot),
@@ -603,7 +611,7 @@ public void finalizeAction(Action action, OutputMetadataStore outputMetadataStor
603611
try (var s = Profiler.instance().profile(ProfilerTask.REMOTE_DOWNLOAD, "Download outputs")) {
604612
getFromFuture(
605613
prefetchFiles(
606-
outputsToDownload, outputMetadataStore::getOutputMetadata, Priority.HIGH));
614+
action, outputsToDownload, outputMetadataStore::getOutputMetadata, Priority.HIGH));
607615
}
608616
}
609617
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.annotations.VisibleForTesting;
2626
import com.google.common.collect.ImmutableList;
2727
import com.google.common.collect.ImmutableMap;
28+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
2829
import com.google.devtools.build.lib.actions.ActionInput;
2930
import com.google.devtools.build.lib.actions.ActionInputHelper;
3031
import com.google.devtools.build.lib.actions.ActionInputMap;
@@ -81,6 +82,7 @@ public class RemoteActionFileSystem extends DelegateFileSystem {
8182
private final RemoteActionInputFetcher inputFetcher;
8283
private final RemoteInMemoryFileSystem remoteOutputTree;
8384

85+
@Nullable private ActionExecutionMetadata action = null;
8486
@Nullable private MetadataInjector metadataInjector = null;
8587

8688
RemoteActionFileSystem(
@@ -121,7 +123,8 @@ private boolean isRemote(PathFragment path) {
121123
return getRemoteMetadata(path) != null;
122124
}
123125

124-
public void updateContext(MetadataInjector metadataInjector) {
126+
public void updateContext(ActionExecutionMetadata action, MetadataInjector metadataInjector) {
127+
this.action = action;
125128
this.metadataInjector = metadataInjector;
126129
}
127130

@@ -625,7 +628,7 @@ private void downloadFileIfRemote(PathFragment path) throws IOException {
625628
}
626629
getFromFuture(
627630
inputFetcher.prefetchFiles(
628-
ImmutableList.of(input), this::getInputMetadata, Priority.CRITICAL));
631+
action, ImmutableList.of(input), this::getInputMetadata, Priority.CRITICAL));
629632
} catch (InterruptedException e) {
630633
Thread.currentThread().interrupt();
631634
throw new IOException(String.format("Received interrupt while fetching file '%s'", path), e);

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

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
package com.google.devtools.build.lib.remote;
1515

1616
import static com.google.common.base.Preconditions.checkArgument;
17-
import static com.google.devtools.build.lib.remote.common.ProgressStatusListener.NO_ACTION;
1817

1918
import build.bazel.remote.execution.v2.Digest;
2019
import build.bazel.remote.execution.v2.RequestMetadata;
2120
import com.google.common.base.Preconditions;
2221
import com.google.common.util.concurrent.ListenableFuture;
22+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
2323
import com.google.devtools.build.lib.actions.FileArtifactValue;
2424
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
25-
import com.google.devtools.build.lib.events.ExtendedEventHandler.FetchProgress;
2625
import com.google.devtools.build.lib.events.Reporter;
27-
import com.google.devtools.build.lib.exec.SpawnProgressEvent;
28-
import com.google.devtools.build.lib.remote.RemoteCache.DownloadProgressReporter;
2926
import com.google.devtools.build.lib.remote.common.RemoteActionExecutionContext;
3027
import com.google.devtools.build.lib.remote.util.DigestUtil;
3128
import com.google.devtools.build.lib.remote.util.TempPathGenerator;
@@ -79,6 +76,7 @@ protected boolean canDownloadFile(Path path, FileArtifactValue metadata) {
7976

8077
@Override
8178
protected ListenableFuture<Void> doDownloadFile(
79+
ActionExecutionMetadata action,
8280
Reporter reporter,
8381
Path tempPath,
8482
PathFragment execPath,
@@ -87,47 +85,18 @@ protected ListenableFuture<Void> doDownloadFile(
8785
throws IOException {
8886
checkArgument(metadata.isRemote(), "Cannot download file that is not a remote file.");
8987
RequestMetadata requestMetadata =
90-
TracingMetadataUtils.buildMetadata(buildRequestId, commandId, "prefetcher", null);
88+
TracingMetadataUtils.buildMetadata(buildRequestId, commandId, "prefetcher", action);
9189
RemoteActionExecutionContext context = RemoteActionExecutionContext.create(requestMetadata);
9290

9391
Digest digest = DigestUtil.buildDigest(metadata.getDigest(), metadata.getSize());
9492

95-
DownloadProgressReporter downloadProgressReporter;
96-
if (priority == Priority.LOW) {
97-
// Only report download progress for toplevel outputs
98-
downloadProgressReporter =
99-
new DownloadProgressReporter(
100-
/* includeFile= */ false,
101-
progress -> reporter.post(new DownloadProgress(progress)),
102-
execPath.toString(),
103-
metadata.getSize());
104-
} else {
105-
downloadProgressReporter = new DownloadProgressReporter(NO_ACTION, "", 0);
106-
}
107-
108-
return remoteCache.downloadFile(context, tempPath, digest, downloadProgressReporter);
109-
}
110-
111-
public static class DownloadProgress implements FetchProgress {
112-
private final SpawnProgressEvent progress;
113-
114-
public DownloadProgress(SpawnProgressEvent progress) {
115-
this.progress = progress;
116-
}
117-
118-
@Override
119-
public String getResourceIdentifier() {
120-
return progress.progressId();
121-
}
122-
123-
@Override
124-
public String getProgress() {
125-
return progress.progress();
126-
}
127-
128-
@Override
129-
public boolean isFinished() {
130-
return progress.finished();
131-
}
93+
return remoteCache.downloadFile(
94+
context,
95+
tempPath,
96+
digest,
97+
new RemoteCache.DownloadProgressReporter(
98+
progress -> progress.postTo(reporter, action),
99+
execPath.toString(),
100+
digest.getSizeBytes()));
132101
}
133102
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.collect.ImmutableMap;
2222
import com.google.common.eventbus.Subscribe;
2323
import com.google.devtools.build.lib.actions.Action;
24+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
2425
import com.google.devtools.build.lib.actions.ActionInputMap;
2526
import com.google.devtools.build.lib.actions.Artifact;
2627
import com.google.devtools.build.lib.actions.ArtifactPathResolver;
@@ -99,11 +100,12 @@ public FileSystem createActionFileSystem(
99100

100101
@Override
101102
public void updateActionFileSystemContext(
103+
ActionExecutionMetadata action,
102104
FileSystem actionFileSystem,
103105
Environment env,
104106
MetadataInjector injector,
105107
ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> filesets) {
106-
((RemoteActionFileSystem) actionFileSystem).updateContext(injector);
108+
((RemoteActionFileSystem) actionFileSystem).updateContext(action, injector);
107109
}
108110

109111
@Override

‎src/main/java/com/google/devtools/build/lib/skyframe/SkyframeActionExecutor.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,13 @@ FileSystem createActionFileSystem(
382382
}
383383

384384
private void updateActionFileSystemContext(
385+
Action action,
385386
FileSystem actionFileSystem,
386387
Environment env,
387388
MetadataInjector metadataInjector,
388389
ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> filesets) {
389-
outputService.updateActionFileSystemContext(actionFileSystem, env, metadataInjector, filesets);
390+
outputService.updateActionFileSystemContext(
391+
action, actionFileSystem, env, metadataInjector, filesets);
390392
}
391393

392394
void executionOver() {
@@ -489,7 +491,8 @@ ActionExecutionValue executeAction(
489491
boolean hasDiscoveredInputs)
490492
throws ActionExecutionException, InterruptedException {
491493
if (actionFileSystem != null) {
492-
updateActionFileSystemContext(actionFileSystem, env, metadataHandler, expandedFilesets);
494+
updateActionFileSystemContext(
495+
action, actionFileSystem, env, metadataHandler, expandedFilesets);
493496
}
494497

495498
ActionExecutionContext actionExecutionContext =
@@ -834,6 +837,7 @@ NestedSet<Artifact> discoverInputs(
834837
threadStateReceiverFactory.apply(actionLookupData));
835838
if (actionFileSystem != null) {
836839
updateActionFileSystemContext(
840+
action,
837841
actionFileSystem,
838842
env,
839843
THROWING_METADATA_INJECTOR_FOR_ACTIONFS,

‎src/main/java/com/google/devtools/build/lib/vfs/OutputService.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.ImmutableList;
1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.devtools.build.lib.actions.Action;
21+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
2122
import com.google.devtools.build.lib.actions.ActionInputMap;
2223
import com.google.devtools.build.lib.actions.Artifact;
2324
import com.google.devtools.build.lib.actions.ArtifactPathResolver;
@@ -196,6 +197,7 @@ default FileSystem createActionFileSystem(
196197
* @param filesets The Fileset symlinks known for this action.
197198
*/
198199
default void updateActionFileSystemContext(
200+
ActionExecutionMetadata action,
199201
FileSystem actionFileSystem,
200202
Environment env,
201203
MetadataInjector injector,

0 commit comments

Comments
 (0)