Skip to content

Commit 2b21eea

Browse files
coeuvreBencodes
authored andcommitted
Remote: Add support for tag no-remote-cache-upload.
When executing a spawn that is tagged with no-remote-cache-upload, Bazel still looks up the remote cache. However, its local outputs are not uploaded after local execution. Part of bazelbuild#14338. PiperOrigin-RevId: 414708472
1 parent a7a4fb4 commit 2b21eea

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

src/main/java/com/google/devtools/build/lib/actions/ExecutionRequirements.java

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ public enum WorkerProtocolFormat {
194194
/** Disables remote caching of a spawn. Note: does not disable remote execution */
195195
public static final String NO_REMOTE_CACHE = "no-remote-cache";
196196

197+
/** Disables upload part of remote caching of a spawn. Note: does not disable remote execution */
198+
public static final String NO_REMOTE_CACHE_UPLOAD = "no-remote-cache-upload";
199+
197200
/** Disables remote execution of a spawn. Note: does not disable remote caching */
198201
public static final String NO_REMOTE_EXEC = "no-remote-exec";
199202

src/main/java/com/google/devtools/build/lib/actions/Spawns.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,33 @@
1919
import com.google.devtools.build.lib.server.FailureDetails.Spawn.Code;
2020
import java.io.IOException;
2121
import java.time.Duration;
22+
import java.util.Map;
2223

2324
/** Helper methods relating to implementations of {@link Spawn}. */
2425
public final class Spawns {
2526
private Spawns() {}
2627

27-
/**
28-
* Returns {@code true} if the result of {@code spawn} may be cached.
29-
*/
28+
/** Returns {@code true} if the result of {@code spawn} may be cached. */
3029
public static boolean mayBeCached(Spawn spawn) {
31-
return !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_CACHE)
32-
&& !spawn.getExecutionInfo().containsKey(ExecutionRequirements.LOCAL);
30+
return mayBeCached(spawn.getExecutionInfo());
31+
}
32+
33+
/** Returns {@code true} if the result of {@code spawn} may be cached. */
34+
public static boolean mayBeCached(Map<String, String> executionInfo) {
35+
return !executionInfo.containsKey(ExecutionRequirements.NO_CACHE)
36+
&& !executionInfo.containsKey(ExecutionRequirements.LOCAL);
3337
}
3438

3539
/** Returns {@code true} if the result of {@code spawn} may be cached remotely. */
3640
public static boolean mayBeCachedRemotely(Spawn spawn) {
37-
return mayBeCached(spawn)
38-
&& !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_REMOTE)
39-
&& !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_REMOTE_CACHE);
41+
return mayBeCachedRemotely(spawn.getExecutionInfo());
42+
}
43+
44+
/** Returns {@code true} if the result of {@code spawn} may be cached remotely. */
45+
public static boolean mayBeCachedRemotely(Map<String, String> executionInfo) {
46+
return mayBeCached(executionInfo)
47+
&& !executionInfo.containsKey(ExecutionRequirements.NO_REMOTE)
48+
&& !executionInfo.containsKey(ExecutionRequirements.NO_REMOTE_CACHE);
4049
}
4150

4251
/** Returns {@code true} if {@code spawn} may be executed remotely. */

src/main/java/com/google/devtools/build/lib/remote/util/Utils.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.base.Preconditions;
2626
import com.google.common.base.Throwables;
2727
import com.google.common.collect.ImmutableList;
28+
import com.google.common.collect.ImmutableMap;
2829
import com.google.common.collect.ImmutableSet;
2930
import com.google.common.util.concurrent.AsyncCallable;
3031
import com.google.common.util.concurrent.FluentFuture;
@@ -71,6 +72,7 @@
7172
import java.util.Arrays;
7273
import java.util.Collection;
7374
import java.util.Locale;
75+
import java.util.Map;
7476
import java.util.concurrent.Callable;
7577
import java.util.concurrent.ExecutionException;
7678
import java.util.function.BiFunction;
@@ -596,9 +598,20 @@ public static boolean shouldAcceptCachedResultFromCombinedCache(
596598
}
597599

598600
public static boolean shouldUploadLocalResultsToRemoteCache(
599-
RemoteOptions remoteOptions, @Nullable Spawn spawn) {
601+
RemoteOptions remoteOptions, @Nullable Map<String, String> executionInfo) {
600602
return remoteOptions.remoteUploadLocalResults
601-
&& (spawn == null || Spawns.mayBeCachedRemotely(spawn));
603+
&& (executionInfo == null
604+
|| (Spawns.mayBeCachedRemotely(executionInfo)
605+
&& !executionInfo.containsKey(ExecutionRequirements.NO_REMOTE_CACHE_UPLOAD)));
606+
}
607+
608+
public static boolean shouldUploadLocalResultsToRemoteCache(
609+
RemoteOptions remoteOptions, @Nullable Spawn spawn) {
610+
ImmutableMap<String, String> executionInfo = null;
611+
if (spawn != null) {
612+
executionInfo = spawn.getExecutionInfo();
613+
}
614+
return shouldUploadLocalResultsToRemoteCache(remoteOptions, executionInfo);
602615
}
603616

604617
public static boolean shouldUploadLocalResultsToDiskCache(
@@ -614,13 +627,11 @@ public static boolean shouldUploadLocalResultsToCombinedDisk(
614627
RemoteOptions remoteOptions, @Nullable Spawn spawn) {
615628
if (remoteOptions.incompatibleRemoteResultsIgnoreDisk) {
616629
// If --incompatible_remote_results_ignore_disk is set, we treat the disk cache part as local
617-
// cache. Actions
618-
// which are tagged with `no-remote-cache` can still hit the disk cache.
619-
return spawn == null || Spawns.mayBeCached(spawn);
630+
// cache. Actions which are tagged with `no-remote-cache` can still hit the disk cache.
631+
return shouldUploadLocalResultsToDiskCache(remoteOptions, spawn);
620632
} else {
621633
// Otherwise, it's treated as a remote cache and disabled for `no-remote-cache`.
622-
return remoteOptions.remoteUploadLocalResults
623-
&& (spawn == null || Spawns.mayBeCachedRemotely(spawn));
634+
return shouldUploadLocalResultsToRemoteCache(remoteOptions, spawn);
624635
}
625636
}
626637
}

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

+36
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,42 @@ EOF
19001900
expect_not_log "remote cache hit"
19011901
}
19021902

1903+
function test_tag_no_remote_cache_upload() {
1904+
mkdir -p a
1905+
cat > a/BUILD <<'EOF'
1906+
genrule(
1907+
name = "foo",
1908+
srcs = [],
1909+
outs = ["foo.txt"],
1910+
cmd = "echo \"foo\" > \"$@\"",
1911+
)
1912+
EOF
1913+
1914+
bazel build \
1915+
--remote_cache=grpc://localhost:${worker_port} \
1916+
--modify_execution_info=.*=+no-remote-cache-upload \
1917+
//a:foo >& $TEST_log || "Failed to build //a:foo"
1918+
1919+
remote_ac_files="$(count_remote_ac_files)"
1920+
[[ "$remote_ac_files" == 0 ]] || fail "Expected 0 remote action cache entries, not $remote_ac_files"
1921+
1922+
# populate the cache
1923+
bazel clean
1924+
1925+
bazel build \
1926+
--remote_cache=grpc://localhost:${worker_port} \
1927+
//a:foo >& $TEST_log || "Failed to build //a:foo"
1928+
1929+
bazel clean
1930+
1931+
bazel build \
1932+
--remote_cache=grpc://localhost:${worker_port} \
1933+
--modify_execution_info=.*=+no-remote-cache-upload \
1934+
//a:foo >& $TEST_log || "Failed to build //a:foo"
1935+
1936+
expect_log "remote cache hit"
1937+
}
1938+
19031939
function test_tag_no_remote_cache_for_disk_cache() {
19041940
mkdir -p a
19051941
cat > a/BUILD <<'EOF'

0 commit comments

Comments
 (0)