diff --git a/docs/services/emr.md b/docs/services/emr.md index 85fd8c6d46..5d7d35dbf7 100644 --- a/docs/services/emr.md +++ b/docs/services/emr.md @@ -166,6 +166,16 @@ A cluster's `SecurityConfiguration` names one `CreateSecurityConfiguration` made `RunJobFlow` naming one that does not exist is refused with `ValidationException` "Security configuration does not exist: "; no source gives AWS's answer, so the code is the one `RunJobFlow` refuses its other input with, and the wording that of `DescribeSecurityConfiguration` and `DeleteSecurityConfiguration`. +A cluster made with `RunJobFlow`'s uniform form, `MasterInstanceType`, `SlaveInstanceType` and `InstanceCount` in place +of `InstanceGroups` or `InstanceFleets`, gets the instance groups that form describes: "If the value is 1, the same +instance serves as both the master and core and task node. If the value is greater than 1, one instance is the master +node and all others are core and task nodes." That is a `MASTER` group of one `MasterInstanceType` instance and, for a +count above 1, a `CORE` group of the rest as `SlaveInstanceType`, both `ON_DEMAND`. The master group is named +`Master InstanceGroup`, as in `DescribeJobFlows`' sample response; no source names the core group, so Floci names it +`Core InstanceGroup`. A single instance needs no `SlaveInstanceType`, as AWS runs one without it, but core nodes do, as +"all three must be present": a request for more instances without it is refused with `ValidationException` +"SlaveInstanceType is required when InstanceCount is greater than 1."; AWS's wording is not documented, so it is +Floci's. A cluster has instance groups or instance fleets, never both, as "instance fleets and instance groups cannot coexist in a cluster": `RunJobFlow` with both is refused with `ValidationException` "The RunJobFlow request can contain InstanceFleets parameters or InstanceGroups parameters, but not both.", the API reference's own words, and @@ -926,8 +936,6 @@ holding the driver and its executors, or a command or a script. The cluster itse that it does not scale below them, but no error for the request. - **No YARN node labels.** An application master can run on any node, as on EMR's 6.x releases by default. EMR's 7.x releases label nodes by market type and 5.x's keep application masters on core nodes. -- **No nodes for a cluster made with `MasterInstanceType`, `SlaveInstanceType` and `InstanceCount`.** Floci keeps no - instance groups for that older form of `RunJobFlow`, so such a cluster runs on its primary node alone. - **An `emr-spark-*` cluster's HDFS and YARN are `emr-7.5.0`'s.** The Release Guide lists both at Hadoop 3.4.2 for `emr-spark-8.0.0` and `emr-spark-8.1.0`, and EMR Serverless publishes no image holding them, so the primary node runs Hadoop 3.4.0's. Spark and its Hadoop client are the release's own. 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 8309347edc..c7c9f99c93 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrHandler.java +++ b/src/main/java/io/floci/aws/services/emr/EmrHandler.java @@ -49,6 +49,11 @@ 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; + // RunJobFlow's uniform form's instance groups (see uniformInstanceGroups). + static final String UNIFORM_MASTER_GROUP = "Master InstanceGroup"; + static final String UNIFORM_CORE_GROUP = "Core InstanceGroup"; + static final String NO_CORE_INSTANCE_TYPE = + "SlaveInstanceType is required when InstanceCount is greater than 1."; // 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; @@ -199,8 +204,10 @@ public class EmrHandler { cluster.setUnhealthyNodeReplacement(instances.path("UnhealthyNodeReplacement").asBoolean(false)); cluster.setSessionEnabled(request.path("SessionEnabled").asBoolean(false)); cluster.setEc2Attributes(buildEc2Attributes(instances, region)); - cluster.setInstanceGroups(parseInstanceGroups(instances.path("InstanceGroups"))); - cluster.setInstanceFleets(parseInstanceFleets(instances.path("InstanceFleets"))); + List groups = parseInstanceGroups(instances.path("InstanceGroups")); + List fleets = parseInstanceFleets(instances.path("InstanceFleets")); + cluster.setInstanceGroups(groups.isEmpty() && fleets.isEmpty() ? uniformInstanceGroups(instances) : groups); + cluster.setInstanceFleets(fleets); cluster.setSteps(parseSteps(request.path("Steps"), text(request, "StepExecutionRoleArn"))); EmrCluster created = service.runJobFlow(cluster, region); @@ -1721,6 +1728,45 @@ public class EmrHandler { return groups; } + /** + * The instance groups of RunJobFlow's uniform form, a cluster given as its MasterInstanceType, SlaveInstanceType + * and InstanceCount: "If the value is 1, the same instance serves as both the master and core and task node. If the + * value is greater than 1, one instance is the master node and all others are core and task nodes." None without a + * MasterInstanceType. The master group's name is the one DescribeJobFlows' sample response gives it; no source + * names the core group, so its name follows the master's. + * + *

A single instance needs no SlaveInstanceType, as AWS runs one without it, but core nodes do, as "all three + * must be present": a request for more without it is refused with a ValidationException, whose wording, AWS's not + * being documented, is Floci's. + */ + static List uniformInstanceGroups(JsonNode instances) { + List groups = new ArrayList<>(); + String masterType = instances.path("MasterInstanceType").asText(null); + if (masterType == null) { + return groups; + } + groups.add(uniformInstanceGroup(UNIFORM_MASTER_GROUP, "MASTER", masterType, 1)); + int count = instances.path("InstanceCount").asInt(1); + if (count > 1) { + String coreType = instances.path("SlaveInstanceType").asText(null); + if (coreType == null || coreType.isBlank()) { + throw new AwsException("ValidationException", NO_CORE_INSTANCE_TYPE, 400); + } + groups.add(uniformInstanceGroup(UNIFORM_CORE_GROUP, "CORE", coreType, count - 1)); + } + return groups; + } + + private static EmrInstanceGroup uniformInstanceGroup(String name, String role, String instanceType, int count) { + EmrInstanceGroup group = new EmrInstanceGroup(); + group.setName(name); + group.setInstanceGroupType(role); + group.setInstanceType(instanceType); + group.setMarket("ON_DEMAND"); + group.setRequestedInstanceCount(count); + return group; + } + private List parseInstanceFleets(JsonNode fleetsNode) { List fleets = new ArrayList<>(); if (!fleetsNode.isArray()) { 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 33c8bab84c..eac279aee7 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrService.java @@ -3049,40 +3049,47 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear // A cluster's core and task nodes as containers: "Core nodes run the Data Node daemon" and "a core node runs YARN // NodeManager daemons", and "Task nodes don't run the Data Node daemon". No more than the setting allows, core // nodes first, as they hold HDFS, and no fewer core nodes than HDFS keeps copies, as "Amazon EMR will not allow - // clusters to scale core nodes below dfs.replication". A fleet's unit of capacity is one node. - private List workerNodes(EmrCluster cluster) { + // clusters to scale core nodes below dfs.replication". A fleet's unit of capacity is one node. Neither kind is + // built past the setting, however many a cluster asks for. + List workerNodes(EmrCluster cluster) { int floor = Math.min(cluster.getProvisionedCoreNodes(), replication(cluster)); List core = new ArrayList<>(); List task = new ArrayList<>(); + long asked = 0; for (EmrInstanceGroup group : cluster.getInstanceGroups()) { boolean isCore = "CORE".equals(group.getInstanceGroupType()); if (isCore || "TASK".equals(group.getInstanceGroupType())) { int count = group.getRequestedInstanceCount(); - addNodes(cluster, groupNumber(cluster.getInstanceGroups(), group), isCore ? Math.max(count, floor) : count, - isCore, isCore ? core : task); + count = isCore ? Math.max(count, floor) : count; + asked += Math.max(count, 0); + addNodes(cluster, groupNumber(cluster.getInstanceGroups(), group), count, isCore, isCore ? core : task, + maxWorkerNodes); } } for (EmrInstanceFleet fleet : cluster.getInstanceFleets()) { int number = fleetNumber(cluster.getInstanceFleets(), fleet); if (number > 0) { boolean isCore = "CORE".equals(fleet.getInstanceFleetType()); - int count = fleet.getTargetOnDemandCapacity() + fleet.getTargetSpotCapacity(); - addNodes(cluster, number, isCore ? Math.max(count, floor) : count, isCore, isCore ? core : task); + long capacity = (long) fleet.getTargetOnDemandCapacity() + fleet.getTargetSpotCapacity(); + long count = isCore ? Math.max(capacity, floor) : capacity; + asked += Math.max(count, 0); + addNodes(cluster, number, count, isCore, isCore ? core : task, maxWorkerNodes); } } List nodes = new ArrayList<>(core); nodes.addAll(task); - if (nodes.size() > maxWorkerNodes) { + if (asked > maxWorkerNodes) { LOG.infov("EMR cluster {0} asks for {1} core and task nodes, and runs the first {2}, as yarn-max-worker-nodes" - + " says", cluster.getId(), nodes.size(), maxWorkerNodes); - return List.copyOf(nodes.subList(0, maxWorkerNodes)); + + " says", cluster.getId(), asked, maxWorkerNodes); + return List.copyOf(nodes.subList(0, Math.min(nodes.size(), maxWorkerNodes))); } return nodes; } - private static void addNodes(EmrCluster cluster, int group, int count, boolean core, - List nodes) { - for (int ordinal = 1; ordinal <= count; ordinal++) { + // Up to count nodes of the group, as long as the list holds fewer than limit. + private static void addNodes(EmrCluster cluster, int group, long count, boolean core, + List nodes, int limit) { + for (int ordinal = 1; ordinal <= count && nodes.size() < limit; ordinal++) { String id = group + "-" + ordinal; nodes.add(new ClusterStorage.Node(id, AwsRegions.ec2PrivateIpDnsName(privateIpAddress(group, ordinal), cluster.getRegion()), core, diff --git a/src/test/java/io/floci/aws/services/emr/EmrIntegrationTest.java b/src/test/java/io/floci/aws/services/emr/EmrIntegrationTest.java index f36274017a..551787531a 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrIntegrationTest.java @@ -570,4 +570,65 @@ class EmrIntegrationTest { .then().statusCode(200); } } + + @Test + @Order(21) + void aClusterGivenAsItsInstanceTypesAndCountGetsAMasterGroupAndACoreGroupOfTheRest() { + String uniform = emr("RunJobFlow", "{\"Name\":\"uniform\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true," + + "\"MasterInstanceType\":\"m5.xlarge\",\"SlaveInstanceType\":\"m5.large\",\"InstanceCount\":3}}") + .then().statusCode(200).extract().path("JobFlowId"); + // One instance serves as both the master and the core node. + String single = emr("RunJobFlow", "{\"Name\":\"single\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true," + + "\"MasterInstanceType\":\"m5.xlarge\",\"InstanceCount\":1}}") + .then().statusCode(200).extract().path("JobFlowId"); + // Instance groups given are the cluster's, whatever else it names. + String grouped = emr("RunJobFlow", "{\"Name\":\"grouped\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true," + + "\"MasterInstanceType\":\"m5.xlarge\",\"SlaveInstanceType\":\"m5.large\",\"InstanceCount\":3," + + "\"InstanceGroups\":[{\"Name\":\"only\",\"InstanceRole\":\"MASTER\"," + + "\"InstanceType\":\"m5.2xlarge\",\"InstanceCount\":1}]}}") + .then().statusCode(200).extract().path("JobFlowId"); + String master = "InstanceGroups.find { it.InstanceGroupType == 'MASTER' }."; + String core = "InstanceGroups.find { it.InstanceGroupType == 'CORE' }."; + // Core nodes need their instance type: "all three must be present". + emr("RunJobFlow", "{\"Name\":\"no-core-type\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true," + + "\"MasterInstanceType\":\"m5.xlarge\",\"InstanceCount\":3}}") + .then().statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("SlaveInstanceType is required when InstanceCount is greater than 1.")); + emr("ListClusters", "{}") + .then().statusCode(200) + .body("Clusters.findAll { it.Name == 'no-core-type' }", hasSize(0)); + try { + emr("ListInstanceGroups", "{\"ClusterId\":\"" + uniform + "\"}") + .then().statusCode(200) + .body("InstanceGroups", hasSize(2)) + .body(master + "Name", equalTo("Master InstanceGroup")) + .body(master + "InstanceType", equalTo("m5.xlarge")) + .body(master + "Market", equalTo("ON_DEMAND")) + .body(master + "RequestedInstanceCount", equalTo(1)) + .body(core + "Name", equalTo("Core InstanceGroup")) + .body(core + "InstanceType", equalTo("m5.large")) + .body(core + "Market", equalTo("ON_DEMAND")) + .body(core + "RequestedInstanceCount", equalTo(2)); + emr("ListInstances", "{\"ClusterId\":\"" + uniform + "\"}") + .then().statusCode(200) + .body("Instances", hasSize(3)); + emr("DescribeCluster", "{\"ClusterId\":\"" + uniform + "\"}") + .then().statusCode(200) + .body("Cluster.InstanceCollectionType", equalTo("INSTANCE_GROUP")); + emr("ListInstanceGroups", "{\"ClusterId\":\"" + single + "\"}") + .then().statusCode(200) + .body("InstanceGroups", hasSize(1)) + .body(master + "Name", equalTo("Master InstanceGroup")) + .body(master + "RequestedInstanceCount", equalTo(1)); + emr("ListInstanceGroups", "{\"ClusterId\":\"" + grouped + "\"}") + .then().statusCode(200) + .body("InstanceGroups", hasSize(1)) + .body(master + "Name", equalTo("only")) + .body(master + "InstanceType", equalTo("m5.2xlarge")); + } finally { + emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + uniform + "\",\"" + single + "\",\"" + grouped + + "\"]}").then().statusCode(200); + } + } } 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 8be1bbcd39..ebeb93bd49 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java @@ -800,6 +800,25 @@ class EmrServiceTest { assertEquals(Map.of("ErrorCode", "INVALID_JOB_SHUT_DOWN"), refused.getExtendedData()); } + /** + * No more core and task nodes are built than {@code yarn-max-worker-nodes}, 4 in the fixture, however many a + * cluster asks for: core nodes first, and none past the setting, so a cluster asking for billions costs no more + * than one asking for 4. + */ + @Test + void noMoreWorkerNodesAreBuiltThanTheSettingAllowsHoweverManyAClusterAsksFor() { + EmrServiceFixture fixture = new EmrServiceFixture(0, 0, 0); + EmrCluster requested = cluster("huge", true); + requested.getInstanceGroups().addAll(List.of(group("MASTER", 1), group("CORE", Integer.MAX_VALUE), + group("TASK", Integer.MAX_VALUE))); + EmrCluster cluster = fixture.service.runJobFlow(requested, REGION); + + List nodes = fixture.service.workerNodes(cluster); + + assertEquals(4, nodes.size()); + assertTrue(nodes.stream().allMatch(ClusterStorage.Node::core), nodes.toString()); + } + /** ModifyInstanceFleet's answer for a cluster that is starting or bootstrapping. */ private static void assertNotRunningOrWaiting(AwsException refused) { assertEquals("InvalidRequestException", refused.getErrorCode());