feat(emr): run a cluster's bootstrap actions on its core and task nodes (#8502)

Bootstrap actions "run on cluster after Amazon EMR launches the
instance", on every node, and "If you add nodes to a running cluster,
bootstrap actions also run on those nodes in the same way". Floci ran
them once, as on the primary node, and every node ran from the image they
left, so an action's branch for the core or task nodes never ran.

Where a cluster's core and task nodes run in containers of their own,
each now runs the actions too, in a bootstrap container of its own beside
the primary node's, as that node: its node files say CORE or TASK, the
cluster's relay answers its instance metadata as that node, its output
goes under its own instance id, and the container is kept as the image
the node runs from, so it holds what its own actions installed. A node a
resize adds runs them before it starts, and one that leaves takes its
image with it, so one added again runs them again.

AWS terminates a node whose action fails and says only that "If too many
instances fail their bootstrap actions, then Amazon EMR terminates the
cluster. If just a few instances fail, Amazon EMR attempts to reallocate
the failed instances and continue". Floci runs a node's actions again
once, in a fresh container, and a second failure ends the cluster with
BOOTSTRAP_FAILURE and a message of Floci's after the primary node's, as
no source gives AWS's words or an error code for a core or task node.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 17:41:55 -04:00
1 parent 84c06d59e7
commit ec9c19ff62
12 files changed
+891 -120

No files matched your search

@@ -227,6 +227,46 @@ class EmrBootstrapRunnerTest {
verify(lifecycle, never()).commit(any(), any(), any(), any(), any());
}
@Test
void aCoreNodesBootstrapRunsInAContainerOfItsOwnAsThatNodeAndIsKeptAsItsImage() {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", "");
String repository = ContainerStorageHelper.dockerName(config, "emr-bootstrap");
String nodeImage = "sha256:" + "3".repeat(64);
when(docker.inspectImageCmd(repository + ":j-1-1-2").exec().getId()).thenReturn(nodeImage);
BootstrapRunner.Outcome outcome = runner.run(bootstrap(null, action("s3://bucket/bootstrap/install.sh"))
.onNode("1-2", "CORE", "s3://logs/j-1/node/i-00000000000000003/bootstrap-actions/"), () -> false);
// Apart from the primary node's, and from each other node's, with a session of its own.
assertEquals(BootstrapRunner.Outcome.succeeded(nodeImage), outcome);
verify(spec).withName(ContainerStorageHelper.dockerName(config, "emr-j-1-bootstrap-1-2"));
verify(roleCredentials).forRole("arn:aws:iam::" + ACCOUNT + ":role/emr-ec2", ACCOUNT, "j-1-1-2-bootstrap");
// Its files say which node it is.
assertEquals(List.of("env", "FLOCI_INSTANCE_ROLE=CORE", "bash", "-c", EmrServerlessSparkRunner.NODE_INFO),
commands.get(0));
verify(lifecycle).commit(eq(CONTAINER), eq(repository), eq("j-1-1-2"), eq("hadoop"), any());
assertTrue(uploaded.containsKey("logs/j-1/node/i-00000000000000003/bootstrap-actions/1/stdout.gz"),
uploaded.toString());
}
@Test
void stoppingAClustersBootstrapStopsEachOfItsNodesButNoOtherClusters() {
ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", "");
when(s3.getObject("bucket", "bootstrap/install.sh")).thenAnswer(read -> {
runner.stop("j-10");
runner.stop("j-1-1");
verify(lifecycle, never()).stopAndRemove(CONTAINER, null, 0);
runner.stop("j-1");
return content("#!/bin/bash\n");
});
BootstrapRunner.Outcome outcome = runner.run(bootstrap(null, action("s3://bucket/bootstrap/install.sh"))
.onNode("1-1", "CORE", null), () -> false);
assertEquals(BootstrapRunner.Outcome.halted(), outcome);
verify(lifecycle).stopAndRemove(CONTAINER, null, 0);
}
@Test
void thePrimaryNodesFilesAreWrittenBeforeTheFirstActionAndGoBeforeTheImageIsKept() {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", "");
@@ -255,6 +255,26 @@ class EmrClusterStorageTest {
verify(lifecycle, never()).removeIfExistsStrict(coreName);
}
@Test
void aNodeWithAnImageOfItsOwnRunsFromItAndStartsAgainWhereItRunsAnother() {
nodesStart(coreName, taskName);
runningNodes(node(coreName, "running", "sha256:core-node", CORE_HOST, true),
node(taskName, "running", IMAGE, TASK_HOST, false));
// Each from the image its own bootstrap left: the task node's is new, as a resize added it again.
ClusterStorage.Yarn ownImages = new ClusterStorage.Yarn(4096, 4).onNodes(List.of(
new ClusterStorage.Node("1-1", CORE_HOST, true, "sha256:core-node"),
new ClusterStorage.Node("2-1", TASK_HOST, false, "sha256:task-node")), 1, "s3://logs/j-1/containers/");
when(containerBuilder.newContainer("sha256:task-node")).thenReturn(spec);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> ownImages,
NOT_STOPPED);
verify(spec, never()).withName(coreName);
verify(spec).withName(taskName);
verify(containerBuilder).newContainer("sha256:task-node");
verify(containerBuilder, never()).newContainer("sha256:core-node");
}
@Test
void aNodeInAnotherImageOrStoppedStartsAgainOnItsData() {
nodesStart(coreName, taskName);
@@ -657,6 +677,30 @@ class EmrClusterStorageTest {
order.verify(lifecycle).removeVolume(coreName);
}
@Test
void aCoreNodeThatStopsWhileItLeavesStartsAgainFromTheImageItRan() {
AtomicLong clock = new AtomicLong();
EmrClusterStorage ticking = new EmrClusterStorage(lifecycle, containerBuilder, awsEnv, roleCredentials,
currentContainer, config, s3, Duration.ofSeconds(120),
() -> clock.getAndAdd(Duration.ofMinutes(30).toNanos()), Runnable::run, Duration.ZERO);
nodesStart(coreName, taskName);
ticking.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
// It ran from the image its own bootstrap left.
runningNodes(node(coreName, "running", "sha256:core-node", CORE_HOST, true),
node(taskName, "running", IMAGE, TASK_HOST, false));
when(containerBuilder.newContainer("sha256:core-node")).thenReturn(spec);
ContainerExecStubs.completeEveryExec(docker, hdfsName, 1L, "", "");
when(lifecycle.probeContainer(coreName)).thenReturn(ContainerLiveness.NOT_RUNNING, ContainerLiveness.RUNNING);
clearInvocations(spec, lifecycle, containerBuilder);
ClusterStorage.Yarn taskOnly = new ClusterStorage.Yarn(4096, 4).onNodes(List.of(TASK_NODE), 1, null);
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> ticking.resize("j-1", () -> taskOnly));
verify(containerBuilder).newContainer("sha256:core-node");
verify(spec).withName(coreName);
}
@Test
void aRoundThatFailsIsTriedAgainOnTheNext() {
nodesStart(coreName, taskName);
@@ -25,12 +25,17 @@ import org.mockito.InOrder;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import static io.floci.aws.services.emr.EmrServiceFixture.ACCOUNT;
import static io.floci.aws.services.emr.EmrServiceFixture.REGION;
@@ -1606,6 +1611,256 @@ class EmrServiceContainerStepsTest {
assertEquals(List.of(cluster.getId()), bootstrapping.clusterStorage.removed);
}
@Test
void eachCoreAndTaskNodeRunsTheBootstrapActionsAsItselfBesideThePrimaryNodeAndRunsFromWhatTheyLeft() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
bootstrap.outcomes = ran -> BootstrapRunner.Outcome.succeeded("sha256:" + ran.name());
EmrCluster requested = bootstrapped(cluster("node-bootstraps", true, sparkStep("report", "CONTINUE")));
requested.setLogUri("s3://logs/emr/");
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 2), group("TASK", 1))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
String id = cluster.getId();
bootstrapping.runStepThread();
// The primary node's, and each node's as that node, in the same image, its output under its own instance.
Map<String, BootstrapRunner.Bootstrap> byNode = new HashMap<>();
bootstrap.bootstraps.forEach(ran -> byNode.put(String.valueOf(ran.node()), ran));
assertEquals(Set.of("null", "1-1", "1-2", "2-1"), byNode.keySet());
assertEquals("MASTER", byNode.get("null").instanceRole());
assertEquals("CORE", byNode.get("1-1").instanceRole());
assertEquals("CORE", byNode.get("1-2").instanceRole());
assertEquals("TASK", byNode.get("2-1").instanceRole());
assertEquals("s3://logs/emr/" + id + "/node/i-00000000000000001/bootstrap-actions/",
byNode.get("null").logFolder());
assertEquals("s3://logs/emr/" + id + "/node/i-00000000000000004/bootstrap-actions/",
byNode.get("2-1").logFolder());
assertEquals(byNode.get("null").imageTemplate(), byNode.get("2-1").imageTemplate());
assertEquals(byNode.get("null").actions(), byNode.get("1-2").actions());
EmrCluster ready = bootstrapping.service.describeCluster(id);
assertEquals("sha256:" + id, ready.getBootstrapImage());
assertEquals(Map.of("1-1", "sha256:" + id + "-1-1", "1-2", "sha256:" + id + "-1-2", "2-1",
"sha256:" + id + "-2-1"), ready.getNodeBootstrapImages());
bootstrapping.runStepThread();
// Each node runs from what its own actions left, the steps from the primary node's.
assertEquals(List.of("sha256:" + id + "-1-1", "sha256:" + id + "-1-2", "sha256:" + id + "-2-1"),
bootstrapping.clusterStorage.yarns.get(0).nodes().stream().map(ClusterStorage.Node::image).toList());
assertEquals("sha256:" + id, runner.jobs.get(0).imageTemplate());
}
@Test
void aClusterWhoseNodesRunInNoContainersOfTheirOwnRunsItsBootstrapOnThePrimaryNodeAlone() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
EmrCluster requested = bootstrapped(cluster("primary-bootstrap", true));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 2))));
bootstrapping.service.runJobFlow(requested, REGION);
bootstrapping.runStepThread();
assertEquals(1, bootstrap.bootstraps.size());
assertNull(bootstrap.bootstraps.get(0).node());
}
@Test
void aNodeWhoseActionsFailRunsThemAgainInAFreshContainerAndTheClusterCarriesOn() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
AtomicInteger attempts = new AtomicInteger();
bootstrap.outcomes = ran -> "1-1".equals(ran.node()) && attempts.getAndIncrement() == 0
? BootstrapRunner.Outcome.actionFailed(2, 1)
: BootstrapRunner.Outcome.succeeded("sha256:" + ran.name());
EmrCluster requested = bootstrapped(cluster("node-retry", true));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
bootstrapping.runStepThread();
// As EMR "attempts to reallocate the failed instances and continue".
assertEquals(2, bootstrap.bootstraps.stream().filter(ran -> "1-1".equals(ran.node())).count());
EmrCluster ready = bootstrapping.service.describeCluster(cluster.getId());
assertEquals("WAITING", ready.getState());
assertEquals(Map.of("1-1", "sha256:" + cluster.getId() + "-1-1"), ready.getNodeBootstrapImages());
}
@Test
void aNodeWhoseActionsFailTwiceEndsTheClusterInFlocisWordsAndItsImagesGo() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
bootstrap.outcomes = ran -> "2-1".equals(ran.node()) ? BootstrapRunner.Outcome.actionFailed(1, 3)
: BootstrapRunner.Outcome.succeeded("sha256:" + ran.name());
EmrCluster requested = bootstrapped(cluster("node-failure", true, sparkStep("report", "CONTINUE")));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1), group("TASK", 1))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
String id = cluster.getId();
bootstrapping.runStepThread();
assertEquals(2, bootstrap.bootstraps.stream().filter(ran -> "2-1".equals(ran.node())).count());
EmrCluster ended = bootstrapping.service.describeCluster(id);
assertEquals("TERMINATED_WITH_ERRORS", ended.getState());
assertEquals("BOOTSTRAP_FAILURE", ended.getStateChangeReasonCode());
assertEquals("On the task instance (i-00000000000000003), bootstrap action 1 returned a non-zero return code",
ended.getStateChangeMessage());
// No source gives an error code for a node that is not the primary.
assertEquals(List.of(), ended.getErrorDetails());
assertEquals("CANCELLED", bootstrapping.service.describeStep(id, cluster.getSteps().get(0).getId()).getState());
while (bootstrapping.stepThreadsWaiting() > 0) {
bootstrapping.runStepThread();
}
assertEquals(Set.of("sha256:" + id, "sha256:" + id + "-1-1"), Set.copyOf(bootstrap.removed));
}
@Test
void aNodeAResizeAddsRunsTheBootstrapActionsBeforeItStartsAndOneThatLeavesKeepsItsImageUntilTheClusterEnds() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
bootstrap.outcomes = ran -> BootstrapRunner.Outcome.succeeded("sha256:" + ran.name());
EmrCluster requested = bootstrapped(cluster("node-resize", true, sparkStep("report", "CONTINUE")));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1), group("TASK", 1))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
String id = cluster.getId();
bootstrapping.runStepThread();
bootstrapping.runStepThread();
String task = cluster.getInstanceGroups().get(2).getId();
bootstrapping.service.modifyInstanceGroups(id, REGION,
List.of(new EmrService.InstanceGroupModification(task, 2, null)));
bootstrapping.runStepThread();
// "If you add nodes to a running cluster, bootstrap actions also run on those nodes in the same way."
BootstrapRunner.Bootstrap added = bootstrap.bootstraps.get(bootstrap.bootstraps.size() - 1);
assertEquals("2-2", added.node());
assertEquals("TASK", added.instanceRole());
assertEquals("sha256:" + id + "-2-2", bootstrapping.clusterStorage.resizes.get(0).nodes().get(2).image());
bootstrapping.service.modifyInstanceGroups(id, REGION,
List.of(new EmrService.InstanceGroupModification(task, 1, null)));
bootstrapping.runStepThread();
// Added again, it would run them again, as a new instance does; being decommissioned, it may still run from
// its image, which goes as the cluster ends.
EmrCluster shrunk = bootstrapping.service.describeCluster(id);
assertFalse(shrunk.getNodeBootstrapImages().containsKey("2-2"));
assertEquals(List.of("sha256:" + id + "-2-2"), shrunk.getRetiredBootstrapImages());
assertFalse(bootstrap.removed.contains("sha256:" + id + "-2-2"));
bootstrapping.service.terminateJobFlows(List.of(id));
while (bootstrapping.stepThreadsWaiting() > 0) {
bootstrapping.runStepThread();
}
assertTrue(bootstrap.removed.contains("sha256:" + id + "-2-2"));
assertEquals(List.of(), bootstrapping.service.describeCluster(id).getRetiredBootstrapImages());
}
@Test
void aNodeAResizeTookAwayWhileItsBootstrapRanLeavesNoImageForOneAddedInItsPlace() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
EmrCluster requested = bootstrapped(cluster("node-gone", true, sparkStep("report", "CONTINUE")));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1), group("TASK", 1))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
String id = cluster.getId();
String task = cluster.getInstanceGroups().get(2).getId();
AtomicBoolean shrunk = new AtomicBoolean();
bootstrap.outcomes = ran -> {
if ("2-2".equals(ran.node()) && shrunk.compareAndSet(false, true)) {
bootstrapping.service.modifyInstanceGroups(id, REGION,
List.of(new EmrService.InstanceGroupModification(task, 1, null)));
}
return BootstrapRunner.Outcome.succeeded("sha256:" + ran.name());
};
bootstrapping.runStepThread();
bootstrapping.runStepThread();
bootstrapping.service.modifyInstanceGroups(id, REGION,
List.of(new EmrService.InstanceGroupModification(task, 2, null)));
bootstrapping.runStepThread();
EmrCluster now = bootstrapping.service.describeCluster(id);
assertFalse(now.getNodeBootstrapImages().containsKey("2-2"));
assertEquals(List.of("sha256:" + id + "-2-2"), now.getRetiredBootstrapImages());
}
@Test
void aBootstrapThatCannotRunForTheNodesAResizeAddsEndsTheClusterAsAtItsStart() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
bootstrapping.clusterStorage.metadata = true;
bootstrap.outcomes = ran -> BootstrapRunner.Outcome.succeeded("sha256:" + ran.name());
EmrCluster requested = bootstrapped(cluster("node-setup", true, sparkStep("report", "CONTINUE")));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1), group("TASK", 1))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
String id = cluster.getId();
bootstrapping.runStepThread();
bootstrapping.runStepThread();
bootstrapping.clusterStorage.metadataFailure = new IllegalStateException("The relay did not listen");
bootstrapping.service.modifyInstanceGroups(id, REGION,
List.of(new EmrService.InstanceGroupModification(cluster.getInstanceGroups().get(2).getId(), 2, null)));
bootstrapping.runStepThread();
EmrCluster ended = bootstrapping.service.describeCluster(id);
assertEquals("TERMINATED_WITH_ERRORS", ended.getState());
assertEquals("INTERNAL_ERROR", ended.getStateChangeReasonCode());
assertEquals("Floci could not run the bootstrap actions: The relay did not listen",
ended.getStateChangeMessage());
// Its nodes do not change.
assertEquals(List.of(), bootstrapping.clusterStorage.resizes);
}
@Test
void aPrimaryNodeWhoseActionsFailStopsItsNodesBootstrapsAndEndsTheClusterWithItsFailure() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
// A node's bootstrap ends stopped, as the stop removes its container.
bootstrap.outcomes = ran -> ran.node() == null ? BootstrapRunner.Outcome.actionFailed(1, 2)
: BootstrapRunner.Outcome.halted();
EmrCluster requested = bootstrapped(cluster("primary-failure", true));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 2))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
bootstrapping.runStepThread();
assertEquals(List.of(cluster.getId()), bootstrap.stopped);
EmrCluster ended = bootstrapping.service.describeCluster(cluster.getId());
assertEquals("TERMINATED_WITH_ERRORS", ended.getState());
assertEquals("On the master instance (i-00000000000000001), bootstrap action 1 returned a non-zero return code",
ended.getStateChangeMessage());
}
@Test
void aClusterThatEndsWhileANodeAResizeAddedRunsItsBootstrapStopsIt() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
bootstrapping.clusterStorage.ownNetwork = true;
EmrCluster requested = bootstrapped(cluster("ends-resizing", true, sparkStep("report", "CONTINUE")));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1), group("TASK", 1))));
EmrCluster cluster = bootstrapping.service.runJobFlow(requested, REGION);
String id = cluster.getId();
AtomicBoolean ended = new AtomicBoolean();
bootstrap.outcomes = ran -> {
if ("2-2".equals(ran.node()) && ended.compareAndSet(false, true)) {
bootstrapping.service.terminateJobFlows(List.of(id));
// As the stop removes its container.
return BootstrapRunner.Outcome.halted();
}
return BootstrapRunner.Outcome.succeeded("sha256:" + ran.name());
};
bootstrapping.runStepThread();
bootstrapping.runStepThread();
bootstrapping.service.modifyInstanceGroups(id, REGION,
List.of(new EmrService.InstanceGroupModification(cluster.getInstanceGroups().get(2).getId(), 2, null)));
while (bootstrapping.stepThreadsWaiting() > 0) {
bootstrapping.runStepThread();
}
assertEquals(List.of(id), bootstrap.stopped);
assertEquals("TERMINATED", bootstrapping.service.describeCluster(id).getState());
assertEquals(List.of(), bootstrapping.clusterStorage.resizes);
}
@Test
void aClusterWithHiveThatEndsBeforeItsBootstrapOrStepRunsMakesNoImage() {
EmrServiceFixture bootstrapping = EmrServiceFixture.docker(runner, bootstrap, null);
@@ -2096,11 +2351,14 @@ class EmrServiceContainerStepsTest {
* {@code outcome}, or stopped once {@link #stop} has been called for it.
*/
private static final class FakeBootstrap implements BootstrapRunner {
final List<Bootstrap> bootstraps = new ArrayList<>();
// A cluster's nodes' bootstraps run on threads of their own.
final List<Bootstrap> bootstraps = Collections.synchronizedList(new ArrayList<>());
final List<String> stopped = new ArrayList<>();
final List<String> removed = new ArrayList<>();
final List<String> removed = Collections.synchronizedList(new ArrayList<>());
final List<Set<String>> kept = new ArrayList<>();
Outcome outcome = Outcome.succeeded(IMAGE);
// How each bootstrap ends, where not as outcome says.
Function<Bootstrap, Outcome> outcomes;
Runnable whileRunning = () -> { };
// Whether Docker removes an image it is asked to.
boolean removes = true;
@@ -2112,7 +2370,7 @@ class EmrServiceContainerStepsTest {
return Outcome.halted();
}
whileRunning.run();
return outcome;
return outcomes != null ? outcomes.apply(bootstrap) : outcome;
}
@Override