fix(ec2): let only the latest service-network lookup publish its address (#7583)

StartInstances keeps the container, so the container check alone let a lookup started before a stop overwrite the address learned after the next start. Each lookup now takes a sequence number under the instance lock and publishes only while it is still the latest for the instance.

fix(ec2): drop service-network lookup state when an instance terminates

The lookup sequence and retry marker were cleared only when a launch failed, so every instance terminated normally kept an entry for the life of the process. terminate now clears both under the instance lock, and a lookup that runs once the instance is shutting down or terminated starts nothing and records nothing.

test(ec2): share the paused service-network lookup between the race tests

The replacement, stop-and-start and teardown tests each built the same paused lookup by hand: two latches, an inspect that blocks on its first call, and a virtual-thread lookup joined after the release. PausedLookup holds that setup once.

Closes #7582
This commit is contained in:
jibin-jusdb authored and GitHub committed 2026-11-05 13:58:32 -04:00
1 parent e42f97df08
commit a613d7b0c3
2 files changed
+110 -32

No files matched your search

@@ -165,6 +165,8 @@ public class Ec2ContainerManager {
private final Map<String, CompletableFuture<Void>> lifecycleOperations = new ConcurrentHashMap<>();
/** Instances whose service-network lookup failed and is retried on the next address read. */
private final Set<String> serviceNetworkRetries = ConcurrentHashMap.newKeySet();
/** The latest service-network lookup started per instance; only that one may publish. */
private final Map<String, Long> serviceNetworkLookups = new ConcurrentHashMap<>();
private final Set<String> serviceNetworkRetriesRunning = ConcurrentHashMap.newKeySet();
/** Runs service-network lookup retries; see {@link #createServiceNetworkRetryExecutor}. */
private final ExecutorService serviceNetworkRetryExecutor;
@@ -797,6 +799,7 @@ public class Ec2ContainerManager {
instance.setContainerBridgeIp(null);
instance.setServiceNetworkIp(null);
serviceNetworkRetries.remove(instance.getInstanceId());
serviceNetworkLookups.remove(instance.getInstanceId());
}
if (alreadyCleaned) {
return;
@@ -1372,19 +1375,26 @@ public class Ec2ContainerManager {
*/
void refreshServiceNetworkIp(Instance instance) {
String instanceId = instance.getInstanceId();
// The container is the lookup's generation: a result is published only while the instance
// still runs that container, so a lookup that outlives a replacement or a teardown cannot
// overwrite what came after it. Docker is asked outside the instance lock.
// A result is published only while the instance still runs the container the lookup read
// and no later lookup has started, so a lookup that outlives a replacement, a teardown or a
// stop and start of the same container cannot overwrite what came after it. Docker is asked
// outside the instance lock.
String containerId;
long lookup;
synchronized (instance) {
if (isLaunchCancelledState(instance)) {
// Torn down or going: nothing to report, and no lookup state to leave behind.
return;
}
containerId = instance.getDockerContainerId();
lookup = serviceNetworkLookups.merge(instanceId, 1L, Long::sum);
}
String address;
try {
address = resolveServiceNetworkIp(instanceId, containerId);
} catch (RuntimeException e) {
synchronized (instance) {
if (containerId != null && containerId.equals(instance.getDockerContainerId())) {
if (containerId != null && isLatestServiceNetworkLookup(instance, containerId, lookup)) {
// A Docker hiccup is not an answer: keep what was recorded and let the next
// address read retry the lookup in the background.
serviceNetworkRetries.add(instanceId);
@@ -1395,7 +1405,7 @@ public class Ec2ContainerManager {
return;
}
synchronized (instance) {
if (!Objects.equals(containerId, instance.getDockerContainerId())) {
if (!isLatestServiceNetworkLookup(instance, containerId, lookup)) {
return;
}
instance.setServiceNetworkIp(address);
@@ -1403,6 +1413,16 @@ public class Ec2ContainerManager {
}
}
boolean hasServiceNetworkLookupStateForTest(String instanceId) {
return serviceNetworkLookups.containsKey(instanceId) || serviceNetworkRetries.contains(instanceId);
}
/** Called under the instance lock. */
private boolean isLatestServiceNetworkLookup(Instance instance, String containerId, long lookup) {
return Objects.equals(containerId, instance.getDockerContainerId())
&& Objects.equals(lookup, serviceNetworkLookups.get(instance.getInstanceId()));
}
/**
* Retries a failed {@link #refreshServiceNetworkIp} in the background, at most one attempt per
* instance at a time, so the address read that notices it never waits on Docker.
@@ -1536,6 +1556,9 @@ public class Ec2ContainerManager {
firewallEniId = instance.getNetworkInterfaces() == null || instance.getNetworkInterfaces().isEmpty()
? null : instance.getNetworkInterfaces().getFirst().getNetworkInterfaceId();
instance.setState(InstanceState.shuttingDown());
// In-flight lookups no longer match and publish nothing; later ones see the state.
serviceNetworkRetries.remove(instance.getInstanceId());
serviceNetworkLookups.remove(instance.getInstanceId());
}
executor.submit(() -> {
// The post-teardown hook's only precondition; see the Javadoc above for why.