fix(autoscaling): retry failed standby registrations (#7253)

* fix(autoscaling): retry failed standby registrations

Retain Pending lifecycle and the ExitStandby activity until target-group and Classic load-balancer registration succeeds. Preserve deleted-load-balancer tolerance, initial-launch behavior and all current-main sparse-member regressions.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(autoscaling): retain standby registration warnings

Warn before retryable registration failures escape, retain the exception cause, and register launches and returning instances before InService through the same helpers.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

---------

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-01 20:24:51 -04:00
1 parent b4ae0ce9e0
commit 830e558e36
2 files changed
+117 -10

No files matched your search

@@ -293,11 +293,12 @@ public class AutoScalingReconciler {
}
String ec2State = ec2Instances.get(0).getState().getName();
if ("running".equals(ec2State)) {
asgInst.setLifecycleState("InService");
registerWithTargetGroups(asg, asgInst);
registerWithClassicLoadBalancers(asg, asgInst);
changed = true;
String standbyActivityId = asgInst.getStandbyActivityId();
boolean returningFromStandby = standbyActivityId != null;
registerWithTargetGroups(asg, asgInst, returningFromStandby);
registerWithClassicLoadBalancers(asg, asgInst, returningFromStandby);
asgInst.setLifecycleState("InService");
changed = true;
if (standbyActivityId != null) {
// An instance back from standby keeps the health it had there and completes its
// ExitStandby activity; nothing was launched.
@@ -875,14 +876,15 @@ public class AutoScalingReconciler {
/**
* Registers a newly launched instance with every Classic load balancer the group names.
* Registers a newly launched or returning instance with every Classic load balancer the group names.
*
* <p>An ASG attached through {@code LoadBalancerNames} feeds a Classic load balancer, not a
* target group, and the two lists are independent — a group can carry either or both. Without
* target group, and the two lists are independent; a group can carry either or both. Without
* this the Classic load balancer would stay empty, and a {@code min_elb_capacity} wait would
* never be satisfied.
*/
private void registerWithClassicLoadBalancers(AutoScalingGroup asg, AsgInstance asgInst) {
private void registerWithClassicLoadBalancers(AutoScalingGroup asg, AsgInstance asgInst,
boolean returningFromStandby) {
if (elbClassicService == null) {
return;
}
@@ -893,13 +895,18 @@ public class AutoScalingReconciler {
LOG.debugv("ASG {0}: registered {1} with Classic ELB {2}",
asg.getAutoScalingGroupName(), asgInst.getInstanceId(), lbName);
} catch (Exception e) {
LOG.warnv("ASG {0}: could not register {1} with Classic ELB {2}: {3}",
LOG.warnv(e, "ASG {0}: could not register {1} with Classic ELB {2}: {3}",
asg.getAutoScalingGroupName(), asgInst.getInstanceId(), lbName, e.getMessage());
if (returningFromStandby && !(e instanceof AwsException aws
&& "LoadBalancerNotFound".equals(aws.getErrorCode()))) {
throw e;
}
}
}
}
private void registerWithTargetGroups(AutoScalingGroup asg, AsgInstance asgInst) {
private void registerWithTargetGroups(AutoScalingGroup asg, AsgInstance asgInst,
boolean returningFromStandby) {
for (String tgArn : asg.getTargetGroupARNs()) {
try {
TargetDescription td = new TargetDescription();
@@ -908,8 +915,12 @@ public class AutoScalingReconciler {
LOG.debugv("ASG {0}: registered {1} with TG {2}",
asg.getAutoScalingGroupName(), asgInst.getInstanceId(), tgArn);
} catch (Exception e) {
LOG.warnv("ASG {0}: could not register {1} with TG {2}: {3}",
LOG.warnv(e, "ASG {0}: could not register {1} with TG {2}: {3}",
asg.getAutoScalingGroupName(), asgInst.getInstanceId(), tgArn, e.getMessage());
if (returningFromStandby && !(e instanceof AwsException aws
&& "TargetGroupNotFound".equals(aws.getErrorCode()))) {
throw e;
}
}
}
}
@@ -14,12 +14,14 @@ import io.github.hectorvent.floci.services.ec2.model.Reservation;
import io.github.hectorvent.floci.services.elb.ElbClassicService;
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;
import io.github.hectorvent.floci.testutil.LogCapture;
import io.restassured.path.xml.XmlPath;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.Duration;
@@ -34,6 +36,8 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.*;
@@ -640,6 +644,98 @@ class AutoScalingStandbyTest {
assertEquals("EnterStandby".equals(action) ? 0 : 2, group().getDesiredCapacity());
}
@ParameterizedTest
@CsvSource({"true,true", "true,false", "false,true", "false,false"})
void returningInstanceRetriesFailedRegistrationBeforeCompletingActivity(boolean classicFailure,
boolean awsFailure) {
ok(enterStandby(true, "i-a"));
reconciler.reconcile(group());
String activityId = ok(exitStandby("i-a")).getString(EXITED + ".ActivityId");
RuntimeException failure = awsFailure
? new AwsException("InternalFailure", "retry registration", 500)
: new IllegalStateException("retry registration");
if (classicFailure) {
when(elbClassic.registerInstances(REGION, CLASSIC, List.of("i-a")))
.thenThrow(failure).thenReturn(List.of());
} else {
doThrow(failure).doNothing().when(elbV2).registerTargets(eq(REGION), eq(TARGET_GROUP), anyList());
}
List<LogRecord> records = LogCapture.capture(AutoScalingReconciler.class,
() -> reconciler.reconcile(group()));
List<LogRecord> warnings = records.stream()
.filter(record -> record.getLevel().intValue() == Level.WARNING.intValue()).toList();
assertEquals(1, warnings.size());
LogRecord warning = warnings.getFirst();
assertArrayEquals(new Object[]{GROUP, "i-a", classicFailure ? CLASSIC : TARGET_GROUP,
failure.getMessage()}, warning.getParameters());
assertSame(failure, warning.getThrown());
assertEquals("Pending", lifecycle("i-a"));
AsgInstance returning = group().getInstances().stream()
.filter(instance -> "i-a".equals(instance.getInstanceId())).findFirst().orElseThrow();
assertEquals(activityId, returning.getStandbyActivityId());
XmlPath pending = describeScalingActivities();
assertEquals("PreInService", activity(pending, activityId, "StatusCode"));
assertTrue(activity(pending, activityId, "EndTime").isEmpty());
assertEquals(2, group().getDesiredCapacity());
reconciler.reconcile(group());
assertEquals("InService", lifecycle("i-a"));
assertNull(returning.getStandbyActivityId());
assertEquals("Successful", activity(describeScalingActivities(), activityId, "StatusCode"));
verify(elbV2, atLeastOnce()).registerTargets(eq(REGION), eq(TARGET_GROUP), anyList());
verify(elbClassic, times(classicFailure ? 2 : 1)).registerInstances(REGION, CLASSIC, List.of("i-a"));
assertNoLaunch();
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void returningInstanceToleratesADeletedLoadBalancer(boolean classicDeleted) {
ok(enterStandby(true, "i-a"));
reconciler.reconcile(group());
String activityId = ok(exitStandby("i-a")).getString(EXITED + ".ActivityId");
if (classicDeleted) {
when(elbClassic.registerInstances(REGION, CLASSIC, List.of("i-a")))
.thenThrow(new AwsException("LoadBalancerNotFound", "deleted", 400));
} else {
doThrow(new AwsException("TargetGroupNotFound", "deleted", 400))
.when(elbV2).registerTargets(eq(REGION), eq(TARGET_GROUP), anyList());
}
reconciler.reconcile(group());
assertEquals("InService", lifecycle("i-a"));
assertEquals("Successful", activity(describeScalingActivities(), activityId, "StatusCode"));
verify(elbV2).registerTargets(eq(REGION), eq(TARGET_GROUP), anyList());
verify(elbClassic).registerInstances(REGION, CLASSIC, List.of("i-a"));
assertNoLaunch();
}
@Test
void launchRegistrationPrecedesInServiceWithoutAStandbyActivity() {
AsgInstance launching = group().getInstances().stream()
.filter(instance -> "i-a".equals(instance.getInstanceId())).findFirst().orElseThrow();
launching.setLifecycleState("Pending");
assertNull(launching.getStandbyActivityId());
doAnswer(invocation -> {
assertEquals("Pending", launching.getLifecycleState());
return null;
}).when(elbV2).registerTargets(eq(REGION), eq(TARGET_GROUP), anyList());
when(elbClassic.registerInstances(REGION, CLASSIC, List.of("i-a"))).thenAnswer(invocation -> {
assertEquals("Pending", launching.getLifecycleState());
return List.of();
});
reconciler.reconcile(group());
assertEquals("InService", launching.getLifecycleState());
verify(elbV2).registerTargets(eq(REGION), eq(TARGET_GROUP), anyList());
verify(elbClassic).registerInstances(REGION, CLASSIC, List.of("i-a"));
assertNoLaunch();
}
private Response enterStandby(boolean decrement, String... instanceIds) {
MultivaluedHashMap<String, String> params = params(GROUP);
for (int index = 0; index < instanceIds.length; index++) {