Skip to content

Commit ea4ad30

Browse files
coeuvreiancha1992
andauthored
Add ActionExecutionMetadata as a parameter to ActionInputPrefetcher#prefetchFiles. (#18656)
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 Co-authored-by: Ian (Hee) Cha <[email protected]>
1 parent 9310618 commit ea4ad30

13 files changed

+169
-73
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public interface ActionInputPrefetcher {
2424
new ActionInputPrefetcher() {
2525
@Override
2626
public ListenableFuture<Void> prefetchFiles(
27-
Iterable<? extends ActionInput> inputs, MetadataProvider metadataProvider) {
27+
ActionExecutionMetadata action,
28+
Iterable<? extends ActionInput> inputs,
29+
MetadataProvider metadataProvider) {
2830
// Do nothing.
2931
return immediateVoidFuture();
3032
}
@@ -43,7 +45,9 @@ public boolean supportsPartialTreeArtifactInputs() {
4345
* @return future success if prefetch is finished or {@link IOException}.
4446
*/
4547
ListenableFuture<Void> prefetchFiles(
46-
Iterable<? extends ActionInput> inputs, MetadataProvider metadataProvider);
48+
ActionExecutionMetadata action,
49+
Iterable<? extends ActionInput> inputs,
50+
MetadataProvider metadataProvider);
4751

4852
/**
4953
* Whether the prefetcher is able to fetch individual files in a tree artifact without fetching

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
@@ -246,6 +246,7 @@ public ListenableFuture<Void> prefetchInputs()
246246
return actionExecutionContext
247247
.getActionInputPrefetcher()
248248
.prefetchFiles(
249+
spawn.getResourceOwner(),
249250
getInputMapping(PathFragment.EMPTY_FRAGMENT, /* willAccessRepeatedly= */ true)
250251
.values(),
251252
getMetadataProvider());

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.common.util.concurrent.FutureCallback;
3434
import com.google.common.util.concurrent.ListenableFuture;
3535
import com.google.devtools.build.lib.actions.Action;
36+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
3637
import com.google.devtools.build.lib.actions.ActionInput;
3738
import com.google.devtools.build.lib.actions.ActionInputPrefetcher;
3839
import com.google.devtools.build.lib.actions.Artifact;
@@ -292,6 +293,7 @@ private boolean shouldDownloadFile(Path path, FileArtifactValue metadata) {
292293
* @param tempPath the temporary path which the input should be written to.
293294
*/
294295
protected abstract ListenableFuture<Void> doDownloadFile(
296+
ActionExecutionMetadata action,
295297
Reporter reporter,
296298
Path tempPath,
297299
PathFragment execPath,
@@ -317,11 +319,13 @@ protected Completable onErrorResumeNext(Throwable error) {
317319
*/
318320
@Override
319321
public ListenableFuture<Void> prefetchFiles(
322+
ActionExecutionMetadata action,
320323
Iterable<? extends ActionInput> inputs, MetadataProvider metadataProvider) {
321-
return prefetchFiles(inputs, metadataProvider, Priority.MEDIUM);
324+
return prefetchFiles(action, inputs, metadataProvider, Priority.MEDIUM);
322325
}
323326

324327
protected ListenableFuture<Void> prefetchFiles(
328+
ActionExecutionMetadata action,
325329
Iterable<? extends ActionInput> inputs,
326330
MetadataProvider metadataProvider,
327331
Priority priority) {
@@ -346,7 +350,7 @@ protected ListenableFuture<Void> prefetchFiles(
346350
Flowable<TransferResult> transfers =
347351
Flowable.fromIterable(files)
348352
.flatMapSingle(
349-
input -> toTransferResult(prefetchFile(dirCtx, metadataProvider, input, priority)));
353+
input -> toTransferResult(prefetchFile(action, dirCtx, metadataProvider, input, priority)));
350354

351355
Completable prefetch =
352356
Completable.using(
@@ -357,6 +361,7 @@ protected ListenableFuture<Void> prefetchFiles(
357361
}
358362

359363
private Completable prefetchFile(
364+
ActionExecutionMetadata action,
360365
DirectoryContext dirCtx,
361366
MetadataProvider metadataProvider,
362367
ActionInput input,
@@ -386,6 +391,7 @@ private Completable prefetchFile(
386391

387392
Completable result =
388393
downloadFileNoCheckRx(
394+
action,
389395
dirCtx,
390396
execRoot.getRelative(execPath),
391397
treeRootExecPath != null ? execRoot.getRelative(treeRootExecPath) : null,
@@ -461,6 +467,7 @@ private Symlink maybeGetSymlink(
461467
* download finished.
462468
*/
463469
private Completable downloadFileRx(
470+
ActionExecutionMetadata action,
464471
DirectoryContext dirCtx,
465472
Path path,
466473
@Nullable Path treeRoot,
@@ -470,10 +477,11 @@ private Completable downloadFileRx(
470477
if (!canDownloadFile(path, metadata)) {
471478
return Completable.complete();
472479
}
473-
return downloadFileNoCheckRx(dirCtx, path, treeRoot, actionInput, metadata, priority);
480+
return downloadFileNoCheckRx(action, dirCtx, path, treeRoot, actionInput, metadata, priority);
474481
}
475482

476483
private Completable downloadFileNoCheckRx(
484+
ActionExecutionMetadata action,
477485
DirectoryContext dirCtx,
478486
Path path,
479487
@Nullable Path treeRoot,
@@ -498,6 +506,7 @@ private Completable downloadFileNoCheckRx(
498506
toCompletable(
499507
() ->
500508
doDownloadFile(
509+
action,
501510
reporter,
502511
tempPath,
503512
finalPath.relativeTo(execRoot),
@@ -542,12 +551,15 @@ private Completable downloadFileNoCheckRx(
542551
* <p>The file will be written into a temporary file and moved to the final destination after the
543552
* download finished.
544553
*/
545-
public void downloadFile(Path path, @Nullable ActionInput actionInput, FileArtifactValue metadata)
554+
public void downloadFile(
555+
ActionExecutionMetadata action,
556+
Path path, @Nullable ActionInput actionInput, FileArtifactValue metadata)
546557
throws IOException, InterruptedException {
547-
getFromFuture(downloadFileAsync(path.asFragment(), actionInput, metadata, Priority.CRITICAL));
558+
getFromFuture(downloadFileAsync(action, path.asFragment(), actionInput, metadata, Priority.CRITICAL));
548559
}
549560

550561
protected ListenableFuture<Void> downloadFileAsync(
562+
ActionExecutionMetadata action,
551563
PathFragment path,
552564
@Nullable ActionInput actionInput,
553565
FileArtifactValue metadata,
@@ -557,6 +569,7 @@ protected ListenableFuture<Void> downloadFileAsync(
557569
DirectoryContext::new,
558570
dirCtx ->
559571
downloadFileRx(
572+
action,
560573
dirCtx,
561574
execRoot.getFileSystem().getPath(path),
562575
/* treeRoot= */ null,
@@ -695,7 +708,7 @@ public void finalizeAction(Action action, MetadataHandler metadataHandler) {
695708
}
696709

697710
if (!inputsToDownload.isEmpty()) {
698-
var future = prefetchFiles(inputsToDownload, metadataHandler, Priority.HIGH);
711+
var future = prefetchFiles(action, inputsToDownload, metadataHandler, Priority.HIGH);
699712
addCallback(
700713
future,
701714
new FutureCallback<Void>() {
@@ -716,7 +729,7 @@ public void onFailure(Throwable throwable) {
716729
}
717730

718731
if (!outputsToDownload.isEmpty()) {
719-
var future = prefetchFiles(outputsToDownload, metadataHandler, Priority.LOW);
732+
var future = prefetchFiles(action, outputsToDownload, metadataHandler, Priority.LOW);
720733
addCallback(
721734
future,
722735
new FutureCallback<Void>() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ filegroup(
99
srcs = glob(["*"]) + [
1010
"//src/main/java/com/google/devtools/build/lib/remote/circuitbreaker:srcs",
1111
"//src/main/java/com/google/devtools/build/lib/remote/common:srcs",
12-
"//src/main/java/com/google/devtools/build/lib/remote/downloader:srcs",
1312
"//src/main/java/com/google/devtools/build/lib/remote/disk:srcs",
13+
"//src/main/java/com/google/devtools/build/lib/remote/downloader:srcs",
1414
"//src/main/java/com/google/devtools/build/lib/remote/grpc:srcs",
1515
"//src/main/java/com/google/devtools/build/lib/remote/http:srcs",
1616
"//src/main/java/com/google/devtools/build/lib/remote/logging:srcs",
@@ -206,6 +206,7 @@ java_library(
206206
srcs = ["ToplevelArtifactsDownloader.java"],
207207
deps = [
208208
":abstract_action_input_prefetcher",
209+
"//src/main/java/com/google/devtools/build/lib/actions",
209210
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",
210211
"//src/main/java/com/google/devtools/build/lib/actions:file_metadata",
211212
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",

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
@@ -24,6 +24,7 @@
2424
import com.google.common.annotations.VisibleForTesting;
2525
import com.google.common.collect.ImmutableList;
2626
import com.google.common.collect.ImmutableMap;
27+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
2728
import com.google.devtools.build.lib.actions.ActionInput;
2829
import com.google.devtools.build.lib.actions.ActionInputMap;
2930
import com.google.devtools.build.lib.actions.Artifact;
@@ -77,6 +78,7 @@ public class RemoteActionFileSystem extends DelegateFileSystem {
7778
private final RemoteActionInputFetcher inputFetcher;
7879
private final RemoteInMemoryFileSystem remoteOutputTree;
7980

81+
@Nullable private ActionExecutionMetadata action = null;
8082
@Nullable private MetadataInjector metadataInjector = null;
8183

8284
RemoteActionFileSystem(
@@ -111,7 +113,8 @@ boolean isRemote(Path path) {
111113
return getRemoteMetadata(path.asFragment()) != null;
112114
}
113115

114-
public void updateContext(MetadataInjector metadataInjector) {
116+
public void updateContext(ActionExecutionMetadata action, MetadataInjector metadataInjector) {
117+
this.action = action;
115118
this.metadataInjector = metadataInjector;
116119
}
117120

@@ -579,7 +582,7 @@ private void downloadFileIfRemote(PathFragment path) throws IOException {
579582
FileArtifactValue m = getRemoteMetadata(path);
580583
if (m != null) {
581584
try {
582-
inputFetcher.downloadFile(delegateFs.getPath(path), getActionInput(path), m);
585+
inputFetcher.downloadFile(action, delegateFs.getPath(path), getActionInput(path), m);
583586
} catch (InterruptedException e) {
584587
Thread.currentThread().interrupt();
585588
throw new IOException(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.base.Preconditions;
2222
import com.google.common.collect.ImmutableList;
2323
import com.google.common.util.concurrent.ListenableFuture;
24+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
2425
import com.google.devtools.build.lib.actions.EnvironmentalExecException;
2526
import com.google.devtools.build.lib.actions.FileArtifactValue;
2627
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
@@ -96,6 +97,7 @@ protected boolean canDownloadFile(Path path, FileArtifactValue metadata) {
9697

9798
@Override
9899
protected ListenableFuture<Void> doDownloadFile(
100+
ActionExecutionMetadata action,
99101
Reporter reporter,
100102
Path tempPath,
101103
PathFragment execPath,
@@ -104,7 +106,7 @@ protected ListenableFuture<Void> doDownloadFile(
104106
throws IOException {
105107
checkArgument(metadata.isRemote(), "Cannot download file that is not a remote file.");
106108
RequestMetadata requestMetadata =
107-
TracingMetadataUtils.buildMetadata(buildRequestId, commandId, "prefetcher", null);
109+
TracingMetadataUtils.buildMetadata(buildRequestId, commandId, "prefetcher", action);
108110
RemoteActionExecutionContext context = RemoteActionExecutionContext.create(requestMetadata);
109111

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

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
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableMap;
2121
import com.google.common.eventbus.Subscribe;
2222
import com.google.devtools.build.lib.actions.Action;
23+
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
2324
import com.google.devtools.build.lib.actions.ActionInputMap;
2425
import com.google.devtools.build.lib.actions.Artifact;
2526
import com.google.devtools.build.lib.actions.ArtifactPathResolver;
@@ -84,11 +85,12 @@ public FileSystem createActionFileSystem(
8485

8586
@Override
8687
public void updateActionFileSystemContext(
88+
ActionExecutionMetadata action,
8789
FileSystem actionFileSystem,
8890
Environment env,
8991
MetadataInjector injector,
9092
ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> filesets) {
91-
((RemoteActionFileSystem) actionFileSystem).updateContext(injector);
93+
((RemoteActionFileSystem) actionFileSystem).updateContext(action, injector);
9294
}
9395

9496
@Override

0 commit comments

Comments
 (0)