mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
feat(emr): resize instance groups and fleets, list bootstrap actions (#4900)
This commit is contained in:
1 parent
06ceec2aee
commit
82977a7f03
9 files changed
+435
-3
No files matched your search
@@ -71,6 +71,9 @@ public class EmrHandler {
|
||||
case "ListSecurityConfigurations" -> handleListSecurityConfigurations();
|
||||
case "AddTags" -> handleAddTags(request);
|
||||
case "RemoveTags" -> handleRemoveTags(request);
|
||||
case "ModifyInstanceGroups" -> handleModifyInstanceGroups(request, region);
|
||||
case "ModifyInstanceFleet" -> handleModifyInstanceFleet(request);
|
||||
case "ListBootstrapActions" -> handleListBootstrapActions(request);
|
||||
case "PutManagedScalingPolicy" -> handlePutManagedScalingPolicy(request);
|
||||
case "GetManagedScalingPolicy" -> handleGetManagedScalingPolicy(request);
|
||||
case "RemoveManagedScalingPolicy" -> handleRemoveManagedScalingPolicy(request);
|
||||
@@ -117,6 +120,11 @@ public class EmrHandler {
|
||||
cluster.setApplications(rawArray(request.path("Applications")));
|
||||
cluster.setConfigurations(rawArray(request.path("Configurations")));
|
||||
cluster.setTags(parseTags(request.path("Tags")));
|
||||
JsonNode bootstrapActions = request.path("BootstrapActions");
|
||||
if (!bootstrapActions.isMissingNode() && !bootstrapActions.isNull()) {
|
||||
EmrService.validateBootstrapActions(bootstrapActions);
|
||||
cluster.setBootstrapActions(rawArray(bootstrapActions));
|
||||
}
|
||||
JsonNode managedScaling = request.path("ManagedScalingPolicy");
|
||||
if (!managedScaling.isMissingNode() && !managedScaling.isNull()) {
|
||||
EmrService.validateManagedScalingPolicy(managedScaling);
|
||||
@@ -288,7 +296,7 @@ public class EmrHandler {
|
||||
ArrayNode arr = response.putArray("Instances");
|
||||
int n = 1;
|
||||
for (EmrInstanceGroup group : cluster.getInstanceGroups()) {
|
||||
for (int i = 0; i < Math.max(1, group.getRunningInstanceCount()); i++) {
|
||||
for (int i = 0; i < group.getRunningInstanceCount(); i++) {
|
||||
arr.add(syntheticInstanceNode(cluster, group, n++));
|
||||
}
|
||||
}
|
||||
@@ -346,6 +354,53 @@ public class EmrHandler {
|
||||
|
||||
// ──────────────────────────── Builders ────────────────────────────
|
||||
|
||||
private Response handleModifyInstanceGroups(JsonNode request, String region) {
|
||||
List<EmrService.InstanceGroupModification> modifications = new ArrayList<>();
|
||||
for (JsonNode g : request.path("InstanceGroups")) {
|
||||
JsonNode count = g.path("InstanceCount");
|
||||
JsonNode configurations = g.path("Configurations");
|
||||
modifications.add(new EmrService.InstanceGroupModification(
|
||||
g.path("InstanceGroupId").asText(null),
|
||||
count.isMissingNode() || count.isNull() ? null : count.asInt(),
|
||||
configurations.isArray() ? configurations.toString() : null));
|
||||
}
|
||||
service.modifyInstanceGroups(text(request, "ClusterId"), region, modifications);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleModifyInstanceFleet(JsonNode request) {
|
||||
JsonNode fleet = request.path("InstanceFleet");
|
||||
JsonNode onDemand = fleet.path("TargetOnDemandCapacity");
|
||||
JsonNode spot = fleet.path("TargetSpotCapacity");
|
||||
service.modifyInstanceFleet(text(request, "ClusterId"), fleet.path("InstanceFleetId").asText(null),
|
||||
onDemand.isMissingNode() || onDemand.isNull() ? null : onDemand.asInt(),
|
||||
spot.isMissingNode() || spot.isNull() ? null : spot.asInt());
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
/** Each BootstrapActionConfig given to RunJobFlow, as the Command shape: Name, ScriptPath and Args. */
|
||||
private Response handleListBootstrapActions(JsonNode request) {
|
||||
String raw = service.listBootstrapActions(text(request, "ClusterId"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode arr = response.putArray("BootstrapActions");
|
||||
if (raw == null) {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
try {
|
||||
for (JsonNode action : objectMapper.readTree(raw)) {
|
||||
ObjectNode command = arr.addObject();
|
||||
command.put("Name", action.path("Name").asText());
|
||||
JsonNode script = action.path("ScriptBootstrapAction");
|
||||
command.put("ScriptPath", script.path("Path").asText());
|
||||
ArrayNode args = command.putArray("Args");
|
||||
script.path("Args").forEach(args::add);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to parse stored BootstrapActions JSON; list left empty. Value: {0}", raw);
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handlePutManagedScalingPolicy(JsonNode request) {
|
||||
service.putManagedScalingPolicy(text(request, "ClusterId"), request.get("ManagedScalingPolicy"));
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
@@ -529,6 +584,13 @@ public class EmrHandler {
|
||||
if (g.getAutoScalingPolicy() != null) {
|
||||
node.set("AutoScalingPolicy", autoScalingPolicyNode(g.getAutoScalingPolicy()));
|
||||
}
|
||||
if (g.getConfigurations() != null) {
|
||||
// Floci applies a reconfiguration at once, so the applied configuration is the requested one.
|
||||
setRawJson(node, "Configurations", g.getConfigurations());
|
||||
node.put("ConfigurationsVersion", g.getConfigurationsVersion());
|
||||
setRawJson(node, "LastSuccessfullyAppliedConfigurations", g.getConfigurations());
|
||||
node.put("LastSuccessfullyAppliedConfigurationsVersion", g.getConfigurationsVersion());
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -628,6 +690,9 @@ public class EmrHandler {
|
||||
group.setMarket(g.path("Market").asText(null));
|
||||
group.setBidPrice(g.path("BidPrice").asText(null));
|
||||
group.setRequestedInstanceCount(g.path("InstanceCount").asInt(0));
|
||||
// Kept as given, an empty list included, so creation reads back like ModifyInstanceGroups.
|
||||
JsonNode configurations = g.path("Configurations");
|
||||
group.setConfigurations(configurations.isArray() ? configurations.toString() : null);
|
||||
JsonNode autoScaling = g.path("AutoScalingPolicy");
|
||||
if (!autoScaling.isMissingNode() && !autoScaling.isNull()) {
|
||||
EmrService.validateAutoScalingPolicy(autoScaling, group.getInstanceGroupType());
|
||||
|
||||
@@ -19,6 +19,7 @@ import jakarta.inject.Inject;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -262,6 +263,52 @@ public class EmrService {
|
||||
return ids;
|
||||
}
|
||||
|
||||
/** One InstanceGroupModifyConfig: a null count or configurations leaves that setting as it is. */
|
||||
public record InstanceGroupModification(String instanceGroupId, Integer instanceCount, String configurations) {}
|
||||
|
||||
/**
|
||||
* Resizes or reconfigures instance groups in place. ClusterId is optional in the request, so
|
||||
* without it each group is looked up across the account's clusters in the Region. Every modification is
|
||||
* checked before any is applied.
|
||||
*/
|
||||
public synchronized void modifyInstanceGroups(String clusterId, String region,
|
||||
List<InstanceGroupModification> modifications) {
|
||||
if (modifications.isEmpty()) {
|
||||
throw invalid("InstanceGroups must contain at least one instance group.");
|
||||
}
|
||||
Map<String, EmrCluster> touched = new LinkedHashMap<>();
|
||||
List<EmrInstanceGroup> groups = new ArrayList<>();
|
||||
for (InstanceGroupModification mod : modifications) {
|
||||
EmrCluster found = clusterId != null ? requireCluster(clusterId) : clusterOwning(region, mod.instanceGroupId());
|
||||
// One copy per cluster, so two groups of the same cluster are changed and stored together.
|
||||
EmrCluster cluster = touched.computeIfAbsent(found.getId(), id -> found);
|
||||
EmrInstanceGroup group = requireInstanceGroup(cluster, mod.instanceGroupId());
|
||||
if (mod.instanceCount() != null) {
|
||||
if (mod.instanceCount() < 0) {
|
||||
throw invalid("InstanceCount must not be negative.");
|
||||
}
|
||||
if ("MASTER".equals(group.getInstanceGroupType())
|
||||
&& mod.instanceCount() != group.getRequestedInstanceCount()) {
|
||||
throw invalid("The instance count of a master instance group cannot be modified.");
|
||||
}
|
||||
}
|
||||
groups.add(group);
|
||||
}
|
||||
for (int i = 0; i < modifications.size(); i++) {
|
||||
InstanceGroupModification mod = modifications.get(i);
|
||||
EmrInstanceGroup group = groups.get(i);
|
||||
if (mod.instanceCount() != null) {
|
||||
group.setRequestedInstanceCount(mod.instanceCount());
|
||||
group.setRunningInstanceCount(mod.instanceCount());
|
||||
}
|
||||
if (mod.configurations() != null) {
|
||||
group.setConfigurations(mod.configurations());
|
||||
group.setConfigurationsVersion(group.getConfigurationsVersion() + 1);
|
||||
}
|
||||
}
|
||||
touched.forEach(clusterStore::put);
|
||||
}
|
||||
|
||||
public List<EmrInstanceGroup> listInstanceGroups(String clusterId) {
|
||||
return requireCluster(clusterId).getInstanceGroups();
|
||||
}
|
||||
@@ -277,6 +324,38 @@ public class EmrService {
|
||||
return fleet.getId();
|
||||
}
|
||||
|
||||
/** Resizes an instance fleet; a null target leaves that capacity as it is. */
|
||||
public synchronized void modifyInstanceFleet(String clusterId, String instanceFleetId,
|
||||
Integer targetOnDemandCapacity, Integer targetSpotCapacity) {
|
||||
EmrCluster cluster = requireCluster(clusterId);
|
||||
if (instanceFleetId == null) {
|
||||
throw invalid("InstanceFleetId is required.");
|
||||
}
|
||||
EmrInstanceFleet fleet = cluster.getInstanceFleets().stream()
|
||||
.filter(f -> instanceFleetId.equals(f.getId()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> invalid("Instance fleet " + instanceFleetId + " is not in cluster " + clusterId + "."));
|
||||
int onDemand = targetOnDemandCapacity != null ? targetOnDemandCapacity : fleet.getTargetOnDemandCapacity();
|
||||
int spot = targetSpotCapacity != null ? targetSpotCapacity : fleet.getTargetSpotCapacity();
|
||||
if (onDemand < 0 || spot < 0) {
|
||||
throw invalid("Target capacities must not be negative.");
|
||||
}
|
||||
if ("MASTER".equals(fleet.getInstanceFleetType())
|
||||
&& (onDemand != fleet.getTargetOnDemandCapacity() || spot != fleet.getTargetSpotCapacity())) {
|
||||
throw invalid("The target capacity of a master instance fleet cannot be modified.");
|
||||
}
|
||||
fleet.setTargetOnDemandCapacity(onDemand);
|
||||
fleet.setTargetSpotCapacity(spot);
|
||||
fleet.setProvisionedOnDemandCapacity(onDemand);
|
||||
fleet.setProvisionedSpotCapacity(spot);
|
||||
clusterStore.put(clusterId, cluster);
|
||||
}
|
||||
|
||||
/** The cluster's bootstrap actions as given to RunJobFlow, raw JSON; null when it had none. */
|
||||
public String listBootstrapActions(String clusterId) {
|
||||
return requireCluster(clusterId).getBootstrapActions();
|
||||
}
|
||||
|
||||
public List<EmrInstanceFleet> listInstanceFleets(String clusterId) {
|
||||
return requireCluster(clusterId).getInstanceFleets();
|
||||
}
|
||||
@@ -527,6 +606,30 @@ public class EmrService {
|
||||
}
|
||||
}
|
||||
|
||||
/** Each BootstrapActionConfig needs a Name and a ScriptBootstrapAction with a Path. */
|
||||
static void validateBootstrapActions(JsonNode actions) {
|
||||
if (!actions.isArray()) {
|
||||
throw invalid("BootstrapActions must be a list.");
|
||||
}
|
||||
for (JsonNode action : actions) {
|
||||
if (action.path("Name").asText("").isEmpty()
|
||||
|| action.path("ScriptBootstrapAction").path("Path").asText("").isEmpty()) {
|
||||
throw invalid("Each bootstrap action needs a Name and a ScriptBootstrapAction.Path.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private EmrCluster clusterOwning(String region, String instanceGroupId) {
|
||||
if (instanceGroupId == null) {
|
||||
throw invalid("InstanceGroupId is required.");
|
||||
}
|
||||
return clusterStore.scan(k -> true).stream()
|
||||
.filter(c -> region.equals(c.getRegion()))
|
||||
.filter(c -> c.getInstanceGroups().stream().anyMatch(g -> instanceGroupId.equals(g.getId())))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> invalid("Instance group " + instanceGroupId + " is not valid."));
|
||||
}
|
||||
|
||||
private static AwsException invalid(String message) {
|
||||
return new AwsException("InvalidRequestException", message, 400);
|
||||
}
|
||||
|
||||
@@ -44,6 +44,8 @@ public class EmrCluster {
|
||||
private String ec2Attributes;
|
||||
private String applications;
|
||||
private String configurations;
|
||||
// The cluster's BootstrapActions (BootstrapActionConfig list) as given, raw JSON; null when none.
|
||||
private String bootstrapActions;
|
||||
// The cluster's ManagedScalingPolicy and AutoTerminationPolicy as given, raw JSON; null when none.
|
||||
private String managedScalingPolicy;
|
||||
private String autoTerminationPolicy;
|
||||
@@ -157,6 +159,9 @@ public class EmrCluster {
|
||||
public String getConfigurations() { return configurations; }
|
||||
public void setConfigurations(String configurations) { this.configurations = configurations; }
|
||||
|
||||
public String getBootstrapActions() { return bootstrapActions; }
|
||||
public void setBootstrapActions(String bootstrapActions) { this.bootstrapActions = bootstrapActions; }
|
||||
|
||||
public String getManagedScalingPolicy() { return managedScalingPolicy; }
|
||||
public void setManagedScalingPolicy(String managedScalingPolicy) { this.managedScalingPolicy = managedScalingPolicy; }
|
||||
|
||||
|
||||
@@ -17,6 +17,9 @@ public class EmrInstanceGroup {
|
||||
private String state;
|
||||
/** The AutoScalingPolicy as given (Constraints and Rules), raw JSON; null when none is attached. */
|
||||
private String autoScalingPolicy;
|
||||
/** 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;
|
||||
|
||||
public EmrInstanceGroup() {}
|
||||
|
||||
@@ -35,6 +38,12 @@ public class EmrInstanceGroup {
|
||||
public String getMarket() { return market; }
|
||||
public void setMarket(String market) { this.market = market; }
|
||||
|
||||
public String getConfigurations() { return configurations; }
|
||||
public void setConfigurations(String configurations) { this.configurations = configurations; }
|
||||
|
||||
public long getConfigurationsVersion() { return configurationsVersion; }
|
||||
public void setConfigurationsVersion(long configurationsVersion) { this.configurationsVersion = configurationsVersion; }
|
||||
|
||||
public String getBidPrice() { return bidPrice; }
|
||||
public void setBidPrice(String bidPrice) { this.bidPrice = bidPrice; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user