Skip to content

Commit b0c5eb3

Browse files
coeuvrecopybara-github
authored andcommitted
Dont query remote cache but always use bytestream protocol
When using `--experimental_remote_build_event_upload=minimal`, instead of querying remote cache to decide whether convert local path of a file to `bytestream://`, it now always use `bytestream://` for BEP referenced files. Closes bazelbuild#16999. Closes bazelbuild#17025. PiperOrigin-RevId: 502351401 Change-Id: Ic858367ffaf3c2a2c20db88ada85fbbe1d93aae8
1 parent 2b22bf0 commit b0c5eb3

File tree

4 files changed

+97
-58
lines changed

4 files changed

+97
-58
lines changed

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

+26-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import build.bazel.remote.execution.v2.RequestMetadata;
2323
import com.google.common.base.Preconditions;
2424
import com.google.common.collect.ImmutableSet;
25+
import com.google.common.collect.Maps;
2526
import com.google.common.collect.Sets;
2627
import com.google.common.eventbus.Subscribe;
2728
import com.google.common.util.concurrent.ListenableFuture;
@@ -47,7 +48,6 @@
4748
import io.reactivex.rxjava3.schedulers.Schedulers;
4849
import java.io.IOException;
4950
import java.util.ArrayList;
50-
import java.util.HashMap;
5151
import java.util.HashSet;
5252
import java.util.List;
5353
import java.util.Map;
@@ -107,10 +107,16 @@ class ByteStreamBuildEventArtifactUploader extends AbstractReferenceCounted
107107
}
108108

109109
public void omitFile(Path file) {
110+
Preconditions.checkState(
111+
remoteBuildEventUploadMode != RemoteBuildEventUploadMode.MINIMAL,
112+
"Cannot omit file in MINIMAL mode");
110113
omittedFiles.add(file.asFragment());
111114
}
112115

113116
public void omitTree(Path treeRoot) {
117+
Preconditions.checkState(
118+
remoteBuildEventUploadMode != RemoteBuildEventUploadMode.MINIMAL,
119+
"Cannot omit tree in MINIMAL mode");
114120
omittedTreeRoots.add(treeRoot.asFragment());
115121
}
116122

@@ -209,10 +215,6 @@ private static void processQueryResult(
209215
}
210216
}
211217

212-
private boolean shouldQuery(PathMetadata path) {
213-
return path.getDigest() != null && !path.isRemote() && !path.isDirectory();
214-
}
215-
216218
private boolean shouldUpload(PathMetadata path) {
217219
boolean result =
218220
path.getDigest() != null && !path.isRemote() && !path.isDirectory() && !path.isOmitted();
@@ -239,7 +241,8 @@ private Single<List<PathMetadata>> queryRemoteCache(
239241
List<PathMetadata> filesToQuery = new ArrayList<>();
240242
Set<Digest> digestsToQuery = new HashSet<>();
241243
for (PathMetadata path : paths) {
242-
if (shouldQuery(path)) {
244+
// Query remote cache for files even if omitted from uploading
245+
if (shouldUpload(path) || path.isOmitted()) {
243246
filesToQuery.add(path);
244247
digestsToQuery.add(path.getDigest());
245248
} else {
@@ -329,16 +332,19 @@ private Single<PathConverter> upload(Set<Path> files) {
329332
reporterUploadError(e);
330333
return new PathMetadata(
331334
file,
332-
/*digest=*/ null,
333-
/*directory=*/ false,
334-
/*remote=*/ false,
335-
/*omitted=*/ false);
335+
/* digest= */ null,
336+
/* directory= */ false,
337+
/* remote= */ false,
338+
/* omitted= */ false);
336339
}
337340
})
338341
.collect(Collectors.toList())
339342
.flatMap(paths -> queryRemoteCache(remoteCache, context, paths))
340343
.flatMap(paths -> uploadLocalFiles(remoteCache, context, paths))
341-
.map(paths -> new PathConverterImpl(remoteServerInstanceName, paths)),
344+
.map(
345+
paths ->
346+
new PathConverterImpl(
347+
remoteServerInstanceName, paths, remoteBuildEventUploadMode)),
342348
RemoteCache::release);
343349
}
344350

@@ -377,17 +383,23 @@ private static class PathConverterImpl implements PathConverter {
377383
private final Set<Path> skippedPaths;
378384
private final Set<Path> localPaths;
379385

380-
PathConverterImpl(String remoteServerInstanceName, List<PathMetadata> uploads) {
386+
PathConverterImpl(
387+
String remoteServerInstanceName,
388+
List<PathMetadata> uploads,
389+
RemoteBuildEventUploadMode remoteBuildEventUploadMode) {
381390
Preconditions.checkNotNull(uploads);
382391
this.remoteServerInstanceName = remoteServerInstanceName;
383-
pathToDigest = new HashMap<>(uploads.size());
392+
pathToDigest = Maps.newHashMapWithExpectedSize(uploads.size());
384393
ImmutableSet.Builder<Path> skippedPaths = ImmutableSet.builder();
385394
ImmutableSet.Builder<Path> localPaths = ImmutableSet.builder();
386395
for (PathMetadata pair : uploads) {
387396
Path path = pair.getPath();
388397
Digest digest = pair.getDigest();
389398
if (digest != null) {
390-
if (pair.isRemote()) {
399+
// Always use bytestream:// in MINIMAL mode
400+
if (remoteBuildEventUploadMode == RemoteBuildEventUploadMode.MINIMAL) {
401+
pathToDigest.put(path, digest);
402+
} else if (pair.isRemote()) {
391403
pathToDigest.put(path, digest);
392404
} else {
393405
localPaths.add(path);

src/main/java/com/google/devtools/build/lib/remote/options/RemoteOptions.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,8 @@ public String getTypeDescription() {
302302
"If set to 'all', all local outputs referenced by BEP are uploaded to remote cache.\n"
303303
+ "If set to 'minimal', local outputs referenced by BEP are not uploaded to the"
304304
+ " remote cache, except for files that are important to the consumers of BEP (e.g."
305-
+ " test logs and timing profile).\n"
306-
+ "file:// scheme is used for the paths of local files and bytestream:// scheme is"
307-
+ " used for the paths of (already) uploaded files.\n"
305+
+ " test logs and timing profile). bytestream:// scheme is always used for the uri of"
306+
+ " files even if they are missing from remote cache.\n"
308307
+ "Default to 'all'.")
309308
public RemoteBuildEventUploadMode remoteBuildEventUploadMode;
310309

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

+60-41
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ else
7171
declare -r EXE_EXT=""
7272
fi
7373

74+
BEP_JSON=bep.json
75+
76+
function expect_bes_file_uploaded() {
77+
local file=$1
78+
if [[ $(cat $BEP_JSON) =~ ${file}\",\"uri\":\"bytestream://localhost:${worker_port}/blobs/([^/]*) ]]; then
79+
if ! remote_cas_file_exist ${BASH_REMATCH[1]}; then
80+
cat $BEP_JSON >> $TEST_log && append_remote_cas_files $TEST_log && fail "$file is not uploaded"
81+
fi
82+
else
83+
cat $BEP_JSON > $TEST_log
84+
fail "$file is not converted to bytestream://"
85+
fi
86+
}
87+
88+
function expect_bes_file_not_uploaded() {
89+
local file=$1
90+
if [[ $(cat $BEP_JSON) =~ ${file}\",\"uri\":\"bytestream://localhost:${worker_port}/blobs/([^/]*) ]]; then
91+
if remote_cas_file_exist ${BASH_REMATCH[1]}; then
92+
cat $BEP_JSON >> $TEST_log && append_remote_cas_files $TEST_log && fail "$file is uploaded"
93+
fi
94+
else
95+
cat $BEP_JSON > $TEST_log
96+
fail "$file is not converted to bytestream://"
97+
fi
98+
}
99+
74100
function test_upload_minimal_convert_paths_for_existed_blobs() {
75101
mkdir -p a
76102
cat > a/BUILD <<EOF
@@ -84,12 +110,11 @@ EOF
84110
bazel build \
85111
--remote_executor=grpc://localhost:${worker_port} \
86112
--experimental_remote_build_event_upload=minimal \
87-
--build_event_json_file=bep.json \
113+
--build_event_json_file=$BEP_JSON \
88114
//a:foo >& $TEST_log || fail "Failed to build"
89115

90-
cat bep.json > $TEST_log
91-
expect_log "a:foo.*bytestream://" || fail "paths for existed blobs should be converted"
92-
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
116+
expect_bes_file_uploaded foo.txt
117+
expect_bes_file_uploaded command.profile.gz
93118
}
94119

95120
function test_upload_minimal_doesnt_upload_missing_blobs() {
@@ -106,12 +131,11 @@ EOF
106131
bazel build \
107132
--remote_executor=grpc://localhost:${worker_port} \
108133
--experimental_remote_build_event_upload=minimal \
109-
--build_event_json_file=bep.json \
134+
--build_event_json_file=$BEP_JSON \
110135
//a:foo >& $TEST_log || fail "Failed to build"
111136

112-
cat bep.json > $TEST_log
113-
expect_not_log "a:foo.*bytestream://" || fail "local files are uploaded"
114-
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
137+
expect_bes_file_not_uploaded foo.txt
138+
expect_bes_file_uploaded command.profile.gz
115139
}
116140

117141
function test_upload_minimal_respect_no_upload_results() {
@@ -128,12 +152,11 @@ EOF
128152
--remote_cache=grpc://localhost:${worker_port} \
129153
--remote_upload_local_results=false \
130154
--experimental_remote_build_event_upload=minimal \
131-
--build_event_json_file=bep.json \
155+
--build_event_json_file=$BEP_JSON \
132156
//a:foo >& $TEST_log || fail "Failed to build"
133157

134-
cat bep.json > $TEST_log
135-
expect_not_log "a:foo.*bytestream://" || fail "local files are uploaded"
136-
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
158+
expect_bes_file_not_uploaded foo.txt
159+
expect_bes_file_uploaded command.profile.gz
137160
}
138161

139162
function test_upload_minimal_respect_no_upload_results_combined_cache() {
@@ -154,12 +177,11 @@ EOF
154177
--incompatible_remote_results_ignore_disk \
155178
--remote_upload_local_results=false \
156179
--experimental_remote_build_event_upload=minimal \
157-
--build_event_json_file=bep.json \
180+
--build_event_json_file=$BEP_JSON \
158181
//a:foo >& $TEST_log || fail "Failed to build"
159182

160-
cat bep.json > $TEST_log
161-
expect_not_log "a:foo.*bytestream://" || fail "local files are uploaded"
162-
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
183+
expect_bes_file_not_uploaded foo.txt
184+
expect_bes_file_uploaded command.profile.gz
163185
remote_cas_files="$(count_remote_cas_files)"
164186
[[ "$remote_cas_files" == 1 ]] || fail "Expected 1 remote cas entries, not $remote_cas_files"
165187
disk_cas_files="$(count_disk_cas_files $cache_dir)"
@@ -187,12 +209,11 @@ EOF
187209
bazel build \
188210
--remote_executor=grpc://localhost:${worker_port} \
189211
--experimental_remote_build_event_upload=minimal \
190-
--build_event_json_file=bep.json \
212+
--build_event_json_file=$BEP_JSON \
191213
//a:foo-alias >& $TEST_log || fail "Failed to build"
192214

193-
cat bep.json > $TEST_log
194-
expect_not_log "a:foo.*bytestream://"
195-
expect_log "command.profile.gz.*bytestream://"
215+
expect_bes_file_not_uploaded foo.txt
216+
expect_bes_file_uploaded command.profile.gz
196217
}
197218

198219
function test_upload_minimal_trees_doesnt_upload_missing_blobs() {
@@ -204,8 +225,9 @@ def _gen_output_dir_impl(ctx):
204225
outputs = [output_dir],
205226
inputs = [],
206227
command = """
207-
mkdir -p $1/sub; \
208-
index=0; while ((index<10)); do echo $index >$1/$index.txt; index=$(($index+1)); done
228+
echo 0 > $1/0.txt
229+
echo 1 > $1/1.txt
230+
mkdir -p $1/sub
209231
echo "Shuffle, duffle, muzzle, muff" > $1/sub/bar
210232
""",
211233
arguments = [output_dir.path],
@@ -233,13 +255,13 @@ EOF
233255
bazel build \
234256
--remote_executor=grpc://localhost:${worker_port} \
235257
--experimental_remote_build_event_upload=minimal \
236-
--build_event_json_file=bep.json \
258+
--build_event_json_file=$BEP_JSON \
237259
//a:foo >& $TEST_log || fail "Failed to build"
238260

239-
cat bep.json > $TEST_log
240-
expect_not_log "a:foo.*bytestream://" || fail "local tree files are uploaded"
241-
expect_not_log "a/dir/.*bytestream://" || fail "local tree files are uploaded"
242-
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
261+
expect_bes_file_not_uploaded dir/0.txt
262+
expect_bes_file_not_uploaded dir/1.txt
263+
expect_bes_file_not_uploaded dir/sub/bar
264+
expect_bes_file_uploaded command.profile.gz
243265
}
244266

245267
function test_upload_minimal_upload_testlogs() {
@@ -259,14 +281,13 @@ EOF
259281
bazel test \
260282
--remote_executor=grpc://localhost:${worker_port} \
261283
--experimental_remote_build_event_upload=minimal \
262-
--build_event_json_file=bep.json \
284+
--build_event_json_file=$BEP_JSON \
263285
//a:test >& $TEST_log || fail "Failed to build"
264286

265-
cat bep.json > $TEST_log
266-
expect_not_log "test.sh.*bytestream://" || fail "test script is uploaded"
267-
expect_log "test.log.*bytestream://" || fail "should upload test.log"
268-
expect_log "test.xml.*bytestream://" || fail "should upload test.xml"
269-
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
287+
expect_bes_file_not_uploaded test.sh
288+
expect_bes_file_uploaded test.log
289+
expect_bes_file_uploaded test.xml
290+
expect_bes_file_uploaded command.profile.gz
270291
}
271292

272293
function test_upload_minimal_upload_buildlogs() {
@@ -283,13 +304,12 @@ EOF
283304
bazel build \
284305
--remote_executor=grpc://localhost:${worker_port} \
285306
--experimental_remote_build_event_upload=minimal \
286-
--build_event_json_file=bep.json \
307+
--build_event_json_file=$BEP_JSON \
287308
//a:foo >& $TEST_log || true
288309

289-
cat bep.json > $TEST_log
290-
expect_log "stdout.*bytestream://" || fail "should upload stdout"
291-
expect_log "stderr.*bytestream://" || fail "should upload stderr"
292-
expect_log "command.profile.gz.*bytestream://" || fail "should upload profile data"
310+
expect_bes_file_uploaded stdout
311+
expect_bes_file_uploaded stderr
312+
expect_bes_file_uploaded command.profile.gz
293313
}
294314

295315
function test_upload_minimal_upload_profile() {
@@ -306,11 +326,10 @@ EOF
306326
--remote_executor=grpc://localhost:${worker_port} \
307327
--experimental_remote_build_event_upload=minimal \
308328
--profile=mycommand.profile.gz \
309-
--build_event_json_file=bep.json \
329+
--build_event_json_file=$BEP_JSON \
310330
//a:foo >& $TEST_log || fail "Failed to build"
311331

312-
cat bep.json > $TEST_log
313-
expect_log "mycommand.profile.gz.*bytestream://" || fail "should upload profile data"
332+
expect_bes_file_uploaded "mycommand.profile.gz"
314333
}
315334

316335
run_suite "Remote build event uploader tests"

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

+9
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ function count_remote_cas_files() {
9696
echo 0
9797
fi
9898
}
99+
100+
function remote_cas_file_exist() {
101+
local file=$1
102+
[ -f "$cas_path/cas/${file:0:2}/$file" ]
103+
}
104+
105+
function append_remote_cas_files() {
106+
find "$cas_path/cas" -type f >> $1
107+
}

0 commit comments

Comments
 (0)