Skip to content

Commit

Permalink
Make SpawnAction implement Action.execute() instead of beginExecution().
Browse files Browse the repository at this point in the history
One step towards removing the continuation-based action execution machinery.

SpawnStrategyResolver will still keep its beginExecution() method since it is
used by a number of other Action classes. Once those are gone, we can clean
that up, too.

RELNOTES: None.
PiperOrigin-RevId: 490218924
Change-Id: I6ed1abd2a3ac84bb11aca5827ab1b6a276b0e33b
  • Loading branch information
lberki authored and copybara-github committed Nov 22, 2022
1 parent 3f127d7 commit 0eb4937
Showing 1 changed file with 18 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Interner;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.devtools.build.lib.actions.AbstractAction;
import com.google.devtools.build.lib.actions.ActionContinuationOrResult;
import com.google.devtools.build.lib.actions.ActionEnvironment;
import com.google.devtools.build.lib.actions.ActionExecutionContext;
import com.google.devtools.build.lib.actions.ActionExecutionException;
Expand Down Expand Up @@ -334,23 +332,30 @@ protected void afterExecute(
throws ExecException {}

@Override
public final ActionContinuationOrResult beginExecution(
ActionExecutionContext actionExecutionContext)
public final ActionResult execute(ActionExecutionContext actionExecutionContext)
throws ActionExecutionException, InterruptedException {
Spawn spawn;
try {
beforeExecute(actionExecutionContext);
spawn = getSpawn(actionExecutionContext);
} catch (ExecException e) {
throw ActionExecutionException.fromExecException(e, this);
Spawn spawn = getSpawn(actionExecutionContext);
SpawnContinuation continuation =
actionExecutionContext
.getContext(SpawnStrategyResolver.class)
.beginExecution(spawn, actionExecutionContext);
while (!continuation.isDone()) {
continuation = continuation.execute();
}

ImmutableList<SpawnResult> result = continuation.get();
if (resultConsumer != null) {
resultConsumer.accept(Pair.of(actionExecutionContext, result));
}
afterExecute(actionExecutionContext, result);
return ActionResult.create(result);
} catch (CommandLineExpansionException e) {
throw createCommandLineException(e);
} catch (ExecException e) {
throw ActionExecutionException.fromExecException(e, this);
}
SpawnContinuation spawnContinuation =
actionExecutionContext
.getContext(SpawnStrategyResolver.class)
.beginExecution(spawn, actionExecutionContext);
return new SpawnActionContinuation(actionExecutionContext, spawnContinuation);
}

private ActionExecutionException createCommandLineException(CommandLineExpansionException e) {
Expand Down Expand Up @@ -1440,41 +1445,6 @@ public Builder addResultConsumer(
}
}

private final class SpawnActionContinuation extends ActionContinuationOrResult {
private final ActionExecutionContext actionExecutionContext;
private final SpawnContinuation spawnContinuation;

public SpawnActionContinuation(
ActionExecutionContext actionExecutionContext, SpawnContinuation spawnContinuation) {
this.actionExecutionContext = actionExecutionContext;
this.spawnContinuation = spawnContinuation;
}

@Override
public ListenableFuture<?> getFuture() {
return spawnContinuation.getFuture();
}

@Override
public ActionContinuationOrResult execute()
throws ActionExecutionException, InterruptedException {
try {
SpawnContinuation nextContinuation = spawnContinuation.execute();
if (nextContinuation.isDone()) {
if (resultConsumer != null) {
resultConsumer.accept(Pair.of(actionExecutionContext, nextContinuation.get()));
}
List<SpawnResult> spawnResults = nextContinuation.get();
afterExecute(actionExecutionContext, spawnResults);
return ActionContinuationOrResult.of(ActionResult.create(nextContinuation.get()));
}
return new SpawnActionContinuation(actionExecutionContext, nextContinuation);
} catch (ExecException e) {
throw ActionExecutionException.fromExecException(e, SpawnAction.this);
}
}
}

/** A {@link PathFragment} that is expanded with {@link PathFragment#getCallablePathString()}. */
private static final class CallablePathFragment implements CommandLines.PathStrippable {

Expand Down

0 comments on commit 0eb4937

Please sign in to comment.