diff --git a/src/main/java/com/google/devtools/build/lib/remote/Scrubber.java b/src/main/java/com/google/devtools/build/lib/remote/Scrubber.java index e0ff40881a597b..886a681129935a 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/Scrubber.java +++ b/src/main/java/com/google/devtools/build/lib/remote/Scrubber.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.actions.ActionInput; +import com.google.devtools.build.lib.actions.ActionOwner; import com.google.devtools.build.lib.actions.Spawn; import com.google.devtools.build.lib.actions.cache.VirtualActionInput; import com.google.devtools.build.lib.remote.RemoteScrubbing.Config; @@ -104,6 +105,7 @@ public static class SpawnScrubber { private final Pattern mnemonicPattern; private final Pattern labelPattern; + private final Pattern kindPattern; private final boolean matchTools; private final ImmutableList omittedInputPatterns; @@ -114,6 +116,7 @@ private SpawnScrubber(Config.Rule ruleProto) { Config.Matcher matcherProto = ruleProto.getMatcher(); this.mnemonicPattern = Pattern.compile(emptyToAll(matcherProto.getMnemonic())); this.labelPattern = Pattern.compile(emptyToAll(matcherProto.getLabel())); + this.kindPattern = Pattern.compile(emptyToAll(matcherProto.getKind())); this.matchTools = matcherProto.getMatchTools(); Config.Transform transformProto = ruleProto.getTransform(); @@ -134,12 +137,15 @@ private String emptyToAll(String s) { /** Whether this scrubber applies to the given {@link Spawn}. */ private boolean matches(Spawn spawn) { String mnemonic = spawn.getMnemonic(); - String label = spawn.getResourceOwner().getOwner().getLabel().getCanonicalForm(); - boolean isForTool = spawn.getResourceOwner().getOwner().isBuildConfigurationForTool(); + ActionOwner actionOwner = spawn.getResourceOwner().getOwner(); + String label = actionOwner.getLabel().getCanonicalForm(); + String kind = actionOwner.getTargetKind(); + boolean isForTool = actionOwner.isBuildConfigurationForTool(); return (!isForTool || matchTools) && mnemonicPattern.matcher(mnemonic).matches() - && labelPattern.matcher(label).matches(); + && labelPattern.matcher(label).matches() + && kindPattern.matcher(kind).matches(); } /** Whether the given input should be omitted from the cache key. */ diff --git a/src/main/protobuf/remote_scrubbing.proto b/src/main/protobuf/remote_scrubbing.proto index 9cbffd93448fa4..ad6405e119200c 100644 --- a/src/main/protobuf/remote_scrubbing.proto +++ b/src/main/protobuf/remote_scrubbing.proto @@ -23,6 +23,10 @@ option java_package = "com.google.devtools.build.lib.remote"; message Config { // Describes a set of actions. An action must pass all criteria to match. message Matcher { + // A regex matching the rule kind of the action owner. + // Use .* if a partial match is desired. + // If empty, matches every kind. + string kind = 4; // A regex matching the canonical label of the action owner. // Use .* if a partial match is desired. // If empty, matches every label. diff --git a/src/test/java/com/google/devtools/build/lib/exec/util/FakeOwner.java b/src/test/java/com/google/devtools/build/lib/exec/util/FakeOwner.java index 2e9ad03a2562c4..bbf9fbfb5fb87a 100644 --- a/src/test/java/com/google/devtools/build/lib/exec/util/FakeOwner.java +++ b/src/test/java/com/google/devtools/build/lib/exec/util/FakeOwner.java @@ -43,7 +43,8 @@ public class FakeOwner implements ActionExecutionMetadata { private final String mnemonic; private final String progressMessage; - @Nullable private final String ownerLabel; + private final String ownerLabel; + private final String ownerRuleKind; @Nullable private final Artifact primaryOutput; @Nullable private final PlatformInfo platform; private final ImmutableMap execProperties; @@ -53,6 +54,7 @@ public class FakeOwner implements ActionExecutionMetadata { String mnemonic, String progressMessage, String ownerLabel, + String ownerRuleKind, @Nullable Artifact primaryOutput, @Nullable PlatformInfo platform, ImmutableMap execProperties, @@ -60,6 +62,7 @@ public class FakeOwner implements ActionExecutionMetadata { this.mnemonic = mnemonic; this.progressMessage = progressMessage; this.ownerLabel = checkNotNull(ownerLabel); + this.ownerRuleKind = checkNotNull(ownerRuleKind); this.primaryOutput = primaryOutput; this.platform = platform; this.execProperties = execProperties; @@ -72,6 +75,7 @@ private FakeOwner( mnemonic, progressMessage, ownerLabel, + /* ownerRuleKind= */ "dummy-target-kind", /* primaryOutput= */ null, platform, ImmutableMap.of(), @@ -87,7 +91,7 @@ public ActionOwner getOwner() { return ActionOwner.createDummy( Label.parseCanonicalUnchecked(ownerLabel), new Location("dummy-file", 0, 0), - /* targetKind= */ "dummy-target-kind", + ownerRuleKind, mnemonic, /* configurationChecksum= */ "configurationChecksum", new BuildConfigurationEvent( diff --git a/src/test/java/com/google/devtools/build/lib/exec/util/SpawnBuilder.java b/src/test/java/com/google/devtools/build/lib/exec/util/SpawnBuilder.java index 5f3a1aedffb109..334684427c53f4 100644 --- a/src/test/java/com/google/devtools/build/lib/exec/util/SpawnBuilder.java +++ b/src/test/java/com/google/devtools/build/lib/exec/util/SpawnBuilder.java @@ -47,6 +47,7 @@ public final class SpawnBuilder { private String mnemonic = "Mnemonic"; private String progressMessage = "progress message"; private String ownerLabel = "//dummy:label"; + private String ownerRuleKind = "dummy-target-kind"; @Nullable private Artifact ownerPrimaryOutput; @Nullable private PlatformInfo platform; private final List args; @@ -96,6 +97,7 @@ public Spawn build() { mnemonic, progressMessage, ownerLabel, + ownerRuleKind, ownerPrimaryOutput, platform, execProperties, @@ -140,6 +142,12 @@ public SpawnBuilder withOwnerLabel(String ownerLabel) { return this; } + @CanIgnoreReturnValue + public SpawnBuilder withOwnerRuleKind(String ownerRuleKind) { + this.ownerRuleKind = checkNotNull(ownerRuleKind); + return this; + } + @CanIgnoreReturnValue public SpawnBuilder withOwnerPrimaryOutput(Artifact output) { ownerPrimaryOutput = checkNotNull(output); diff --git a/src/test/java/com/google/devtools/build/lib/remote/ScrubberTest.java b/src/test/java/com/google/devtools/build/lib/remote/ScrubberTest.java index 2eb2c0cc050b8b..f8f3cd8d71c403 100644 --- a/src/test/java/com/google/devtools/build/lib/remote/ScrubberTest.java +++ b/src/test/java/com/google/devtools/build/lib/remote/ScrubberTest.java @@ -125,6 +125,54 @@ public void matchWildcardLabel() { assertThat(scrubber.forSpawn(createSpawn("//spam:eggs", "Foo"))).isNull(); } + @Test + public void matchExactKind() { + var scrubber = + new Scrubber( + Config.newBuilder() + .addRules( + Config.Rule.newBuilder() + .setMatcher(Config.Matcher.newBuilder().setKind("java_library"))) + .build()); + + assertThat(scrubber.forSpawn(createSpawn("//foo:bar", "Foo", "java_library", false))) + .isNotNull(); + assertThat(scrubber.forSpawn(createSpawn("//foo:barbaz", "Foo", "java_test", false))).isNull(); + } + + @Test + public void matchUnionKind() { + var scrubber = + new Scrubber( + Config.newBuilder() + .addRules( + Config.Rule.newBuilder() + .setMatcher(Config.Matcher.newBuilder().setKind("java_library|java_test"))) + .build()); + + assertThat(scrubber.forSpawn(createSpawn("//foo:bar", "Foo", "java_library", false))) + .isNotNull(); + assertThat(scrubber.forSpawn(createSpawn("//spam:eggs", "Foo", "java_test", false))) + .isNotNull(); + assertThat(scrubber.forSpawn(createSpawn("//quux:xyzzy", "Foo", "go_library", false))).isNull(); + } + + @Test + public void matchWildcardKind() { + var scrubber = + new Scrubber( + Config.newBuilder() + .addRules( + Config.Rule.newBuilder() + .setMatcher(Config.Matcher.newBuilder().setKind("java_.*"))) + .build()); + + assertThat(scrubber.forSpawn(createSpawn("//foo:bar", "Foo", "java_library", false))) + .isNotNull(); + assertThat(scrubber.forSpawn(createSpawn("//foo:baz", "Foo", "java_test", false))).isNotNull(); + assertThat(scrubber.forSpawn(createSpawn("//spam:eggs", "Foo", "go_library", false))).isNull(); + } + @Test public void rejectToolAction() { var scrubber = @@ -137,7 +185,9 @@ public void rejectToolAction() { .build()); assertThat(scrubber.forSpawn(createSpawn("//foo:bar", "Foo"))).isNotNull(); - assertThat(scrubber.forSpawn(createSpawn("//foo:bar", "Foo", /* forTool= */ true))).isNull(); + assertThat( + scrubber.forSpawn(createSpawn("//foo:bar", "Foo", "java_library", /* forTool= */ true))) + .isNull(); } @Test @@ -155,7 +205,9 @@ public void acceptToolAction() { .build()); assertThat(scrubber.forSpawn(createSpawn("//foo:bar", "Foo"))).isNotNull(); - assertThat(scrubber.forSpawn(createSpawn("//foo:bar", "Foo", /* forTool= */ true))).isNotNull(); + assertThat( + scrubber.forSpawn(createSpawn("//foo:bar", "Foo", "java_library", /* forTool= */ true))) + .isNotNull(); } @Test @@ -420,13 +472,15 @@ private static Spawn createSpawn() { } private static Spawn createSpawn(String label, String mnemonic) { - return createSpawn(label, mnemonic, /* forTool= */ false); + return createSpawn(label, mnemonic, /* ruleKind= */ "dummy-target-kind", /* forTool= */ false); } - private static Spawn createSpawn(String label, String mnemonic, boolean forTool) { + private static Spawn createSpawn( + String label, String mnemonic, String ruleKind, boolean forTool) { return new SpawnBuilder("cmd") .withOwnerLabel(label) .withMnemonic(mnemonic) + .withOwnerRuleKind(ruleKind) .setBuiltForToolConfiguration(forTool) .build(); }