Skip to content

Commit

Permalink
Simplify ActionContextProvider: remove init().
Browse files Browse the repository at this point in the history
This method is only implemented in one action context provider and the provided file cache is always strictly derived from the command environment. Move the file cache creation to the command environment instead which is accessible everywhere the cache is needed.

RELNOTES: None.
PiperOrigin-RevId: 288554250
  • Loading branch information
aragos authored and copybara-github committed Jan 7, 2020
1 parent 8e9c11d commit e643340
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.devtools.build.lib.actions.Executor;
import com.google.devtools.build.lib.actions.ExecutorInitException;
import com.google.devtools.build.lib.actions.LocalHostCapacity;
import com.google.devtools.build.lib.actions.MetadataProvider;
import com.google.devtools.build.lib.actions.PackageRoots;
import com.google.devtools.build.lib.actions.ResourceManager;
import com.google.devtools.build.lib.actions.ResourceSet;
Expand Down Expand Up @@ -63,7 +62,6 @@
import com.google.devtools.build.lib.exec.CheckUpToDateFilter;
import com.google.devtools.build.lib.exec.ExecutionOptions;
import com.google.devtools.build.lib.exec.ExecutorBuilder;
import com.google.devtools.build.lib.exec.SingleBuildFileCache;
import com.google.devtools.build.lib.exec.SpawnActionContextMaps;
import com.google.devtools.build.lib.exec.SymlinkTreeStrategy;
import com.google.devtools.build.lib.profiler.AutoProfiler;
Expand Down Expand Up @@ -121,7 +119,6 @@ public class ExecutionTool {
private final BlazeRuntime runtime;
private final BuildRequest request;
private BlazeExecutor executor;
private final MetadataProvider fileCache;
private final ActionInputPrefetcher prefetcher;
private final ImmutableList<ActionContextProvider> actionContextProviders;
private SpawnActionContextMaps spawnActionContextMaps;
Expand Down Expand Up @@ -151,18 +148,9 @@ public class ExecutionTool {
builder
.addStrategyByContext(WorkspaceStatusAction.Context.class, "")
.addStrategyByContext(SymlinkTreeActionContext.class, "");

// Unfortunately, the exec root cache is not shared with caches in the remote execution client.
this.fileCache =
new SingleBuildFileCache(env.getExecRoot().getPathString(), runtime.getFileSystem());

this.prefetcher = builder.getActionInputPrefetcher();

this.actionContextProviders = builder.getActionContextProviders();
for (ActionContextProvider provider : actionContextProviders) {
try (SilentCloseable closeable = Profiler.instance().profile(provider + ".init")) {
provider.init(fileCache);
}
}

// There are many different SpawnActions, and we want to control the action context they use
// independently from each other, for example, to run genrules locally and Java compile action
Expand Down Expand Up @@ -689,7 +677,7 @@ private Builder createBuilder(
request.getPackageCacheOptions().checkOutputFiles
? modifiedOutputFiles
: ModifiedFileSet.NOTHING_MODIFIED,
fileCache,
env.getFileCache(),
prefetcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.google.devtools.build.lib.actions.ActionContext;
import com.google.devtools.build.lib.actions.ActionGraph;
import com.google.devtools.build.lib.actions.ExecutorInitException;
import com.google.devtools.build.lib.actions.MetadataProvider;
import com.google.devtools.build.lib.analysis.ArtifactsToOwnerLabels;

/**
Expand All @@ -34,15 +33,6 @@ public abstract class ActionContextProvider {
*/
public abstract Iterable<? extends ActionContext> getActionContexts();

/**
* Two-phase initialization. The input file cache usually comes from a different module than the
* {@link ActionContextProvider} instances that require it, so this method is called after {@link
* com.google.devtools.build.lib.runtime.BlazeModule#executorInit}.
*
* @param actionInputFileCache the input file cache
*/
public void init(MetadataProvider actionInputFileCache) {}

/**
* Called when the executor is constructed. The parameter contains all the contexts that were
* selected for this execution phase.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.eventbus.EventBus;
import com.google.devtools.build.lib.actions.MetadataProvider;
import com.google.devtools.build.lib.actions.ResourceManager;
import com.google.devtools.build.lib.actions.cache.ActionCache;
import com.google.devtools.build.lib.analysis.AnalysisOptions;
Expand All @@ -25,6 +26,7 @@
import com.google.devtools.build.lib.analysis.config.CoreOptions;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.exec.SingleBuildFileCache;
import com.google.devtools.build.lib.packages.StarlarkSemanticsOptions;
import com.google.devtools.build.lib.pkgcache.PackageCacheOptions;
import com.google.devtools.build.lib.pkgcache.PackageManager;
Expand Down Expand Up @@ -58,6 +60,7 @@
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;

/**
* Encapsulates the state needed for a single command. The environment is dropped when the current
Expand Down Expand Up @@ -93,9 +96,13 @@ public final class CommandEnvironment {
private Path workingDirectory;
private String workspaceName;
private boolean haveSetupPackageCache = false;

private AtomicReference<AbruptExitException> pendingException = new AtomicReference<>();

private final Object fileCacheLock = new Object();

@GuardedBy("fileCacheLock")
private MetadataProvider fileCache;

private class BlazeModuleEnvironment implements BlazeModule.ModuleEnvironment {
@Override
public Path getFileFromWorkspace(Label label) {
Expand Down Expand Up @@ -747,4 +754,15 @@ public Map<String, String> getActionClientEnv() {
public Map<String, String> getRepoEnv() {
return Collections.unmodifiableMap(repoEnv);
}

/** Returns the file cache to use during this build. */
public MetadataProvider getFileCache() {
synchronized (fileCacheLock) {
if (fileCache == null) {
fileCache =
new SingleBuildFileCache(getExecRoot().getPathString(), getRuntime().getFileSystem());
}
return fileCache;
}
}
}

0 comments on commit e643340

Please sign in to comment.