Skip to content

Commit f5cf8b0

Browse files
coeuvrecopybara-github
authored andcommitted
Remote: Fixes an issue when --experimental_remote_cache_async encounter flaky tests.
When `--experimental_remote_cache_async` is set, the uploads happened in the background -- usually after spawn execution. When the test is failed and there is another test attempt, the outputs of previous test attempt are moved to other places immediately after spawn execution. This fine when combining `--experimental_remote_cache_async` because outputs of failed action don't get uploaded. However, there is an exception that `test.xml` is generated with another spawn before the "move" happens. The result of the spawn used to generate `test.xml` is usually "succeed" which means Bazel will attempt upload `test.xml` even if the test itself is failed. This PR makes the `test.xml` generation spawn ignores remote cache if the test itself is failed. Fixes bazelbuild#14008. Closes bazelbuild#14220. PiperOrigin-RevId: 408237437
1 parent bd8f1e6 commit f5cf8b0

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.ImmutableMap;
2222
import com.google.common.collect.ImmutableSet;
23+
import com.google.common.collect.Maps;
2324
import com.google.common.io.ByteStreams;
2425
import com.google.common.util.concurrent.ListenableFuture;
2526
import com.google.devtools.build.lib.actions.ActionExecutionContext;
@@ -535,13 +536,22 @@ private static Spawn createXmlGeneratingSpawn(
535536
envBuilder.put("TEST_SHARD_INDEX", "0");
536537
envBuilder.put("TEST_TOTAL_SHARDS", "0");
537538
}
539+
Map<String, String> executionInfo =
540+
Maps.newHashMapWithExpectedSize(action.getExecutionInfo().size() + 1);
541+
executionInfo.putAll(action.getExecutionInfo());
542+
if (result.exitCode() != 0) {
543+
// If the test is failed, the spawn shouldn't use remote cache since the test.xml file is
544+
// renamed immediately after the spawn execution. If there is another test attempt, the async
545+
// upload will fail because it cannot read the file at original position.
546+
executionInfo.put(ExecutionRequirements.NO_REMOTE_CACHE, "");
547+
}
538548
return new SimpleSpawn(
539549
action,
540550
args,
541551
envBuilder.build(),
542552
// Pass the execution info of the action which is identical to the supported tags set on the
543553
// test target. In particular, this does not set the test timeout on the spawn.
544-
ImmutableMap.copyOf(action.getExecutionInfo()),
554+
ImmutableMap.copyOf(executionInfo),
545555
null,
546556
ImmutableMap.of(),
547557
/*inputs=*/ NestedSetBuilder.create(

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

+33
Original file line numberDiff line numberDiff line change
@@ -3171,4 +3171,37 @@ EOF
31713171
expect_log "-r-xr-xr-x"
31723172
}
31733173

3174+
function test_async_upload_works_for_flaky_tests() {
3175+
mkdir -p a
3176+
cat > a/BUILD <<EOF
3177+
sh_test(
3178+
name = "test",
3179+
srcs = ["test.sh"],
3180+
)
3181+
3182+
genrule(
3183+
name = "foo",
3184+
outs = ["foo.txt"],
3185+
cmd = "echo \"foo bar\" > \$@",
3186+
)
3187+
EOF
3188+
cat > a/test.sh <<EOF
3189+
#!/bin/sh
3190+
echo "it always fails"
3191+
exit 1
3192+
EOF
3193+
chmod +x a/test.sh
3194+
3195+
# Check the error message when failed to upload
3196+
bazel build --remote_cache=http://nonexistent.example.org //a:foo >& $TEST_log || fail "Failed to build"
3197+
expect_log "WARNING: Writing to Remote Cache:"
3198+
3199+
bazel test \
3200+
--remote_cache=grpc://localhost:${worker_port} \
3201+
--experimental_remote_cache_async \
3202+
--flaky_test_attempts=2 \
3203+
//a:test >& $TEST_log && fail "expected failure" || true
3204+
expect_not_log "WARNING: Writing to Remote Cache:"
3205+
}
3206+
31743207
run_suite "Remote execution and remote cache tests"

0 commit comments

Comments
 (0)