mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elasticache): restore a snapshot by file before the server starts on every path (#7879)
Every Redis/Valkey backend on the default path started with --enable-debug-command local so a snapshot could be loaded into the running server with DEBUG RELOAD NOSAVE. A Redis before 7.0 refuses to start with that flag, so an image forwarding VALKEY_EXTRA_FLAGS to one no longer started, and the official redis:* images, which ignore VALKEY_EXTRA_FLAGS, kept DEBUG off on 7.0 or later and could not be seeded. The default path now seeds a group the way the shell-less path already did: the snapshot file is placed in the data directory before the server starts, and the server's dir and dbfilename are checked once it is up. No backend gets --enable-debug-command, and the DEBUG RELOAD restore is gone. Closes #7878
This commit is contained in:
1 parent
3d6a05d656
commit
4d9c5faf56
9 files changed
+104
-203
No files matched your search
-109
@@ -1,109 +0,0 @@
|
||||
package io.floci.aws.services.elasticache;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.elasticache.container.ElastiCacheContainerManager;
|
||||
import io.floci.aws.services.elasticache.model.AuthMode;
|
||||
import io.floci.aws.services.elasticache.proxy.ElastiCacheProxyManager;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A group created from a snapshot when the backend is the shell-less one: the snapshot goes to the
|
||||
* container manager with the start, so it is loaded by the server at startup and never through
|
||||
* {@code restore}, and a group that cannot be started with it is rolled back without touching the
|
||||
* group or the snapshot it came from.
|
||||
*/
|
||||
class ElastiCacheShellLessSnapshotServiceTest {
|
||||
|
||||
private static final String RDB = "UkVESVMwMDEx+gl2YWxrZXktdmVyBTguMC4x/w==";
|
||||
|
||||
private ElastiCacheService service;
|
||||
private ElastiCacheContainerManager containerManager;
|
||||
private ElastiCacheProxyManager proxyManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ElastiCacheServiceFixture fixture = new ElastiCacheServiceFixture();
|
||||
service = fixture.service;
|
||||
containerManager = fixture.containerManager;
|
||||
proxyManager = fixture.proxyManager;
|
||||
when(containerManager.isShellLessBackend()).thenReturn(true);
|
||||
doAnswer(inv -> ElastiCacheServiceFixture.containerHandle(inv.getArgument(0)))
|
||||
.when(containerManager).tryStart(anyString(), anyString(), any(), any());
|
||||
when(containerManager.snapshot("container-source")).thenReturn(RDB);
|
||||
service.createReplicationGroup("source", "group source", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
service.createSnapshot("snap", "source", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theSnapshotIsHandedToTheStartAndNeverRestoredIntoARunningServer() {
|
||||
service.createReplicationGroupFromSnapshot(request("restored"), "snap");
|
||||
|
||||
verify(containerManager).tryStart("restored", "valkey/valkey:8", "us-east-1", RDB);
|
||||
verify(containerManager, never()).restore(anyString(), anyString());
|
||||
verify(proxyManager).startProxy(eq("restored"), any(), anyInt(), anyString(), anyInt(), any());
|
||||
assertEquals("restored", service.getReplicationGroup("restored").getReplicationGroupId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGroupWithoutASnapshotStartsWithoutOne() {
|
||||
service.createReplicationGroup("plain", "group plain", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
|
||||
verify(containerManager).tryStart("plain", "valkey/valkey:8", "us-east-1");
|
||||
verify(containerManager, never()).tryStart(eq("plain"), anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStartTheSnapshotCannotBeLoadedInRollsTheNewGroupBackAndKeepsTheRest() {
|
||||
doAnswer(inv -> {
|
||||
throw new ElastiCacheContainerManager.SnapshotRestoreException("the server could not load it");
|
||||
}).when(containerManager).tryStart(eq("restored"), anyString(), anyString(), any());
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> service.createReplicationGroupFromSnapshot(request("restored"), "snap"));
|
||||
|
||||
assertEquals("InvalidSnapshotState", failure.getErrorCode());
|
||||
// The container of the failed start is removed by id, since no handle came back.
|
||||
verify(containerManager).stopByGroupId("restored");
|
||||
verify(proxyManager, never()).startProxy(eq("restored"), any(), anyInt(), anyString(), anyInt(), any());
|
||||
assertEquals("ReplicationGroupNotFoundFault", fault(() -> service.getReplicationGroup("restored")));
|
||||
// Neither the group the snapshot came from nor the snapshot is lost.
|
||||
assertEquals("source", service.getReplicationGroup("source").getReplicationGroupId());
|
||||
assertEquals("available", service.getSnapshot("snap").getSnapshotStatus());
|
||||
// The failed group's name and port are free again.
|
||||
service.createReplicationGroupFromSnapshot(request("restored-again"), "snap");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutADockerDaemonASnapshotCannotBeRestoredAndNothingIsLeftBehind() {
|
||||
doAnswer(inv -> null).when(containerManager).tryStart(eq("headless"), anyString(), anyString(), any());
|
||||
|
||||
assertEquals("InvalidReplicationGroupState",
|
||||
fault(() -> service.createReplicationGroupFromSnapshot(request("headless"), "snap")));
|
||||
assertEquals("ReplicationGroupNotFoundFault", fault(() -> service.getReplicationGroup("headless")));
|
||||
verify(containerManager).stopByGroupId("headless");
|
||||
}
|
||||
|
||||
private static ElastiCacheService.CreateReplicationGroupRequest request(String id) {
|
||||
return new ElastiCacheService.CreateReplicationGroupRequest(id, "group " + id, AuthMode.NO_AUTH, null,
|
||||
"us-east-1", null, null, null, null, null, null, null, null, null, null, null, null, null,
|
||||
Map.of(), null, null);
|
||||
}
|
||||
|
||||
private static String fault(Runnable call) {
|
||||
return assertThrows(AwsException.class, call::run).getErrorCode();
|
||||
}
|
||||
}
|
||||
+38
-8
@@ -56,6 +56,9 @@ class ElastiCacheSnapshotServiceTest {
|
||||
doAnswer(inv -> "headless".equals(inv.getArgument(0)) ? null
|
||||
: ElastiCacheServiceFixture.containerHandle(inv.getArgument(0)))
|
||||
.when(containerManager).tryStart(anyString(), anyString(), any());
|
||||
doAnswer(inv -> "headless".equals(inv.getArgument(0)) ? null
|
||||
: ElastiCacheServiceFixture.containerHandle(inv.getArgument(0)))
|
||||
.when(containerManager).tryStart(anyString(), anyString(), anyString(), any());
|
||||
when(containerManager.snapshot("container-source")).thenReturn(RDB);
|
||||
}
|
||||
|
||||
@@ -70,10 +73,18 @@ class ElastiCacheSnapshotServiceTest {
|
||||
assertEquals("manual", snapshot.getSnapshotSource());
|
||||
assertEquals("source", snapshot.getReplicationGroupId());
|
||||
assertEquals("arn:aws:elasticache:us-east-1:000000000000:snapshot:snap", snapshot.getArn());
|
||||
verify(containerManager).restore("container-restored", RDB);
|
||||
verify(containerManager).tryStart("restored", "valkey/valkey:8", "us-east-1", RDB);
|
||||
assertEquals("restored", service.getReplicationGroup("restored").getReplicationGroupId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGroupWithoutASnapshotStartsWithoutOne() {
|
||||
group("plain");
|
||||
|
||||
verify(containerManager).tryStart("plain", "valkey/valkey:8", "us-east-1");
|
||||
verify(containerManager, never()).tryStart(eq("plain"), anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theRdbIsLoadedBeforeTheGroupIsPublishedOrReachable() {
|
||||
group("source");
|
||||
@@ -81,14 +92,14 @@ class ElastiCacheSnapshotServiceTest {
|
||||
AtomicReference<String> duringLoad = new AtomicReference<>();
|
||||
doAnswer(inv -> {
|
||||
duringLoad.set(fault(() -> service.getReplicationGroup("restored")));
|
||||
return null;
|
||||
}).when(containerManager).restore("container-restored", RDB);
|
||||
return ElastiCacheServiceFixture.containerHandle("restored");
|
||||
}).when(containerManager).tryStart("restored", "valkey/valkey:8", "us-east-1", RDB);
|
||||
|
||||
service.createReplicationGroupFromSnapshot(request("restored", null), "snap");
|
||||
|
||||
assertEquals("ReplicationGroupNotFoundFault", duringLoad.get());
|
||||
InOrder order = inOrder(containerManager, proxyManager);
|
||||
order.verify(containerManager).restore("container-restored", RDB);
|
||||
order.verify(containerManager).tryStart("restored", "valkey/valkey:8", "us-east-1", RDB);
|
||||
order.verify(proxyManager).startProxy(eq("restored"), any(), anyInt(), anyString(), anyInt(), any());
|
||||
}
|
||||
|
||||
@@ -177,20 +188,38 @@ class ElastiCacheSnapshotServiceTest {
|
||||
fault(() -> service.createReplicationGroupFromSnapshot(request("restored", null), "snap")));
|
||||
assertEquals("ReplicationGroupNotFoundFault", fault(() -> service.getReplicationGroup("restored")));
|
||||
verify(containerManager, never()).tryStart(eq("restored"), anyString(), any());
|
||||
verify(containerManager, never()).tryStart(eq("restored"), anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGroupTheRdbCannotBeLoadedIntoIsRemoved() {
|
||||
void aGroupTheRdbCannotBeLoadedIntoIsRemovedAndTheRestIsKept() {
|
||||
group("source");
|
||||
service.createSnapshot("snap", "source", null);
|
||||
doThrow(new IllegalStateException("DEBUG RELOAD failed"))
|
||||
.when(containerManager).restore("container-restored", RDB);
|
||||
doThrow(new ElastiCacheContainerManager.SnapshotRestoreException("the server could not load it"))
|
||||
.when(containerManager).tryStart("restored", "valkey/valkey:8", "us-east-1", RDB);
|
||||
|
||||
assertEquals("InvalidSnapshotState",
|
||||
fault(() -> service.createReplicationGroupFromSnapshot(request("restored", null), "snap")));
|
||||
assertEquals("ReplicationGroupNotFoundFault", fault(() -> service.getReplicationGroup("restored")));
|
||||
verify(proxyManager, never()).startProxy(eq("restored"), any(), anyInt(), anyString(), anyInt(), any());
|
||||
verify(containerManager).stop(any());
|
||||
// The container of the failed start is removed by id, since no handle came back.
|
||||
verify(containerManager).stopByGroupId("restored");
|
||||
// Neither the group the snapshot came from nor the snapshot is lost.
|
||||
assertEquals("source", service.getReplicationGroup("source").getReplicationGroupId());
|
||||
assertEquals("available", service.getSnapshot("snap").getSnapshotStatus());
|
||||
// The failed group's name and port are free again.
|
||||
service.createReplicationGroupFromSnapshot(request("restored-again", null), "snap");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutADockerDaemonASnapshotCannotBeRestoredAndNothingIsLeftBehind() {
|
||||
group("source");
|
||||
service.createSnapshot("snap", "source", null);
|
||||
|
||||
assertEquals("InvalidReplicationGroupState",
|
||||
fault(() -> service.createReplicationGroupFromSnapshot(request("headless", null), "snap")));
|
||||
assertEquals("ReplicationGroupNotFoundFault", fault(() -> service.getReplicationGroup("headless")));
|
||||
verify(containerManager).stopByGroupId("headless");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -201,6 +230,7 @@ class ElastiCacheSnapshotServiceTest {
|
||||
assertEquals("InvalidParameterCombination",
|
||||
fault(() -> service.createReplicationGroupFromSnapshot(request("sharded", "enabled"), "snap")));
|
||||
verify(containerManager, never()).tryStart(eq("sharded"), anyString(), any());
|
||||
verify(containerManager, never()).tryStart(eq("sharded"), anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+22
-13
@@ -134,21 +134,10 @@ class ElastiCacheShellLessContainerManagerTest {
|
||||
void theDefaultPathStillUsesTheValkeyExtraFlagsHook() throws IOException {
|
||||
startAgainstFakeBackend(manager(false), CLUSTER_FLAGS);
|
||||
|
||||
verify(builder).withEnv("VALKEY_EXTRA_FLAGS", "--loglevel verbose --enable-debug-command local "
|
||||
+ String.join(" ", CLUSTER_FLAGS));
|
||||
verify(builder).withEnv("VALKEY_EXTRA_FLAGS", "--loglevel verbose " + String.join(" ", CLUSTER_FLAGS));
|
||||
verify(builder, never()).withCmd(any(List.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSnapshotIsLoadedAtStartupOnlyWithTheShellLessBackend() {
|
||||
ElastiCacheContainerManager manager = manager(false);
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> manager.start("my-group", "valkey/valkey:8", List.of(), "us-east-1", "UkVESVM="));
|
||||
|
||||
verify(lifecycleManager, never()).createAndStart(any(ContainerSpec.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSnapshotIsReadOnlyFromABackendThatIsRunning() {
|
||||
ElastiCacheContainerManager manager = manager(true);
|
||||
@@ -209,6 +198,11 @@ class ElastiCacheShellLessContainerManagerTest {
|
||||
}
|
||||
|
||||
private ElastiCacheContainerManager seededManager(ServerSocket backend, String configuredFileName) {
|
||||
return seededManager(backend, configuredFileName, true);
|
||||
}
|
||||
|
||||
private ElastiCacheContainerManager seededManager(ServerSocket backend, String configuredFileName,
|
||||
boolean shellLess) {
|
||||
DockerClient docker = mock(DockerClient.class, RETURNS_DEEP_STUBS);
|
||||
when(docker.inspectContainerCmd(anyString()).exec().getConfig().getWorkingDir()).thenReturn("/data");
|
||||
when(lifecycleManager.getDockerClient()).thenReturn(docker);
|
||||
@@ -218,11 +212,26 @@ class ElastiCacheShellLessContainerManagerTest {
|
||||
return new ContainerLifecycleManager.ContainerInfo("container-id", Map.of(6379,
|
||||
new ContainerLifecycleManager.EndpointInfo("127.0.0.1", backend.getLocalPort())));
|
||||
});
|
||||
ElastiCacheContainerManager manager = manager(true);
|
||||
ElastiCacheContainerManager manager = manager(shellLess);
|
||||
when(elasticache.shellLessBackendDbfilename()).thenReturn(configuredFileName);
|
||||
return manager;
|
||||
}
|
||||
|
||||
// Catches: the default path loading a snapshot with DEBUG RELOAD, which needs
|
||||
// --enable-debug-command, a flag a Redis before 7 refuses to start with.
|
||||
@Test
|
||||
void theDefaultPathPlacesTheSnapshotBeforeTheServerStartsWithoutTheDebugCommand() throws IOException {
|
||||
try (ServerSocket backend = fakeBackend("/data", "dump.rdb")) {
|
||||
ElastiCacheContainerManager manager = seededManager(backend, "dump.rdb", false);
|
||||
|
||||
manager.start("my-group", "valkey/valkey:8", List.of(), "us-east-1", "UkVESVM=");
|
||||
|
||||
verify(lifecycleManager.getDockerClient()).copyArchiveToContainerCmd("container-id");
|
||||
verify(builder).withEnv("VALKEY_EXTRA_FLAGS", "--loglevel verbose");
|
||||
verify(builder, never()).withCmd(any(List.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRestoreIsConfirmedWhenTheServerLoadsTheFileThatWasPlaced() throws IOException {
|
||||
try (ServerSocket backend = fakeBackend("/data", "dump.rdb")) {
|
||||
|
||||
Reference in new issue
Block a user