mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(rds): stop, start and reboot DB instances and clusters (#4071)
* feat(rds): stop, start and reboot DB instances and clusters Add StopDBInstance, StartDBInstance, StopDBCluster, StartDBCluster and RebootDBCluster. A stop reports "stopping", takes the optional snapshot, removes the container and proxy and settles the record to "stopped" while its identifier, endpoint, groups and Docker volume stay; a start reports "starting" and brings the container back on the kept volume and port. Cluster stop, start and reboot carry the member instances along. StopDBInstance refuses cluster members, read replicas and their sources, and anything not available; ModifyDBInstance refuses a stopped instance; a stopped resource stays stopped across an emulator restart. Closes #4043 * feat(rds): refuse starting a stopped cluster's member alone, serialize cluster lifecycle calls StartDBInstance now refuses a cluster member as StopDBInstance does, and both answer InvalidDBClusterStateFault, the fault the model lists for a member on either call; a member started alone would have got a standalone container on its own volume while the cluster stayed stopped. stopDbCluster, startDbCluster and rebootDbCluster are synchronized like the instance calls, so two concurrent starts cannot both see STOPPED. The five handler arms use the shared required-parameter helper and the central AwsException handling. * test(rds): import DbInstanceStatus instead of qualifying it inline AGENTS.md asks for imports over inline fully-qualified names; the file already imports DbInstanceStatus.
This commit is contained in:
1 parent
c083aad12b
commit
1542c641a6
8 files changed
+910
-1
No files matched your search
+32
@@ -14,7 +14,9 @@ import software.amazon.awssdk.services.rds.model.ConnectionPoolConfigurationInfo
|
||||
import software.amazon.awssdk.services.rds.model.CreateDbProxyResponse;
|
||||
import software.amazon.awssdk.services.rds.model.CreateDbSubnetGroupResponse;
|
||||
import software.amazon.awssdk.services.rds.model.CreateOptionGroupResponse;
|
||||
import software.amazon.awssdk.services.rds.model.DBInstance;
|
||||
import software.amazon.awssdk.services.rds.model.DBProxyTarget;
|
||||
import software.amazon.awssdk.services.rds.model.InvalidDbInstanceStateException;
|
||||
import software.amazon.awssdk.services.rds.model.DescribeDbSubnetGroupsResponse;
|
||||
import software.amazon.awssdk.services.rds.model.DescribeOptionGroupsResponse;
|
||||
import software.amazon.awssdk.services.rds.model.DescribeOrderableDbInstanceOptionsResponse;
|
||||
@@ -394,6 +396,36 @@ class RdsControlPlaneTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkStopsAndStartsAStandaloneInstance() throws Exception {
|
||||
String instanceName = TestFixtures.uniqueName("rds-stop-db");
|
||||
try {
|
||||
createDbInstance(rds, instanceName, "stop-secret");
|
||||
String endpoint = rds.describeDBInstances(b -> b.dbInstanceIdentifier(instanceName))
|
||||
.dbInstances().get(0).endpoint().address();
|
||||
|
||||
DBInstance stopping = rds.stopDBInstance(b -> b.dbInstanceIdentifier(instanceName)).dbInstance();
|
||||
assertThat(stopping.dbInstanceStatus()).isEqualTo("stopping");
|
||||
assertThat(rds.describeDBInstances(b -> b.dbInstanceIdentifier(instanceName))
|
||||
.dbInstances().get(0).dbInstanceStatus()).isEqualTo("stopped");
|
||||
|
||||
assertThatThrownBy(() -> rds.stopDBInstance(b -> b.dbInstanceIdentifier(instanceName)))
|
||||
.isInstanceOf(InvalidDbInstanceStateException.class);
|
||||
assertThatThrownBy(() -> rds.modifyDBInstance(b -> b
|
||||
.dbInstanceIdentifier(instanceName).masterUserPassword("changed-secret")))
|
||||
.isInstanceOf(InvalidDbInstanceStateException.class);
|
||||
|
||||
DBInstance starting = rds.startDBInstance(b -> b.dbInstanceIdentifier(instanceName)).dbInstance();
|
||||
assertThat(starting.dbInstanceStatus()).isEqualTo("starting");
|
||||
DBInstance started = rds.describeDBInstances(b -> b.dbInstanceIdentifier(instanceName))
|
||||
.dbInstances().get(0);
|
||||
assertThat(started.dbInstanceStatus()).isEqualTo("available");
|
||||
assertThat(started.endpoint().address()).isEqualTo(endpoint);
|
||||
} finally {
|
||||
deleteDbInstance(rds, instanceName);
|
||||
}
|
||||
}
|
||||
|
||||
private static CreateDbProxyResponse createIamProxy(RdsClient client, String name, List<String> vpcSubnetIds) {
|
||||
return client.createDBProxy(b -> b
|
||||
.dbProxyName(name)
|
||||
|
||||
@@ -18,6 +18,11 @@ RDS Data API (`rds-data`) is documented separately because it uses REST JSON rou
|
||||
| `DeleteDBInstance` | Stop and remove an instance |
|
||||
| `ModifyDBInstance` | Update instance settings |
|
||||
| `RebootDBInstance` | Restart a database instance |
|
||||
| `StopDBInstance` | Stop a standalone instance temporarily: an optional `DBSnapshotIdentifier` first, then its container goes away while the record, endpoint and volume stay |
|
||||
| `StartDBInstance` | Start a stopped instance on the volume it kept, same endpoint |
|
||||
| `StopDBCluster` | Stop a cluster and its member instances |
|
||||
| `StartDBCluster` | Start a stopped cluster and its members |
|
||||
| `RebootDBCluster` | Restart a cluster's database and its members' proxies |
|
||||
| `CreateDBInstanceReadReplica` | Create a read replica of a PostgreSQL instance, initialised from a copy of the source; see [Read replicas](#read-replicas) |
|
||||
| `PromoteReadReplica` | Detach a read replica into a standalone instance and turn automated backups on |
|
||||
| `SwitchoverReadReplica` | Refused with `InvalidDBInstanceState`: AWS supports switchover only for Oracle and SQL Server replicas, neither of which is emulated |
|
||||
@@ -95,6 +100,20 @@ usual default, a 30-minute window starting where the given one ends); a window g
|
||||
checked against the instance's other window. Modifications apply immediately —
|
||||
`PendingModifiedValues` is not modeled.
|
||||
|
||||
!!! note "Stopping and starting"
|
||||
|
||||
`StopDBInstance` and `StopDBCluster` follow the user guide: the response reports `stopping`
|
||||
(`StopDBCluster` for the cluster and its members), the stored status settles to `stopped`,
|
||||
and `StartDBInstance` / `StartDBCluster` report `starting` and settle to `available`. While
|
||||
stopped, the identifier, endpoint (same port), parameter and option groups and the Docker
|
||||
volume all stay, so the data comes back on start; the container itself is removed and the
|
||||
endpoint refuses connections. `StopDBInstance` refuses a cluster member (use `StopDBCluster`),
|
||||
a read replica or an instance that has one, and anything not `available`, with
|
||||
`InvalidDBInstanceState`; `ModifyDBInstance` on a stopped instance is refused the same way.
|
||||
`DeleteDBInstance` works on a stopped instance. A stopped instance or cluster stays stopped
|
||||
across an emulator restart. Not modeled: the automatic restart after seven days, and the
|
||||
Multi-AZ SQL Server restriction.
|
||||
|
||||
!!! note "DB snapshot tagging and lifecycle"
|
||||
|
||||
`CreateDBSnapshot` accepts `Tags`, and `TagResource`/`UntagResource`/`ListTagsForResource`
|
||||
|
||||
@@ -40,6 +40,7 @@ import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
* Query-protocol handler for all RDS actions (form-encoded POST, XML response).
|
||||
@@ -76,6 +77,11 @@ public class RdsQueryHandler {
|
||||
case "DeleteDBInstance" -> handleDeleteDbInstance(params, region);
|
||||
case "ModifyDBInstance" -> handleModifyDbInstance(params, region);
|
||||
case "RebootDBInstance" -> handleRebootDbInstance(params, region);
|
||||
case "StopDBInstance" -> handleStopDbInstance(params, region);
|
||||
case "StartDBInstance" -> handleStartDbInstance(params, region);
|
||||
case "StopDBCluster" -> handleStopDbCluster(params, region);
|
||||
case "StartDBCluster" -> handleStartDbCluster(params, region);
|
||||
case "RebootDBCluster" -> handleRebootDbCluster(params, region);
|
||||
case "CreateDBInstanceReadReplica" -> handleCreateDbInstanceReadReplica(params, region);
|
||||
case "PromoteReadReplica" -> handlePromoteReadReplica(params, region);
|
||||
case "SwitchoverReadReplica" -> handleSwitchoverReadReplica(params, region);
|
||||
@@ -606,6 +612,49 @@ public class RdsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleStopDbInstance(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("DBInstanceIdentifier");
|
||||
Response missing = firstMissingParam("DBInstanceIdentifier", id);
|
||||
if (missing != null) {
|
||||
return missing;
|
||||
}
|
||||
DbInstance instance = service.stopDbInstance(id, params.getFirst("DBSnapshotIdentifier"), region);
|
||||
return Response.ok(AwsQueryResponse.envelope("StopDBInstance", AwsNamespaces.RDS, dbInstanceXml(instance))).build();
|
||||
}
|
||||
|
||||
private Response handleStartDbInstance(MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("DBInstanceIdentifier");
|
||||
Response missing = firstMissingParam("DBInstanceIdentifier", id);
|
||||
if (missing != null) {
|
||||
return missing;
|
||||
}
|
||||
DbInstance instance = service.startDbInstance(id, region);
|
||||
return Response.ok(AwsQueryResponse.envelope("StartDBInstance", AwsNamespaces.RDS, dbInstanceXml(instance))).build();
|
||||
}
|
||||
|
||||
private Response handleStopDbCluster(MultivaluedMap<String, String> params, String region) {
|
||||
return clusterLifecycleResponse("StopDBCluster", params, region, service::stopDbCluster);
|
||||
}
|
||||
|
||||
private Response handleStartDbCluster(MultivaluedMap<String, String> params, String region) {
|
||||
return clusterLifecycleResponse("StartDBCluster", params, region, service::startDbCluster);
|
||||
}
|
||||
|
||||
private Response handleRebootDbCluster(MultivaluedMap<String, String> params, String region) {
|
||||
return clusterLifecycleResponse("RebootDBCluster", params, region, service::rebootDbCluster);
|
||||
}
|
||||
|
||||
private Response clusterLifecycleResponse(String action, MultivaluedMap<String, String> params, String region,
|
||||
BiFunction<String, String, DbCluster> operation) {
|
||||
String id = params.getFirst("DBClusterIdentifier");
|
||||
Response missing = firstMissingParam("DBClusterIdentifier", id);
|
||||
if (missing != null) {
|
||||
return missing;
|
||||
}
|
||||
DbCluster cluster = operation.apply(id, region);
|
||||
return Response.ok(AwsQueryResponse.envelope(action, AwsNamespaces.RDS, dbClusterXml(cluster))).build();
|
||||
}
|
||||
|
||||
private Response handleRebootDbInstance(
|
||||
MultivaluedMap<String, String> params, String region) {
|
||||
String id = params.getFirst("DBInstanceIdentifier");
|
||||
@@ -2491,6 +2540,9 @@ public class RdsQueryHandler {
|
||||
case REBOOTING -> "rebooting";
|
||||
case MODIFYING -> "modifying";
|
||||
case FAILED -> "failed";
|
||||
case STOPPING -> "stopping";
|
||||
case STOPPED -> "stopped";
|
||||
case STARTING -> "starting";
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@ package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
@@ -80,6 +82,12 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(RdsService.class);
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
// Copies a record for a response that must report a transitional status ("stopping",
|
||||
// "starting") while the stored record settles to the final one in the same call. The
|
||||
// records already round-trip through Jackson for persistence.
|
||||
private static final ObjectMapper RESPONSE_COPIER = new ObjectMapper()
|
||||
.registerModule(new JavaTimeModule())
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
private static final Duration EVENT_RETENTION = Duration.ofDays(14);
|
||||
/** Value AWS reports as the OwningService of a secret RDS manages. */
|
||||
private static final String MANAGED_SECRET_OWNING_SERVICE = "rds";
|
||||
@@ -2042,6 +2050,12 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
validateInstanceSettings(settings);
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbInstance instance = getDbInstance(id, effectiveRegion);
|
||||
// "You can't modify a stopped DB instance" (user guide, stopping an instance temporarily).
|
||||
if (isStoppedOrInTransit(instance.getStatus())) {
|
||||
throw new AwsException("InvalidDBInstanceState",
|
||||
"DB instance " + id + " is in state " + instance.getStatus().name().toLowerCase(Locale.ROOT)
|
||||
+ " and cannot be modified.", 400);
|
||||
}
|
||||
DbInstanceSettings effective = withEffectiveWindows(settings, instance);
|
||||
instance.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
if (optionGroupName != null && !optionGroupName.isBlank()) {
|
||||
@@ -2227,6 +2241,338 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
.toList();
|
||||
}
|
||||
|
||||
// ── Stop, start and reboot ────────────────────────────────────────────────
|
||||
|
||||
/** Aurora members are stopped and started through their cluster; the model lists InvalidDBClusterStateFault for both calls. */
|
||||
private static void refuseClusterMember(DbInstance instance, String id, String clusterOperation) {
|
||||
if (instance.getDbClusterIdentifier() != null && !instance.getDbClusterIdentifier().isBlank()) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB instance " + id + " is a member of DB cluster " + instance.getDbClusterIdentifier()
|
||||
+ "; use " + clusterOperation + " on the cluster.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isStoppedOrInTransit(DbInstanceStatus status) {
|
||||
return status == DbInstanceStatus.STOPPING || status == DbInstanceStatus.STOPPED
|
||||
|| status == DbInstanceStatus.STARTING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops an available standalone instance: an optional snapshot first, then the proxy and
|
||||
* the container go away while the record, its endpoint and its storage volume stay, so
|
||||
* StartDBInstance brings the same database back. The response reports "stopping" and the
|
||||
* stored instance settles to "stopped", the two statuses the user guide describes.
|
||||
*/
|
||||
public DbInstance stopDbInstance(String id, String snapshotId) {
|
||||
return stopDbInstance(id, snapshotId, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public synchronized DbInstance stopDbInstance(String id, String snapshotId, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
DbInstance instance = getDbInstance(id, effectiveRegion);
|
||||
refuseClusterMember(instance, id, "StopDBCluster");
|
||||
if (instance.getReadReplicaSourceDbInstanceIdentifier() != null
|
||||
|| !instance.getReadReplicaDbInstanceIdentifiers().isEmpty()) {
|
||||
throw new AwsException("InvalidDBInstanceState",
|
||||
"DB instance " + id + " has a read replica or is a read replica and cannot be stopped.", 400);
|
||||
}
|
||||
if (instance.getStatus() != DbInstanceStatus.AVAILABLE) {
|
||||
throw new AwsException("InvalidDBInstanceState",
|
||||
"DB instance " + id + " is not in available state.", 400);
|
||||
}
|
||||
if (snapshotId != null && !snapshotId.isBlank()) {
|
||||
createDbSnapshot(snapshotId, id, null, effectiveRegion);
|
||||
}
|
||||
|
||||
instance.setStatus(DbInstanceStatus.STOPPING);
|
||||
putInstanceForScope(accountId, effectiveRegion, id, instance);
|
||||
DbInstance response = RESPONSE_COPIER.convertValue(instance, DbInstance.class);
|
||||
|
||||
if (!config.services().rds().mock()) {
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id));
|
||||
if (instance.getContainerId() != null) {
|
||||
try {
|
||||
containerManager.stop(buildHandle(instance));
|
||||
} catch (RuntimeException | Error e) {
|
||||
instance.setStatus(DbInstanceStatus.FAILED);
|
||||
putInstanceForScope(accountId, effectiveRegion, id, instance);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
// The volume stays; only the container is gone until StartDBInstance.
|
||||
instance.setContainerId(null);
|
||||
instance.setContainerHost(null);
|
||||
instance.setContainerPort(0);
|
||||
}
|
||||
instance.setStatus(DbInstanceStatus.STOPPED);
|
||||
putInstanceForScope(accountId, effectiveRegion, id, instance);
|
||||
LOG.infov("DB instance {0} stopped", id);
|
||||
return response;
|
||||
}
|
||||
|
||||
/** Starts a stopped instance on the volume it kept; the response reports "starting". */
|
||||
public DbInstance startDbInstance(String id) {
|
||||
return startDbInstance(id, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public synchronized DbInstance startDbInstance(String id, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
DbInstance instance = getDbInstance(id, effectiveRegion);
|
||||
// A stopped cluster's members are stopped with it and only StartDBCluster brings them
|
||||
// back: started alone, a member would get a standalone container on its own volume
|
||||
// while the cluster stayed stopped.
|
||||
refuseClusterMember(instance, id, "StartDBCluster");
|
||||
if (instance.getStatus() != DbInstanceStatus.STOPPED) {
|
||||
throw new AwsException("InvalidDBInstanceState",
|
||||
"DB instance " + id + " is not in stopped state.", 400);
|
||||
}
|
||||
instance.setStatus(DbInstanceStatus.STARTING);
|
||||
putInstanceForScope(accountId, effectiveRegion, id, instance);
|
||||
DbInstance response = RESPONSE_COPIER.convertValue(instance, DbInstance.class);
|
||||
|
||||
if (!config.services().rds().mock()) {
|
||||
startStandaloneInstanceBackend(instance, id, effectiveRegion);
|
||||
}
|
||||
instance.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putInstanceForScope(accountId, effectiveRegion, id, instance);
|
||||
LOG.infov("DB instance {0} started", id);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Brings a standalone instance's container up on its existing volume and its proxy with
|
||||
* it, the way a reboot does after stopping them.
|
||||
*/
|
||||
private void startStandaloneInstanceBackend(DbInstance instance, String id, String effectiveRegion) {
|
||||
String image = imageForEngine(instance.getEngine(), instance.getEngineVersion());
|
||||
String storageResourceId = resolvedInstanceStorageResourceId(instance);
|
||||
String dockerVolumeName = resolvedInstanceDockerVolumeName(instance);
|
||||
RdsContainerHandle handle;
|
||||
try {
|
||||
handle = containerManager.tryStart(
|
||||
instance.getDbInstanceArn(), id, storageResourceId,
|
||||
dockerVolumeName, instance.getEngine(), image, instance.getMasterUsername(),
|
||||
instance.getMasterPassword(), instance.getDbName());
|
||||
} catch (RuntimeException | Error e) {
|
||||
instance.setStatus(DbInstanceStatus.FAILED);
|
||||
putInstanceForScope(currentAccountId(), effectiveRegion, id, instance);
|
||||
throw e;
|
||||
}
|
||||
instance.setContainerStorageResourceId(storageResourceId);
|
||||
instance.setDockerVolumeName(dockerVolumeName);
|
||||
instance.setContainerId(handle != null ? handle.getContainerId() : null);
|
||||
instance.setContainerHost(handle != null ? handle.getHost() : null);
|
||||
instance.setContainerPort(handle != null ? handle.getPort() : 0);
|
||||
if (hasBackend(instance.getContainerHost(), instance.getContainerPort())) {
|
||||
String effectiveMasterUser = instance.getMasterUsername() != null
|
||||
? instance.getMasterUsername() : "root";
|
||||
final String accountId = accountIdFromArn(instance.getDbInstanceArn());
|
||||
final String instanceRegion = regionFromArn(instance.getDbInstanceArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
instance.getEngine(),
|
||||
instance.isIamDatabaseAuthenticationEnabled(),
|
||||
instance.getProxyPort(), instance.getContainerHost(), instance.getContainerPort(),
|
||||
instance.getEndpoint().address(),
|
||||
effectiveMasterUser, instance.getMasterPassword(), instance.getDbName(),
|
||||
(user, pw) -> validateDbPasswordForScope(accountId, instanceRegion, id, user, pw));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops an available cluster and its members: the cluster's proxy and container go away,
|
||||
* every member's proxy with them, and all of them report "stopped" until StartDBCluster.
|
||||
*/
|
||||
public synchronized DbCluster stopDbCluster(String id, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
DbCluster cluster = getDbCluster(id, effectiveRegion);
|
||||
if (cluster.getStatus() != null && cluster.getStatus() != DbInstanceStatus.AVAILABLE) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + id + " is not in available state.", 400);
|
||||
}
|
||||
cluster.setStatus(DbInstanceStatus.STOPPING);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
DbCluster response = RESPONSE_COPIER.convertValue(cluster, DbCluster.class);
|
||||
|
||||
boolean mock = config.services().rds().mock();
|
||||
for (String memberId : cluster.getDbClusterMembers()) {
|
||||
DbInstance member = findInstanceForScope(accountId, effectiveRegion, memberId);
|
||||
if (member == null) {
|
||||
continue;
|
||||
}
|
||||
if (!mock) {
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(member.getDbInstanceArn(), memberId));
|
||||
}
|
||||
member.setStatus(DbInstanceStatus.STOPPED);
|
||||
member.setContainerHost(null);
|
||||
member.setContainerPort(0);
|
||||
putInstanceForScope(accountId, effectiveRegion, memberId, member);
|
||||
}
|
||||
if (!mock) {
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id));
|
||||
if (cluster.getContainerId() != null) {
|
||||
try {
|
||||
containerManager.stop(buildClusterHandle(cluster));
|
||||
} catch (RuntimeException | Error e) {
|
||||
cluster.setStatus(DbInstanceStatus.FAILED);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
cluster.setContainerId(null);
|
||||
cluster.setContainerHost(null);
|
||||
cluster.setContainerPort(0);
|
||||
}
|
||||
cluster.setStatus(DbInstanceStatus.STOPPED);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
LOG.infov("DB cluster {0} stopped", id);
|
||||
return response;
|
||||
}
|
||||
|
||||
/** Starts a stopped cluster and its members on the cluster's kept volume. */
|
||||
public synchronized DbCluster startDbCluster(String id, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
DbCluster cluster = getDbCluster(id, effectiveRegion);
|
||||
if (cluster.getStatus() != DbInstanceStatus.STOPPED) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + id + " is not in stopped state.", 400);
|
||||
}
|
||||
cluster.setStatus(DbInstanceStatus.STARTING);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
DbCluster response = RESPONSE_COPIER.convertValue(cluster, DbCluster.class);
|
||||
|
||||
if (!config.services().rds().mock()) {
|
||||
startClusterBackend(cluster, id, effectiveRegion);
|
||||
}
|
||||
cluster.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
for (String memberId : cluster.getDbClusterMembers()) {
|
||||
DbInstance member = findInstanceForScope(accountId, effectiveRegion, memberId);
|
||||
if (member == null) {
|
||||
continue;
|
||||
}
|
||||
member.setContainerId(cluster.getContainerId());
|
||||
member.setContainerHost(cluster.getContainerHost());
|
||||
member.setContainerPort(cluster.getContainerPort());
|
||||
if (!config.services().rds().mock() && hasBackend(cluster.getContainerHost(), cluster.getContainerPort())) {
|
||||
final String memberRegion = regionFromArn(member.getDbInstanceArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(member.getDbInstanceArn(), memberId),
|
||||
member.getEngine(), member.isIamDatabaseAuthenticationEnabled(), member.getProxyPort(),
|
||||
cluster.getContainerHost(), cluster.getContainerPort(), member.getEndpoint().address(),
|
||||
member.getMasterUsername() != null ? member.getMasterUsername() : "root",
|
||||
member.getMasterPassword(), member.getDbName(),
|
||||
(user, pw) -> validateDbPasswordForScope(accountId, memberRegion, memberId, user, pw));
|
||||
}
|
||||
member.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putInstanceForScope(accountId, effectiveRegion, memberId, member);
|
||||
}
|
||||
LOG.infov("DB cluster {0} started", id);
|
||||
return response;
|
||||
}
|
||||
|
||||
/** Reboots a cluster: its container and proxies go down and come back on the same volume. */
|
||||
public synchronized DbCluster rebootDbCluster(String id, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
DbCluster cluster = getDbCluster(id, effectiveRegion);
|
||||
if (cluster.getStatus() != null && cluster.getStatus() != DbInstanceStatus.AVAILABLE) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + id + " is not in available state.", 400);
|
||||
}
|
||||
cluster.setStatus(DbInstanceStatus.REBOOTING);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
DbCluster response = RESPONSE_COPIER.convertValue(cluster, DbCluster.class);
|
||||
stopDbClusterRuntime(cluster, id, effectiveRegion, accountId);
|
||||
if (!config.services().rds().mock()) {
|
||||
startClusterBackend(cluster, id, effectiveRegion);
|
||||
}
|
||||
cluster.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
for (String memberId : cluster.getDbClusterMembers()) {
|
||||
DbInstance member = findInstanceForScope(accountId, effectiveRegion, memberId);
|
||||
if (member == null) {
|
||||
continue;
|
||||
}
|
||||
member.setContainerId(cluster.getContainerId());
|
||||
member.setContainerHost(cluster.getContainerHost());
|
||||
member.setContainerPort(cluster.getContainerPort());
|
||||
if (!config.services().rds().mock() && hasBackend(cluster.getContainerHost(), cluster.getContainerPort())) {
|
||||
final String memberRegion = regionFromArn(member.getDbInstanceArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(member.getDbInstanceArn(), memberId),
|
||||
member.getEngine(), member.isIamDatabaseAuthenticationEnabled(), member.getProxyPort(),
|
||||
cluster.getContainerHost(), cluster.getContainerPort(), member.getEndpoint().address(),
|
||||
member.getMasterUsername() != null ? member.getMasterUsername() : "root",
|
||||
member.getMasterPassword(), member.getDbName(),
|
||||
(user, pw) -> validateDbPasswordForScope(accountId, memberRegion, memberId, user, pw));
|
||||
}
|
||||
member.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putInstanceForScope(accountId, effectiveRegion, memberId, member);
|
||||
}
|
||||
LOG.infov("DB cluster {0} rebooted", id);
|
||||
return response;
|
||||
}
|
||||
|
||||
private void stopDbClusterRuntime(DbCluster cluster, String id, String effectiveRegion, String accountId) {
|
||||
if (config.services().rds().mock()) {
|
||||
return;
|
||||
}
|
||||
for (String memberId : cluster.getDbClusterMembers()) {
|
||||
DbInstance member = findInstanceForScope(accountId, effectiveRegion, memberId);
|
||||
if (member != null) {
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(member.getDbInstanceArn(), memberId));
|
||||
}
|
||||
}
|
||||
proxyManager.stopProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id));
|
||||
if (cluster.getContainerId() != null) {
|
||||
try {
|
||||
containerManager.stop(buildClusterHandle(cluster));
|
||||
} catch (RuntimeException | Error e) {
|
||||
cluster.setStatus(DbInstanceStatus.FAILED);
|
||||
putClusterForScope(accountId, effectiveRegion, id, cluster);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
cluster.setContainerId(null);
|
||||
cluster.setContainerHost(null);
|
||||
cluster.setContainerPort(0);
|
||||
}
|
||||
|
||||
/** Brings a cluster's container up on its existing volume and its proxy with it. */
|
||||
private void startClusterBackend(DbCluster cluster, String id, String effectiveRegion) {
|
||||
String image = imageForEngine(cluster.getEngine(), cluster.getEngineVersion());
|
||||
String storageResourceId = resolvedClusterStorageResourceId(cluster);
|
||||
String dockerVolumeName = resolvedClusterDockerVolumeName(cluster);
|
||||
RdsContainerHandle handle;
|
||||
try {
|
||||
handle = containerManager.tryStart(
|
||||
cluster.getDbClusterArn(), id, storageResourceId, dockerVolumeName,
|
||||
cluster.getEngine(), image, cluster.getMasterUsername(), cluster.getMasterPassword(),
|
||||
cluster.getDatabaseName());
|
||||
} catch (RuntimeException | Error e) {
|
||||
cluster.setStatus(DbInstanceStatus.FAILED);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
throw e;
|
||||
}
|
||||
cluster.setContainerStorageResourceId(storageResourceId);
|
||||
cluster.setDockerVolumeName(dockerVolumeName);
|
||||
cluster.setContainerId(handle != null ? handle.getContainerId() : null);
|
||||
cluster.setContainerHost(handle != null ? handle.getHost() : null);
|
||||
cluster.setContainerPort(handle != null ? handle.getPort() : 0);
|
||||
if (handle != null) {
|
||||
final String accountId = accountIdFromArn(cluster.getDbClusterArn());
|
||||
final String clusterRegion = regionFromArn(cluster.getDbClusterArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id),
|
||||
cluster.getEngine(), cluster.isIamDatabaseAuthenticationEnabled(), cluster.getProxyPort(),
|
||||
handle.getHost(), handle.getPort(), cluster.getEndpoint().address(),
|
||||
cluster.getMasterUsername() != null ? cluster.getMasterUsername() : "root",
|
||||
cluster.getMasterPassword(), cluster.getDatabaseName(),
|
||||
(user, pw) -> validateDbClusterPasswordForScope(accountId, clusterRegion, id, user, pw));
|
||||
}
|
||||
}
|
||||
|
||||
public DbInstance rebootDbInstance(String id) {
|
||||
return rebootDbInstance(id, regionResolver.getDefaultRegion());
|
||||
}
|
||||
@@ -6014,6 +6360,20 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (cluster.getStatus() == DbInstanceStatus.DELETING) {
|
||||
continue;
|
||||
}
|
||||
if (cluster.getStatus() == DbInstanceStatus.STOPPED) {
|
||||
// A stopped cluster stays stopped across an emulator restart; StartDBCluster
|
||||
// brings its container back. Its endpoint keeps its port when that port is free.
|
||||
int port = reserveOrAllocateProxyPort(cluster.getProxyPort());
|
||||
if (port != cluster.getProxyPort()) {
|
||||
cluster.setProxyPort(port);
|
||||
DbEndpoint endpoint = proxyEndpoint(port);
|
||||
cluster.setEndpoint(endpoint);
|
||||
cluster.setReaderEndpoint(endpoint);
|
||||
putClusterForScope(accountIdFromArn(cluster.getDbClusterArn()),
|
||||
regionFromArn(cluster.getDbClusterArn()), cluster.getDbClusterIdentifier(), cluster);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
String accountId = accountIdFromArn(cluster.getDbClusterArn());
|
||||
String clusterRegion = regionFromArn(cluster.getDbClusterArn());
|
||||
String storageResourceId = resolvedClusterStorageResourceId(cluster);
|
||||
@@ -6109,6 +6469,17 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (instance.getStatus() == DbInstanceStatus.DELETING) {
|
||||
continue;
|
||||
}
|
||||
if (instance.getStatus() == DbInstanceStatus.STOPPED) {
|
||||
// Stays stopped across a restart; StartDBInstance brings the container back.
|
||||
int port = reserveOrAllocateProxyPort(instance.getProxyPort());
|
||||
if (port != instance.getProxyPort()) {
|
||||
instance.setProxyPort(port);
|
||||
instance.setEndpoint(proxyEndpoint(port));
|
||||
putInstanceForScope(accountIdFromArn(instance.getDbInstanceArn()),
|
||||
regionFromArn(instance.getDbInstanceArn()), instance.getDbInstanceIdentifier(), instance);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
String accountId = accountIdFromArn(instance.getDbInstanceArn());
|
||||
String instanceRegion = regionFromArn(instance.getDbInstanceArn());
|
||||
String clusterId = instance.getDbClusterIdentifier();
|
||||
|
||||
@@ -4,5 +4,5 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public enum DbInstanceStatus {
|
||||
CREATING, AVAILABLE, DELETING, REBOOTING, MODIFYING, FAILED
|
||||
CREATING, AVAILABLE, DELETING, REBOOTING, MODIFYING, FAILED, STOPPING, STOPPED, STARTING
|
||||
}
|
||||
@@ -1913,6 +1913,44 @@ class RdsQueryHandlerTest {
|
||||
verify(service).modifyDbSnapshot("mysnap", "14", "new-options", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopStartAndRebootActions_dispatchAndRenderTransitionalStatuses() {
|
||||
DbInstance stopping = makeInstance("standalone");
|
||||
stopping.setStatus(DbInstanceStatus.STOPPING);
|
||||
when(service.stopDbInstance(eq("standalone"), eq("before-stop"), isNull())).thenReturn(stopping);
|
||||
MultivaluedMap<String, String> stop = params();
|
||||
stop.add("DBInstanceIdentifier", "standalone");
|
||||
stop.add("DBSnapshotIdentifier", "before-stop");
|
||||
String stopBody = (String) handler.handle("StopDBInstance", stop).getEntity();
|
||||
assertTrue(stopBody.contains("<StopDBInstanceResult>"));
|
||||
assertTrue(stopBody.contains("<DBInstanceStatus>stopping</DBInstanceStatus>"));
|
||||
|
||||
DbInstance starting = makeInstance("standalone");
|
||||
starting.setStatus(DbInstanceStatus.STARTING);
|
||||
when(service.startDbInstance(eq("standalone"), isNull())).thenReturn(starting);
|
||||
MultivaluedMap<String, String> start = params();
|
||||
start.add("DBInstanceIdentifier", "standalone");
|
||||
String startBody = (String) handler.handle("StartDBInstance", start).getEntity();
|
||||
assertTrue(startBody.contains("<StartDBInstanceResult>"));
|
||||
assertTrue(startBody.contains("<DBInstanceStatus>starting</DBInstanceStatus>"));
|
||||
|
||||
DbCluster cluster = new DbCluster();
|
||||
cluster.setDbClusterIdentifier("aurora");
|
||||
cluster.setStatus(DbInstanceStatus.STOPPED);
|
||||
when(service.stopDbCluster(eq("aurora"), isNull())).thenReturn(cluster);
|
||||
MultivaluedMap<String, String> stopCluster = params();
|
||||
stopCluster.add("DBClusterIdentifier", "aurora");
|
||||
String clusterBody = (String) handler.handle("StopDBCluster", stopCluster).getEntity();
|
||||
assertTrue(clusterBody.contains("<StopDBClusterResult>"));
|
||||
assertTrue(clusterBody.contains("<Status>stopped</Status>"));
|
||||
|
||||
assertEquals(400, handler.handle("StopDBInstance", params()).getStatus());
|
||||
assertEquals(400, handler.handle("StartDBInstance", params()).getStatus());
|
||||
assertEquals(400, handler.handle("StartDBCluster", params()).getStatus());
|
||||
assertEquals(400, handler.handle("RebootDBCluster", params()).getStatus());
|
||||
verify(service, never()).startDbCluster(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeDbSnapshotAttributes_returnsRestoreAttribute() {
|
||||
io.github.hectorvent.floci.services.rds.model.DbSnapshot snapshot = new io.github.hectorvent.floci.services.rds.model.DbSnapshot();
|
||||
|
||||
@@ -2029,6 +2029,142 @@ class RdsServiceTest {
|
||||
assertNotEquals(a.getEndpoint().port(), b.getEndpoint().port());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopAndStartDbInstanceReleaseAndRestoreTheContainerOnTheSameVolume() {
|
||||
DbInstance created = rdsService.createDbInstance("standalone", "postgres", "16",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
String volume = created.getDockerVolumeName();
|
||||
int port = created.getEndpoint().port();
|
||||
|
||||
DbInstance stopping = rdsService.stopDbInstance("standalone", null);
|
||||
assertEquals(DbInstanceStatus.STOPPING, stopping.getStatus());
|
||||
DbInstance stopped = rdsService.getDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.STOPPED, stopped.getStatus());
|
||||
assertNull(stopped.getContainerId());
|
||||
assertEquals(volume, stopped.getDockerVolumeName());
|
||||
assertEquals(port, stopped.getEndpoint().port());
|
||||
verify(containerManager).stop(any());
|
||||
verify(proxyManager).stopProxy(any());
|
||||
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbInstance("standalone", null)).getErrorCode());
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.modifyDbInstance("standalone", "new-password", null, null, null, null, null)).getErrorCode());
|
||||
|
||||
DbInstance starting = rdsService.startDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.STARTING, starting.getStatus());
|
||||
DbInstance started = rdsService.getDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, started.getStatus());
|
||||
assertEquals("cont-id", started.getContainerId());
|
||||
assertEquals(port, started.getEndpoint().port());
|
||||
verify(containerManager, times(2)).tryStart(any(), any(), any(), eq(volume), any(), any(), any(), any(), any());
|
||||
verify(proxyManager, times(2)).startProxy(any(), any(), anyBoolean(), eq(port), any(), anyInt(),
|
||||
any(), any(), any(), any(), any());
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.startDbInstance("standalone")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopDbInstanceTakesTheRequestedSnapshotFirst() {
|
||||
rdsService.createDbInstance("standalone", "postgres", "16",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("DUMP");
|
||||
|
||||
rdsService.stopDbInstance("standalone", "before-stop");
|
||||
|
||||
assertEquals("available", rdsService.describeDbSnapshots("before-stop", null).iterator().next().getStatus());
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("standalone").getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopDbInstanceRefusesClusterMembersAndReplicas() {
|
||||
rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
rdsService.createDbInstance("aurora-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r6g.large",
|
||||
20, false, null, null, "aurora-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
AwsException member = assertThrows(AwsException.class, () -> rdsService.stopDbInstance("aurora-member", null));
|
||||
assertEquals("InvalidDBClusterStateFault", member.getErrorCode());
|
||||
assertTrue(member.getMessage().contains("StopDBCluster"));
|
||||
// A stopped cluster's member cannot be started on its own either.
|
||||
rdsService.stopDbCluster("aurora-cluster", "us-east-1");
|
||||
AwsException startMember = assertThrows(AwsException.class, () -> rdsService.startDbInstance("aurora-member"));
|
||||
assertEquals("InvalidDBClusterStateFault", startMember.getErrorCode());
|
||||
assertTrue(startMember.getMessage().contains("StartDBCluster"));
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
rdsService.startDbCluster("aurora-cluster", "us-east-1");
|
||||
|
||||
when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenAnswer(invocation -> new RdsContainerHandle(
|
||||
"cont-" + invocation.getArgument(1), invocation.getArgument(1), "localhost", 5432));
|
||||
createPostgresSource("primary");
|
||||
when(containerManager.createPostgresSnapshot("cont-primary", "admin")).thenReturn("DUMP");
|
||||
rdsService.createDbInstanceReadReplica(replicaRequest("primary-replica", "primary"), null);
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbInstance("primary", null)).getErrorCode());
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbInstance("primary-replica", null)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopStartAndRebootDbClusterCarryTheMembersAlong() {
|
||||
DbCluster created = rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
rdsService.createDbInstance("aurora-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r6g.large",
|
||||
20, false, null, null, "aurora-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
int port = created.getEndpoint().port();
|
||||
|
||||
DbCluster stopping = rdsService.stopDbCluster("aurora-cluster", "us-east-1");
|
||||
assertEquals(DbInstanceStatus.STOPPING, stopping.getStatus());
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbCluster("aurora-cluster").getStatus());
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
assertNull(rdsService.getDbCluster("aurora-cluster").getContainerId());
|
||||
assertEquals("InvalidDBClusterStateFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbCluster("aurora-cluster", "us-east-1")).getErrorCode());
|
||||
assertEquals("InvalidDBClusterStateFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.rebootDbCluster("aurora-cluster", "us-east-1")).getErrorCode());
|
||||
|
||||
DbCluster starting = rdsService.startDbCluster("aurora-cluster", "us-east-1");
|
||||
assertEquals(DbInstanceStatus.STARTING, starting.getStatus());
|
||||
DbCluster started = rdsService.getDbCluster("aurora-cluster");
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, started.getStatus());
|
||||
assertEquals(port, started.getEndpoint().port());
|
||||
assertEquals("cont-id", started.getContainerId());
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
assertEquals("InvalidDBClusterStateFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.startDbCluster("aurora-cluster", "us-east-1")).getErrorCode());
|
||||
|
||||
DbCluster rebooting = rdsService.rebootDbCluster("aurora-cluster", "us-east-1");
|
||||
assertEquals(DbInstanceStatus.REBOOTING, rebooting.getStatus());
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbCluster("aurora-cluster").getStatus());
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
// create, start and reboot each brought the cluster container up once.
|
||||
verify(containerManager, times(3)).tryStart(eq(created.getDbClusterArn()), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
assertEquals("DBClusterNotFoundFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbCluster("absent", "us-east-1")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockModeStopAndStartTouchNoContainerOrProxy() {
|
||||
when(config.services().rds().mock()).thenReturn(true);
|
||||
rdsService.createDbInstance("standalone", "postgres", "16",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
|
||||
rdsService.stopDbInstance("standalone", null);
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("standalone").getStatus());
|
||||
rdsService.startDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbInstance("standalone").getStatus());
|
||||
verify(containerManager, never()).stop(any());
|
||||
verify(containerManager, never()).tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
verify(proxyManager, never()).stopProxy(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockModeRebootSkipsContainerAndProxy() {
|
||||
when(config.services().rds().mock()).thenReturn(true);
|
||||
|
||||
+261
@@ -0,0 +1,261 @@
|
||||
package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import com.github.dockerjava.core.command.ExecStartResultCallback;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstance;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.restassured.http.ContentType.URLENC;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
* Stop and start against real Postgres containers: a stopped instance has no container and
|
||||
* its endpoint refuses connections, a started one has the same endpoint and the row written
|
||||
* before the stop, and a cluster carries its member along. Statuses follow the user guide:
|
||||
* "stopping" in the stop response, "stopped" afterwards, "starting" then "available".
|
||||
*/
|
||||
@QuarkusTest
|
||||
@Tag("docker")
|
||||
class RdsStopStartIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260921/us-east-1/rds/aws4_request, "
|
||||
+ "SignedHeaders=content-type;host, Signature=test";
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
RdsService rdsService;
|
||||
|
||||
@BeforeEach
|
||||
void checkDocker() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
} catch (Exception e) {
|
||||
assumeTrue(false, "Docker is not available: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static RequestSpecification query(String action) {
|
||||
return given().header("Authorization", AUTH)
|
||||
.contentType(URLENC)
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2014-10-31");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppedInstanceKeepsItsDataAndEndpointForTheStart() throws Exception {
|
||||
String id = "stop-db-" + Long.toString(System.nanoTime(), 36);
|
||||
query("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.formParam("Engine", "postgres")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("DBName", "appdb")
|
||||
.formParam("DBInstanceClass", "db.t3.micro")
|
||||
.formParam("AllocatedStorage", "5")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForInstance(id, "available");
|
||||
DbInstance running = rdsService.getDbInstance(id);
|
||||
String containerId = running.getContainerId();
|
||||
int port = running.getEndpoint().port();
|
||||
psql(containerId, "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('before-stop');");
|
||||
assertTrue(portOpen(port), "the endpoint must accept connections while available");
|
||||
|
||||
query("StopDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StopDBInstanceResult>"))
|
||||
.body(containsString("<DBInstanceStatus>stopping</DBInstanceStatus>"));
|
||||
waitForInstance(id, "stopped");
|
||||
DbInstance stopped = rdsService.getDbInstance(id);
|
||||
assertNull(stopped.getContainerId(), "a stopped instance has no container");
|
||||
assertFalse(containerExists(containerId), "the container is gone while stopped");
|
||||
assertFalse(portOpen(port), "the endpoint refuses connections while stopped");
|
||||
|
||||
query("ModifyDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.formParam("MasterUserPassword", "changed")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidDBInstanceState</Code>"));
|
||||
query("StopDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidDBInstanceState</Code>"));
|
||||
|
||||
query("StartDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StartDBInstanceResult>"))
|
||||
.body(containsString("<DBInstanceStatus>starting</DBInstanceStatus>"));
|
||||
waitForInstance(id, "available");
|
||||
DbInstance started = rdsService.getDbInstance(id);
|
||||
assertTrue(started.getEndpoint().port() == port, "the endpoint keeps its port");
|
||||
assertTrue(portOpen(port), "the endpoint accepts connections again");
|
||||
assertTrue(psql(started.getContainerId(), "SELECT v FROM marker;").contains("before-stop"),
|
||||
"the row written before the stop must survive on the volume");
|
||||
|
||||
query("DeleteDBInstance").formParam("DBInstanceIdentifier", id).formParam("SkipFinalSnapshot", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppedClusterCarriesItsMemberAndRebootKeepsTheData() throws Exception {
|
||||
String cluster = "stop-cluster-" + Long.toString(System.nanoTime(), 36);
|
||||
String member = cluster + "-1";
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("EngineVersion", "16.3")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("DatabaseName", "appdb")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForCluster(cluster, "available");
|
||||
query("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", member)
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("DBInstanceClass", "db.r6g.large")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForInstance(member, "available");
|
||||
DbCluster running = rdsService.getDbCluster(cluster);
|
||||
psql(running.getContainerId(), "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('cluster-row');");
|
||||
|
||||
query("StopDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StopDBClusterResult>"))
|
||||
.body(containsString("<Status>stopping</Status>"));
|
||||
waitForCluster(cluster, "stopped");
|
||||
waitForInstance(member, "stopped");
|
||||
assertNull(rdsService.getDbCluster(cluster).getContainerId());
|
||||
|
||||
query("StartDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<Status>starting</Status>"));
|
||||
waitForCluster(cluster, "available");
|
||||
waitForInstance(member, "available");
|
||||
assertTrue(psql(rdsService.getDbCluster(cluster).getContainerId(), "SELECT v FROM marker;").contains("cluster-row"));
|
||||
|
||||
query("RebootDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<RebootDBClusterResult>"))
|
||||
.body(containsString("<Status>rebooting</Status>"));
|
||||
waitForCluster(cluster, "available");
|
||||
assertTrue(psql(rdsService.getDbCluster(cluster).getContainerId(), "SELECT v FROM marker;").contains("cluster-row"));
|
||||
|
||||
query("DeleteDBInstance").formParam("DBInstanceIdentifier", member).formParam("SkipFinalSnapshot", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
query("DeleteDBCluster").formParam("DBClusterIdentifier", cluster).formParam("SkipFinalSnapshot", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static boolean portOpen(int port) {
|
||||
try (Socket socket = new Socket("localhost", port)) {
|
||||
return socket.isConnected();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean containerExists(String containerId) {
|
||||
try {
|
||||
dockerClient.inspectContainerCmd(containerId).exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void waitForInstance(String id, String expected) throws Exception {
|
||||
for (int i = 0; i < 60; i++) {
|
||||
String status = query("DescribeDBInstances").formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().extract().xmlPath().getString(
|
||||
"DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.DBInstanceStatus");
|
||||
if (expected.equals(status)) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
throw new RuntimeException("DB instance " + id + " did not reach " + expected + ".");
|
||||
}
|
||||
|
||||
private void waitForCluster(String id, String expected) throws Exception {
|
||||
for (int i = 0; i < 60; i++) {
|
||||
String status = query("DescribeDBClusters").formParam("DBClusterIdentifier", id)
|
||||
.when().post("/").then().extract().xmlPath().getString(
|
||||
"DescribeDBClustersResponse.DescribeDBClustersResult.DBClusters.DBCluster.Status");
|
||||
if (expected.equals(status)) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
throw new RuntimeException("DB cluster " + id + " did not reach " + expected + ".");
|
||||
}
|
||||
|
||||
private String psql(String containerId, String sql) throws Exception {
|
||||
String[] command = {"psql", "-U", "admin", "-d", "appdb", "-tAc", sql};
|
||||
String execId = dockerClient.execCreateCmd(containerId)
|
||||
.withCmd(command).withAttachStdout(true).withAttachStderr(true).exec().getId();
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errors = new ByteArrayOutputStream();
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
Closeable callback = dockerClient.execStartCmd(execId).exec(new ExecStartResultCallback() {
|
||||
@Override
|
||||
public void onNext(Frame frame) {
|
||||
try {
|
||||
if (frame.getStreamType() == StreamType.STDOUT) {
|
||||
output.write(frame.getPayload());
|
||||
} else if (frame.getStreamType() == StreamType.STDERR) {
|
||||
errors.write(frame.getPayload());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to capture psql output", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
assertTrue(latch.await(30, TimeUnit.SECONDS), "Timed out running psql");
|
||||
long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();
|
||||
assertTrue(exitCode == 0, "psql failed with exit code " + exitCode + ": " + errors.toString(StandardCharsets.UTF_8));
|
||||
return output.toString(StandardCharsets.UTF_8);
|
||||
} finally {
|
||||
callback.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user