Skip to content

Commit 58ea4dd

Browse files
coeuvrecopybara-github
authored andcommitted
Remote: Don't upload BES referenced blobs to disk cache.
Fixes #14435. Closes #14451. PiperOrigin-RevId: 417629899
1 parent 10a7a6e commit 58ea4dd

File tree

6 files changed

+84
-15
lines changed

6 files changed

+84
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private Single<PathConverter> upload(Set<Path> files) {
252252

253253
RequestMetadata metadata =
254254
TracingMetadataUtils.buildMetadata(buildRequestId, commandId, "bes-upload", null);
255-
RemoteActionExecutionContext context = RemoteActionExecutionContext.create(metadata);
255+
RemoteActionExecutionContext context = RemoteActionExecutionContext.createForBES(metadata);
256256

257257
return Single.using(
258258
remoteCache::retain,

src/main/java/com/google/devtools/build/lib/remote/common/RemoteActionExecutionContext.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020

2121
/** A context that provide remote execution related information for executing an action remotely. */
2222
public interface RemoteActionExecutionContext {
23+
/** The type of the context. */
24+
enum Type {
25+
REMOTE_EXECUTION,
26+
BUILD_EVENT_SERVICE,
27+
}
28+
29+
/** Returns the {@link Type} of the context. */
30+
Type getType();
2331

2432
/** Returns the {@link Spawn} of the action being executed or {@code null}. */
2533
@Nullable
@@ -46,14 +54,21 @@ default ActionExecutionMetadata getSpawnOwner() {
4654

4755
/** Creates a {@link SimpleRemoteActionExecutionContext} with given {@link RequestMetadata}. */
4856
static RemoteActionExecutionContext create(RequestMetadata metadata) {
49-
return new SimpleRemoteActionExecutionContext(/*spawn=*/ null, metadata, new NetworkTime());
57+
return new SimpleRemoteActionExecutionContext(
58+
/*type=*/ Type.REMOTE_EXECUTION, /*spawn=*/ null, metadata, new NetworkTime());
5059
}
5160

5261
/**
5362
* Creates a {@link SimpleRemoteActionExecutionContext} with given {@link Spawn} and {@link
5463
* RequestMetadata}.
5564
*/
5665
static RemoteActionExecutionContext create(@Nullable Spawn spawn, RequestMetadata metadata) {
57-
return new SimpleRemoteActionExecutionContext(spawn, metadata, new NetworkTime());
66+
return new SimpleRemoteActionExecutionContext(
67+
/*type=*/ Type.REMOTE_EXECUTION, spawn, metadata, new NetworkTime());
68+
}
69+
70+
static RemoteActionExecutionContext createForBES(RequestMetadata metadata) {
71+
return new SimpleRemoteActionExecutionContext(
72+
/*type=*/ Type.BUILD_EVENT_SERVICE, /*spawn=*/ null, metadata, new NetworkTime());
5873
}
5974
}

src/main/java/com/google/devtools/build/lib/remote/common/SimpleRemoteActionExecutionContext.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,24 @@
2020
/** A {@link RemoteActionExecutionContext} implementation */
2121
public class SimpleRemoteActionExecutionContext implements RemoteActionExecutionContext {
2222

23+
private final Type type;
2324
private final Spawn spawn;
2425
private final RequestMetadata requestMetadata;
2526
private final NetworkTime networkTime;
2627

2728
public SimpleRemoteActionExecutionContext(
28-
Spawn spawn, RequestMetadata requestMetadata, NetworkTime networkTime) {
29+
Type type, Spawn spawn, RequestMetadata requestMetadata, NetworkTime networkTime) {
30+
this.type = type;
2931
this.spawn = spawn;
3032
this.requestMetadata = requestMetadata;
3133
this.networkTime = networkTime;
3234
}
3335

36+
@Override
37+
public Type getType() {
38+
return type;
39+
}
40+
3441
@Nullable
3542
@Override
3643
public Spawn getSpawn() {

src/main/java/com/google/devtools/build/lib/remote/disk/DiskAndRemoteCacheClient.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@ public void close() {
7474
@Override
7575
public ListenableFuture<Void> uploadFile(
7676
RemoteActionExecutionContext context, Digest digest, Path file) {
77+
// For BES upload, we only upload to the remote cache.
78+
if (context.getType() == RemoteActionExecutionContext.Type.BUILD_EVENT_SERVICE) {
79+
return remoteCache.uploadFile(context, digest, file);
80+
}
81+
7782
ListenableFuture<Void> future = diskCache.uploadFile(context, digest, file);
7883

79-
boolean uploadForSpawn = context.getSpawn() != null;
80-
// If not upload for spawn e.g. for build event artifacts, we always upload files to remote
81-
// cache.
82-
if (!uploadForSpawn
83-
|| options.isRemoteExecutionEnabled()
84+
if (options.isRemoteExecutionEnabled()
8485
|| shouldUploadLocalResultsToRemoteCache(options, context.getSpawn())) {
8586
future =
8687
Futures.transformAsync(
@@ -113,6 +114,12 @@ public ListenableFuture<ImmutableSet<Digest>> findMissingDigests(
113114
if (options.isRemoteExecutionEnabled()) {
114115
return remoteCache.findMissingDigests(context, digests);
115116
}
117+
118+
// For BES upload, we only check the remote cache.
119+
if (context.getType() == RemoteActionExecutionContext.Type.BUILD_EVENT_SERVICE) {
120+
return remoteCache.findMissingDigests(context, digests);
121+
}
122+
116123
ListenableFuture<ImmutableSet<Digest>> diskQuery =
117124
diskCache.findMissingDigests(context, digests);
118125
if (shouldUploadLocalResultsToRemoteCache(options, context.getSpawn())) {

src/test/shell/bazel/remote/remote_execution_test.sh

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3332,7 +3332,7 @@ EOF
33323332
//a:consumer >& $TEST_log || fail "Failed to build without remote cache"
33333333
}
33343334

3335-
function test_uploader_respsect_no_cache() {
3335+
function test_uploader_respect_no_cache() {
33363336
mkdir -p a
33373337
cat > a/BUILD <<EOF
33383338
genrule(
@@ -3354,7 +3354,7 @@ EOF
33543354
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
33553355
}
33563356

3357-
function test_uploader_respsect_no_cache_trees() {
3357+
function test_uploader_respect_no_cache_trees() {
33583358
mkdir -p a
33593359
cat > a/output_dir.bzl <<'EOF'
33603360
def _gen_output_dir_impl(ctx):
@@ -3401,7 +3401,7 @@ EOF
34013401
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
34023402
}
34033403

3404-
function test_uploader_respsect_no_upload_results() {
3404+
function test_uploader_respect_no_upload_results() {
34053405
mkdir -p a
34063406
cat > a/BUILD <<EOF
34073407
genrule(
@@ -3423,7 +3423,7 @@ EOF
34233423
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
34243424
}
34253425

3426-
function test_uploader_respsect_no_upload_results_combined_cache() {
3426+
function test_uploader_respect_no_upload_results_combined_cache() {
34273427
mkdir -p a
34283428
cat > a/BUILD <<EOF
34293429
genrule(
@@ -3433,9 +3433,11 @@ genrule(
34333433
)
34343434
EOF
34353435

3436+
cache_dir=$(mktemp -d)
3437+
34363438
bazel build \
34373439
--remote_cache=grpc://localhost:${worker_port} \
3438-
--disk_cache="${TEST_TMPDIR}/disk_cache" \
3440+
--disk_cache=$cache_dir \
34393441
--remote_upload_local_results=false \
34403442
--incompatible_remote_build_event_upload_respect_no_cache \
34413443
--build_event_json_file=bep.json \
@@ -3445,7 +3447,35 @@ EOF
34453447
expect_not_log "a:foo.*bytestream://" || fail "local files are converted"
34463448
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
34473449
remote_cas_files="$(count_remote_cas_files)"
3448-
[[ "$remote_cas_files" == 1 ]] || fail "Expected 1 remote action cache entries, not $remote_cas_files"
3450+
[[ "$remote_cas_files" == 1 ]] || fail "Expected 1 remote cas entries, not $remote_cas_files"
3451+
}
3452+
3453+
function test_uploader_ignore_disk_cache_of_combined_cache() {
3454+
mkdir -p a
3455+
cat > a/BUILD <<EOF
3456+
genrule(
3457+
name = 'foo',
3458+
outs = ["foo.txt"],
3459+
cmd = "echo \"foo bar\" > \$@",
3460+
tags = ["no-cache"],
3461+
)
3462+
EOF
3463+
3464+
cache_dir=$(mktemp -d)
3465+
3466+
bazel build \
3467+
--remote_cache=grpc://localhost:${worker_port} \
3468+
--disk_cache=$cache_dir \
3469+
--incompatible_remote_build_event_upload_respect_no_cache \
3470+
--build_event_json_file=bep.json \
3471+
//a:foo >& $TEST_log || fail "Failed to build"
3472+
3473+
cat bep.json > $TEST_log
3474+
expect_not_log "a:foo.*bytestream://" || fail "local files are converted"
3475+
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
3476+
3477+
disk_cas_files="$(count_disk_cas_files $cache_dir)"
3478+
[[ "$disk_cas_files" == 0 ]] || fail "Expected 0 disk cas entries, not $disk_cas_files"
34493479
}
34503480

34513481
run_suite "Remote execution and remote cache tests"

src/test/shell/bazel/remote/remote_utils.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ function count_disk_ac_files() {
7171
fi
7272
}
7373

74+
# Pass in the root of the disk cache and count number of files under /cas directory
75+
# output int to stdout
76+
function count_disk_cas_files() {
77+
if [ -d "$1/cas" ]; then
78+
expr $(find "$1/cas" -type f | wc -l)
79+
else
80+
echo 0
81+
fi
82+
}
83+
7484
function count_remote_ac_files() {
7585
if [ -d "$cas_path/ac" ]; then
7686
expr $(find "$cas_path/ac" -type f | wc -l)

0 commit comments

Comments
 (0)