Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(redshiftserverless): add snapshot and restore operations (#5184)

yarammanasa2001-max9934b39a4218
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java
index 168aa7c2a4..f5f017bf4a 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java
@@ -13,6 +13,7 @@ import software.amazon.awssdk.services.redshiftserverless.model.ConflictExceptio
import software.amazon.awssdk.services.redshiftserverless.model.Namespace;
import software.amazon.awssdk.services.redshiftserverless.model.NamespaceStatus;
import software.amazon.awssdk.services.redshiftserverless.model.ResourceNotFoundException;
+import software.amazon.awssdk.services.redshiftserverless.model.Snapshot;
import software.amazon.awssdk.services.redshiftserverless.model.Tag;
import software.amazon.awssdk.services.redshiftserverless.model.Workgroup;
import software.amazon.awssdk.services.redshiftserverless.model.WorkgroupStatus;
@@ -161,6 +162,54 @@ class RedshiftServerlessTest {
}
}
+ @Test
+ void snapshotLifecycleUsesAwsSdk() {
+ assumeFalse(TestFixtures.isRealAws(), "Creates snapshots instantly and asserts emulator-local behavior");
+
+ try (RedshiftServerlessClient client = TestFixtures.redshiftServerlessClient()) {
+ String namespaceName = "floci-compat-snap-ns";
+ String workgroupName = "floci-compat-snap-wg";
+ String snapshotName = "floci-compat-snap";
+ try {
+ client.createNamespace(request -> request.namespaceName(namespaceName).adminUsername("admin")
+ .adminUserPassword("Secret123!"));
+ client.createWorkgroup(request -> request.workgroupName(workgroupName).namespaceName(namespaceName));
+
+ Snapshot created = client.createSnapshot(request -> request
+ .snapshotName(snapshotName).namespaceName(namespaceName)).snapshot();
+ assertThat(created.snapshotName()).isEqualTo(snapshotName);
+ assertThat(created.status().toString()).isEqualTo("AVAILABLE");
+ assertThat(created.snapshotCreateTime()).isNotNull();
+
+ assertThat(client.getSnapshot(request -> request.snapshotArn(created.snapshotArn())).snapshot()
+ .snapshotName()).isEqualTo(snapshotName);
+ assertThat(client.listSnapshots(request -> request.namespaceName(namespaceName)).snapshots())
+ .extracting(Snapshot::snapshotName)
+ .containsExactly(snapshotName);
+
+ assertThat(client.restoreFromSnapshot(request -> request
+ .namespaceName(namespaceName).workgroupName(workgroupName).snapshotName(snapshotName))
+ .snapshotName()).isEqualTo(snapshotName);
+
+ client.deleteSnapshot(request -> request.snapshotName(snapshotName));
+ assertThatThrownBy(() -> client.getSnapshot(request -> request.snapshotName(snapshotName)))
+ .isInstanceOf(ResourceNotFoundException.class);
+ } finally {
+ deleteSnapshotBestEffort(client, snapshotName);
+ deleteWorkgroupBestEffort(client, workgroupName);
+ deleteBestEffort(client, namespaceName);
+ }
+ }
+ }
+
+ private static void deleteSnapshotBestEffort(RedshiftServerlessClient client, String snapshotName) {
+ try {
+ client.deleteSnapshot(request -> request.snapshotName(snapshotName));
+ } catch (Exception cleanupError) {
+ LOG.warnf(cleanupError, "Best-effort cleanup failed for snapshotName=%s", snapshotName);
+ }
+ }
+
private static void deleteWorkgroupBestEffort(RedshiftServerlessClient client, String workgroupName) {
try {
client.deleteWorkgroup(request -> request.workgroupName(workgroupName));
diff --git a/docs/services/index.md b/docs/services/index.md
index 52d5a11d14..715090351e 100644
--- a/docs/services/index.md
+++ b/docs/services/index.md
@@ -76,7 +76,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [DMS](dms.md) | `POST /` + `X-Amz-Target: AmazonDMSv20160101.*` | JSON 1.1 | 20 |
| [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 35 |
| [Redshift Data API](redshift-data.md) | `POST /` + `X-Amz-Target: RedshiftData.*` | JSON 1.1 | 11 |
-| [Redshift Serverless](redshift-serverless.md) | `POST /` + `X-Amz-Target: RedshiftServerless.*` | JSON 1.1 | 14 |
+| [Redshift Serverless](redshift-serverless.md) | `POST /` + `X-Amz-Target: RedshiftServerless.*` | JSON 1.1 | 19 |
| [EMR](emr.md) | `POST /` + `X-Amz-Target: ElasticMapReduce.*` | JSON 1.1 | 37 |
| [EMR Serverless](emr-serverless.md) | `/applications/*`, `/tags/*` | REST JSON | 10 |
| [Data Firehose](firehose.md) | `POST /` + `X-Amz-Target: Firehose_20150804.*` | JSON 1.1 | 12 |
diff --git a/docs/services/redshift-serverless.md b/docs/services/redshift-serverless.md
index f4397a94c3..4422b36c0c 100644
--- a/docs/services/redshift-serverless.md
+++ b/docs/services/redshift-serverless.md
@@ -22,6 +22,11 @@ For the upstream API shape, see the [Amazon Redshift Serverless API Reference](h
| `ListWorkgroups` | Page through the workgroups in the account and Region |
| `UpdateWorkgroup` | Apply the supplied fields to an existing workgroup and return it |
| `DeleteWorkgroup` | Stop a workgroup's container and proxy, release its endpoint, and return it with `status` `DELETING` |
+| `CreateSnapshot` | Create an `AVAILABLE` snapshot of a namespace, dumping the workgroup's database when it is running; a snapshot created in a standby Region stands in for a cross-Region copy |
+| `GetSnapshot` | Return a snapshot by `snapshotName` or `snapshotArn` |
+| `ListSnapshots` | Page through the snapshots in the Region, filtered by `namespaceName`, `namespaceArn`, `ownerAccount` and a `startTime`/`endTime` window |
+| `DeleteSnapshot` | Remove a snapshot and return it |
+| `RestoreFromSnapshot` | Restore into an existing namespace and workgroup, replaying the snapshot's dump into the workgroup's database; neither is created |
| `GetCredentials` | Mint a temporary database user and password for a workgroup, valid on its endpoint until `expiration` |
| `ListTagsForResource` | Return the tags on the namespace or workgroup named by `resourceArn` |
| `TagResource` | Merge tags into the namespace or workgroup named by `resourceArn` |
@@ -32,7 +37,7 @@ Namespace and workgroup state is account and Region scoped and persisted through
## Compatibility Notes
-- **A workgroup runs a real PostgreSQL container.** `CreateWorkgroup` starts the container (this needs Docker and uses `floci.services.redshift.image-version`) and an authenticating proxy on the `endpoint`, whose port comes from the same pool as provisioned Redshift clusters (`floci.services.redshift.proxy-base-port` to `proxy-max-port`). Connect with any PostgreSQL client or JDBC driver using the namespace admin user and password, to the namespace `dbName`. The Redshift Data API accepts `WorkgroupName` ([details](redshift-data.md)). Only the 14 operations in the Supported Actions table are served. The rest of the Redshift Serverless API is not emulated and answers `UnknownOperationException`: snapshots and snapshot copy configurations, recovery points, table restore and restore status, usage limits, endpoint access, resource policies, custom domain associations, scheduled actions, reservations and reservation offerings, tracks, managed workgroups, the Identity Center token, and lakehouse configuration.
+- **A workgroup runs a real PostgreSQL container.** `CreateWorkgroup` starts the container (this needs Docker and uses `floci.services.redshift.image-version`) and an authenticating proxy on the `endpoint`, whose port comes from the same pool as provisioned Redshift clusters (`floci.services.redshift.proxy-base-port` to `proxy-max-port`). Connect with any PostgreSQL client or JDBC driver using the namespace admin user and password, to the namespace `dbName`. The Redshift Data API accepts `WorkgroupName` ([details](redshift-data.md)). Only the operations in the Supported Actions table are served. The rest of the Redshift Serverless API is not emulated and answers `UnknownOperationException`: snapshot copy configurations, snapshot restore access policies, recovery points, table restore and restore status, usage limits, endpoint access, resource policies, custom domain associations, scheduled actions, reservations and reservation offerings, tracks, managed workgroups, the Identity Center token, and lakehouse configuration.
- **Admin credentials come from the namespace.** The backend role is the namespace `adminUsername` (default `admin`) and the password is the `adminUserPassword` the namespace was created with. A namespace created without a password gets a generated one that is kept internally and never returned, so such a workgroup is reachable through `GetCredentials` and the Data API only. `manageAdminPassword` still creates no secret. Changing `adminUserPassword` with `UpdateNamespace` takes effect for new connections and is refused with `ValidationException` if the backend rejects it (AWS password rules: 8 to 64 characters with upper case, lower case and a digit, and none of `' " / @`). Changing `adminUsername` on a running workgroup does not rename the backend role; the new name is accepted at the proxy, which opens the backend as the original role.
- **`GetCredentials` returns a master-equivalent password.** `dbUser` is derived from the signing identity (`IAM:<user>` or `IAMR:<role>`, falling back to `IAMR:floci` when the caller cannot be resolved), `dbPassword` is random, and `expiration` and `nextRefreshTime` are epoch seconds (the same instant). `durationSeconds` defaults to 900 and must be 900 to 3600. `dbName`, when given, must be the namespace `dbName`. `customDomainName` is not supported. A returned credential stays valid until it expires or the workgroup is deleted.
- **The runtime survives a restart when its container does.** On startup Floci re-attaches to the container and restarts the proxy on the same port, so the endpoint is stable. If the container is gone the workgroup comes back with an empty database; if neither can be started, the workgroup stays `AVAILABLE`, the Data API reports that its runtime is not available, and the failure is logged. Containers have no Docker volume, as for provisioned clusters.
@@ -97,3 +102,4 @@ aws redshift-serverless update-workgroup --workgroup-name analytics-wg --base-ca
aws redshift-serverless delete-workgroup --workgroup-name analytics-wg
aws redshift-serverless delete-namespace --namespace-name analytics
```
+- **Snapshots dump the database.** While the namespace's workgroup is running, `CreateSnapshot` runs `pg_dump` on its container and stores the dump under `<persistent-path>/redshift-serverless-dumps/<account>/<region>/`. `RestoreFromSnapshot` drops every user schema (the catalog bootstrap schema excepted) and replays the dump with `psql`, stopping at the first failing statement, so rows written after the snapshot are gone. The current contents are dumped first and put back if the replay fails, and a restore whose recorded dump file has gone missing fails instead of reporting success. A snapshot taken with no running workgroup is metadata only, and restoring it leaves the database untouched. `DeleteSnapshot` removes the dump. Snapshot ARNs use the snapshot name as their id, snapshot names are 3-255 lowercase letters, digits and hyphens, and `retentionPeriod` is `-1` (indefinite, the default) or 1 to 3653 days. Snapshots are taggable by ARN. `snapshotName` and `snapshotArn` cannot be sent together. Floci's own `floci_internal` bootstrap schema, which feeds the `svv_external_*` views from the Glue catalog, is left out of both the dump and the reset, matching AWS where the Glue catalog is outside a namespace snapshot, so a client schema with that exact name is not captured. A real snapshot expires after its retention period; Floci reports `snapshotRemainingDays` but does not expire snapshots.
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
index f79294160f..6de5c284e1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
@@ -181,16 +181,44 @@ public class RedshiftContainerManager {
return ContainerStorageHelper.dockerName(config, "redshift-" + accountId + "-" + clusterIdentifier);
}
- public void takeSnapshot(String accountId, String clusterIdentifier, String username, String dbname, Path outputFile) {
+ private RedshiftContainerHandle requireHandle(String accountId, String clusterIdentifier) {
RedshiftContainerHandle handle = containers.get(containerKey(accountId, clusterIdentifier));
if (handle == null) {
throw new AwsException("ClusterNotFound", "Cluster container for " + clusterIdentifier + " not found", 404);
}
+ return handle;
+ }
+
+ private static String userOrDefault(String username) {
+ return (username != null && !username.isBlank()) ? username : "postgres";
+ }
+
+ private static String databaseOrDefault(String dbname) {
+ return (dbname != null && !dbname.isBlank()) ? dbname : DEFAULT_DATABASE;
+ }
+
+ /** Schema {@code bootstrap-catalog.sql} owns; the catalog views read it, so snapshots leave it alone. */
+ public static final String BOOTSTRAP_SCHEMA = "floci_internal";
+
+ public void takeSnapshot(String accountId, String clusterIdentifier, String username, String dbname, Path outputFile) {
+ takeSnapshot(accountId, clusterIdentifier, username, dbname, outputFile, false);
+ }
- String effectiveUser = (username != null && !username.isBlank()) ? username : "postgres";
- String effectiveDb = (dbname != null && !dbname.isBlank()) ? dbname : "dev";
+ /**
+ * Dumps the database with pg_dump. With {@code skipBootstrapSchema} the {@link #BOOTSTRAP_SCHEMA}
+ * is left out, which a dump meant to be replayed into a live, already bootstrapped container needs.
+ */
+ public void takeSnapshot(String accountId, String clusterIdentifier, String username, String dbname,
+ Path outputFile, boolean skipBootstrapSchema) {
+ RedshiftContainerHandle handle = requireHandle(accountId, clusterIdentifier);
- String[] cmd = new String[]{"pg_dump", "-U", effectiveUser, effectiveDb, "-f", "/tmp/dump.sql"};
+ String effectiveUser = userOrDefault(username);
+ String effectiveDb = databaseOrDefault(dbname);
+
+ String[] cmd = skipBootstrapSchema
+ ? new String[]{"pg_dump", "-U", effectiveUser, "--exclude-schema=" + BOOTSTRAP_SCHEMA, effectiveDb,
+ "-f", "/tmp/dump.sql"}
+ : new String[]{"pg_dump", "-U", effectiveUser, effectiveDb, "-f", "/tmp/dump.sql"};
try {
ContainerExec.Result result = execInContainer(handle.getContainerId(), cmd, 30);
if (result.exitCode() != 0) {
@@ -222,11 +250,8 @@ public class RedshiftContainerManager {
/** As the four-argument form, connecting to {@code databaseName} instead of {@code dev}. */
public void alterUserPassword(String accountId, String clusterIdentifier, String username, String newPassword,
String databaseName) {
- RedshiftContainerHandle handle = containers.get(containerKey(accountId, clusterIdentifier));
- if (handle == null) {
- throw new AwsException("ClusterNotFound", "Cluster container for " + clusterIdentifier + " not found", 404);
- }
- String effectiveUser = (username != null && !username.isBlank()) ? username : "postgres";
+ RedshiftContainerHandle handle = requireHandle(accountId, clusterIdentifier);
+ String effectiveUser = userOrDefault(username);
// Validate effectiveUser against safe SQL identifier pattern to prevent SQL injection.
// psql -c sends the query string to Postgres, which allows multiple ;-separated statements,
// so even argv-escaping doesn't protect against a username like "postgres; DROP TABLE ..."
@@ -281,18 +306,24 @@ public class RedshiftContainerManager {
}
public void restoreSnapshot(String accountId, String clusterIdentifier, String username, String dbname, Path sqlDumpFile) {
- RedshiftContainerHandle handle = containers.get(containerKey(accountId, clusterIdentifier));
- if (handle == null) {
- throw new AwsException("ClusterNotFound", "Cluster container for " + clusterIdentifier + " not found", 404);
- }
+ restoreSnapshot(accountId, clusterIdentifier, username, dbname, sqlDumpFile, false);
+ }
+
+ /**
+ * Replays the dump with psql. With {@code stopOnError} the first failing statement aborts the
+ * replay and fails the call, instead of psql skipping it and exiting successfully.
+ */
+ public void restoreSnapshot(String accountId, String clusterIdentifier, String username, String dbname,
+ Path sqlDumpFile, boolean stopOnError) {
+ RedshiftContainerHandle handle = requireHandle(accountId, clusterIdentifier);
if (sqlDumpFile == null || !Files.exists(sqlDumpFile)) {
LOG.infov("Empty snapshot dump for cluster {0}, skipping restore", clusterIdentifier);
return;
}
- String effectiveUser = (username != null && !username.isBlank()) ? username : "postgres";
- String effectiveDb = (dbname != null && !dbname.isBlank()) ? dbname : "dev";
+ String effectiveUser = userOrDefault(username);
+ String effectiveDb = databaseOrDefault(dbname);
String fileName = sqlDumpFile.getFileName().toString();
try {
@@ -301,7 +332,10 @@ public class RedshiftContainerManager {
.withRemotePath("/tmp")
.exec();
- String[] cmd = new String[]{"psql", "-U", effectiveUser, "-d", effectiveDb, "-f", "/tmp/" + fileName};
+ String[] cmd = stopOnError
+ ? new String[]{"psql", "-U", effectiveUser, "-d", effectiveDb, "-v", "ON_ERROR_STOP=1",
+ "-f", "/tmp/" + fileName}
+ : new String[]{"psql", "-U", effectiveUser, "-d", effectiveDb, "-f", "/tmp/" + fileName};
ContainerExec.Result result = execInContainer(handle.getContainerId(), cmd, 60);
if (result.exitCode() != 0) {
LOG.warnv("psql restore failed for cluster {0} (exit {1}): {2}", clusterIdentifier, result.exitCode(), result.stderr());
@@ -315,6 +349,40 @@ public class RedshiftContainerManager {
}
}
+ private static final String DROP_USER_SCHEMAS_SQL = "DO $$ DECLARE s text; BEGIN "
+ + "PERFORM lo_unlink(oid) FROM pg_largeobject_metadata; "
+ + "FOR s IN SELECT nspname FROM pg_namespace WHERE nspname NOT IN ('pg_catalog', 'information_schema') "
+ + "AND nspname <> '" + BOOTSTRAP_SCHEMA + "' AND nspname NOT LIKE 'pg\\_%' LOOP EXECUTE format('DROP SCHEMA %I CASCADE', s); END LOOP; "
+ + "CREATE SCHEMA public; END $$;";
+
+ /**
+ * Drops every large object and every user schema except the bootstrap one, and recreates an
+ * empty {@code public} one, so a following {@link #restoreSnapshot} replays into a clean
+ * database rather than appending to what is already there.
+ */
+ public void resetUserSchemas(String accountId, String clusterIdentifier, String username, String dbname) {
+ RedshiftContainerHandle handle = requireHandle(accountId, clusterIdentifier);
+ String effectiveUser = userOrDefault(username);
+ String effectiveDb = databaseOrDefault(dbname);
+ String[] cmd = new String[]{"psql", "-U", effectiveUser, "-d", effectiveDb, "-c",
+ DROP_USER_SCHEMAS_SQL};
+ try {
+ ContainerExec.Result result = execInContainer(handle.getContainerId(), cmd, 30);
+ if (result.exitCode() != 0) {
+ LOG.warnv("Schema reset failed for cluster {0} (exit {1}): {2}", clusterIdentifier,
+ result.exitCode(), result.stderr());
+ throw new AwsException("InternalFailure", "Failed to reset the database of " + clusterIdentifier
+ + ": " + result.stderr(), 500);
+ }
+ } catch (AwsException e) {
+ throw e;
+ } catch (Exception e) {
+ LOG.errorv(e, "Error resetting the database of cluster {0}", clusterIdentifier);
+ throw new AwsException("InternalFailure", "Failed to reset the database of " + clusterIdentifier
+ + ": " + e.getMessage(), 500);
+ }
+ }
+
public void restoreSnapshot(String accountId, String clusterIdentifier, String username, Path sqlDumpFile) {
restoreSnapshot(accountId, clusterIdentifier, username, "dev", sqlDumpFile);
}
@@ -344,7 +412,7 @@ public class RedshiftContainerManager {
return bos.toByteArray();
}
private void waitForReady(String containerName, String containerId, String username, String dbName) {
- String effectiveUser = (username != null && !username.isBlank()) ? username : "postgres";
+ String effectiveUser = userOrDefault(username);
String[] cmd = {
"psql",
"-h", "127.0.0.1",
@@ -384,7 +452,7 @@ public class RedshiftContainerManager {
* and migration tooling can inspect metadata without relation-does-not-exist errors.
*/
void bootstrapCatalog(String containerId, String username, String dbName) {
- String effectiveUser = (username != null && !username.isBlank()) ? username : "postgres";
+ String effectiveUser = userOrDefault(username);
String effectiveDb = (dbName != null && !dbName.isBlank()) ? dbName : "dev";
try (InputStream in = getClass().getResourceAsStream("/redshift/bootstrap-catalog.sql")) {
if (in == null) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java
index 50e655832c..6d7e61f4ec 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java
@@ -13,12 +13,15 @@ import io.github.hectorvent.floci.services.redshift.TempCredential;
import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.RedshiftServerlessSnapshot;
import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
+import java.time.Duration;
+import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@@ -76,6 +79,11 @@ public class RedshiftServerlessJsonHandler {
case "ListWorkgroups" -> handleListWorkgroups(request, region);
case "UpdateWorkgroup" -> handleUpdateWorkgroup(request, region);
case "DeleteWorkgroup" -> handleDeleteWorkgroup(request, region);
+ case "CreateSnapshot" -> handleCreateSnapshot(request, region);
+ case "GetSnapshot" -> handleGetSnapshot(request, region);
+ case "ListSnapshots" -> handleListSnapshots(request, region);
+ case "DeleteSnapshot" -> handleDeleteSnapshot(request, region);
+ case "RestoreFromSnapshot" -> handleRestoreFromSnapshot(request, region);
case "GetCredentials" -> handleGetCredentials(request, region, authorizationHeader);
case "ListTagsForResource" -> handleListTagsForResource(request, region);
case "TagResource" -> handleTagResource(request, region);
@@ -199,6 +207,59 @@ public class RedshiftServerlessJsonHandler {
return Response.ok(response).build();
}
+ private Response handleCreateSnapshot(JsonNode request, String region) {
+ return snapshotResponse(service.createSnapshot(
+ text(request, "snapshotName"), text(request, "namespaceName"),
+ parseInteger(request, "retentionPeriod"), parseTagList(request.path("tags"), "tags"), region));
+ }
+
+ /**
+ * GetSnapshot also accepts {@code snapshotArn}, which must name a snapshot of this account
+ * and Region.
+ */
+ private Response handleGetSnapshot(JsonNode request, String region) {
+ return snapshotResponse(service.getSnapshot(snapshotNameOf(request, region), region));
+ }
+
+ private Response handleListSnapshots(JsonNode request, String region) {
+ PaginatedResult<RedshiftServerlessSnapshot> page = service.listSnapshots(
+ text(request, "namespaceName"), text(request, "namespaceArn"), text(request, "ownerAccount"),
+ parseEpochSeconds(request, "startTime"), parseEpochSeconds(request, "endTime"), region,
+ parseMaxResults(request), text(request, "nextToken"));
+ ObjectNode response = objectMapper.createObjectNode();
+ ArrayNode items = response.putArray("snapshots");
+ page.items().forEach(snapshot -> items.add(snapshotNode(snapshot)));
+ if (page.nextToken() != null) {
+ response.put("nextToken", page.nextToken());
+ }
+ return Response.ok(response).build();
+ }
+
+ private Response handleDeleteSnapshot(JsonNode request, String region) {
+ return snapshotResponse(service.deleteSnapshot(text(request, "snapshotName"), region));
+ }
+
+ private Response handleRestoreFromSnapshot(JsonNode request, String region) {
+ RedshiftServerlessService.RestoreResult result = service.restoreFromSnapshot(
+ text(request, "namespaceName"), text(request, "workgroupName"),
+ snapshotNameOf(request, region), text(request, "ownerAccount"), region);
+ ObjectNode response = objectMapper.createObjectNode();
+ response.set("namespace", namespaceNode(result.namespace()));
+ response.put("ownerAccount", result.snapshot().getOwnerAccount());
+ response.put("snapshotName", result.snapshot().getSnapshotName());
+ return Response.ok(response).build();
+ }
+
+ /** The snapshot name from {@code snapshotName}, or recovered from {@code snapshotArn} when absent. */
+ private String snapshotNameOf(JsonNode request, String region) {
+ String snapshotName = text(request, "snapshotName");
+ String snapshotArn = text(request, "snapshotArn");
+ if (snapshotName != null && snapshotArn != null) {
+ throw validation("snapshotName and snapshotArn cannot both be specified.");
+ }
+ return snapshotArn == null ? snapshotName : service.snapshotNameFromArn(snapshotArn, region);
+ }
+
private Response handleListTagsForResource(JsonNode request, String region) {
Map<String, String> tags = service.listTagsForResource(text(request, "resourceArn"), region);
ObjectNode response = objectMapper.createObjectNode();
@@ -260,6 +321,35 @@ public class RedshiftServerlessJsonHandler {
return node;
}
+ private Response snapshotResponse(RedshiftServerlessSnapshot snapshot) {
+ ObjectNode response = objectMapper.createObjectNode();
+ response.set("snapshot", snapshotNode(snapshot));
+ return Response.ok(response).build();
+ }
+
+ private ObjectNode snapshotNode(RedshiftServerlessSnapshot snapshot) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("snapshotName", snapshot.getSnapshotName());
+ node.put("snapshotArn", snapshot.getSnapshotArn());
+ node.put("namespaceName", snapshot.getNamespaceName());
+ node.put("namespaceArn", snapshot.getNamespaceArn());
+ node.put("ownerAccount", snapshot.getOwnerAccount());
+ node.put("status", snapshot.getStatus());
+ node.put("adminUsername", snapshot.getAdminUsername());
+ if (snapshot.getKmsKeyId() != null) {
+ node.put("kmsKeyId", snapshot.getKmsKeyId());
+ }
+ if (snapshot.getRetentionPeriod() != null) {
+ node.put("snapshotRetentionPeriod", snapshot.getRetentionPeriod());
+ long elapsedDays = Duration.between(snapshot.getSnapshotCreateTime(), Instant.now()).toDays();
+ node.put("snapshotRemainingDays", Math.max(0, snapshot.getRetentionPeriod() - elapsedDays));
+ }
+ if (snapshot.getSnapshotCreateTime() != null) {
+ node.put("snapshotCreateTime", CREATION_DATE_FORMAT.format(snapshot.getSnapshotCreateTime()));
+ }
+ return node;
+ }
+
private Response workgroupResponse(Workgroup workgroup) {
ObjectNode response = objectMapper.createObjectNode();
response.set("workgroup", workgroupNode(workgroup));
@@ -387,6 +477,18 @@ public class RedshiftServerlessJsonHandler {
return new PricePerformanceTarget(text(node, "status"), parseInteger(node, "level"));
}
+ /** A timestamp request member, which awsJson1.1 sends as epoch seconds unless the model says otherwise. */
+ private Instant parseEpochSeconds(JsonNode request, String field) {
+ JsonNode node = request.path(field);
+ if (node.isMissingNode() || node.isNull()) {
+ return null;
+ }
+ if (!node.isNumber()) {
+ throw validation(field + " must be a timestamp.");
+ }
+ return Instant.ofEpochMilli(Math.round(node.asDouble() * 1000));
+ }
+
private Integer parseMaxResults(JsonNode request) {
JsonNode node = request.path("maxResults");
if (node.isMissingNode() || node.isNull()) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java
index 7465f6c996..0384fa236d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.redshiftserverless;
+import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
import io.github.hectorvent.floci.services.redshift.RedshiftCredentialBroker;
import io.github.hectorvent.floci.services.redshift.TempCredential;
@@ -11,6 +12,9 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.security.SecureRandom;
import java.util.List;
import java.util.Optional;
@@ -81,6 +85,58 @@ public class RedshiftServerlessRuntime {
}
}
+ /** Dumps the workgroup's database to {@code outputFile} with pg_dump. */
+ public void takeSnapshot(String accountId, String region, String workgroupName, String masterUsername,
+ String dbName, Path outputFile) {
+ containerManager.takeSnapshot(accountId, backendId(region, workgroupName), masterUsername, dbName, outputFile,
+ true);
+ }
+
+ /**
+ * Replaces the workgroup's database contents with the dump in {@code dumpFile}. The current
+ * contents are dumped first, and when the replay fails they are put back, so a bad dump never
+ * leaves the workgroup empty or half restored.
+ */
+ public void restoreSnapshot(String accountId, String region, String workgroupName, String masterUsername,
+ String dbName, Path dumpFile) {
+ String backendId = backendId(region, workgroupName);
+ Path safety;
+ try {
+ safety = Files.createTempFile("redshift-serverless-restore-" + workgroupName + "-", ".sql");
+ } catch (IOException e) {
+ throw new AwsException("InternalServerException",
+ "Failed to prepare the restore of workgroup " + workgroupName + ": " + e.getMessage(), 500);
+ }
+ try {
+ containerManager.takeSnapshot(accountId, backendId, masterUsername, dbName, safety, true);
+ try {
+ containerManager.resetUserSchemas(accountId, backendId, masterUsername, dbName);
+ containerManager.restoreSnapshot(accountId, backendId, masterUsername, dbName, dumpFile, true);
+ } catch (RuntimeException e) {
+ rollBackRestore(accountId, backendId, masterUsername, dbName, safety, e);
+ throw e;
+ }
+ } finally {
+ try {
+ Files.deleteIfExists(safety);
+ } catch (IOException e) {
+ LOG.warnv(e, "Could not delete the pre-restore dump {0}", safety);
+ }
+ }
+ }
+
+ private void rollBackRestore(String accountId, String backendId, String masterUsername, String dbName,
+ Path safety, RuntimeException failure) {
+ try {
+ containerManager.resetUserSchemas(accountId, backendId, masterUsername, dbName);
+ containerManager.restoreSnapshot(accountId, backendId, masterUsername, dbName, safety, true);
+ } catch (RuntimeException rollbackFailure) {
+ LOG.errorv(rollbackFailure, "Could not put back the previous contents of {0} after a failed restore",
+ backendId);
+ failure.addSuppressed(rollbackFailure);
+ }
+ }
+
/**
* Stops the proxy first and lets a failure propagate: if the listener cannot be closed the
* port must stay reserved and the workgroup must stay on record, so the caller can retry.
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java
index 7e8737ccf9..78069ad157 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import com.fasterxml.jackson.core.type.TypeReference;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
@@ -12,6 +13,7 @@ import io.github.hectorvent.floci.services.redshift.TempCredential;
import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.RedshiftServerlessSnapshot;
import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
import io.quarkus.runtime.StartupEvent;
import jakarta.annotation.Priority;
@@ -21,6 +23,10 @@ import jakarta.inject.Inject;
import jakarta.interceptor.Interceptor;
import org.jboss.logging.Logger;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -87,6 +93,8 @@ public class RedshiftServerlessService implements Resettable {
public static final int MIN_CREDENTIAL_DURATION_SECONDS = 900;
public static final int MAX_CREDENTIAL_DURATION_SECONDS = 3600;
+ private static final int MAX_RETENTION_DAYS = 3653;
+ private static final Pattern SNAPSHOT_NAME = Pattern.compile("[a-z0-9-]+");
private static final Pattern WORKGROUP_NAME = Pattern.compile("[a-z0-9-]+");
private static final Pattern TRACK_NAME = Pattern.compile("[a-zA-Z0-9_]+");
private static final Set<String> IP_ADDRESS_TYPES = Set.of("ipv4", "dualstack");
@@ -94,20 +102,26 @@ public class RedshiftServerlessService implements Resettable {
private final AccountAwareStorageBackend<Namespace> namespaces;
private final AccountAwareStorageBackend<Workgroup> workgroups;
+ private final AccountAwareStorageBackend<RedshiftServerlessSnapshot> snapshots;
private final RegionResolver regionResolver;
private final RedshiftServerlessEndpoints endpoints;
private final RedshiftServerlessRuntime runtime;
+ private final EmulatorConfig config;
@Inject
public RedshiftServerlessService(StorageFactory storageFactory, RegionResolver regionResolver,
- RedshiftServerlessEndpoints endpoints, RedshiftServerlessRuntime runtime) {
+ RedshiftServerlessEndpoints endpoints, RedshiftServerlessRuntime runtime,
+ EmulatorConfig config) {
this.namespaces = storageFactory.create("redshiftserverless", "redshiftserverless-namespaces.json",
new TypeReference<Map<String, Namespace>>() {});
this.workgroups = storageFactory.create("redshiftserverless", "redshiftserverless-workgroups.json",
new TypeReference<Map<String, Workgroup>>() {});
+ this.snapshots = storageFactory.create("redshiftserverless", "redshiftserverless-snapshots.json",
+ new TypeReference<Map<String, RedshiftServerlessSnapshot>>() {});
this.regionResolver = regionResolver;
this.endpoints = endpoints;
this.runtime = runtime;
+ this.config = config;
}
/**
@@ -333,13 +347,209 @@ public class RedshiftServerlessService implements Resettable {
return deleted;
}
+ /**
+ * Creates a snapshot directly in {@code region}. Real {@code CreateSnapshot} always creates
+ * in the source namespace's own region; a snapshot that should appear as a cross-region copy
+ * is created by calling this in the standby region, where it is immediately AVAILABLE. When
+ * the namespace's workgroup has a live database its contents are dumped with pg_dump, the way
+ * provisioned Redshift snapshots work; with no live database the snapshot is metadata only.
+ */
+ public synchronized RedshiftServerlessSnapshot createSnapshot(String snapshotName, String namespaceName,
+ Integer retentionPeriod,
+ Map<String, String> tags, String region) {
+ validateSnapshotName(snapshotName);
+ Integer retentionDays = validateRetentionPeriod(retentionPeriod);
+ Namespace namespace = getNamespace(namespaceName, region);
+ String key = storageKey(region, snapshotName);
+ if (snapshots.get(key).isPresent()) {
+ throw new AwsException("ConflictException",
+ "The snapshot " + snapshotName + " already exists.", 409);
+ }
+ RedshiftServerlessSnapshot snapshot = new RedshiftServerlessSnapshot();
+ snapshot.setSnapshotName(snapshotName);
+ snapshot.setNamespaceName(namespaceName);
+ snapshot.setNamespaceArn(namespace.getNamespaceArn());
+ snapshot.setRegion(region);
+ snapshot.setAccountId(regionResolver.getAccountId());
+ snapshot.setOwnerAccount(regionResolver.getAccountId());
+ snapshot.setSnapshotArn(regionResolver.buildArn("redshift-serverless", region, "snapshot/" + snapshotName));
+ snapshot.setStatus("AVAILABLE");
+ snapshot.setSnapshotCreateTime(Instant.now());
+ snapshot.setAdminUsername(adminUserOf(namespace));
+ snapshot.setKmsKeyId(namespace.getKmsKeyId());
+ snapshot.setRetentionPeriod(retentionDays);
+ snapshot.setTags(tags);
+ dumpDatabase(snapshot, namespace, region);
+ snapshots.put(key, snapshot);
+ return snapshot;
+ }
+
+ private static Integer validateRetentionPeriod(Integer retentionPeriod) {
+ if (retentionPeriod == null || retentionPeriod == -1) {
+ return null;
+ }
+ if (retentionPeriod < 1 || retentionPeriod > MAX_RETENTION_DAYS) {
+ throw validation("retentionPeriod must be -1 (indefinite) or between 1 and " + MAX_RETENTION_DAYS + ".");
+ }
+ return retentionPeriod;
+ }
+
+ private void dumpDatabase(RedshiftServerlessSnapshot snapshot, Namespace namespace, String region) {
+ Optional<Workgroup> attached = workgroupOf(namespace.getNamespaceName(), region);
+ if (attached.isEmpty() || !hasLiveRuntime(attached.get())) {
+ return;
+ }
+ Path dumpFile = dumpFileOf(snapshot.getSnapshotName(), region);
+ try {
+ Files.createDirectories(dumpFile.getParent());
+ runtime.takeSnapshot(workgroups.accountId(), region, attached.get().getWorkgroupName(),
+ attached.get().getMasterUsername(), namespace.getDbName(), dumpFile);
+ snapshot.setSqlDump(dumpFile.toString());
+ } catch (IOException e) {
+ throw new AwsException("InternalServerException",
+ "Failed to take snapshot " + snapshot.getSnapshotName() + ": " + e.getMessage(), 500);
+ } catch (RuntimeException e) {
+ deleteDumpQuietly(dumpFile);
+ throw e;
+ }
+ }
+
+ private static boolean hasLiveRuntime(Workgroup workgroup) {
+ return workgroup.getRuntimeHost() != null && workgroup.getRuntimePort() > 0;
+ }
+
+ /** Absolute, normalised {@code <persistentPath>/redshift-serverless-dumps/<accountId>/<region>} directory. */
+ private Path dumpDir(String region) {
+ return Paths.get(config.storage().persistentPath())
+ .resolve("redshift-serverless-dumps")
+ .resolve(workgroups.accountId())
+ .resolve(region)
+ .toAbsolutePath()
+ .normalize();
+ }
+
+ private Path dumpFileOf(String snapshotName, String region) {
+ Path dir = dumpDir(region);
+ Path file = dir.resolve(snapshotName + ".sql").normalize();
+ if (!file.startsWith(dir)) {
+ throw validation("Invalid snapshotName.");
+ }
+ return file;
+ }
+
+ /** A stored dump path is trusted only while it still resolves inside this account's dump directory. */
+ private Optional<Path> trustedDump(RedshiftServerlessSnapshot snapshot, String region) {
+ String stored = snapshot.getSqlDump();
+ if (stored == null || stored.isBlank()) {
+ return Optional.empty();
+ }
+ Path file = Paths.get(stored).toAbsolutePath().normalize();
+ return file.startsWith(dumpDir(region)) ? Optional.of(file) : Optional.empty();
+ }
+
+ private static void deleteDumpQuietly(Path dumpFile) {
+ try {
+ Files.deleteIfExists(dumpFile);
+ } catch (IOException e) {
+ LOG.warnv(e, "Could not delete the snapshot dump {0}", dumpFile);
+ }
+ }
+
+ public RedshiftServerlessSnapshot getSnapshot(String snapshotName, String region) {
+ requireSnapshotName(snapshotName);
+ return snapshots.get(storageKey(region, snapshotName))
+ .orElseThrow(() -> new AwsException("ResourceNotFoundException",
+ "The snapshot " + snapshotName + " was not found.", 404));
+ }
+
+ /**
+ * Recovers the snapshot name from a snapshot ARN, rejecting one that names another account,
+ * Region or resource type rather than resolving it to a local snapshot of the same name.
+ */
+ public String snapshotNameFromArn(String snapshotArn, String region) {
+ String prefix = regionResolver.buildArn("redshift-serverless", region, "snapshot/");
+ if (snapshotArn == null || !snapshotArn.startsWith(prefix) || snapshotArn.length() == prefix.length()) {
+ throw validation("snapshotArn does not identify a snapshot in this account and Region.");
+ }
+ return snapshotArn.substring(prefix.length());
+ }
+
+ public synchronized RedshiftServerlessSnapshot deleteSnapshot(String snapshotName, String region) {
+ RedshiftServerlessSnapshot deleted = getSnapshot(snapshotName, region);
+ snapshots.delete(storageKey(region, snapshotName));
+ trustedDump(deleted, region).ifPresent(RedshiftServerlessService::deleteDumpQuietly);
+ return deleted;
+ }
+
+ /**
+ * Lists the Region's snapshots, narrowed by every filter that is supplied: the namespace by
+ * name or ARN, the owning account, and a creation-time window that includes both ends.
+ */
+ public PaginatedResult<RedshiftServerlessSnapshot> listSnapshots(String namespaceName, String namespaceArn,
+ String ownerAccount, Instant startTime,
+ Instant endTime, String region,
+ Integer maxResults, String nextToken) {
+ List<RedshiftServerlessSnapshot> all = snapshots.scan(key -> key.startsWith(region + "::")).stream()
+ .filter(snapshot -> namespaceName == null || namespaceName.isBlank()
+ || namespaceName.equals(snapshot.getNamespaceName()))
+ .filter(snapshot -> namespaceArn == null || namespaceArn.isBlank()
+ || namespaceArn.equals(snapshot.getNamespaceArn()))
+ .filter(snapshot -> ownerAccount == null || ownerAccount.isBlank()
+ || ownerAccount.equals(snapshot.getOwnerAccount()))
+ .filter(snapshot -> startTime == null || !snapshot.getSnapshotCreateTime().isBefore(startTime))
+ .filter(snapshot -> endTime == null || !snapshot.getSnapshotCreateTime().isAfter(endTime))
+ .toList();
+ return Pagination.paginate(all, RedshiftServerlessSnapshot::getSnapshotName, maxResults, nextToken,
+ 100, 100, "ValidationException");
+ }
+
+ /**
+ * Restores into the already-existing namespace and workgroup in {@code region} and never
+ * creates either, mirroring real {@code RestoreFromSnapshot}. A snapshot that carries a dump
+ * replaces the workgroup's database contents with it; a metadata-only snapshot leaves the
+ * database untouched and only reports the namespace {@code AVAILABLE}.
+ */
+ public synchronized RestoreResult restoreFromSnapshot(String namespaceName, String workgroupName,
+ String snapshotName, String ownerAccount,
+ String region) {
+ validateNamespaceName(namespaceName);
+ RedshiftServerlessSnapshot snapshot = getSnapshot(snapshotName, region);
+ if (ownerAccount != null && !ownerAccount.isBlank() && !ownerAccount.equals(snapshot.getOwnerAccount())) {
+ throw new AwsException("ResourceNotFoundException",
+ "The snapshot " + snapshotName + " was not found.", 404);
+ }
+ Workgroup workgroup = getWorkgroup(workgroupName, region);
+ if (!namespaceName.equals(workgroup.getNamespaceName())) {
+ throw validation("The workgroup " + workgroupName + " does not belong to the namespace "
+ + namespaceName + ".");
+ }
+ Namespace namespace = getNamespace(namespaceName, region);
+ if (snapshot.getSqlDump() != null && hasLiveRuntime(workgroup)) {
+ Path dump = trustedDump(snapshot, region).filter(Files::exists).orElseThrow(() -> new AwsException(
+ "InternalServerException", "The data of snapshot " + snapshotName + " is no longer available.",
+ 500));
+ runtime.restoreSnapshot(workgroups.accountId(), region, workgroupName, workgroup.getMasterUsername(),
+ namespace.getDbName(), dump);
+ }
+ Namespace restored = new Namespace(namespace);
+ restored.setStatus("AVAILABLE");
+ namespaces.put(storageKey(region, namespaceName), restored);
+ return new RestoreResult(restored, snapshot);
+ }
+
+ public record RestoreResult(Namespace namespace, RedshiftServerlessSnapshot snapshot) {}
+
public Map<String, String> listTagsForResource(String resourceArn, String region) {
requireResourceArn(resourceArn);
Optional<Namespace> namespace = namespaceByArn(resourceArn, region);
if (namespace.isPresent()) {
return new LinkedHashMap<>(namespace.get().getTags());
}
- return new LinkedHashMap<>(workgroupByArn(resourceArn, region).orElseThrow(
+ Optional<Workgroup> workgroup = workgroupByArn(resourceArn, region);
+ if (workgroup.isPresent()) {
+ return new LinkedHashMap<>(workgroup.get().getTags());
+ }
+ return new LinkedHashMap<>(snapshotByArn(resourceArn, region).orElseThrow(
() -> resourceNotFound(resourceArn)).getTags());
}
@@ -353,7 +563,8 @@ public class RedshiftServerlessService implements Resettable {
public synchronized Map<String, String> untagResource(String resourceArn, List<String> tagKeys, String region) {
requireResourceArn(resourceArn);
- if (namespaceByArn(resourceArn, region).isEmpty() && workgroupByArn(resourceArn, region).isEmpty()) {
+ if (namespaceByArn(resourceArn, region).isEmpty() && workgroupByArn(resourceArn, region).isEmpty()
+ && snapshotByArn(resourceArn, region).isEmpty()) {
throw resourceNotFound(resourceArn);
}
if (tagKeys == null) {
@@ -364,7 +575,7 @@ public class RedshiftServerlessService implements Resettable {
/**
* Redshift Serverless tags whatever the ARN names, so the lookup is by ARN rather than by name.
- * Namespaces and workgroups are the taggable resources in Floci, so any other Redshift Serverless
+ * Namespaces, workgroups and snapshots are the taggable resources in Floci, so any other Redshift Serverless
* ARN resolves to nothing and is reported as absent rather than as an unsupported resource type.
* The change is applied to a copy and stored, never to the instance readers may hold.
*/
@@ -377,13 +588,27 @@ public class RedshiftServerlessService implements Resettable {
namespaces.put(storageKey(region, updated.getNamespaceName()), updated);
return new LinkedHashMap<>(updated.getTags());
}
- Workgroup workgroup = workgroupByArn(resourceArn, region).orElseThrow(() -> resourceNotFound(resourceArn));
- Workgroup updated = new Workgroup(workgroup);
+ Optional<Workgroup> workgroup = workgroupByArn(resourceArn, region);
+ if (workgroup.isPresent()) {
+ Workgroup updated = new Workgroup(workgroup.get());
+ change.accept(updated.getTags());
+ workgroups.put(storageKey(region, updated.getWorkgroupName()), updated);
+ return new LinkedHashMap<>(updated.getTags());
+ }
+ RedshiftServerlessSnapshot snapshot = snapshotByArn(resourceArn, region)
+ .orElseThrow(() -> resourceNotFound(resourceArn));
+ RedshiftServerlessSnapshot updated = new RedshiftServerlessSnapshot(snapshot);
change.accept(updated.getTags());
- workgroups.put(storageKey(region, updated.getWorkgroupName()), updated);
+ snapshots.put(storageKey(region, updated.getSnapshotName()), updated);
return new LinkedHashMap<>(updated.getTags());
}
+ private Optional<RedshiftServerlessSnapshot> snapshotByArn(String resourceArn, String region) {
+ return snapshots.scan(key -> key.startsWith(region + "::")).stream()
+ .filter(snapshot -> resourceArn.equals(snapshot.getSnapshotArn()))
+ .findFirst();
+ }
+
private Optional<Namespace> namespaceByArn(String resourceArn, String region) {
return namespaces.scan(key -> key.startsWith(region + "::")).stream()
.filter(namespace -> resourceArn.equals(namespace.getNamespaceArn()))
@@ -673,6 +898,27 @@ public class RedshiftServerlessService implements Resettable {
}
namespaces.clear();
workgroups.clear();
+ for (AccountAwareStorageBackend.AccountEntry<RedshiftServerlessSnapshot> entry
+ : snapshots.scanAllAccountEntries(key -> true)) {
+ String dump = entry.value().getSqlDump();
+ if (dump != null && !dump.isBlank()) {
+ deleteDumpQuietly(Paths.get(dump));
+ }
+ }
+ snapshots.clear();
+ }
+
+ private static void requireSnapshotName(String snapshotName) {
+ if (snapshotName == null || snapshotName.isBlank()) {
+ throw validation("snapshotName is required.");
+ }
+ }
+
+ private static void validateSnapshotName(String snapshotName) {
+ requireSnapshotName(snapshotName);
+ if (snapshotName.length() < 3 || snapshotName.length() > 255 || !SNAPSHOT_NAME.matcher(snapshotName).matches()) {
+ throw validation("snapshotName must be 3-255 characters of lowercase letters, numbers, and hyphens.");
+ }
}
private static void validateNamespaceName(String namespaceName) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/RedshiftServerlessSnapshot.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/RedshiftServerlessSnapshot.java
new file mode 100644
index 0000000000..41a0ccae19
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/RedshiftServerlessSnapshot.java
@@ -0,0 +1,168 @@
+package io.github.hectorvent.floci.services.redshiftserverless.model;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.time.Instant;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * A Redshift Serverless snapshot. Floci stores snapshot metadata only and does not model async
+ * cross-region copy: a snapshot created via {@code CreateSnapshot} in the standby region is
+ * immediately {@code AVAILABLE} there, standing in for what a real
+ * {@code SnapshotCopyConfiguration} would eventually produce.
+ */
+@RegisterForReflection
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class RedshiftServerlessSnapshot {
+
+ private String snapshotName;
+ private String snapshotArn;
+ private String namespaceName;
+ private String namespaceArn;
+ private String region;
+ private String accountId;
+ private String ownerAccount;
+ private String status = "AVAILABLE";
+ private Instant snapshotCreateTime;
+ private String adminUsername;
+ private String kmsKeyId;
+ private Integer retentionPeriod;
+ private String sqlDump;
+ private Map<String, String> tags = new LinkedHashMap<>();
+
+ public RedshiftServerlessSnapshot() {
+ }
+
+ public RedshiftServerlessSnapshot(RedshiftServerlessSnapshot other) {
+ this.snapshotName = other.snapshotName;
+ this.snapshotArn = other.snapshotArn;
+ this.namespaceName = other.namespaceName;
+ this.namespaceArn = other.namespaceArn;
+ this.region = other.region;
+ this.accountId = other.accountId;
+ this.ownerAccount = other.ownerAccount;
+ this.status = other.status;
+ this.snapshotCreateTime = other.snapshotCreateTime;
+ this.adminUsername = other.adminUsername;
+ this.kmsKeyId = other.kmsKeyId;
+ this.retentionPeriod = other.retentionPeriod;
+ this.sqlDump = other.sqlDump;
+ this.tags = new LinkedHashMap<>(other.tags);
+ }
+
+ public String getSnapshotName() {
+ return snapshotName;
+ }
+
+ public void setSnapshotName(String snapshotName) {
+ this.snapshotName = snapshotName;
+ }
+
+ public String getSnapshotArn() {
+ return snapshotArn;
+ }
+
+ public void setSnapshotArn(String snapshotArn) {
+ this.snapshotArn = snapshotArn;
+ }
+
+ public String getNamespaceName() {
+ return namespaceName;
+ }
+
+ public void setNamespaceName(String namespaceName) {
+ this.namespaceName = namespaceName;
+ }
+
+ public String getNamespaceArn() {
+ return namespaceArn;
+ }
+
+ public void setNamespaceArn(String namespaceArn) {
+ this.namespaceArn = namespaceArn;
+ }
+
+ public String getRegion() {
+ return region;
+ }
+
+ public void setRegion(String region) {
+ this.region = region;
+ }
+
+ public String getAccountId() {
+ return accountId;
+ }
+
+ public void setAccountId(String accountId) {
+ this.accountId = accountId;
+ }
+
+ public String getOwnerAccount() {
+ return ownerAccount;
+ }
+
+ public void setOwnerAccount(String ownerAccount) {
+ this.ownerAccount = ownerAccount;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(String status) {
+ this.status = status;
+ }
+
+ public Instant getSnapshotCreateTime() {
+ return snapshotCreateTime;
+ }
+
+ public void setSnapshotCreateTime(Instant snapshotCreateTime) {
+ this.snapshotCreateTime = snapshotCreateTime;
+ }
+
+ public String getAdminUsername() {
+ return adminUsername;
+ }
+
+ public void setAdminUsername(String adminUsername) {
+ this.adminUsername = adminUsername;
+ }
+
+ public String getKmsKeyId() {
+ return kmsKeyId;
+ }
+
+ public void setKmsKeyId(String kmsKeyId) {
+ this.kmsKeyId = kmsKeyId;
+ }
+
+ /** Days the snapshot is kept, or {@code null} for indefinitely. */
+ public Integer getRetentionPeriod() {
+ return retentionPeriod;
+ }
+
+ public void setRetentionPeriod(Integer retentionPeriod) {
+ this.retentionPeriod = retentionPeriod;
+ }
+
+ /** Path of the pg_dump file, or {@code null} for a metadata-only snapshot. */
+ public String getSqlDump() {
+ return sqlDump;
+ }
+
+ public void setSqlDump(String sqlDump) {
+ this.sqlDump = sqlDump;
+ }
+
+ public Map<String, String> getTags() {
+ return tags;
+ }
+
+ public void setTags(Map<String, String> tags) {
+ this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java
index 0feda779c8..4ee4d750d1 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java
@@ -8,15 +8,20 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
@QuarkusTest
@@ -187,6 +192,83 @@ class RedshiftServerlessIntegrationTest {
.body("__type", equalTo("ResourceNotFoundException"));
}
+ @Test
+ void snapshotLifecycleIsVisibleThroughSeparateReads() {
+ doAnswer(invocation -> {
+ Files.writeString(invocation.getArgument(5, Path.class), "-- dump");
+ return null;
+ }).when(runtime).takeSnapshot(any(), any(), any(), any(), any(), any());
+ call("CreateNamespace", "{\"namespaceName\":\"snap-life-ns\",\"adminUsername\":\"admin\"}").statusCode(200);
+ call("CreateWorkgroup", "{\"workgroupName\":\"snap-life-wg\",\"namespaceName\":\"snap-life-ns\"}")
+ .statusCode(200);
+
+ String snapshotArn = call("CreateSnapshot",
+ "{\"snapshotName\":\"snap-life\",\"namespaceName\":\"snap-life-ns\",\"retentionPeriod\":7,"
+ + "\"tags\":[{\"key\":\"env\",\"value\":\"dev\"}]}")
+ .statusCode(200)
+ .body("snapshot.snapshotName", equalTo("snap-life"))
+ .body("snapshot.snapshotRetentionPeriod", equalTo(7))
+ .body("snapshot.snapshotRemainingDays", equalTo(7))
+ .body("snapshot.adminUsername", equalTo("admin"))
+ .body("snapshot.kmsKeyId", equalTo("AWS_OWNED_KMS_KEY"))
+ .body("snapshot.namespaceName", equalTo("snap-life-ns"))
+ .body("snapshot.status", equalTo("AVAILABLE"))
+ .body("snapshot.snapshotCreateTime", matchesPattern("\\d{4}-\\d{2}-\\d{2}T.*Z"))
+ .extract().path("snapshot.snapshotArn");
+
+ call("CreateSnapshot", "{\"snapshotName\":\"snap-life\",\"namespaceName\":\"snap-life-ns\"}")
+ .statusCode(409)
+ .body("__type", equalTo("ConflictException"));
+
+ call("GetSnapshot", "{\"snapshotName\":\"snap-life\"}")
+ .statusCode(200)
+ .body("snapshot.snapshotArn", equalTo(snapshotArn));
+ call("GetSnapshot", "{\"snapshotArn\":\"" + snapshotArn + "\"}")
+ .statusCode(200)
+ .body("snapshot.snapshotName", equalTo("snap-life"));
+ call("GetSnapshot", "{\"snapshotArn\":\"arn:aws:redshift-serverless:us-east-1:999999999999:snapshot/snap-life\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"));
+
+ call("ListSnapshots", "{\"namespaceName\":\"snap-life-ns\",\"maxResults\":1}")
+ .statusCode(200)
+ .body("snapshots.snapshotName", hasItem("snap-life"));
+ call("ListTagsForResource", "{\"resourceArn\":\"" + snapshotArn + "\"}")
+ .statusCode(200)
+ .body("tags.key", hasItem("env"));
+ call("ListSnapshots", "{\"ownerAccount\":\"999999999999\"}")
+ .statusCode(200)
+ .body("snapshots", hasSize(0));
+ call("ListSnapshots", "{\"startTime\":4102444800}")
+ .statusCode(200)
+ .body("snapshots", hasSize(0));
+ call("RestoreFromSnapshot", "{\"namespaceName\":\"snap-life-ns\",\"workgroupName\":\"snap-life-wg\","
+ + "\"snapshotName\":\"snap-life\",\"snapshotArn\":\"" + snapshotArn + "\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"));
+ call("ListSnapshots", "{\"namespaceName\":\"no-such-ns\"}")
+ .statusCode(200)
+ .body("snapshots", hasSize(0));
+
+ call("RestoreFromSnapshot", "{\"namespaceName\":\"snap-life-ns\",\"workgroupName\":\"snap-life-wg\","
+ + "\"snapshotArn\":\"" + snapshotArn + "\"}")
+ .statusCode(200)
+ .body("snapshotName", equalTo("snap-life"))
+ .body("namespace.namespaceName", equalTo("snap-life-ns"))
+ .body("namespace.status", equalTo("AVAILABLE"))
+ .body("ownerAccount", matchesPattern("\\d{12}"));
+ call("RestoreFromSnapshot", "{\"workgroupName\":\"snap-life-wg\",\"snapshotName\":\"snap-life\"}")
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"));
+
+ call("DeleteSnapshot", "{\"snapshotName\":\"snap-life\"}")
+ .statusCode(200)
+ .body("snapshot.snapshotName", equalTo("snap-life"));
+ call("GetSnapshot", "{\"snapshotName\":\"snap-life\"}")
+ .statusCode(404)
+ .body("__type", equalTo("ResourceNotFoundException"));
+ }
+
@Test
void workgroupLifecycleIsVisibleThroughSeparateReads() {
call("CreateNamespace", "{\"namespaceName\":\"wg-life-ns\",\"adminUsername\":\"admin\"}").statusCode(200);
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java
index 694f07288c..da0d2dbfc3 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -74,8 +75,10 @@ class RedshiftServerlessJsonHandlerTest {
Instant.ofEpochSecond(1_800_000_000L), List.of()));
iamDbUserResolver = mock(RedshiftIamDbUserResolver.class);
when(iamDbUserResolver.resolveDbUser("AUTH")).thenReturn("IAM:alice");
+ EmulatorConfig config = mock(EmulatorConfig.class, org.mockito.Mockito.RETURNS_DEEP_STUBS);
+ when(config.storage().persistentPath()).thenReturn(System.getProperty("java.io.tmpdir"));
handler = new RedshiftServerlessJsonHandler(
- new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime), mapper,
+ new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime, config), mapper,
iamDbUserResolver);
}
@@ -287,7 +290,7 @@ class RedshiftServerlessJsonHandlerTest {
@Test
void anUnknownActionIsReportedAsUnknownOperation() {
- Response response = handler.handle("CreateSnapshot", mapper.createObjectNode(), REGION);
+ Response response = handler.handle("CreateScheduledAction", mapper.createObjectNode(), REGION);
assertEquals(400, response.getStatus());
assertEquals("UnknownOperationException", errorType(response));
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java
index a7c5915bf8..d88c0d4658 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.redshiftserverless;
+import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
import io.github.hectorvent.floci.services.redshift.RedshiftCredentialBroker;
import io.github.hectorvent.floci.services.redshift.TempCredential;
@@ -11,6 +12,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
+import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
@@ -25,6 +27,7 @@ import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -70,6 +73,41 @@ class RedshiftServerlessRuntimeTest {
verify(containers, never()).adoptOrStart(any(), any(), any(), any(), any());
}
+ @Test
+ void takeSnapshotDumpsTheWorkgroupBackend() {
+ Path dump = Path.of("/tmp/dump.sql");
+
+ runtime.takeSnapshot(ACCOUNT, REGION, "my-wg", "root", "analytics", dump);
+
+ verify(containers).takeSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", dump, true);
+ }
+
+ @Test
+ void restoreSnapshotEmptiesTheSchemasBeforeReplayingTheDumpAndStopsOnError() {
+ Path dump = Path.of("/tmp/dump.sql");
+
+ runtime.restoreSnapshot(ACCOUNT, REGION, "my-wg", "root", "analytics", dump);
+
+ InOrder order = inOrder(containers);
+ order.verify(containers).takeSnapshot(eq(ACCOUNT), eq(BACKEND_ID), eq("root"), eq("analytics"), any(Path.class), eq(true));
+ order.verify(containers).resetUserSchemas(ACCOUNT, BACKEND_ID, "root", "analytics");
+ order.verify(containers).restoreSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", dump, true);
+ }
+
+ @Test
+ void aFailedReplayPutsTheEarlierContentsBack() {
+ Path dump = Path.of("/tmp/dump.sql");
+ doThrow(new AwsException("InternalFailure", "bad dump", 500)).when(containers)
+ .restoreSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", dump, true);
+
+ assertThrows(AwsException.class,
+ () -> runtime.restoreSnapshot(ACCOUNT, REGION, "my-wg", "root", "analytics", dump));
+
+ verify(containers, times(2)).resetUserSchemas(ACCOUNT, BACKEND_ID, "root", "analytics");
+ verify(containers, times(2)).restoreSnapshot(eq(ACCOUNT), eq(BACKEND_ID), eq("root"), eq("analytics"),
+ any(Path.class), eq(true));
+ }
+
@Test
void adoptReattachesToASurvivingContainer() {
when(containers.adoptOrStart(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics")).thenReturn(handle());
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java
index d4bf203423..7bdb66a2e6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import com.fasterxml.jackson.core.type.TypeReference;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -11,11 +12,15 @@ import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.RedshiftServerlessSnapshot;
import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.time.Instant;
import java.util.List;
import java.util.Map;
@@ -24,6 +29,7 @@ import java.util.UUID;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -34,6 +40,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -52,6 +59,9 @@ class RedshiftServerlessServiceTest {
private RedshiftServerlessRuntime runtime;
private int nextPort;
+ @TempDir
+ Path dumpDir;
+
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
@@ -67,7 +77,11 @@ class RedshiftServerlessServiceTest {
when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-workgroups.json"),
any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) workgroupStore);
+ when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-snapshots.json"),
+ any(TypeReference.class))).thenReturn(AccountAwareStorageBackend.inMemory(ACCOUNT_ID));
+
RegionResolver regionResolver = mock(RegionResolver.class);
+ when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
when(regionResolver.buildArn(eq("redshift-serverless"), any(String.class), any(String.class)))
.thenAnswer(invocation -> "arn:aws:redshift-serverless:"
+ invocation.getArgument(1, String.class) + ":" + ACCOUNT_ID + ":"
@@ -80,7 +94,9 @@ class RedshiftServerlessServiceTest {
when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
.thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
- service = new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime);
+ EmulatorConfig config = mock(EmulatorConfig.class, org.mockito.Mockito.RETURNS_DEEP_STUBS);
+ when(config.storage().persistentPath()).thenReturn(dumpDir.toString());
+ service = new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime, config);
}
@Test
@@ -791,6 +807,218 @@ class RedshiftServerlessServiceTest {
verify(runtime).stop(ACCOUNT_ID, REGION, "clr-wg");
}
+ @Test
+ void listSnapshotsFiltersByNamespace() {
+ create("snap-a");
+ create("snap-b");
+ RedshiftServerlessSnapshot snapshot = service.createSnapshot("snap-one", "snap-a", null, null, REGION);
+ service.createSnapshot("snap-two", "snap-b", null, null, REGION);
+
+ assertEquals("AVAILABLE", snapshot.getStatus());
+ assertEquals(ACCOUNT_ID, snapshot.getOwnerAccount());
+ assertEquals(List.of("snap-one"), service.listSnapshots("snap-a", null, null, null, null, REGION, null, null).items().stream()
+ .map(RedshiftServerlessSnapshot::getSnapshotName).toList());
+ assertEquals(2, service.listSnapshots(null, null, null, null, null, REGION, null, null).items().size());
+
+ AwsException conflict = assertThrows(AwsException.class,
+ () -> service.createSnapshot("snap-one", "snap-a", null, null, REGION));
+ assertEquals("ConflictException", conflict.getErrorCode());
+ }
+
+ @Test
+ void restoreRequiresAnExistingNamespaceWorkgroupAndSnapshot() {
+ create("restore-ns");
+ service.createSnapshot("restore-snap", "restore-ns", null, null, REGION);
+
+ AwsException noWorkgroup = assertThrows(AwsException.class,
+ () -> service.restoreFromSnapshot("restore-ns", "restore-wg", "restore-snap", null, REGION));
+ assertEquals("ResourceNotFoundException", noWorkgroup.getErrorCode());
+
+ createWorkgroup("restore-wg", "restore-ns");
+ RedshiftServerlessService.RestoreResult restored =
+ service.restoreFromSnapshot("restore-ns", "restore-wg", "restore-snap", null, REGION);
+ assertEquals("AVAILABLE", restored.namespace().getStatus());
+ assertEquals("restore-snap", restored.snapshot().getSnapshotName());
+
+ AwsException noSnapshot = assertThrows(AwsException.class,
+ () -> service.restoreFromSnapshot("restore-ns", "restore-wg", "missing-snap", null, REGION));
+ assertEquals("ResourceNotFoundException", noSnapshot.getErrorCode());
+ }
+
+ @Test
+ void restoreRejectsAWorkgroupOfAnotherNamespace() {
+ create("rest-a");
+ create("rest-b");
+ service.createSnapshot("rest-snap", "rest-a", null, null, REGION);
+ createWorkgroup("rest-b-wg", "rest-b");
+
+ AwsException mismatch = assertThrows(AwsException.class,
+ () -> service.restoreFromSnapshot("rest-a", "rest-b-wg", "rest-snap", null, REGION));
+ assertEquals("ValidationException", mismatch.getErrorCode());
+ }
+
+ @Test
+ void snapshotNameFromArnRejectsAnArnOutsideThisAccountAndRegion() {
+ create("arn-ns");
+ RedshiftServerlessSnapshot snapshot = service.createSnapshot("arn-snap", "arn-ns", null, null, REGION);
+
+ assertEquals("arn-snap", service.snapshotNameFromArn(snapshot.getSnapshotArn(), REGION));
+ for (String foreign : List.of(
+ "arn:aws:redshift-serverless:eu-west-1:" + ACCOUNT_ID + ":snapshot/arn-snap",
+ "arn:aws:redshift-serverless:us-east-1:999999999999:snapshot/arn-snap",
+ "arn:aws:redshift-serverless:us-east-1:" + ACCOUNT_ID + ":workgroup/arn-snap")) {
+ AwsException rejected = assertThrows(AwsException.class,
+ () -> service.snapshotNameFromArn(foreign, REGION));
+ assertEquals("ValidationException", rejected.getErrorCode());
+ }
+ }
+
+ @Test
+ void restoreWithoutANamespaceNameIsAValidationError() {
+ create("nullns-ns");
+ service.createSnapshot("nullns-snap", "nullns-ns", null, null, REGION);
+ createWorkgroup("nullns-wg", "nullns-ns");
+
+ AwsException missing = assertThrows(AwsException.class,
+ () -> service.restoreFromSnapshot(null, "nullns-wg", "nullns-snap", null, REGION));
+ assertEquals("ValidationException", missing.getErrorCode());
+ }
+
+ @Test
+ void snapshotNamesAreValidated() {
+ create("names-ns");
+ for (String bad : new String[] {null, "", "ab", "has/slash", "has::colons", "UPPER"}) {
+ AwsException rejected = assertThrows(AwsException.class,
+ () -> service.createSnapshot(bad, "names-ns", null, null, REGION));
+ assertEquals("ValidationException", rejected.getErrorCode());
+ }
+ AwsException missing = assertThrows(AwsException.class, () -> service.getSnapshot(null, REGION));
+ assertEquals("ValidationException", missing.getErrorCode());
+ assertThrows(AwsException.class, () -> service.deleteSnapshot(null, REGION));
+ }
+
+ @Test
+ void createSnapshotDumpsALiveWorkgroupAndRestoreReplaysIt() throws Exception {
+ create("dump-ns");
+ createWorkgroup("dump-wg", "dump-ns");
+ doAnswer(invocation -> {
+ Files.writeString(invocation.getArgument(5, Path.class), "-- dump");
+ return null;
+ }).when(runtime).takeSnapshot(any(), any(), any(), any(), any(), any());
+
+ RedshiftServerlessSnapshot snapshot = service.createSnapshot("dump-snap", "dump-ns", null, null, REGION);
+
+ assertTrue(Files.exists(Path.of(snapshot.getSqlDump())));
+ service.restoreFromSnapshot("dump-ns", "dump-wg", "dump-snap", null, REGION);
+ verify(runtime).restoreSnapshot(eq(ACCOUNT_ID), eq(REGION), eq("dump-wg"), any(), eq("dev"),
+ eq(Path.of(snapshot.getSqlDump())));
+
+ service.deleteSnapshot("dump-snap", REGION);
+ assertFalse(Files.exists(Path.of(snapshot.getSqlDump())));
+ }
+
+ @Test
+ void restoreFailsWhenTheDumpFileIsGone() throws Exception {
+ create("gone-ns");
+ createWorkgroup("gone-wg", "gone-ns");
+ doAnswer(invocation -> {
+ Files.writeString(invocation.getArgument(5, Path.class), "-- dump");
+ return null;
+ }).when(runtime).takeSnapshot(any(), any(), any(), any(), any(), any());
+ RedshiftServerlessSnapshot snapshot = service.createSnapshot("gone-snap", "gone-ns", null, null, REGION);
+ Files.delete(Path.of(snapshot.getSqlDump()));
+
+ AwsException missing = assertThrows(AwsException.class,
+ () -> service.restoreFromSnapshot("gone-ns", "gone-wg", "gone-snap", null, REGION));
+ assertEquals("InternalServerException", missing.getErrorCode());
+ verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void createSnapshotWithoutALiveWorkgroupIsMetadataOnly() {
+ create("meta-ns");
+ createWorkgroup("meta-wg", "meta-ns");
+ Workgroup stopped = new Workgroup(service.getWorkgroup("meta-wg", REGION));
+ stopped.setRuntimeHost(null);
+ stopped.setRuntimePort(0);
+ workgroupStore.put(REGION + "::meta-wg", stopped);
+
+ RedshiftServerlessSnapshot snapshot = service.createSnapshot("meta-snap", "meta-ns", null, null, REGION);
+ service.restoreFromSnapshot("meta-ns", "meta-wg", "meta-snap", null, REGION);
+
+ assertNull(snapshot.getSqlDump());
+ verify(runtime, never()).takeSnapshot(any(), any(), any(), any(), any(), any());
+ verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void snapshotCarriesRetentionNamespaceDetailsAndTags() {
+ create("meta-fields-ns");
+
+ RedshiftServerlessSnapshot snapshot = service.createSnapshot("fields-snap", "meta-fields-ns", 30,
+ Map.of("env", "dev"), REGION);
+
+ assertEquals(30, snapshot.getRetentionPeriod());
+ assertEquals("admin", snapshot.getAdminUsername());
+ assertEquals("AWS_OWNED_KMS_KEY", snapshot.getKmsKeyId());
+ assertEquals(Map.of("env", "dev"), service.listTagsForResource(snapshot.getSnapshotArn(), REGION));
+
+ service.tagResource(snapshot.getSnapshotArn(), Map.of("team", "data"), REGION);
+ service.untagResource(snapshot.getSnapshotArn(), List.of("env"), REGION);
+ assertEquals(Map.of("team", "data"), service.listTagsForResource(snapshot.getSnapshotArn(), REGION));
+
+ assertNull(service.createSnapshot("indef-snap", "meta-fields-ns", -1, null, REGION).getRetentionPeriod());
+ for (int bad : new int[] {0, 3654, -2}) {
+ AwsException rejected = assertThrows(AwsException.class,
+ () -> service.createSnapshot("bad-ret-snap", "meta-fields-ns", bad, null, REGION));
+ assertEquals("ValidationException", rejected.getErrorCode());
+ }
+ }
+
+ @Test
+ void listSnapshotsAppliesEveryFilter() throws Exception {
+ create("filt-a");
+ create("filt-b");
+ RedshiftServerlessSnapshot first = service.createSnapshot("filt-one", "filt-a", null, null, REGION);
+ service.createSnapshot("filt-two", "filt-b", null, null, REGION);
+ Instant created = first.getSnapshotCreateTime();
+
+ assertEquals(1, service.listSnapshots(null, first.getNamespaceArn(), null, null, null, REGION, null, null)
+ .items().size());
+ assertEquals(2, service.listSnapshots(null, null, ACCOUNT_ID, null, null, REGION, null, null).items().size());
+ assertEquals(0, service.listSnapshots(null, null, "999999999999", null, null, REGION, null, null)
+ .items().size());
+ assertEquals(0, service.listSnapshots(null, null, null, created.plusSeconds(3600), null, REGION, null, null)
+ .items().size());
+ assertEquals(0, service.listSnapshots(null, null, null, null, created.minusSeconds(3600), REGION, null, null)
+ .items().size());
+ assertEquals(2, service.listSnapshots(null, null, null, created.minusSeconds(3600),
+ created.plusSeconds(3600), REGION, null, null).items().size());
+ }
+
+ @Test
+ void restoreRejectsASnapshotOwnedByAnotherAccount() {
+ create("owner-ns");
+ service.createSnapshot("owner-snap", "owner-ns", null, null, REGION);
+ createWorkgroup("owner-wg", "owner-ns");
+
+ AwsException missing = assertThrows(AwsException.class,
+ () -> service.restoreFromSnapshot("owner-ns", "owner-wg", "owner-snap", "999999999999", REGION));
+ assertEquals("ResourceNotFoundException", missing.getErrorCode());
+ service.restoreFromSnapshot("owner-ns", "owner-wg", "owner-snap", ACCOUNT_ID, REGION);
+ }
+
+ @Test
+ void deleteSnapshotRemovesIt() {
+ create("snapdel-ns");
+ service.createSnapshot("snapdel", "snapdel-ns", null, null, REGION);
+
+ service.deleteSnapshot("snapdel", REGION);
+
+ AwsException missing = assertThrows(AwsException.class, () -> service.getSnapshot("snapdel", REGION));
+ assertEquals("ResourceNotFoundException", missing.getErrorCode());
+ }
+
private Namespace create(String namespaceName) {
return service.createNamespace(namespaceName, "admin", null, null, null, null, null, Map.of(), REGION);
}