mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(ec2): seed the default network before marking the scope seeded (#6637)
* fix(ec2): seed the default network before marking the scope seeded ensureDefaultResources added the account/region to the seeded set before seeding, so a concurrent first call returned at once and read a partial or empty default VPC. seed under a lock and mark the scope afterwards. * test(ec2): check the seed race with ParkingStore instead of a fixed wait The first call parks in its first subnet write and the test waits until the second call is blocked on the seed lock, so it fails at once on the old code and no longer sleeps two seconds.
This commit is contained in:
1 parent
0ec28aee68
commit
79e47cd7fc
2 files changed
+92
-1
No files matched your search
@@ -1427,9 +1427,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
public void ensureDefaultResources(String region) {
|
||||
String ownerAccountId = callerAccountId();
|
||||
String seedScope = ownerAccountId + "::" + region;
|
||||
if (!seededAccountRegions.add(seedScope)) {
|
||||
if (seededAccountRegions.contains(seedScope)) {
|
||||
return;
|
||||
}
|
||||
// Mark the scope only once it is seeded: a concurrent first call waits here instead of
|
||||
// reading a half-built default network (#6247).
|
||||
synchronized (seededAccountRegions) {
|
||||
if (seededAccountRegions.contains(seedScope)) {
|
||||
return;
|
||||
}
|
||||
seedDefaultResources(region, ownerAccountId);
|
||||
seededAccountRegions.add(seedScope);
|
||||
}
|
||||
}
|
||||
|
||||
private void seedDefaultResources(String region, String ownerAccountId) {
|
||||
List<Vpc> existingVpcs = vpcs.scan(k -> k.startsWith(region + "::"));
|
||||
// Already provisioned in a previous run and reloaded from persistent storage: the default
|
||||
// VPC (and everything else) is present, so don't re-seed and create duplicates (#1297).
|
||||
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Subnet;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.github.hectorvent.floci.testing.ParkingStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Regression test for issue #6247: a call that arrives while the first call for an account and
|
||||
* region is still seeding the default network waits for it instead of reading a partial network.
|
||||
*/
|
||||
class Ec2DefaultResourcesSeedTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Test
|
||||
void aConcurrentFirstCallSeesTheWholeDefaultNetwork() throws Exception {
|
||||
ParkingStore<Subnet> subnets = new ParkingStore<>();
|
||||
Ec2Service service = newService(subnets);
|
||||
// Holds the first call in the middle of seeding, before any default subnet is stored.
|
||||
subnets.parkNextWrite();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<?> first = pool.submit(() -> service.ensureDefaultResources(REGION));
|
||||
assertTrue(subnets.awaitParked(), "the first call must reach its first subnet write");
|
||||
AtomicReference<Thread> secondThread = new AtomicReference<>();
|
||||
Future<Integer> second = pool.submit(() -> {
|
||||
secondThread.set(Thread.currentThread());
|
||||
return service.describeSubnets(REGION, List.of(), Map.of()).size();
|
||||
});
|
||||
awaitBlockedOrDone(secondThread, second);
|
||||
assertFalse(second.isDone(), "the second call read the default network while it was still being seeded");
|
||||
|
||||
subnets.release();
|
||||
first.get(2, TimeUnit.SECONDS);
|
||||
assertEquals(3, second.get(2, TimeUnit.SECONDS), "the second call sees the three default subnets");
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
private Ec2Service newService(StorageBackend<String, Subnet> subnets) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.Ec2ServiceConfig ec2 = mock(EmulatorConfig.Ec2ServiceConfig.class);
|
||||
when(config.defaultAccountId()).thenReturn("000000000000");
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.ec2()).thenReturn(ec2);
|
||||
when(ec2.mock()).thenReturn(true);
|
||||
when(ec2.mockPublicAddresses()).thenReturn(EmulatorConfig.Ec2ServiceConfig.MockPublicAddresses.NONE);
|
||||
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
|
||||
return new Ec2Service(config, null, mock(Ec2PortForwardManager.class),
|
||||
new AmiImageResolver(imageCatalog), imageCatalog,
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorage<>(), subnets, new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user