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
@@ -1917,18 +1917,18 @@ public interface EmulatorConfig {
|
||||
/**
|
||||
* Start and snapshot the backend of a Redis or Valkey replication group the way an image
|
||||
* with no shell and no CLI needs: server flags go to the container as arguments instead of
|
||||
* the {@code VALKEY_EXTRA_FLAGS} hook, a snapshot is taken with {@code SAVE} over the RESP
|
||||
* connection and read out of the container through the Docker archive API, and a snapshot
|
||||
* is restored by placing its file in the data directory before the server starts, so no
|
||||
* {@code DEBUG} command is involved. Off by default and never inferred from the image name:
|
||||
* the default image keeps the {@code VALKEY_EXTRA_FLAGS} and CLI path.
|
||||
* the {@code VALKEY_EXTRA_FLAGS} hook, and a snapshot is taken with {@code SAVE} over the
|
||||
* RESP connection and read out of the container through the Docker archive API. Off by
|
||||
* default and never inferred from the image name: the default image keeps the
|
||||
* {@code VALKEY_EXTRA_FLAGS} and CLI path. Either way a snapshot is restored by placing its
|
||||
* file in the data directory before the server starts, so no {@code DEBUG} command is involved.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean shellLessBackend();
|
||||
|
||||
/**
|
||||
* Name of the snapshot file the server loads from its data directory at startup. Only used
|
||||
* with {@link #shellLessBackend()}: a restore places the snapshot under this name, and a
|
||||
* Name of the snapshot file the server loads from its data directory at startup. A restore
|
||||
* places the snapshot under this name on either path, and with {@link #shellLessBackend()} a
|
||||
* snapshot export falls back to it when the server does not report {@code dbfilename}.
|
||||
* Set it to the name the image's server uses ({@code dump.rdb} for Redis and Valkey).
|
||||
*/
|
||||
|
||||
@@ -452,21 +452,21 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
// derived from configuration and need no Docker, so the group is created and reaches
|
||||
// 'available' even when no daemon is reachable. Only connecting to the cache needs
|
||||
// the container.
|
||||
// With the shell-less backend the snapshot is placed in the data directory before the
|
||||
// server starts; otherwise it is loaded into the running server below.
|
||||
String startupSeed = seed != null && containerManager.isShellLessBackend() ? seed.rdbBase64() : null;
|
||||
// A snapshot is placed in the data directory before the server starts, so the server
|
||||
// loads it itself and no server version needs DEBUG.
|
||||
try {
|
||||
handle = startupSeed == null
|
||||
handle = seed == null
|
||||
? containerManager.tryStart(groupId, image, request.region())
|
||||
: containerManager.tryStart(groupId, image, request.region(), startupSeed);
|
||||
: containerManager.tryStart(groupId, image, request.region(), seed.rdbBase64());
|
||||
} catch (ElastiCacheContainerManager.SnapshotRestoreException e) {
|
||||
LOG.warnv(e, "Failed to restore snapshot {0} into replication group {1}",
|
||||
seed.snapshotName(), groupId);
|
||||
throw new AwsException("InvalidSnapshotState", "Failed to restore snapshot " + seed.snapshotName()
|
||||
+ " into replication group " + groupId + ": " + e.getMessage(), 400);
|
||||
}
|
||||
if (seed != null && (startupSeed == null || handle == null)) {
|
||||
loadSnapshot(groupId, handle, seed);
|
||||
if (seed != null && handle == null) {
|
||||
throw new AwsException("InvalidReplicationGroupState", "Replication group " + groupId
|
||||
+ " has no running backend to restore snapshot " + seed.snapshotName() + " into.", 400);
|
||||
}
|
||||
|
||||
String endpointHost = resolveEndpointHost();
|
||||
@@ -1591,20 +1591,6 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
return createReplicationGroup(request, new SnapshotSeed(snapshotName, archive));
|
||||
}
|
||||
|
||||
private void loadSnapshot(String groupId, ElastiCacheContainerHandle handle, SnapshotSeed seed) {
|
||||
if (handle == null) {
|
||||
throw new AwsException("InvalidReplicationGroupState", "Replication group " + groupId
|
||||
+ " has no running backend to restore snapshot " + seed.snapshotName() + " into.", 400);
|
||||
}
|
||||
try {
|
||||
containerManager.restore(handle.getContainerId(), seed.rdbBase64());
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Failed to restore snapshot {0} into replication group {1}", seed.snapshotName(), groupId);
|
||||
throw new AwsException("InvalidSnapshotState", "Failed to restore snapshot " + seed.snapshotName()
|
||||
+ " into replication group " + groupId + ": " + e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Cache Clusters (single-node Redis/Valkey) ─────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
+7
-25
@@ -151,9 +151,10 @@ public class ElastiCacheContainerManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #start(String, String, List, String)} that also loads a snapshot at startup, for the
|
||||
* shell-less path only: the RDB file is placed in the container's data directory before the
|
||||
* server process starts, so it loads the dataset itself and no {@code DEBUG} command is needed.
|
||||
* {@link #start(String, String, List, String)} that also loads a snapshot at startup: the RDB
|
||||
* file is placed in the container's data directory before the server process starts, so it loads
|
||||
* the dataset itself. No {@code DEBUG} command is needed, nor {@code --enable-debug-command},
|
||||
* which a server before Redis 7 refuses to start with.
|
||||
* A container that cannot be seeded is removed and reported as a {@link SnapshotRestoreException}.
|
||||
*
|
||||
* @param seedRdbBase64 a {@link #snapshot} archive to start from, or {@code null} for an empty cache
|
||||
@@ -162,10 +163,6 @@ public class ElastiCacheContainerManager {
|
||||
String region, String seedRdbBase64) {
|
||||
LOG.infov("Starting ElastiCache backend container for group: {0}", groupId);
|
||||
boolean shellLess = isShellLessBackend();
|
||||
if (seedRdbBase64 != null && !shellLess) {
|
||||
throw new IllegalArgumentException(
|
||||
"A snapshot is loaded at startup only with floci.services.elasticache.shell-less-backend");
|
||||
}
|
||||
|
||||
String containerName = containerName(groupId);
|
||||
|
||||
@@ -191,9 +188,9 @@ public class ElastiCacheContainerManager {
|
||||
arguments.addAll(extraServerFlags);
|
||||
specBuilder.withCmd(arguments);
|
||||
} else {
|
||||
// DEBUG is enabled for connections from inside the container only, so a snapshot restore
|
||||
// can reload the dataset (DEBUG RELOAD NOSAVE); clients reach the server through the proxy.
|
||||
StringBuilder serverFlags = new StringBuilder("--loglevel verbose --enable-debug-command local");
|
||||
// No --enable-debug-command, which a server before Redis 7 refuses to start with: a
|
||||
// snapshot is restored by file, before start, here too.
|
||||
StringBuilder serverFlags = new StringBuilder("--loglevel verbose");
|
||||
for (String flag : extraServerFlags) {
|
||||
serverFlags.append(' ').append(flag);
|
||||
}
|
||||
@@ -443,21 +440,6 @@ public class ElastiCacheContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** Replaces the dataset with a {@link #snapshot} RDB file and reloads it without saving first. */
|
||||
public void restore(String containerId, String rdbBase64) {
|
||||
byte[] rdb = Base64.getDecoder().decode(rdbBase64);
|
||||
RetryingTarCopier.copyBytes(lifecycleManager.getDockerClient(), containerId, "/tmp",
|
||||
"floci-restore.rdb", rdb, 0644);
|
||||
String[] cmd = {"sh", "-c", CLI + "D=$($C CONFIG GET dir | sed -n 2p); F=$($C CONFIG GET dbfilename | sed -n 2p);"
|
||||
+ " cp /tmp/floci-restore.rdb \"$D/$F\" && rm -f /tmp/floci-restore.rdb"
|
||||
+ " && $C DEBUG RELOAD NOSAVE | grep -q OK"};
|
||||
ContainerExec.Result result = ContainerExec.run(lifecycleManager.getDockerClient(), containerId, cmd, 300);
|
||||
if (result.exitCode() != 0) {
|
||||
throw new IllegalStateException("restore failed with exit code " + result.exitCode() + ": "
|
||||
+ result.stderr() + result.stdout());
|
||||
}
|
||||
}
|
||||
|
||||
public void stop(ElastiCacheContainerHandle handle) {
|
||||
if (handle == null) {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user