mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(autoscaling): retain membership when instance termination fails (#4115)
* fix(autoscaling): retain membership when instance termination fails * fix(autoscaling): keep stopped terminating instances and bound failure status A Terminating instance whose termination failed was pruned as stale when its EC2 state was stopping or stopped, recording a successful cleanup and leaving the instance behind. A stopped instance still exists, so only a missing, shutting-down or terminated record now counts as gone for a Terminating member; Pending keeps the wider set. The failed activity's StatusMessage is capped at 255 characters, the XmlStringMaxLen255 limit, and falls back to the exception type when there is no message. A repeated failure is deduplicated against the latest activity with the same description rather than the latest activity overall, so two failures alternating in one pass no longer defeat it. * fix(autoscaling): record a termination failure again when its error changes A repeated termination failure was skipped whenever the latest activity for the same instances had failed, so a failure that changed cause kept reporting the old reason. It is now skipped only when the previous failure carried the same StatusMessage. --------- Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
1 parent
0bc85f3ef8
commit
e377148a52
2 files changed
+436
-14
No files matched your search
+78
-14
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfiguration;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.ScalingActivity;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
@@ -42,6 +43,19 @@ public class AutoScalingReconciler {
|
||||
private static final Set<String> LAUNCH_TEMPLATE_NOT_FOUND_CODES =
|
||||
Set.of("InvalidLaunchTemplateName.NotFoundException", "InvalidLaunchTemplateId.NotFound");
|
||||
|
||||
/** EC2 states in which a launch that never came up is given up on. */
|
||||
private static final Set<String> ABANDONED_LAUNCH_STATES =
|
||||
Set.of("shutting-down", "terminated", "stopping", "stopped");
|
||||
|
||||
/**
|
||||
* EC2 states of an instance that is already going away. A stopped instance still exists and
|
||||
* still needs terminating, so it is not one of them.
|
||||
*/
|
||||
private static final Set<String> TERMINATED_STATES = Set.of("shutting-down", "terminated");
|
||||
|
||||
/** The Activity StatusMessage limit (XmlStringMaxLen255). */
|
||||
private static final int STATUS_MESSAGE_MAX_LENGTH = 255;
|
||||
|
||||
private final AutoScalingService asgService;
|
||||
private final Ec2Service ec2Service;
|
||||
private final ElbV2Service elbV2Service;
|
||||
@@ -217,15 +231,19 @@ public class AutoScalingReconciler {
|
||||
return !ec2Service.isInstanceContainerRunning(instance.getInstanceId());
|
||||
}
|
||||
if ("Pending".equals(lifecycleState)) {
|
||||
return isMissingOrTerminalEc2Instance(asg, instance);
|
||||
return isMissingOrTerminalEc2Instance(asg, instance.getInstanceId(), ABANDONED_LAUNCH_STATES);
|
||||
}
|
||||
if ("Terminating".equals(lifecycleState)) {
|
||||
return isMissingOrTerminalEc2Instance(asg, instance.getInstanceId(), TERMINATED_STATES);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isMissingOrTerminalEc2Instance(AutoScalingGroup asg, AsgInstance instance) {
|
||||
private boolean isMissingOrTerminalEc2Instance(AutoScalingGroup asg, String instanceId,
|
||||
Set<String> terminalStates) {
|
||||
try {
|
||||
List<Instance> ec2Instances = ec2Service
|
||||
.describeInstances(asg.getRegion(), List.of(instance.getInstanceId()), null)
|
||||
.describeInstances(asg.getRegion(), List.of(instanceId), null)
|
||||
.stream()
|
||||
.flatMap(r -> r.getInstances().stream())
|
||||
.collect(Collectors.toList());
|
||||
@@ -235,14 +253,11 @@ public class AutoScalingReconciler {
|
||||
String state = ec2Instances.getFirst().getState() != null
|
||||
? ec2Instances.getFirst().getState().getName()
|
||||
: null;
|
||||
return "shutting-down".equals(state)
|
||||
|| "terminated".equals(state)
|
||||
|| "stopping".equals(state)
|
||||
|| "stopped".equals(state);
|
||||
return state != null && terminalStates.contains(state);
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.debugv("ASG {0}: keeping pending instance {1} during stale check: {2}",
|
||||
asg.getAutoScalingGroupName(), instance.getInstanceId(), e.getMessage());
|
||||
LOG.debugv("ASG {0}: keeping instance {1} during stale check: {2}",
|
||||
asg.getAutoScalingGroupName(), instanceId, e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -290,15 +305,25 @@ public class AutoScalingReconciler {
|
||||
List<String> instanceIds = terminatingInstances.stream()
|
||||
.map(AsgInstance::getInstanceId)
|
||||
.collect(Collectors.toList());
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
try {
|
||||
ec2Service.terminateInstances(asg.getRegion(), instanceIds);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("ASG {0}: failed to terminate refreshing instances {1}: {2}",
|
||||
asg.getAutoScalingGroupName(), instanceIds, e.getMessage());
|
||||
// A terminating instance whose EC2 record is already gone is not a failure: it is pruned
|
||||
// as stale by removeStaleInstances in this same pass. Only real failures are recorded.
|
||||
List<String> stillPresent = instanceIds.stream()
|
||||
.filter(id -> !isMissingOrTerminalEc2Instance(asg, id, TERMINATED_STATES))
|
||||
.toList();
|
||||
if (!stillPresent.isEmpty()) {
|
||||
recordFailedActivity(asg, "Terminating EC2 instance(s) for refresh: " + stillPresent,
|
||||
"An instance refresh requested replacement of active instances.", e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
asg.getInstances().removeIf(instance -> instanceIds.contains(instance.getInstanceId()));
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
@@ -403,16 +428,19 @@ public class AutoScalingReconciler {
|
||||
.map(AsgInstance::getInstanceId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
|
||||
try {
|
||||
ec2Service.terminateInstances(asg.getRegion(), instanceIds);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("ASG {0}: failed to terminate instances {1}: {2}",
|
||||
asg.getAutoScalingGroupName(), instanceIds, e.getMessage());
|
||||
recordFailedActivity(asg, "Terminating EC2 instance(s): " + instanceIds,
|
||||
"An instance was terminated in response to a desired capacity change.", e);
|
||||
return;
|
||||
}
|
||||
|
||||
deregisterFromTargetGroups(asg, instanceIds);
|
||||
deregisterFromClassicLoadBalancers(asg, instanceIds);
|
||||
|
||||
asg.getInstances().removeIf(i -> instanceIds.contains(i.getInstanceId()));
|
||||
asgService.saveAutoScalingGroupIfPresent(asg);
|
||||
asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
@@ -421,6 +449,42 @@ public class AutoScalingReconciler {
|
||||
"Successful");
|
||||
}
|
||||
|
||||
/**
|
||||
* Records a failed scaling activity with the error text, matching AWS (100% progress and a
|
||||
* {@code StatusMessage}). A failure that repeats on a later pass with the same error, because
|
||||
* termination keeps failing, is not recorded again: the latest activity with the same
|
||||
* description already records it, whatever other activities were recorded after it. A
|
||||
* different error is recorded, so the history carries the current reason.
|
||||
*/
|
||||
private void recordFailedActivity(AutoScalingGroup asg, String description, String cause, Exception failure) {
|
||||
String statusMessage = statusMessage(failure);
|
||||
ScalingActivity previous = asgService.describeScalingActivities(asg.getRegion(), asg.getAutoScalingGroupName())
|
||||
.stream()
|
||||
.filter(activity -> description.equals(activity.getDescription()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (previous != null && "Failed".equals(previous.getStatusCode())
|
||||
&& statusMessage.equals(previous.getStatusMessage())) {
|
||||
return;
|
||||
}
|
||||
ScalingActivity activity = asgService.recordActivity(asg.getRegion(), asg.getAutoScalingGroupName(),
|
||||
description, cause, "Failed");
|
||||
asgService.completeActivity(activity.getActivityId(), "Failed", statusMessage);
|
||||
}
|
||||
|
||||
private static String statusMessage(Exception failure) {
|
||||
String message = failure.getMessage() != null && !failure.getMessage().isBlank()
|
||||
? failure.getMessage()
|
||||
: failure.getClass().getSimpleName();
|
||||
if (message.length() <= STATUS_MESSAGE_MAX_LENGTH) {
|
||||
return message;
|
||||
}
|
||||
int end = Character.isHighSurrogate(message.charAt(STATUS_MESSAGE_MAX_LENGTH - 1))
|
||||
? STATUS_MESSAGE_MAX_LENGTH - 1
|
||||
: STATUS_MESSAGE_MAX_LENGTH;
|
||||
return message.substring(0, end);
|
||||
}
|
||||
|
||||
private void deregisterFromTargetGroups(AutoScalingGroup asg, List<String> instanceIds) {
|
||||
for (String tgArn : asg.getTargetGroupARNs()) {
|
||||
try {
|
||||
|
||||
+358
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfiguration;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.ScalingActivity;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.InstanceState;
|
||||
@@ -28,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.anyList;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -549,6 +551,362 @@ class AutoScalingReconcilerTest {
|
||||
assertEquals("RequestLimitExceeded", thrown.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scaleInTerminationFailureKeepsMembershipAndRecordsFailedActivity() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.setTargetGroupARNs(List.of("arn:aws:elasticloadbalancing:us-east-1:000000000000:targetgroup/app/123"));
|
||||
asg.getInstances().add(instance("i-keep", "InService"));
|
||||
when(ec2Service.isInstanceContainerRunning("i-keep")).thenReturn(true);
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-keep")))
|
||||
.thenThrow(new AwsException("InsufficientInstanceCapacity", "termination refused", 500));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
assertEquals(1, asg.getInstances().size());
|
||||
assertEquals("i-keep", asg.getInstances().getFirst().getInstanceId());
|
||||
verify(asgService).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s): [i-keep]"),
|
||||
eq("An instance was terminated in response to a desired capacity change."),
|
||||
eq("Failed"));
|
||||
verify(asgService).completeActivity("activity-1", "Failed", "termination refused");
|
||||
verify(elbV2Service, never()).deregisterTargets(
|
||||
eq("us-east-1"), eq(asg.getTargetGroupARNs().getFirst()), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatedTerminationFailureIsRecordedOnce() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
when(asgService.describeScalingActivities("us-east-1", "app-asg"))
|
||||
.thenReturn(List.of(failedActivity("Terminating EC2 instance(s): [i-keep]", "termination refused")));
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-keep", "InService"));
|
||||
when(ec2Service.isInstanceContainerRunning("i-keep")).thenReturn(true);
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-keep")))
|
||||
.thenThrow(new AwsException("InsufficientInstanceCapacity", "termination refused", 500));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
verify(asgService, never()).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s): [i-keep]"),
|
||||
eq("An instance was terminated in response to a desired capacity change."),
|
||||
eq("Failed"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scaleInTerminatesAndRemovesTheInstanceOnSuccess() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.setTargetGroupARNs(List.of("arn:aws:elasticloadbalancing:us-east-1:000000000000:targetgroup/app/123"));
|
||||
asg.getInstances().add(instance("i-gone", "InService"));
|
||||
when(ec2Service.isInstanceContainerRunning("i-gone")).thenReturn(true);
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
assertEquals(0, asg.getInstances().size());
|
||||
verify(ec2Service).terminateInstances("us-east-1", List.of("i-gone"));
|
||||
verify(elbV2Service).deregisterTargets(
|
||||
eq("us-east-1"), eq(asg.getTargetGroupARNs().getFirst()), anyList());
|
||||
verify(asgService).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s): [i-gone]"),
|
||||
eq("An instance was terminated in response to a desired capacity change."),
|
||||
eq("Successful"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminatingInstanceTerminationFailureKeepsMembershipAndRecordsFailedActivity() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-term", "Terminating"));
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-term")))
|
||||
.thenThrow(new AwsException("InsufficientInstanceCapacity", "termination refused", 500));
|
||||
when(ec2Service.describeInstances("us-east-1", List.of("i-term"), null))
|
||||
.thenReturn(List.of(reservation(runningEc2Instance("i-term"))));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
assertEquals(1, asg.getInstances().size());
|
||||
assertEquals("i-term", asg.getInstances().getFirst().getInstanceId());
|
||||
verify(asgService).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s) for refresh: [i-term]"),
|
||||
eq("An instance refresh requested replacement of active instances."),
|
||||
eq("Failed"));
|
||||
verify(asgService).completeActivity("activity-1", "Failed", "termination refused");
|
||||
verify(asgService, never()).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s) for refresh: [i-term]"),
|
||||
eq("An instance refresh requested replacement of active instances."),
|
||||
eq("Successful"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminatingInstanceWithMissingEc2RecordIsPrunedAsStale() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-term", "Terminating"));
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-term")))
|
||||
.thenThrow(new AwsException("InvalidInstanceID.NotFound", "not found", 400));
|
||||
Instance ec2Instance = new Instance();
|
||||
ec2Instance.setInstanceId("i-term");
|
||||
ec2Instance.setState(InstanceState.terminated());
|
||||
Reservation reservation = new Reservation();
|
||||
reservation.setInstances(List.of(ec2Instance));
|
||||
when(ec2Service.describeInstances("us-east-1", List.of("i-term"), null))
|
||||
.thenReturn(List.of(reservation));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
assertEquals(0, asg.getInstances().size());
|
||||
verify(asgService).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Removing stale EC2 instance reference(s): [i-term]"),
|
||||
eq("Persisted Auto Scaling state referenced instance containers that are no longer running."),
|
||||
eq("Successful"));
|
||||
verify(asgService, never()).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s) for refresh: [i-term]"),
|
||||
eq("An instance refresh requested replacement of active instances."),
|
||||
eq("Failed"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminatingStoppedInstanceIsRetriedRatherThanPrunedAsStale() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-term", "Terminating"));
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-term")))
|
||||
.thenThrow(new AwsException("InsufficientInstanceCapacity", "termination refused", 500));
|
||||
when(ec2Service.describeInstances("us-east-1", List.of("i-term"), null))
|
||||
.thenReturn(List.of(reservation(ec2Instance("i-term", InstanceState.stopped()))));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
assertEquals(1, asg.getInstances().size());
|
||||
assertEquals("Terminating", asg.getInstances().getFirst().getLifecycleState());
|
||||
verify(asgService).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s) for refresh: [i-term]"),
|
||||
eq("An instance refresh requested replacement of active instances."),
|
||||
eq("Failed"));
|
||||
verify(asgService, never()).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Removing stale EC2 instance reference(s): [i-term]"),
|
||||
anyString(),
|
||||
anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pendingStoppedInstanceIsStillPrunedAsStale() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-pending", "Pending"));
|
||||
when(ec2Service.describeInstances("us-east-1", List.of("i-pending"), null))
|
||||
.thenReturn(List.of(reservation(ec2Instance("i-pending", InstanceState.stopped()))));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
assertEquals(0, asg.getInstances().size());
|
||||
verify(asgService).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Removing stale EC2 instance reference(s): [i-pending]"),
|
||||
eq("Persisted Auto Scaling state referenced instance containers that are no longer running."),
|
||||
eq("Successful"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatedTerminationFailureIsRecordedOnceWhenOtherActivitiesFollowIt() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
ElbV2Service elbV2Service = mock(ElbV2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
when(asgService.describeScalingActivities("us-east-1", "app-asg"))
|
||||
.thenReturn(List.of(
|
||||
failedActivity("Terminating EC2 instance(s) for refresh: [i-other]", "termination refused"),
|
||||
failedActivity("Terminating EC2 instance(s): [i-keep]", "termination refused")));
|
||||
AutoScalingReconciler reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-keep", "InService"));
|
||||
when(ec2Service.isInstanceContainerRunning("i-keep")).thenReturn(true);
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-keep")))
|
||||
.thenThrow(new AwsException("InsufficientInstanceCapacity", "termination refused", 500));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
verify(asgService, never()).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s): [i-keep]"),
|
||||
eq("An instance was terminated in response to a desired capacity change."),
|
||||
eq("Failed"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminationFailureWithADifferentErrorIsRecordedAgain() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
when(asgService.describeScalingActivities("us-east-1", "app-asg"))
|
||||
.thenReturn(List.of(failedActivity("Terminating EC2 instance(s): [i-keep]", "termination refused")));
|
||||
AutoScalingReconciler reconciler =
|
||||
new AutoScalingReconciler(asgService, ec2Service, mock(ElbV2Service.class));
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-keep", "InService"));
|
||||
when(ec2Service.isInstanceContainerRunning("i-keep")).thenReturn(true);
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-keep")))
|
||||
.thenThrow(new AwsException("RequestLimitExceeded", "Request limit exceeded.", 503));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
verify(asgService).recordActivity(
|
||||
eq("us-east-1"),
|
||||
eq("app-asg"),
|
||||
eq("Terminating EC2 instance(s): [i-keep]"),
|
||||
eq("An instance was terminated in response to a desired capacity change."),
|
||||
eq("Failed"));
|
||||
verify(asgService).completeActivity("activity-1", "Failed", "Request limit exceeded.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedActivityStatusMessageIsCappedAt255Characters() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
AutoScalingReconciler reconciler =
|
||||
new AutoScalingReconciler(asgService, ec2Service, mock(ElbV2Service.class));
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-keep", "InService"));
|
||||
when(ec2Service.isInstanceContainerRunning("i-keep")).thenReturn(true);
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-keep")))
|
||||
.thenThrow(new IllegalStateException("x".repeat(300)));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
verify(asgService).completeActivity("activity-1", "Failed", "x".repeat(255));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedActivityWithoutAnErrorMessageNamesTheException() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
stubActivityRecording(asgService);
|
||||
AutoScalingReconciler reconciler =
|
||||
new AutoScalingReconciler(asgService, ec2Service, mock(ElbV2Service.class));
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(0);
|
||||
asg.getInstances().add(instance("i-keep", "InService"));
|
||||
when(ec2Service.isInstanceContainerRunning("i-keep")).thenReturn(true);
|
||||
when(ec2Service.terminateInstances("us-east-1", List.of("i-keep")))
|
||||
.thenThrow(new IllegalStateException());
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
verify(asgService).completeActivity("activity-1", "Failed", "IllegalStateException");
|
||||
}
|
||||
|
||||
private static ScalingActivity stubActivityRecording(AutoScalingService asgService) {
|
||||
ScalingActivity activity = new ScalingActivity();
|
||||
activity.setActivityId("activity-1");
|
||||
when(asgService.recordActivity(anyString(), anyString(), anyString(), anyString(), anyString()))
|
||||
.thenReturn(activity);
|
||||
return activity;
|
||||
}
|
||||
|
||||
private static ScalingActivity failedActivity(String description, String statusMessage) {
|
||||
ScalingActivity activity = new ScalingActivity();
|
||||
activity.setDescription(description);
|
||||
activity.setStatusCode("Failed");
|
||||
activity.setStatusMessage(statusMessage);
|
||||
return activity;
|
||||
}
|
||||
|
||||
private static Instance runningEc2Instance(String instanceId) {
|
||||
return ec2Instance(instanceId, InstanceState.running());
|
||||
}
|
||||
|
||||
private static Instance ec2Instance(String instanceId, InstanceState state) {
|
||||
Instance ec2Instance = new Instance();
|
||||
ec2Instance.setInstanceId(instanceId);
|
||||
ec2Instance.setState(state);
|
||||
return ec2Instance;
|
||||
}
|
||||
|
||||
private static Reservation reservation(Instance instance) {
|
||||
Reservation reservation = new Reservation();
|
||||
reservation.setInstances(List.of(instance));
|
||||
return reservation;
|
||||
}
|
||||
|
||||
private static AutoScalingGroup asgWhoseLaunchTemplateLookupFails(Ec2Service ec2Service,
|
||||
AwsException failure) {
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
|
||||
Reference in new issue
Block a user