From f15c14979e0d63ac782e66061b7a12e3322c12df Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:51:10 -0400 Subject: [PATCH] fix(emr): give a cluster's instance groups and fleets its states (#8526) A cluster's instance groups and fleets reported RUNNING, with all their capacity, from its creation until it was TERMINATED, whatever state the cluster itself was in. InstanceGroupState and InstanceFleetState have PROVISIONING, BOOTSTRAPPING and TERMINATING too, and the Management Guide's events end an instance fleet's provisioning with "Provisioning for instance fleet ... is complete ... The instance fleet now has On-Demand capacity of Num". No source says when each applies, so they now follow the cluster's state, Floci's reading: PROVISIONING while it is STARTING, with nothing running or provisioned yet, BOOTSTRAPPING with every instance up for its bootstrap actions, RUNNING while it runs or waits, and TERMINATING as it ends, the instances still running. One added later takes the state its cluster's others are in. ListInstances gives each instance its group's or fleet's state, lists those being provisioned, and filters by InstanceStates per instance. A cluster terminated while it is STARTING keeps listing the instances it was provisioning, still PROVISIONING and none of them running, until it is TERMINATED, so the same instances are listed throughout. --- docs/services/emr.md | 17 ++- .../aws/services/emr/EmrClusterLifecycle.java | 66 +++++++++- .../io/floci/aws/services/emr/EmrHandler.java | 62 +++++++-- .../io/floci/aws/services/emr/EmrService.java | 14 +-- .../aws/services/emr/EmrServiceTest.java | 119 +++++++++++++++++- .../emr/EmrTimedLifecycleIntegrationTest.java | 60 +++++++++ 6 files changed, 311 insertions(+), 27 deletions(-) diff --git a/docs/services/emr.md b/docs/services/emr.md index e08f457e30..8f37d55d7b 100644 --- a/docs/services/emr.md +++ b/docs/services/emr.md @@ -104,9 +104,20 @@ also applies when termination protection is enabled. `AddJobFlowSteps` rejects a `TERMINATING` or `TERMINATED` cluster without adding steps or changing its termination status, and answers an unknown cluster with `ValidationException` "Specified job flow ID does not exist.", as AWS does. -Instance groups and fleets report `RUNNING` from the cluster's creation until it is `TERMINATED`. Then -they report `TERMINATED` with zero running or provisioned capacity; their requested and target -capacities remain recorded. `ListInstances` numbers the synthetic IDs and addresses of the `MASTER` +Instance groups and fleets follow their cluster's state, Floci's reading, as no source says when theirs apply: +`PROVISIONING` while it is `STARTING`, with nothing running or provisioned yet (the guide's events end an instance +fleet's provisioning with "Provisioning for instance fleet ... is complete ... The instance fleet now has On-Demand +capacity of Num"), `BOOTSTRAPPING` while it is, with every instance up for its bootstrap actions, `RUNNING` while it +runs or waits, and `TERMINATING` as it ends; one added later takes the state the cluster's others are in. Once it is +`TERMINATED` they report `TERMINATED` with zero running or provisioned capacity; their requested and target +capacities remain recorded, and a group that ended while it was provisioned counts the instances it asked for as +terminated, which `ListInstances` lists so, as an ended fleet's targets are. `ListInstances` gives each instance +its group's or fleet's state, `PROVISIONING`, +`BOOTSTRAPPING`, `RUNNING` (while its cluster ends too) or `TERMINATED`, lists one being provisioned though none runs +yet, and its `InstanceStates` filter keeps the instances in the states it names. A cluster terminated while it is +`STARTING` keeps the instances it was provisioning listed, still `PROVISIONING` and with none of them running, until +it is `TERMINATED`, so the same instances are listed throughout. `ListInstances` numbers the +synthetic IDs and addresses of the `MASTER` group's or fleet's instance first, so the primary node keeps `i-00000000000000001` and `10.0.0.1`, as `DescribeCluster`'s `MasterPublicDnsName` says, while the others resize, and every other instance's private address is `10..0.`, its group's or fleet's number in the cluster and its own in it, so it too stays while diff --git a/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java b/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java index e492e55184..1439acac2b 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java +++ b/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java @@ -32,6 +32,7 @@ final class EmrClusterLifecycle { static final String STARTING = "STARTING"; static final String BOOTSTRAPPING = "BOOTSTRAPPING"; + static final String PROVISIONING = "PROVISIONING"; static final String RUNNING = "RUNNING"; static final String WAITING = "WAITING"; static final String TERMINATING = "TERMINATING"; @@ -112,6 +113,7 @@ final class EmrClusterLifecycle { /** A new cluster, STARTING at {@code now}, with its steps PENDING. */ void start(EmrCluster cluster, Instant now, String region) { cluster.setState(STARTING); + instancesFollow(cluster); cluster.setCreationDateTime(now); events.queue(events.clusterStateChange(cluster, STARTING, now), region); submit(cluster, cluster.getSteps(), now, region); @@ -150,6 +152,7 @@ final class EmrClusterLifecycle { bootstrapping.stop(cluster); } cluster.setState(TERMINATING); + instancesFollow(cluster); cluster.setStateChangeReasonCode(code); cluster.setStateChangeMessage(message); cluster.setTerminatingDateTime(now); @@ -230,6 +233,8 @@ final class EmrClusterLifecycle { switch (cluster.getState()) { case STARTING -> { cluster.setState(BOOTSTRAPPING); + // Its instances are up before its bootstrap actions run on them. + instancesFollow(cluster); if (bootstrapping != null && cluster.getBootstrapActions() != null) { cluster.setBootstrapInContainer(true); bootstrapping.start(cluster, region); @@ -281,6 +286,7 @@ final class EmrClusterLifecycle { private void ready(EmrCluster cluster, Instant at, String region) { cluster.setReadyDateTime(at); cluster.setState(RUNNING); + instancesFollow(cluster); runSteps(cluster, at, region); } @@ -433,10 +439,68 @@ final class EmrClusterLifecycle { return TERMINATED.equals(state) || TERMINATED_WITH_ERRORS.equals(state); } + /** + * The state a cluster's instance groups and fleets are in while it is in {@code clusterState}, Floci's reading, as + * no source says when theirs apply: PROVISIONING while it starts, as the guide's events have an instance fleet's + * "Provisioning ... is complete" end it, then BOOTSTRAPPING, RUNNING while it runs or waits, and TERMINATING as + * it ends; TERMINATED once it has (see releaseInstanceCapacity). + */ + static String instancesState(String clusterState) { + return switch (clusterState) { + case STARTING -> PROVISIONING; + case BOOTSTRAPPING -> BOOTSTRAPPING; + case TERMINATING -> TERMINATING; + default -> RUNNING; + }; + } + + /** + * An instance group in {@code state}: none of its instances running while they are provisioned, every one it asks + * for from its cluster's bootstrap on, which runs on them; as it ends they run until they are gone. + */ + static void groupIn(EmrInstanceGroup group, String state) { + group.setState(state); + if (PROVISIONING.equals(state)) { + group.setRunningInstanceCount(0); + } else if (!TERMINATING.equals(state)) { + group.setRunningInstanceCount(group.getRequestedInstanceCount()); + } + } + + /** An instance fleet in {@code state}, its capacity provisioned as {@link #groupIn} has a group's instances run. */ + static void fleetIn(EmrInstanceFleet fleet, String state) { + fleet.setState(state); + if (PROVISIONING.equals(state)) { + fleet.setProvisionedOnDemandCapacity(0); + fleet.setProvisionedSpotCapacity(0); + } else if (!TERMINATING.equals(state)) { + fleet.setProvisionedOnDemandCapacity(fleet.getTargetOnDemandCapacity()); + fleet.setProvisionedSpotCapacity(fleet.getTargetSpotCapacity()); + } + } + + // The cluster's instance groups and fleets, but those that have ended, in the state its own puts them in. + private static void instancesFollow(EmrCluster cluster) { + String state = instancesState(cluster.getState()); + for (EmrInstanceGroup group : cluster.getInstanceGroups()) { + if (!TERMINATED.equals(group.getState())) { + groupIn(group, state); + } + } + for (EmrInstanceFleet fleet : cluster.getInstanceFleets()) { + if (!TERMINATED.equals(fleet.getState())) { + fleetIn(fleet, state); + } + } + } + private static void releaseInstanceCapacity(EmrCluster cluster) { for (EmrInstanceGroup group : cluster.getInstanceGroups()) { group.setState(TERMINATED); - group.setTerminatedInstanceCount(group.getRunningInstanceCount()); + // One that ended while it was provisioned ran none, and the instances it asked for, which ListInstances + // listed, have ended, as an ended fleet's targets say what it ran. + group.setTerminatedInstanceCount(group.getRunningInstanceCount() > 0 ? group.getRunningInstanceCount() + : group.getRequestedInstanceCount()); group.setRunningInstanceCount(0); } for (EmrInstanceFleet fleet : cluster.getInstanceFleets()) { diff --git a/src/main/java/io/floci/aws/services/emr/EmrHandler.java b/src/main/java/io/floci/aws/services/emr/EmrHandler.java index 32f9e8ef7b..784427239a 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrHandler.java +++ b/src/main/java/io/floci/aws/services/emr/EmrHandler.java @@ -330,8 +330,7 @@ public class EmrHandler { ArrayNode arr = response.putArray("Instances"); boolean terminated = EmrClusterLifecycle.isTerminated(cluster.getState()); List states = stringList(request.path("InstanceStates")); - if ((!states.isEmpty() && !states.contains(terminated ? "TERMINATED" : "RUNNING")) - || (terminated && !withinTerminatedInstanceRetention(cluster.getEndDateTime(), Instant.now()))) { + if (terminated && !withinTerminatedInstanceRetention(cluster.getEndDateTime(), Instant.now())) { return Response.ok(response).build(); } String groupId = text(request, "InstanceGroupId"); @@ -353,7 +352,8 @@ public class EmrHandler { if (set != primary) { next += set.count(); } - if (!set.matches(groupId, groupTypes, fleetId, fleetType)) { + if (!set.matches(groupId, groupTypes, fleetId, fleetType) + || !states.isEmpty() && !states.contains(set.state())) { continue; } if (skip >= set.count()) { @@ -399,27 +399,61 @@ public class EmrHandler { for (EmrInstanceGroup group : groups) { int count = listedInstanceCount(group, terminated); listed.add(new ListedInstances(group.getId(), null, group.getInstanceGroupType(), group.getInstanceType(), - EmrService.groupNumber(groups, group), count, group.getMarket(), count)); + EmrService.groupNumber(groups, group), count, group.getMarket(), count, + instanceState(group.getState(), beingProvisioned(group), terminated))); } List fleets = cluster.getInstanceFleets(); for (EmrInstanceFleet fleet : fleets) { - // An ended fleet has no capacity provisioned any more; its targets, which it can no longer change, say what - // it ran. - long onDemand = terminated ? fleet.getTargetOnDemandCapacity() : fleet.getProvisionedOnDemandCapacity(); - long spot = terminated ? fleet.getTargetSpotCapacity() : fleet.getProvisionedSpotCapacity(); + // An ended fleet has no capacity provisioned any more, and one being provisioned none yet: its targets, + // which an ended one can no longer change, say what it ran, or what it is being provisioned with. + boolean provisioning = beingProvisioned(fleet); + boolean targets = terminated || provisioning; + long onDemand = targets ? fleet.getTargetOnDemandCapacity() : fleet.getProvisionedOnDemandCapacity(); + long spot = targets ? fleet.getTargetSpotCapacity() : fleet.getProvisionedSpotCapacity(); listed.add(new ListedInstances(null, fleet.getId(), fleet.getInstanceFleetType(), fleet.getInstanceTypes().isEmpty() ? null : fleet.getInstanceTypes().getFirst(), - EmrService.fleetNumber(fleets, fleet), onDemand + spot, "ON_DEMAND", onDemand)); + EmrService.fleetNumber(fleets, fleet), onDemand + spot, "ON_DEMAND", onDemand, + instanceState(fleet.getState(), provisioning, terminated))); } return listed; } + // The state ListInstances gives the instances of a group or fleet in groupState, InstanceState's: being + // provisioned, bootstrapped, running, as they still are while their cluster ends, or gone. + private static String instanceState(String groupState, boolean provisioning, boolean terminated) { + if (terminated || EmrClusterLifecycle.TERMINATED.equals(groupState)) { + return "TERMINATED"; + } + if (provisioning) { + return EmrClusterLifecycle.PROVISIONING; + } + return EmrClusterLifecycle.BOOTSTRAPPING.equals(groupState) ? groupState : "RUNNING"; + } + + /** + * Whether a group's instances are still being provisioned: while it is PROVISIONING, and while its cluster ends + * before any of them ran, TERMINATING with none running. Those it asks for stay listed, the instances ListInstances + * showed being provisioned, until they have ended, with none of them counted as running. + */ + static boolean beingProvisioned(EmrInstanceGroup group) { + return EmrClusterLifecycle.PROVISIONING.equals(group.getState()) + || EmrClusterLifecycle.TERMINATING.equals(group.getState()) && group.getRunningInstanceCount() == 0; + } + + /** Whether a fleet's capacity is still being provisioned, as {@link #beingProvisioned(EmrInstanceGroup)}. */ + static boolean beingProvisioned(EmrInstanceFleet fleet) { + return EmrClusterLifecycle.PROVISIONING.equals(fleet.getState()) + || EmrClusterLifecycle.TERMINATING.equals(fleet.getState()) + && fleet.getProvisionedOnDemandCapacity() + fleet.getProvisionedSpotCapacity() == 0; + } + /** * The instances ListInstances lists for one instance group or fleet: {@code count} of them, with addresses numbered - * {@code number}, those after the first {@code spotAfter} on the spot market and the others on {@code market}. + * {@code number}, those after the first {@code spotAfter} on the spot market and the others on {@code market}, each + * in {@code state}. */ record ListedInstances(String groupId, String fleetId, String type, String instanceType, int number, long count, - String market, long spotAfter) { + String market, long spotAfter, String state) { String marketOf(long ordinal) { return ordinal <= spotAfter ? market : "SPOT"; @@ -442,6 +476,10 @@ public class EmrHandler { if (terminated && group.getTerminatedInstanceCount() > 0) { return group.getTerminatedInstanceCount(); } + // Being provisioned, none of them runs yet, and each is listed. + if (beingProvisioned(group)) { + return group.getRequestedInstanceCount(); + } return group.getRunningInstanceCount(); } @@ -953,7 +991,7 @@ public class EmrHandler { if (set.instanceType() != null) { node.put("InstanceType", set.instanceType()); } - node.putObject("Status").put("State", terminated ? "TERMINATED" : "RUNNING"); + node.putObject("Status").put("State", set.state()); return node; } diff --git a/src/main/java/io/floci/aws/services/emr/EmrService.java b/src/main/java/io/floci/aws/services/emr/EmrService.java index 1c7ff024de..faf7f2b60e 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrService.java @@ -442,16 +442,12 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear ? "INSTANCE_GROUP" : "INSTANCE_FLEET"); cluster.setAutoTerminate(!cluster.isKeepJobFlowAliveWhenNoSteps()); cluster.setMasterPublicDnsName(masterDnsName(region)); + // Their states and what runs of them follow the cluster's, from its start (see EmrClusterLifecycle). for (EmrInstanceGroup g : cluster.getInstanceGroups()) { g.setId("ig-" + randomId(13)); - g.setRunningInstanceCount(g.getRequestedInstanceCount()); - g.setState("RUNNING"); } for (EmrInstanceFleet f : cluster.getInstanceFleets()) { f.setId("if-" + randomId(13)); - f.setProvisionedOnDemandCapacity(f.getTargetOnDemandCapacity()); - f.setProvisionedSpotCapacity(f.getTargetSpotCapacity()); - f.setState("RUNNING"); } cluster.setProvisionedCoreNodes(coreNodes(cluster)); cluster.getSteps().forEach(step -> step.setId("s-" + randomId(13))); @@ -666,8 +662,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear List ids = new ArrayList<>(); for (EmrInstanceGroup group : groups) { group.setId("ig-" + randomId(13)); - group.setRunningInstanceCount(group.getRequestedInstanceCount()); - group.setState("RUNNING"); + // As the cluster's others are while it is as it is. + EmrClusterLifecycle.groupIn(group, EmrClusterLifecycle.instancesState(cluster.getState())); cluster.getInstanceGroups().add(group); ids.add(group.getId()); } @@ -773,9 +769,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear throw invalid(fleetsAndGroupsApart(clusterId, "instance groups")); } fleet.setId("if-" + randomId(13)); - fleet.setProvisionedOnDemandCapacity(fleet.getTargetOnDemandCapacity()); - fleet.setProvisionedSpotCapacity(fleet.getTargetSpotCapacity()); - fleet.setState("RUNNING"); + EmrClusterLifecycle.fleetIn(fleet, EmrClusterLifecycle.instancesState(cluster.getState())); cluster.getInstanceFleets().add(fleet); clusterStore.put(clusterId, cluster); resizeNodes(cluster); diff --git a/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java b/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java index 081f08044c..66946a324c 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static io.floci.aws.services.emr.EmrServiceFixture.REGION; import static io.floci.aws.services.emr.EmrServiceFixture.cluster; @@ -369,6 +370,120 @@ class EmrServiceTest { assertFalse(EmrClusterLifecycle.cancelsRunningSteps("emr-4.9.6")); } + @Test + void aClustersInstanceGroupsAndFleetsFollowItsStateAndRunNothingWhileTheyAreProvisioned() { + EmrServiceFixture fixture = new EmrServiceFixture(10, 0, 30); + EmrService service = fixture.service; + EmrCluster requested = cluster("states", true); + requested.getInstanceGroups().addAll(List.of(group("MASTER", 1), group("CORE", 2))); + String id = service.runJobFlow(requested, REGION).getId(); + EmrCluster fleets = cluster("fleets", true); + fleets.getInstanceFleets().add(fleet("CORE", 3)); + String fleetsId = service.runJobFlow(fleets, REGION).getId(); + + assertEquals(List.of("PROVISIONING", "PROVISIONING"), groupStates(service, id)); + assertEquals(0, service.listInstanceGroups(id).get(1).getRunningInstanceCount()); + assertEquals("PROVISIONING", service.listInstanceFleets(fleetsId).get(0).getState()); + assertEquals(0, service.listInstanceFleets(fleetsId).get(0).getProvisionedOnDemandCapacity()); + // One added while it starts is as its others are. + service.addInstanceGroups(id, List.of(group("TASK", 1))); + assertEquals(List.of("PROVISIONING", "PROVISIONING", "PROVISIONING"), groupStates(service, id)); + + fixture.clock.advance(Duration.ofSeconds(5)); + assertEquals("BOOTSTRAPPING", service.describeCluster(id).getState()); + assertEquals(List.of("BOOTSTRAPPING", "BOOTSTRAPPING", "BOOTSTRAPPING"), groupStates(service, id)); + assertEquals(2, service.listInstanceGroups(id).get(1).getRunningInstanceCount()); + assertEquals(3, service.listInstanceFleets(fleetsId).get(0).getProvisionedOnDemandCapacity()); + + fixture.clock.advance(Duration.ofSeconds(5)); + assertEquals("WAITING", service.describeCluster(id).getState()); + assertEquals(List.of("RUNNING", "RUNNING", "RUNNING"), groupStates(service, id)); + assertEquals("RUNNING", service.listInstanceFleets(fleetsId).get(0).getState()); + + service.terminateJobFlows(List.of(id)); + assertEquals(List.of("TERMINATING", "TERMINATING", "TERMINATING"), groupStates(service, id)); + fixture.clock.advance(Duration.ofSeconds(30)); + assertEquals(List.of("TERMINATED", "TERMINATED", "TERMINATED"), groupStates(service, id)); + assertEquals(0, service.listInstanceGroups(id).get(1).getRunningInstanceCount()); + } + + @Test + void aGroupThatEndsWhileItIsProvisionedListsTheInstancesItAskedForAsEnded() { + EmrServiceFixture fixture = new EmrServiceFixture(10, 0, 30); + EmrService service = fixture.service; + EmrCluster requested = cluster("ends-early", true); + requested.getInstanceGroups().addAll(List.of(group("MASTER", 1), group("CORE", 2))); + String id = service.runJobFlow(requested, REGION).getId(); + assertEquals(Map.of("PROVISIONING", 3L), listedByState(service.describeCluster(id), false)); + + service.terminateJobFlows(List.of(id)); + + // The same instances while it ends, still being provisioned, with none of them running. + EmrCluster ending = service.describeCluster(id); + assertEquals("TERMINATING", ending.getState()); + assertEquals(List.of(0, 0), service.listInstanceGroups(id).stream() + .map(EmrInstanceGroup::getRunningInstanceCount).toList()); + assertEquals(Map.of("PROVISIONING", 3L), listedByState(ending, false)); + fixture.clock.advance(Duration.ofSeconds(30)); + + // Listed as provisioned, then as ended, as an ended fleet's are. + EmrCluster ended = service.describeCluster(id); + assertEquals("TERMINATED", ended.getState()); + assertEquals(2, service.listInstanceGroups(id).get(1).getTerminatedInstanceCount()); + assertEquals(0, service.listInstanceGroups(id).get(1).getRunningInstanceCount()); + assertEquals(Map.of("TERMINATED", 3L), listedByState(ended, true)); + } + + @Test + void aFleetThatEndsWhileItIsProvisionedKeepsItsInstancesListedUntilTheyEnd() { + EmrServiceFixture fixture = new EmrServiceFixture(10, 0, 30); + EmrService service = fixture.service; + EmrCluster requested = cluster("fleet-ends-early", true); + requested.getInstanceFleets().addAll(List.of(fleet("MASTER", 1), fleet("CORE", 2))); + String id = service.runJobFlow(requested, REGION).getId(); + assertEquals(Map.of("PROVISIONING", 3L), listedByState(service.describeCluster(id), false)); + + service.terminateJobFlows(List.of(id)); + + EmrCluster ending = service.describeCluster(id); + assertEquals("TERMINATING", ending.getState()); + assertEquals(List.of(0, 0), ending.getInstanceFleets().stream() + .map(EmrInstanceFleet::getProvisionedOnDemandCapacity).toList()); + assertEquals(Map.of("PROVISIONING", 3L), listedByState(ending, false)); + fixture.clock.advance(Duration.ofSeconds(30)); + + EmrCluster ended = service.describeCluster(id); + assertEquals("TERMINATED", ended.getState()); + assertEquals(Map.of("TERMINATED", 3L), listedByState(ended, true)); + } + + @Test + void aGroupThatRanIsListedRunningWhileItsClusterEnds() { + EmrServiceFixture fixture = new EmrServiceFixture(0, 0, 30); + EmrService service = fixture.service; + EmrCluster requested = cluster("ends-late", true); + requested.getInstanceGroups().addAll(List.of(group("MASTER", 1), group("CORE", 2))); + String id = service.runJobFlow(requested, REGION).getId(); + assertEquals(Map.of("RUNNING", 3L), listedByState(service.describeCluster(id), false)); + + service.terminateJobFlows(List.of(id)); + + assertEquals("TERMINATING", service.describeCluster(id).getState()); + assertEquals(Map.of("RUNNING", 3L), listedByState(service.describeCluster(id), false)); + } + + /** How many instances ListInstances lists in each state, as its InstanceStates filter keeps them. */ + private static Map listedByState(EmrCluster cluster, boolean terminated) { + return EmrHandler.listedInstances(cluster, terminated).stream() + .filter(set -> set.count() > 0) + .collect(Collectors.groupingBy(EmrHandler.ListedInstances::state, + Collectors.summingLong(EmrHandler.ListedInstances::count))); + } + + private static List groupStates(EmrService service, String clusterId) { + return service.listInstanceGroups(clusterId).stream().map(EmrInstanceGroup::getState).toList(); + } + @Test void terminatingCancelsTheStepsNotYetCompletedAndEndsAfterTheDelay() { EmrServiceFixture fixture = new EmrServiceFixture(0, 60, 30); @@ -387,7 +502,9 @@ class EmrServiceTest { assertEquals("USER_REQUEST", terminating.getStateChangeReasonCode()); assertNull(terminating.getEndDateTime()); assertEquals(List.of("CANCELLED", "CANCELLED"), stepStates(service.listSteps(id, null, null))); - assertEquals("RUNNING", service.listInstanceGroups(id).get(0).getState()); + // Its instances run until they are gone. + assertEquals("TERMINATING", service.listInstanceGroups(id).get(0).getState()); + assertEquals(2, service.listInstanceGroups(id).get(0).getRunningInstanceCount()); AwsException refused = assertThrows(AwsException.class, () -> service.addJobFlowSteps(id, List.of(step("late")))); assertEquals(SHUT_DOWN, refused.getMessage()); diff --git a/src/test/java/io/floci/aws/services/emr/EmrTimedLifecycleIntegrationTest.java b/src/test/java/io/floci/aws/services/emr/EmrTimedLifecycleIntegrationTest.java index 0ed3d1e9b8..fe15f86e12 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrTimedLifecycleIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrTimedLifecycleIntegrationTest.java @@ -13,6 +13,7 @@ import java.util.Map; import static io.floci.aws.testing.EmrRequests.emr; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.nullValue; /** @@ -74,4 +75,63 @@ class EmrTimedLifecycleIntegrationTest { .body("message", equalTo( "A job flow that is shutting down, terminated, or finished may not be modified.")); } + + @Test + void aStartingClustersInstancesAreListedAsProvisionedWithNoneRunningYet() { + String clusterId = emr("RunJobFlow", "{\"Name\":\"provisioning\",\"Instances\":{" + + "\"KeepJobFlowAliveWhenNoSteps\":true,\"InstanceGroups\":[" + + "{\"InstanceRole\":\"MASTER\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1}," + + "{\"InstanceRole\":\"CORE\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":2}]}}") + .then().statusCode(200).extract().path("JobFlowId"); + + emr("ListInstanceGroups", "{\"ClusterId\":\"" + clusterId + "\"}").then().statusCode(200) + .body("InstanceGroups.Status.State", equalTo(List.of("PROVISIONING", "PROVISIONING"))) + .body("InstanceGroups.RunningInstanceCount", equalTo(List.of(0, 0))); + List ids = emr("ListInstances", "{\"ClusterId\":\"" + clusterId + "\"}").then().statusCode(200) + .body("Instances.Status.State", equalTo(List.of("PROVISIONING", "PROVISIONING", "PROVISIONING"))) + .extract().path("Instances.Id"); + // The filter keeps the states it names. + emr("ListInstances", "{\"ClusterId\":\"" + clusterId + "\",\"InstanceStates\":[\"RUNNING\"]}") + .then().statusCode(200) + .body("Instances", equalTo(List.of())); + emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}").then().statusCode(200); + emr("ListInstanceGroups", "{\"ClusterId\":\"" + clusterId + "\"}").then().statusCode(200) + .body("InstanceGroups.Status.State", equalTo(List.of("TERMINATING", "TERMINATING"))) + .body("InstanceGroups.RunningInstanceCount", equalTo(List.of(0, 0))); + // While it ends, the same instances, still being provisioned, none of them running. + emr("ListInstances", "{\"ClusterId\":\"" + clusterId + "\",\"InstanceStates\":[\"PROVISIONING\"]}") + .then().statusCode(200) + .body("Instances.Id", equalTo(ids)); + emr("ListInstances", "{\"ClusterId\":\"" + clusterId + "\",\"InstanceStates\":[\"RUNNING\"]}") + .then().statusCode(200) + .body("Instances", equalTo(List.of())); + } + + @Test + void aStartingClustersFleetsKeepTheirInstancesListedWhileItEnds() { + String clusterId = emr("RunJobFlow", "{\"Name\":\"provisioning-fleets\",\"Instances\":{" + + "\"KeepJobFlowAliveWhenNoSteps\":true,\"InstanceFleets\":[" + + "{\"InstanceFleetType\":\"MASTER\",\"TargetOnDemandCapacity\":1," + + "\"InstanceTypeConfigs\":[{\"InstanceType\":\"m5.xlarge\"}]}," + + "{\"InstanceFleetType\":\"CORE\",\"TargetOnDemandCapacity\":2," + + "\"InstanceTypeConfigs\":[{\"InstanceType\":\"m5.xlarge\"}]}]}}") + .then().statusCode(200).extract().path("JobFlowId"); + List ids = emr("ListInstances", "{\"ClusterId\":\"" + clusterId + + "\",\"InstanceStates\":[\"PROVISIONING\"]}") + .then().statusCode(200) + .body("Instances.InstanceFleetId", hasSize(3)) + .extract().path("Instances.Id"); + + emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}").then().statusCode(200); + + emr("ListInstanceFleets", "{\"ClusterId\":\"" + clusterId + "\"}").then().statusCode(200) + .body("InstanceFleets.Status.State", equalTo(List.of("TERMINATING", "TERMINATING"))) + .body("InstanceFleets.ProvisionedOnDemandCapacity", equalTo(List.of(0, 0))); + emr("ListInstances", "{\"ClusterId\":\"" + clusterId + "\",\"InstanceStates\":[\"PROVISIONING\"]}") + .then().statusCode(200) + .body("Instances.Id", equalTo(ids)); + emr("ListInstances", "{\"ClusterId\":\"" + clusterId + "\",\"InstanceStates\":[\"RUNNING\"]}") + .then().statusCode(200) + .body("Instances", equalTo(List.of())); + } }