From 3739927847f712f1fbf954c754131ea2593815b6 Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Thu, 8 Sep 2022 15:13:00 +0300 Subject: [PATCH] Add `experimental_remote_cache_key_ignore_stamping` to skip volatile stamping files on compute shared cache key --- .../lib/remote/RemoteExecutionService.java | 39 +++++++++++++++++-- .../lib/remote/options/RemoteOptions.java | 8 ++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java index 4eaf15b5ae6615..b300d5014ebe54 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java @@ -80,6 +80,7 @@ import com.google.devtools.build.lib.actions.UserExecException; import com.google.devtools.build.lib.analysis.platform.PlatformUtils; import com.google.devtools.build.lib.buildtool.buildevent.BuildInterruptedEvent; +import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.Reporter; import com.google.devtools.build.lib.exec.SpawnInputExpander.InputWalker; @@ -125,6 +126,7 @@ import io.reactivex.rxjava3.core.SingleObserver; import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.schedulers.Schedulers; + import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -365,7 +367,8 @@ public CachePolicy getWriteCachePolicy(Spawn spawn) { public boolean mayBeExecutedRemotely(Spawn spawn) { return remoteCache instanceof RemoteExecutionCache && remoteExecutor != null - && Spawns.mayBeExecutedRemotely(spawn); + && Spawns.mayBeExecutedRemotely(spawn) + && !(remoteOptions.remoteCacheKeyIgnoreStamping && hasVolatileArtifacts(spawn.getInputFiles())); } private SortedMap buildOutputDirMap(Spawn spawn) { @@ -410,7 +413,7 @@ private MerkleTree buildInputMerkleTree(Spawn spawn, SpawnExecutionContext conte newInputMap.putAll(outputDirMap); inputMap = newInputMap; } - return MerkleTree.build(inputMap, context.getMetadataProvider(), execRoot, digestUtil); + return MerkleTree.build(filterInputs(inputMap), context.getMetadataProvider(), execRoot, digestUtil); } } @@ -431,7 +434,7 @@ public MerkleTree uncachedBuildMerkleTreeVisitor( throws IOException, ForbiddenActionInputException { ConcurrentLinkedQueue subMerkleTrees = new ConcurrentLinkedQueue<>(); subMerkleTrees.add( - MerkleTree.build(walker.getLeavesInputMapping(), metadataProvider, execRoot, digestUtil)); + MerkleTree.build(filterInputs(walker.getLeavesInputMapping()), metadataProvider, execRoot, digestUtil)); walker.visitNonLeaves( (Object subNodeKey, InputWalker subWalker) -> { subMerkleTrees.add(buildMerkleTreeVisitor(subNodeKey, subWalker, metadataProvider)); @@ -439,6 +442,20 @@ public MerkleTree uncachedBuildMerkleTreeVisitor( return MerkleTree.merge(subMerkleTrees, digestUtil); } + private SortedMap filterInputs(SortedMap inputs) { + if (!remoteOptions.remoteCacheKeyIgnoreStamping) { + return inputs; + } + SortedMap result = new TreeMap<>(); + for (Entry entry : inputs.entrySet()) { + ActionInput input = entry.getValue(); + if (!isConstantMetadata(input)) { + result.put(entry.getKey(), input); + } + } + return result; + } + @Nullable private static ByteString buildSalt(Spawn spawn) { String workspace = @@ -1494,4 +1511,20 @@ void report(Event evt) { reporter.handle(evt); } } + + private static boolean hasVolatileArtifacts(NestedSet inputFiles) { + for (ActionInput inputFile : inputFiles.getLeaves()) { + if (isConstantMetadata(inputFile)) { + return true; + } + } + return false; + } + + private static boolean isConstantMetadata(ActionInput input) { + if (input instanceof Artifact) { + return ((Artifact) input).isConstantMetadata(); + } + return false; + } } diff --git a/src/main/java/com/google/devtools/build/lib/remote/options/RemoteOptions.java b/src/main/java/com/google/devtools/build/lib/remote/options/RemoteOptions.java index 332959a9105b09..b4a076fe9bfdfa 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/options/RemoteOptions.java +++ b/src/main/java/com/google/devtools/build/lib/remote/options/RemoteOptions.java @@ -83,6 +83,14 @@ public final class RemoteOptions extends CommonRemoteOptions { + "disable TLS.") public String remoteExecutor; + @Option( + name = "experimental_remote_cache_key_ignore_stamping", + defaultValue = "false", + documentationCategory = OptionDocumentationCategory.REMOTE, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "Don't use volatile stamping data in shared cache key. Also disable remote execution for stamping actions.") + public boolean remoteCacheKeyIgnoreStamping; + @Option( name = "experimental_remote_execution_keepalive", defaultValue = "false",