diff options
author | 2017-03-24 15:53:25 +0000 | |
---|---|---|
committer | 2017-03-27 11:35:10 +0000 | |
commit | 79d7ab9fa21b0726ae223b44f2d017f0f8d3b77c (patch) | |
tree | 948125e5ec9de4da018718a0454067886c2754c0 /src/main/java/com/google | |
parent | 8a638d58259b4251c52cd9561588573911db0c1f (diff) |
sandbox: Refactor sandbox strategies to use a common exec method.
--
PiperOrigin-RevId: 151130566
MOS_MIGRATED_REVID=151130566
Diffstat (limited to 'src/main/java/com/google')
3 files changed, 44 insertions, 69 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java index d17f5760f9..c06d1d78f8 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java @@ -20,16 +20,11 @@ import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import com.google.common.eventbus.EventBus; import com.google.devtools.build.lib.actions.ActionExecutionContext; -import com.google.devtools.build.lib.actions.ActionExecutionMetadata; -import com.google.devtools.build.lib.actions.ActionStatusMessage; import com.google.devtools.build.lib.actions.EnvironmentalExecException; import com.google.devtools.build.lib.actions.ExecException; import com.google.devtools.build.lib.actions.ExecutionStrategy; import com.google.devtools.build.lib.actions.Executor; -import com.google.devtools.build.lib.actions.ResourceManager; -import com.google.devtools.build.lib.actions.ResourceManager.ResourceHandle; import com.google.devtools.build.lib.actions.Spawn; import com.google.devtools.build.lib.actions.SpawnActionContext; import com.google.devtools.build.lib.actions.UserExecException; @@ -150,35 +145,7 @@ public class DarwinSandboxedStrategy extends SandboxStrategy { } @Override - public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext) - throws ExecException, InterruptedException { - exec(spawn, actionExecutionContext, null); - } - - @Override - public void exec( - Spawn spawn, - ActionExecutionContext actionExecutionContext, - AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) - throws ExecException, InterruptedException { - Executor executor = actionExecutionContext.getExecutor(); - // Certain actions can't run remotely or in a sandbox - pass them on to the standalone strategy. - if (!spawn.isRemotable() || spawn.hasNoSandbox()) { - SandboxHelpers.fallbackToNonSandboxedExecution(spawn, actionExecutionContext, executor); - return; - } - - EventBus eventBus = actionExecutionContext.getExecutor().getEventBus(); - ActionExecutionMetadata owner = spawn.getResourceOwner(); - eventBus.post(ActionStatusMessage.schedulingStrategy(owner)); - try (ResourceHandle handle = - ResourceManager.instance().acquireResources(owner, spawn.getLocalResources())) { - SandboxHelpers.postActionStatusMessage(eventBus, spawn); - actuallyExec(spawn, actionExecutionContext, writeOutputFiles); - } - } - - private void actuallyExec( + protected void actuallyExec( Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java b/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java index b2fdfacf6a..8a55fac308 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java @@ -17,15 +17,10 @@ package com.google.devtools.build.lib.sandbox; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; -import com.google.common.eventbus.EventBus; import com.google.devtools.build.lib.actions.ActionExecutionContext; -import com.google.devtools.build.lib.actions.ActionExecutionMetadata; -import com.google.devtools.build.lib.actions.ActionStatusMessage; import com.google.devtools.build.lib.actions.ExecException; import com.google.devtools.build.lib.actions.ExecutionStrategy; import com.google.devtools.build.lib.actions.Executor; -import com.google.devtools.build.lib.actions.ResourceManager; -import com.google.devtools.build.lib.actions.ResourceManager.ResourceHandle; import com.google.devtools.build.lib.actions.Spawn; import com.google.devtools.build.lib.actions.SpawnActionContext; import com.google.devtools.build.lib.actions.UserExecException; @@ -90,37 +85,8 @@ public class LinuxSandboxedStrategy extends SandboxStrategy { this.fullySupported = fullySupported; } - /** Executes the given {@code spawn}. */ @Override - public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext) - throws ExecException, InterruptedException { - exec(spawn, actionExecutionContext, null); - } - - @Override - public void exec( - Spawn spawn, - ActionExecutionContext actionExecutionContext, - AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) - throws ExecException, InterruptedException { - Executor executor = actionExecutionContext.getExecutor(); - // Certain actions can't run remotely or in a sandbox - pass them on to the standalone strategy. - if (!spawn.isRemotable() || spawn.hasNoSandbox()) { - SandboxHelpers.fallbackToNonSandboxedExecution(spawn, actionExecutionContext, executor); - return; - } - - EventBus eventBus = actionExecutionContext.getExecutor().getEventBus(); - ActionExecutionMetadata owner = spawn.getResourceOwner(); - eventBus.post(ActionStatusMessage.schedulingStrategy(owner)); - try (ResourceHandle handle = - ResourceManager.instance().acquireResources(owner, spawn.getLocalResources())) { - SandboxHelpers.postActionStatusMessage(eventBus, spawn); - actuallyExec(spawn, actionExecutionContext, writeOutputFiles); - } - } - - public void actuallyExec( + protected void actuallyExec( Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java index 778b8e5a00..40f9b36ac2 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java @@ -16,11 +16,17 @@ package com.google.devtools.build.lib.sandbox; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; +import com.google.common.eventbus.EventBus; import com.google.devtools.build.lib.actions.ActionExecutionContext; +import com.google.devtools.build.lib.actions.ActionExecutionMetadata; import com.google.devtools.build.lib.actions.ActionInput; +import com.google.devtools.build.lib.actions.ActionStatusMessage; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.EnvironmentalExecException; import com.google.devtools.build.lib.actions.ExecException; +import com.google.devtools.build.lib.actions.Executor; +import com.google.devtools.build.lib.actions.ResourceManager; +import com.google.devtools.build.lib.actions.ResourceManager.ResourceHandle; import com.google.devtools.build.lib.actions.SandboxedSpawnActionContext; import com.google.devtools.build.lib.actions.Spawn; import com.google.devtools.build.lib.actions.SpawnActionContext; @@ -64,6 +70,42 @@ abstract class SandboxStrategy implements SandboxedSpawnActionContext { this.spawnInputExpander = new SpawnInputExpander(/*strict=*/false); } + /** Executes the given {@code spawn}. */ + @Override + public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext) + throws ExecException, InterruptedException { + exec(spawn, actionExecutionContext, null); + } + + @Override + public void exec( + Spawn spawn, + ActionExecutionContext actionExecutionContext, + AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) + throws ExecException, InterruptedException { + Executor executor = actionExecutionContext.getExecutor(); + // Certain actions can't run remotely or in a sandbox - pass them on to the standalone strategy. + if (!spawn.isRemotable() || spawn.hasNoSandbox()) { + SandboxHelpers.fallbackToNonSandboxedExecution(spawn, actionExecutionContext, executor); + return; + } + + EventBus eventBus = actionExecutionContext.getExecutor().getEventBus(); + ActionExecutionMetadata owner = spawn.getResourceOwner(); + eventBus.post(ActionStatusMessage.schedulingStrategy(owner)); + try (ResourceHandle ignored = + ResourceManager.instance().acquireResources(owner, spawn.getLocalResources())) { + SandboxHelpers.postActionStatusMessage(eventBus, spawn); + actuallyExec(spawn, actionExecutionContext, writeOutputFiles); + } + } + + protected abstract void actuallyExec( + Spawn spawn, + ActionExecutionContext actionExecutionContext, + AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) + throws ExecException, InterruptedException; + protected void runSpawn( Spawn spawn, ActionExecutionContext actionExecutionContext, |