From c99d8ee5ea7f633761cf597d681cdebaacb63e03 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:19:19 -0400 Subject: [PATCH] fix(emr): list each instance fleet's InstanceTypeSpecifications (#8354) ListInstanceFleets wrote no InstanceTypeSpecifications: the fleet kept only the names of the instance types it was created with. The Terraform AWS provider reads instance_type_configs back from that member, a ForceNew set in both aws_emr_cluster and aws_emr_instance_fleet, so a fleet cluster read back empty after apply and every later plan replaced it. A fleet keeps the InstanceTypeConfigs it was given, from RunJobFlow and AddInstanceFleet, and ListInstanceFleets returns each as the InstanceTypeSpecification it describes: its type, bid price, custom AMI, priority and configurations as given, a WeightedCapacity of 1 where it sets none and a BidPriceAsPercentageOfOnDemandPrice of 100 where it sets neither bid price, the defaults the API reference gives for both, and its EbsConfiguration as one EbsBlockDevice per volume. The reference names no devices, so they are /dev/sdb, /dev/sdc and on. A fleet stored before this has its instance types alone, with the same defaults. --- docs/services/emr.md | 7 + .../io/floci/aws/services/emr/EmrHandler.java | 109 +++++++++++++++ .../services/emr/model/EmrInstanceFleet.java | 5 + ...anceTypeSpecificationsIntegrationTest.java | 131 ++++++++++++++++++ 4 files changed, 252 insertions(+) create mode 100644 src/test/java/io/floci/aws/services/emr/EmrInstanceTypeSpecificationsIntegrationTest.java diff --git a/docs/services/emr.md b/docs/services/emr.md index dbb10af232..bb0e7040c6 100644 --- a/docs/services/emr.md +++ b/docs/services/emr.md @@ -122,6 +122,13 @@ InstanceFleets parameters or InstanceGroups parameters, but not both.", the API instance groups with `InvalidRequestException`, each saying the cluster uses the other kind; AWS's wording for these is not documented, so it is Floci's. A fleet without its `InstanceFleetType`, or with an `InstanceTypeConfigs` member without its `InstanceType`, both required, is refused with the framework's `ValidationException`. +`ListInstanceFleets` gives each fleet's `InstanceTypeConfigs` back as its `InstanceTypeSpecifications`, with a +`WeightedCapacity` of 1 where a config sets none and a `BidPriceAsPercentageOfOnDemandPrice` of 100 where it sets +neither bid price, the defaults the API reference gives, and its `EbsConfiguration` as one `EbsBlockDevice` per volume. +The reference names no devices, so Floci names them `/dev/sdb`, `/dev/sdc` and on. A config attaching more than 25 +volumes to an instance is refused with `ValidationException` "There is a limit of 25 volumes per instance.", the +Management Guide's own limit and sentence; AWS's error for it is not documented. `ModifyInstanceFleet` changes a +fleet's target capacities only; the `InstanceTypeConfigs` it takes are not applied. A cluster that is `TERMINATING`, `TERMINATED` or `TERMINATED_WITH_ERRORS` also refuses `ModifyCluster`, the four `Set*` setters, and `Put` and `Remove` of its managed scaling, auto-termination and automatic 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 8aa74d8015..955515ff0c 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrHandler.java +++ b/src/main/java/io/floci/aws/services/emr/EmrHandler.java @@ -1,5 +1,6 @@ package io.floci.aws.services.emr; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -41,6 +42,8 @@ import java.util.Map; public class EmrHandler { private static final Logger LOG = Logger.getLogger(EmrHandler.class); + /** "There is a limit of 25 volumes per instance." (Amazon EMR Management Guide, Instance storage) */ + private static final int EBS_VOLUMES_PER_INSTANCE = 25; // ListInstances "Provides information for all active Amazon EC2 instances and Amazon EC2 instances terminated in the // last 30 days, up to a maximum of 2,000": that many to a call, the rest after its Marker. static final int INSTANCES_PER_PAGE = 2000; @@ -304,6 +307,7 @@ public class EmrHandler { if (!violations.isEmpty()) { throw SmithyValidationErrors.validationException(violations); } + requireEbsVolumeLimit(request.path("InstanceFleet")); String id = service.addInstanceFleet(text(request, "ClusterId"), fleet); ObjectNode response = objectMapper.createObjectNode(); response.put("ClusterId", text(request, "ClusterId")); @@ -850,10 +854,82 @@ public class EmrHandler { node.put("TargetSpotCapacity", f.getTargetSpotCapacity()); node.put("ProvisionedOnDemandCapacity", f.getProvisionedOnDemandCapacity()); node.put("ProvisionedSpotCapacity", f.getProvisionedSpotCapacity()); + node.set("InstanceTypeSpecifications", instanceTypeSpecifications(f)); node.putObject("Status").put("State", f.getState()); return node; } + /** + * Each InstanceTypeConfig the fleet was given, as the InstanceTypeSpecification it describes. A config that sets + * no WeightedCapacity has 1, and one that sets neither bid price has BidPriceAsPercentageOfOnDemandPrice 100, the + * defaults the API reference gives for both. Its EbsConfiguration becomes one EbsBlockDevice per volume, the way + * VolumesPerInstance counts them, named {@code /dev/sdb} on: the reference gives no device names. A fleet stored + * before its configs were kept has its instance types alone, with those same defaults. + */ + private ArrayNode instanceTypeSpecifications(EmrInstanceFleet f) { + ArrayNode specifications = objectMapper.createArrayNode(); + JsonNode configs = readRawArray(f.getInstanceTypeConfigs()); + if (configs == null) { + ArrayNode named = objectMapper.createArrayNode(); + f.getInstanceTypes().forEach(type -> named.addObject().put("InstanceType", type)); + configs = named; + } + for (JsonNode config : configs) { + ObjectNode specification = specifications.addObject(); + specification.put("InstanceType", config.path("InstanceType").asText(null)); + specification.put("WeightedCapacity", config.path("WeightedCapacity").asInt(1)); + boolean bidPrice = config.hasNonNull("BidPrice"); + if (bidPrice) { + specification.put("BidPrice", config.path("BidPrice").asText()); + } + if (config.hasNonNull("BidPriceAsPercentageOfOnDemandPrice")) { + specification.put("BidPriceAsPercentageOfOnDemandPrice", + config.path("BidPriceAsPercentageOfOnDemandPrice").asDouble()); + } else if (!bidPrice) { + specification.put("BidPriceAsPercentageOfOnDemandPrice", 100.0); + } + if (config.hasNonNull("Configurations")) { + specification.set("Configurations", config.get("Configurations")); + } + JsonNode ebs = config.path("EbsConfiguration"); + ArrayNode devices = objectMapper.createArrayNode(); + for (JsonNode device : ebs.path("EbsBlockDeviceConfigs")) { + // Bounded for a config stored before the limit was held at create. + int volumes = Math.min(device.path("VolumesPerInstance").asInt(1), + EBS_VOLUMES_PER_INSTANCE - devices.size()); + for (int volume = 0; volume < volumes; volume++) { + ObjectNode blockDevice = devices.addObject(); + if (device.hasNonNull("VolumeSpecification")) { + blockDevice.set("VolumeSpecification", device.get("VolumeSpecification")); + } + blockDevice.put("Device", deviceName(devices.size() - 1)); + } + } + if (!devices.isEmpty()) { + specification.set("EbsBlockDevices", devices); + } + if (ebs.hasNonNull("EbsOptimized")) { + specification.put("EbsOptimized", ebs.path("EbsOptimized").asBoolean()); + } + if (config.hasNonNull("CustomAmiId")) { + specification.put("CustomAmiId", config.path("CustomAmiId").asText()); + } + if (config.hasNonNull("Priority")) { + specification.put("Priority", config.path("Priority").asDouble()); + } + } + return specifications; + } + + /** {@code /dev/sdb}, {@code /dev/sdc} and on for the volumes of an instance, past {@code sdz} as Linux goes on. */ + static String deviceName(int index) { + StringBuilder letters = new StringBuilder(); + for (int n = index + 1; n >= 0; n = n / 26 - 1) { + letters.insert(0, (char) ('a' + n % 26)); + } + return "/dev/sd" + letters; + } + private ObjectNode syntheticInstanceNode(EmrCluster cluster, ListedInstances set, long index, long ordinal, boolean terminated) { String privateIp = EmrService.privateIpAddress(set.number(), ordinal); @@ -956,9 +1032,27 @@ public class EmrHandler { if (!violations.isEmpty()) { throw SmithyValidationErrors.validationException(violations); } + fleetsNode.forEach(EmrHandler::requireEbsVolumeLimit); return fleets; } + /** + * Refuses an InstanceTypeConfig whose EbsConfiguration attaches more volumes to an instance than the Management + * Guide allows: "There is a limit of 25 volumes per instance." AWS's error for it is not documented, so the + * ValidationException carrying that sentence is Floci's. + */ + private static void requireEbsVolumeLimit(JsonNode fleet) { + for (JsonNode config : fleet.path("InstanceTypeConfigs")) { + long volumes = 0; + for (JsonNode device : config.path("EbsConfiguration").path("EbsBlockDeviceConfigs")) { + volumes += device.path("VolumesPerInstance").asLong(1); + } + if (volumes > EBS_VOLUMES_PER_INSTANCE) { + throw new AwsException("ValidationException", "There is a limit of 25 volumes per instance.", 400); + } + } + } + /** * An InstanceFleetConfig at {@code path} in its request, as the framework names a member's place; one without its * required InstanceFleetType, or with an InstanceTypeConfigs member without its required InstanceType, adds to @@ -983,6 +1077,7 @@ public class EmrHandler { position++; } fleet.setInstanceTypes(instanceTypes); + fleet.setInstanceTypeConfigs(rawArray(f.path("InstanceTypeConfigs"))); fleet.setTargetOnDemandCapacity(f.path("TargetOnDemandCapacity").asInt(0)); fleet.setTargetSpotCapacity(f.path("TargetSpotCapacity").asInt(0)); return fleet; @@ -1054,6 +1149,20 @@ public class EmrHandler { return (node != null && node.isArray() && !node.isEmpty()) ? node.toString() : null; } + /** A stored raw JSON array, or null when there is none or it cannot be read. */ + private JsonNode readRawArray(String rawJson) { + if (rawJson == null) { + return null; + } + try { + JsonNode node = objectMapper.readTree(rawJson); + return node.isArray() ? node : null; + } catch (JsonProcessingException e) { + LOG.warnv(e, "Failed to parse stored JSON array; ignored. Value: {0}", rawJson); + return null; + } + } + private void setRawJson(ObjectNode target, String field, String rawJson) { if (rawJson == null) { return; diff --git a/src/main/java/io/floci/aws/services/emr/model/EmrInstanceFleet.java b/src/main/java/io/floci/aws/services/emr/model/EmrInstanceFleet.java index 619aa5527e..3843ee47d1 100644 --- a/src/main/java/io/floci/aws/services/emr/model/EmrInstanceFleet.java +++ b/src/main/java/io/floci/aws/services/emr/model/EmrInstanceFleet.java @@ -13,6 +13,7 @@ public class EmrInstanceFleet { private String name; private String instanceFleetType; private List instanceTypes = new ArrayList<>(); + private String instanceTypeConfigs; private int targetOnDemandCapacity; private int targetSpotCapacity; private int provisionedOnDemandCapacity; @@ -34,6 +35,10 @@ public class EmrInstanceFleet { public List getInstanceTypes() { return instanceTypes; } public void setInstanceTypes(List instanceTypes) { this.instanceTypes = instanceTypes; } + /** Its InstanceTypeConfigs as given, raw JSON; null for a fleet stored before they were kept. */ + public String getInstanceTypeConfigs() { return instanceTypeConfigs; } + public void setInstanceTypeConfigs(String instanceTypeConfigs) { this.instanceTypeConfigs = instanceTypeConfigs; } + public int getTargetOnDemandCapacity() { return targetOnDemandCapacity; } public void setTargetOnDemandCapacity(int targetOnDemandCapacity) { this.targetOnDemandCapacity = targetOnDemandCapacity; diff --git a/src/test/java/io/floci/aws/services/emr/EmrInstanceTypeSpecificationsIntegrationTest.java b/src/test/java/io/floci/aws/services/emr/EmrInstanceTypeSpecificationsIntegrationTest.java new file mode 100644 index 0000000000..8129aa269d --- /dev/null +++ b/src/test/java/io/floci/aws/services/emr/EmrInstanceTypeSpecificationsIntegrationTest.java @@ -0,0 +1,131 @@ +package io.floci.aws.services.emr; + +import io.floci.aws.testing.PartitionCleanup; +import io.floci.aws.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.util.List; + +import static io.floci.aws.testing.EmrRequests.emr; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@code ListInstanceFleets} gives each fleet's {@code InstanceTypeConfigs} back as its + * {@code InstanceTypeSpecifications}, with the defaults the API reference gives where a config leaves them out, which is + * what a client reading a fleet's configuration back compares its own against. + */ +@QuarkusTest +class EmrInstanceTypeSpecificationsIntegrationTest { + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void eachInstanceTypeConfigIsListedAsTheSpecificationItDescribes() { + String id = cluster(""" + [{"InstanceFleetType": "MASTER", "TargetOnDemandCapacity": 1, + "InstanceTypeConfigs": [{"InstanceType": "m5.xlarge"}]}, + {"InstanceFleetType": "CORE", "TargetOnDemandCapacity": 2, "TargetSpotCapacity": 4, + "InstanceTypeConfigs": [ + {"InstanceType": "r5.2xlarge", "WeightedCapacity": 2, "BidPrice": "0.5", + "CustomAmiId": "ami-0123456789abcdef0", "Priority": 1.0, + "Configurations": [{"Classification": "spark", "Properties": {"maximizeResourceAllocation": "true"}}], + "EbsConfiguration": {"EbsOptimized": true, "EbsBlockDeviceConfigs": [ + {"VolumeSpecification": {"VolumeType": "gp3", "SizeInGB": 64, "Iops": 3000, "Throughput": 125}, + "VolumesPerInstance": 2}, + {"VolumeSpecification": {"VolumeType": "gp2", "SizeInGB": 32}}]}}, + {"InstanceType": "m5.2xlarge", "BidPriceAsPercentageOfOnDemandPrice": 60}]}]"""); + + String master = "InstanceFleets.find { it.InstanceFleetType == 'MASTER' }.InstanceTypeSpecifications"; + String core = "InstanceFleets.find { it.InstanceFleetType == 'CORE' }.InstanceTypeSpecifications"; + ValidatableResponse fleets = emr("ListInstanceFleets", "{\"ClusterId\":\"" + id + "\"}").then().statusCode(200); + fleets.body(master, hasSize(1)) + .body(master + "[0].InstanceType", equalTo("m5.xlarge")) + .body(master + "[0].WeightedCapacity", equalTo(1)) + .body(master + "[0].BidPriceAsPercentageOfOnDemandPrice", equalTo(100.0f)) + .body(master + "[0]", not(hasKey("BidPrice"))) + .body(master + "[0]", not(hasKey("EbsBlockDevices"))) + .body(core, hasSize(2)) + .body(core + "[0].InstanceType", equalTo("r5.2xlarge")) + .body(core + "[0].WeightedCapacity", equalTo(2)) + .body(core + "[0].BidPrice", equalTo("0.5")) + .body(core + "[0]", not(hasKey("BidPriceAsPercentageOfOnDemandPrice"))) + .body(core + "[0].CustomAmiId", equalTo("ami-0123456789abcdef0")) + .body(core + "[0].Priority", equalTo(1.0f)) + .body(core + "[0].Configurations[0].Classification", equalTo("spark")) + // GPath answers null for a key named Properties read as ".Properties" or "['Properties']". + .body(core + "[0].Configurations[0].get('Properties').maximizeResourceAllocation", equalTo("true")) + .body(core + "[0].EbsOptimized", equalTo(true)) + .body(core + "[0].EbsBlockDevices.Device", equalTo(List.of("/dev/sdb", "/dev/sdc", "/dev/sdd"))) + .body(core + "[0].EbsBlockDevices.VolumeSpecification.VolumeType", equalTo(List.of("gp3", "gp3", "gp2"))) + .body(core + "[0].EbsBlockDevices[1].VolumeSpecification.SizeInGB", equalTo(64)) + .body(core + "[0].EbsBlockDevices[1].VolumeSpecification.Iops", equalTo(3000)) + .body(core + "[0].EbsBlockDevices[1].VolumeSpecification.Throughput", equalTo(125)) + .body(core + "[1].InstanceType", equalTo("m5.2xlarge")) + .body(core + "[1].WeightedCapacity", equalTo(1)) + .body(core + "[1].BidPriceAsPercentageOfOnDemandPrice", equalTo(60.0f)) + .body(core + "[1]", not(hasKey("BidPrice"))); + } + + @Test + void anAddedFleetListsItsSpecificationsToo() { + String id = cluster(""" + [{"InstanceFleetType": "MASTER", "TargetOnDemandCapacity": 1, + "InstanceTypeConfigs": [{"InstanceType": "m5.xlarge"}]}]"""); + String task = emr("AddInstanceFleet", "{\"ClusterId\":\"" + id + "\",\"InstanceFleet\":" + + "{\"InstanceFleetType\":\"TASK\",\"TargetSpotCapacity\":1," + + "\"InstanceTypeConfigs\":[{\"InstanceType\":\"c5.xlarge\",\"BidPrice\":\"0.2\"}]}}") + .then().statusCode(200).extract().path("InstanceFleetId"); + + String specifications = "InstanceFleets.find { it.Id == '" + task + "' }.InstanceTypeSpecifications"; + emr("ListInstanceFleets", "{\"ClusterId\":\"" + id + "\"}").then().statusCode(200) + .body(specifications + ".InstanceType", equalTo(List.of("c5.xlarge"))) + .body(specifications + "[0].BidPrice", equalTo("0.2")) + .body(specifications + "[0].WeightedCapacity", equalTo(1)); + } + + /** The Management Guide: "There is a limit of 25 volumes per instance." */ + @Test + void aConfigAttachingMoreThan25VolumesPerInstanceIsRefused() { + String tooMany = """ + [{"InstanceFleetType": "MASTER", "TargetOnDemandCapacity": 1, + "InstanceTypeConfigs": [{"InstanceType": "m5.xlarge", "EbsConfiguration": {"EbsBlockDeviceConfigs": [ + {"VolumeSpecification": {"VolumeType": "gp3", "SizeInGB": 32}, "VolumesPerInstance": 20}, + {"VolumeSpecification": {"VolumeType": "gp2", "SizeInGB": 32}, "VolumesPerInstance": 6}]}}]}]"""; + emr("RunJobFlow", "{\"Name\":\"too-many-volumes\",\"ReleaseLabel\":\"emr-7.2.0\"," + + "\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,\"InstanceFleets\":" + tooMany + "}}") + .then().statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("There is a limit of 25 volumes per instance.")); + } + + @Test + void devicesAreNamedPastSdzAsLinuxNamesThem() { + assertEquals("/dev/sdb", EmrHandler.deviceName(0)); + assertEquals("/dev/sdz", EmrHandler.deviceName(24)); + assertEquals("/dev/sdaa", EmrHandler.deviceName(25)); + assertEquals("/dev/sdab", EmrHandler.deviceName(26)); + } + + /** A cluster with {@code fleets}, terminated after the test. */ + private String cluster(String fleets) { + String id = emr("RunJobFlow", "{\"Name\":\"fleet-specifications\",\"ReleaseLabel\":\"emr-7.2.0\"," + + "\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,\"InstanceFleets\":" + fleets + "}}") + .then().statusCode(200).extract().path("JobFlowId"); + cleanup.register(() -> emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + id + "\"]}").then().statusCode(200)); + return id; + } +}