mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(autoscaling): fail SSM commands for stale ASG instances (#1600)
* fix(autoscaling): fail SSM commands for stale ASG instances * fix(ssm): clear stale instance message indexes
This commit is contained in:
1 parent
fef03c6b0f
commit
a895802b8b
4 files changed
+173
-1
No files matched your search
+24
-1
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.ec2.model.Reservation;
|
||||
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetHealth;
|
||||
import io.github.hectorvent.floci.services.ssm.SsmCommandService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
@@ -34,15 +35,22 @@ public class AutoScalingReconciler {
|
||||
private final AutoScalingService asgService;
|
||||
private final Ec2Service ec2Service;
|
||||
private final ElbV2Service elbV2Service;
|
||||
private final SsmCommandService ssmCommandService;
|
||||
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
|
||||
r -> new Thread(r, "asg-reconciler"));
|
||||
|
||||
@Inject
|
||||
AutoScalingReconciler(AutoScalingService asgService, Ec2Service ec2Service,
|
||||
ElbV2Service elbV2Service) {
|
||||
ElbV2Service elbV2Service, SsmCommandService ssmCommandService) {
|
||||
this.asgService = asgService;
|
||||
this.ec2Service = ec2Service;
|
||||
this.elbV2Service = elbV2Service;
|
||||
this.ssmCommandService = ssmCommandService;
|
||||
}
|
||||
|
||||
AutoScalingReconciler(AutoScalingService asgService, Ec2Service ec2Service,
|
||||
ElbV2Service elbV2Service) {
|
||||
this(asgService, ec2Service, elbV2Service, null);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@@ -138,6 +146,7 @@ public class AutoScalingReconciler {
|
||||
List<String> instanceIds = staleInstances.stream()
|
||||
.map(AsgInstance::getInstanceId)
|
||||
.collect(Collectors.toList());
|
||||
failActiveSsmInvocations(asg, instanceIds);
|
||||
asg.getInstances().removeIf(instance -> instanceIds.contains(instance.getInstanceId()));
|
||||
asgService.saveAutoScalingGroup(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
@@ -148,6 +157,20 @@ public class AutoScalingReconciler {
|
||||
asg.getAutoScalingGroupName(), instanceIds);
|
||||
}
|
||||
|
||||
private void failActiveSsmInvocations(AutoScalingGroup asg, List<String> instanceIds) {
|
||||
if (ssmCommandService == null) {
|
||||
return;
|
||||
}
|
||||
int failed = ssmCommandService.failActiveInvocationsForInstances(
|
||||
asg.getRegion(),
|
||||
Set.copyOf(instanceIds),
|
||||
"Undeliverable");
|
||||
if (failed > 0) {
|
||||
LOG.infov("ASG {0}: marked {1} active SSM command invocation(s) failed for stale instances {2}",
|
||||
asg.getAutoScalingGroupName(), failed, instanceIds);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isStaleInstance(AutoScalingGroup asg, AsgInstance instance) {
|
||||
String lifecycleState = instance.getLifecycleState();
|
||||
if ("InService".equals(lifecycleState)) {
|
||||
|
||||
@@ -21,9 +21,11 @@ import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -275,6 +277,51 @@ public class SsmCommandService implements Resettable {
|
||||
LOG.infov("CancelCommand: commandId={0}", commandId);
|
||||
}
|
||||
|
||||
public int failActiveInvocationsForInstance(String region, String instanceId, String statusDetails) {
|
||||
return failActiveInvocationsForInstances(region, Set.of(instanceId), statusDetails);
|
||||
}
|
||||
|
||||
public int failActiveInvocationsForInstances(String region, Set<String> instanceIds, String statusDetails) {
|
||||
if (instanceIds == null || instanceIds.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
String prefix = region + "::";
|
||||
Instant now = Instant.now();
|
||||
Set<String> commandIds = new LinkedHashSet<>();
|
||||
int failed = 0;
|
||||
|
||||
for (CommandInvocation invocation : invocationStore.scan(key -> key.startsWith(prefix))) {
|
||||
if (!instanceIds.contains(invocation.getInstanceId())) {
|
||||
continue;
|
||||
}
|
||||
if (!isActiveInvocation(invocation.getStatus())) {
|
||||
continue;
|
||||
}
|
||||
invocation.setStatus("Failed");
|
||||
invocation.setStatusDetails(statusDetails);
|
||||
invocation.setResponseCode(-1);
|
||||
invocation.setExecutionEndDateTime(now);
|
||||
invocationStore.put(invocationKey(region, invocation.getCommandId(), invocation.getInstanceId()), invocation);
|
||||
commandIds.add(invocation.getCommandId());
|
||||
failed++;
|
||||
}
|
||||
|
||||
for (String instanceId : instanceIds) {
|
||||
Queue<PendingMessage> queue = messageQueues.remove(instanceId);
|
||||
if (queue != null) {
|
||||
queue.clear();
|
||||
}
|
||||
}
|
||||
messageIndex.entrySet().removeIf(entry -> {
|
||||
String[] meta = entry.getValue();
|
||||
return meta.length == 3
|
||||
&& instanceIds.contains(meta[1])
|
||||
&& region.equals(meta[2]);
|
||||
});
|
||||
commandIds.forEach(commandId -> updateCommandStatus(commandId, region));
|
||||
return failed;
|
||||
}
|
||||
|
||||
// ── ec2messages agent protocol ──────────────────────────────────────────
|
||||
|
||||
public List<Map<String, Object>> getMessages(String instanceId, String messagesRequestId, int visibilityTimeout) {
|
||||
@@ -665,6 +712,10 @@ public class SsmCommandService implements Resettable {
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean isActiveInvocation(String status) {
|
||||
return "Pending".equals(status) || "InProgress".equals(status);
|
||||
}
|
||||
|
||||
private static String statusDetails(String status) {
|
||||
return switch (status) {
|
||||
case "InProgress" -> "In Progress";
|
||||
|
||||
Reference in new issue
Block a user