diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsLogicalReplicationTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsLogicalReplicationTest.java new file mode 100644 index 0000000000..0b373d2479 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsLogicalReplicationTest.java @@ -0,0 +1,99 @@ +package com.floci.test; + +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.rds.RdsClient; +import software.amazon.awssdk.services.rds.model.ApplyMethod; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Properties; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("RDS PostgreSQL logical replication") +class RdsLogicalReplicationTest { + + private static final String USERNAME = "admin"; + private static final String PASSWORD = "secret123"; + + @Test + @DisplayName("rds.logical_replication=1 in the DB parameter group starts PostgreSQL with wal_level=logical") + void logicalReplicationParameterSetsLogicalWalLevel() throws Exception { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Disposable emulator database fixtures"); + String group = TestFixtures.uniqueName("rds-logical-pg"); + String id = TestFixtures.uniqueName("rds-logical"); + try (RdsClient rds = TestFixtures.rdsClient()) { + rds.createDBParameterGroup(r -> r.dbParameterGroupName(group) + .dbParameterGroupFamily("postgres16").description("logical replication")); + try { + rds.modifyDBParameterGroup(r -> r.dbParameterGroupName(group).parameters(p -> p + .parameterName("rds.logical_replication").parameterValue("1") + .applyMethod(ApplyMethod.PENDING_REBOOT))); + int port = rds.createDBInstance(r -> r.dbInstanceIdentifier(id).engine("postgres") + .dbInstanceClass("db.t3.micro").allocatedStorage(20).dbParameterGroupName(group) + .masterUsername(USERNAME).masterUserPassword(PASSWORD)) + .dbInstance().endpoint().port(); + + assertThat(walLevel(port)).isEqualTo("logical"); + } finally { + rds.deleteDBInstance(r -> r.dbInstanceIdentifier(id).skipFinalSnapshot(true)); + rds.deleteDBParameterGroup(r -> r.dbParameterGroupName(group)); + } + } + } + + @Test + @DisplayName("rds.logical_replication=1 in the DB cluster parameter group applies to the Aurora PostgreSQL writer on reboot") + void clusterLogicalReplicationParameterAppliesOnWriterReboot() throws Exception { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Disposable emulator database fixtures"); + String group = TestFixtures.uniqueName("rds-logical-cpg"); + String clusterId = TestFixtures.uniqueName("rds-logical-cluster"); + String writerId = clusterId + "-writer"; + try (RdsClient rds = TestFixtures.rdsClient()) { + rds.createDBClusterParameterGroup(r -> r.dbClusterParameterGroupName(group) + .dbParameterGroupFamily("aurora-postgresql16").description("logical replication")); + try { + int port = rds.createDBCluster(r -> r.dbClusterIdentifier(clusterId).engine("aurora-postgresql") + .dbClusterParameterGroupName(group) + .masterUsername(USERNAME).masterUserPassword(PASSWORD)) + .dbCluster().port(); + rds.createDBInstance(r -> r.dbInstanceIdentifier(writerId).dbClusterIdentifier(clusterId) + .engine("aurora-postgresql").dbInstanceClass("db.r6g.large")); + assertThat(walLevel(port)).isEqualTo("replica"); + + rds.modifyDBClusterParameterGroup(r -> r.dbClusterParameterGroupName(group).parameters(p -> p + .parameterName("rds.logical_replication").parameterValue("1") + .applyMethod(ApplyMethod.PENDING_REBOOT))); + assertThat(walLevel(port)).isEqualTo("replica"); + + rds.rebootDBInstance(r -> r.dbInstanceIdentifier(writerId)); + assertThat(walLevel(port)).isEqualTo("logical"); + } finally { + try { + rds.deleteDBInstance(r -> r.dbInstanceIdentifier(writerId).skipFinalSnapshot(true)); + } finally { + rds.deleteDBCluster(r -> r.dbClusterIdentifier(clusterId).skipFinalSnapshot(true)); + rds.deleteDBClusterParameterGroup(r -> r.dbClusterParameterGroupName(group)); + } + } + } + } + + private static String walLevel(int port) throws Exception { + Properties properties = new Properties(); + properties.setProperty("user", USERNAME); + properties.setProperty("password", PASSWORD); + properties.setProperty("sslmode", "disable"); + properties.setProperty("connectTimeout", "5"); + try (Connection connection = TestFixtures.awaitJdbcConnection( + "jdbc:postgresql://" + TestFixtures.proxyHost() + ":" + port + "/postgres", properties); + Statement statement = connection.createStatement(); + ResultSet rows = statement.executeQuery("SHOW wal_level")) { + assertThat(rows.next()).isTrue(); + return rows.getString(1); + } + } +} diff --git a/docs/services/rds.md b/docs/services/rds.md index 8c04905ad0..795cff8e94 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -668,8 +668,10 @@ one only Aurora has, is skipped with a warning in the database log. A formula va `require_secure_transport` and `skip_grant_tables`, which would lock out Floci's proxy (it talks to the server without TLS). When the server refuses a value and stops, the instance, or every member of the cluster, reports `incompatible-parameters` and Floci logs the server's error; correct -the group and reboot to start it again. PostgreSQL and SQL Server databases do not take parameter -group values yet. +the group and reboot to start it again. A PostgreSQL or Aurora PostgreSQL database takes one value +from its groups, layered and applied the same way: `rds.logical_replication` of `1` starts the +server with `wal_level=logical`, as on RDS. Its other parameter group values, and those of SQL +Server, are not applied yet. ## Option Groups diff --git a/src/main/java/io/floci/aws/services/rds/container/RdsContainerManager.java b/src/main/java/io/floci/aws/services/rds/container/RdsContainerManager.java index 5d4946f4bc..84a3fd7591 100644 --- a/src/main/java/io/floci/aws/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/floci/aws/services/rds/container/RdsContainerManager.java @@ -1106,9 +1106,9 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe } /** - * Waits for a MySQL or MariaDB server started with a parameter group's options to accept TCP - * connections. The image checks the options before it starts anything, and the server reads - * them again as it starts, so a container that stops here refused one of them. A server still + * Waits for a server started with a parameter group's options to accept TCP connections. The + * MySQL and MariaDB images check the options before they start anything, and every server reads + * them as it starts, so a container that stops here refused one of them. A server still * starting when the wait ends is left to finish, as one started without a group is. */ private void awaitServerWithParameterGroup(String containerName, String containerId, int enginePort) { @@ -1691,18 +1691,24 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe } /** - * A parameter group's overrides as MySQL or MariaDB server options. Each takes the - * {@code --loose-} prefix, so a parameter only Aurora or RDS knows (or one an older server - * does not have) is skipped with a warning instead of stopping the server. A value that is a - * formula, such as {@code {DBInstanceClassMemory*3/4}}, is worked out by AWS from the - * instance class and is left to the server's default, as are the parameters that decide - * where the server listens and keeps its data, which Floci controls, and the ones that would - * lock Floci's proxy out. {@code time_zone} is set through {@code --default-time-zone}, and + * A parameter group's overrides as server options. PostgreSQL takes only + * {@code rds.logical_replication}, whose value of 1 becomes {@code wal_level=logical}; its + * other values, and SQL Server's, give no options. For MySQL or MariaDB each + * override takes the {@code --loose-} prefix, so a parameter only Aurora or RDS knows (or one + * an older server does not have) is skipped with a warning instead of stopping the server. A + * value that is a formula, such as {@code {DBInstanceClassMemory*3/4}}, is worked out by AWS + * from the instance class and is left to the server's default, as are the parameters that + * decide where the server listens and keeps its data, which Floci controls, and the ones that + * would lock Floci's proxy out. {@code time_zone} is set through {@code --default-time-zone}, and * Aurora's {@code binlog_format=OFF}, which turns binary logging off, through * {@code --skip-log-bin}. */ public static List engineParameterOptions( DatabaseEngine engine, Map engineParameters) { + if (engine == DatabaseEngine.POSTGRES && engineParameters != null + && "1".equals(engineParameters.get("rds.logical_replication"))) { + return List.of("-c", "wal_level=logical"); + } if (engineParameters == null || engineParameters.isEmpty() || (engine != DatabaseEngine.MYSQL && engine != DatabaseEngine.MARIADB)) { return List.of(); diff --git a/src/test/java/io/floci/aws/services/rds/RdsParameterGroupEngineIntegrationTest.java b/src/test/java/io/floci/aws/services/rds/RdsParameterGroupEngineIntegrationTest.java index 945bd6ca59..6797c27566 100644 --- a/src/test/java/io/floci/aws/services/rds/RdsParameterGroupEngineIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/rds/RdsParameterGroupEngineIntegrationTest.java @@ -13,6 +13,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; +import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import static io.floci.aws.testing.RdsRequests.assumeDocker; @@ -26,7 +27,9 @@ import static org.hamcrest.Matchers.equalTo; * its DB cluster parameter group with its writer's DB parameter group on top, a standalone MySQL * instance with its DB parameter group, and a value changed in the group, or a group switched by * ModifyDBCluster or ModifyDBInstance, takes effect when the writer or the instance is rebooted. A - * value the server refuses leaves the instance incompatible-parameters. Skipped without Docker. + * value the server refuses leaves the instance incompatible-parameters. A PostgreSQL instance or an + * Aurora PostgreSQL writer whose groups set {@code rds.logical_replication} to 1 runs with + * {@code wal_level=logical}, from the same reboot. Skipped without Docker. */ @QuarkusTest @Tag("docker") @@ -46,11 +49,7 @@ class RdsParameterGroupEngineIntegrationTest { String group = "engine-cpg-" + suffix; String clusterId = "engine-cpg-cluster-" + suffix; String writerId = "engine-cpg-writer-" + suffix; - rds("CreateDBClusterParameterGroup") - .formParam("DBClusterParameterGroupName", group) - .formParam("DBParameterGroupFamily", "aurora-mysql8.0") - .formParam("Description", "engine parameters") - .when().post("/").then().statusCode(200); + createClusterParameterGroup(group, "aurora-mysql8.0"); try { rds("ModifyDBClusterParameterGroup") .formParam("DBClusterParameterGroupName", group) @@ -96,13 +95,8 @@ class RdsParameterGroupEngineIntegrationTest { + ".DBCluster.DBClusterParameterGroup"); assertThat(reported, equalTo(group)); - createWriter(writerId, clusterId, null); - rds("ModifyDBClusterParameterGroup") - .formParam("DBClusterParameterGroupName", group) - .formParam("Parameters.member.1.ParameterName", "max_connections") - .formParam("Parameters.member.1.ParameterValue", "400") - .formParam("Parameters.member.1.ApplyMethod", "pending-reboot") - .when().post("/").then().statusCode(200); + createWriter(writerId, clusterId, "aurora-mysql", null); + modifyClusterParameterGroup(group, "max_connections", "400", "pending-reboot"); rebootInstance(writerId); assertThat(query(port, "SELECT @@GLOBAL.max_connections"), equalTo("400")); @@ -141,7 +135,7 @@ class RdsParameterGroupEngineIntegrationTest { assertThat(query(port, "SELECT @@GLOBAL.max_connections, @@GLOBAL.slow_query_log"), equalTo("500 1")); - createWriter(writerId, clusterId, writerGroup); + createWriter(writerId, clusterId, "aurora-mysql", writerGroup); assertThat(query(port, "SELECT @@GLOBAL.max_connections, @@GLOBAL.slow_query_log"), equalTo("800 1")); @@ -180,7 +174,7 @@ class RdsParameterGroupEngineIntegrationTest { createParameterGroup(group, "mysql8.0", "max_connections", "300"); createParameterGroup(switchedGroup, "mysql8.0", "max_connections", "600"); try { - int port = createMysqlInstance(instanceId, group); + int port = createInstance(instanceId, "mysql", "8.0", group); assertThat(query(port, "SELECT @@GLOBAL.max_connections"), equalTo("300")); @@ -210,15 +204,10 @@ class RdsParameterGroupEngineIntegrationTest { String instanceId = "engine-bad-db-" + suffix; createParameterGroup(group, "mysql8.0", "sql_mode", "NOT_A_MODE"); try { - int port = createMysqlInstance(instanceId, group); + int port = createInstance(instanceId, "mysql", "8.0", group); assertThat(instanceStatus(instanceId), equalTo("incompatible-parameters")); - rds("ModifyDBParameterGroup") - .formParam("DBParameterGroupName", group) - .formParam("Parameters.member.1.ParameterName", "sql_mode") - .formParam("Parameters.member.1.ParameterValue", "STRICT_TRANS_TABLES") - .formParam("Parameters.member.1.ApplyMethod", "immediate") - .when().post("/").then().statusCode(200); + modifyParameterGroup(group, "sql_mode", "STRICT_TRANS_TABLES", "immediate"); rebootInstance(instanceId); assertThat(instanceStatus(instanceId), equalTo("available")); @@ -231,11 +220,73 @@ class RdsParameterGroupEngineIntegrationTest { } } - private static int createMysqlInstance(String instanceId, String parameterGroup) { + @Test + void auroraPostgresqlWriterRunsWithLogicalReplicationFromItsClusterParameterGroup() throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String group = "engine-pg-cpg-" + suffix; + String clusterId = "engine-pg-cluster-" + suffix; + String writerId = "engine-pg-writer-" + suffix; + createClusterParameterGroup(group, "aurora-postgresql16"); + try { + modifyClusterParameterGroup(group, "rds.logical_replication", "1", "pending-reboot"); + + int port = rds("CreateDBCluster") + .formParam("DBClusterIdentifier", clusterId) + .formParam("Engine", "aurora-postgresql") + .formParam("DBClusterParameterGroupName", group) + .formParam("MasterUsername", MASTER_USER) + .formParam("MasterUserPassword", MASTER_PASSWORD) + .when().post("/").then().statusCode(200).extract().xmlPath() + .getInt("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.Port"); + createWriter(writerId, clusterId, "aurora-postgresql", null); + assertThat(postgresQuery(port, "SHOW wal_level"), equalTo("logical")); + + modifyClusterParameterGroup(group, "rds.logical_replication", "0", "pending-reboot"); + assertThat(postgresQuery(port, "SHOW wal_level"), equalTo("logical")); + + rebootInstance(writerId); + assertThat(postgresQuery(port, "SHOW wal_level"), equalTo("replica")); + } finally { + deleteInstance(writerId); + rds("DeleteDBCluster") + .formParam("DBClusterIdentifier", clusterId) + .formParam("SkipFinalSnapshot", "true") + .when().post("/"); + rds("DeleteDBClusterParameterGroup") + .formParam("DBClusterParameterGroupName", group) + .when().post("/"); + } + } + + @Test + void postgresInstanceTurnsOnLogicalReplicationWhenRebooted() throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String group = "engine-pg-pg-" + suffix; + String instanceId = "engine-pg-db-" + suffix; + createParameterGroup(group, "postgres16"); + try { + int port = createInstance(instanceId, "postgres", "16", group); + assertThat(postgresQuery(port, "SHOW wal_level"), equalTo("replica")); + + modifyParameterGroup(group, "rds.logical_replication", "1", "pending-reboot"); + assertThat(postgresQuery(port, "SHOW wal_level"), equalTo("replica")); + + rebootInstance(instanceId); + assertThat(postgresQuery(port, "SHOW wal_level"), equalTo("logical")); + } finally { + deleteInstance(instanceId); + rds("DeleteDBParameterGroup") + .formParam("DBParameterGroupName", group) + .when().post("/"); + } + } + + private static int createInstance( + String instanceId, String engine, String engineVersion, String parameterGroup) { return rds("CreateDBInstance") .formParam("DBInstanceIdentifier", instanceId) - .formParam("Engine", "mysql") - .formParam("EngineVersion", "8.0") + .formParam("Engine", engine) + .formParam("EngineVersion", engineVersion) .formParam("DBInstanceClass", "db.t3.micro") .formParam("AllocatedStorage", "20") .formParam("DBParameterGroupName", parameterGroup) @@ -245,11 +296,11 @@ class RdsParameterGroupEngineIntegrationTest { .getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port"); } - private static void createWriter(String writerId, String clusterId, String parameterGroup) { + private static void createWriter(String writerId, String clusterId, String engine, String parameterGroup) { RequestSpecification request = rds("CreateDBInstance") .formParam("DBInstanceIdentifier", writerId) .formParam("DBClusterIdentifier", clusterId) - .formParam("Engine", "aurora-mysql") + .formParam("Engine", engine) .formParam("DBInstanceClass", "db.r6g.large"); if (parameterGroup != null) { request = request.formParam("DBParameterGroupName", parameterGroup); @@ -272,26 +323,30 @@ class RdsParameterGroupEngineIntegrationTest { } private static void createParameterGroup(String name, String family, String parameter, String value) { + createParameterGroup(name, family); + modifyParameterGroup(name, parameter, value, "immediate"); + } + + private static void createParameterGroup(String name, String family) { rds("CreateDBParameterGroup") .formParam("DBParameterGroupName", name) .formParam("DBParameterGroupFamily", family) .formParam("Description", "engine parameters") .when().post("/").then().statusCode(200); + } + + private static void modifyParameterGroup(String name, String parameter, String value, String applyMethod) { rds("ModifyDBParameterGroup") .formParam("DBParameterGroupName", name) .formParam("Parameters.member.1.ParameterName", parameter) .formParam("Parameters.member.1.ParameterValue", value) - .formParam("Parameters.member.1.ApplyMethod", "immediate") + .formParam("Parameters.member.1.ApplyMethod", applyMethod) .when().post("/").then().statusCode(200); } private static void createClusterParameterGroup( String name, String parameter, String value, String secondParameter, String secondValue) { - rds("CreateDBClusterParameterGroup") - .formParam("DBClusterParameterGroupName", name) - .formParam("DBParameterGroupFamily", "aurora-mysql8.0") - .formParam("Description", "engine parameters") - .when().post("/").then().statusCode(200); + createClusterParameterGroup(name, "aurora-mysql8.0"); rds("ModifyDBClusterParameterGroup") .formParam("DBClusterParameterGroupName", name) .formParam("Parameters.member.1.ParameterName", parameter) @@ -303,15 +358,52 @@ class RdsParameterGroupEngineIntegrationTest { .when().post("/").then().statusCode(200); } - /** - * The first row of a query, its columns joined by single spaces. The MySQL image restarts its - * server once after initializing a new data directory, so a refused connection is retried. - */ + private static void createClusterParameterGroup(String name, String family) { + rds("CreateDBClusterParameterGroup") + .formParam("DBClusterParameterGroupName", name) + .formParam("DBParameterGroupFamily", family) + .formParam("Description", "engine parameters") + .when().post("/").then().statusCode(200); + } + + private static void modifyClusterParameterGroup( + String name, String parameter, String value, String applyMethod) { + rds("ModifyDBClusterParameterGroup") + .formParam("DBClusterParameterGroupName", name) + .formParam("Parameters.member.1.ParameterName", parameter) + .formParam("Parameters.member.1.ParameterValue", value) + .formParam("Parameters.member.1.ApplyMethod", applyMethod) + .when().post("/").then().statusCode(200); + } + + /** The first row of a MySQL query, its columns joined by single spaces. */ private static String query(int port, String sql) throws Exception { + return retryUntilConnected(() -> queryOnce(port, sql)); + } + + private static String postgresQuery(int port, String sql) throws Exception { + return retryUntilConnected(() -> { + try (Connection connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:" + port + + "/postgres?sslmode=disable&connectTimeout=5&socketTimeout=10", + MASTER_USER, MASTER_PASSWORD); + Statement statement = connection.createStatement(); + ResultSet rows = statement.executeQuery(sql)) { + rows.next(); + return rows.getString(1); + } + }); + } + + /** + * Runs a query until the server answers it, for up to 90 seconds. A server still starting + * refuses connections, and the MySQL image restarts its server once after initializing a new + * data directory, so a refused connection is retried. + */ + private static String retryUntilConnected(Callable attempt) throws Exception { long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(90); while (true) { try { - return queryOnce(port, sql); + return attempt.call(); } catch (SQLException e) { if (System.nanoTime() > deadline) { throw e; diff --git a/src/test/java/io/floci/aws/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/floci/aws/services/rds/container/RdsContainerManagerTest.java index 016d6a9e0e..d0988dd83b 100644 --- a/src/test/java/io/floci/aws/services/rds/container/RdsContainerManagerTest.java +++ b/src/test/java/io/floci/aws/services/rds/container/RdsContainerManagerTest.java @@ -171,6 +171,18 @@ class RdsContainerManagerTest { RdsContainerManager.engineParameterOptions(DatabaseEngine.MYSQL, Map.of("binlog_format", "ROW"))); } + @Test + void postgresLogicalReplicationParameterStartsTheServerWithLogicalWal() { + Map overrides = new LinkedHashMap<>(); + overrides.put("rds.logical_replication", "1"); + overrides.put("max_connections", "500"); + + assertEquals(List.of("-c", "wal_level=logical"), + RdsContainerManager.buildContainerCmd(DatabaseEngine.POSTGRES, "postgres:16-alpine", overrides)); + assertTrue(RdsContainerManager.engineParameterOptions( + DatabaseEngine.POSTGRES, Map.of("rds.logical_replication", "0")).isEmpty()); + } + @Test void parameterGroupOverridesAreNotPassedToOtherEngines() { assertTrue(RdsContainerManager.buildContainerCmd(