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

Remote: Do not upload empty output to remote cache. #13594

Closed
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 @@ -132,6 +132,32 @@ public ActionResult downloadActionResult(
return getFromFuture(cacheProtocol.downloadActionResult(context, actionKey, inlineOutErr));
}

/** Upload a local file to the remote cache. */
public ListenableFuture<Void> uploadFile(
RemoteActionExecutionContext context,
Digest digest,
Path file
) {
if (digest.getSizeBytes() == 0) {
return COMPLETED_SUCCESS;
}

return cacheProtocol.uploadFile(context, digest, file);
}

/** Upload sequence of bytes to the remote cache. */
public ListenableFuture<Void> uploadBlob(
RemoteActionExecutionContext context,
Digest digest,
ByteString data
) {
if (digest.getSizeBytes() == 0) {
return COMPLETED_SUCCESS;
}

return cacheProtocol.uploadBlob(context, digest, data);
}

/**
* Upload the result of a locally executed action to the remote cache.
*
Expand Down Expand Up @@ -212,14 +238,14 @@ private void uploadOutputs(
for (Digest digest : digestsToUpload) {
Path file = digestToFile.get(digest);
if (file != null) {
uploads.add(cacheProtocol.uploadFile(context, digest, file));
uploads.add(uploadFile(context, digest, file));
} else {
ByteString blob = digestToBlobs.get(digest);
if (blob == null) {
String message = "FindMissingBlobs call returned an unknown digest: " + digest;
throw new IOException(message);
}
uploads.add(cacheProtocol.uploadBlob(context, digest, blob));
uploads.add(uploadBlob(context, digest, blob));
}
}

Expand Down Expand Up @@ -588,7 +614,15 @@ public void onFailure(Throwable t) {
return outerF;
}

private List<ListenableFuture<FileMetadata>> downloadOutErr(
private ListenableFuture<Void> downloadBlob(RemoteActionExecutionContext context, Digest digest, OutputStream out) {
if (digest.getSizeBytes() == 0) {
return COMPLETED_SUCCESS;
}

return cacheProtocol.downloadBlob(context, digest, out);
}

public List<ListenableFuture<FileMetadata>> downloadOutErr(
RemoteActionExecutionContext context, ActionResult result, OutErr outErr) {
List<ListenableFuture<FileMetadata>> downloads = new ArrayList<>();
if (!result.getStdoutRaw().isEmpty()) {
Expand All @@ -601,7 +635,7 @@ private List<ListenableFuture<FileMetadata>> downloadOutErr(
} else if (result.hasStdoutDigest()) {
downloads.add(
Futures.transform(
cacheProtocol.downloadBlob(
downloadBlob(
context, result.getStdoutDigest(), outErr.getOutputStream()),
(d) -> null,
directExecutor()));
Expand All @@ -616,7 +650,7 @@ private List<ListenableFuture<FileMetadata>> downloadOutErr(
} else if (result.hasStderrDigest()) {
downloads.add(
Futures.transform(
cacheProtocol.downloadBlob(
downloadBlob(
context, result.getStderrDigest(), outErr.getErrorStream()),
(d) -> null,
directExecutor()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,27 @@ public void testDownloadEmptyBlobAndFile() throws Exception {
assertThat(file.getFileSize()).isEqualTo(0);
}

@Test
public void testDownloadEmptyOutErr() throws Exception {
// Test that downloading empty stdout/stderr does not try to perform a download.

InMemoryRemoteCache remoteCache = newRemoteCache();
Digest emptyDigest = digestUtil.compute(new byte[0]);
ActionResult.Builder result = ActionResult.newBuilder();
result.setStdoutDigest(emptyDigest);
result.setStderrDigest(emptyDigest);

RemoteCache.waitForBulkTransfer(
remoteCache.downloadOutErr(
context,
result.build(),
new FileOutErr(execRoot.getRelative("stdout"), execRoot.getRelative("stderr"))),
true);

assertThat(remoteCache.getNumSuccessfulDownloads()).isEqualTo(0);
assertThat(remoteCache.getNumFailedDownloads()).isEqualTo(0);
}

@Test
public void testDownloadFileWithSymlinkTemplate() throws Exception {
// Test that when a symlink template is provided, we don't actually download files to disk.
Expand Down Expand Up @@ -1547,6 +1568,50 @@ public void testUploadDirectory() throws Exception {
assertThat(remoteCache.findMissingDigests(context, toQuery)).isEmpty();
}

@Test
public void testUploadEmptyBlobAndFile() throws Exception {
// Test that uploading an empty BLOB/file does not try to perform an upload.
InMemoryRemoteCache remoteCache = newRemoteCache();
Digest emptyDigest = fakeFileCache.createScratchInput(ActionInputHelper.fromPath("file"), "");
Path file = execRoot.getRelative("file");

Utils.getFromFuture(remoteCache.uploadBlob(context, emptyDigest, ByteString.EMPTY));
assertThat(remoteCache.findMissingDigests(context, ImmutableSet.of(emptyDigest)))
.containsExactly(emptyDigest);

Utils.getFromFuture(remoteCache.uploadFile(context, emptyDigest, file));
assertThat(remoteCache.findMissingDigests(context, ImmutableSet.of(emptyDigest)))
.containsExactly(emptyDigest);
}

@Test
public void testUploadEmptyOutputs() throws Exception {
// Test that uploading an empty output does not try to perform an upload.

// arrange
Digest emptyDigest =
fakeFileCache.createScratchInput(ActionInputHelper.fromPath("bar/test/wobble"), "");
Path file = execRoot.getRelative("bar/test/wobble");
InMemoryRemoteCache remoteCache = newRemoteCache();
Action action = Action.getDefaultInstance();
ActionKey actionDigest = digestUtil.computeActionKey(action);
Command cmd = Command.getDefaultInstance();

// act
remoteCache.upload(
context,
remotePathResolver,
actionDigest,
action,
cmd,
ImmutableList.of(file),
new FileOutErr(execRoot.getRelative("stdout"), execRoot.getRelative("stderr")));

// assert
assertThat(remoteCache.findMissingDigests(context, ImmutableSet.of(emptyDigest)))
.containsExactly(emptyDigest);
}

@Test
public void testUploadEmptyDirectory() throws Exception {
// Test that uploading an empty directory works.
Expand Down