fix(emr): make a uniform RunJobFlow cluster's master and core instance groups (#9107)

RunJobFlow's uniform form, MasterInstanceType, SlaveInstanceType and
InstanceCount in place of InstanceGroups or InstanceFleets, made no
instance groups, so ListInstanceGroups listed none, ListInstances no
instances, and with runner-mode docker the cluster ran on its primary
node alone. It now makes the groups the API reference 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 request that gives
InstanceGroups or InstanceFleets keeps its own.

A request for more than one instance without SlaveInstanceType is
refused with a ValidationException, as JobFlowInstancesConfig says "all
three must be present"; a single instance needs none, as AWS runs one
without it. However many instances a cluster asks for, in this form or
any other, its core and task nodes are now built no further than
yarn-max-worker-nodes, where every one asked for was built before the
rest were dropped.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-20 15:03:53 -04:00
1 parent c9aad84192
commit 6b420b166f
5 files changed
+157 -16

No files matched your search

@@ -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<EmrInstanceGroup> groups = parseInstanceGroups(instances.path("InstanceGroups"));
List<EmrInstanceFleet> 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.
*
* <p>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<EmrInstanceGroup> uniformInstanceGroups(JsonNode instances) {
List<EmrInstanceGroup> 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<EmrInstanceFleet> parseInstanceFleets(JsonNode fleetsNode) {
List<EmrInstanceFleet> fleets = new ArrayList<>();
if (!fleetsNode.isArray()) {
@@ -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<ClusterStorage.Node> 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<ClusterStorage.Node> workerNodes(EmrCluster cluster) {
int floor = Math.min(cluster.getProvisionedCoreNodes(), replication(cluster));
List<ClusterStorage.Node> core = new ArrayList<>();
List<ClusterStorage.Node> 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<ClusterStorage.Node> 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<ClusterStorage.Node> 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<ClusterStorage.Node> 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,