Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(rds-data): reject concurrent transaction requests (#5575)

Aniket Shukla2a04cd78c72c
diff --git a/docs/services/rds-data.md b/docs/services/rds-data.md
index 39115edb89..d0a38a5c80 100644
--- a/docs/services/rds-data.md
+++ b/docs/services/rds-data.md
@@ -34,6 +34,7 @@ The deprecated `ExecuteSql` operation is recognized at `POST /ExecuteSql` and re
- Resolving a cluster ARN retries missing active member relays even if the cluster backend is already running. A failed member relay does not block SQL through the healthy cluster backend and is retried on a subsequent request. Healthy relays are not rebound and stopped members remain stopped. See [recoverable RDS backend startup failures](rds.md#recoverable-backend-startup-failures).
- `database` is optional when the resolved RDS resource has a database name; otherwise it must be provided. Transactional `ExecuteStatement` requests must use the same database as the active transaction when `database` is present.
- Transaction requests validate `resourceArn` against the active transaction resource. Floci resolves accepted ARN aliases to the local resource before comparing transaction identity.
+- Requests sharing a `transactionId` do not queue behind an active request: `ExecuteStatement`, `BatchExecuteStatement`, `CommitTransaction`, and `RollbackTransaction` immediately return HTTP 400 `DatabaseErrorException` with `Transaction is still running a query`. Retry after the active request finishes. Other transactions remain independent, and expiry cleanup skips active requests. The idle timeout starts when the statement or batch finishes.
- MySQL, MariaDB, and PostgreSQL resources are supported. Aurora PostgreSQL resources resolve to the same PostgreSQL execution path.
- SQL is sent directly to the local database engine through JDBC. `SqlParameter` binding is supported for all engines: named `:placeholder` markers are rewritten to positional JDBC bind parameters and executed through a `PreparedStatement`. Colons inside string literals, quoted/backtick identifiers, comments, PostgreSQL `::` casts, and PostgreSQL dollar-quoted strings are left untouched. A placeholder used more than once binds its value at each position. Supported value variants are `stringValue`, `booleanValue`, `longValue`, `doubleValue`, `blobValue`, and `isNull`; `typeHint` values `DECIMAL`, `TIMESTAMP`, `DATE`, `TIME`, `UUID`, and `JSON` are honored. `arrayValue` parameters are not supported yet and return `BadRequestException`, as do malformed `parameters` payloads (not a JSON array, or an entry missing `name`).
- Result records include Data API field variants such as `stringValue`, `longValue`, `blobValue`, `booleanValue`, `doubleValue`, and `isNull`. `records` is returned only for statements that produce a result set — empty when the query matched no rows — and is omitted entirely from the response to a statement that reports an update count, as AWS does. An `INSERT`, `UPDATE`, `DELETE`, or DDL statement therefore answers with `numberOfRecordsUpdated` and `generatedFields` and no `records` field at all.
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
index 00392a5bcf..69527b289d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
@@ -40,6 +40,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
@ApplicationScoped
public class RdsDataService implements Resettable {
@@ -121,22 +122,28 @@ public class RdsDataService implements Resettable {
void shutdown() {
transactionCleanupExecutor.shutdownNow();
transactions.forEach((id, tx) -> {
- synchronized (tx) {
+ tx.lock.lock();
+ try {
if (transactions.remove(id, tx)) {
rollbackQuietly(tx.connection);
tx.close();
}
+ } finally {
+ tx.lock.unlock();
}
});
}
public void clear() {
transactions.forEach((id, tx) -> {
- synchronized (tx) {
+ tx.lock.lock();
+ try {
if (transactions.remove(id, tx)) {
rollbackQuietly(tx.connection);
tx.close();
}
+ } finally {
+ tx.lock.unlock();
}
});
transactions.clear();
@@ -211,9 +218,10 @@ public class RdsDataService implements Resettable {
String resourceArn = requiredText(request, "resourceArn");
requiredText(request, "secretArn");
TransactionContext tx = transaction(transactionId);
- synchronized (tx) {
+ validateTransactionResource(tx, resourceArn, region);
+ acquireTransaction(tx);
+ try {
requireActiveTransaction(transactionId, tx);
- validateTransactionResource(tx, resourceArn, region);
if (!transactions.remove(transactionId, tx)) {
throw transactionNotFound(transactionId);
}
@@ -224,6 +232,8 @@ public class RdsDataService implements Resettable {
} finally {
tx.close();
}
+ } finally {
+ tx.lock.unlock();
}
ObjectNode response = objectMapper.createObjectNode();
response.put("transactionStatus", "Transaction Committed");
@@ -235,9 +245,10 @@ public class RdsDataService implements Resettable {
String resourceArn = requiredText(request, "resourceArn");
requiredText(request, "secretArn");
TransactionContext tx = transaction(transactionId);
- synchronized (tx) {
+ validateTransactionResource(tx, resourceArn, region);
+ acquireTransaction(tx);
+ try {
requireActiveTransaction(transactionId, tx);
- validateTransactionResource(tx, resourceArn, region);
if (!transactions.remove(transactionId, tx)) {
throw transactionNotFound(transactionId);
}
@@ -248,6 +259,8 @@ public class RdsDataService implements Resettable {
} finally {
tx.close();
}
+ } finally {
+ tx.lock.unlock();
}
ObjectNode response = objectMapper.createObjectNode();
response.put("transactionStatus", "Rollback Complete");
@@ -262,11 +275,14 @@ public class RdsDataService implements Resettable {
if (transactionId != null && !transactionId.isBlank()) {
TransactionContext tx = transaction(transactionId);
- synchronized (tx) {
+ validateTransactionIdentity(tx, request, region);
+ acquireTransaction(tx);
+ try {
requireActiveTransaction(transactionId, tx);
- validateTransactionIdentity(tx, request, region);
- tx.refresh(transactionTtl);
return work.execute(tx.connection, tx.engine);
+ } finally {
+ tx.refresh(transactionTtl);
+ tx.lock.unlock();
}
}
@@ -654,15 +670,24 @@ public class RdsDataService implements Resettable {
}
}
+ private void acquireTransaction(TransactionContext tx) {
+ if (!tx.lock.tryLock()) {
+ requireActiveTransaction(tx.id, tx);
+ throw new AwsException("DatabaseErrorException", "Transaction is still running a query", 400);
+ }
+ }
+
private void cleanupExpiredTransactions() {
Instant now = Instant.now();
transactions.forEach((id, tx) -> {
- if (tx.expiresAt.isBefore(now)) {
- synchronized (tx) {
+ if (tx.expiresAt.isBefore(now) && tx.lock.tryLock()) {
+ try {
if (tx.expiresAt.isBefore(now) && transactions.remove(id, tx)) {
rollbackQuietly(tx.connection);
tx.close();
}
+ } finally {
+ tx.lock.unlock();
}
}
});
@@ -816,6 +841,7 @@ public class RdsDataService implements Resettable {
private final String database;
private final String region;
private final RdsBackendGate.Lease lease;
+ private final ReentrantLock lock = new ReentrantLock();
private volatile Instant expiresAt;
private TransactionContext(
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataTransactionConcurrencyTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataTransactionConcurrencyTest.java
new file mode 100644
index 0000000000..548f88dab3
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataTransactionConcurrencyTest.java
@@ -0,0 +1,239 @@
+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.services.rds.model.DatabaseEngine;
+import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
+import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
+import org.h2.Driver;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+import org.mockito.invocation.InvocationOnMock;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Duration;
+import java.util.UUID;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+class RdsDataTransactionConcurrencyTest {
+ private static final String REGION = "us-east-1";
+ private static final String RESOURCE_ARN = "arn:aws:rds:us-east-1:000000000000:cluster:test";
+ private static final String BLOCKED_QUERY = "select 1";
+ private static final String BLOCKED_BATCH = "insert into items(id) values (1)";
+
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final CountDownLatch started = new CountDownLatch(1);
+ private final CountDownLatch release = new CountDownLatch(1);
+ private String jdbcUrl;
+ private RdsDataService service;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ Driver.load();
+ jdbcUrl = "jdbc:h2:mem:busy_" + UUID.randomUUID() + ";MODE=PostgreSQL;DB_CLOSE_DELAY=-1";
+ try (Connection connection = DriverManager.getConnection(jdbcUrl, "sa", "");
+ Statement statement = connection.createStatement()) {
+ statement.execute("create table items(id integer)");
+ }
+ service = createService(Duration.ofSeconds(60));
+ }
+
+ 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"));
+ 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);
+ }
+
+ @AfterEach
+ void tearDown() {
+ release.countDown();
+ if (service != null) {
+ service.shutdown();
+ }
+ }
+
+ @ParameterizedTest
+ @EnumSource(Operation.class)
+ void rejectsConcurrentRequestsWithoutWaiting(Operation operation) throws Exception {
+ String tx = begin();
+ try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
+ Future<ObjectNode> first = executor.submit(() -> service.executeStatement(request(tx, BLOCKED_QUERY), REGION));
+ try {
+ assertTrue(started.await(5, TimeUnit.SECONDS));
+ Future<AwsException> second = executor.submit(() -> assertThrows(AwsException.class,
+ () -> perform(operation, tx)));
+ AwsException error = second.get(5, TimeUnit.SECONDS);
+ assertEquals("DatabaseErrorException", error.getErrorCode());
+ assertEquals("Transaction is still running a query", error.getMessage());
+ assertEquals(400, error.getHttpStatus());
+
+ String independent = begin();
+ assertEquals(2, query(independent));
+ service.rollbackTransaction(request(independent, null), REGION);
+ } finally {
+ release.countDown();
+ }
+ assertEquals(1, first.get(5, TimeUnit.SECONDS).path("records").path(0).path(0).path("longValue").asInt());
+ }
+ assertEquals(2, query(tx));
+ service.commitTransaction(request(tx, null), REGION);
+ }
+
+ @Test
+ void anInFlightBatchAlsoRejectsConcurrentStatements() throws Exception {
+ String tx = begin();
+ try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
+ Future<ObjectNode> first = executor.submit(() -> service.batchExecuteStatement(batch(tx, BLOCKED_BATCH), REGION));
+ try {
+ assertTrue(started.await(5, TimeUnit.SECONDS));
+ Future<AwsException> second = executor.submit(() -> assertThrows(AwsException.class, () -> query(tx)));
+ assertEquals("Transaction is still running a query", second.get(5, TimeUnit.SECONDS).getMessage());
+ } finally {
+ release.countDown();
+ }
+ assertEquals(1, first.get(5, TimeUnit.SECONDS).path("updateResults").size());
+ }
+ assertEquals(2, query(tx));
+ service.rollbackTransaction(request(tx, null), REGION);
+ }
+
+ @Test
+ void aFailedStatementReleasesTheTransaction() {
+ String tx = begin();
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.executeStatement(request(tx, "select * from missing_table"), REGION));
+ assertEquals("DatabaseErrorException", error.getErrorCode());
+ assertEquals(2, query(tx));
+ service.rollbackTransaction(request(tx, null), REGION);
+ }
+
+ @Test
+ void cleanupSkipsActiveQueriesAndTheIdleTimeoutStartsWhenTheyFinish() throws Exception {
+ service.shutdown();
+ service = createService(Duration.ofSeconds(1));
+ String tx = begin();
+ try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
+ Future<ObjectNode> first = executor.submit(() -> service.executeStatement(request(tx, BLOCKED_QUERY), REGION));
+ try {
+ assertTrue(started.await(5, TimeUnit.SECONDS));
+ Thread.sleep(1100);
+ Future<String> independent = executor.submit(this::begin);
+ String other = independent.get(5, TimeUnit.SECONDS);
+ service.rollbackTransaction(request(other, null), REGION);
+ } finally {
+ release.countDown();
+ }
+ first.get(5, TimeUnit.SECONDS);
+ }
+ assertEquals(2, query(tx));
+ service.rollbackTransaction(request(tx, null), REGION);
+ }
+
+ @Test
+ void busyTransactionsStillRejectRequestsForAnotherRegion() throws Exception {
+ String tx = begin();
+ try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
+ Future<ObjectNode> first = executor.submit(() -> service.executeStatement(request(tx, BLOCKED_QUERY), REGION));
+ try {
+ assertTrue(started.await(5, TimeUnit.SECONDS));
+ Future<AwsException> invalid = executor.submit(() -> assertThrows(AwsException.class,
+ () -> service.executeStatement(request(tx, "select 2"), "us-west-2")));
+ assertEquals("TransactionNotFoundException", invalid.get(5, TimeUnit.SECONDS).getErrorCode());
+ } finally {
+ release.countDown();
+ }
+ first.get(5, TimeUnit.SECONDS);
+ }
+ }
+
+ private Connection connection() throws SQLException {
+ Connection connection = spy(DriverManager.getConnection(jdbcUrl, "sa", ""));
+ doAnswer(invocation -> {
+ PreparedStatement statement = spy((PreparedStatement) invocation.callRealMethod());
+ String sql = invocation.getArgument(0);
+ if (BLOCKED_QUERY.equals(sql)) {
+ doAnswer(this::awaitQuery).when(statement).execute();
+ } else if (BLOCKED_BATCH.equals(sql)) {
+ doAnswer(this::awaitQuery).when(statement).executeBatch();
+ }
+ return statement;
+ }).when(connection).prepareStatement(anyString());
+ return connection;
+ }
+
+ private Object awaitQuery(InvocationOnMock invocation) throws Throwable {
+ started.countDown();
+ if (!release.await(15, TimeUnit.SECONDS)) {
+ throw new SQLException("Timed out waiting for the test to release the query");
+ }
+ return invocation.callRealMethod();
+ }
+
+ private String begin() {
+ return service.beginTransaction(request(null, null), REGION).path("transactionId").asText();
+ }
+
+ private int query(String tx) {
+ return service.executeStatement(request(tx, "select 2"), REGION)
+ .path("records").path(0).path(0).path("longValue").asInt();
+ }
+
+ private ObjectNode request(String tx, String sql) {
+ ObjectNode request = mapper.createObjectNode().put("resourceArn", RESOURCE_ARN)
+ .put("secretArn", "arn:aws:secretsmanager:us-east-1:000000000000:secret:test")
+ .put("database", "app");
+ if (tx != null) {
+ request.put("transactionId", tx);
+ }
+ if (sql != null) {
+ request.put("sql", sql);
+ }
+ return request;
+ }
+
+ private ObjectNode batch(String tx, String sql) {
+ return request(tx, sql).set("parameterSets", mapper.createArrayNode().add(mapper.createArrayNode()));
+ }
+
+ private ObjectNode perform(Operation operation, String tx) {
+ return switch (operation) {
+ case EXECUTE -> service.executeStatement(request(tx, "select 2"), REGION);
+ case BATCH -> service.batchExecuteStatement(batch(tx, "delete from items"), REGION);
+ case COMMIT -> service.commitTransaction(request(tx, null), REGION);
+ case ROLLBACK -> service.rollbackTransaction(request(tx, null), REGION);
+ };
+ }
+
+ private enum Operation {
+ EXECUTE, BATCH, COMMIT, ROLLBACK
+ }
+}