mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(autoscaling): skip groups deleted mid-iteration and lock instance updates (#5751)
The groups map is a StorageBackedMap once storage is initialised. Its iterator snapshots keys and reads each value lazily, so a group deleted between the two yields a null value. describeAutoScalingGroups returned that null (the reconciler then NPE'd in reconcile and again in its catch block), and describeAutoScalingInstances, SetInstanceHealth and TerminateInstanceInAutoScalingGroup NPE'd outright (HTTP 500). Skip null values at those iteration sites. SetInstanceHealth and TerminateInstanceInAutoScalingGroup now re-read and update the group under its per-group lock, as saveAutoScalingGroupIfPresent does, instead of an unlocked find/mutate/put that could race the reconciler or resurrect a just-deleted group.
This commit is contained in:
1 parent
0492fc9842
commit
f045d7d0a8
3 files changed
+90
-30
No files matched your search
+42
-30
@@ -21,6 +21,7 @@ import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -115,7 +116,7 @@ public class AutoScalingService {
|
||||
|
||||
// region :: name → resource
|
||||
private Map<String, LaunchConfiguration> launchConfigs = new ConcurrentHashMap<>();
|
||||
private Map<String, AutoScalingGroup> groups = new ConcurrentHashMap<>();
|
||||
Map<String, AutoScalingGroup> groups = new ConcurrentHashMap<>();
|
||||
private final Map<String, Object> groupLocks = new ConcurrentHashMap<>();
|
||||
private AccountAwareStorageBackend<AutoScalingGroup> groupsStore;
|
||||
private Map<String, LifecycleHook> hooks = new ConcurrentHashMap<>();
|
||||
@@ -486,7 +487,9 @@ public class AutoScalingService {
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
// StorageBackedMap reads each value lazily, so a group deleted mid-iteration shows up as null.
|
||||
return groups.values().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(g -> region == null || region.equals(g.getRegion()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
@@ -601,6 +604,7 @@ public class AutoScalingService {
|
||||
|
||||
public List<AsgInstance> describeAutoScalingInstances(String region, List<String> instanceIds) {
|
||||
List<AsgInstance> all = groups.values().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(g -> region.equals(g.getRegion()))
|
||||
.flatMap(g -> g.getInstances().stream())
|
||||
.collect(Collectors.toList());
|
||||
@@ -647,18 +651,39 @@ public class AutoScalingService {
|
||||
throw new AwsException("ValidationError",
|
||||
"HealthStatus must be Healthy or Unhealthy.", 400);
|
||||
}
|
||||
AutoScalingGroup asg = groups.values().stream()
|
||||
.filter(group -> region.equals(group.getRegion()))
|
||||
.filter(group -> group.getInstances().stream()
|
||||
.anyMatch(instance -> instanceId != null && instanceId.equals(instance.getInstanceId())))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("ValidationError",
|
||||
"Instance '" + instanceId + "' not found in any Auto Scaling group.", 400));
|
||||
asg.getInstances().stream()
|
||||
updateGroupOfInstance(region, instanceId, asg -> asg.getInstances().stream()
|
||||
.filter(instance -> instanceId.equals(instance.getInstanceId()))
|
||||
.findFirst()
|
||||
.ifPresent(instance -> instance.setHealthStatus(healthStatus));
|
||||
groups.put(asgKey(region, asg.getAutoScalingGroupName()), asg);
|
||||
.ifPresent(instance -> instance.setHealthStatus(healthStatus)));
|
||||
}
|
||||
|
||||
/** Applies {@code update} to the group holding {@code instanceId}, under that group's lock. */
|
||||
private void updateGroupOfInstance(String region, String instanceId, Consumer<AutoScalingGroup> update) {
|
||||
String name = groups.values().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(g -> region.equals(g.getRegion()) && containsInstance(g, instanceId))
|
||||
.map(AutoScalingGroup::getAutoScalingGroupName)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> instanceNotFound(instanceId));
|
||||
String key = asgKey(region, name);
|
||||
synchronized (lockFor(key)) {
|
||||
// Re-read under the lock: the group may have been deleted or replaced since the scan.
|
||||
AutoScalingGroup asg = groups.get(key);
|
||||
if (asg == null || !containsInstance(asg, instanceId)) {
|
||||
throw instanceNotFound(instanceId);
|
||||
}
|
||||
update.accept(asg);
|
||||
groups.put(key, asg);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException instanceNotFound(String instanceId) {
|
||||
return new AwsException("ValidationError",
|
||||
"Instance '" + instanceId + "' not found in any Auto Scaling group.", 400);
|
||||
}
|
||||
|
||||
private static boolean containsInstance(AutoScalingGroup asg, String instanceId) {
|
||||
return instanceId != null && asg.getInstances().stream().anyMatch(i -> instanceId.equals(i.getInstanceId()));
|
||||
}
|
||||
|
||||
public void attachInstances(String region, String name, List<String> instanceIds) {
|
||||
@@ -701,30 +726,17 @@ public class AutoScalingService {
|
||||
|
||||
public void terminateInstanceInAutoScalingGroup(String region, String instanceId,
|
||||
boolean decrementDesiredCapacity) {
|
||||
AutoScalingGroup asg = groups.values().stream()
|
||||
.filter(g -> region.equals(g.getRegion()))
|
||||
.filter(g -> g.getInstances().stream().anyMatch(i -> instanceId.equals(i.getInstanceId())))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("ValidationError",
|
||||
"Instance '" + instanceId + "' not found in any Auto Scaling group.", 400));
|
||||
String key = asgKey(region, asg.getAutoScalingGroupName());
|
||||
synchronized (lockFor(key)) {
|
||||
if (groups.get(key) != asg) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Instance '" + instanceId + "' not found in any Auto Scaling group.", 400);
|
||||
}
|
||||
AsgInstance instance = asg.getInstances().stream()
|
||||
.filter(i -> instanceId.equals(i.getInstanceId())).findFirst()
|
||||
.orElseThrow(() -> new AwsException("ValidationError",
|
||||
"Instance '" + instanceId + "' not found in any Auto Scaling group.", 400));
|
||||
updateGroupOfInstance(region, instanceId, asg -> {
|
||||
releaseRefreshOwnership(asg, List.of(instanceId));
|
||||
instance.setLifecycleState("Terminating");
|
||||
asg.getInstances().stream()
|
||||
.filter(i -> instanceId.equals(i.getInstanceId()))
|
||||
.findFirst()
|
||||
.ifPresent(i -> i.setLifecycleState("Terminating"));
|
||||
if (decrementDesiredCapacity) {
|
||||
int newDesired = Math.max(asg.getMinSize(), asg.getDesiredCapacity() - 1);
|
||||
asg.setDesiredCapacity(newDesired);
|
||||
}
|
||||
groups.put(key, asg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void releaseRefreshOwnership(AutoScalingGroup asg, List<String> instanceIds) {
|
||||
|
||||
+13
@@ -391,6 +391,19 @@ class AutoScalingRefreshStorageTest {
|
||||
assertEquals(1, group.getInstances().size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"persistent", "hybrid", "wal"})
|
||||
void terminatedInstanceDropsOutOfActiveRefresh(String mode) throws IOException {
|
||||
setUp(mode);
|
||||
service.startInstanceRefresh(REGION, "suspended-group", new InstanceRefresh());
|
||||
service.terminateInstanceInAutoScalingGroup(REGION, "i-original", true);
|
||||
restart(mode);
|
||||
AutoScalingGroup restored = service.describeAutoScalingGroups(REGION, List.of("suspended-group")).getFirst();
|
||||
InstanceRefresh refresh = service.activeInstanceRefresh(restored);
|
||||
assertNotNull(refresh);
|
||||
assertTrue(refresh.getReplacements().isEmpty(), "terminated instance must leave the refresh");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"persistent", "hybrid", "wal"})
|
||||
void explicitTerminationOfSelectedOriginalIsNotPaused(String mode) throws IOException {
|
||||
|
||||
+35
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.autoscaling;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
@@ -27,8 +29,10 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
@@ -908,6 +912,37 @@ class AutoScalingServiceTest {
|
||||
.getFirst().getHealthStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupDeletedMidIterationIsSkippedNotReturnedAsNull() {
|
||||
// A StorageBackedMap snapshots keys, then reads each value lazily; a group deleted between the
|
||||
// two reads comes back as a key with no value. Model that with a key the store lists but cannot get.
|
||||
InMemoryStorage<String, AutoScalingGroup> storage = new InMemoryStorage<>() {
|
||||
@Override
|
||||
public Set<String> keys() {
|
||||
Set<String> keys = new LinkedHashSet<>();
|
||||
keys.add(AutoScalingService.asgKey(REGION, "deleted-asg"));
|
||||
keys.addAll(super.keys());
|
||||
return keys;
|
||||
}
|
||||
};
|
||||
StorageBackedMap<AutoScalingGroup> groups = new StorageBackedMap<>(storage);
|
||||
groups.putAll(service.groups);
|
||||
service.groups = groups;
|
||||
service.attachInstances(REGION, "test-asg", List.of("i-live"));
|
||||
|
||||
assertFalse(service.describeAutoScalingGroups(null, null).contains(null));
|
||||
assertEquals(List.of("test-asg"), service.describeAutoScalingGroups(REGION, null).stream()
|
||||
.map(AutoScalingGroup::getAutoScalingGroupName).toList());
|
||||
assertEquals(1, service.describeAutoScalingInstances(REGION, null).size());
|
||||
|
||||
service.setInstanceHealth(REGION, "i-live", "Unhealthy", false);
|
||||
service.terminateInstanceInAutoScalingGroup(REGION, "i-live", false);
|
||||
AsgInstance stored = storage.get(AutoScalingService.asgKey(REGION, "test-asg")).orElseThrow()
|
||||
.getInstances().getFirst();
|
||||
assertEquals("Unhealthy", stored.getHealthStatus());
|
||||
assertEquals("Terminating", stored.getLifecycleState());
|
||||
}
|
||||
|
||||
private static final class AutoScalingGroupFixture {
|
||||
private static void addInstance(AutoScalingService service, String region, String name, String instanceId,
|
||||
String lifecycleState, String launchTemplateId, String launchTemplateVersion) {
|
||||
|
||||
Reference in new issue
Block a user