mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(rdsdata): refresh backend endpoints before JDBC connect (#6148)
* refactor(rdsdata): carry backend runtime identity * fix(rdsdata): resolve runtime endpoint before JDBC connect * test(rdsdata): cover backend endpoint refresh * refactor(rds): make backend gates runtime-only * test(rds): enforce proxy runtime identity * test(rdsdata): reuse proxy endpoint refresh backends * docs(rds): fix backend gate Javadoc link
This commit is contained in:
1 parent
d2f2625005
commit
b58947c70a
17 files changed
+350
-146
No files matched your search
@@ -120,7 +120,7 @@ grep -q 'RdsAwsIntegrationTest' "$SHARD_FILE" && [ -n "$SQLSERVER_IMAGE" ] && pu
|
||||
# provisioner tests reach it through Redshift. It was pulled inline in all four shards.
|
||||
REDSHIFT_PG_IMAGE="$(grep -oE 'image-version: *postgres:[^ ]+' src/main/resources/application.yml | awk '{print $2}')"
|
||||
grep -qE '/redshift/|/cloudformation/' "$SHARD_FILE" && [ -n "$REDSHIFT_PG_IMAGE" ] && pull "$REDSHIFT_PG_IMAGE"
|
||||
grep -q '/rds/' "$SHARD_FILE" && pull postgres:16.3-alpine
|
||||
grep -qE '/rds/|/rdsdata/' "$SHARD_FILE" && pull postgres:16.3-alpine
|
||||
# The InfluxDB and k3s pins live in application.yml like the sidecars above.
|
||||
INFLUX_IMAGE="$(grep -oE 'default-image: *"influxdb:[^"]+"' src/main/resources/application.yml | grep -oE '"[^"]+"' | tr -d '"')"
|
||||
grep -q '/timestreaminfluxdb/' "$SHARD_FILE" && [ -n "$INFLUX_IMAGE" ] && pull "$INFLUX_IMAGE"
|
||||
|
||||
@@ -3847,6 +3847,11 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/** Returns the runtime that owns an instance's backing container. Cluster members share their cluster runtime. */
|
||||
public String instanceBackendRuntimeId(DbInstance instance) {
|
||||
return instanceBackendIdentity(instance).runtimeId();
|
||||
}
|
||||
|
||||
private InstanceBackendIdentity instanceBackendIdentity(DbInstance instance) {
|
||||
String clusterId = instance.getDbClusterIdentifier();
|
||||
if (clusterId != null && !clusterId.isBlank()) {
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package io.github.hectorvent.floci.services.rds.container;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
|
||||
/** Resolves the current reachable endpoint of the container owned by an RDS runtime. */
|
||||
@FunctionalInterface
|
||||
public interface RdsBackendEndpointResolver {
|
||||
|
||||
EndpointInfo resolveBackendEndpoint(String runtimeId, DatabaseEngine engine);
|
||||
}
|
||||
+7
-16
@@ -8,32 +8,23 @@ package io.github.hectorvent.floci.services.rds.container;
|
||||
public interface RdsBackendGate {
|
||||
|
||||
/** A gate for backends that never pause. */
|
||||
RdsBackendGate OPEN = (host, port) -> Lease.NONE;
|
||||
RdsBackendGate OPEN = runtimeId -> Lease.NONE;
|
||||
|
||||
/**
|
||||
* Waits until the container serving {@code host:port} can take a client, resuming it when it
|
||||
* Waits until the container owned by {@code runtimeId} can take a client, resuming it when it
|
||||
* is auto-paused, and keeps it awake until the lease is closed. A backend with no auto-pause
|
||||
* gets {@link Lease#NONE}.
|
||||
*
|
||||
* @throws InterruptedException when interrupted while a pause or resume is under way
|
||||
*/
|
||||
Lease enter(String host, int port) throws InterruptedException;
|
||||
Lease enterRuntime(String runtimeId) throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Like {@link #enter(String, int)}, but with {@code waitForResume=false} starts resume in the
|
||||
* background and throws {@link ResumingException} if the backend is not yet ready.
|
||||
* Backends without auto-pause may use this default implementation.
|
||||
* Like {@link #enterRuntime(String)}, but with {@code waitForResume=false} starts resume in the
|
||||
* background and throws {@link ResumingException} if the runtime is not yet ready.
|
||||
*/
|
||||
default Lease enter(String host, int port, boolean waitForResume) throws InterruptedException {
|
||||
return enter(host, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enters the container owned by {@code runtimeId}. Backends that do not track runtime
|
||||
* identities may use this default implementation.
|
||||
*/
|
||||
default Lease enterRuntime(String runtimeId) throws InterruptedException {
|
||||
return Lease.NONE;
|
||||
default Lease enterRuntime(String runtimeId, boolean waitForResume) throws InterruptedException {
|
||||
return enterRuntime(runtimeId);
|
||||
}
|
||||
|
||||
/** The backend is being resumed; no lease was acquired and the caller must retry. */
|
||||
|
||||
+10
-20
@@ -51,11 +51,11 @@ import java.util.regex.Pattern;
|
||||
/**
|
||||
* Manages backend Docker container lifecycle for RDS DB instances and clusters.
|
||||
* Starts postgres/mysql/mariadb containers and resolves the backend host:port for the auth proxy.
|
||||
* Every client of a container enters it through {@link #enter}, which is how an Aurora Serverless
|
||||
* Every client of a container enters it through {@link #enterRuntime(String)}, which is how an Aurora Serverless
|
||||
* v2 cluster with MinCapacity 0 knows when it is idle enough to pause and when to resume.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointResolver, Resettable {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(RdsContainerManager.class);
|
||||
private static final Pattern SAFE_STORAGE_COMPONENT = Pattern.compile("[A-Za-z0-9._-]+");
|
||||
@@ -516,26 +516,16 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
|
||||
@Override
|
||||
public Lease enterRuntime(String runtimeId) throws InterruptedException {
|
||||
AutoPauseController controller = runtimeId != null ? autoPauses.get(runtimeId) : null;
|
||||
return controller != null ? controller.acquire() : Lease.NONE;
|
||||
return enterRuntime(runtimeId, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lease enter(String host, int port) throws InterruptedException {
|
||||
return enter(host, port, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lease enter(String host, int port, boolean waitForResume) throws InterruptedException {
|
||||
if (host == null) {
|
||||
return Lease.NONE;
|
||||
public Lease enterRuntime(String runtimeId, boolean waitForResume) throws InterruptedException {
|
||||
if (runtimeId == null || runtimeId.isBlank()) {
|
||||
throw new IllegalArgumentException("RDS runtime ID is required to enter its backend");
|
||||
}
|
||||
for (AutoPauseController controller : autoPauses.values()) {
|
||||
if (controller.serves(host, port)) {
|
||||
return controller.acquire(waitForResume);
|
||||
}
|
||||
}
|
||||
return Lease.NONE;
|
||||
AutoPauseController controller = autoPauses.get(runtimeId);
|
||||
return controller != null ? controller.acquire(waitForResume) : Lease.NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -571,8 +561,8 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
AutoPauseController controller = new AutoPauseController(
|
||||
handle.getContainerId(), handle.getHost(), handle.getPort(), dockerFreezer,
|
||||
this::scheduleAutoPauseTask, autoPauseEvents, Clock.systemUTC(), resumeDelay);
|
||||
// Clients find a container by its address, so the new container owns it from now on,
|
||||
// even if Docker removed the previous owner without Floci stopping it.
|
||||
// Drop any controller still tied to this endpoint: Docker may have removed its container
|
||||
// without Floci observing the stop before assigning the endpoint to this runtime.
|
||||
for (Map.Entry<String, AutoPauseController> entry : List.copyOf(autoPauses.entrySet())) {
|
||||
if (entry.getValue().serves(handle.getHost(), handle.getPort())
|
||||
&& autoPauses.remove(entry.getKey(), entry.getValue())) {
|
||||
|
||||
@@ -51,32 +51,35 @@ public class RdsAuthProxy {
|
||||
Duration acceptRetirementTimeout = Duration.ofSeconds(5);
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
|
||||
MasterPasswordCheck passwordValidator,
|
||||
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
|
||||
int maxConnections) {
|
||||
this(instanceId, backendHost, backendPort, engine, iamEnabled, masterUsername, masterPassword,
|
||||
dbName, sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis,
|
||||
backendConnectTimeoutMillis, maxConnections, null, username -> false,
|
||||
RdsBackendGate.OPEN);
|
||||
this(instanceId, backendHost, backendPort, backendRuntimeId, engine, iamEnabled,
|
||||
masterUsername, masterPassword, dbName, sigV4, tlsCertificates, passwordValidator,
|
||||
handshakeTimeoutMillis, backendConnectTimeoutMillis, maxConnections, null,
|
||||
username -> false, RdsBackendGate.OPEN);
|
||||
}
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
|
||||
MasterPasswordCheck passwordValidator,
|
||||
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
|
||||
int maxConnections, RdsProxyBinding binding, RdsBackendGate backendGate) {
|
||||
this(instanceId, backendHost, backendPort, engine, iamEnabled, masterUsername, masterPassword,
|
||||
dbName, sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis,
|
||||
backendConnectTimeoutMillis, maxConnections, binding,
|
||||
this(instanceId, backendHost, backendPort, backendRuntimeId, engine, iamEnabled,
|
||||
masterUsername, masterPassword, dbName, sigV4, tlsCertificates, passwordValidator,
|
||||
handshakeTimeoutMillis, backendConnectTimeoutMillis, maxConnections, binding,
|
||||
username -> binding != null, backendGate);
|
||||
}
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
|
||||
@@ -85,13 +88,14 @@ public class RdsAuthProxy {
|
||||
int maxConnections, RdsProxyBinding binding,
|
||||
MySqlProtocolHandler.IamUserChecker iamUserChecker,
|
||||
RdsBackendGate backendGate) {
|
||||
this(instanceId, backendHost, backendPort, engine, iamEnabled, masterUsername, masterPassword,
|
||||
dbName, sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis,
|
||||
backendConnectTimeoutMillis, maxConnections, binding, iamUserChecker, backendGate,
|
||||
EndpointKind.DATABASE);
|
||||
this(instanceId, backendHost, backendPort, backendRuntimeId, engine, iamEnabled,
|
||||
masterUsername, masterPassword, dbName, sigV4, tlsCertificates, passwordValidator,
|
||||
handshakeTimeoutMillis, backendConnectTimeoutMillis, maxConnections, binding,
|
||||
iamUserChecker, backendGate, EndpointKind.DATABASE);
|
||||
}
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
|
||||
@@ -101,24 +105,7 @@ public class RdsAuthProxy {
|
||||
MySqlProtocolHandler.IamUserChecker iamUserChecker,
|
||||
RdsBackendGate backendGate, EndpointKind endpointKind) {
|
||||
this(instanceId, backendHost, backendPort,
|
||||
() -> new BackendEndpoint(backendHost, backendPort),
|
||||
engine, iamEnabled, masterUsername, masterPassword, dbName, sigV4,
|
||||
tlsCertificates, passwordValidator, handshakeTimeoutMillis,
|
||||
backendConnectTimeoutMillis, maxConnections, binding, iamUserChecker,
|
||||
backendGate, endpointKind);
|
||||
}
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
BackendEndpointResolver backendEndpointResolver,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
|
||||
MasterPasswordCheck passwordValidator,
|
||||
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
|
||||
int maxConnections, RdsProxyBinding binding,
|
||||
MySqlProtocolHandler.IamUserChecker iamUserChecker,
|
||||
RdsBackendGate backendGate, EndpointKind endpointKind) {
|
||||
this(instanceId, backendHost, backendPort, backendEndpointResolver, null,
|
||||
() -> new BackendEndpoint(backendHost, backendPort), backendRuntimeId,
|
||||
engine, iamEnabled, masterUsername, masterPassword, dbName, sigV4,
|
||||
tlsCertificates, passwordValidator, handshakeTimeoutMillis,
|
||||
backendConnectTimeoutMillis, maxConnections, binding, iamUserChecker,
|
||||
@@ -135,6 +122,9 @@ public class RdsAuthProxy {
|
||||
int maxConnections, RdsProxyBinding binding,
|
||||
MySqlProtocolHandler.IamUserChecker iamUserChecker,
|
||||
RdsBackendGate backendGate, EndpointKind endpointKind) {
|
||||
if (backendRuntimeId == null || backendRuntimeId.isBlank()) {
|
||||
throw new IllegalArgumentException("RDS backend runtime ID is required");
|
||||
}
|
||||
this.instanceId = instanceId;
|
||||
this.backendHost = backendHost;
|
||||
this.backendPort = backendPort;
|
||||
@@ -241,9 +231,7 @@ public class RdsAuthProxy {
|
||||
try {
|
||||
// An auto-paused Aurora backend resumes before the client is served: the client is
|
||||
// held, not refused, and the backend stays awake for as long as the connection lasts.
|
||||
lease = backendRuntimeId != null
|
||||
? backendGate.enterRuntime(backendRuntimeId)
|
||||
: backendGate.enter(backendHost, backendPort);
|
||||
lease = backendGate.enterRuntime(backendRuntimeId);
|
||||
client.setTcpNoDelay(true);
|
||||
|
||||
// RDS only proxy-validates the master user; a non-master user passes through so the
|
||||
|
||||
+3
-1
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.rdsdata;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
@@ -18,10 +19,11 @@ class RdsDataConnectionFactory {
|
||||
}
|
||||
|
||||
Connection open(RdsDataResourceResolver.DatabaseTarget target,
|
||||
EndpointInfo endpoint,
|
||||
String username,
|
||||
String password,
|
||||
String database) throws SQLException {
|
||||
String url = buildUrl(target.engine(), target.host(), target.port(), database);
|
||||
String url = buildUrl(target.engine(), endpoint.host(), endpoint.port(), database);
|
||||
Properties props = new Properties();
|
||||
props.setProperty("user", username);
|
||||
props.setProperty("password", password);
|
||||
|
||||
+9
-9
@@ -78,25 +78,25 @@ class RdsDataResourceResolver {
|
||||
&& !rdsService.hasMissingClusterMemberBackends(cluster, region)
|
||||
? cluster
|
||||
: rdsService.ensureClusterBackend(cluster.getDbClusterIdentifier(), region);
|
||||
return target(resolved.getDbClusterArn(), resolved.getEngine(), resolved.getContainerHost(),
|
||||
resolved.getContainerPort(), resolved.getMasterUsername(), resolved.getMasterPassword(),
|
||||
resolved.getDatabaseName());
|
||||
return target(resolved.getDbClusterArn(), resolved.getDbClusterArn(), resolved.getEngine(),
|
||||
resolved.getContainerHost(), resolved.getContainerPort(), resolved.getMasterUsername(),
|
||||
resolved.getMasterPassword(), resolved.getDatabaseName());
|
||||
}
|
||||
|
||||
private DatabaseTarget fromInstance(DbInstance instance, String region) {
|
||||
DbInstance resolved = hasRuntime(instance.getContainerHost(), instance.getContainerPort())
|
||||
? instance
|
||||
: rdsService.ensureInstanceBackend(instance.getDbInstanceIdentifier(), region);
|
||||
return target(resolved.getDbInstanceArn(), resolved.getEngine(), resolved.getContainerHost(),
|
||||
resolved.getContainerPort(), resolved.getMasterUsername(), resolved.getMasterPassword(),
|
||||
resolved.getDbName());
|
||||
return target(resolved.getDbInstanceArn(), rdsService.instanceBackendRuntimeId(resolved),
|
||||
resolved.getEngine(), resolved.getContainerHost(), resolved.getContainerPort(),
|
||||
resolved.getMasterUsername(), resolved.getMasterPassword(), resolved.getDbName());
|
||||
}
|
||||
|
||||
private static boolean hasRuntime(String host, int port) {
|
||||
return host != null && !host.isBlank() && port > 0;
|
||||
}
|
||||
|
||||
private DatabaseTarget target(String arn, DatabaseEngine engine, String host, int port,
|
||||
private DatabaseTarget target(String arn, String runtimeId, DatabaseEngine engine, String host, int port,
|
||||
String username, String password, String databaseName) {
|
||||
if (!hasRuntime(host, port)) {
|
||||
// The Data API is RDS's data plane: it needs a real database, which Floci can only
|
||||
@@ -111,10 +111,10 @@ class RdsDataResourceResolver {
|
||||
throw new AwsException("BadRequestException",
|
||||
"RDS resource runtime is not available for Data API execution.", 400);
|
||||
}
|
||||
return new DatabaseTarget(arn, engine, host, port, username, password, databaseName);
|
||||
return new DatabaseTarget(arn, runtimeId, engine, username, password, databaseName);
|
||||
}
|
||||
|
||||
record DatabaseTarget(String arn, DatabaseEngine engine, String host, int port,
|
||||
record DatabaseTarget(String arn, String runtimeId, DatabaseEngine engine,
|
||||
String username, String password, String databaseName) {
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.DatabaseSecretCredentials;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendEndpointResolver;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendGate;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
@@ -73,6 +75,7 @@ public class RdsDataService implements Resettable {
|
||||
private final ConcurrentMap<String, TransactionContext> transactions = new ConcurrentHashMap<>();
|
||||
private final ScheduledExecutorService transactionCleanupExecutor;
|
||||
private final RdsBackendGate backendGate;
|
||||
private final RdsBackendEndpointResolver backendEndpointResolver;
|
||||
private final boolean databaseResumingExceptionEnabled;
|
||||
|
||||
@Inject
|
||||
@@ -81,19 +84,11 @@ public class RdsDataService implements Resettable {
|
||||
ObjectMapper objectMapper,
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
EmulatorConfig config,
|
||||
RdsBackendGate backendGate) {
|
||||
RdsBackendGate backendGate,
|
||||
RdsBackendEndpointResolver backendEndpointResolver) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory,
|
||||
Duration.ofSeconds(config.services().rdsData().transactionTtlSeconds()), backendGate,
|
||||
config.services().rdsData().databaseResumingExceptionEnabled());
|
||||
}
|
||||
|
||||
RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
SecretsManagerService secretsManagerService,
|
||||
ObjectMapper objectMapper,
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
Duration transactionTtl) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory, transactionTtl,
|
||||
RdsBackendGate.OPEN);
|
||||
backendEndpointResolver, config.services().rdsData().databaseResumingExceptionEnabled());
|
||||
}
|
||||
|
||||
RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
@@ -101,8 +96,9 @@ public class RdsDataService implements Resettable {
|
||||
ObjectMapper objectMapper,
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
Duration transactionTtl,
|
||||
RdsBackendGate backendGate) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory, transactionTtl, backendGate, false);
|
||||
RdsBackendEndpointResolver backendEndpointResolver) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory, transactionTtl,
|
||||
RdsBackendGate.OPEN, backendEndpointResolver);
|
||||
}
|
||||
|
||||
RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
@@ -111,6 +107,18 @@ public class RdsDataService implements Resettable {
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
Duration transactionTtl,
|
||||
RdsBackendGate backendGate,
|
||||
RdsBackendEndpointResolver backendEndpointResolver) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory, transactionTtl,
|
||||
backendGate, backendEndpointResolver, false);
|
||||
}
|
||||
|
||||
RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
SecretsManagerService secretsManagerService,
|
||||
ObjectMapper objectMapper,
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
Duration transactionTtl,
|
||||
RdsBackendGate backendGate,
|
||||
RdsBackendEndpointResolver backendEndpointResolver,
|
||||
boolean databaseResumingExceptionEnabled) {
|
||||
this.resourceResolver = resourceResolver;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
@@ -118,6 +126,7 @@ public class RdsDataService implements Resettable {
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.transactionTtl = transactionTtl;
|
||||
this.backendGate = backendGate;
|
||||
this.backendEndpointResolver = backendEndpointResolver;
|
||||
this.databaseResumingExceptionEnabled = databaseResumingExceptionEnabled;
|
||||
this.transactionCleanupExecutor = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
Thread thread = new Thread(r, "rds-data-transaction-cleanup");
|
||||
@@ -205,7 +214,9 @@ public class RdsDataService implements Resettable {
|
||||
boolean leaseHandedOver = false;
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = connectionFactory.open(target, credentials.username(), credentials.password(), database);
|
||||
EndpointInfo endpoint = resolveBackendEndpoint(target);
|
||||
connection = connectionFactory.open(
|
||||
target, endpoint, credentials.username(), credentials.password(), database);
|
||||
connection.setAutoCommit(false);
|
||||
String transactionId = UUID.randomUUID().toString();
|
||||
transactions.put(transactionId, new TransactionContext(
|
||||
@@ -305,9 +316,22 @@ public class RdsDataService implements Resettable {
|
||||
resourceResolver.resolve(resourceArn, region);
|
||||
DatabaseSecretCredentials credentials = credentials(request, target, region);
|
||||
String database = databaseName(request, target);
|
||||
try (RdsBackendGate.Lease _ = enterBackend(target);
|
||||
Connection connection = connectionFactory.open(target, credentials.username(), credentials.password(), database)) {
|
||||
return work.execute(connection, target.engine());
|
||||
try (RdsBackendGate.Lease _ = enterBackend(target)) {
|
||||
EndpointInfo endpoint = resolveBackendEndpoint(target);
|
||||
try (Connection connection = connectionFactory.open(
|
||||
target, endpoint, credentials.username(), credentials.password(), database)) {
|
||||
return work.execute(connection, target.engine());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private EndpointInfo resolveBackendEndpoint(RdsDataResourceResolver.DatabaseTarget target) {
|
||||
try {
|
||||
return backendEndpointResolver.resolveBackendEndpoint(target.runtimeId(), target.engine());
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Could not resolve backend endpoint for RDS runtime {0}", target.runtimeId());
|
||||
throw new AwsException("InternalServerErrorException",
|
||||
"Could not resolve backend endpoint for RDS runtime " + target.runtimeId(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,7 +341,7 @@ public class RdsDataService implements Resettable {
|
||||
*/
|
||||
private RdsBackendGate.Lease enterBackend(RdsDataResourceResolver.DatabaseTarget target) {
|
||||
try {
|
||||
return backendGate.enter(target.host(), target.port(), !databaseResumingExceptionEnabled);
|
||||
return backendGate.enterRuntime(target.runtimeId(), !databaseResumingExceptionEnabled);
|
||||
} catch (RdsBackendGate.ResumingException e) {
|
||||
throw new AwsException("DatabaseResumingException",
|
||||
"The Aurora Serverless v2 DB instance is resuming. Wait a few seconds and try again.", 400);
|
||||
|
||||
+18
-6
@@ -1331,13 +1331,14 @@ class RdsContainerManagerTest {
|
||||
DatabaseEngine.MYSQL, "mysql:8.0", "root", "password", "db");
|
||||
manager.configureAutoPause(runtimeId, 300, alwaysMayPause());
|
||||
|
||||
try (RdsBackendGate.Lease client = manager.enter("db1", 3306)) {
|
||||
try (RdsBackendGate.Lease client = manager.enterRuntime(runtimeId)) {
|
||||
assertFalse(manager.pauseIfIdle(runtimeId), "a client is connected");
|
||||
}
|
||||
assertTrue(manager.pauseIfIdle(runtimeId));
|
||||
verify(pause).exec();
|
||||
|
||||
assertThrows(RdsBackendGate.ResumingException.class, () -> manager.enter("db1", 3306, false));
|
||||
assertThrows(RdsBackendGate.ResumingException.class,
|
||||
() -> manager.enterRuntime(runtimeId, false));
|
||||
manager.enterRuntime(runtimeId).close();
|
||||
verify(unpause).exec();
|
||||
|
||||
@@ -1346,7 +1347,6 @@ class RdsContainerManagerTest {
|
||||
InOrder order = inOrder(unpause, lifecycleManager);
|
||||
order.verify(unpause, times(2)).exec();
|
||||
order.verify(lifecycleManager).stopAndRemoveStrict(eq("cluster-container"), any());
|
||||
assertSame(RdsBackendGate.Lease.NONE, manager.enter("db1", 3306));
|
||||
assertSame(RdsBackendGate.Lease.NONE, manager.enterRuntime(runtimeId));
|
||||
}
|
||||
|
||||
@@ -1371,7 +1371,7 @@ class RdsContainerManagerTest {
|
||||
manager.clear();
|
||||
|
||||
verify(unpause).exec();
|
||||
assertSame(RdsBackendGate.Lease.NONE, manager.enter("db1", 3306));
|
||||
assertSame(RdsBackendGate.Lease.NONE, manager.enterRuntime(runtimeId));
|
||||
assertFalse(manager.pauseIfIdle(runtimeId));
|
||||
}
|
||||
|
||||
@@ -1395,14 +1395,26 @@ class RdsContainerManagerTest {
|
||||
manager.configureAutoPause(removedRuntime, 300, alwaysMayPause());
|
||||
assertTrue(manager.pauseIfIdle(removedRuntime));
|
||||
|
||||
manager.start("arn:aws:rds:us-east-1:000000000000:cluster:new", "new", null,
|
||||
String newRuntime = "arn:aws:rds:us-east-1:000000000000:cluster:new";
|
||||
manager.start(newRuntime, "new", null,
|
||||
DatabaseEngine.MYSQL, "mysql:8.0", "root", "password", "db");
|
||||
manager.enter("db1", 3306).close();
|
||||
manager.enterRuntime(newRuntime).close();
|
||||
|
||||
verify(dockerClient, never()).unpauseContainerCmd("new-container");
|
||||
assertSame(RdsBackendGate.Lease.NONE, manager.enterRuntime(removedRuntime));
|
||||
assertFalse(manager.pauseIfIdle(removedRuntime), "the removed container's auto-pause is gone");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingRuntimeIdentityAtTheGate() {
|
||||
RdsContainerManager manager = autoPauseManager(
|
||||
config(tempDir.resolve("missing-runtime-root")), mock(ContainerLifecycleManager.class));
|
||||
|
||||
for (String runtimeId : new String[] {null, "", " "}) {
|
||||
assertThrows(IllegalArgumentException.class, () -> manager.enterRuntime(runtimeId));
|
||||
}
|
||||
}
|
||||
|
||||
private RdsContainerManager autoPauseManager(EmulatorConfig config, ContainerLifecycleManager lifecycleManager) {
|
||||
ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
|
||||
lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");
|
||||
|
||||
+1
-1
@@ -168,7 +168,7 @@ class RdsAuthProxyBackendResolutionTest {
|
||||
RdsAuthProxy.BackendEndpointResolver endpointResolver,
|
||||
DatabaseEngine engine) {
|
||||
RdsAuthProxy proxy = new RdsAuthProxy(
|
||||
id, "127.0.0.1", initialBackendPort, endpointResolver,
|
||||
id, "127.0.0.1", initialBackendPort, endpointResolver, "runtime:" + id,
|
||||
engine, false, "sa", "Secret123", null,
|
||||
mock(RdsSigV4Validator.class), mock(RdsProxyTlsCertificates.class),
|
||||
(user, password) -> true, 5000, 5000, 100, null,
|
||||
|
||||
+21
-3
@@ -18,6 +18,7 @@ import java.nio.file.Path;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -103,18 +104,28 @@ class RdsAuthProxyBoundsTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingBackendRuntimeIdentity() {
|
||||
for (String runtimeId : new String[] {null, "", " "}) {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> newProxy(runtimeId, 5000, 5000, 100));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void holdsTheClientUntilTheBackendGateAdmitsItAndReleasesTheLeaseWhenItLeaves() throws Exception {
|
||||
fakeBackend = new ServerSocket(0);
|
||||
fakeBackend.setSoTimeout(300);
|
||||
CountDownLatch admit = new CountDownLatch(1);
|
||||
CountDownLatch released = new CountDownLatch(1);
|
||||
RdsBackendGate gate = (host, port) -> {
|
||||
AtomicReference<String> enteredRuntime = new AtomicReference<>();
|
||||
RdsBackendGate gate = runtimeId -> {
|
||||
enteredRuntime.set(runtimeId);
|
||||
admit.await();
|
||||
return released::countDown;
|
||||
};
|
||||
int proxyPort = FreePorts.anyInterfacePort();
|
||||
proxy = new RdsAuthProxy("db-1", "localhost", fakeBackend.getLocalPort(),
|
||||
proxy = new RdsAuthProxy("db-1", "localhost", fakeBackend.getLocalPort(), "runtime:db-1",
|
||||
DatabaseEngine.SQLSERVER, false, "sa", "Secret123", null,
|
||||
mock(RdsSigV4Validator.class), realTls(), (user, pw) -> true,
|
||||
5000, 5000, 100, null, gate);
|
||||
@@ -131,12 +142,19 @@ class RdsAuthProxyBoundsTest {
|
||||
assertEquals('x', client.getInputStream().read(), "the held client is served once admitted");
|
||||
}
|
||||
}
|
||||
assertEquals("runtime:db-1", enteredRuntime.get());
|
||||
assertTrue(released.await(5, TimeUnit.SECONDS), "the lease is released when the connection ends");
|
||||
}
|
||||
|
||||
private RdsAuthProxy newProxy(int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
|
||||
int maxConnections) {
|
||||
return new RdsAuthProxy("db-1", "localhost", fakeBackend.getLocalPort(),
|
||||
return newProxy("runtime:db-1", handshakeTimeoutMillis, backendConnectTimeoutMillis, maxConnections);
|
||||
}
|
||||
|
||||
private RdsAuthProxy newProxy(String runtimeId, int handshakeTimeoutMillis,
|
||||
int backendConnectTimeoutMillis, int maxConnections) {
|
||||
int backendPort = fakeBackend != null ? fakeBackend.getLocalPort() : 1;
|
||||
return new RdsAuthProxy("db-1", "localhost", backendPort, runtimeId,
|
||||
DatabaseEngine.POSTGRES, false,
|
||||
"admin", "Secret123", "dev",
|
||||
mock(RdsSigV4Validator.class), realTls(), (user, pw) -> true,
|
||||
|
||||
+2
-1
@@ -402,7 +402,8 @@ class RdsProxyManagerTest {
|
||||
}
|
||||
|
||||
private static RdsAuthProxy authProxy(String key) {
|
||||
return new RdsAuthProxy(key, "localhost", 1, DatabaseEngine.POSTGRES, false,
|
||||
return new RdsAuthProxy(key, "localhost", 1, "runtime:" + key,
|
||||
DatabaseEngine.POSTGRES, false,
|
||||
"admin", "secret", "app", mock(RdsSigV4Validator.class),
|
||||
mock(RdsProxyTlsCertificates.class), (user, password) -> true,
|
||||
5000, 5000, 100);
|
||||
|
||||
+79
-12
@@ -1,5 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
package io.github.hectorvent.floci.services.rdsdata;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.CreateContainerResponse;
|
||||
import com.github.dockerjava.api.command.CreateNetworkResponse;
|
||||
@@ -16,8 +18,14 @@ import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerExec;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendEndpointResolver;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendGate;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsAuthProxy;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyTlsCertificates;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsSigV4Validator;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import io.github.hectorvent.floci.testutil.FreePorts;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
@@ -41,11 +49,13 @@ import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
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.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -53,15 +63,24 @@ import static org.mockito.Mockito.when;
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class RdsBackendEndpointRefreshIntegrationTest {
|
||||
|
||||
private static final String IMAGE = "postgres:16-alpine";
|
||||
private static final String IMAGE = "postgres:16.3-alpine";
|
||||
private static final String USER = "admin";
|
||||
private static final String PASSWORD = "password123";
|
||||
private static final String DATABASE = "appdb";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String FIRST_RESOURCE_ARN =
|
||||
"arn:aws:rds:us-east-1:000000000000:cluster:data-refresh-first";
|
||||
private static final String SECOND_RESOURCE_ARN =
|
||||
"arn:aws:rds:us-east-1:000000000000:cluster:data-refresh-second";
|
||||
private static final String SECRET_ARN =
|
||||
"arn:aws:secretsmanager:us-east-1:000000000000:secret:data-refresh";
|
||||
private static final int POSTGRES_PORT = 5432;
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final List<RdsAuthProxy> proxies = new ArrayList<>();
|
||||
private DockerClient dockerClient;
|
||||
private ContainerLifecycleManager lifecycleManager;
|
||||
private RdsDataService dataService;
|
||||
private String networkId;
|
||||
private String networkName;
|
||||
private String firstContainerId;
|
||||
@@ -120,10 +139,15 @@ class RdsBackendEndpointRefreshIntegrationTest {
|
||||
assumeTrue(canConnectEventually(firstAddress, POSTGRES_PORT)
|
||||
&& canConnectEventually(secondAddress, POSTGRES_PORT),
|
||||
"Docker bridge addresses are not reachable from this test JVM");
|
||||
dataService = createDataService();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
if (dataService != null) {
|
||||
dataService.shutdown();
|
||||
dataService = null;
|
||||
}
|
||||
for (RdsAuthProxy proxy : proxies) {
|
||||
proxy.stop();
|
||||
}
|
||||
@@ -143,15 +167,18 @@ class RdsBackendEndpointRefreshIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void proxyPortsKeepTheirInstancesAfterContainerAddressesSwap() throws Exception {
|
||||
void proxyAndDataApiKeepRuntimeIdentityAfterContainerAddressesSwap() throws Exception {
|
||||
int[] proxyPorts = FreePorts.anyInterfacePorts(2);
|
||||
RdsAuthProxy firstProxy = proxy("first", firstContainerId, firstAddress);
|
||||
RdsAuthProxy secondProxy = proxy("second", secondContainerId, secondAddress);
|
||||
firstProxy.start(proxyPorts[0]);
|
||||
secondProxy.start(proxyPorts[1]);
|
||||
String firstSystemIdentifier = systemIdentifier(proxyPorts[0]);
|
||||
String secondSystemIdentifier = systemIdentifier(proxyPorts[1]);
|
||||
String firstSystemIdentifier = proxySystemIdentifier(proxyPorts[0]);
|
||||
String secondSystemIdentifier = proxySystemIdentifier(proxyPorts[1]);
|
||||
assertNotEquals(firstSystemIdentifier, secondSystemIdentifier);
|
||||
assertAll("both data planes initially reach their assigned runtime",
|
||||
() -> assertEquals(firstSystemIdentifier, dataApiSystemIdentifier(FIRST_RESOURCE_ARN)),
|
||||
() -> assertEquals(secondSystemIdentifier, dataApiSystemIdentifier(SECOND_RESOURCE_ARN)));
|
||||
|
||||
stopAndDisconnect(firstContainerId);
|
||||
stopAndDisconnect(secondContainerId);
|
||||
@@ -164,16 +191,20 @@ class RdsBackendEndpointRefreshIntegrationTest {
|
||||
assertEquals(secondAddress, containerAddress(firstContainerId));
|
||||
assertEquals(firstAddress, containerAddress(secondContainerId));
|
||||
|
||||
assertEquals(firstSystemIdentifier, systemIdentifier(proxyPorts[0]));
|
||||
assertEquals(secondSystemIdentifier, systemIdentifier(proxyPorts[1]));
|
||||
assertAll("both data planes follow runtime identity after the address swap",
|
||||
() -> assertEquals(firstSystemIdentifier, proxySystemIdentifier(proxyPorts[0])),
|
||||
() -> assertEquals(firstSystemIdentifier, dataApiSystemIdentifier(FIRST_RESOURCE_ARN)),
|
||||
() -> assertEquals(secondSystemIdentifier, proxySystemIdentifier(proxyPorts[1])),
|
||||
() -> assertEquals(secondSystemIdentifier, dataApiSystemIdentifier(SECOND_RESOURCE_ARN)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void proxyUsesRestartedContainerWhenItsPreviousAddressIsUnused() throws Exception {
|
||||
void proxyAndDataApiUseRestartedRuntimeWhenItsPreviousAddressIsUnused() throws Exception {
|
||||
int proxyPort = FreePorts.anyInterfacePort();
|
||||
RdsAuthProxy proxy = proxy("first", firstContainerId, firstAddress);
|
||||
proxy.start(proxyPort);
|
||||
String systemIdentifier = systemIdentifier(proxyPort);
|
||||
String systemIdentifier = proxySystemIdentifier(proxyPort);
|
||||
assertEquals(systemIdentifier, dataApiSystemIdentifier(FIRST_RESOURCE_ARN));
|
||||
|
||||
stopAndDisconnect(firstContainerId);
|
||||
stopAndDisconnect(secondContainerId);
|
||||
@@ -184,7 +215,9 @@ class RdsBackendEndpointRefreshIntegrationTest {
|
||||
assertFalse(canConnect(firstAddress, POSTGRES_PORT),
|
||||
"the stale backend address must be unused in this scenario");
|
||||
|
||||
assertEquals(systemIdentifier, systemIdentifier(proxyPort));
|
||||
assertAll("both data planes follow the runtime when its old address is unused",
|
||||
() -> assertEquals(systemIdentifier, proxySystemIdentifier(proxyPort)),
|
||||
() -> assertEquals(systemIdentifier, dataApiSystemIdentifier(FIRST_RESOURCE_ARN)));
|
||||
}
|
||||
|
||||
private RdsAuthProxy proxy(String id, String containerId, String initialAddress) {
|
||||
@@ -194,7 +227,8 @@ class RdsBackendEndpointRefreshIntegrationTest {
|
||||
return new RdsAuthProxy.BackendEndpoint(endpoint.host(), endpoint.port());
|
||||
};
|
||||
RdsAuthProxy proxy = new RdsAuthProxy(
|
||||
id, initialAddress, POSTGRES_PORT, resolver, DatabaseEngine.POSTGRES, false,
|
||||
id, initialAddress, POSTGRES_PORT, resolver, containerId,
|
||||
DatabaseEngine.POSTGRES, false,
|
||||
USER, PASSWORD, DATABASE, mock(RdsSigV4Validator.class),
|
||||
mock(RdsProxyTlsCertificates.class), (user, password) -> true,
|
||||
5000, 5000, 100, null, username -> false,
|
||||
@@ -203,6 +237,39 @@ class RdsBackendEndpointRefreshIntegrationTest {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
private RdsDataService createDataService() {
|
||||
RdsDataResourceResolver resolver = mock(RdsDataResourceResolver.class);
|
||||
when(resolver.resolve(FIRST_RESOURCE_ARN, REGION))
|
||||
.thenReturn(target(FIRST_RESOURCE_ARN, firstContainerId));
|
||||
when(resolver.resolve(SECOND_RESOURCE_ARN, REGION))
|
||||
.thenReturn(target(SECOND_RESOURCE_ARN, secondContainerId));
|
||||
SecretsManagerService secrets = mock(SecretsManagerService.class);
|
||||
SecretVersion version = new SecretVersion();
|
||||
version.setSecretString("{\"username\":\"" + USER + "\",\"password\":\"" + PASSWORD + "\"}");
|
||||
when(secrets.getSecretValue(any(), any(), any(), any())).thenReturn(version);
|
||||
RdsBackendEndpointResolver endpointResolver = (runtimeId, engine) ->
|
||||
lifecycleManager.resolveEndpoint(runtimeId, POSTGRES_PORT, networkName);
|
||||
return new RdsDataService(resolver, secrets, objectMapper, new RdsDataConnectionFactory(),
|
||||
Duration.ofSeconds(60), RdsBackendGate.OPEN, endpointResolver);
|
||||
}
|
||||
|
||||
private static RdsDataResourceResolver.DatabaseTarget target(String resourceArn, String runtimeId) {
|
||||
return new RdsDataResourceResolver.DatabaseTarget(
|
||||
resourceArn, runtimeId, DatabaseEngine.POSTGRES, USER, PASSWORD, DATABASE);
|
||||
}
|
||||
|
||||
private String dataApiSystemIdentifier(String resourceArn) {
|
||||
ObjectNode request = objectMapper.createObjectNode();
|
||||
request.put("resourceArn", resourceArn);
|
||||
request.put("secretArn", SECRET_ARN);
|
||||
request.put("database", DATABASE);
|
||||
request.put("sql", "SELECT system_identifier::text FROM pg_control_system()");
|
||||
ObjectNode response = dataService.executeStatement(request, REGION);
|
||||
String value = response.path("records").path(0).path(0).path("stringValue").asText();
|
||||
assertFalse(value.isBlank(), "PostgreSQL must return its system identifier");
|
||||
return value;
|
||||
}
|
||||
|
||||
private String startPostgres(String suffix) {
|
||||
CreateContainerResponse container = dockerClient.createContainerCmd(IMAGE)
|
||||
.withName(networkName + "-" + suffix)
|
||||
@@ -239,7 +306,7 @@ class RdsBackendEndpointRefreshIntegrationTest {
|
||||
.getNetworkSettings().getNetworks().get(networkName).getIpAddress();
|
||||
}
|
||||
|
||||
private String systemIdentifier(int proxyPort) throws Exception {
|
||||
private String proxySystemIdentifier(int proxyPort) throws Exception {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", USER);
|
||||
+26
-11
@@ -60,8 +60,8 @@ class RdsDataResourceResolverTest {
|
||||
.resolve("arn:aws:rds:us-east-1:000000000000:cluster:cluster1");
|
||||
|
||||
assertEquals(DatabaseEngine.MYSQL, target.engine());
|
||||
assertEquals("127.0.0.1", target.host());
|
||||
assertEquals(3306, target.port());
|
||||
assertEquals(cluster.getDbClusterArn(), target.arn());
|
||||
assertEquals(cluster.getDbClusterArn(), target.runtimeId());
|
||||
assertEquals("app", target.databaseName());
|
||||
verify(rdsService, never()).ensureClusterBackend(anyString(), anyString());
|
||||
}
|
||||
@@ -103,12 +103,12 @@ class RdsDataResourceResolverTest {
|
||||
started.setContainerPort(5432);
|
||||
when(rdsService.getDbInstance("probe-db", "us-east-1")).thenReturn(daemonlessInstance());
|
||||
when(rdsService.ensureInstanceBackend("probe-db", "us-east-1")).thenReturn(started);
|
||||
when(rdsService.instanceBackendRuntimeId(started)).thenReturn(started.getDbInstanceArn());
|
||||
|
||||
RdsDataResourceResolver.DatabaseTarget target = new RdsDataResourceResolver(rdsService)
|
||||
.resolve("arn:aws:rds:us-east-1:000000000000:db:probe-db");
|
||||
|
||||
assertEquals("127.0.0.1", target.host());
|
||||
assertEquals(5432, target.port());
|
||||
assertEquals(started.getDbInstanceArn(), target.runtimeId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -130,8 +130,25 @@ class RdsDataResourceResolverTest {
|
||||
RdsDataResourceResolver.DatabaseTarget target = new RdsDataResourceResolver(rdsService)
|
||||
.resolve("arn:aws:rds:us-east-1:000000000000:cluster:cluster1");
|
||||
|
||||
assertEquals("127.0.0.1", target.host());
|
||||
assertEquals(5432, target.port());
|
||||
assertEquals(started.getDbClusterArn(), target.runtimeId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesClusterMemberToItsSharedClusterRuntime() {
|
||||
RdsService rdsService = mock(RdsService.class);
|
||||
DbInstance member = clusterMember("member1", 7002, DbInstanceStatus.AVAILABLE);
|
||||
member.setContainerHost("127.0.0.1");
|
||||
member.setContainerPort(5432);
|
||||
String clusterArn = "arn:aws:rds:us-west-2:000000000000:cluster:cluster1";
|
||||
when(rdsService.getDbInstance("member1", "us-west-2")).thenReturn(member);
|
||||
when(rdsService.instanceBackendRuntimeId(member)).thenReturn(clusterArn);
|
||||
|
||||
RdsDataResourceResolver.DatabaseTarget target = new RdsDataResourceResolver(rdsService)
|
||||
.resolve(member.getDbInstanceArn(), "us-west-2");
|
||||
|
||||
assertEquals(member.getDbInstanceArn(), target.arn());
|
||||
assertEquals(clusterArn, target.runtimeId());
|
||||
verify(rdsService, never()).ensureInstanceBackend(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -156,8 +173,7 @@ class RdsDataResourceResolverTest {
|
||||
|
||||
RdsDataResourceResolver.DatabaseTarget firstTarget = resolver.resolve(cluster.getDbClusterArn(), "us-west-2");
|
||||
|
||||
assertEquals("127.0.0.1", firstTarget.host());
|
||||
assertEquals(15432, firstTarget.port());
|
||||
assertEquals(cluster.getDbClusterArn(), firstTarget.runtimeId());
|
||||
assertEquals(DbInstanceStatus.FAILED, retryMember.getStatus());
|
||||
assertEquals("cluster-container", otherMember.getContainerId());
|
||||
|
||||
@@ -202,8 +218,7 @@ class RdsDataResourceResolverTest {
|
||||
Future<RdsDataResourceResolver.DatabaseTarget> resolved = executor.submit(() ->
|
||||
resolver.resolve(fixture.cluster().getDbClusterArn(), "us-west-2"));
|
||||
RdsDataResourceResolver.DatabaseTarget target = resolved.get(5, TimeUnit.SECONDS);
|
||||
assertEquals("127.0.0.1", target.host());
|
||||
assertEquals(15432, target.port());
|
||||
assertEquals(fixture.cluster().getDbClusterArn(), target.runtimeId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,7 +239,7 @@ class RdsDataResourceResolverTest {
|
||||
RdsDataResourceResolver.DatabaseTarget target = new RdsDataResourceResolver(fixture.service())
|
||||
.resolve(fixture.cluster().getDbClusterArn(), "us-west-2");
|
||||
|
||||
assertEquals(15432, target.port());
|
||||
assertEquals(fixture.cluster().getDbClusterArn(), target.runtimeId());
|
||||
assertEquals("127.0.0.1", member.getContainerHost());
|
||||
assertEquals(15432, member.getContainerPort());
|
||||
verify(fixture.service()).ensureClusterBackend("cluster1", "us-west-2");
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendEndpointResolver;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendGate;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
@@ -28,6 +30,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -947,7 +950,7 @@ class RdsDataServiceTest {
|
||||
|
||||
harness.service.executeStatement(harness.request("select 1"), REGION);
|
||||
|
||||
assertEquals(List.of("127.0.0.1:3306"), harness.gate.entered);
|
||||
assertEquals(List.of(RESOURCE_ARN), harness.gate.entered);
|
||||
assertEquals(0, harness.gate.held.get());
|
||||
}
|
||||
|
||||
@@ -1000,6 +1003,60 @@ class RdsDataServiceTest {
|
||||
assertEquals(0, harness.gate.held.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesTheCurrentEndpointForEveryNewPhysicalConnection() {
|
||||
TestHarness harness = new TestHarness();
|
||||
EndpointInfo first = new EndpointInfo("198.18.200.10", 3306);
|
||||
EndpointInfo second = new EndpointInfo("198.18.200.11", 3306);
|
||||
harness.currentEndpoint.set(first);
|
||||
|
||||
harness.service.executeStatement(harness.request("select 1"), REGION);
|
||||
harness.currentEndpoint.set(second);
|
||||
harness.service.executeStatement(harness.request("select 1"), REGION);
|
||||
|
||||
assertEquals(2, harness.endpointsResolved.get());
|
||||
assertEquals(List.of(first, second), harness.endpointsOpened);
|
||||
assertEquals(List.of(RESOURCE_ARN, RESOURCE_ARN), harness.gate.entered);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOpenTransactionKeepsItsConnectionWhileNewConnectionsRefresh() {
|
||||
TestHarness harness = new TestHarness();
|
||||
EndpointInfo first = new EndpointInfo("198.18.200.10", 3306);
|
||||
EndpointInfo second = new EndpointInfo("198.18.200.11", 3306);
|
||||
harness.currentEndpoint.set(first);
|
||||
String firstTransaction = harness.service.beginTransaction(harness.beginRequest(), REGION)
|
||||
.path("transactionId").asText();
|
||||
harness.currentEndpoint.set(second);
|
||||
|
||||
ObjectNode inTransaction = harness.request("select 1");
|
||||
inTransaction.put("transactionId", firstTransaction);
|
||||
harness.service.executeStatement(inTransaction, REGION);
|
||||
harness.service.executeStatement(harness.request("select 1"), REGION);
|
||||
String secondTransaction = harness.service.beginTransaction(harness.beginRequest(), REGION)
|
||||
.path("transactionId").asText();
|
||||
|
||||
assertEquals(3, harness.endpointsResolved.get());
|
||||
assertEquals(List.of(first, second, second), harness.endpointsOpened);
|
||||
harness.service.rollbackTransaction(harness.transactionRequest(firstTransaction), REGION);
|
||||
harness.service.rollbackTransaction(harness.transactionRequest(secondTransaction), REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointResolutionFailureFailsClosedAndReleasesTheLease() {
|
||||
TestHarness harness = new TestHarness();
|
||||
harness.endpointFailure = new IllegalStateException("Docker inspect failed");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> harness.service.executeStatement(harness.request("select 1"), REGION));
|
||||
|
||||
assertEquals("InternalServerErrorException", error.getErrorCode());
|
||||
assertEquals(500, error.getHttpStatus());
|
||||
assertEquals(1, harness.endpointsResolved.get());
|
||||
assertEquals(0, harness.connectionsOpened.get());
|
||||
assertEquals(0, harness.gate.held.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void closesConnectionWhenTransactionSetupFails() {
|
||||
RdsDataResourceResolver resolver = mock(RdsDataResourceResolver.class);
|
||||
@@ -1010,13 +1067,15 @@ class RdsDataServiceTest {
|
||||
RdsDataConnectionFactory failingFactory = new RdsDataConnectionFactory() {
|
||||
@Override
|
||||
Connection open(RdsDataResourceResolver.DatabaseTarget target,
|
||||
EndpointInfo endpoint,
|
||||
String username,
|
||||
String password,
|
||||
String database) {
|
||||
return throwingSetAutoCommitConnection(closed);
|
||||
}
|
||||
};
|
||||
RdsDataService service = new RdsDataService(resolver, secrets, objectMapper, failingFactory, Duration.ofSeconds(60));
|
||||
RdsDataService service = new RdsDataService(
|
||||
resolver, secrets, objectMapper, failingFactory, Duration.ofSeconds(60), endpointResolver());
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.beginTransaction(beginRequest(), REGION));
|
||||
|
||||
@@ -1107,8 +1166,12 @@ class RdsDataServiceTest {
|
||||
}
|
||||
|
||||
private static RdsDataResourceResolver.DatabaseTarget target(String resourceArn, DatabaseEngine engine) {
|
||||
return new RdsDataResourceResolver.DatabaseTarget(resourceArn, engine,
|
||||
"127.0.0.1", 3306, "sa", "", "app");
|
||||
return new RdsDataResourceResolver.DatabaseTarget(
|
||||
resourceArn, resourceArn, engine, "sa", "", "app");
|
||||
}
|
||||
|
||||
private static RdsBackendEndpointResolver endpointResolver() {
|
||||
return (runtimeId, engine) -> new EndpointInfo("127.0.0.1", engine.defaultPort());
|
||||
}
|
||||
|
||||
/** The H2 compatibility mode standing in for {@code engine}. */
|
||||
@@ -1160,6 +1223,7 @@ class RdsDataServiceTest {
|
||||
assertEquals("DatabaseResumingException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(0, harness.connectionsOpened.get());
|
||||
assertEquals(0, harness.endpointsResolved.get());
|
||||
assertEquals(0, harness.gate.held.get());
|
||||
|
||||
harness.gate.resuming = false;
|
||||
@@ -1206,19 +1270,19 @@ class RdsDataServiceTest {
|
||||
private boolean resuming;
|
||||
|
||||
@Override
|
||||
public Lease enter(String host, int port, boolean waitForResume) {
|
||||
if (!waitForResume && resuming) {
|
||||
throw new ResumingException();
|
||||
}
|
||||
return enter(host, port);
|
||||
public Lease enterRuntime(String runtimeId) {
|
||||
return enterRuntime(runtimeId, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lease enter(String host, int port) {
|
||||
public Lease enterRuntime(String runtimeId, boolean waitForResume) {
|
||||
if (!waitForResume && resuming) {
|
||||
throw new ResumingException();
|
||||
}
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
entered.add(host + ":" + port);
|
||||
entered.add(runtimeId);
|
||||
held.incrementAndGet();
|
||||
AtomicBoolean closed = new AtomicBoolean();
|
||||
return () -> {
|
||||
@@ -1235,6 +1299,10 @@ class RdsDataServiceTest {
|
||||
private final RdsDataResourceResolver.DatabaseTarget target;
|
||||
private final CountingGate gate = new CountingGate();
|
||||
private final AtomicInteger connectionsOpened = new AtomicInteger();
|
||||
private final AtomicInteger endpointsResolved = new AtomicInteger();
|
||||
private final AtomicReference<EndpointInfo> currentEndpoint = new AtomicReference<>();
|
||||
private final List<EndpointInfo> endpointsOpened = new CopyOnWriteArrayList<>();
|
||||
private volatile RuntimeException endpointFailure;
|
||||
private final RdsDataService service;
|
||||
|
||||
private TestHarness() {
|
||||
@@ -1276,18 +1344,28 @@ class RdsDataServiceTest {
|
||||
when(resolver.resolve(OTHER_RESOURCE_ARN, REGION)).thenReturn(target(OTHER_RESOURCE_ARN));
|
||||
when(resolver.resolve(UNKNOWN_RESOURCE_ARN, REGION))
|
||||
.thenThrow(new AwsException("BadRequestException", "resource is missing", 400));
|
||||
currentEndpoint.set(new EndpointInfo("127.0.0.1", engine.defaultPort()));
|
||||
RdsBackendEndpointResolver backendEndpointResolver = (runtimeId, databaseEngine) -> {
|
||||
endpointsResolved.incrementAndGet();
|
||||
if (endpointFailure != null) {
|
||||
throw endpointFailure;
|
||||
}
|
||||
return currentEndpoint.get();
|
||||
};
|
||||
RdsDataConnectionFactory connectionFactory = new RdsDataConnectionFactory() {
|
||||
@Override
|
||||
Connection open(RdsDataResourceResolver.DatabaseTarget target,
|
||||
EndpointInfo endpoint,
|
||||
String username,
|
||||
String password,
|
||||
String database) throws SQLException {
|
||||
endpointsOpened.add(endpoint);
|
||||
connectionsOpened.incrementAndGet();
|
||||
return getConnection();
|
||||
}
|
||||
};
|
||||
service = new RdsDataService(resolver, secrets, objectMapper, connectionFactory,
|
||||
transactionTtl, gate, resumingExceptionEnabled);
|
||||
transactionTtl, gate, backendEndpointResolver, resumingExceptionEnabled);
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
|
||||
+5
-3
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rdsdata;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
@@ -63,14 +64,15 @@ class RdsDataTransactionConcurrencyTest {
|
||||
private RdsDataService createService(Duration ttl) throws SQLException {
|
||||
RdsDataResourceResolver resolver = mock(RdsDataResourceResolver.class);
|
||||
when(resolver.resolve(RESOURCE_ARN, REGION)).thenReturn(new RdsDataResourceResolver.DatabaseTarget(
|
||||
RESOURCE_ARN, DatabaseEngine.POSTGRES, "127.0.0.1", 5432, "sa", "", "app"));
|
||||
RESOURCE_ARN, RESOURCE_ARN, DatabaseEngine.POSTGRES, "sa", "", "app"));
|
||||
SecretsManagerService secrets = mock(SecretsManagerService.class);
|
||||
SecretVersion version = new SecretVersion();
|
||||
version.setSecretString("{\"username\":\"sa\",\"password\":\"\"}");
|
||||
when(secrets.getSecretValue(any(), any(), any(), any())).thenReturn(version);
|
||||
RdsDataConnectionFactory factory = mock(RdsDataConnectionFactory.class);
|
||||
when(factory.open(any(), any(), any(), any())).thenAnswer(invocation -> connection());
|
||||
return new RdsDataService(resolver, secrets, mapper, factory, ttl);
|
||||
when(factory.open(any(), any(), any(), any(), any())).thenAnswer(invocation -> connection());
|
||||
return new RdsDataService(resolver, secrets, mapper, factory, ttl,
|
||||
(runtimeId, engine) -> new EndpointInfo("127.0.0.1", 5432));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
||||
Reference in new issue
Block a user