Skip to content

Commit

Permalink
Hack to specificy the cache strategy per action mnemonic via command …
Browse files Browse the repository at this point in the history
…line (bazelbuild#13)

* [WIP] Hack to specificy the cache strategy per action mnemonic via command line

* Reset cache strategy
  • Loading branch information
antonlopyrevsc authored and chancila committed Nov 3, 2020
1 parent 8e6af06 commit 699bed3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/main/java/com/google/devtools/build/lib/actions/Spawns.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.lib.actions;

import com.google.common.collect.LinkedHashMultimap;
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.server.FailureDetails.Spawn.Code;
Expand All @@ -23,25 +24,57 @@
import java.io.IOException;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;

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

/**
* This is a dirty hack by Anton. Sorry.
*/
private static final LinkedHashMultimap<String, String> sCacheStrategyByMnemonicMap =
LinkedHashMultimap.create();

public static void resetCacheStategy() {
sCacheStrategyByMnemonicMap.clear();
}

/**
* Sets the cache strategy names for a given action mnemonic.
*/
public static void addCacheStrategyByMnemonic(String mnemonic, List<String> strategies) {
sCacheStrategyByMnemonicMap.replaceValues(mnemonic, strategies);
}

/**
* Returns {@code true} if the result of {@code spawn} may be cached.
*/
public static boolean mayBeCached(Spawn spawn) {
return !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_CACHE)
&& !spawn.getExecutionInfo().containsKey(ExecutionRequirements.LOCAL);
if (!spawn.getExecutionInfo().containsKey(ExecutionRequirements.LOCAL)) {
if (sCacheStrategyByMnemonicMap.containsKey(spawn.getMnemonic())) {
return !sCacheStrategyByMnemonicMap.get(spawn.getMnemonic()).contains(ExecutionRequirements.NO_CACHE);
} else {
return !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_CACHE);
}
} else {
return false;
}
}

/** Returns {@code true} if the result of {@code spawn} may be cached remotely. */
public static boolean mayBeCachedRemotely(Spawn spawn) {
return mayBeCached(spawn)
&& !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_REMOTE)
&& !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_REMOTE_CACHE);
if (mayBeCached(spawn)
&& !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_REMOTE)) {
if (sCacheStrategyByMnemonicMap.containsKey(spawn.getMnemonic())) {
return !sCacheStrategyByMnemonicMap.get(spawn.getMnemonic()).contains(ExecutionRequirements.NO_REMOTE_CACHE);
} else {
return !spawn.getExecutionInfo().containsKey(ExecutionRequirements.NO_REMOTE_CACHE);
}
} else {
return false;
}
}

/** Returns {@code true} if {@code spawn} may be executed remotely. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.devtools.build.lib.bazel.rules;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Spawns;
import com.google.devtools.build.lib.analysis.actions.FileWriteActionContext;
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionContext;
import com.google.devtools.build.lib.buildtool.BuildRequest;
Expand Down Expand Up @@ -64,6 +65,11 @@ public void registerSpawnStrategies(
ExecutionOptions options = env.getOptions().getOptions(ExecutionOptions.class);
RemoteOptions remoteOptions = env.getOptions().getOptions(RemoteOptions.class);

Spawns.resetCacheStategy();
for (Map.Entry<String, List<String>> strategy : options.cacheStrategy) {
Spawns.addCacheStrategyByMnemonic(strategy.getKey(), strategy.getValue());
}

List<String> spawnStrategies = new ArrayList<>(options.spawnStrategy);

if (spawnStrategies.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public class ExecutionOptions extends OptionsBase {

public static final ExecutionOptions DEFAULTS = Options.getDefaults(ExecutionOptions.class);

@Option(
name = "cache_strategy",
allowMultiple = true,
converter = Converters.StringToStringListConverter.class,
defaultValue = "null",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
help =
"Specify the cache policy for specific actions by mnemonic")
public List<Map.Entry<String, List<String>>> cacheStrategy;

@Option(
name = "spawn_strategy",
defaultValue = "",
Expand Down

0 comments on commit 699bed3

Please sign in to comment.