diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java index 725e79d6b5..cd5690ceea 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java @@ -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. * *
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;
+ }
}
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingStandbyTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingStandbyTest.java
index 0c53cb1f7a..c40b927423 100644
--- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingStandbyTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingStandbyTest.java
@@ -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