mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elasticache): re-provision caches on restart instead of reporting a dead endpoint as available (#4094)
* fix(elasticache): re-provision caches on restart instead of reporting a dead endpoint as available Only cluster-mode replication groups were re-provisioned on boot. Cluster-mode-disabled groups and Memcached clusters were deliberately out of scope, so a restart removed their containers (EmulatorLifecycle stops and removes them on ShutdownEvent) and nothing brought them back, while the persisted record kept its `available` status: the control plane answered healthy with nothing behind the endpoint, and every connection failed. The proxy port made it worse. `usedPorts` is process-local and was only re-reserved inside the cluster-mode branch, so a restarted group's advertised port was free for the next create to take, pointing the old endpoint at an unrelated cache rather than failing cleanly. Restore now covers every record, mirroring the create path for the shape it restores: the container and auth proxy come back for a cluster-mode-disabled group, the container for a Memcached cluster. Ports are reserved and records marked `creating` synchronously, before anything can be served; the container work runs in the background so a slow Docker daemon cannot delay readiness. A record whose data plane cannot come back is reported `create-failed` (`restore-failed` for a cache cluster, the value AWS models there) with no endpoint, and its port is released. No reachable Docker daemon stays the documented degraded mode rather than a failure: the record keeps its metadata and reports available without a container, as it does on create. CacheClusterStatus gains RESTORE_FAILED and a wireName(), matching ReplicationGroupStatus. Both are values AWS models: CacheClusterStatus is one of available, creating, deleted, deleting, incompatible-network, modifying, rebooting cluster nodes, restore-failed or snapshotting, and ReplicationGroup Status is one of creating, available, modifying, deleting, create-failed or snapshotting. * fix(elasticache): take the group monitor before a restore writes back Restore ran its data-plane work and its write-back outside the per-record monitor `deleteReplicationGroup` takes, so a delete that arrived in the first seconds after boot, while the record was still `creating`, could be undone: the delete removed the record and released its port, then the restore finished starting the container and proxy and put the record back as `available`. The group the caller was told had been deleted came back, with a live cache behind it. `deleteCacheCluster` took no monitor at all, so Memcached had the same race with nothing to synchronize against. The container work still runs outside the monitor, as it does on create: a restore that may pull an image must not hold a delete behind it. Only the write-back takes the monitor, and it re-reads the record first. A record that is gone, or has become `deleting`, is not written back; the container the attempt started is stopped, and the port is left alone because the delete that removed the record released it already. Releasing it a second time would return a port a later create had since taken to the pool. The same guard covers the failure paths: a restore that fails for a record deleted meanwhile tears down but reports nothing, rather than resurrecting the record as `create-failed`. `ElastiCacheMemcachedService` gains the per-cluster monitor `ElastiCacheService` already had, taken by `deleteCacheCluster` and the restore write-back.
This commit is contained in:
1 parent
532bae5525
commit
14d13ee798
9 files changed
+700
-73
No files matched your search
@@ -5,13 +5,13 @@ import io.github.hectorvent.floci.core.common.ContainerTeardown;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentPathValidator;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.lifecycle.InitLifecycleState;
|
||||
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
|
||||
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner;
|
||||
import io.github.hectorvent.floci.services.appsync.graphql.SchemaCreationWorker;
|
||||
import io.github.hectorvent.floci.services.docdb.container.DocDbContainerManager;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.elasticache.ElastiCacheMemcachedService;
|
||||
import io.github.hectorvent.floci.services.elasticache.ElastiCacheService;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheMemcachedContainerManager;
|
||||
@@ -39,6 +39,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
@@ -73,6 +74,7 @@ class EmulatorLifecycleTest {
|
||||
@Mock private IamService iamService;
|
||||
@Mock private EmulatorConfig.ElastiCacheServiceConfig elastiCacheServiceConfig;
|
||||
@Mock private ElastiCacheService elastiCacheService;
|
||||
@Mock private ElastiCacheMemcachedService elastiCacheMemcachedService;
|
||||
@Mock private ElastiCacheContainerManager elastiCacheContainerManager;
|
||||
@Mock private ElastiCacheMemcachedContainerManager elastiCacheMemcachedContainerManager;
|
||||
@Mock private ElastiCacheProxyManager elastiCacheProxyManager;
|
||||
@@ -131,7 +133,7 @@ class EmulatorLifecycleTest {
|
||||
|
||||
emulatorLifecycle = new EmulatorLifecycle(
|
||||
storageFactory, serviceRegistry, config,
|
||||
iamService, elastiCacheService,
|
||||
iamService, elastiCacheService, elastiCacheMemcachedService,
|
||||
elastiCacheContainerManager, elastiCacheMemcachedContainerManager,
|
||||
elastiCacheProxyManager, rdsContainerManager, rdsProxyManager,
|
||||
memoryDbContainerManager, memoryDbProxyManager,
|
||||
@@ -206,15 +208,18 @@ class EmulatorLifecycleTest {
|
||||
stubStorageConfig();
|
||||
when(elastiCacheServiceConfig.enabled()).thenReturn(true);
|
||||
when(elastiCacheService.restorePersistedRuntime())
|
||||
.thenReturn(java.util.concurrent.CompletableFuture.completedFuture(null));
|
||||
.thenReturn(CompletableFuture.completedFuture(null));
|
||||
when(elastiCacheMemcachedService.restorePersistedRuntime())
|
||||
.thenReturn(CompletableFuture.completedFuture(null));
|
||||
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, elastiCacheService);
|
||||
InOrder inOrder = Mockito.inOrder(storageFactory, elastiCacheService, elastiCacheMemcachedService);
|
||||
inOrder.verify(storageFactory).loadAll();
|
||||
inOrder.verify(elastiCacheService).restorePersistedRuntime();
|
||||
inOrder.verify(elastiCacheMemcachedService).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -227,6 +232,7 @@ class EmulatorLifecycleTest {
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
|
||||
Mockito.verify(elastiCacheService, Mockito.never()).restorePersistedRuntime();
|
||||
Mockito.verify(elastiCacheMemcachedService, Mockito.never()).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+94
@@ -12,12 +12,16 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -144,4 +148,94 @@ class ElastiCacheMemcachedServiceTest {
|
||||
service.deleteCacheCluster("no-docker-cluster");
|
||||
verify(containerManager, never()).stop(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restorePersistedRuntimeRestartsTheContainerAndRepointsTheEndpoint() {
|
||||
StorageFactory storageFactory = sharedStorageFactory();
|
||||
ElastiCacheMemcachedContainerManager beforeRestart = mock(ElastiCacheMemcachedContainerManager.class);
|
||||
when(beforeRestart.tryStart(anyString(), anyString()))
|
||||
.thenReturn(new ElastiCacheContainerHandle("cid", "my-cluster", "localhost", 32770));
|
||||
serviceWith(storageFactory, beforeRestart).createCacheCluster("my-cluster");
|
||||
|
||||
ElastiCacheMemcachedContainerManager restarted = mock(ElastiCacheMemcachedContainerManager.class);
|
||||
when(restarted.tryStart(anyString(), anyString()))
|
||||
.thenReturn(new ElastiCacheContainerHandle("cid2", "my-cluster", "localhost", 32771));
|
||||
ElastiCacheMemcachedService restartedService = serviceWith(storageFactory, restarted);
|
||||
|
||||
restartedService.restorePersistedRuntime().join();
|
||||
|
||||
verify(restarted).tryStart(eq("my-cluster"), anyString());
|
||||
CacheCluster cluster = restartedService.getCacheCluster("my-cluster");
|
||||
assertEquals(CacheClusterStatus.AVAILABLE, cluster.getCacheClusterStatus());
|
||||
assertEquals(32771, cluster.getConfigurationEndpoint().port(),
|
||||
"Docker publishes a fresh host port per run, so the endpoint must follow it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void memcachedRestoreFailureReportsRestoreFailed() {
|
||||
StorageFactory storageFactory = sharedStorageFactory();
|
||||
ElastiCacheMemcachedContainerManager beforeRestart = mock(ElastiCacheMemcachedContainerManager.class);
|
||||
when(beforeRestart.tryStart(anyString(), anyString()))
|
||||
.thenReturn(new ElastiCacheContainerHandle("cid", "my-cluster", "localhost", 11211));
|
||||
serviceWith(storageFactory, beforeRestart).createCacheCluster("my-cluster");
|
||||
|
||||
ElastiCacheMemcachedContainerManager restarted = mock(ElastiCacheMemcachedContainerManager.class);
|
||||
when(restarted.tryStart(anyString(), anyString()))
|
||||
.thenThrow(new RuntimeException("container failed"));
|
||||
ElastiCacheMemcachedService restartedService = serviceWith(storageFactory, restarted);
|
||||
|
||||
restartedService.restorePersistedRuntime().join();
|
||||
|
||||
CacheCluster cluster = restartedService.getCacheCluster("my-cluster");
|
||||
assertEquals(CacheClusterStatus.RESTORE_FAILED, cluster.getCacheClusterStatus());
|
||||
assertNull(cluster.getConfigurationEndpoint(),
|
||||
"A cluster whose container is gone must not advertise an endpoint");
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreDoesNotResurrectAClusterDeletedWhileItWasRestoring() {
|
||||
StorageFactory storageFactory = sharedStorageFactory();
|
||||
ElastiCacheMemcachedContainerManager beforeRestart = mock(ElastiCacheMemcachedContainerManager.class);
|
||||
when(beforeRestart.tryStart(anyString(), anyString()))
|
||||
.thenReturn(new ElastiCacheContainerHandle("cid", "my-cluster", "localhost", 32770));
|
||||
serviceWith(storageFactory, beforeRestart).createCacheCluster("my-cluster");
|
||||
|
||||
ElastiCacheMemcachedContainerManager restarted = mock(ElastiCacheMemcachedContainerManager.class);
|
||||
ElastiCacheMemcachedService restartedService = serviceWith(storageFactory, restarted);
|
||||
ElastiCacheContainerHandle restoredHandle =
|
||||
new ElastiCacheContainerHandle("cid2", "my-cluster", "localhost", 32771);
|
||||
// The delete lands in the window the cluster's monitor closes: the container is up, the
|
||||
// record has not been written back yet.
|
||||
when(restarted.tryStart(anyString(), anyString())).thenAnswer(inv -> {
|
||||
restartedService.deleteCacheCluster("my-cluster");
|
||||
return restoredHandle;
|
||||
});
|
||||
|
||||
restartedService.restorePersistedRuntime().join();
|
||||
|
||||
assertThrows(AwsException.class, () -> restartedService.getCacheCluster("my-cluster"),
|
||||
"A cluster deleted while it was restoring must stay deleted");
|
||||
verify(restarted).stop(restoredHandle);
|
||||
}
|
||||
|
||||
private static StorageFactory sharedStorageFactory() {
|
||||
StorageFactory storageFactory = mock(StorageFactory.class);
|
||||
Map<String, Object> backends = new ConcurrentHashMap<>();
|
||||
when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(inv ->
|
||||
backends.computeIfAbsent(inv.getArgument(1, String.class),
|
||||
key -> AccountAwareStorageBackend.inMemory("000000000000")));
|
||||
return storageFactory;
|
||||
}
|
||||
|
||||
private static ElastiCacheMemcachedService serviceWith(StorageFactory storageFactory,
|
||||
ElastiCacheMemcachedContainerManager containerManager) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig servicesConfig = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.ElastiCacheServiceConfig ecConfig = mock(EmulatorConfig.ElastiCacheServiceConfig.class);
|
||||
when(config.services()).thenReturn(servicesConfig);
|
||||
when(servicesConfig.elasticache()).thenReturn(ecConfig);
|
||||
when(ecConfig.defaultMemcachedImage()).thenReturn("memcached:1.6");
|
||||
when(config.hostname()).thenReturn(Optional.of("localhost"));
|
||||
return new ElastiCacheMemcachedService(containerManager, storageFactory, config);
|
||||
}
|
||||
}
|
||||
+142
@@ -485,6 +485,148 @@ class ElastiCacheServiceTest {
|
||||
.thenReturn(new ElastiCacheContainerHandle("cid", "grp", "localhost", 6379));
|
||||
}
|
||||
|
||||
private static void stubSingleNodeContainer(ElastiCacheContainerManager containerManager) {
|
||||
when(containerManager.tryStart(anyString(), anyString())).thenAnswer(inv ->
|
||||
new ElastiCacheContainerHandle("cid-" + inv.getArgument(0, String.class),
|
||||
inv.getArgument(0, String.class), "localhost", 6379));
|
||||
}
|
||||
|
||||
private static StorageFactory storageWithSingleNodeGroup(String groupId) {
|
||||
StorageFactory storageFactory = sharedStorageFactory();
|
||||
ElastiCacheContainerManager beforeRestart = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(beforeRestart);
|
||||
serviceWith(storageFactory, beforeRestart, mock(ElastiCacheProxyManager.class),
|
||||
mock(ValkeyClusterFormation.class))
|
||||
.createReplicationGroup(groupId, "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
return storageFactory;
|
||||
}
|
||||
|
||||
@Test
|
||||
void restorePersistedRuntimeReprovisionsSingleNodeGroups() {
|
||||
StorageFactory storageFactory = storageWithSingleNodeGroup("grp");
|
||||
|
||||
ElastiCacheContainerManager restartedContainers = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(restartedContainers);
|
||||
ElastiCacheProxyManager restartedProxies = mock(ElastiCacheProxyManager.class);
|
||||
ElastiCacheService restarted = serviceWith(storageFactory, restartedContainers,
|
||||
restartedProxies, mock(ValkeyClusterFormation.class));
|
||||
|
||||
restarted.restorePersistedRuntime().join();
|
||||
|
||||
verify(restartedContainers).tryStart(eq("grp"), anyString());
|
||||
verify(restartedProxies).startProxy(eq("grp"), eq(AuthMode.PASSWORD), eq(16379),
|
||||
eq("localhost"), eq(6379), any());
|
||||
ReplicationGroup restored = restarted.getReplicationGroup("grp");
|
||||
assertEquals(ReplicationGroupStatus.AVAILABLE, restored.getStatus());
|
||||
assertEquals(16379, restored.getConfigurationEndpoint().port());
|
||||
assertEquals("cid-grp", restored.getContainerId(),
|
||||
"A restored group must track the container it actually has");
|
||||
|
||||
ReplicationGroup next =
|
||||
restarted.createReplicationGroup("grp2", "test", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
assertEquals(16380, next.getProxyPort(),
|
||||
"A restored group's port must be reserved again so new groups cannot take it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleNodeRestoreFailureReportsCreateFailedAndReleasesThePort() {
|
||||
StorageFactory storageFactory = storageWithSingleNodeGroup("grp");
|
||||
|
||||
ElastiCacheContainerManager restartedContainers = mock(ElastiCacheContainerManager.class);
|
||||
// Only the restore fails: the create that checks the port was freed must still get through.
|
||||
when(restartedContainers.tryStart(eq("grp"), anyString()))
|
||||
.thenThrow(new RuntimeException("container failed"));
|
||||
ElastiCacheProxyManager restartedProxies = mock(ElastiCacheProxyManager.class);
|
||||
ElastiCacheService restarted = serviceWith(storageFactory, restartedContainers,
|
||||
restartedProxies, mock(ValkeyClusterFormation.class));
|
||||
|
||||
restarted.restorePersistedRuntime().join();
|
||||
|
||||
ReplicationGroup failed = restarted.getReplicationGroup("grp");
|
||||
assertEquals(ReplicationGroupStatus.CREATE_FAILED, failed.getStatus());
|
||||
assertNull(failed.getConfigurationEndpoint(),
|
||||
"A group whose data plane is gone must not advertise an endpoint");
|
||||
verify(restartedProxies, never()).startProxy(anyString(), any(), anyInt(), anyString(), anyInt(), any());
|
||||
|
||||
ReplicationGroup next =
|
||||
restarted.createReplicationGroup("grp2", "test", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
assertEquals(16379, next.getProxyPort(),
|
||||
"The failed restore's port must be released for the next group");
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleNodeRestoreWithoutADockerDaemonKeepsTheGroupAvailable() {
|
||||
StorageFactory storageFactory = storageWithSingleNodeGroup("grp");
|
||||
|
||||
ElastiCacheContainerManager restartedContainers = mock(ElastiCacheContainerManager.class);
|
||||
when(restartedContainers.tryStart(anyString(), anyString())).thenReturn(null);
|
||||
ElastiCacheProxyManager restartedProxies = mock(ElastiCacheProxyManager.class);
|
||||
ElastiCacheService restarted = serviceWith(storageFactory, restartedContainers,
|
||||
restartedProxies, mock(ValkeyClusterFormation.class));
|
||||
|
||||
restarted.restorePersistedRuntime().join();
|
||||
|
||||
ReplicationGroup restored = restarted.getReplicationGroup("grp");
|
||||
assertEquals(ReplicationGroupStatus.AVAILABLE, restored.getStatus(),
|
||||
"No reachable daemon is the create path's documented degraded mode, not a failure");
|
||||
assertNull(restored.getContainerId());
|
||||
verify(restartedProxies, never()).startProxy(anyString(), any(), anyInt(), anyString(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreDoesNotResurrectAGroupDeletedWhileItWasRestoring() {
|
||||
StorageFactory storageFactory = storageWithSingleNodeGroup("grp");
|
||||
|
||||
ElastiCacheContainerManager restartedContainers = mock(ElastiCacheContainerManager.class);
|
||||
ElastiCacheProxyManager restartedProxies = mock(ElastiCacheProxyManager.class);
|
||||
ElastiCacheService restarted = serviceWith(storageFactory, restartedContainers,
|
||||
restartedProxies, mock(ValkeyClusterFormation.class));
|
||||
ElastiCacheContainerHandle restoredHandle =
|
||||
new ElastiCacheContainerHandle("cid-grp-restored", "grp", "localhost", 6379);
|
||||
// The delete lands in the window the group's monitor closes: the container is up, the
|
||||
// record has not been written back yet.
|
||||
when(restartedContainers.tryStart(eq("grp"), anyString())).thenAnswer(inv -> {
|
||||
restarted.deleteReplicationGroup("grp");
|
||||
// Takes the port that delete just freed, so a restore that released it a second
|
||||
// time would hand the same port out twice.
|
||||
restarted.createReplicationGroup("grp2", "test", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
return restoredHandle;
|
||||
});
|
||||
|
||||
restarted.restorePersistedRuntime().join();
|
||||
|
||||
assertThrows(AwsException.class, () -> restarted.getReplicationGroup("grp"),
|
||||
"A group deleted while it was restoring must stay deleted");
|
||||
verify(restartedProxies, never()).startProxy(eq("grp"), any(), anyInt(), anyString(), anyInt(), any());
|
||||
verify(restartedContainers).stop(restoredHandle);
|
||||
|
||||
ReplicationGroup next =
|
||||
restarted.createReplicationGroup("grp3", "test", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
assertEquals(16380, next.getProxyPort(),
|
||||
"The abandoned restore must leave grp2 holding the port the delete released");
|
||||
}
|
||||
|
||||
@Test
|
||||
void restorePersistedRuntimeSkipsGroupsBeingDeleted() {
|
||||
StorageFactory storageFactory = storageWithSingleNodeGroup("grp");
|
||||
ElastiCacheContainerManager beforeRestart = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(beforeRestart);
|
||||
ElastiCacheService before = serviceWith(storageFactory, beforeRestart,
|
||||
mock(ElastiCacheProxyManager.class), mock(ValkeyClusterFormation.class));
|
||||
// The in-memory backend hands back the stored instance, so this is the persisted record.
|
||||
before.getReplicationGroup("grp").setStatus(ReplicationGroupStatus.DELETING);
|
||||
|
||||
ElastiCacheContainerManager restartedContainers = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(restartedContainers);
|
||||
ElastiCacheService restarted = serviceWith(storageFactory, restartedContainers,
|
||||
mock(ElastiCacheProxyManager.class), mock(ValkeyClusterFormation.class));
|
||||
|
||||
restarted.restorePersistedRuntime().join();
|
||||
|
||||
verify(restartedContainers, never()).tryStart(anyString(), anyString());
|
||||
assertEquals(ReplicationGroupStatus.DELETING, restarted.getReplicationGroup("grp").getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restorePersistedRuntimeReprovisionsClusterModeGroups() {
|
||||
StorageFactory storageFactory = sharedStorageFactory();
|
||||
|
||||
Reference in new issue
Block a user