28
28
import build .bazel .remote .execution .v2 .Tree ;
29
29
import com .google .common .annotations .VisibleForTesting ;
30
30
import com .google .common .base .Preconditions ;
31
+ import com .google .common .collect .ImmutableList ;
32
+ import com .google .devtools .build .lib .actions .ActionExecutionMetadata ;
33
+ import com .google .devtools .build .lib .actions .ActionUploadFinishedEvent ;
34
+ import com .google .devtools .build .lib .actions .ActionUploadStartedEvent ;
31
35
import com .google .devtools .build .lib .actions .ExecException ;
32
36
import com .google .devtools .build .lib .actions .UserExecException ;
37
+ import com .google .devtools .build .lib .events .ExtendedEventHandler ;
33
38
import com .google .devtools .build .lib .remote .common .RemoteActionExecutionContext ;
34
39
import com .google .devtools .build .lib .remote .common .RemoteCacheClient ;
35
40
import com .google .devtools .build .lib .remote .common .RemoteCacheClient .ActionKey ;
36
41
import com .google .devtools .build .lib .remote .common .RemotePathResolver ;
37
42
import com .google .devtools .build .lib .remote .options .RemoteOptions ;
38
43
import com .google .devtools .build .lib .remote .util .DigestUtil ;
44
+ import com .google .devtools .build .lib .remote .util .RxUtils ;
39
45
import com .google .devtools .build .lib .server .FailureDetails .FailureDetail ;
40
46
import com .google .devtools .build .lib .server .FailureDetails .RemoteExecution ;
41
47
import com .google .devtools .build .lib .server .FailureDetails .RemoteExecution .Code ;
56
62
import java .util .HashMap ;
57
63
import java .util .List ;
58
64
import java .util .Map ;
65
+ import java .util .stream .Collectors ;
59
66
import javax .annotation .Nullable ;
60
67
61
68
/** UploadManifest adds output metadata to a {@link ActionResult}. */
@@ -341,10 +348,11 @@ ActionResult getActionResult() {
341
348
}
342
349
343
350
/** Uploads outputs and action result (if exit code is 0) to remote cache. */
344
- public ActionResult upload (RemoteActionExecutionContext context , RemoteCache remoteCache )
351
+ public ActionResult upload (
352
+ RemoteActionExecutionContext context , RemoteCache remoteCache , ExtendedEventHandler reporter )
345
353
throws IOException , InterruptedException {
346
354
try {
347
- return uploadAsync (context , remoteCache ).blockingGet ();
355
+ return uploadAsync (context , remoteCache , reporter ).blockingGet ();
348
356
} catch (RuntimeException e ) {
349
357
throwIfInstanceOf (e .getCause (), InterruptedException .class );
350
358
throwIfInstanceOf (e .getCause (), IOException .class );
@@ -368,29 +376,91 @@ private Completable upload(
368
376
return toCompletable (() -> remoteCache .uploadBlob (context , digest , blob ), directExecutor ());
369
377
}
370
378
379
+ private static void reportUploadStarted (
380
+ ExtendedEventHandler reporter ,
381
+ @ Nullable ActionExecutionMetadata action ,
382
+ String prefix ,
383
+ Iterable <Digest > digests ) {
384
+ if (action != null ) {
385
+ for (Digest digest : digests ) {
386
+ reporter .post (ActionUploadStartedEvent .create (action , prefix + digest .getHash ()));
387
+ }
388
+ }
389
+ }
390
+
391
+ private static void reportUploadFinished (
392
+ ExtendedEventHandler reporter ,
393
+ @ Nullable ActionExecutionMetadata action ,
394
+ String resourceIdPrefix ,
395
+ Iterable <Digest > digests ) {
396
+ if (action != null ) {
397
+ for (Digest digest : digests ) {
398
+ reporter .post (
399
+ ActionUploadFinishedEvent .create (action , resourceIdPrefix + digest .getHash ()));
400
+ }
401
+ }
402
+ }
403
+
371
404
/**
372
405
* Returns a {@link Single} which upon subscription will upload outputs and action result (if exit
373
406
* code is 0) to remote cache.
374
407
*/
375
408
public Single <ActionResult > uploadAsync (
376
- RemoteActionExecutionContext context , RemoteCache remoteCache ) {
409
+ RemoteActionExecutionContext context ,
410
+ RemoteCache remoteCache ,
411
+ ExtendedEventHandler reporter ) {
377
412
Collection <Digest > digests = new ArrayList <>();
378
413
digests .addAll (digestToFile .keySet ());
379
414
digests .addAll (digestToBlobs .keySet ());
380
415
381
- Completable uploadOutputs =
382
- mergeBulkTransfer (
383
- toSingle (() -> remoteCache .findMissingDigests (context , digests ), directExecutor ())
384
- .flatMapPublisher (Flowable ::fromIterable )
385
- .flatMapSingle (digest -> toTransferResult (upload (context , remoteCache , digest ))));
416
+ ActionExecutionMetadata action = context .getSpawnOwner ();
417
+
418
+ String outputPrefix = "cas/" ;
419
+ Flowable <RxUtils .TransferResult > bulkTransfers =
420
+ toSingle (() -> remoteCache .findMissingDigests (context , digests ), directExecutor ())
421
+ .doOnSubscribe (d -> reportUploadStarted (reporter , action , outputPrefix , digests ))
422
+ .doOnError (error -> reportUploadFinished (reporter , action , outputPrefix , digests ))
423
+ .doOnDispose (() -> reportUploadFinished (reporter , action , outputPrefix , digests ))
424
+ .doOnSuccess (
425
+ missingDigests -> {
426
+ List <Digest > existedDigests =
427
+ digests .stream ()
428
+ .filter (digest -> !missingDigests .contains (digest ))
429
+ .collect (Collectors .toList ());
430
+ reportUploadFinished (reporter , action , outputPrefix , existedDigests );
431
+ })
432
+ .flatMapPublisher (Flowable ::fromIterable )
433
+ .flatMapSingle (
434
+ digest ->
435
+ toTransferResult (upload (context , remoteCache , digest ))
436
+ .doFinally (
437
+ () ->
438
+ reportUploadFinished (
439
+ reporter , action , outputPrefix , ImmutableList .of (digest ))));
440
+ Completable uploadOutputs = mergeBulkTransfer (bulkTransfers );
386
441
387
442
ActionResult actionResult = result .build ();
388
443
Completable uploadActionResult = Completable .complete ();
389
444
if (actionResult .getExitCode () == 0 && actionKey != null ) {
445
+ String actionResultPrefix = "ac/" ;
390
446
uploadActionResult =
391
447
toCompletable (
392
- () -> remoteCache .uploadActionResult (context , actionKey , actionResult ),
393
- directExecutor ());
448
+ () -> remoteCache .uploadActionResult (context , actionKey , actionResult ),
449
+ directExecutor ())
450
+ .doOnSubscribe (
451
+ d ->
452
+ reportUploadStarted (
453
+ reporter ,
454
+ action ,
455
+ actionResultPrefix ,
456
+ ImmutableList .of (actionKey .getDigest ())))
457
+ .doFinally (
458
+ () ->
459
+ reportUploadFinished (
460
+ reporter ,
461
+ action ,
462
+ actionResultPrefix ,
463
+ ImmutableList .of (actionKey .getDigest ())));
394
464
}
395
465
396
466
return Completable .concatArray (uploadOutputs , uploadActionResult ).toSingleDefault (actionResult );
0 commit comments