Skip to content

Commit

Permalink
Slightly refactor SpawnAction to improve env handling
Browse files Browse the repository at this point in the history
This is in preparation for fixing env handling as well as cache key (to use
env) computations in subclasses of SpawnAction.

PiperOrigin-RevId: 196626495
  • Loading branch information
ulfjack authored and Copybara-Service committed May 15, 2018
1 parent 150a906 commit 27487c7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
* <p>Inherited environment variables must be declared in the Action interface (see {@link
* Action#getClientEnvironmentVariables}), so that the dependency on the client environment is known
* to the execution framework for correct incremental builds.
*
* <p>By splitting the environment, we can handle environment variable changes more efficiently -
* the dependency of the action on the environment variable are tracked in Skyframe (and in the
* action cache), such that Bazel knows exactly which actions it needs to rerun, and does not have
* to reanalyze the entire dependency graph.
*/
@AutoCodec
public final class ActionEnvironment {
Expand Down Expand Up @@ -96,10 +101,26 @@ public static ActionEnvironment create(Map<String, String> fixedEnv) {
return new ActionEnvironment(ImmutableMap.copyOf(fixedEnv), ImmutableSet.of());
}

/** Returns the combined size of the fixed and inherited environments. */
public int size() {
return fixedEnv.size() + inheritedEnv.size();
}

/**
* Returns the 'fixed' part of the environment, i.e., those environment variables that are set to
* fixed values and their values. This should only be used for testing and to compute the cache
* keys of actions. Use {@link #resolve} instead to get the complete environment.
*/
public ImmutableMap<String, String> getFixedEnv() {
return fixedEnv;
}

/**
* Returns the 'inherited' part of the environment, i.e., those environment variables that are
* inherited from the client environment and therefore have no fixed value here. This should only
* be used for testing and to compute the cache keys of actions. Use {@link #resolve} instead to
* get the complete environment.
*/
public ImmutableSet<String> getInheritedEnv() {
return inheritedEnv;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.devtools.build.lib.actions.AbstractAction;
Expand Down Expand Up @@ -339,9 +340,10 @@ private static String truncate(String s, int maxLen) {
* which also depends on the client environment. Subclasses that which to override the way to get
* a spawn should override the other GetSpawn() methods instead.
*/
@VisibleForTesting
public final Spawn getSpawn() throws CommandLineExpansionException {
return new ActionSpawn(
commandLines.allArguments(), null, ImmutableList.of(), ImmutableMap.of());
commandLines.allArguments(), ImmutableMap.of(), ImmutableList.of(), ImmutableMap.of());
}

/**
Expand Down Expand Up @@ -385,7 +387,7 @@ protected void computeKey(ActionKeyContext actionKeyContext, Fingerprint fp)
for (Artifact runfilesManifest : runfilesManifests) {
fp.addPath(runfilesManifest.getExecPath());
}
fp.addStringMap(getEnvironment());
fp.addStringMap(env.getFixedEnv());
fp.addStrings(getClientEnvironmentVariables());
fp.addStringMap(getExecutionInfo());
}
Expand All @@ -395,7 +397,7 @@ public String describeKey() {
StringBuilder message = new StringBuilder();
message.append(getProgressMessage());
message.append('\n');
for (Map.Entry<String, String> entry : getEnvironment().entrySet()) {
for (Map.Entry<String, String> entry : env.getFixedEnv().entrySet()) {
message.append(" Environment variable: ");
message.append(ShellEscaper.escapeString(entry.getKey()));
message.append('=');
Expand Down Expand Up @@ -476,7 +478,7 @@ protected SpawnInfo getExtraActionSpawnInfo() throws CommandLineExpansionExcepti
}

@Override
public ImmutableMap<String, String> getEnvironment() {
public final ImmutableMap<String, String> getEnvironment() {
// TODO(ulfjack): AbstractAction should declare getEnvironment with a return value of type
// ActionEnvironment to avoid developers misunderstanding the purpose of this method. That
// requires first updating all subclasses and callers to actually handle environments correctly,
Expand Down Expand Up @@ -532,17 +534,8 @@ private ActionSpawn(
inputs.addAll(additionalInputs);
this.inputs = inputs.build();
this.filesetMappings = filesetMappings;
LinkedHashMap<String, String> env = new LinkedHashMap<>(SpawnAction.this.getEnvironment());
if (clientEnv != null) {
for (String var : SpawnAction.this.getClientEnvironmentVariables()) {
String value = clientEnv.get(var);
if (value == null) {
env.remove(var);
} else {
env.put(var, value);
}
}
}
LinkedHashMap<String, String> env = new LinkedHashMap<>(SpawnAction.this.env.size());
SpawnAction.this.env.resolve(env, clientEnv);
effectiveEnvironment = ImmutableMap.copyOf(env);
}

Expand Down Expand Up @@ -575,6 +568,7 @@ public static class Builder {
private final List<RunfilesSupplier> toolRunfilesSuppliers = new ArrayList<>();
private ResourceSet resourceSet = AbstractAction.DEFAULT_RESOURCE_SET;
private ImmutableMap<String, String> environment = ImmutableMap.of();
private ImmutableSet<String> inheritedEnvironment = ImmutableSet.of();
private ImmutableMap<String, String> executionInfo = ImmutableMap.of();
private boolean isShellCommand = false;
private boolean useDefaultShellEnvironment = false;
Expand Down Expand Up @@ -673,7 +667,7 @@ public Action[] build(ActionOwner owner, AnalysisEnvironment analysisEnvironment
ActionEnvironment env =
useDefaultShellEnvironment
? configuration.getActionEnvironment()
: ActionEnvironment.create(this.environment);
: ActionEnvironment.create(environment, inheritedEnvironment);
Action spawnAction =
buildSpawnAction(owner, commandLines, configuration.getCommandLineLimits(), env);
actions[0] = spawnAction;
Expand All @@ -692,7 +686,7 @@ SpawnAction buildForActionTemplate(ActionOwner owner) {
owner,
result.build(),
CommandLineLimits.UNLIMITED,
ActionEnvironment.create(this.environment));
ActionEnvironment.create(environment, inheritedEnvironment));
}

private CommandLine buildCommandLinesAndParamFileActions(
Expand Down Expand Up @@ -954,6 +948,16 @@ public Builder setEnvironment(Map<String, String> environment) {
return this;
}

/**
* Sets the set of inherited environment variables. Do not use! This makes the builder ignore
* the 'default shell environment', which is computed from the --action_env command line option.
*/
public Builder setInheritedEnvironment(Iterable<String> inheritedEnvironment) {
this.inheritedEnvironment = ImmutableSet.copyOf(inheritedEnvironment);
this.useDefaultShellEnvironment = false;
return this;
}

/**
* Sets the map of execution info.
*/
Expand Down Expand Up @@ -995,7 +999,8 @@ public Builder setExecutionInfo(Map<String, String> info) {
*/
public Builder useDefaultShellEnvironment() {
this.environment = null;
this.useDefaultShellEnvironment = true;
this.inheritedEnvironment = null;
this.useDefaultShellEnvironment = true;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ protected void computeKey(ActionKeyContext actionKeyContext, Fingerprint fp) {
}
fp.addPath(imports.getExecPath());
}
fp.addStringMap(getEnvironment());
fp.addStringMap(env.getFixedEnv());
fp.addStrings(env.getInheritedEnv());
fp.addStringMap(getExecutionInfo());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.devtools.build.lib.exec.util.TestExecutorBuilder;
import com.google.devtools.build.lib.util.io.FileOutErr;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
Expand Down Expand Up @@ -157,7 +158,8 @@ private enum KeyAttributes {
MNEMONIC,
RUNFILES_SUPPLIER,
INPUT,
ENVIRONMENT
FIXED_ENVIRONMENT,
VARIABLE_ENVIRONMENT
}

@Test
Expand Down Expand Up @@ -204,10 +206,13 @@ public Action generate(ImmutableSet<KeyAttributes> attributesToFlip) {
}

Map<String, String> env = new HashMap<>();
if (attributesToFlip.contains(KeyAttributes.ENVIRONMENT)) {
if (attributesToFlip.contains(KeyAttributes.FIXED_ENVIRONMENT)) {
env.put("foo", "bar");
}
builder.setEnvironment(env);
if (attributesToFlip.contains(KeyAttributes.VARIABLE_ENVIRONMENT)) {
builder.setInheritedEnvironment(Arrays.asList("baz"));
}

Action[] actions =
builder.build(
Expand Down

0 comments on commit 27487c7

Please sign in to comment.