Skip to content

Commit

Permalink
Allow banning symlink action outputs from being uploaded to a remote …
Browse files Browse the repository at this point in the history
…cache.

This is mostly a roll-forward of 4465dae, which
was reverted by fa36d2f. The main difference is
that the new behavior is now gated behind the --noremote_allow_symlink_upload
flag.

https://docs.google.com/document/d/1gnOYszitgrLVet3sQk-TKGqIcpkkDsc6aw-izoo-d64
is a design proposal to support symlinks in the remote cache, which would render
this change moot. I'd like to be able to prevent incorrect cache behavior until
that change is implemented, though.

This fixes #4840 (again).

Closes #5122.

Change-Id: I2136cfe82c2e1a8a9f5856e12a37d42cabd0e299
PiperOrigin-RevId: 195261827
  • Loading branch information
benjaminp authored and Copybara-Service committed May 3, 2018
1 parent adf464f commit dd3ddb0
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -95,8 +94,7 @@ public List<SpawnResult> exec(
// Actual execution.
spawnResult = spawnRunner.exec(spawn, context);
if (cacheHandle.willStore()) {
cacheHandle.store(
spawnResult, listExistingOutputFiles(spawn, actionExecutionContext.getExecRoot()));
cacheHandle.store(spawnResult);
}
}
}
Expand Down Expand Up @@ -144,19 +142,6 @@ public List<SpawnResult> exec(
return ImmutableList.of(spawnResult);
}

private List<Path> listExistingOutputFiles(Spawn spawn, Path execRoot) {
ArrayList<Path> outputFiles = new ArrayList<>();
for (ActionInput output : spawn.getOutputFiles()) {
Path outputPath = execRoot.getRelative(output.getExecPathString());
// TODO(ulfjack): Store the actual list of output files in SpawnResult and use that instead
// of statting the files here again.
if (outputPath.exists()) {
outputFiles.add(outputPath);
}
}
return outputFiles;
}

private final class SpawnExecutionContextImpl implements SpawnExecutionContext {
private final Spawn spawn;
private final ActionExecutionContext actionExecutionContext;
Expand Down
62 changes: 28 additions & 34 deletions src/main/java/com/google/devtools/build/lib/exec/SpawnCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionContext;
import com.google.devtools.build.lib.vfs.Path;
import java.io.Closeable;
import java.io.IOException;
import java.util.Collection;
import java.util.NoSuchElementException;

/**
Expand All @@ -33,32 +31,31 @@
*/
public interface SpawnCache extends ActionContext {
/** A no-op implementation that has no result, and performs no upload. */
public static CacheHandle NO_RESULT_NO_STORE = new CacheHandle() {
@Override
public boolean hasResult() {
return false;
}

@Override
public SpawnResult getResult() {
throw new NoSuchElementException();
}

@Override
public boolean willStore() {
return false;
}

@Override
public void store(SpawnResult result, Collection<Path> files)
throws InterruptedException, IOException {
// Do nothing.
}

@Override
public void close() {
}
};
public static CacheHandle NO_RESULT_NO_STORE =
new CacheHandle() {
@Override
public boolean hasResult() {
return false;
}

@Override
public SpawnResult getResult() {
throw new NoSuchElementException();
}

@Override
public boolean willStore() {
return false;
}

@Override
public void store(SpawnResult result) throws InterruptedException, IOException {
// Do nothing.
}

@Override
public void close() {}
};

/**
* Helper method to create a {@link CacheHandle} from a successful {@link SpawnResult} instance.
Expand All @@ -81,14 +78,12 @@ public boolean willStore() {
}

@Override
public void store(SpawnResult result, Collection<Path> files)
throws InterruptedException, IOException {
public void store(SpawnResult result) throws InterruptedException, IOException {
throw new IllegalStateException();
}

@Override
public void close() {
}
public void close() {}
};
}

Expand Down Expand Up @@ -148,8 +143,7 @@ interface CacheHandle extends Closeable {
* <p>If the current thread is interrupted, then this method should return as quickly as
* possible with an {@link InterruptedException}.
*/
void store(SpawnResult result, Collection<Path> files)
throws InterruptedException, IOException;
void store(SpawnResult result) throws ExecException, InterruptedException, IOException;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

import com.google.devtools.build.lib.actions.EnvironmentalExecException;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.actions.UserExecException;
import com.google.devtools.build.lib.concurrent.ThreadSafety;
import com.google.devtools.build.lib.remote.TreeNodeRepository.TreeNode;
import com.google.devtools.build.lib.remote.util.DigestUtil;
import com.google.devtools.build.lib.util.io.FileOutErr;
import com.google.devtools.build.lib.vfs.Dirent;
import com.google.devtools.build.lib.vfs.FileStatus;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.Symlinks;
import com.google.devtools.remoteexecution.v1test.ActionResult;
import com.google.devtools.remoteexecution.v1test.Command;
import com.google.devtools.remoteexecution.v1test.Digest;
Expand All @@ -45,9 +48,11 @@
/** A cache for storing artifacts (input and output) as well as the output of running an action. */
@ThreadSafety.ThreadSafe
public abstract class AbstractRemoteActionCache implements AutoCloseable {
protected final RemoteOptions options;
protected final DigestUtil digestUtil;

public AbstractRemoteActionCache(DigestUtil digestUtil) {
public AbstractRemoteActionCache(RemoteOptions options, DigestUtil digestUtil) {
this.options = options;
this.digestUtil = digestUtil;
}

Expand Down Expand Up @@ -93,7 +98,7 @@ abstract void upload(
Collection<Path> files,
FileOutErr outErr,
boolean uploadAction)
throws IOException, InterruptedException;
throws ExecException, IOException, InterruptedException;

/**
* Download a remote blob to a local destination.
Expand Down Expand Up @@ -253,46 +258,58 @@ private void downloadOutErr(ActionResult result, FileOutErr outErr)
}
}

/**
* The UploadManifest is used to mutualize upload between the RemoteActionCache implementations.
*/
public class UploadManifest {
/** UploadManifest adds output metadata to a {@link ActionResult}. */
static class UploadManifest {
private final DigestUtil digestUtil;
private final ActionResult.Builder result;
private final Path execRoot;
private final boolean allowSymlinks;
private final Map<Digest, Path> digestToFile;
private final Map<Digest, Chunker> digestToChunkers;

/**
* Create an UploadManifest from an ActionResult builder and an exec root. The ActionResult
* builder is populated through a call to {@link #addFile(Digest, Path)}.
*/
public UploadManifest(ActionResult.Builder result, Path execRoot) {
public UploadManifest(
DigestUtil digestUtil, ActionResult.Builder result, Path execRoot, boolean allowSymlinks) {
this.digestUtil = digestUtil;
this.result = result;
this.execRoot = execRoot;
this.allowSymlinks = allowSymlinks;

this.digestToFile = new HashMap<>();
this.digestToChunkers = new HashMap<>();
}

/**
* Add a collection of files (and directories) to the UploadManifest. Adding a directory has the
* Add a collection of files or directories to the UploadManifest. Adding a directory has the
* effect of 1) uploading a {@link Tree} protobuf message from which the whole structure of the
* directory, including the descendants, can be reconstructed and 2) uploading all the
* non-directory descendant files.
*
* <p>Attempting to a upload symlink results in a {@link
* com.google.build.lib.actions.ExecException}, since cachable actions shouldn't emit symlinks.
*/
public void addFiles(Collection<Path> files) throws IOException, InterruptedException {
public void addFiles(Collection<Path> files)
throws ExecException, IOException, InterruptedException {
for (Path file : files) {
// TODO(ulfjack): Maybe pass in a SpawnResult here, add a list of output files to that, and
// rely on the local spawn runner to stat the files, instead of statting here.
if (!file.exists()) {
FileStatus stat = file.statIfFound(Symlinks.NOFOLLOW);
if (stat == null) {
// We ignore requested results that have not been generated by the action.
continue;
}
if (file.isDirectory()) {
if (stat.isDirectory()) {
addDirectory(file);
} else {
Digest digest = digestUtil.compute(file);
} else if (stat.isFile()) {
Digest digest = digestUtil.compute(file, stat.getSize());
addFile(digest, file);
} else if (allowSymlinks && stat.isSymbolicLink()) {
addFile(digestUtil.compute(file), file);
} else {
illegalOutput(file);
}
}
}
Expand Down Expand Up @@ -321,7 +338,7 @@ private void addFile(Digest digest, Path file) throws IOException {
digestToFile.put(digest, file);
}

private void addDirectory(Path dir) throws IOException {
private void addDirectory(Path dir) throws ExecException, IOException {
Tree.Builder tree = Tree.newBuilder();
Directory root = computeDirectory(dir, tree);
tree.setRoot(root);
Expand All @@ -340,7 +357,8 @@ private void addDirectory(Path dir) throws IOException {
digestToChunkers.put(chunker.digest(), chunker);
}

private Directory computeDirectory(Path path, Tree.Builder tree) throws IOException {
private Directory computeDirectory(Path path, Tree.Builder tree)
throws ExecException, IOException {
Directory.Builder b = Directory.newBuilder();

List<Dirent> sortedDirent = new ArrayList<>(path.readdir(TreeNodeRepository.SYMLINK_POLICY));
Expand All @@ -353,15 +371,28 @@ private Directory computeDirectory(Path path, Tree.Builder tree) throws IOExcept
Directory dir = computeDirectory(child, tree);
b.addDirectoriesBuilder().setName(name).setDigest(digestUtil.compute(dir));
tree.addChildren(dir);
} else {
} else if (dirent.getType() == Dirent.Type.FILE
|| (dirent.getType() == Dirent.Type.SYMLINK && allowSymlinks)) {
Digest digest = digestUtil.compute(child);
b.addFilesBuilder().setName(name).setDigest(digest).setIsExecutable(child.isExecutable());
digestToFile.put(digest, child);
} else {
illegalOutput(child);
}
}

return b.build();
}

private void illegalOutput(Path what) throws ExecException, IOException {
String kind = what.isSymbolicLink() ? "symbolic link" : "special file";
throw new UserExecException(
String.format(
"Output %s is a %s. Only regular files and directories may be "
+ "uploaded to a remote cache. "
+ "Change the file type or use --remote_allow_symlink_upload.",
what.relativeTo(execRoot), kind));
}
}

/** Release resources associated with the cache. The cache may not be used after calling this. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.actions.MetadataProvider;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.remote.Retrier.RetryException;
Expand Down Expand Up @@ -65,7 +66,6 @@
/** A RemoteActionCache implementation that uses gRPC calls to a remote cache server. */
@ThreadSafe
public class GrpcRemoteCache extends AbstractRemoteActionCache {
private final RemoteOptions options;
private final CallCredentials credentials;
private final Channel channel;
private final RemoteRetrier retrier;
Expand All @@ -80,8 +80,7 @@ public GrpcRemoteCache(
RemoteOptions options,
RemoteRetrier retrier,
DigestUtil digestUtil) {
super(digestUtil);
this.options = options;
super(options, digestUtil);
this.credentials = credentials;
this.channel = channel;
this.retrier = retrier;
Expand Down Expand Up @@ -240,7 +239,7 @@ public void upload(
Collection<Path> files,
FileOutErr outErr,
boolean uploadAction)
throws IOException, InterruptedException {
throws ExecException, IOException, InterruptedException {
ActionResult.Builder result = ActionResult.newBuilder();
upload(execRoot, files, outErr, result);
if (!uploadAction) {
Expand All @@ -266,8 +265,9 @@ public void upload(
}

void upload(Path execRoot, Collection<Path> files, FileOutErr outErr, ActionResult.Builder result)
throws IOException, InterruptedException {
UploadManifest manifest = new UploadManifest(result, execRoot);
throws ExecException, IOException, InterruptedException {
UploadManifest manifest =
new UploadManifest(digestUtil, result, execRoot, options.allowSymlinkUpload);
manifest.addFiles(files);

List<Chunker> filesToUpload = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void beforeCommand(CommandEnvironment env) {
if (remoteOrLocalCache) {
cache =
new SimpleBlobStoreActionCache(
remoteOptions,
SimpleBlobStoreFactory.create(
remoteOptions,
GoogleAuthUtils.newCredentials(authAndTlsOptions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,19 @@ public final class RemoteOptions extends OptionsBase {
+ "LogEntry.writeDelimitedTo(OutputStream)."
)
public String experimentalRemoteGrpcLog;

@Option(
name = "remote_allow_symlink_upload",
defaultValue = "true",
category = "remote",
documentationCategory = OptionDocumentationCategory.EXECUTION_STRATEGY,
effectTags = {OptionEffectTag.EXECUTION},
help =
"If true, upload action symlink outputs to the remote cache. "
+ "The remote cache currently doesn't support symlinks, "
+ "so symlink outputs are converted into regular files. "
+ "If this option is not enabled, "
+ "otherwise cachable actions that output symlinks will fail."
)
public boolean allowSymlinkUpload;
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public boolean willStore() {
}

@Override
public void store(SpawnResult result, Collection<Path> files)
throws InterruptedException, IOException {
public void store(SpawnResult result)
throws ExecException, InterruptedException, IOException {
if (options.experimentalGuardAgainstConcurrentChanges) {
try {
checkForConcurrentModifications();
Expand All @@ -183,6 +183,8 @@ public void store(SpawnResult result, Collection<Path> files)
&& Status.SUCCESS.equals(result.status())
&& result.exitCode() == 0;
Context previous = withMetadata.attach();
Collection<Path> files =
RemoteSpawnRunner.resolveActionInputs(execRoot, spawn.getOutputFiles());
try {
remoteCache.upload(actionKey, execRoot, files, context.getFileOutErr(), uploadAction);
} catch (IOException e) {
Expand Down
Loading

0 comments on commit dd3ddb0

Please sign in to comment.