mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 08:09:43 -05:00
fix(emr): release instance capacity when clusters terminate (#6095)
* fix(emr): release instance capacity when clusters terminate I mark instance groups and fleets TERMINATED and clear their active capacity on automatic and manual cluster termination. I preserve requested and target capacity and cover both paths through HTTP regressions. * fix(emr): retain terminated instances and reject capacity changes
This commit is contained in:
1 parent
95bf6df474
commit
a578bb0811
5 files changed
+279
-4
No files matched your search
@@ -22,6 +22,7 @@ import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -300,15 +301,33 @@ public class EmrHandler {
|
||||
EmrCluster cluster = service.getClusterForInstances(text(request, "ClusterId"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode arr = response.putArray("Instances");
|
||||
boolean terminated = "TERMINATED".equals(cluster.getState());
|
||||
List<String> states = stringList(request.path("InstanceStates"));
|
||||
if ((!states.isEmpty() && !states.contains(terminated ? "TERMINATED" : "RUNNING"))
|
||||
|| (terminated && !withinTerminatedInstanceRetention(cluster.getEndDateTime(), Instant.now()))) {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
int n = 1;
|
||||
for (EmrInstanceGroup group : cluster.getInstanceGroups()) {
|
||||
for (int i = 0; i < group.getRunningInstanceCount(); i++) {
|
||||
arr.add(syntheticInstanceNode(cluster, group, n++));
|
||||
int count = listedInstanceCount(group, terminated);
|
||||
for (int i = 0; i < count; i++) {
|
||||
arr.add(syntheticInstanceNode(cluster, group, n++, terminated));
|
||||
}
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
static boolean withinTerminatedInstanceRetention(Instant endedAt, Instant now) {
|
||||
return endedAt != null && !endedAt.isBefore(now.minus(30, ChronoUnit.DAYS));
|
||||
}
|
||||
|
||||
static int listedInstanceCount(EmrInstanceGroup group, boolean terminated) {
|
||||
if (terminated && group.getTerminatedInstanceCount() > 0) {
|
||||
return group.getTerminatedInstanceCount();
|
||||
}
|
||||
return group.getRunningInstanceCount();
|
||||
}
|
||||
|
||||
// ──────────────────────────── Security configurations ────────────────────────────
|
||||
|
||||
private Response handleCreateSecurityConfiguration(JsonNode request) {
|
||||
@@ -697,7 +716,8 @@ public class EmrHandler {
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode syntheticInstanceNode(EmrCluster cluster, EmrInstanceGroup group, int index) {
|
||||
private ObjectNode syntheticInstanceNode(EmrCluster cluster, EmrInstanceGroup group, int index,
|
||||
boolean terminated) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("Id", "ci-" + index + cluster.getId());
|
||||
node.put("Ec2InstanceId", "i-" + String.format("%017d", index));
|
||||
@@ -710,7 +730,7 @@ public class EmrHandler {
|
||||
node.put("Market", group.getMarket());
|
||||
}
|
||||
node.put("InstanceType", group.getInstanceType());
|
||||
node.putObject("Status").put("State", "RUNNING");
|
||||
node.putObject("Status").put("State", terminated ? "TERMINATED" : "RUNNING");
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@@ -157,6 +157,7 @@ public class EmrService {
|
||||
continue;
|
||||
}
|
||||
cluster.setState("TERMINATED");
|
||||
releaseInstanceCapacity(cluster);
|
||||
cluster.setStateChangeReasonCode("USER_REQUEST");
|
||||
cluster.setStateChangeMessage("Terminated by user request");
|
||||
cluster.setEndDateTime(Instant.now());
|
||||
@@ -268,6 +269,7 @@ public class EmrService {
|
||||
|
||||
public synchronized List<String> addInstanceGroups(String clusterId, List<EmrInstanceGroup> groups) {
|
||||
EmrCluster cluster = requireCluster(clusterId);
|
||||
rejectTerminatedCluster(cluster);
|
||||
List<String> ids = new ArrayList<>();
|
||||
for (EmrInstanceGroup group : groups) {
|
||||
group.setId("ig-" + randomId(13));
|
||||
@@ -297,6 +299,7 @@ public class EmrService {
|
||||
List<EmrInstanceGroup> groups = new ArrayList<>();
|
||||
for (InstanceGroupModification mod : modifications) {
|
||||
EmrCluster found = clusterId != null ? requireCluster(clusterId) : clusterOwning(region, mod.instanceGroupId());
|
||||
rejectTerminatedCluster(found);
|
||||
// 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());
|
||||
@@ -332,6 +335,7 @@ public class EmrService {
|
||||
|
||||
public synchronized String addInstanceFleet(String clusterId, EmrInstanceFleet fleet) {
|
||||
EmrCluster cluster = requireCluster(clusterId);
|
||||
rejectTerminatedCluster(cluster);
|
||||
fleet.setId("if-" + randomId(13));
|
||||
fleet.setProvisionedOnDemandCapacity(fleet.getTargetOnDemandCapacity());
|
||||
fleet.setProvisionedSpotCapacity(fleet.getTargetSpotCapacity());
|
||||
@@ -348,6 +352,7 @@ public class EmrService {
|
||||
if (instanceFleetId == null) {
|
||||
throw invalid("InstanceFleetId is required.");
|
||||
}
|
||||
rejectTerminatedCluster(cluster);
|
||||
EmrInstanceFleet fleet = cluster.getInstanceFleets().stream()
|
||||
.filter(f -> instanceFleetId.equals(f.getId()))
|
||||
.findFirst()
|
||||
@@ -742,6 +747,12 @@ public class EmrService {
|
||||
return new AwsException("InvalidRequestException", message, 400);
|
||||
}
|
||||
|
||||
private static void rejectTerminatedCluster(EmrCluster cluster) {
|
||||
if ("TERMINATED".equals(cluster.getState())) {
|
||||
throw invalid("Cluster " + cluster.getId() + " is terminated.");
|
||||
}
|
||||
}
|
||||
|
||||
private static EmrInstanceGroup requireInstanceGroup(EmrCluster cluster, String instanceGroupId) {
|
||||
if (instanceGroupId == null) {
|
||||
throw invalid("InstanceGroupId is required.");
|
||||
@@ -791,11 +802,25 @@ public class EmrService {
|
||||
return;
|
||||
}
|
||||
cluster.setState("TERMINATED");
|
||||
releaseInstanceCapacity(cluster);
|
||||
cluster.setStateChangeReasonCode("ALL_STEPS_COMPLETED");
|
||||
cluster.setStateChangeMessage("All steps completed.");
|
||||
cluster.setEndDateTime(Instant.now());
|
||||
}
|
||||
|
||||
private void releaseInstanceCapacity(EmrCluster cluster) {
|
||||
for (EmrInstanceGroup group : cluster.getInstanceGroups()) {
|
||||
group.setState("TERMINATED");
|
||||
group.setTerminatedInstanceCount(group.getRunningInstanceCount());
|
||||
group.setRunningInstanceCount(0);
|
||||
}
|
||||
for (EmrInstanceFleet fleet : cluster.getInstanceFleets()) {
|
||||
fleet.setState("TERMINATED");
|
||||
fleet.setProvisionedOnDemandCapacity(0);
|
||||
fleet.setProvisionedSpotCapacity(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void initStep(EmrStep step) {
|
||||
step.setId("s-" + randomId(13));
|
||||
step.setState("PENDING");
|
||||
|
||||
@@ -14,6 +14,7 @@ public class EmrInstanceGroup {
|
||||
private String bidPrice;
|
||||
private int requestedInstanceCount;
|
||||
private int runningInstanceCount;
|
||||
private int terminatedInstanceCount;
|
||||
private String state;
|
||||
/** The AutoScalingPolicy as given (Constraints and Rules), raw JSON; null when none is attached. */
|
||||
private String autoScalingPolicy;
|
||||
@@ -57,6 +58,11 @@ public class EmrInstanceGroup {
|
||||
this.runningInstanceCount = runningInstanceCount;
|
||||
}
|
||||
|
||||
public int getTerminatedInstanceCount() { return terminatedInstanceCount; }
|
||||
public void setTerminatedInstanceCount(int terminatedInstanceCount) {
|
||||
this.terminatedInstanceCount = terminatedInstanceCount;
|
||||
}
|
||||
|
||||
public String getState() { return state; }
|
||||
public void setState(String state) { this.state = state; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user