mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(autoscaling): prevent reconciler resurrecting deleted groups (#3818)
* fix(autoscaling): prevent reconciler resurrecting deleted groups * fix(autoscaling): narrow reconciler group locking
This commit is contained in:
1 parent
eb787672b8
commit
bd9700db0d
4 files changed
+143
-33
No files matched your search
+18
-7
@@ -22,6 +22,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -102,8 +103,9 @@ public class AutoScalingReconciler {
|
||||
} else if (activeCapacity > desired) {
|
||||
scaleIn(asg, (int) (activeCapacity - desired));
|
||||
}
|
||||
asgService.saveAutoScalingGroup(asg);
|
||||
asgService.completeInstanceRefreshIfSettled(asg.getRegion(), asg.getAutoScalingGroupName());
|
||||
if (asgService.saveAutoScalingGroupIfPresent(asg)) {
|
||||
asgService.completeInstanceRefreshIfSettled(asg.getRegion(), asg.getAutoScalingGroupName());
|
||||
}
|
||||
}
|
||||
|
||||
static long activeCapacity(AutoScalingGroup asg) {
|
||||
@@ -148,7 +150,7 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
asgService.saveAutoScalingGroup(asg);
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,7 +169,7 @@ public class AutoScalingReconciler {
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
asg.getInstances().removeIf(instance -> instanceIds.contains(instance.getInstanceId()));
|
||||
asgService.saveAutoScalingGroup(asg);
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
"Removing stale EC2 instance reference(s): " + instanceIds,
|
||||
"Persisted Auto Scaling state referenced instance containers that are no longer running.",
|
||||
@@ -279,7 +281,7 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
|
||||
asg.getInstances().removeIf(instance -> instanceIds.contains(instance.getInstanceId()));
|
||||
asgService.saveAutoScalingGroup(asg);
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
"Terminating EC2 instance(s) for refresh: " + instanceIds,
|
||||
"An instance refresh requested replacement of active instances.",
|
||||
@@ -318,6 +320,7 @@ public class AutoScalingReconciler {
|
||||
launchSource.iamInstanceProfile(),
|
||||
launchSource.associatePublicIpAddress());
|
||||
|
||||
List<String> launchedInstanceIds = new ArrayList<>();
|
||||
for (Instance ec2Inst : reservation.getInstances()) {
|
||||
AsgInstance asgInst = new AsgInstance();
|
||||
asgInst.setInstanceId(ec2Inst.getInstanceId());
|
||||
@@ -330,10 +333,18 @@ public class AutoScalingReconciler {
|
||||
asgInst.setLaunchTemplateVersion(launchSource.launchTemplateVersion());
|
||||
asgInst.setInstanceType(launchSource.instanceType());
|
||||
asg.getInstances().add(asgInst);
|
||||
launchedInstanceIds.add(ec2Inst.getInstanceId());
|
||||
LOG.infov("ASG {0}: launched instance {1} (Pending)",
|
||||
asg.getAutoScalingGroupName(), ec2Inst.getInstanceId());
|
||||
}
|
||||
asgService.saveAutoScalingGroup(asg);
|
||||
if (!asgService.saveAutoScalingGroupIfPresent(asg) && !launchedInstanceIds.isEmpty()) {
|
||||
try {
|
||||
ec2Service.terminateInstances(asg.getRegion(), launchedInstanceIds);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("ASG {0}: failed to clean up instances {1} after group deletion: {2}",
|
||||
asg.getAutoScalingGroupName(), launchedInstanceIds, e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("ASG {0}: failed to launch instances: {1}",
|
||||
asg.getAutoScalingGroupName(), e.getMessage());
|
||||
@@ -384,7 +395,7 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
|
||||
asg.getInstances().removeIf(i -> instanceIds.contains(i.getInstanceId()));
|
||||
asgService.saveAutoScalingGroup(asg);
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
"Terminating EC2 instance(s): " + instanceIds,
|
||||
"An instance was terminated in response to a desired capacity change.",
|
||||
|
||||
+40
-24
@@ -63,6 +63,7 @@ public class AutoScalingService {
|
||||
// region :: name → resource
|
||||
private Map<String, LaunchConfiguration> launchConfigs = new ConcurrentHashMap<>();
|
||||
private Map<String, AutoScalingGroup> groups = new ConcurrentHashMap<>();
|
||||
private final Map<String, Object> groupLocks = new ConcurrentHashMap<>();
|
||||
private Map<String, LifecycleHook> hooks = new ConcurrentHashMap<>();
|
||||
private Map<String, ScalingPolicy> policies = new ConcurrentHashMap<>();
|
||||
private Map<String, ScalingActivity> activities = new ConcurrentHashMap<>();
|
||||
@@ -328,29 +329,32 @@ public class AutoScalingService {
|
||||
}
|
||||
|
||||
public void deleteAutoScalingGroup(String region, String name, boolean forceDelete) {
|
||||
AutoScalingGroup asg = requireGroup(region, name);
|
||||
List<AsgInstance> active = asg.getInstances().stream()
|
||||
.filter(i -> !"Terminated".equals(i.getLifecycleState()))
|
||||
.collect(Collectors.toList());
|
||||
if (!active.isEmpty() && !forceDelete) {
|
||||
throw new AwsException("ResourceInUse",
|
||||
"Auto Scaling group '" + name + "' has " + active.size()
|
||||
+ " instance(s). Set ForceDelete=true to delete anyway.", 400);
|
||||
String key = asgKey(region, name);
|
||||
synchronized (lockFor(key)) {
|
||||
AutoScalingGroup asg = requireGroup(region, name);
|
||||
List<AsgInstance> active = asg.getInstances().stream()
|
||||
.filter(i -> !"Terminated".equals(i.getLifecycleState()))
|
||||
.collect(Collectors.toList());
|
||||
if (!active.isEmpty() && !forceDelete) {
|
||||
throw new AwsException("ResourceInUse",
|
||||
"Auto Scaling group '" + name + "' has " + active.size()
|
||||
+ " instance(s). Set ForceDelete=true to delete anyway.", 400);
|
||||
}
|
||||
if (forceDelete && ec2Service != null && !active.isEmpty()) {
|
||||
active.stream()
|
||||
.map(AsgInstance::getInstanceId)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(instanceId -> {
|
||||
try {
|
||||
ec2Service.terminateInstances(region, List.of(instanceId));
|
||||
}
|
||||
catch (AwsException ignored) {
|
||||
// ForceDelete should remove stale ASG membership even if EC2 no longer has the instance.
|
||||
}
|
||||
});
|
||||
}
|
||||
groups.remove(key);
|
||||
}
|
||||
if (forceDelete && ec2Service != null && !active.isEmpty()) {
|
||||
active.stream()
|
||||
.map(AsgInstance::getInstanceId)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(instanceId -> {
|
||||
try {
|
||||
ec2Service.terminateInstances(region, List.of(instanceId));
|
||||
}
|
||||
catch (AwsException ignored) {
|
||||
// ForceDelete should remove stale ASG membership even if EC2 no longer has the instance.
|
||||
}
|
||||
});
|
||||
}
|
||||
groups.remove(asgKey(region, name));
|
||||
// clean up associated hooks and policies
|
||||
hooks.entrySet().removeIf(e -> e.getValue().getAutoScalingGroupName().equals(name));
|
||||
policies.entrySet().removeIf(e -> e.getValue().getAutoScalingGroupName().equals(name));
|
||||
@@ -372,8 +376,20 @@ public class AutoScalingService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void saveAutoScalingGroup(AutoScalingGroup asg) {
|
||||
groups.put(asgKey(asg.getRegion(), asg.getAutoScalingGroupName()), asg);
|
||||
public boolean saveAutoScalingGroupIfPresent(AutoScalingGroup asg) {
|
||||
String key = asgKey(asg.getRegion(), asg.getAutoScalingGroupName());
|
||||
synchronized (lockFor(key)) {
|
||||
AutoScalingGroup current = groups.get(key);
|
||||
if (current == asg) {
|
||||
groups.put(key, asg);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Object lockFor(String key) {
|
||||
return groupLocks.computeIfAbsent(key, ignored -> new Object());
|
||||
}
|
||||
|
||||
public void setDesiredCapacity(String region, String name, int desiredCapacity) {
|
||||
|
||||
Reference in new issue
Block a user