-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic strategy-policy implementation
Basic foundation for limiting strategies used in bazel a la invocation policy. Implemented as an explicit policy for fine-grained control, though for now we just implement an allowlist (in the future it should be extensible enough for more creativity). Naming of the fields in the proto definition is biased towards more ergonomic textproto configuration. Alternatives considered include extending invocation policy to support key/value flags, but this seemed like too much power being added to the flag system for too narrow of a use case. Note that this policy applies universally, regardless of whether or not strategies come by flag or are hard-coded in bazel. This is intentional - it allows *full* control over where/how bazel is executing actions. --strategy_regexp is presently ignored, it requires larger changes to implement, will be done in the next CLs. PiperOrigin-RevId: 635733824 Change-Id: Ic43cf76c2b58b0fcce089119b8ce43a0116c8490
- Loading branch information
1 parent
d553273
commit fdaaa6a
Showing
18 changed files
with
425 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/com/google/devtools/build/lib/exec/SpawnStrategyPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.exec; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.devtools.build.lib.runtime.proto.MnemonicPolicy; | ||
import com.google.devtools.build.lib.runtime.proto.StrategiesForMnemonic; | ||
import java.util.List; | ||
|
||
/** Policy for filtering spawn strategies. */ | ||
public interface SpawnStrategyPolicy { | ||
|
||
/** Returns result of applying policy to per-mnemonic strategies. */ | ||
ImmutableList<String> apply(String mnemonic, List<String> strategies); | ||
|
||
/** Creates new policy from proto descriptor. Empty proto policy implies everything allowed. */ | ||
static SpawnStrategyPolicy create(MnemonicPolicy policy) { | ||
if (MnemonicPolicy.getDefaultInstance().equals(policy)) { | ||
return new AllowAllStrategiesPolicy(); | ||
} | ||
|
||
ImmutableMap.Builder<String, ImmutableSet<String>> perMnemonicAllowList = | ||
ImmutableMap.builder(); | ||
for (StrategiesForMnemonic strategiesForMnemonic : policy.getStrategyAllowlistList()) { | ||
perMnemonicAllowList.put( | ||
strategiesForMnemonic.getMnemonic(), | ||
ImmutableSet.copyOf(strategiesForMnemonic.getStrategyList())); | ||
} | ||
return new SpawnStrategyPolicyImpl( | ||
perMnemonicAllowList.buildKeepingLast(), | ||
ImmutableSet.copyOf(policy.getDefaultAllowlistList())); | ||
} | ||
|
||
/** Allows all strategies - effectively a no-op strategy. */ | ||
class AllowAllStrategiesPolicy implements SpawnStrategyPolicy { | ||
|
||
private AllowAllStrategiesPolicy() {} | ||
|
||
@Override | ||
public ImmutableList<String> apply(String mnemonic, List<String> strategies) { | ||
return ImmutableList.copyOf(strategies); | ||
} | ||
} | ||
|
||
/** Enforces a real strategy policy based on provided config. */ | ||
class SpawnStrategyPolicyImpl implements SpawnStrategyPolicy { | ||
|
||
private final ImmutableMap<String, ImmutableSet<String>> perMnemonicAllowList; | ||
private final ImmutableSet<String> defaultAllowList; | ||
|
||
private SpawnStrategyPolicyImpl( | ||
ImmutableMap<String, ImmutableSet<String>> perMnemonicAllowList, | ||
ImmutableSet<String> defaultAllowList) { | ||
this.perMnemonicAllowList = perMnemonicAllowList; | ||
this.defaultAllowList = defaultAllowList; | ||
} | ||
|
||
@Override | ||
public ImmutableList<String> apply(String mnemonic, List<String> strategies) { | ||
ImmutableSet<String> allowList = | ||
perMnemonicAllowList.getOrDefault(mnemonic, defaultAllowList); | ||
return strategies.stream().filter(allowList::contains).collect(toImmutableList()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.