mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(emr): keep instance groups' volumes, AMI and shrink policy, and list instances' volumes and public address (#9163)
An instance group dropped the EbsConfiguration and CustomAmiId that RunJobFlow and AddInstanceGroups gave it, and ModifyInstanceGroups its ShrinkPolicy, and ListInstances gave no instance an EbsVolumes or a PublicIpAddress. - A group keeps its EbsConfiguration and CustomAmiId, and ListInstanceGroups gives them back as its EbsBlockDevices, one per volume named /dev/sdb on as a fleet's are, its EbsOptimized and its CustomAmiId, under the same limit of 25 volumes per instance. - ModifyInstanceGroups keeps a group's ShrinkPolicy, which ListInstanceGroups gives back, and a shrink it comes with follows its InstanceResizePolicy: the instances it names to terminate end first, then the group's last instances, passing the protected ones. They end as EC2InstanceIdsToTerminate ends them, so their ids are not taken again, and a later entry for the same group counts from what an earlier one leaves. An id to terminate that is not a running instance of the group is refused, as EC2InstanceIdsToTerminate refuses it, and so is a shrink the protected instances leave short. - ListInstances gives each instance the PublicIpAddress its PublicDnsName names, in 203.0.113.0/24 for a cluster's first 254 instances and in 100.64.0.0/10 past them, and an EbsVolumes entry for each device its group or instance type config gives it, with volume ids that differ between clusters. A group's instances' EC2 descriptions give its CustomAmiId as their image. - AWS::EMR::Cluster no longer warns that a group's EbsConfiguration and CustomAmiId are not stored.
This commit is contained in:
1 parent
6fe1da3219
commit
5a912d5263
10 files changed
+436
-57
No files matched your search
+2
-11
@@ -109,8 +109,7 @@ public class EmrCfnProvisioner implements CfnResourceProvisioner {
|
||||
/** The members of {@code Instances} Floci's RunJobFlow keeps nothing of, accepted with a warning. */
|
||||
private static final List<String> INSTANCES_NOT_STORED = List.of("Ec2SubnetIds", "AdditionalMasterSecurityGroups",
|
||||
"AdditionalSlaveSecurityGroups", "HadoopVersion");
|
||||
/** The members of an instance group, and of an instance fleet, Floci's RunJobFlow keeps nothing of. */
|
||||
private static final List<String> GROUP_NOT_STORED = List.of("EbsConfiguration", "CustomAmiId");
|
||||
/** The members of an instance fleet Floci's RunJobFlow keeps nothing of. */
|
||||
private static final List<String> FLEET_NOT_STORED = List.of("LaunchSpecifications", "ResizeSpecifications");
|
||||
private static final String GROUPS = "groups";
|
||||
private static final String FLEETS = "fleets";
|
||||
@@ -299,7 +298,7 @@ public class EmrCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
/** Warns of each property, or member of {@code Instances}, its groups or fleets, RunJobFlow keeps nothing of. */
|
||||
/** Warns of each property, or member of {@code Instances} or its fleets, RunJobFlow keeps nothing of. */
|
||||
private static void warnNotStored(String logicalId, JsonNode template) {
|
||||
List<String> ignored = new ArrayList<>();
|
||||
for (String name : CLUSTER_NOT_STORED) {
|
||||
@@ -313,17 +312,9 @@ public class EmrCfnProvisioner implements CfnResourceProvisioner {
|
||||
ignored.add("Instances." + name);
|
||||
}
|
||||
}
|
||||
List<JsonNode> groups = new ArrayList<>(List.of(instances.path("MasterInstanceGroup"),
|
||||
instances.path("CoreInstanceGroup")));
|
||||
instances.path("TaskInstanceGroups").forEach(groups::add);
|
||||
List<JsonNode> fleets = new ArrayList<>(List.of(instances.path("MasterInstanceFleet"),
|
||||
instances.path("CoreInstanceFleet")));
|
||||
instances.path("TaskInstanceFleets").forEach(fleets::add);
|
||||
for (String name : GROUP_NOT_STORED) {
|
||||
if (groups.stream().anyMatch(group -> group.has(name))) {
|
||||
ignored.add("an instance group's " + name);
|
||||
}
|
||||
}
|
||||
for (String name : FLEET_NOT_STORED) {
|
||||
if (fleets.stream().anyMatch(fleet -> fleet.has(name))) {
|
||||
ignored.add("an instance fleet's " + name);
|
||||
|
||||
@@ -1003,7 +1003,7 @@ public class EmrHandler {
|
||||
if (!violations.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(violations);
|
||||
}
|
||||
requireEbsVolumeLimit(fleetNode);
|
||||
requireFleetEbsVolumeLimit(fleetNode);
|
||||
return fleet;
|
||||
}
|
||||
|
||||
@@ -1065,13 +1065,14 @@ public class EmrHandler {
|
||||
}
|
||||
long first = EmrService.firstIndex(listed, set);
|
||||
PrimitiveIterator.OfLong ordinals = set.ordinalsFrom(skip + 1);
|
||||
ArrayNode devices = ebsBlockDevices(ebsConfigurationOf(cluster, set));
|
||||
for (long k = skip + 1; k <= set.count(); k++) {
|
||||
if (page == INSTANCES_PER_PAGE) {
|
||||
response.put("Marker", String.valueOf(start + page));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
long ordinal = ordinals.nextLong();
|
||||
arr.add(syntheticInstanceNode(cluster, set, first + ordinal - 1, ordinal, terminated));
|
||||
arr.add(syntheticInstanceNode(cluster, set, first + ordinal - 1, ordinal, terminated, devices));
|
||||
page++;
|
||||
}
|
||||
skip = 0;
|
||||
@@ -1348,11 +1349,13 @@ public class EmrHandler {
|
||||
for (JsonNode g : groups) {
|
||||
JsonNode count = g.path("InstanceCount");
|
||||
JsonNode configurations = g.path("Configurations");
|
||||
JsonNode shrink = g.path("ShrinkPolicy");
|
||||
modifications.add(new EmrService.InstanceGroupModification(
|
||||
g.path("InstanceGroupId").asText(null),
|
||||
count.isMissingNode() || count.isNull() ? null : count.asInt(),
|
||||
configurations.isArray() ? configurations.toString() : null,
|
||||
g.path("ReconfigurationType").asText(null), instanceIds(g.path("EC2InstanceIdsToTerminate"))));
|
||||
g.path("ReconfigurationType").asText(null), instanceIds(g.path("EC2InstanceIdsToTerminate")),
|
||||
shrink.isObject() ? shrink : null));
|
||||
}
|
||||
}
|
||||
service.modifyInstanceGroups(text(request, "ClusterId"), region, modifications);
|
||||
@@ -1611,9 +1614,46 @@ public class EmrHandler {
|
||||
setRawJson(node, "LastSuccessfullyAppliedConfigurations", g.getConfigurations());
|
||||
node.put("LastSuccessfullyAppliedConfigurationsVersion", g.getConfigurationsVersion());
|
||||
}
|
||||
JsonNode ebs = readRawObject(g.getEbsConfiguration());
|
||||
if (ebs != null) {
|
||||
ArrayNode devices = ebsBlockDevices(ebs);
|
||||
if (!devices.isEmpty()) {
|
||||
node.set("EbsBlockDevices", devices);
|
||||
}
|
||||
if (ebs.hasNonNull("EbsOptimized")) {
|
||||
node.put("EbsOptimized", ebs.path("EbsOptimized").asBoolean());
|
||||
}
|
||||
}
|
||||
if (g.getCustomAmiId() != null) {
|
||||
node.put("CustomAmiId", g.getCustomAmiId());
|
||||
}
|
||||
if (g.getShrinkPolicy() != null) {
|
||||
setRawJson(node, "ShrinkPolicy", g.getShrinkPolicy());
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* An EbsConfiguration as the EbsBlockDevices it gives each instance: one per volume, the way VolumesPerInstance
|
||||
* counts them, with its VolumeSpecification, named {@code /dev/sdb} on, as the reference gives no device names.
|
||||
*/
|
||||
private ArrayNode ebsBlockDevices(JsonNode ebs) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
/** An AutoScalingPolicyDescription: the stored Constraints and Rules with an ATTACHED status. */
|
||||
private ObjectNode autoScalingPolicyNode(String rawPolicy) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
@@ -1677,19 +1717,7 @@ public class EmrHandler {
|
||||
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));
|
||||
}
|
||||
}
|
||||
ArrayNode devices = ebsBlockDevices(ebs);
|
||||
if (!devices.isEmpty()) {
|
||||
specification.set("EbsBlockDevices", devices);
|
||||
}
|
||||
@@ -1715,13 +1743,15 @@ public class EmrHandler {
|
||||
return "/dev/sd" + letters;
|
||||
}
|
||||
|
||||
/** An instance of {@code set}, whose EBS block {@code devices} each instance of the set has. */
|
||||
private ObjectNode syntheticInstanceNode(EmrCluster cluster, ListedInstances set, long index, long ordinal,
|
||||
boolean terminated) {
|
||||
boolean terminated, ArrayNode devices) {
|
||||
String privateIp = EmrService.privateIpAddress(set.number(), ordinal);
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("Id", "ci-" + index + cluster.getId());
|
||||
node.put("Ec2InstanceId", EmrService.ec2InstanceId(index));
|
||||
node.put("PublicDnsName", EmrService.publicDnsName(index, cluster.getRegion()));
|
||||
node.put("PublicIpAddress", EmrService.publicIpAddress(index));
|
||||
node.put("PrivateDnsName", AwsRegions.ec2PrivateIpDnsName(privateIp, cluster.getRegion()));
|
||||
node.put("PrivateIpAddress", privateIp);
|
||||
if (set.groupId() != null) {
|
||||
@@ -1737,9 +1767,44 @@ public class EmrHandler {
|
||||
node.put("InstanceType", set.instanceType());
|
||||
}
|
||||
node.putObject("Status").put("State", set.state());
|
||||
if (!devices.isEmpty()) {
|
||||
ArrayNode volumes = node.putArray("EbsVolumes");
|
||||
for (int device = 0; device < devices.size(); device++) {
|
||||
volumes.addObject().put("Device", devices.get(device).path("Device").asText())
|
||||
.put("VolumeId", EmrService.ebsVolumeId(cluster.getId(), index, device));
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* The EbsConfiguration an instance of {@code set} was given: its group's, or, in a fleet, that of the
|
||||
* InstanceTypeConfig of its instance type. An empty one for none.
|
||||
*/
|
||||
private JsonNode ebsConfigurationOf(EmrCluster cluster, ListedInstances set) {
|
||||
if (set.groupId() != null) {
|
||||
for (EmrInstanceGroup group : cluster.getInstanceGroups()) {
|
||||
if (set.groupId().equals(group.getId())) {
|
||||
JsonNode ebs = readRawObject(group.getEbsConfiguration());
|
||||
return ebs == null ? objectMapper.createObjectNode() : ebs;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (set.fleetId() != null) {
|
||||
for (EmrInstanceFleet fleet : cluster.getInstanceFleets()) {
|
||||
if (set.fleetId().equals(fleet.getId())) {
|
||||
JsonNode configs = readRawArray(fleet.getInstanceTypeConfigs());
|
||||
for (JsonNode config : configs == null ? objectMapper.createArrayNode() : configs) {
|
||||
if (config.path("InstanceType").asText("").equals(set.instanceType())) {
|
||||
return config.path("EbsConfiguration");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return objectMapper.createObjectNode();
|
||||
}
|
||||
|
||||
private ArrayNode tagArray(Map<String, String> tags) {
|
||||
ArrayNode out = objectMapper.createArrayNode();
|
||||
tags.forEach((k, v) -> {
|
||||
@@ -1802,6 +1867,12 @@ public class EmrHandler {
|
||||
EmrService.validateAutoScalingPolicy(autoScaling, group.getInstanceGroupType());
|
||||
group.setAutoScalingPolicy(autoScaling.toString());
|
||||
}
|
||||
JsonNode ebs = g.path("EbsConfiguration");
|
||||
if (ebs.isObject()) {
|
||||
requireEbsVolumeLimit(ebs);
|
||||
group.setEbsConfiguration(ebs.toString());
|
||||
}
|
||||
group.setCustomAmiId(g.path("CustomAmiId").asText(null));
|
||||
groups.add(group);
|
||||
}
|
||||
return groups;
|
||||
@@ -1859,7 +1930,7 @@ public class EmrHandler {
|
||||
if (!violations.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(violations);
|
||||
}
|
||||
fleetsNode.forEach(EmrHandler::requireEbsVolumeLimit);
|
||||
fleetsNode.forEach(EmrHandler::requireFleetEbsVolumeLimit);
|
||||
return fleets;
|
||||
}
|
||||
|
||||
@@ -1868,15 +1939,20 @@ public class EmrHandler {
|
||||
* 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) {
|
||||
private static void requireFleetEbsVolumeLimit(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);
|
||||
}
|
||||
requireEbsVolumeLimit(config.path("EbsConfiguration"));
|
||||
}
|
||||
}
|
||||
|
||||
/** An EbsConfiguration's volumes, counted as VolumesPerInstance counts them, hold to AWS's limit per instance. */
|
||||
private static void requireEbsVolumeLimit(JsonNode ebs) {
|
||||
long volumes = 0;
|
||||
for (JsonNode device : ebs.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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1998,6 +2074,20 @@ public class EmrHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/** A stored raw JSON object, or null when there is none or it cannot be read. */
|
||||
private JsonNode readRawObject(String rawJson) {
|
||||
if (rawJson == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JsonNode node = objectMapper.readTree(rawJson);
|
||||
return node.isObject() ? node : null;
|
||||
} catch (JsonProcessingException e) {
|
||||
LOG.warnv(e, "Failed to parse stored JSON object; ignored. Value: {0}", rawJson);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void setRawJson(ObjectNode target, String field, String rawJson) {
|
||||
if (rawJson == null) {
|
||||
return;
|
||||
|
||||
@@ -1463,15 +1463,21 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
* reconfiguration type is OVERWRITE, and {@code instancesToTerminate} are EC2InstanceIdsToTerminate, none for null.
|
||||
*/
|
||||
public record InstanceGroupModification(String instanceGroupId, Integer instanceCount, String configurations,
|
||||
String reconfigurationType, List<String> instancesToTerminate) {
|
||||
String reconfigurationType, List<String> instancesToTerminate,
|
||||
JsonNode shrinkPolicy) {
|
||||
|
||||
public InstanceGroupModification(String instanceGroupId, Integer instanceCount, String configurations) {
|
||||
this(instanceGroupId, instanceCount, configurations, null, null);
|
||||
this(instanceGroupId, instanceCount, configurations, null, null, null);
|
||||
}
|
||||
|
||||
public InstanceGroupModification(String instanceGroupId, Integer instanceCount, String configurations,
|
||||
String reconfigurationType) {
|
||||
this(instanceGroupId, instanceCount, configurations, reconfigurationType, null);
|
||||
this(instanceGroupId, instanceCount, configurations, reconfigurationType, null, null);
|
||||
}
|
||||
|
||||
public InstanceGroupModification(String instanceGroupId, Integer instanceCount, String configurations,
|
||||
String reconfigurationType, List<String> instancesToTerminate) {
|
||||
this(instanceGroupId, instanceCount, configurations, reconfigurationType, instancesToTerminate, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1491,6 +1497,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
Map<String, EmrCluster> touched = new LinkedHashMap<>();
|
||||
List<EmrInstanceGroup> groups = new ArrayList<>();
|
||||
List<List<Integer>> terminations = new ArrayList<>();
|
||||
Map<String, Set<Integer>> endings = new HashMap<>();
|
||||
for (int position = 1; position <= modifications.size(); position++) {
|
||||
InstanceGroupModification mod = modifications.get(position - 1);
|
||||
EmrCluster found = clusterId != null
|
||||
@@ -1514,7 +1521,19 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
throw invalid("The instance count of a master instance group cannot be modified.");
|
||||
}
|
||||
}
|
||||
terminations.add(ordinalsToTerminate(cluster, group, mod.instancesToTerminate()));
|
||||
List<Integer> named = ordinalsToTerminate(cluster, group, mod.instancesToTerminate());
|
||||
List<Integer> ending = new ArrayList<>(named);
|
||||
// What the request's earlier entries for the same group end, so a shrink counts from what they leave.
|
||||
Set<Integer> endingOfGroup = endings.computeIfAbsent(cluster.getId() + "/" + group.getId(),
|
||||
key -> new LinkedHashSet<>());
|
||||
endingOfGroup.addAll(named);
|
||||
if (mod.instanceCount() != null && mod.shrinkPolicy() != null) {
|
||||
List<Integer> shrink = shrinkOrdinals(cluster, group, mod.instanceCount(), mod.shrinkPolicy(),
|
||||
endingOfGroup);
|
||||
ending.addAll(shrink);
|
||||
endingOfGroup.addAll(shrink);
|
||||
}
|
||||
terminations.add(ending);
|
||||
groups.add(group);
|
||||
}
|
||||
for (int i = 0; i < modifications.size(); i++) {
|
||||
@@ -1535,6 +1554,9 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
group.setRequestedInstanceCount(mod.instanceCount());
|
||||
group.setRunningInstanceCount(mod.instanceCount());
|
||||
}
|
||||
if (mod.shrinkPolicy() != null) {
|
||||
group.setShrinkPolicy(mod.shrinkPolicy().toString());
|
||||
}
|
||||
if (mod.configurations() != null) {
|
||||
group.setConfigurations(MERGE.equals(mod.reconfigurationType())
|
||||
? mergedConfigurations(group.getConfigurations(), mod.configurations())
|
||||
@@ -1561,18 +1583,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
if ("MASTER".equals(group.getInstanceGroupType())) {
|
||||
throw invalid("The instance count of a master instance group cannot be modified.");
|
||||
}
|
||||
List<EmrHandler.ListedInstances> listed = EmrHandler.listedInstances(cluster, false);
|
||||
Map<String, Integer> current = new HashMap<>();
|
||||
for (EmrHandler.ListedInstances set : listed) {
|
||||
if (group.getId().equals(set.groupId())) {
|
||||
long first = firstIndex(listed, set);
|
||||
PrimitiveIterator.OfLong ordinals = set.ordinalsFrom(1);
|
||||
for (long k = 1; k <= set.count(); k++) {
|
||||
long ordinal = ordinals.nextLong();
|
||||
current.put(ec2InstanceId(first + ordinal - 1), (int) ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, Integer> current = runningOrdinals(cluster, group);
|
||||
List<Integer> ordinals = new ArrayList<>();
|
||||
for (String instanceId : new LinkedHashSet<>(instanceIds)) {
|
||||
Integer ordinal = current.get(instanceId);
|
||||
@@ -1585,6 +1596,71 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
return ordinals;
|
||||
}
|
||||
|
||||
/** The group's instances as ListInstances gives them now: each EC2 instance id and its ordinal, in that order. */
|
||||
private static Map<String, Integer> runningOrdinals(EmrCluster cluster, EmrInstanceGroup group) {
|
||||
List<EmrHandler.ListedInstances> listed = EmrHandler.listedInstances(cluster, false);
|
||||
Map<String, Integer> current = new LinkedHashMap<>();
|
||||
for (EmrHandler.ListedInstances set : listed) {
|
||||
if (group.getId().equals(set.groupId())) {
|
||||
long first = firstIndex(listed, set);
|
||||
PrimitiveIterator.OfLong ordinals = set.ordinalsFrom(1);
|
||||
for (long k = 1; k <= set.count(); k++) {
|
||||
long ordinal = ordinals.nextLong();
|
||||
current.put(ec2InstanceId(first + ordinal - 1), (int) ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ordinals a shrink to {@code count} ends under a ShrinkPolicy's InstanceResizePolicy, beyond those already
|
||||
* {@code ending}, by EC2InstanceIdsToTerminate or the request's earlier entries for the group: first its "Specific
|
||||
* list of instances to be terminated when shrinking an instance group", then the group's last instances, passing
|
||||
* its "Specific list of instances to be protected when shrinking an instance group". Ended this way, as named
|
||||
* instances are, their ids are not taken again. An id to terminate that is not one of the group's running
|
||||
* instances is refused, as EC2InstanceIdsToTerminate refuses it, and so is a shrink the protected instances leave
|
||||
* short, rather than ending one of them. No source gives AWS's answer to either, so the wording is Floci's.
|
||||
*/
|
||||
private static List<Integer> shrinkOrdinals(EmrCluster cluster, EmrInstanceGroup group, int count,
|
||||
JsonNode shrinkPolicy, Set<Integer> ending) {
|
||||
Map<String, Integer> running = runningOrdinals(cluster, group);
|
||||
int shrinkBy = running.size() - ending.size() - count;
|
||||
JsonNode resize = shrinkPolicy.path("InstanceResizePolicy");
|
||||
if (shrinkBy <= 0 || !resize.isObject()) {
|
||||
return List.of();
|
||||
}
|
||||
Set<String> protect = new HashSet<>();
|
||||
resize.path("InstancesToProtect").forEach(id -> protect.add(id.asText()));
|
||||
List<Integer> chosen = new ArrayList<>();
|
||||
for (JsonNode id : resize.path("InstancesToTerminate")) {
|
||||
Integer ordinal = running.get(id.asText());
|
||||
if (ordinal == null) {
|
||||
throw new AwsException("ValidationException", "Instance " + id.asText()
|
||||
+ " is not a running instance of instance group " + group.getId() + ".", 400);
|
||||
}
|
||||
if (chosen.size() < shrinkBy && !ending.contains(ordinal) && !chosen.contains(ordinal)) {
|
||||
chosen.add(ordinal);
|
||||
}
|
||||
}
|
||||
List<Map.Entry<String, Integer>> fromTheEnd = new ArrayList<>(running.entrySet());
|
||||
Collections.reverse(fromTheEnd);
|
||||
for (Map.Entry<String, Integer> instance : fromTheEnd) {
|
||||
if (chosen.size() >= shrinkBy) {
|
||||
break;
|
||||
}
|
||||
Integer ordinal = instance.getValue();
|
||||
if (!protect.contains(instance.getKey()) && !ending.contains(ordinal) && !chosen.contains(ordinal)) {
|
||||
chosen.add(ordinal);
|
||||
}
|
||||
}
|
||||
if (chosen.size() < shrinkBy) {
|
||||
throw new AwsException("ValidationException", "Instance group " + group.getId() + " cannot shrink to "
|
||||
+ count + " instances without ending one its shrink policy protects.", 400);
|
||||
}
|
||||
return chosen;
|
||||
}
|
||||
|
||||
/**
|
||||
* The index, in the cluster's instance numbering, of the first place {@code set} takes: 1 for the primary node's
|
||||
* group or fleet, and after the places of those before it for the others.
|
||||
@@ -3903,7 +3979,29 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
|
||||
/** The public DNS name ListInstances gives a cluster's instance {@code index}, counted from 1, in a Region. */
|
||||
static String publicDnsName(long index, String region) {
|
||||
return AwsEndpoints.ec2PublicDns("203.0.113." + index, region);
|
||||
return AwsEndpoints.ec2PublicDns(publicIpAddress(index), region);
|
||||
}
|
||||
|
||||
/**
|
||||
* The public address ListInstances gives a cluster's instance {@code index}, counted from 1, the one its public DNS
|
||||
* name names: in 203.0.113.0/24, RFC 5737's documentation range, for the first 254, and past them in
|
||||
* 100.64.0.0/10, RFC 6598's shared address space, so no instance gets an address someone else holds.
|
||||
*/
|
||||
static String publicIpAddress(long index) {
|
||||
if (index <= 254) {
|
||||
return "203.0.113." + index;
|
||||
}
|
||||
long n = index - 255;
|
||||
return "100." + (64 + n / (254 * 256)) + "." + (n / 254) % 256 + "." + (n % 254 + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* The id ListInstances gives the {@code device}-th EBS volume, counted from 0, of cluster {@code clusterId}'s
|
||||
* instance {@code index}: 17 hex digits, as EC2's are, the first 8 from the cluster, so two clusters' volumes
|
||||
* differ.
|
||||
*/
|
||||
static String ebsVolumeId(String clusterId, long index, int device) {
|
||||
return "vol-" + String.format("%08x%09x", clusterId.hashCode(), index * 100 + device + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4000,10 +4098,18 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
List<EmrHandler.ListedInstances> listed = EmrHandler.listedInstances(cluster, false);
|
||||
long index = ordinal;
|
||||
String instanceType = null;
|
||||
String imageId = cluster.getCustomAmiId();
|
||||
for (EmrHandler.ListedInstances set : listed) {
|
||||
if (set.number() == group) {
|
||||
index = firstIndex(listed, set) + ordinal - 1;
|
||||
instanceType = set.instanceType();
|
||||
// A group's own CustomAmiId is the one its instances run, over the cluster's.
|
||||
for (EmrInstanceGroup instanceGroup : cluster.getInstanceGroups()) {
|
||||
if (set.groupId() != null && set.groupId().equals(instanceGroup.getId())
|
||||
&& instanceGroup.getCustomAmiId() != null) {
|
||||
imageId = instanceGroup.getCustomAmiId();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -4011,11 +4117,12 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
instance.setInstanceId(ec2InstanceId(index));
|
||||
instance.setOwnerId(accountId);
|
||||
instance.setInstanceType(instanceType);
|
||||
instance.setImageId(cluster.getCustomAmiId());
|
||||
instance.setImageId(imageId);
|
||||
String address = privateIpAddress(group, ordinal);
|
||||
instance.setPrivateIpAddress(address);
|
||||
instance.setPrivateDnsName(AwsRegions.ec2PrivateIpDnsName(address, cluster.getRegion()));
|
||||
instance.setPublicDnsName(publicDnsName(index, cluster.getRegion()));
|
||||
instance.setPublicIpAddress(publicIpAddress(index));
|
||||
instance.setPlacement(new Placement(availabilityZone(cluster)));
|
||||
instance.setRegion(cluster.getRegion());
|
||||
instance.setState(InstanceState.running());
|
||||
|
||||
@@ -27,6 +27,11 @@ public class EmrInstanceGroup {
|
||||
/** The group's Configurations as given, raw JSON; null when none. Applied at once, so it is also the last applied. */
|
||||
private String configurations;
|
||||
private long configurationsVersion;
|
||||
/** The EbsConfiguration as given (EbsBlockDeviceConfigs and EbsOptimized), raw JSON; null when none. */
|
||||
private String ebsConfiguration;
|
||||
private String customAmiId;
|
||||
/** The ShrinkPolicy the last ModifyInstanceGroups gave, raw JSON; null when none. */
|
||||
private String shrinkPolicy;
|
||||
|
||||
public EmrInstanceGroup() {}
|
||||
|
||||
@@ -79,4 +84,13 @@ public class EmrInstanceGroup {
|
||||
|
||||
public String getAutoScalingPolicy() { return autoScalingPolicy; }
|
||||
public void setAutoScalingPolicy(String autoScalingPolicy) { this.autoScalingPolicy = autoScalingPolicy; }
|
||||
|
||||
public String getEbsConfiguration() { return ebsConfiguration; }
|
||||
public void setEbsConfiguration(String ebsConfiguration) { this.ebsConfiguration = ebsConfiguration; }
|
||||
|
||||
public String getCustomAmiId() { return customAmiId; }
|
||||
public void setCustomAmiId(String customAmiId) { this.customAmiId = customAmiId; }
|
||||
|
||||
public String getShrinkPolicy() { return shrinkPolicy; }
|
||||
public void setShrinkPolicy(String shrinkPolicy) { this.shrinkPolicy = shrinkPolicy; }
|
||||
}
|
||||
Reference in new issue
Block a user