fix(elbv2): move persisted-listener restore out of @PostConstruct (#2015)

* fix(elbv2): move persisted-listener restore out of @PostConstruct

ElbV2Service called back into itself through its own CDI client proxy while it
was still being constructed. rebuildIndexes(), reached from the @PostConstruct
initializeStorage(), called dataPlane.startListener() for every restored
listener; ElbV2DataPlane.binding() then called elbV2Service.getLoadBalancer(),
and because Arc had not yet registered the instance in the ApplicationScoped
context the proxy started another bean creation. That recursed until the process
died.

Only reproducible after a restart with persistent storage: on a fresh boot the
listener map is empty, so rebuildIndexes() never reaches startListener and the
callback never happens.

rebuildIndexes() now only builds the in-memory indexes. Starting the data plane
and the health checks moved to restorePersistedRuntime(), invoked from
EmulatorLifecycle after storageFactory.loadAll() alongside the four services that
already restore persisted runtime there. With the bean fully constructed before
any listener starts, the callback resolves normally.

The blast radius was wider than the issue title: the bean is created lazily on
first use, so after a restart every ELBv2 operation failed, not just DeleteStack.
DescribeLoadBalancers and DescribeTargetGroups both returned 500.

Note the symptom changed with #1931: the storage dedupe removed the per-level
allocation, so current main raises StackOverflowError where 1.5.32 exhausted the
heap and was OOM-killed.

Closes #1913

* test(elbv2): cover the enabled and disabled lifecycle restore paths

elbv2 was stubbed disabled in setup, so nothing exercised the new
EmulatorLifecycle call: dropping it entirely would have left the suite green.

Pin both halves of the gate — restore runs after loadAll when elbv2 is enabled,
and does not run when it is disabled. Verified the ordering assertion fails with
'Wanted but not invoked' when the lifecycle call is removed.

Refs #1913
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-27 22:23:50 -05:00
1 parent 836c162136
commit 468e943c83
4 files changed
+108 -8

No files matched your search

@@ -57,6 +57,7 @@ class EmulatorLifecycleTest {
@Mock private EmulatorConfig.StorageConfig storageConfig;
@Mock private EmulatorConfig.ServicesConfig servicesConfig;
@Mock private EmulatorConfig.Ec2ServiceConfig ec2ServiceConfig;
@Mock private EmulatorConfig.ElbV2ServiceConfig elbv2ServiceConfig;
@Mock private ElastiCacheContainerManager elastiCacheContainerManager;
@Mock private ElastiCacheMemcachedContainerManager elastiCacheMemcachedContainerManager;
@Mock private ElastiCacheProxyManager elastiCacheProxyManager;
@@ -69,6 +70,7 @@ class EmulatorLifecycleTest {
@Mock private NeptuneProxyManager neptuneProxyManager;
@Mock private io.github.hectorvent.floci.services.amazonmq.container.RabbitMqManager rabbitMqManager;
@Mock private RdsService rdsService;
@Mock private io.github.hectorvent.floci.services.elbv2.ElbV2Service elbV2Service;
@Mock private InitializationHooksRunner initializationHooksRunner;
@Mock private SqsEventSourcePoller sqsPoller;
@Mock private KinesisEventSourcePoller kinesisPoller;
@@ -90,6 +92,8 @@ class EmulatorLifecycleTest {
Mockito.lenient().when(config.services()).thenReturn(servicesConfig);
Mockito.lenient().when(servicesConfig.ec2()).thenReturn(ec2ServiceConfig);
Mockito.lenient().when(ec2ServiceConfig.enabled()).thenReturn(false);
Mockito.lenient().when(servicesConfig.elbv2()).thenReturn(elbv2ServiceConfig);
Mockito.lenient().when(elbv2ServiceConfig.enabled()).thenReturn(false);
Mockito.lenient().when(config.tls()).thenReturn(tlsConfig);
Mockito.lenient().when(tlsConfig.enabled()).thenReturn(false);
@@ -99,7 +103,7 @@ class EmulatorLifecycleTest {
elastiCacheProxyManager, rdsContainerManager, rdsProxyManager,
memoryDbContainerManager, memoryDbProxyManager,
docDbContainerManager, neptuneContainerManager, neptuneProxyManager,
rabbitMqManager, rdsService,
rabbitMqManager, rdsService, elbV2Service,
initializationHooksRunner, sqsPoller, kinesisPoller, dynamodbStreamsPoller,
pipesService, ec2MetadataServer, ecrRegistryManager, flociUiManager, initLifecycleState,
schemaCreationWorker, containerTeardowns, persistentPathValidator);
@@ -129,6 +133,36 @@ class EmulatorLifecycleTest {
inOrder.verify(rdsService).restorePersistedRuntime();
}
@Test
@DisplayName("Should restore ELBv2 persisted runtime after loading storage when elbv2 is enabled")
void shouldRestoreElbV2PersistedRuntimeAfterStorageLoad() {
// The restore has to happen after the bean is constructed, not from @PostConstruct, or
// ElbV2DataPlane's callback re-enters bean creation (#1913). Pin the ordering so the call
// cannot be dropped or moved back into construction unnoticed.
stubStorageConfig();
when(elbv2ServiceConfig.enabled()).thenReturn(true);
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
var inOrder = Mockito.inOrder(storageFactory, elbV2Service);
inOrder.verify(storageFactory).loadAll();
inOrder.verify(elbV2Service).restorePersistedRuntime();
}
@Test
@DisplayName("Should not restore ELBv2 persisted runtime when elbv2 is disabled")
void shouldNotRestoreElbV2PersistedRuntimeWhenDisabled() {
stubStorageConfig();
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
Mockito.verify(elbV2Service, Mockito.never()).restorePersistedRuntime();
}
@Test
@DisplayName("Should validate the persistent path after BOOT hooks and before loading storage")
void shouldValidatePersistentPathBeforeStorageLoad() {
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -179,6 +180,9 @@ class ElbV2ServiceTest {
assertTrue(reloadedRules.stream().anyMatch(candidate -> "10".equals(candidate.getPriority())));
assertEquals("test", reloaded.describeTags(List.of(lbArn)).get(lbArn).get("env"));
assertThrows(AwsException.class, () -> reloaded.deleteTargetGroup(REGION, tgArn));
// Data-plane and health-check startup is deliberately not part of construction (#1913).
reloaded.restorePersistedRuntime();
verify(reloadedHealthChecker).startMonitoring(any(TargetGroup.class));
ArgumentCaptor<List<TargetDescription>> targetsCaptor = ArgumentCaptor.captor();
verify(reloadedHealthChecker).addTargets(eq(tgArn), targetsCaptor.capture(), any(TargetGroup.class));
@@ -193,7 +197,41 @@ class ElbV2ServiceTest {
}
@Test
void initializeStorageStartsPersistedListenersWithoutTargetGroups() {
void initializeStorageDoesNotTouchCollaborators() {
// #1913: ElbV2DataPlane.binding() calls back into this service, and during @PostConstruct the
// ApplicationScoped context has no instance yet — so CDI re-enters bean creation and recurses
// until the stack (or, before the storage dedupe in #1931, the heap) is gone. Nothing reachable
// from initializeStorage() may call an injected collaborator. A mock cannot reproduce the
// recursion, since it never calls back, so assert the absence of the interaction instead.
SharedStorageFactory storageFactory = new SharedStorageFactory();
ElbV2Service first = serviceWithStorage(storageFactory, mock(ElbV2DataPlane.class), mock(ElbV2HealthChecker.class));
String lbArn = first.createLoadBalancer(
REGION, "recursion-lb", "internal", "application", "ipv4",
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
String tgArn = first.createTargetGroup(
REGION, "recursion-tg", "HTTP", "HTTP1", 8080, "vpc-a", "instance",
"HTTP", "traffic-port", true, "/health", 15, 5, 3, 2, "200",
"ipv4", Map.of()).getTargetGroupArn();
String listenerArn = first.createListener(
REGION, lbArn, "HTTP", 9090, null, List.of(),
List.of(forwardAction(tgArn)), List.of(), Map.of()).getListenerArn();
ElbV2DataPlane reloadedDataPlane = mock(ElbV2DataPlane.class);
ElbV2HealthChecker reloadedHealthChecker = mock(ElbV2HealthChecker.class);
ElbV2Service reloaded = serviceWithStorage(storageFactory, reloadedDataPlane, reloadedHealthChecker);
verifyNoInteractions(reloadedDataPlane, reloadedHealthChecker);
reloaded.restorePersistedRuntime();
ArgumentCaptor<Listener> listenerCaptor = ArgumentCaptor.captor();
verify(reloadedDataPlane).startListener(listenerCaptor.capture(), eq(REGION), anyList());
assertEquals(listenerArn, listenerCaptor.getValue().getListenerArn());
verify(reloadedHealthChecker).startMonitoring(any(TargetGroup.class));
}
@Test
void restorePersistedRuntimeStartsPersistedListenersWithoutTargetGroups() {
SharedStorageFactory storageFactory = new SharedStorageFactory();
ElbV2Service first = serviceWithStorage(storageFactory, mock(ElbV2DataPlane.class), mock(ElbV2HealthChecker.class));
String lbArn = first.createLoadBalancer(
@@ -204,7 +242,8 @@ class ElbV2ServiceTest {
List.of(), List.of(), Map.of()).getListenerArn();
ElbV2DataPlane reloadedDataPlane = mock(ElbV2DataPlane.class);
serviceWithStorage(storageFactory, reloadedDataPlane, mock(ElbV2HealthChecker.class));
ElbV2Service reloaded = serviceWithStorage(storageFactory, reloadedDataPlane, mock(ElbV2HealthChecker.class));
reloaded.restorePersistedRuntime();
ArgumentCaptor<Listener> listenerCaptor = ArgumentCaptor.captor();
verify(reloadedDataPlane).startListener(listenerCaptor.capture(), eq(REGION), anyList());