diff options
author | ulfjack <ulfjack@google.com> | 2018-06-11 01:38:33 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-06-11 01:40:17 -0700 |
commit | 2c89206a407d8cae0945abe7eb179ee6bb77d9de (patch) | |
tree | 1314352acb1ccda52be27d8d063104eae891669c /src/main/java/com/google/devtools/build/lib/exec | |
parent | ce9aafcf63dd198fe16c682dfeedc444f07b5669 (diff) |
Remove ActionContextConsumer
Instead, add some simple APIs to ExecutorBuilder and inline all the previous
subclasses into their corresponding modules. This removes a bunch of
boilerplate.
PiperOrigin-RevId: 200017162
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/exec')
4 files changed, 52 insertions, 67 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/exec/ActionContextConsumer.java b/src/main/java/com/google/devtools/build/lib/exec/ActionContextConsumer.java deleted file mode 100644 index a63176b4b1..0000000000 --- a/src/main/java/com/google/devtools/build/lib/exec/ActionContextConsumer.java +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2014 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 com.google.devtools.build.lib.actions.ActionContext; -import com.google.devtools.build.lib.exec.SpawnActionContextMaps.Builder; - -/** - * An object describing that actions require a particular implementation of an {@link - * ActionContext}. - * - * <p>This is expected to be implemented by modules that also implement actions which need these - * contexts. Other modules will provide implementations for various action contexts by implementing - * {@link ActionContextProvider}. - * - * <p>Example: a module requires {@code SpawnActionContext} to do its job, and it creates actions - * with the mnemonic <code>C++</code>. Then the {@link #populate(Builder)} method of this module - * would put <code>("C++", strategy)</code> in the map returned by {@link - * Builder#strategyByMnemonicMap()}. - * - * <p>The module can either decide for itself which implementation is needed and make the value - * associated with this key a constant or defer that decision to the user, for example, by providing - * a command line option and setting the value in the map based on that. - * - * <p>Other modules are free to provide different implementations of {@code SpawnActionContext}. - * This can be used, for example, to implement sandboxed or distributed execution of {@code - * SpawnAction}s in different ways, while giving the user control over how exactly they are - * executed. - * - * <p>Example: if a module requires {@code MyCustomActionContext} to be available, but doesn't - * associate it with any strategy, its {@link #populate(Builder)} should add {@code - * (MyCustomActionContext.class, "")} to the builder's {@link Builder#strategyByContextMap}. - * - * <p>Example: if a module requires {@code MyLocalCustomActionContext} to be available, and wants it - * to always use the "local" strategy, its {@link #populate(Builder)} should add {@code - * (MyCustomActionContext.class, "local")} to the builder's {@link Builder#strategyByContextMap}. . - */ -public interface ActionContextConsumer { - /** - * Provides a {@link SpawnActionContextMaps.Builder} instance which modules may use to configure - * the {@link ActionContext} instances the module requires for particular actions. - */ - void populate(SpawnActionContextMaps.Builder builder); -} diff --git a/src/main/java/com/google/devtools/build/lib/exec/ActionContextProvider.java b/src/main/java/com/google/devtools/build/lib/exec/ActionContextProvider.java index 79ea2cdf49..0afdb1366b 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/ActionContextProvider.java +++ b/src/main/java/com/google/devtools/build/lib/exec/ActionContextProvider.java @@ -22,7 +22,7 @@ import com.google.devtools.build.lib.actions.MetadataProvider; /** * An object that provides execution strategies to {@link BlazeExecutor}. * - * <p>For more information, see {@link ActionContextConsumer}. + * <p>For more information, see {@link ExecutorBuilder}. */ public abstract class ActionContextProvider { /** diff --git a/src/main/java/com/google/devtools/build/lib/exec/ExecutorBuilder.java b/src/main/java/com/google/devtools/build/lib/exec/ExecutorBuilder.java index 398e183c43..753dd8fbce 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/ExecutorBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/exec/ExecutorBuilder.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.actions.ActionContext; import com.google.devtools.build.lib.actions.ActionInputPrefetcher; import com.google.devtools.build.lib.actions.Executor; +import com.google.devtools.build.lib.util.RegexFilter; import java.util.ArrayList; import java.util.List; @@ -27,7 +28,8 @@ import java.util.List; */ public class ExecutorBuilder { private final List<ActionContextProvider> actionContextProviders = new ArrayList<>(); - private final List<ActionContextConsumer> actionContextConsumers = new ArrayList<>(); + private final SpawnActionContextMaps.Builder spawnActionContextMapsBuilder = + new SpawnActionContextMaps.Builder(); private ActionInputPrefetcher prefetcher; // These methods shouldn't be public, but they have to be right now as ExecutionTool is in another @@ -36,8 +38,8 @@ public class ExecutorBuilder { return ImmutableList.copyOf(actionContextProviders); } - public ImmutableList<ActionContextConsumer> getActionContextConsumers() { - return ImmutableList.copyOf(actionContextConsumers); + public SpawnActionContextMaps.Builder getSpawnActionContextMapsBuilder() { + return spawnActionContextMapsBuilder; } public ActionInputPrefetcher getActionInputPrefetcher() { @@ -61,10 +63,48 @@ public class ExecutorBuilder { } /** - * Adds the specified action context consumer to the executor. + * Sets the strategy name for a given action mnemonic. + * + * <p>The calling module can either decide for itself which implementation is needed and make the + * value associated with this key a constant or defer that decision to the user, for example, by + * providing a command line option and setting the value in the map based on that. + * + * <p>Example: a module requires {@code SpawnActionContext} to do its job, and it creates actions + * with the mnemonic <code>C++</code>. The the module can call + * <code>addStrategyByMnemonic("C++", strategy)</code>. */ - public ExecutorBuilder addActionContextConsumer(ActionContextConsumer consumer) { - this.actionContextConsumers.add(consumer); + public ExecutorBuilder addStrategyByMnemonic(String mnemonic, String strategy) { + spawnActionContextMapsBuilder.strategyByMnemonicMap().put(mnemonic, strategy); + return this; + } + + /** + * Adds an implementation with a specific strategy name. + * + * <p>Modules are free to provide different implementations of {@code ActionContext}. This can be + * used, for example, to implement sandboxed or distributed execution of {@code SpawnAction}s in + * different ways, while giving the user control over how exactly they are executed. + * + * <p>Example: a module requires {@code MyCustomActionContext} to be available, but doesn't + * associate it with any strategy. Call + * <code>addStrategyByContext(MyCustomActionContext.class, "")</code>. + * + * <p>Example: a module requires {@code MyLocalCustomActionContext} to be available, and wants + * it to always use the "local" strategy. Call + * <code>addStrategyByContext(MyCustomActionContext.class, "local")</code>. + */ + public ExecutorBuilder addStrategyByContext( + Class<? extends ActionContext> actionContext, String strategy) { + spawnActionContextMapsBuilder.strategyByContextMap().put(actionContext, strategy); + return this; + } + + /** + * Similar to {@link #addStrategyByMnemonic}, but allows specifying a regex for the set of + * matching mnemonics, instead of an exact string. + */ + public ExecutorBuilder addStrategyByRegexp(RegexFilter regexFilter, String strategy) { + spawnActionContextMapsBuilder.addStrategyByRegexp(regexFilter, strategy); return this; } diff --git a/src/main/java/com/google/devtools/build/lib/exec/SpawnActionContextMaps.java b/src/main/java/com/google/devtools/build/lib/exec/SpawnActionContextMaps.java index 4fc32aa37d..8a1a06779e 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/SpawnActionContextMaps.java +++ b/src/main/java/com/google/devtools/build/lib/exec/SpawnActionContextMaps.java @@ -54,8 +54,8 @@ import java.util.TreeMap; /** * Container for looking up the {@link ActionContext} to use for a given action. * - * <p>Holds {@link ActionContext} mappings populated by {@link ActionContextConsumer} modules. These - * include mappings from mnemonics and from description patterns. + * <p>Holds {@link ActionContext} mappings populated by modules. These include mappings from + * mnemonics and from description patterns. * * <p>At startup time, the application provides {@link Builder} to each module to register its * contexts and mappings. At runtime, the {@link BlazeExecutor} uses the constructed object to find @@ -201,9 +201,9 @@ public final class SpawnActionContextMaps { strategyByContextMapBuilder = ImmutableListMultimap.builder(); private final ImmutableList.Builder<RegexFilterStrategy> strategyByRegexpBuilder = - new ImmutableList.Builder(); + ImmutableList.builder(); - /* + /** * Returns a builder modules can use to add mappings from mnemonics to strategy names. * * <p>If a spawn action is executed whose mnemonic maps to the empty string or is not present in @@ -217,7 +217,7 @@ public final class SpawnActionContextMaps { return strategyByMnemonicMapBuilder; } - /* + /** * Returns a builder modules can use to associate {@link ActionContext} classes with * strategy names. */ |