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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 20:51:10 -04:00
1 parent 0d2ea3a9eb
commit f15c14979e
6 files changed
+311 -27

No files matched your search

@@ -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()) {
@@ -330,8 +330,7 @@ public class EmrHandler {
ArrayNode arr = response.putArray("Instances");
boolean terminated = EmrClusterLifecycle.isTerminated(cluster.getState());
List<String> 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<EmrInstanceFleet> 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;
}
@@ -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<String> 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);