Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.1] Remote: Don't upload action result if declared outputs are not created. #15050

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -1009,6 +1010,23 @@ public InMemoryOutput downloadOutputs(RemoteAction action, RemoteActionResult re
metadata = parseActionResultMetadata(action, result);
}

// Check that all mandatory outputs are created.
for (ActionInput output : action.spawn.getOutputFiles()) {
if (action.spawn.isMandatoryOutput(output)) {
Path localPath = execRoot.getRelative(output.getExecPath());
if (!metadata.files.containsKey(localPath)
&& !metadata.directories.containsKey(localPath)
&& !metadata.symlinks.containsKey(localPath)) {
throw new IOException(
"Invalid action cache entry "
+ action.actionKey.getDigest().getHash()
+ ": expected output "
+ prettyPrint(output)
+ " does not exist.");
}
}
}

FileOutErr outErr = action.spawnExecutionContext.getFileOutErr();

ImmutableList.Builder<ListenableFuture<FileMetadata>> downloadsBuilder =
Expand Down Expand Up @@ -1120,24 +1138,56 @@ public InMemoryOutput downloadOutputs(RemoteAction action, RemoteActionResult re
return null;
}

private static String prettyPrint(ActionInput actionInput) {
if (actionInput instanceof Artifact) {
return ((Artifact) actionInput).prettyPrint();
} else {
return actionInput.getExecPathString();
}
}

private Single<UploadManifest> buildUploadManifestAsync(
RemoteAction action, SpawnResult spawnResult) {
return Single.fromCallable(
() -> {
ImmutableList.Builder<Path> outputFiles = ImmutableList.builder();
// Check that all mandatory outputs are created.
for (ActionInput outputFile : action.spawn.getOutputFiles()) {
Path localPath = execRoot.getRelative(outputFile.getExecPath());
if (action.spawn.isMandatoryOutput(outputFile) && !localPath.exists()) {
throw new IOException(
"Expected output " + prettyPrint(outputFile) + " was not created locally.");
}
outputFiles.add(localPath);
}

return UploadManifest.create(
remoteOptions,
digestUtil,
remotePathResolver,
action.actionKey,
action.action,
action.command,
outputFiles.build(),
action.spawnExecutionContext.getFileOutErr(),
spawnResult.exitCode());
});
}

@VisibleForTesting
UploadManifest buildUploadManifest(RemoteAction action, SpawnResult spawnResult)
throws ExecException, IOException {
Collection<Path> outputFiles =
action.spawn.getOutputFiles().stream()
.map((inp) -> execRoot.getRelative(inp.getExecPath()))
.collect(ImmutableList.toImmutableList());

return UploadManifest.create(
remoteOptions,
digestUtil,
remotePathResolver,
action.actionKey,
action.action,
action.command,
outputFiles,
action.spawnExecutionContext.getFileOutErr(),
/* exitCode= */ 0);
throws IOException, ExecException, InterruptedException {
try {
return buildUploadManifestAsync(action, spawnResult).blockingGet();
} catch (RuntimeException e) {
Throwable cause = e.getCause();
if (cause != null) {
Throwables.throwIfInstanceOf(cause, IOException.class);
Throwables.throwIfInstanceOf(cause, ExecException.class);
Throwables.throwIfInstanceOf(cause, InterruptedException.class);
}
throw e;
}
}

/** Upload outputs of a remote action which was executed locally to remote cache. */
Expand All @@ -1149,42 +1199,43 @@ public void uploadOutputs(RemoteAction action, SpawnResult spawnResult)
SpawnResult.Status.SUCCESS.equals(spawnResult.status()) && spawnResult.exitCode() == 0,
"shouldn't upload outputs of failed local action");

try {
UploadManifest manifest = buildUploadManifest(action, spawnResult);
if (remoteOptions.remoteCacheAsync) {
Single.using(
remoteCache::retain,
remoteCache ->
manifest.uploadAsync(
action.getRemoteActionExecutionContext(), remoteCache, reporter),
RemoteCache::release)
.subscribeOn(scheduler)
.subscribe(
new SingleObserver<ActionResult>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
backgroundTaskPhaser.register();
}

@Override
public void onSuccess(@NonNull ActionResult actionResult) {
backgroundTaskPhaser.arriveAndDeregister();
}

@Override
public void onError(@NonNull Throwable e) {
backgroundTaskPhaser.arriveAndDeregister();
reportUploadError(e);
}
});
} else {
try (SilentCloseable c =
Profiler.instance().profile(ProfilerTask.UPLOAD_TIME, "upload outputs")) {
manifest.upload(action.getRemoteActionExecutionContext(), remoteCache, reporter);
}
if (remoteOptions.remoteCacheAsync) {
Single.using(
remoteCache::retain,
remoteCache ->
buildUploadManifestAsync(action, spawnResult)
.flatMap(
manifest ->
manifest.uploadAsync(
action.getRemoteActionExecutionContext(), remoteCache, reporter)),
RemoteCache::release)
.subscribeOn(scheduler)
.subscribe(
new SingleObserver<ActionResult>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
backgroundTaskPhaser.register();
}

@Override
public void onSuccess(@NonNull ActionResult actionResult) {
backgroundTaskPhaser.arriveAndDeregister();
}

@Override
public void onError(@NonNull Throwable e) {
backgroundTaskPhaser.arriveAndDeregister();
reportUploadError(e);
}
});
} else {
try (SilentCloseable c =
Profiler.instance().profile(ProfilerTask.UPLOAD_TIME, "upload outputs")) {
UploadManifest manifest = buildUploadManifest(action, spawnResult);
manifest.upload(action.getRemoteActionExecutionContext(), remoteCache, reporter);
} catch (IOException e) {
reportUploadError(e);
}
} catch (IOException e) {
reportUploadError(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,15 @@ ActionResult getActionResult() {
/** Uploads outputs and action result (if exit code is 0) to remote cache. */
public ActionResult upload(
RemoteActionExecutionContext context, RemoteCache remoteCache, ExtendedEventHandler reporter)
throws IOException, InterruptedException {
throws IOException, InterruptedException, ExecException {
try {
return uploadAsync(context, remoteCache, reporter).blockingGet();
} catch (RuntimeException e) {
Throwable cause = e.getCause();
if (cause != null) {
throwIfInstanceOf(cause, InterruptedException.class);
throwIfInstanceOf(cause, IOException.class);
throwIfInstanceOf(cause, ExecException.class);
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1099,9 +1099,21 @@ public void downloadOutputs_missingInMemoryOutputWithMinimal_returnsNull() throw
ActionResult r = ActionResult.newBuilder().setExitCode(0).build();
RemoteActionResult result = RemoteActionResult.createFromCache(CachedActionResult.remote(r));
Artifact a1 = ActionsTestUtil.createArtifact(artifactRoot, "file1");
// set file1 as declared output but not mandatory output
Spawn spawn =
newSpawn(
ImmutableMap.of(REMOTE_EXECUTION_INLINE_OUTPUTS, "outputs/file1"), ImmutableSet.of(a1));
new SimpleSpawn(
new FakeOwner("foo", "bar", "//dummy:label"),
/*arguments=*/ ImmutableList.of(),
/*environment=*/ ImmutableMap.of(),
/*executionInfo=*/ ImmutableMap.of(REMOTE_EXECUTION_INLINE_OUTPUTS, "outputs/file1"),
/*runfilesSupplier=*/ null,
/*filesetMappings=*/ ImmutableMap.of(),
/*inputs=*/ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
/*tools=*/ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
/*outputs=*/ ImmutableSet.of(a1),
/*mandatoryOutputs=*/ ImmutableSet.of(),
ResourceSet.ZERO);

MetadataInjector injector = mock(MetadataInjector.class);
FakeSpawnExecutionContext context = newSpawnExecutionContext(spawn, injector);
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
Expand All @@ -1118,6 +1130,32 @@ public void downloadOutputs_missingInMemoryOutputWithMinimal_returnsNull() throw
verify(injector, never()).injectFile(eq(a1), remoteFileMatchingDigest(d1));
}

@Test
public void downloadOutputs_missingMandatoryOutputs_reportError() throws Exception {
// Test that an AC which misses mandatory outputs is correctly ignored.
Digest fooDigest = cache.addContents(remoteActionExecutionContext, "foo-contents");
ActionResult.Builder builder = ActionResult.newBuilder();
builder.addOutputFilesBuilder().setPath("outputs/foo").setDigest(fooDigest);
RemoteActionResult result =
RemoteActionResult.createFromCache(CachedActionResult.remote(builder.build()));
ImmutableSet.Builder<Artifact> outputs = ImmutableSet.builder();
ImmutableList<String> expectedOutputFiles = ImmutableList.of("outputs/foo", "outputs/bar");
for (String outputFile : expectedOutputFiles) {
Path path = remotePathResolver.outputPathToLocalPath(outputFile);
Artifact output = ActionsTestUtil.createArtifact(artifactRoot, path);
outputs.add(output);
}
Spawn spawn = newSpawn(ImmutableMap.of(), outputs.build());
FakeSpawnExecutionContext context = newSpawnExecutionContext(spawn);
RemoteExecutionService service = newRemoteExecutionService();
RemoteAction action = service.buildRemoteAction(spawn, context);

IOException error =
assertThrows(IOException.class, () -> service.downloadOutputs(action, result));

assertThat(error).hasMessageThat().containsMatch("expected output .+ does not exist.");
}

@Test
public void uploadOutputs_uploadDirectory_works() throws Exception {
// Test that uploading a directory works.
Expand Down Expand Up @@ -1365,6 +1403,27 @@ public void uploadOutputs_firesUploadEvents() throws Exception {
spawn.getResourceOwner(), "ac/" + action.getActionId()));
}

@Test
public void uploadOutputs_missingMandatoryOutputs_dontUpload() throws Exception {
Path file = execRoot.getRelative("outputs/file");
Artifact outputFile = ActionsTestUtil.createArtifact(artifactRoot, file);
RemoteExecutionService service = newRemoteExecutionService();
Spawn spawn = newSpawn(ImmutableMap.of(), ImmutableSet.of(outputFile));
FakeSpawnExecutionContext context = newSpawnExecutionContext(spawn);
RemoteAction action = service.buildRemoteAction(spawn, context);
SpawnResult spawnResult =
new SpawnResult.Builder()
.setExitCode(0)
.setStatus(SpawnResult.Status.SUCCESS)
.setRunnerName("test")
.build();

service.uploadOutputs(action, spawnResult);

// assert
assertThat(cache.getNumFindMissingDigests()).isEmpty();
}

@Test
public void uploadInputsIfNotPresent_deduplicateFindMissingBlobCalls() throws Exception {
int taskCount = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -597,6 +598,7 @@ public void testDownloadMinimal() throws Exception {
when(remoteCache.downloadActionResult(
any(RemoteActionExecutionContext.class), any(), /* inlineOutErr= */ eq(false)))
.thenReturn(CachedActionResult.remote(success));
doReturn(null).when(cache.getRemoteExecutionService()).downloadOutputs(any(), any());

// act
CacheHandle cacheHandle = cache.lookup(simpleSpawn, simplePolicy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ public void testDownloadTopLevel() throws Exception {
RemoteSpawnRunner runner = newSpawnRunner(ImmutableSet.of(topLevelOutput));
RemoteExecutionService service = runner.getRemoteExecutionService();
doReturn(cachedActionResult).when(service).lookupCache(any());
doReturn(null).when(service).downloadOutputs(any(), any());

Spawn spawn = newSimpleSpawn(topLevelOutput);
FakeSpawnExecutionContext policy = getSpawnContext(spawn);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 The Bazel Authors. All rights reserved.
/// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -196,9 +196,12 @@ public final void setUp() throws Exception {
ImmutableList.of("/bin/echo", "Hi!"),
ImmutableMap.of("VARIABLE", "value"),
/*executionInfo=*/ ImmutableMap.<String, String>of(),
/*runfilesSupplier=*/ null,
/*filesetMappings=*/ ImmutableMap.of(),
/*inputs=*/ NestedSetBuilder.create(
Order.STABLE_ORDER, ActionInputHelper.fromPath("input")),
/*outputs=*/ ImmutableSet.<ActionInput>of(
/*tools=*/ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
/*outputs=*/ ImmutableSet.of(
new ActionInput() {
@Override
public String getExecPathString() {
Expand Down Expand Up @@ -231,6 +234,7 @@ public PathFragment getExecPath() {
return PathFragment.create("bar");
}
}),
/*mandatoryOutputs=*/ ImmutableSet.of(),
ResourceSet.ZERO);

Path stdout = fs.getPath("/tmp/stdout");
Expand Down
23 changes: 23 additions & 0 deletions src/test/shell/bazel/remote/remote_execution_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3499,4 +3499,27 @@ EOF
[[ "$disk_cas_files" == 3 ]] || fail "Expected 3 disk cas entries, not $disk_cas_files"
}

function test_missing_outputs_dont_upload_action_result() {
# Test that if declared outputs are not created, even the exit code of action
# is 0, we treat this as failed and don't upload action result.
# See https://github.com/bazelbuild/bazel/issues/14543.
mkdir -p a
cat > a/BUILD <<EOF
genrule(
name = 'foo',
outs = ["foo.txt"],
cmd = "echo foo",
)
EOF

bazel build \
--remote_cache=grpc://localhost:${worker_port} \
//a:foo >& $TEST_log && fail "Should failed to build"

remote_cas_files="$(count_remote_cas_files)"
[[ "$remote_cas_files" == 0 ]] || fail "Expected 0 remote cas entries, not $remote_cas_files"
remote_ac_files="$(count_remote_ac_files)"
[[ "$remote_ac_files" == 0 ]] || fail "Expected 0 remote action cache entries, not $remote_ac_files"
}

run_suite "Remote execution and remote cache tests"