fix(emr): terminate the instances ModifyInstanceGroups' EC2InstanceIdsToTerminate names (#9142)

ModifyInstanceGroups ignored EC2InstanceIdsToTerminate, "The Amazon EC2
InstanceIds to terminate. After you terminate the instances, the
instance group will not return to its original requested size." Floci's
instances had no identity of their own: their ids and addresses
followed their place in their group, so a group could only shrink from
its end.

Each instance group now keeps the ordinals of the instances those ids
ended, and its instances take the first ordinals that are not among
them. The named instances leave ListInstances and, in the docker runner
mode, take their core or task node with them, HDFS moving its blocks
first, as nodes are named by group and ordinal. The group's requested
and running counts go down by as many. Its other instances keep their
ids, addresses and nodes, as do the cluster's other groups', and an
instance the group gets later takes a new ordinal, not an ended one's.
An InstanceCount beside them is the group's size after that.

An id that is not one of the group's instances is refused with
ValidationException, as ModifyInstanceGroups' other refusals are
recorded, and a MASTER group's with the refusal its count gets; AWS's
wording for these is not documented, so it is Floci's.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-21 11:45:08 -04:00
1 parent 8a0112d9c5
commit 4595e8d1ef
6 files changed
+340 -31

No files matched your search

@@ -17,6 +17,8 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* EMR instance group and fleet resizing and bootstrap action listing over the AWS JSON 1.1 wire
@@ -510,4 +512,58 @@ class EmrResizeIntegrationTest {
.body("__type", equalTo("ValidationException"))
.body("message", containsString("'instanceGroups.1.member.reconfigurationType'"));
}
/**
* EC2InstanceIdsToTerminate ends the named instance: the group's others, and the next group's, keep their ids and
* addresses, the group's counts go down, and an instance it gets later takes a new id.
*/
@Test
void terminatingANamedInstanceLeavesTheOthersTheirIdsAndAddresses() {
String id = emr("RunJobFlow", "{\"Name\":\"named\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,"
+ "\"InstanceGroups\":[{\"InstanceRole\":\"MASTER\",\"InstanceType\":\"m5.xlarge\","
+ "\"InstanceCount\":1},{\"InstanceRole\":\"CORE\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":3},"
+ "{\"InstanceRole\":\"TASK\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1}]}}")
.then().statusCode(200).extract().path("JobFlowId");
try {
String cluster = "{\"ClusterId\":\"" + id + "\"}";
String core = emr("ListInstanceGroups", cluster).jsonPath()
.getString("InstanceGroups.find { it.InstanceGroupType == 'CORE' }.Id");
Response before = emr("ListInstances", cluster);
List<String> ids = before.jsonPath().getList("Instances.Ec2InstanceId");
List<String> addresses = before.jsonPath().getList("Instances.PrivateIpAddress");
String ended = ids.get(2);
emr("ModifyInstanceGroups", "{\"ClusterId\":\"" + id + "\",\"InstanceGroups\":[{\"InstanceGroupId\":\""
+ core + "\",\"EC2InstanceIdsToTerminate\":[\"" + ended + "\"]}]}").then().statusCode(200);
Response after = emr("ListInstances", cluster);
assertEquals(List.of(ids.get(0), ids.get(1), ids.get(3), ids.get(4)),
after.jsonPath().getList("Instances.Ec2InstanceId"));
assertEquals(List.of(addresses.get(0), addresses.get(1), addresses.get(3), addresses.get(4)),
after.jsonPath().getList("Instances.PrivateIpAddress"));
emr("ListInstanceGroups", cluster).then().statusCode(200)
.body("InstanceGroups.find { it.Id == '" + core + "' }.RequestedInstanceCount", equalTo(2))
.body("InstanceGroups.find { it.Id == '" + core + "' }.RunningInstanceCount", equalTo(2));
emr("ModifyInstanceGroups", "{\"ClusterId\":\"" + id + "\",\"InstanceGroups\":[{\"InstanceGroupId\":\""
+ core + "\",\"EC2InstanceIdsToTerminate\":[\"" + ended + "\"]}]}").then().statusCode(400)
.body("__type", equalTo("ValidationException"));
emr("ModifyInstanceGroups", "{\"ClusterId\":\"" + id + "\",\"InstanceGroups\":[{\"InstanceGroupId\":\""
+ core + "\",\"EC2InstanceIdsToTerminate\":\"" + ids.get(3) + "\"}]}").then().statusCode(400)
.body("__type", equalTo("SerializationException"));
// The group's last instance too: the task instance after it keeps its id.
emr("ModifyInstanceGroups", "{\"ClusterId\":\"" + id + "\",\"InstanceGroups\":[{\"InstanceGroupId\":\""
+ core + "\",\"EC2InstanceIdsToTerminate\":[\"" + ids.get(3) + "\"]}]}").then().statusCode(200);
assertEquals(List.of(ids.get(0), ids.get(1), ids.get(4)),
emr("ListInstances", cluster).jsonPath().getList("Instances.Ec2InstanceId"));
emr("ModifyInstanceGroups", "{\"ClusterId\":\"" + id + "\",\"InstanceGroups\":[{\"InstanceGroupId\":\""
+ core + "\",\"InstanceCount\":3}]}").then().statusCode(200);
List<String> grown = emr("ListInstances", "{\"ClusterId\":\"" + id + "\",\"InstanceGroupId\":\"" + core
+ "\"}").jsonPath().getList("Instances.Ec2InstanceId");
assertEquals(3, grown.size());
assertFalse(grown.contains(ended), "an ended instance's id is not taken again");
} finally {
emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + id + "\"]}").then().statusCode(200);
}
}
}
@@ -20,9 +20,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.PrimitiveIterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import static io.floci.aws.services.emr.EmrServiceFixture.REGION;
import static io.floci.aws.services.emr.EmrServiceFixture.cluster;
@@ -859,6 +861,74 @@ class EmrServiceTest {
assertTrue(nodes.stream().allMatch(ClusterStorage.Node::core), nodes.toString());
}
/**
* EC2InstanceIdsToTerminate ends the instances it names: their nodes go, the group's other instances keep their ids
* and nodes, and one the group gets later takes a new ordinal, not an ended one's.
*/
@Test
void terminatingANamedInstanceTakesItsNodeAndLeavesTheOthersTheirs() {
EmrServiceFixture fixture = new EmrServiceFixture(0, 0, 0);
EmrService service = fixture.service;
EmrCluster requested = cluster("named", true);
requested.getInstanceGroups().addAll(List.of(group("MASTER", 1), group("CORE", 3)));
EmrCluster cluster = service.runJobFlow(requested, REGION);
String core = cluster.getInstanceGroups().get(1).getId();
// The primary node is i-...1, and the core group's instances i-...2 to i-...4, ordinals 1 to 3.
String second = EmrService.ec2InstanceId(3);
service.modifyInstanceGroups(cluster.getId(), REGION, List.of(
new EmrService.InstanceGroupModification(core, null, null, null, List.of(second))));
EmrCluster shrunk = service.describeCluster(cluster.getId());
assertEquals(List.of("1-1", "1-3"), service.workerNodes(shrunk).stream().map(ClusterStorage.Node::id).toList());
assertEquals(2, shrunk.getInstanceGroups().get(1).getRequestedInstanceCount());
assertEquals(2, shrunk.getInstanceGroups().get(1).getRunningInstanceCount());
AwsException again = assertThrows(AwsException.class, () -> service.modifyInstanceGroups(cluster.getId(),
REGION, List.of(new EmrService.InstanceGroupModification(core, null, null, null, List.of(second)))));
assertEquals("ValidationException", again.getErrorCode());
AwsException master = assertThrows(AwsException.class, () -> service.modifyInstanceGroups(cluster.getId(),
REGION, List.of(new EmrService.InstanceGroupModification(cluster.getInstanceGroups().get(0).getId(),
null, null, null, List.of(EmrService.ec2InstanceId(1))))));
assertEquals("InvalidRequestException", master.getErrorCode());
service.modifyInstanceGroups(cluster.getId(), REGION,
List.of(new EmrService.InstanceGroupModification(core, 3, null)));
assertEquals(List.of("1-1", "1-3", "1-4"), service.workerNodes(service.describeCluster(cluster.getId()))
.stream().map(ClusterStorage.Node::id).toList());
}
@Test
void anInstancesOrdinalSkipsThoseItsGroupEnded() {
assertEquals(List.of(1L, 4L, 5L, 7L), LongStream.rangeClosed(1, 4)
.map(k -> EmrService.ordinalAt(List.of(2, 3, 6), k)).boxed().toList());
// The walk from any instance gives the same ordinals.
for (long k = 1; k <= 4; k++) {
PrimitiveIterator.OfLong walk = EmrService.ordinalsFrom(List.of(2, 3, 6), k);
for (long next = k; next <= 6; next++) {
assertEquals(EmrService.ordinalAt(List.of(2, 3, 6), next), walk.nextLong(), "from " + k);
}
}
}
/** One group named twice with the same instance ends it once. */
@Test
void anInstanceNamedTwiceInOneRequestIsEndedOnce() {
EmrServiceFixture fixture = new EmrServiceFixture(0, 0, 0);
EmrService service = fixture.service;
EmrCluster requested = cluster("twice", true);
requested.getInstanceGroups().addAll(List.of(group("MASTER", 1), group("CORE", 3)));
EmrCluster cluster = service.runJobFlow(requested, REGION);
String core = cluster.getInstanceGroups().get(1).getId();
EmrService.InstanceGroupModification end =
new EmrService.InstanceGroupModification(core, null, null, null, List.of(EmrService.ec2InstanceId(3)));
service.modifyInstanceGroups(cluster.getId(), REGION, List.of(end, end));
EmrCluster shrunk = service.describeCluster(cluster.getId());
assertEquals(2, shrunk.getInstanceGroups().get(1).getRequestedInstanceCount());
assertEquals(List.of("1-1", "1-3"), service.workerNodes(shrunk).stream().map(ClusterStorage.Node::id).toList());
}
/** ModifyInstanceFleet's answer for a cluster that is starting or bootstrapping. */
private static void assertNotRunningOrWaiting(AwsException refused) {
assertEquals("InvalidRequestException", refused.getErrorCode());