Skip to content

Commit 1962a59

Browse files
Googlerlarsrc-google
Googler
authored andcommitted
Fix the case where if all strategies for one branch of dynamic execution fail to accept (that is, refuse to even take) the action given, the whole action fails. Instead of seeing whether the other branch can run and the action that that it succeeded.
NOTE: This is _not_ about the local/remote execution branch trying to run it and failing. Obviously that should fail the whole dynamic execution. This is about none of the strategies of one branch even stating that they can run it (based on what they return from `canExec`) If the local execution branch requires a specific architecture/OS for some action, and users' bazel is being run a machine that isn't that combination (assuming the local strategy given is already setup to return `false` in `canExec` in this case). Previously the whole execution of the dynamic execution would fail without even trying to see if remote execution succeeded. Now it will gracefully fallback to just waiting on remote strategy. To conditionally use workers for supported actions but still simultaneously kick off a dynamic run, the options`--stragegy=TaskMnemonic=dynamic --dynamic_local_strategy=TaskMnemonic=worker --dynamic_remote_strategy=TaskMnemonic=remote` can be given. Then any action with with the mnemonic `TaskMnemonic` that can't support `worker` execution (xor `remote` execution) will wait for the other execution branch to complete. If neither set of strategies can run the action, then the task fails. Previously, this would have failed if, for example, the `worker` strategy cannot handle the action, even if `remote` could have. This at first glance seems silly as if you know TaskMnemonic doesn't have a worker enabled implementation, why specify `worker` as the local strategy? But keep in mind any action can declare most any mnemonic. In this example, say that first rule doing a TaskMnemonic is worker enabled, so you add the flags. But then someone makes a bazel/starlark rule with the same mnemonic but different implementation (for example, a "mimic" rule), and that new one doesn't support worker. Then this becomes a case of "partial" worker support for a mnemonic. RELNOTES: If all strategies of one branch (the local or remote execution branch) of the `dynamic` strategy fail to even accept (via the response they give from `canExec`) the action, `dynamic` will now try to see if the other branch can accept it. (Trying to run it and it failing will still cause a failure if it was the first result, this is about strategies claiming they can't even try the action) PiperOrigin-RevId: 374265582
1 parent 687fd52 commit 1962a59

File tree

5 files changed

+405
-48
lines changed

5 files changed

+405
-48
lines changed

CONTRIBUTORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,5 @@ Jonathan Dierksen <[email protected]>
108108
Tony Aiuto <[email protected]>
109109
Andy Scott <[email protected]>
110110
Jamie Snape <[email protected]>
111-
Irina Chernushina <[email protected]>
111+
Irina Chernushina <[email protected]>
112+
C. Sean Young <[email protected]>

src/main/java/com/google/devtools/build/lib/dynamic/DynamicSpawnStrategy.java

Lines changed: 153 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@
3535
import com.google.devtools.build.lib.actions.SpawnResult;
3636
import com.google.devtools.build.lib.actions.SpawnResult.Status;
3737
import com.google.devtools.build.lib.actions.SpawnStrategy;
38+
import com.google.devtools.build.lib.actions.UserExecException;
3839
import com.google.devtools.build.lib.events.Event;
3940
import com.google.devtools.build.lib.exec.ExecutionPolicy;
41+
import com.google.devtools.build.lib.server.FailureDetails;
4042
import com.google.devtools.build.lib.server.FailureDetails.DynamicExecution;
4143
import com.google.devtools.build.lib.server.FailureDetails.DynamicExecution.Code;
4244
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
4345
import com.google.devtools.build.lib.util.io.FileOutErr;
4446
import com.google.devtools.build.lib.vfs.Path;
4547
import java.io.IOException;
48+
import java.util.List;
4649
import java.util.Optional;
4750
import java.util.concurrent.Callable;
4851
import java.util.concurrent.CancellationException;
@@ -265,12 +268,17 @@ private static ImmutableList<SpawnResult> waitBranches(
265268
ImmutableList<SpawnResult> remoteResult = waitBranch(remoteBranch);
266269

267270
if (remoteResult != null && localResult != null) {
268-
throw new AssertionError("One branch did not cancel the other one");
269-
} else if (remoteResult != null) {
270-
return remoteResult;
271+
throw new AssertionError(
272+
String.format(
273+
"Neither branch of %s cancelled the other one.",
274+
spawn.getResourceOwner().getPrimaryOutput().prettyPrint()));
271275
} else if (localResult != null) {
272276
return localResult;
277+
} else if (remoteResult != null) {
278+
return remoteResult;
273279
} else {
280+
// TODO(b/173153395): Sometimes gets thrown for currently unknown reasons.
281+
// (sometimes happens in relation to the whole dynamic execution being cancelled)
274282
throw new AssertionError(
275283
"Neither branch completed. Local was "
276284
+ (localBranch.isCancelled() ? "" : "not ")
@@ -321,18 +329,142 @@ static void verifyAvailabilityInfo(DynamicExecutionOptions options, Spawn spawn)
321329
}
322330
}
323331

332+
private static boolean canExecLocalSpawn(
333+
Spawn spawn,
334+
ExecutionPolicy executionPolicy,
335+
ActionContext.ActionContextRegistry actionContextRegistry,
336+
DynamicStrategyRegistry dynamicStrategyRegistry) {
337+
if (!executionPolicy.canRunLocally()) {
338+
return false;
339+
}
340+
List<SandboxedSpawnStrategy> localStrategies =
341+
dynamicStrategyRegistry.getDynamicSpawnActionContexts(
342+
spawn, DynamicStrategyRegistry.DynamicMode.LOCAL);
343+
return localStrategies.stream()
344+
.anyMatch(
345+
s ->
346+
(s.canExec(spawn, actionContextRegistry)
347+
|| s.canExecWithLegacyFallback(spawn, actionContextRegistry)));
348+
}
349+
350+
private boolean canExecLocal(
351+
Spawn spawn,
352+
ExecutionPolicy mainSpawnExecutionPolicy,
353+
ActionContext.ActionContextRegistry actionContextRegistry,
354+
DynamicStrategyRegistry dynamicStrategyRegistry) {
355+
if (!canExecLocalSpawn(
356+
spawn, mainSpawnExecutionPolicy, actionContextRegistry, dynamicStrategyRegistry)) {
357+
return false;
358+
}
359+
// Present if there is a extra local spawn. Unset if not.
360+
Optional<Boolean> canLocalSpawn =
361+
getExtraSpawnForLocalExecution
362+
.apply(spawn)
363+
.map(
364+
extraSpawn ->
365+
canExecLocalSpawn(
366+
extraSpawn,
367+
getExecutionPolicy.apply(extraSpawn),
368+
actionContextRegistry,
369+
dynamicStrategyRegistry));
370+
return canLocalSpawn.orElse(true);
371+
}
372+
373+
private static boolean canExecRemote(
374+
Spawn spawn,
375+
ExecutionPolicy executionPolicy,
376+
ActionContext.ActionContextRegistry actionContextRegistry,
377+
DynamicStrategyRegistry dynamicStrategyRegistry) {
378+
if (!executionPolicy.canRunRemotely()) {
379+
return false;
380+
}
381+
List<SandboxedSpawnStrategy> remoteStrategies =
382+
dynamicStrategyRegistry.getDynamicSpawnActionContexts(
383+
spawn, DynamicStrategyRegistry.DynamicMode.REMOTE);
384+
return remoteStrategies.stream().anyMatch(s -> s.canExec(spawn, actionContextRegistry));
385+
}
386+
387+
@Override
388+
public boolean canExec(Spawn spawn, ActionContext.ActionContextRegistry actionContextRegistry) {
389+
ExecutionPolicy executionPolicy = getExecutionPolicy.apply(spawn);
390+
DynamicStrategyRegistry dynamicStrategyRegistry =
391+
actionContextRegistry.getContext(DynamicStrategyRegistry.class);
392+
393+
return canExecLocal(spawn, executionPolicy, actionContextRegistry, dynamicStrategyRegistry)
394+
|| canExecRemote(spawn, executionPolicy, actionContextRegistry, dynamicStrategyRegistry);
395+
}
396+
397+
/**
398+
* Returns an error string for being unable to execute locally and/or remotely the given execution
399+
* state.
400+
*
401+
* <p>Usage note, this method is only to be called after an impossible condition is already
402+
* detected by the caller, as all this does is give an error string to put in the exception.
403+
*
404+
* @param spawn The action that needs to be executed
405+
* @param localAllowedBySpawnExecutionPolicy whether the execution policy for this spawn allows
406+
* trying local execution.
407+
* @param remoteAllowedBySpawnExecutionPolicy whether the execution policy for this spawn allows
408+
* trying remote execution.
409+
*/
410+
private static String getNoCanExecFailureMessage(
411+
Spawn spawn,
412+
boolean localAllowedBySpawnExecutionPolicy,
413+
boolean remoteAllowedBySpawnExecutionPolicy) {
414+
// TODO(b/188387840): Can't use Spawn.toString() here because tests report FakeOwner instances
415+
// as the resource owner, and those cause toStrings to throw if no primary output.
416+
// TODO(b/188402092): Even if the above is fixed, we still don't want to use Spawn.toString()
417+
// until the mnemonic is included in the output unconditionally. Too useful for the error
418+
// message.
419+
if (!localAllowedBySpawnExecutionPolicy && !remoteAllowedBySpawnExecutionPolicy) {
420+
return "Neither local nor remote execution allowed for action " + spawn.getMnemonic();
421+
} else if (!remoteAllowedBySpawnExecutionPolicy) {
422+
return "No usable dynamic_local_strategy found (and remote execution disabled) for action "
423+
+ spawn.getMnemonic();
424+
} else if (!localAllowedBySpawnExecutionPolicy) {
425+
return "No usable dynamic_remote_strategy found (and local execution disabled) for action "
426+
+ spawn.getMnemonic();
427+
} else {
428+
return "No usable dynamic_local_strategy or dynamic_remote_strategy found for action "
429+
+ spawn.getMnemonic();
430+
}
431+
}
432+
324433
@Override
325434
public ImmutableList<SpawnResult> exec(
326435
final Spawn spawn, final ActionExecutionContext actionExecutionContext)
327436
throws ExecException, InterruptedException {
328437
DynamicSpawnStrategy.verifyAvailabilityInfo(options, spawn);
329438
ExecutionPolicy executionPolicy = getExecutionPolicy.apply(spawn);
330-
if (executionPolicy.canRunLocallyOnly()) {
331-
return runLocally(spawn, actionExecutionContext, null);
332-
}
333-
if (executionPolicy.canRunRemotelyOnly()) {
439+
440+
DynamicStrategyRegistry dynamicStrategyRegistry =
441+
actionExecutionContext.getContext(DynamicStrategyRegistry.class);
442+
boolean localCanExec =
443+
canExecLocal(spawn, executionPolicy, actionExecutionContext, dynamicStrategyRegistry);
444+
445+
boolean remoteCanExec =
446+
canExecRemote(spawn, executionPolicy, actionExecutionContext, dynamicStrategyRegistry);
447+
448+
if (!localCanExec && !remoteCanExec) {
449+
FailureDetail failure =
450+
FailureDetail.newBuilder()
451+
.setMessage(
452+
getNoCanExecFailureMessage(
453+
spawn, executionPolicy.canRunLocally(), executionPolicy.canRunRemotely()))
454+
.setDynamicExecution(
455+
DynamicExecution.newBuilder().setCode(Code.NO_USABLE_STRATEGY_FOUND).build())
456+
.setSpawn(
457+
FailureDetails.Spawn.newBuilder()
458+
.setCode(FailureDetails.Spawn.Code.NO_USABLE_STRATEGY_FOUND)
459+
.build())
460+
.build();
461+
throw new UserExecException(failure);
462+
} else if (!localCanExec && remoteCanExec) {
334463
return runRemotely(spawn, actionExecutionContext, null);
464+
} else if (localCanExec && !remoteCanExec) {
465+
return runLocally(spawn, actionExecutionContext, null);
335466
}
467+
// else both can exec. Fallthrough to below.
336468

337469
// Semaphores to track termination of each branch. These are necessary to wait for the branch to
338470
// finish its own cleanup (e.g. terminating subprocesses) once it has been cancelled.
@@ -448,28 +580,6 @@ public ImmutableList<SpawnResult> callImpl(ActionExecutionContext context)
448580
}
449581
}
450582

451-
@Override
452-
public boolean canExec(Spawn spawn, ActionContext.ActionContextRegistry actionContextRegistry) {
453-
DynamicStrategyRegistry dynamicStrategyRegistry =
454-
actionContextRegistry.getContext(DynamicStrategyRegistry.class);
455-
for (SandboxedSpawnStrategy strategy :
456-
dynamicStrategyRegistry.getDynamicSpawnActionContexts(
457-
spawn, DynamicStrategyRegistry.DynamicMode.LOCAL)) {
458-
if (strategy.canExec(spawn, actionContextRegistry)
459-
|| strategy.canExecWithLegacyFallback(spawn, actionContextRegistry)) {
460-
return true;
461-
}
462-
}
463-
for (SandboxedSpawnStrategy strategy :
464-
dynamicStrategyRegistry.getDynamicSpawnActionContexts(
465-
spawn, DynamicStrategyRegistry.DynamicMode.REMOTE)) {
466-
if (strategy.canExec(spawn, actionContextRegistry)) {
467-
return true;
468-
}
469-
}
470-
return false;
471-
}
472-
473583
@Override
474584
public void usedContext(ActionContext.ActionContextRegistry actionContextRegistry) {
475585
actionContextRegistry
@@ -486,6 +596,12 @@ private static FileOutErr getSuffixedFileOutErr(FileOutErr fileOutErr, String su
486596
outDir.getChild(outBaseName + suffix), errDir.getChild(errBaseName + suffix));
487597
}
488598

599+
/**
600+
* Try to run the given spawn locally.
601+
*
602+
* <p>Precondition: At least one {@code dynamic_local_strategy} returns {@code true} from its
603+
* {@link SpawnStrategy#canExec canExec} method for the given {@code spawn}.
604+
*/
489605
private ImmutableList<SpawnResult> runLocally(
490606
Spawn spawn,
491607
ActionExecutionContext actionExecutionContext,
@@ -529,12 +645,15 @@ private static ImmutableList<SpawnResult> runSpawnLocally(
529645
return strategy.exec(spawn, actionExecutionContext, stopConcurrentSpawns);
530646
}
531647
}
532-
throw new RuntimeException(
533-
String.format(
534-
"executorCreated not yet called or no default dynamic_local_strategy set for %s",
535-
spawn.getMnemonic()));
648+
throw new AssertionError("canExec passed but no usable local strategy for action " + spawn);
536649
}
537650

651+
/**
652+
* Try to run the given spawn locally.
653+
*
654+
* <p>Precondition: At least one {@code dynamic_remote_strategy} returns {@code true} from its
655+
* {@link SpawnStrategy#canExec canExec} method for the given {@code spawn}.
656+
*/
538657
private static ImmutableList<SpawnResult> runRemotely(
539658
Spawn spawn,
540659
ActionExecutionContext actionExecutionContext,
@@ -550,10 +669,7 @@ private static ImmutableList<SpawnResult> runRemotely(
550669
return strategy.exec(spawn, actionExecutionContext, stopConcurrentSpawns);
551670
}
552671
}
553-
throw new RuntimeException(
554-
String.format(
555-
"executorCreated not yet called or no default dynamic_remote_strategy set for %s",
556-
spawn.getMnemonic()));
672+
throw new AssertionError("canExec passed but no usable remote strategy for action " + spawn);
557673
}
558674

559675
/**

src/main/protobuf/failure_details.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ message DynamicExecution {
10361036
XCODE_RELATED_PREREQ_UNMET = 1 [(metadata) = { exit_code: 36 }];
10371037
ACTION_LOG_MOVE_FAILURE = 2 [(metadata) = { exit_code: 1 }];
10381038
RUN_FAILURE = 3 [(metadata) = { exit_code: 1 }];
1039+
NO_USABLE_STRATEGY_FOUND = 4 [(metadata) = { exit_code: 2 }];
10391040
}
10401041

10411042
Code code = 1;

src/test/java/com/google/devtools/build/lib/dynamic/DynamicSpawnStrategyTest.java

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,22 @@ private class MockSpawnStrategy implements SandboxedSpawnStrategy {
137137

138138
private final DoExec doExecAfterStop;
139139

140+
private final boolean canExec;
141+
140142
MockSpawnStrategy(String name) {
141143
this(name, DoExec.NOTHING, DoExec.NOTHING);
142144
}
143145

144146
MockSpawnStrategy(String name, DoExec doExecBeforeStop, DoExec doExecAfterStop) {
147+
this(name, doExecBeforeStop, doExecAfterStop, true);
148+
}
149+
150+
MockSpawnStrategy(
151+
String name, DoExec doExecBeforeStop, DoExec doExecAfterStop, boolean canExec) {
145152
this.name = name;
146153
this.doExecBeforeStop = doExecBeforeStop;
147154
this.doExecAfterStop = doExecAfterStop;
155+
this.canExec = canExec;
148156
}
149157

150158
/** Helper to record an execution failure from within {@link #doExecBeforeStop}. */
@@ -201,7 +209,7 @@ public ImmutableList<SpawnResult> exec(
201209

202210
@Override
203211
public boolean canExec(Spawn spawn, ActionContext.ActionContextRegistry actionContextRegistry) {
204-
return true;
212+
return canExec;
205213
}
206214

207215
@Nullable
@@ -561,6 +569,48 @@ public void actionSucceedsIfRemoteExecutionSucceedsEvenIfLocalFailsLater() throw
561569
assertThat(outErr.outAsLatin1()).doesNotContain("MockLocalSpawnStrategy");
562570
}
563571

572+
@Test
573+
public void actionSucceedsIfLocalExecutionSucceedsEvenIfRemoteRunsNothing() throws Exception {
574+
MockSpawnStrategy localStrategy = new MockSpawnStrategy("MockLocalSpawnStrategy");
575+
576+
MockSpawnStrategy remoteStrategy =
577+
new MockSpawnStrategy("MockRemoteSpawnStrategy", DoExec.NOTHING, DoExec.NOTHING, false);
578+
579+
StrategyAndContext strategyAndContext = createSpawnStrategy(localStrategy, remoteStrategy);
580+
581+
Spawn spawn = newDynamicSpawn();
582+
strategyAndContext.exec(spawn);
583+
584+
assertThat(localStrategy.getExecutedSpawn()).isEqualTo(spawn);
585+
assertThat(localStrategy.succeeded()).isTrue();
586+
assertThat(remoteStrategy.getExecutedSpawn()).isNull();
587+
assertThat(remoteStrategy.succeeded()).isFalse();
588+
589+
assertThat(outErr.outAsLatin1()).contains("output files written with MockLocalSpawnStrategy");
590+
assertThat(outErr.outAsLatin1()).doesNotContain("MockRemoteSpawnStrategy");
591+
}
592+
593+
@Test
594+
public void actionSucceedsIfRemoteExecutionSucceedsEvenIfLocalRunsNothing() throws Exception {
595+
MockSpawnStrategy localStrategy =
596+
new MockSpawnStrategy("MockLocalSpawnStrategy", DoExec.NOTHING, DoExec.NOTHING, false);
597+
598+
MockSpawnStrategy remoteStrategy = new MockSpawnStrategy("MockRemoteSpawnStrategy");
599+
600+
StrategyAndContext strategyAndContext = createSpawnStrategy(localStrategy, remoteStrategy);
601+
602+
Spawn spawn = newDynamicSpawn();
603+
strategyAndContext.exec(spawn);
604+
605+
assertThat(localStrategy.getExecutedSpawn()).isNull();
606+
assertThat(localStrategy.succeeded()).isFalse();
607+
assertThat(remoteStrategy.getExecutedSpawn()).isEqualTo(spawn);
608+
assertThat(remoteStrategy.succeeded()).isTrue();
609+
610+
assertThat(outErr.outAsLatin1()).contains("output files written with MockRemoteSpawnStrategy");
611+
assertThat(outErr.outAsLatin1()).doesNotContain("MockLocalSpawnStrategy");
612+
}
613+
564614
@Test
565615
public void actionFailsIfLocalFailsImmediatelyEvenIfRemoteSucceedsLater() throws Exception {
566616
CountDownLatch countDownLatch = new CountDownLatch(2);
@@ -671,6 +721,34 @@ public void actionFailsIfLocalAndRemoteFail() throws Exception {
671721
assertThat(remoteStrategy.succeeded()).isFalse();
672722
}
673723

724+
@Test
725+
public void actionFailsIfLocalAndRemoteRunNothing() throws Exception {
726+
MockSpawnStrategy localStrategy =
727+
new MockSpawnStrategy("MockLocalSpawnStrategy", DoExec.NOTHING, DoExec.NOTHING, false);
728+
729+
MockSpawnStrategy remoteStrategy =
730+
new MockSpawnStrategy("MockRemoteSpawnStrategy", DoExec.NOTHING, DoExec.NOTHING, false);
731+
732+
StrategyAndContext strategyAndContext = createSpawnStrategy(localStrategy, remoteStrategy);
733+
734+
Spawn spawn = newDynamicSpawn();
735+
ExecException e = assertThrows(UserExecException.class, () -> strategyAndContext.exec(spawn));
736+
737+
// Has "No usable", followed by both dynamic_local_strategy and dynamic_remote_strategy in,
738+
// followed by the action's mnemonic.
739+
String regexMatch =
740+
"[nN]o usable\\b.*\\bdynamic_local_strategy\\b.*\\bdynamic_remote_strategy\\b.*\\b"
741+
+ spawn.getMnemonic()
742+
+ "\\b";
743+
744+
assertThat(e).hasMessageThat().containsMatch(regexMatch);
745+
746+
assertThat(localStrategy.getExecutedSpawn()).isNull();
747+
assertThat(localStrategy.succeeded()).isFalse();
748+
assertThat(remoteStrategy.getExecutedSpawn()).isNull();
749+
assertThat(remoteStrategy.succeeded()).isFalse();
750+
}
751+
674752
@Test
675753
public void stopConcurrentSpawnsWaitForCompletion() throws Exception {
676754
if (legacyBehavior) {

0 commit comments

Comments
 (0)