Light, fluffy, and always free - The AWS Local Emulator alternative
feat(redshift-data): add async execution and statement status events (#5383)
Vũ Thế Huyae75a653d683
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftDataOperationsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftDataOperationsTest.javaindex 7f0463fe3e..8be93c78eb 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftDataOperationsTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftDataOperationsTest.java@@ -1,10 +1,14 @@package com.floci.test;+import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;import org.jboss.logging.Logger;import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Test;+import software.amazon.awssdk.services.eventbridge.EventBridgeClient;+import software.amazon.awssdk.services.eventbridge.model.Target;import software.amazon.awssdk.services.redshift.RedshiftClient;import software.amazon.awssdk.services.redshift.model.CreateClusterRequest;import software.amazon.awssdk.services.redshift.model.DeleteClusterRequest;@@ -15,9 +19,14 @@ import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementRequesimport software.amazon.awssdk.services.redshiftdata.model.GetStatementResultRequest;import software.amazon.awssdk.services.redshiftdata.model.GetStatementResultResponse;import software.amazon.awssdk.services.redshiftdata.model.StatusString;+import software.amazon.awssdk.services.sqs.SqsClient;+import software.amazon.awssdk.services.sqs.model.Message;+import software.amazon.awssdk.services.sqs.model.QueueAttributeName;import java.time.Duration;import java.time.Instant;+import java.util.List;+import java.util.Map;import static org.assertj.core.api.Assertions.assertThat;@@ -144,6 +153,86 @@ class RedshiftDataOperationsTest {assertThat(failure("SELECT '{\"a\":1}'::jsonb")).isEqualTo("ERROR: type \"jsonb\" does not exist");}+ @Test+ @DisplayName("WithEvent routes terminal SQL status through EventBridge to SQS")+ void withEventRoutesFinishedAndFailedStatementsToSqs() throws Exception {+ ObjectMapper mapper = new ObjectMapper();+ String name = TestFixtures.uniqueName("rsdata-event");+ try (EventBridgeClient events = TestFixtures.eventBridgeClient(); SqsClient sqs = TestFixtures.sqsClient()) {+ String queueUrl = sqs.createQueue(request -> request.queueName(name)).queueUrl();+ String queueArn = sqs.getQueueAttributes(request -> request.queueUrl(queueUrl)+ .attributeNames(QueueAttributeName.QUEUE_ARN)).attributes().get(QueueAttributeName.QUEUE_ARN);+ String pattern = mapper.writeValueAsString(Map.of("source", List.of("aws.redshift-data"),+ "detail", Map.of("statementName", List.of(name))));+ String ruleArn = events.putRule(request -> request.name(name).eventPattern(pattern)).ruleArn();+ String policy = mapper.writeValueAsString(Map.of("Version", "2012-10-17", "Statement", List.of(Map.of(+ "Effect", "Allow", "Principal", Map.of("Service", "events.amazonaws.com"),+ "Action", "sqs:SendMessage", "Resource", queueArn,+ "Condition", Map.of("ArnEquals", Map.of("aws:SourceArn", ruleArn))))));+ sqs.setQueueAttributes(request -> request.queueUrl(queueUrl).attributes(Map.of(QueueAttributeName.POLICY, policy)));+ try {+ events.putTargets(request -> request.rule(name).targets(Target.builder().id("queue").arn(queueArn).build()));+ for (String sql : List.of("SELECT 1", "SELECT * FROM missing_event_table")) {+ String id = data.executeStatement(request -> request.clusterIdentifier(clusterId).database(DATABASE)+ .dbUser(USERNAME).statementName(name).sql(sql).withEvent(true)).id();+ Message message = awaitEvent(sqs, queueUrl, mapper, id);+ JsonNode event = mapper.readTree(message.body());+ assertThat(event.path("source").asText()).isEqualTo("aws.redshift-data");+ assertThat(event.path("detail-type").asText()).isEqualTo("Redshift Data Statement Status Change");+ assertThat(event.path("detail").path("statementName").asText()).isEqualTo(name);+ assertThat(event.path("detail").path("state").asText())+ .isEqualTo(sql.equals("SELECT 1") ? "FINISHED" : "FAILED");+ assertThat(describe(id).statusAsString()).isEqualTo(event.path("detail").path("state").asText());+ assertThat(event.path("detail").path("redshiftQueryId").asLong()).isEqualTo(describe(id).redshiftQueryId());+ assertThat(event.path("resources").get(0).asText()).endsWith(":cluster:" + clusterId);+ sqs.deleteMessage(request -> request.queueUrl(queueUrl).receiptHandle(message.receiptHandle()));+ }+ String batchId = data.batchExecuteStatement(request -> request.clusterIdentifier(clusterId)+ .database(DATABASE).dbUser(USERNAME).statementName(name).withEvent(true)+ .sqls("SELECT 10", "SELECT 20")).id();+ Message batchEvent = awaitEvent(sqs, queueUrl, mapper, batchId);+ assertThat(mapper.readTree(batchEvent.body()).path("detail").path("state").asText()).isEqualTo("FINISHED");+ assertThat(describe(batchId).subStatements()).hasSize(2);+ sqs.deleteMessage(request -> request.queueUrl(queueUrl).receiptHandle(batchEvent.receiptHandle()));+ if (!TestFixtures.isRealAws()) {+ String cancelledId = data.executeStatement(request -> request.clusterIdentifier(clusterId)+ .database(DATABASE).dbUser(USERNAME).statementName(name).withEvent(true).sql("SELECT pg_sleep(20)")).id();+ assertThat(data.cancelStatement(request -> request.id(cancelledId)).status()).isTrue();+ Message abortedEvent = awaitEvent(sqs, queueUrl, mapper, cancelledId);+ assertThat(mapper.readTree(abortedEvent.body()).path("detail").path("state").asText()).isEqualTo("ABORTED");+ assertThat(describe(cancelledId).status()).isEqualTo(StatusString.ABORTED);+ sqs.deleteMessage(request -> request.queueUrl(queueUrl).receiptHandle(abortedEvent.receiptHandle()));+ }+ for (boolean explicitFalse : List.of(false, true)) {+ ExecuteStatementRequest.Builder request = ExecuteStatementRequest.builder().clusterIdentifier(clusterId)+ .database(DATABASE).dbUser(USERNAME).statementName(name).sql("SELECT 2");+ if (explicitFalse) {+ request.withEvent(false);+ }+ awaitFinished(data.executeStatement(request.build()).id());+ }+ assertThat(sqs.receiveMessage(request -> request.queueUrl(queueUrl).waitTimeSeconds(1)).messages()).isEmpty();+ } finally {+ events.removeTargets(request -> request.rule(name).ids("queue"));+ events.deleteRule(request -> request.name(name));+ sqs.deleteQueue(request -> request.queueUrl(queueUrl));+ }+ }+ }++ private static Message awaitEvent(SqsClient sqs, String queueUrl, ObjectMapper mapper, String id) throws Exception {+ Instant deadline = Instant.now().plusSeconds(30);+ while (Instant.now().isBefore(deadline)) {+ List<Message> messages = sqs.receiveMessage(request -> request.queueUrl(queueUrl).waitTimeSeconds(1)).messages();+ for (Message message : messages) {+ if (id.equals(mapper.readTree(message.body()).path("detail").path("statementId").asText())) {+ return message;+ }+ }+ }+ throw new AssertionError("No EventBridge event for statement " + id);+ }+private String failure(String sql) throws Exception {String id = submit(sql);Instant deadline = Instant.now().plus(Duration.ofSeconds(30));@@ -178,6 +267,10 @@ class RedshiftDataOperationsTest {.sql(sql).build()).id();+ return awaitFinished(id);+ }++ private String awaitFinished(String id) throws Exception {Instant deadline = Instant.now().plus(Duration.ofSeconds(30));while (Instant.now().isBefore(deadline)) {DescribeStatementResponse describe = describe(id);diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.javaindex d7e13e5fdf..4e3f3dc8cb 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java@@ -144,7 +144,8 @@ class RedshiftServerlessTest {ExecuteStatementResponse executed = dataClient.executeStatement(request -> request.workgroupName(workgroupName).database("dev")- .sql("select 1"));+ .sql("select 1")+ .waitTimeSeconds(30));assertThat(executed.workgroupName()).isEqualTo(workgroupName);assertThat(executed.clusterIdentifier()).isNull();DescribeStatementResponse described = dataClient.describeStatement(request -> request.id(executed.id()));diff --git a/docs/services/redshift-data.md b/docs/services/redshift-data.mdindex 0c437fb21a..f57935cc94 100644--- a/docs/services/redshift-data.md+++ b/docs/services/redshift-data.md@@ -4,7 +4,7 @@**Endpoint:** `POST http://localhost:4566/` with `X-Amz-Target: RedshiftData.<Operation>` and `Content-Type: application/x-amz-json-1.1`**Backing data plane:** the PostgreSQL container behind a Redshift cluster created through the Redshift emulator-Floci emulates the Amazon Redshift Data API: the HTTP API that Lambda, Step Functions, and EventBridge use to run SQL on a cluster without opening a PostgreSQL wire connection. Floci resolves the target cluster, connects straight to its container over JDBC, runs the SQL, and stores the statement and its result set in memory so the polling operations (`DescribeStatement`, `GetStatementResult`) can read them back.+Floci emulates the Amazon Redshift Data API: the HTTP API that Lambda, Step Functions, and EventBridge use to run SQL on a cluster without opening a PostgreSQL wire connection. Floci resolves the target cluster, connects straight to its container over JDBC, queues the SQL on a shared worker pool, and stores the statement and its result set through the configured storage backend so the polling operations (`DescribeStatement`, `GetStatementResult`) can read them back.For the upstream API shape, see the AWS documentation:@@ -19,13 +19,13 @@ For the upstream API shape, see the AWS documentation:<!-- floci:actions:start -->| Action | Description || --- | --- |-| `ExecuteStatement` | Run one SQL statement synchronously against the cluster container and store the result |+| `ExecuteStatement` | Submit one SQL statement asynchronously against the cluster container || `BatchExecuteStatement` | Run `Sqls` in order on one connection in a single transaction || `DescribeStatement` | Return a stored statement's status, timings, row counts, and sub-statements || `GetStatementResult` | Page through a finished statement's rows as typed `Records` || `GetStatementResultV2` | Same rows in the V2 envelope, with `CSVRecords` when the statement used `ResultFormat=CSV` || `ListStatements` | List stored statements newest first, filtered by `StatementName` or `Status` |-| `CancelStatement` | Mark a non-finished statement `ABORTED`; returns `{ "Status": true }` |+| `CancelStatement` | Cancel queued or running SQL; returns whether cancellation was accepted, and rejects terminal statements || `ListDatabases` | List databases on the cluster (`pg_database`) || `ListSchemas` | List schemas, optionally filtered by `SchemaPattern` || `ListTables` | List tables and views, optionally filtered by `SchemaPattern` and `TablePattern` |@@ -45,19 +45,29 @@ A statement run against a workgroup reports `WorkgroupName` and omits `ClusterId## Compatibility Notes-- **Execution is synchronous.** `ExecuteStatement` runs the SQL and returns only once the statement is terminal. `DescribeStatement` reports `FINISHED` or `FAILED`, or `ABORTED` after a `CancelStatement`. It never reports `SUBMITTED` or `STARTED`, and there is no progression over wall-clock time.+- **Execution is asynchronous.** `ExecuteStatement` and `BatchExecuteStatement` return an `Id` after admission to the shared bounded worker pool. `DescribeStatement` progresses through `SUBMITTED`, `STARTED`, and `FINISHED`, `FAILED` or `ABORTED`. Poll before reading results. Optional `WaitTimeSeconds` accepts integers from 1 to 30 and waits up to that duration. HTTP, Step Functions and Scheduler use the same executor. Saturation returns `ActiveStatementsExceededException`.- **Execution errors are not HTTP errors.** A statement that fails to run is stored with `Status=FAILED` and an `Error` message, and `ExecuteStatement` still returns 200 with the statement `Id`. Callers see the failure through `DescribeStatement`, matching AWS.-- **Results are in memory.** Statement metadata and result sets are held in a store swept on a 24 hour TTL and are lost when Floci restarts. `ListStatements` returns only what is currently in memory.+- **Storage and recovery.** Statement metadata and results use the configured storage mode. A sweep evicts terminal statements older than the 24 hour TTL; active statements are retained. With persistence enabled, interrupted nonterminal statements become `FAILED` after restart, without replaying SQL or emitting events. Memory mode loses results on restart.- **`ExecuteStatement` takes one statement.** If the `Sql` contains more than one statement separated by `;`, it is rejected with `ValidationException`. Use `BatchExecuteStatement` for multiple statements.- **`BatchExecuteStatement`** runs every entry of `Sqls` in order on one connection in a single transaction: it commits at the end, and on the first failing sub-statement it rolls back and marks the batch `FAILED` with that sub-statement's error. `DescribeStatement` on the parent id returns `SubStatements`, one per entry, each with its own id `<parentId>:<n>`. `GetStatementResult` on the parent id returns the rows of the last sub-statement that produced a result set; on a sub-statement id it returns that sub-statement's rows.- **`GetStatementResult` and `GetStatementResultV2`** return the typed `Records` shape. `GetStatementResultV2` also returns `ResultFormat`, and when the statement was run with `ResultFormat=CSV` it returns `Records` as `CSVRecords` strings. A statement that produced no result set (an `INSERT`, `UPDATE`, `DELETE`, or DDL statement) returns `ValidationException` with the message `Statement has no result set`.- **Paging.** The page size is 1000 rows. `NextToken` is an opaque base64 row offset; there is no server-side cursor.- **`SqlParameters` / `Parameters`** are bound into a `PreparedStatement`. Named `:placeholder` markers are rewritten to positional JDBC bind parameters. Colons inside string literals, quoted identifiers, comments, PostgreSQL `::` casts, and dollar-quoted strings are left untouched. Redshift Data API parameter values are always strings on the wire; PostgreSQL coerces each bound value to the column type.-- **`CancelStatement`** returns `{ "Status": true }`. In Floci a statement is already terminal by the time it can be cancelled, so `CancelStatement` sets `Status=ABORTED` only when the statement was not already `FINISHED`. An unknown statement id returns `ResourceNotFoundException`.-- **`WithEvent`** is accepted and ignored: no EventBridge event is published.+- **`CancelStatement`** returns `{ "Status": true }` when cancellation is accepted for queued or running work. Running transactional SQL receives JDBC cancellation and rolls back before reaching `ABORTED`; cancelling a batch rolls back its earlier writes. Cancelling a statement that is already terminal returns `ValidationException` ("Could not cancel a query that is already in ABORTED state with ID: <id>"), as AWS does. Repeated cancellation and statements that already own commit return `false` in Floci. Commands requiring auto-commit, including Spectrum external DDL and `VACUUM`, own completion before performing irreversible work and reject cancellation during that work. An unknown statement id returns `ResourceNotFoundException`.+- **`WithEvent=true`** publishes one terminal status event per parent statement to the default EventBridge bus in the captured account and region. Batch children do not emit events. False or omitted `WithEvent` emits none. Delivery occurs after terminal metadata is stored, on a separate bounded pool; disabled EventBridge, rejected delivery or target failures do not change the SQL outcome. Delivery is best effort, with no durable outbox or restart replay.- **`ExecuteSql` and `BatchExecuteSql`** (the deprecated pre-2020 operations) return `ValidationException`.- **Type mapping.** JDBC `BOOLEAN` and `BIT` map to `booleanValue`; integer types to `longValue`; floating-point types to `doubleValue`; `NUMERIC` and `DECIMAL` to `stringValue` (as AWS does); binary types to `blobValue`; everything else, including dates, timestamps, and uuid, to `stringValue`. A SQL `NULL` maps to `isNull`. A result column of type `line`, `json`, or `jsonb` fails the statement with the Redshift error text.+## Statement status events++[Redshift Data API status events](https://docs.aws.amazon.com/redshift/latest/mgmt/data-api-monitoring-events.html)+use source `aws.redshift-data` and detail type `Redshift Data Statement Status Change`. The+`detail` contains `statementId`, optional `statementName` and resolved `principal`,+`redshiftQueryId`, `state`, `rows` and `expireAt` in epoch seconds. Query ids match+`DescribeStatement`; Floci derives them from statement ids rather than backend query ids.+`resources` contains the cluster ARN, or the workgroup ARN for Floci's Serverless support.+EventBridge rules can route these events to SQS, Lambda or other supported targets.+## Spectrum relational queriesThe Data API prepares Glue-backed external CSV tables on the same JDBC connection that executes@@ -69,7 +79,7 @@ failure rolls back the batch. External read or preparation failures are stored areported by `DescribeStatement`, as other SQL execution failures are.Create external schemas and tables outside a batch: `CREATE EXTERNAL TABLE` cannot run inside a transaction block on AWS, and a batch runs as one transaction, so both statements are rejected there.-This does not add Data API session reuse or asynchronous execution. The legacy Phase 1 catalog+Data API session reuse is not supported. The legacy Phase 1 catalogis not the Glue-backed relational path.## Configuration@@ -77,7 +87,10 @@ is not the Glue-backed relational path.| Variable | Default | Description ||---|---|---|| `FLOCI_SERVICES_REDSHIFT_DATA_ENABLED` | `true` | Enable or disable the Redshift Data API service |-| `FLOCI_SERVICES_REDSHIFT_DATA_RESULT_TTL_HOURS` | `24` | Hours a finished statement and its result set are kept in memory before the sweep evicts them |+| `FLOCI_SERVICES_REDSHIFT_DATA_RESULT_TTL_HOURS` | `24` | Hours terminal statement metadata and results are retained before eviction |+| `FLOCI_SERVICES_REDSHIFT_DATA_MAX_CONCURRENT_STATEMENTS` | `10` | Number of shared SQL workers |+| `FLOCI_SERVICES_REDSHIFT_DATA_QUEUE_CAPACITY` | `100` | Maximum queued statements, also the event delivery queue capacity |+| `FLOCI_SERVICES_REDSHIFT_DATA_SHUTDOWN_TIMEOUT_SECONDS` | `10` | Shared deadline for draining SQL and event delivery on shutdown |The Redshift Data API also requires the Redshift service itself to be enabled, because it resolves `ClusterIdentifier` values to local Redshift clusters.@@ -97,7 +110,7 @@ STATEMENT_ID=$(aws redshift-data execute-statement \--cluster-identifier wh \--db-user admin \--database dev \- --sql "create table t (id int, name varchar(20))" \+ --sql "create table t (id int, name varchar(20))" --wait-time-seconds 30 \--query Id --output text \--endpoint-url "$AWS_ENDPOINT_URL")@@ -105,12 +118,12 @@ aws redshift-data describe-statement --id "$STATEMENT_ID" --endpoint-url "$AWS_Eaws redshift-data execute-statement \--cluster-identifier wh --db-user admin --database dev \- --sql "insert into t values (1, 'a'), (2, 'b')" \+ --sql "insert into t values (1, 'a'), (2, 'b')" --wait-time-seconds 30 \--endpoint-url "$AWS_ENDPOINT_URL"SELECT_ID=$(aws redshift-data execute-statement \--cluster-identifier wh --db-user admin --database dev \- --sql "select id, name from t order by id" \+ --sql "select id, name from t order by id" --wait-time-seconds 30 \--query Id --output text \--endpoint-url "$AWS_ENDPOINT_URL")@@ -118,6 +131,7 @@ aws redshift-data get-statement-result --id "$SELECT_ID" --endpoint-url "$AWS_EN``````python+import timeimport boto3data = boto3.client("redshift-data", endpoint_url="http://localhost:4566")@@ -130,7 +144,7 @@ started = data.execute_statement(statement_id = started["Id"]while data.describe_statement(Id=statement_id)["Status"] not in ("FINISHED", "FAILED", "ABORTED"):- pass+ time.sleep(0.2)result = data.get_statement_result(Id=statement_id)print(result["ColumnMetadata"], result["Records"])diff --git a/docs/services/scheduler.md b/docs/services/scheduler.mdindex 90684c7c5c..5f6d006cde 100644--- a/docs/services/scheduler.md+++ b/docs/services/scheduler.md@@ -74,8 +74,10 @@ Universal targets (`arn:aws:scheduler:::aws-sdk:<service>:<action>`) deliver `sn`sqs:sendMessage`, `redshiftdata:executeStatement` and `redshiftdata:batchExecuteStatement`; anyother action fails when the schedule fires. The Redshift Data targets take the Data API requestas `Input` (PascalCase) and run the SQL against a provisioned cluster (`ClusterIdentifier`) or a-Serverless workgroup (`WorkgroupName`). The SQL runs on its own thread, so a slow statement does not-delay other schedules. Delivery succeeds once the Data API accepts the request;+Serverless workgroup (`WorkgroupName`). The SQL runs on the shared bounded Data API worker pool, so a slow statement does not+delay other schedules. Pool saturation rejects admission and follows Scheduler retries.+`WithEvent=true` emits a terminal EventBridge event in the schedule account and region,+with the target role ARN as `principal`. Delivery succeeds once the Data API accepts the request;a SQL error is recorded on the statement and does not trigger Scheduler retries, while a requestthe Data API rejects (an unknown cluster, a malformed request) is retried like any faileddelivery. When `redshift` or `redshift-data` is disabled the target fails withdiff --git a/docs/services/step-functions.md b/docs/services/step-functions.mdindex 604bded197..bb1edaae2a 100644--- a/docs/services/step-functions.md+++ b/docs/services/step-functions.md@@ -500,6 +500,12 @@ the wire and the task fails with `Sfn.StateMachineDoesNotExistException`.| `arn:aws:states:::aws-sdk:scheduler:deleteSchedule` | `{}` | `Scheduler.ResourceNotFoundException` || `arn:aws:states:::aws-sdk:sns:publish` | `{MessageId}` | `Sns.NotFoundException` when the topic does not exist |+Redshift Data API execution returns a statement `Id` before SQL completes. Follow the+`executeStatement` task with `describeStatement`, a Choice on `Status`, and a Wait state+before polling again. Read results only after `FINISHED`; route `FAILED` and `ABORTED`+to the workflow's failure path. `WithEvent=true` can also route the terminal status+through EventBridge.+Scheduler create and update tasks accept `StartDate` and `EndDate` as RFC 3339 strings, includingoffsets and fractional seconds. The direct Scheduler API continues to use numeric epoch seconds.Structured JSON values supplied as `Target.Input` are serialized once to the Scheduler API's stringdiff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 4f8d5d55db..daf74838b4 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -1706,6 +1706,15 @@ public interface EmulatorConfig {@WithDefault("24")int resultTtlHours();++ @WithDefault("10")+ int maxConcurrentStatements();++ @WithDefault("100")+ int queueCapacity();++ @WithDefault("10")+ int shutdownTimeoutSeconds();}interface RedshiftServerlessServiceConfig {diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataBackendSql.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataBackendSql.javaindex 5b697b3bf4..6f4cd1c5ab 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataBackendSql.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataBackendSql.java@@ -12,15 +12,30 @@ import java.sql.Statement;final class RedshiftDataBackendSql implements BackendSql {private final Connection connection;+ private final RedshiftDataExecution execution;RedshiftDataBackendSql(Connection connection) {+ this(connection, null);+ }++ RedshiftDataBackendSql(Connection connection, RedshiftDataExecution execution) {this.connection = connection;+ this.execution = execution;}@Overridepublic void execute(String sql) {try (Statement statement = connection.createStatement()) {- statement.execute(sql);+ if (execution != null) {+ execution.track(statement);+ }+ try {+ statement.execute(sql);+ } finally {+ if (execution != null) {+ execution.untrack(statement);+ }+ }} catch (SQLException exception) {throw failure(exception);}@@ -29,6 +44,9 @@ final class RedshiftDataBackendSql implements BackendSql {@Overridepublic long copyIn(String copySql, InputStream data) {try {+ if (execution != null) {+ execution.checkCancellation();+ }return connection.unwrap(PGConnection.class).getCopyAPI().copyIn(copySql, data);} catch (SQLException exception) {throw failure(exception);diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataConnectionFactory.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataConnectionFactory.javaindex d9af592fc0..fa941eea85 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataConnectionFactory.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataConnectionFactory.java@@ -1,15 +1,17 @@package io.github.hectorvent.floci.services.redshiftdata;import jakarta.enterprise.context.ApplicationScoped;+import org.postgresql.Driver;import java.sql.Connection;-import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;@ApplicationScopedclass RedshiftDataConnectionFactory {+ private final Driver driver = new Driver();+Connection open(RedshiftDataResourceResolver.DatabaseTarget target) throws SQLException {// stringtype=unspecified: Redshift Data API parameter values are always strings on the// wire, so every bind goes through PreparedStatement.setString. With the default@@ -22,6 +24,6 @@ class RedshiftDataConnectionFactory {props.setProperty("user", target.user());props.setProperty("password", target.password());props.setProperty("connectTimeout", "5");- return DriverManager.getConnection(url, props);+ return driver.connect(url, props);}}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventPublisher.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventPublisher.javanew file mode 100644index 0000000000..4323b7f8ff--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventPublisher.java@@ -0,0 +1,66 @@+package io.github.hectorvent.floci.services.redshiftdata;++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.ServiceRegistry;+import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;+import org.jboss.logging.Logger;++import java.util.HashMap;+import java.util.List;+import java.util.Map;++@ApplicationScoped+class RedshiftDataEventPublisher {+ private static final Logger LOG = Logger.getLogger(RedshiftDataEventPublisher.class);++ private final EventBridgeService events;+ private final ObjectMapper mapper;+ private final ServiceRegistry registry;++ @Inject+ RedshiftDataEventPublisher(EventBridgeService events, ObjectMapper mapper, ServiceRegistry registry) {+ this.events = events;+ this.mapper = mapper;+ this.registry = registry;+ }++ void publish(RedshiftDataStatementStore.StoredStatement statement) {+ try {+ if (!registry.isServiceEnabled("eventbridge")) {+ LOG.warnv("EventBridge disabled for statement {0}, account {1}, region {2}",+ statement.id, statement.accountId, statement.region);+ return;+ }+ ObjectNode detail = mapper.createObjectNode();+ detail.put("statementId", statement.id);+ if (statement.statementName != null) {+ detail.put("statementName", statement.statementName);+ }+ if (statement.principal != null) {+ detail.put("principal", statement.principal);+ }+ detail.put("redshiftQueryId", (long) (statement.id.hashCode() & Integer.MAX_VALUE));+ detail.put("state", statement.status.name());+ detail.put("rows", statement.resultRows);+ detail.put("expireAt", statement.expiresAt.getEpochSecond());+ Map<String, Object> entry = new HashMap<>();+ entry.put("Source", "aws.redshift-data");+ entry.put("DetailType", "Redshift Data Statement Status Change");+ entry.put("Detail", detail.toString());+ entry.put("EventBusName", "default");+ entry.put("Resources", mapper.createArrayNode().add(statement.resourceArn));+ EventBridgeService.PutEventsResult result = events.putEvents(List.of(entry), statement.region, statement.accountId);+ if (result.failedCount() > 0) {+ LOG.warnv("EventBridge rejected event for statement {0}, account {1}, region {2}: {3}",+ statement.id, statement.accountId, statement.region, result.entries());+ }+ } catch (AwsException rejected) {+ LOG.warnv(rejected, "EventBridge refused event for statement {0}, account {1}, region {2}",+ statement.id, statement.accountId, statement.region);+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecution.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecution.javanew file mode 100644index 0000000000..582dd51580--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecution.java@@ -0,0 +1,165 @@+package io.github.hectorvent.floci.services.redshiftdata;++import org.jboss.logging.Logger;+import org.postgresql.PGConnection;++import java.sql.Connection;+import java.sql.SQLException;+import java.sql.Statement;+import java.time.Duration;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;++final class RedshiftDataExecution {+ private static final Logger LOG = Logger.getLogger(RedshiftDataExecution.class);++ private final CountDownLatch done = new CountDownLatch(1);+ private final long generation;+ private Statement statement;+ private Connection connection;+ private boolean cancellationRequested;+ private boolean committed;+ private boolean committing;+ private boolean terminal;+ private volatile boolean valid = true;+ private Runnable task;++ RedshiftDataExecution(long generation) {+ this.generation = generation;+ }++ long generation() {+ return generation;+ }++ synchronized void track(Connection active) throws SQLException {+ checkCancellation();+ connection = active;+ }++ synchronized void track(Statement active) throws SQLException {+ checkCancellation();+ statement = active;+ }++ synchronized void untrack(Statement active) {+ if (statement == active) {+ statement = null;+ }+ }++ synchronized void checkCancellation() throws SQLException {+ if (cancellationRequested || !valid) {+ throw new SQLException("Statement cancelled", "57014");+ }+ }++ void commit(SqlAction action) throws SQLException {+ synchronized (this) {+ checkCancellation();+ committing = true;+ }+ try {+ action.run();+ synchronized (this) {+ committed = true;+ }+ } finally {+ synchronized (this) {+ committing = false;+ }+ }+ }++ boolean cancel() {+ if (!requestCancellation()) {+ return false;+ }+ signalCancellation();+ Thread.startVirtualThread(() -> {+ while (isCancellationRequested() && !isDone()) {+ await(Duration.ofMillis(100));+ if (!isDone()) {+ signalCancellation();+ }+ }+ });+ return true;+ }++ synchronized boolean requestCancellation() {+ if (terminal || committed || committing || cancellationRequested) {+ return false;+ }+ cancellationRequested = true;+ return true;+ }++ void signalCancellation() {+ Statement activeStatement;+ Connection activeConnection;+ synchronized (this) {+ activeStatement = statement;+ activeConnection = connection;+ }+ try {+ if (activeStatement != null) {+ activeStatement.cancel();+ } else if (activeConnection != null && activeConnection.isWrapperFor(PGConnection.class)) {+ activeConnection.unwrap(PGConnection.class).cancelQuery();+ }+ } catch (SQLException failure) {+ // The worker still observes cancellation before commit and rolls back its transaction.+ LOG.warnv(failure, "Unable to signal JDBC cancellation; worker will abort before commit");+ }+ }++ synchronized boolean isCancellationRequested() {+ return cancellationRequested;+ }++ synchronized void terminal() {+ terminal = true;+ statement = null;+ connection = null;+ }++ void completed() {+ terminal();+ done.countDown();+ }++ void await(Duration timeout) {+ try {+ done.await(timeout.toNanos(), TimeUnit.NANOSECONDS);+ } catch (InterruptedException expected) {+ // End the bounded wait and preserve interruption for the caller.+ Thread.currentThread().interrupt();+ }+ }++ boolean isDone() {+ return done.getCount() == 0;+ }++ boolean isValid() {+ return valid;+ }++ void invalidate() {+ valid = false;+ }++ void task(Runnable runnable) {+ task = runnable;+ }++ Runnable task() {+ return task;+ }++ @FunctionalInterface+ interface SqlAction {+ void run() throws SQLException;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutor.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutor.javanew file mode 100644index 0000000000..c3fbcd49b2--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutor.java@@ -0,0 +1,235 @@+package io.github.hectorvent.floci.services.redshiftdata;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.RequestScopes;+import jakarta.annotation.PreDestroy;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;+import org.jboss.logging.Logger;++import java.time.Duration;+import java.util.ArrayList;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import java.util.concurrent.ArrayBlockingQueue;+import java.util.concurrent.RejectedExecutionException;+import java.util.concurrent.ThreadPoolExecutor;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.locks.ReentrantReadWriteLock;+import java.util.function.BiConsumer;+import java.util.function.Consumer;++@ApplicationScoped+class RedshiftDataExecutor implements AutoCloseable {+ private static final Logger LOG = Logger.getLogger(RedshiftDataExecutor.class);++ private final ThreadPoolExecutor workers;+ private final ThreadPoolExecutor deliveries;+ private final Map<String, RedshiftDataExecution> executions = new HashMap<>();+ private final int shutdownSeconds;+ // Status writes hold the read lock; reset holds the write lock, so no write lands after a reset.+ private final ReentrantReadWriteLock resetLock = new ReentrantReadWriteLock();+ private volatile boolean closed;+ private volatile long generation;++ @Inject+ RedshiftDataExecutor(EmulatorConfig config) {+ this(config.services().redshiftData().maxConcurrentStatements(),+ config.services().redshiftData().queueCapacity(),+ config.services().redshiftData().shutdownTimeoutSeconds());+ }++ RedshiftDataExecutor(int concurrency, int capacity, int shutdownSeconds) {+ if (concurrency <= 0 || capacity <= 0 || shutdownSeconds < 0) {+ throw new IllegalArgumentException("Invalid Redshift Data executor limits");+ }+ this.shutdownSeconds = shutdownSeconds;+ workers = pool(concurrency, capacity, "redshift-data-sql");+ deliveries = pool(2, capacity, "redshift-data-events");+ }++ private static ThreadPoolExecutor pool(int concurrency, int capacity, String name) {+ return new ThreadPoolExecutor(concurrency, concurrency, 0, TimeUnit.SECONDS,+ new ArrayBlockingQueue<>(capacity), runnable -> {+ Thread thread = new Thread(runnable, name);+ thread.setDaemon(true);+ return thread;+ }, new ThreadPoolExecutor.AbortPolicy());+ }++ RedshiftDataExecution submit(String account, String region, String id,+ Consumer<RedshiftDataExecution> work) {+ return submit(account, region, id, () -> { }, work, (execution, failure) -> { });+ }++ /**+ * Admits a statement. {@code admit} runs under the reset read lock, so a reset cannot slip+ * between persisting the statement and registering its execution. {@code onFailure} runs when+ * the worker dies with an unexpected exception, so the statement can still reach a terminal state.+ */+ RedshiftDataExecution submit(String account, String region, String id, Runnable admit,+ Consumer<RedshiftDataExecution> work,+ BiConsumer<RedshiftDataExecution, RuntimeException> onFailure) {+ resetLock.readLock().lock();+ try {+ if (closed) {+ throw new AwsException("ActiveStatementsExceededException", "Data API is shutting down", 400);+ }+ String key = account + ":" + id;+ RedshiftDataExecution execution = new RedshiftDataExecution(generation);+ Runnable task = () -> {+ try {+ RequestScopes.runAs(account, region, () -> work.accept(execution));+ } catch (RuntimeException failure) {+ LOG.errorv(failure, "Redshift Data worker failed for statement {0} in account {1}", id, account);+ recover(execution, failure, onFailure, id, account, region);+ } finally {+ execution.completed();+ synchronized (this) {+ executions.remove(key, execution);+ }+ }+ };+ execution.task(task);+ admit.run();+ synchronized (this) {+ executions.put(key, execution);+ }+ try {+ workers.execute(task);+ } catch (RejectedExecutionException failure) {+ synchronized (this) {+ executions.remove(key, execution);+ }+ throw new AwsException("ActiveStatementsExceededException", "Too many active statements", 400);+ }+ return execution;+ } finally {+ resetLock.readLock().unlock();+ }+ }++ private static void recover(RedshiftDataExecution execution, RuntimeException failure,+ BiConsumer<RedshiftDataExecution, RuntimeException> onFailure,+ String id, String account, String region) {+ try {+ RequestScopes.runAs(account, region, () -> onFailure.accept(execution, failure));+ } catch (RuntimeException secondary) {+ LOG.errorv(secondary, "Unable to mark statement {0} in account {1} failed after a worker error", id, account);+ }+ }++ boolean cancel(String account, String id) {+ RedshiftDataExecution execution;+ synchronized (this) {+ execution = executions.get(account + ":" + id);+ }+ if (execution == null || !execution.cancel()) {+ return false;+ }+ if (workers.remove(execution.task())) {+ execution.task().run();+ }+ return true;+ }++ void publish(RedshiftDataExecution execution, Runnable write) {+ resetLock.readLock().lock();+ try {+ if (execution.isValid() && execution.generation() == generation) {+ write.run();+ }+ } finally {+ resetLock.readLock().unlock();+ }+ }++ void deliver(RedshiftDataExecution execution, String account, String region,+ String id, Runnable delivery) {+ resetLock.readLock().lock();+ try {+ if (!execution.isValid() || execution.generation() != generation) {+ return;+ }+ // Delivery runs outside the lock so a slow EventBridge call cannot block a reset.+ deliveries.execute(() -> RequestScopes.runAs(account, region, () -> {+ if (execution.isValid() && execution.generation() == generation) {+ try {+ delivery.run();+ } catch (RuntimeException failure) {+ // The SQL outcome is already stored; log the lost event at error level and keep the pool alive.+ LOG.errorv(failure, "Event delivery failed for statement {0}, account {1}, region {2}",+ id, account, region);+ }+ }+ }));+ } catch (RejectedExecutionException failure) {+ LOG.warnv("Event delivery rejected for statement {0}, account {1}, region {2}", id, account, region);+ } finally {+ resetLock.readLock().unlock();+ }+ }++ void clear(Runnable clearStorage) {+ List<RedshiftDataExecution> active;+ resetLock.writeLock().lock();+ try {+ generation++;+ synchronized (this) {+ active = new ArrayList<>(executions.values());+ executions.clear();+ }+ for (RedshiftDataExecution execution : active) {+ execution.invalidate();+ }+ clearStorage.run();+ } finally {+ resetLock.writeLock().unlock();+ }+ for (RedshiftDataExecution execution : active) {+ if (execution.requestCancellation()) {+ Thread.startVirtualThread(execution::signalCancellation);+ }+ if (workers.remove(execution.task())) {+ execution.completed();+ }+ }+ }++ @Override+ @PreDestroy+ public void close() {+ synchronized (this) {+ closed = true;+ }+ long deadline = System.nanoTime() + Duration.ofSeconds(shutdownSeconds).toNanos();+ workers.shutdown();+ waitUntil(workers, deadline);+ List<RedshiftDataExecution> active;+ synchronized (this) {+ active = new ArrayList<>(executions.values());+ }+ for (RedshiftDataExecution execution : active) {+ if (execution.requestCancellation()) {+ Thread.startVirtualThread(execution::signalCancellation);+ }+ }+ for (Runnable queued : workers.shutdownNow()) {+ queued.run();+ }+ deliveries.shutdown();+ waitUntil(deliveries, deadline);+ deliveries.shutdownNow();+ }++ private static void waitUntil(ThreadPoolExecutor pool, long deadline) {+ try {+ pool.awaitTermination(Math.max(0, deadline - System.nanoTime()), TimeUnit.NANOSECONDS);+ } catch (InterruptedException expected) {+ // Continue shutdown cleanup while preserving the caller's interruption.+ Thread.currentThread().interrupt();+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.javaindex 2c215bd2af..3c0a8333fe 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java@@ -4,9 +4,12 @@ import com.fasterxml.jackson.databind.JsonNode;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.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.RequestContext;import io.github.hectorvent.floci.core.common.Resettable;import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;+import io.github.hectorvent.floci.services.iam.IamService;import io.github.hectorvent.floci.services.redshift.spectrum.BackendSql;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumQueryPreparation;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumReadException;@@ -21,6 +24,7 @@ import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;+import java.time.Duration;import java.time.Instant;import java.util.ArrayList;import java.util.Base64;@@ -28,59 +32,161 @@ import java.util.Comparator;import java.util.List;import java.util.Map;import java.util.UUID;+import java.util.function.Consumer;+import java.util.regex.Pattern;@ApplicationScopedpublic class RedshiftDataService implements Resettable {private static final int PAGE_SIZE = 1000;+ static final Pattern AUTO_COMMIT_SQL = Pattern.compile(+ "(?is)\\A(?:\\s+|--[^\\r\\n]*(?:\\r?\\n|$)|/\\*.*?\\*/)*"+ + "(?:VACUUM\\b|(?:CREATE|DROP)\\s+(?:DATABASE|TABLESPACE)\\b|ALTER\\s+SYSTEM\\b"+ + "|CREATE\\s+(?:UNIQUE\\s+)?INDEX\\s+CONCURRENTLY\\b|DROP\\s+INDEX\\s+CONCURRENTLY\\b"+ + "|REINDEX\\b[^;]*?\\bCONCURRENTLY\\b)");private final RedshiftDataResourceResolver resolver;private final RedshiftDataConnectionFactory connectionFactory;private final RedshiftDataStatementStore store;private final ObjectMapper objectMapper;private final SpectrumQueryPreparation preparation;+ private final RedshiftDataExecutor executor;+ private final RedshiftDataEventPublisher publisher;+ private final RequestContext requestContext;+ private final IamService iamService;- public RedshiftDataService(RedshiftDataResourceResolver resolver,- RedshiftDataConnectionFactory connectionFactory,- RedshiftDataStatementStore store,- ObjectMapper objectMapper) {- this(resolver, connectionFactory, store, objectMapper, null);+ RedshiftDataService(RedshiftDataResourceResolver resolver, RedshiftDataConnectionFactory connectionFactory,+ RedshiftDataStatementStore store, ObjectMapper objectMapper, SpectrumQueryPreparation preparation,+ RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher) {+ this(resolver, connectionFactory, store, objectMapper, preparation, executor, publisher, null, null);}@Inject- public RedshiftDataService(RedshiftDataResourceResolver resolver,- RedshiftDataConnectionFactory connectionFactory,- RedshiftDataStatementStore store, ObjectMapper objectMapper,- SpectrumQueryPreparation preparation) {+ public RedshiftDataService(RedshiftDataResourceResolver resolver, RedshiftDataConnectionFactory connectionFactory,+ RedshiftDataStatementStore store, ObjectMapper objectMapper, SpectrumQueryPreparation preparation,+ RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher,+ RequestContext requestContext, IamService iamService) {this.resolver = resolver;this.connectionFactory = connectionFactory;this.store = store;this.objectMapper = objectMapper;this.preparation = preparation;+ this.executor = executor;+ this.publisher = publisher;+ this.requestContext = requestContext;+ this.iamService = iamService;}@Overridepublic void clear() {- store.clear();+ executor.clear(store::clear);}// ── ExecuteStatement ────────────────────────────────────────────────────public ObjectNode executeStatement(JsonNode request, String region) {- PendingStatement pending = prepareStatement(request, region);- pending.run().run();- return executeResponse(pending.statement());+ return executeStatement(request, region, null);}- /**- * Validates and resolves the request on the calling thread, so a rejected request throws here,- * and returns the SQL run for the caller to schedule. The statement is stored as {@code STARTED}- * first, as AWS does, so DescribeStatement can see it while the run is in flight.- */- public Runnable submitStatement(JsonNode request, String region) {- PendingStatement pending = prepareStatement(request, region);- store.put(pending.statement());- return pending.run();+ public ObjectNode executeStatement(JsonNode request, String region, String principal) {+ int waitSeconds = waitSeconds(request);+ return submit(prepareStatement(request, region), waitSeconds, principal);+ }++ private ObjectNode submit(PendingStatement pending, int waitSeconds, String principal) {+ RedshiftDataStatementStore.StoredStatement stored = pending.statement();+ if (principal != null) {+ stored.principal = principal;+ }+ ObjectNode response = executeResponse(stored.snapshot());+ RedshiftDataExecution handle;+ try {+ handle = executor.submit(stored.accountId, stored.region, stored.id,+ () -> store.putForAccount(stored.accountId, stored),+ execution -> {+ try {+ execution.checkCancellation();+ stored.status = RedshiftDataStatementStore.Status.STARTED;+ executor.publish(execution, () -> store.putForAccount(stored.accountId, stored));+ pending.run().accept(execution);+ } catch (Exception failure) {+ stored.status = RedshiftDataStatementStore.Status.FAILED;+ stored.error = failure.getMessage();+ }+ complete(stored, execution);+ },+ (execution, failure) -> recoverStatement(stored, execution, failure));+ } catch (AwsException failure) {+ store.deleteForAccount(stored.accountId, stored.id);+ throw failure;+ }+ if (waitSeconds > 0) {+ handle.await(Duration.ofSeconds(waitSeconds));+ RedshiftDataStatementStore.StoredStatement current = store.getForAccount(stored.accountId, stored.id);+ if (current != null) {+ return executeResponse(current);+ }+ }+ return response;+ }++ private void complete(RedshiftDataStatementStore.StoredStatement stored, RedshiftDataExecution execution) {+ execution.terminal();+ if (execution.isCancellationRequested()) {+ stored.status = RedshiftDataStatementStore.Status.ABORTED;+ stored.error = "Statement cancelled";+ }+ if (stored.status != RedshiftDataStatementStore.Status.FINISHED) {+ stored.hasResultSet = false;+ stored.rows = null;+ stored.resultRows = 0;+ stored.resultSize = 0;+ }+ stored.updatedAt = Instant.now();+ executor.publish(execution, () -> {+ store.putForAccount(stored.accountId, stored);+ if (stored.withEvent && publisher != null) {+ RedshiftDataStatementStore.StoredStatement terminal = stored.snapshot();+ executor.deliver(execution, stored.accountId, stored.region, stored.id,+ () -> publisher.publish(terminal));+ }+ });+ }++ /** Last resort for a worker that died outside the statement handler: never leave a statement active. */+ private void recoverStatement(RedshiftDataStatementStore.StoredStatement stored,+ RedshiftDataExecution execution, RuntimeException failure) {+ RedshiftDataStatementStore.StoredStatement persisted = store.getForAccount(stored.accountId, stored.id);+ if (persisted != null && persisted.status != null && persisted.status.isTerminal()) {+ return;+ }+ stored.status = RedshiftDataStatementStore.Status.FAILED;+ stored.error = "Internal error: " + failure.getMessage();+ complete(stored, execution);+ }++ private static int waitSeconds(JsonNode request) {+ if (!request.has("WaitTimeSeconds")) {+ return 0;+ }+ JsonNode wait = request.get("WaitTimeSeconds");+ if (!wait.isIntegralNumber() || !wait.canConvertToInt() || wait.asInt() < 1 || wait.asInt() > 30) {+ throw new AwsException("ValidationException", "WaitTimeSeconds must be an integer between 1 and 30", 400);+ }+ return wait.asInt();+ }++ private void capture(RedshiftDataStatementStore.StoredStatement stored,+ RedshiftDataResourceResolver.DatabaseTarget target, String region, JsonNode request) {+ AwsArnUtils.Arn arn = AwsArnUtils.parse(target.arn());+ stored.accountId = arn.accountId();+ stored.region = region;+ stored.resourceArn = target.arn();+ stored.withEvent = request.path("WithEvent").asBoolean(false);+ stored.expiresAt = store.expiresAt(stored.createdAt);+ if (stored.withEvent && requestContext != null && requestContext.getAccessKeyId() != null) {+ stored.principal = iamService.resolveCallerArn(requestContext.getAccessKeyId()).orElse(null);+ }}private PendingStatement prepareStatement(JsonNode request, String region) {@@ -98,20 +204,40 @@ public class RedshiftDataService implements Resettable {stored.dbUser = target.user();stored.database = target.database();stored.resultFormat = request.path("ResultFormat").asText("JSON");+ capture(stored, target, region, request);- return new PendingStatement(stored, () -> {- runStatement(stored, target, sql, parameters);- store.put(stored);- });+ return new PendingStatement(stored, execution -> runStatement(stored, target, sql, parameters, execution));}- private record PendingStatement(RedshiftDataStatementStore.StoredStatement statement, Runnable run) { }+ private record PendingStatement(RedshiftDataStatementStore.StoredStatement statement,+ Consumer<RedshiftDataExecution> run) { }private void runStatement(RedshiftDataStatementStore.StoredStatement stored,RedshiftDataResourceResolver.DatabaseTarget target,- String sql, Map<String, String> parameters) {+ String sql, Map<String, String> parameters, RedshiftDataExecution execution) {try (Connection connection = connectionFactory.open(target)) {- runOnConnection(stored, connection, sql, parameters, target.spectrum());+ execution.track(connection);+ try {+ boolean autoCommit = AUTO_COMMIT_SQL.matcher(sql).find()+ || preparation != null && target.spectrum() != null+ && preparation.handlesDdl(sql, target.spectrum());+ if (autoCommit) {+ execution.commit(() -> runOnConnection(stored, connection, sql, parameters,+ target.spectrum(), execution, true));+ } else {+ runOnConnection(stored, connection, sql, parameters, target.spectrum(), execution, false);+ execution.commit(() -> {+ if (!connection.getAutoCommit()) {+ connection.commit();+ }+ });+ }+ } catch (SQLException failure) {+ if (!connection.getAutoCommit()) {+ connection.rollback();+ }+ throw failure;+ }} catch (SQLException e) {stored.status = RedshiftDataStatementStore.Status.FAILED;stored.error = e.getMessage();@@ -138,13 +264,14 @@ public class RedshiftDataService implements Resettable {}private void runOnConnection(RedshiftDataStatementStore.StoredStatement stored,- Connection connection, String sql, Map<String, String> parameters, SpectrumSession context)+ Connection connection, String sql, Map<String, String> parameters, SpectrumSession context,+ RedshiftDataExecution execution, boolean autoCommit)throws SQLException {if (preparation != null && context != null) {SpectrumSession session = new SpectrumSession(context.accountId(), context.clusterKey(), context.databaseName(),context.iamRoleArns(), !connection.getAutoCommit());try {- if (preparation.prepare(sql, session, new RedshiftDataBackendSql(connection))) {+ if (preparation.prepare(sql, session, new RedshiftDataBackendSql(connection, execution))) {stored.status = RedshiftDataStatementStore.Status.FINISHED;return;}@@ -158,22 +285,32 @@ public class RedshiftDataService implements Resettable {}}ParsedSql parsed = RedshiftDataSqlParameters.parse(sql);+ execution.checkCancellation();+ if (!autoCommit) {+ connection.setAutoCommit(false);+ }long t0 = System.nanoTime();try (PreparedStatement statement = connection.prepareStatement(parsed.sql())) {- RedshiftDataSqlParameters.bind(statement, parsed.parameterOrder(), parameters);- boolean hasResultSet = statement.execute();- if (hasResultSet) {- try (ResultSet rs = statement.getResultSet()) {- rejectResultTypesRedshiftLacks(rs.getMetaData());- stored.columnMetadata = RedshiftDataColumnMetadata.toColumnMetadata(objectMapper, rs.getMetaData());- stored.rows = RedshiftDataFieldMapper.rows(objectMapper, rs);- stored.hasResultSet = true;- stored.resultRows = stored.rows.size();- stored.resultSize = RedshiftDataFieldMapper.serializedSize(stored.rows);+ execution.track(statement);+ try {+ RedshiftDataSqlParameters.bind(statement, parsed.parameterOrder(), parameters);+ execution.checkCancellation();+ boolean hasResultSet = statement.execute();+ if (hasResultSet) {+ try (ResultSet rs = statement.getResultSet()) {+ rejectResultTypesRedshiftLacks(rs.getMetaData());+ stored.columnMetadata = RedshiftDataColumnMetadata.toColumnMetadata(objectMapper, rs.getMetaData());+ stored.rows = RedshiftDataFieldMapper.rows(objectMapper, rs);+ stored.hasResultSet = true;+ stored.resultRows = stored.rows.size();+ stored.resultSize = RedshiftDataFieldMapper.serializedSize(stored.rows);+ }+ } else {+ stored.hasResultSet = false;+ stored.resultRows = Math.max(statement.getUpdateCount(), 0);}- } else {- stored.hasResultSet = false;- stored.resultRows = Math.max(statement.getUpdateCount(), 0);+ } finally {+ execution.untrack(statement);}}stored.durationNanos = System.nanoTime() - t0;@@ -336,16 +473,12 @@ public class RedshiftDataService implements Resettable {// ── BatchExecuteStatement ──────────────────────────────────────────────public ObjectNode batchExecuteStatement(JsonNode request, String region) {- PendingStatement pending = prepareBatch(request, region);- pending.run().run();- return executeResponse(pending.statement());+ return batchExecuteStatement(request, region, null);}- /** Batch counterpart of {@link #submitStatement}. */- public Runnable submitBatch(JsonNode request, String region) {- PendingStatement pending = prepareBatch(request, region);- store.put(pending.statement());- return pending.run();+ public ObjectNode batchExecuteStatement(JsonNode request, String region, String principal) {+ int waitSeconds = waitSeconds(request);+ return submit(prepareBatch(request, region), waitSeconds, principal);}private PendingStatement prepareBatch(JsonNode request, String region) {@@ -370,15 +503,17 @@ public class RedshiftDataService implements Resettable {parent.database = target.database();parent.resultFormat = request.path("ResultFormat").asText("JSON");parent.subStatements = new ArrayList<>();+ capture(parent, target, region, request);- return new PendingStatement(parent, () -> runBatch(parent, sqls, target));+ return new PendingStatement(parent, execution -> runBatch(parent, sqls, target, execution));}private void runBatch(RedshiftDataStatementStore.StoredStatement parent, List<String> sqls,- RedshiftDataResourceResolver.DatabaseTarget target) {+ RedshiftDataResourceResolver.DatabaseTarget target, RedshiftDataExecution execution) {long totalDuration = 0;try (Connection connection = connectionFactory.open(target)) {- BackendSql batchBackend = new RedshiftDataBackendSql(connection);+ execution.track(connection);+ BackendSql batchBackend = new RedshiftDataBackendSql(connection, execution);boolean committed = false;try {connection.setAutoCommit(false);@@ -397,13 +532,14 @@ public class RedshiftDataService implements Resettable {sub.createdAt = now;sub.updatedAt = now;try {- runOnConnection(sub, connection, sqls.get(n), Map.of(), target.spectrum());+ execution.checkCancellation();+ runOnConnection(sub, connection, sqls.get(n), Map.of(), target.spectrum(), execution, false);} catch (SQLException e) {sub.status = RedshiftDataStatementStore.Status.FAILED;sub.error = e.getMessage();sub.updatedAt = Instant.now();parent.subStatements.add(sub);- store.put(sub);+ executor.publish(execution, () -> store.putForAccount(parent.accountId, sub));totalDuration += sub.durationNanos;parent.status = RedshiftDataStatementStore.Status.FAILED;parent.error = e.getMessage();@@ -413,11 +549,14 @@ public class RedshiftDataService implements Resettable {}sub.updatedAt = Instant.now();parent.subStatements.add(sub);- store.put(sub);+ executor.publish(execution, () -> {+ store.putForAccount(parent.accountId, sub);+ store.putForAccount(parent.accountId, parent);+ });totalDuration += sub.durationNanos;}if (!failed) {- connection.commit();+ execution.commit(connection::commit);committed = true;parent.status = RedshiftDataStatementStore.Status.FINISHED;}@@ -448,7 +587,6 @@ public class RedshiftDataService implements Resettable {}parent.durationNanos = totalDuration;parent.updatedAt = Instant.now();- store.put(parent);}// ── ListStatements ─────────────────────────────────────────────────────@@ -506,13 +644,13 @@ public class RedshiftDataService implements Resettable {public ObjectNode cancelStatement(JsonNode request) {RedshiftDataStatementStore.StoredStatement stored = require(request);- if (stored.status != RedshiftDataStatementStore.Status.FINISHED- && stored.status != RedshiftDataStatementStore.Status.FAILED) {- stored.status = RedshiftDataStatementStore.Status.ABORTED;- stored.updatedAt = Instant.now();+ if (stored.status.isTerminal()) {+ throw new AwsException("ValidationException",+ "Could not cancel a query that is already in " + stored.status.name() + " state with ID: " + stored.id,+ 400);}ObjectNode response = objectMapper.createObjectNode();- response.put("Status", true);+ response.put("Status", executor.cancel(stored.accountId, stored.id));return response;}@@ -700,7 +838,7 @@ public class RedshiftDataService implements Resettable {Instant now = Instant.now();stored.createdAt = now;stored.updatedAt = now;- stored.status = RedshiftDataStatementStore.Status.STARTED;+ stored.status = RedshiftDataStatementStore.Status.SUBMITTED;return stored;}@@ -715,7 +853,14 @@ public class RedshiftDataService implements Resettable {private RedshiftDataStatementStore.StoredStatement resultBearing(JsonNode request) {RedshiftDataStatementStore.StoredStatement stored = require(request);- if (!stored.hasResultSet || stored.rows == null) {+ int childSuffix = stored.id.indexOf(':');+ if (childSuffix >= 0) {+ RedshiftDataStatementStore.StoredStatement parent = store.get(stored.id.substring(0, childSuffix));+ if (parent == null || parent.status != RedshiftDataStatementStore.Status.FINISHED) {+ throw new AwsException("ValidationException", "Statement has no result set", 400);+ }+ }+ if (stored.status != RedshiftDataStatementStore.Status.FINISHED || !stored.hasResultSet || stored.rows == null) {throw new AwsException("ValidationException", "Statement has no result set", 400);}return stored;diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.javaindex 41bbaf8f9d..407b5fffa1 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.java@@ -28,7 +28,13 @@ class RedshiftDataStatementStore {private static final Logger LOG = Logger.getLogger(RedshiftDataStatementStore.class);@RegisterForReflection- enum Status { PICKED, STARTED, FINISHED, FAILED, ABORTED }+ enum Status {+ SUBMITTED, PICKED, STARTED, FINISHED, FAILED, ABORTED;++ boolean isTerminal() {+ return this == FINISHED || this == FAILED || this == ABORTED;+ }+ }// Statements live in the configured storage backend like every other service's state// (memory mode by default, so they are still lost on restart as documented). The TTL@@ -56,6 +62,16 @@ class RedshiftDataStatementStore {@PostConstructvoid start() {+ for (AccountAwareStorageBackend.AccountEntry<StoredStatement> entry+ : statements.scanAllAccountEntries(k -> true)) {+ StoredStatement interrupted = entry.value().snapshot();+ if (interrupted.status != null && !interrupted.status.isTerminal()) {+ interrupted.status = Status.FAILED;+ interrupted.error = "Execution interrupted by emulator restart";+ interrupted.updatedAt = Instant.now(clock);+ statements.putForAccount(entry.accountId(), entry.key(), interrupted);+ }+ }sweeper = Executors.newSingleThreadScheduledExecutor(r -> {Thread t = new Thread(r, "redshift-data-statement-sweep");t.setDaemon(true);@@ -72,15 +88,31 @@ class RedshiftDataStatementStore {}void put(StoredStatement statement) {- statements.put(statement.id, statement);+ statements.put(statement.id, statement.snapshot());+ }++ void putForAccount(String accountId, StoredStatement statement) {+ statements.putForAccount(accountId, statement.id, statement.snapshot());+ }++ void deleteForAccount(String accountId, String id) {+ statements.deleteForAccount(accountId, id);+ }++ Instant expiresAt(Instant createdAt) {+ return createdAt.plus(ttl);}StoredStatement get(String id) {- return statements.get(id).orElse(null);+ return statements.get(id).map(StoredStatement::snapshot).orElse(null);+ }++ StoredStatement getForAccount(String accountId, String id) {+ return statements.getForAccount(accountId, id).map(StoredStatement::snapshot).orElse(null);}List<StoredStatement> values() {- return statements.scan(k -> true);+ return statements.scan(k -> true).stream().map(StoredStatement::summary).toList();}void clear() {@@ -93,7 +125,8 @@ class RedshiftDataStatementStore {// and deletes per account rather than through the current-account view.for (AccountAwareStorageBackend.AccountEntry<StoredStatement> entry: statements.scanAllAccountEntries(k -> true)) {- if (entry.value().createdAt.isBefore(cutoff)) {+ if (entry.value().status != null && entry.value().status.isTerminal()+ && entry.value().createdAt.isBefore(cutoff)) {statements.deleteForAccount(entry.accountId(), entry.key());}}@@ -132,5 +165,58 @@ class RedshiftDataStatementStore {ArrayNode columnMetadata;List<ArrayNode> rows;List<StoredStatement> subStatements;+ String accountId;+ String region;+ String resourceArn;+ String principal;+ boolean withEvent;+ Instant expiresAt;++ /**+ * Independent copy for storage and reads. Result data is written once when the statement+ * completes, so the row nodes and column metadata are shared instead of deep-copied.+ */+ StoredStatement snapshot() {+ return copy(true);+ }++ /** Copy without result data or sub-statements, for listings that only need statement metadata. */+ StoredStatement summary() {+ return copy(false);+ }++ private StoredStatement copy(boolean withResults) {+ StoredStatement copy = new StoredStatement();+ copy.id = id;+ copy.sql = sql;+ copy.sqls = sqls == null ? null : List.copyOf(sqls);+ copy.batch = batch;+ copy.statementName = statementName;+ copy.clusterIdentifier = clusterIdentifier;+ copy.workgroupName = workgroupName;+ copy.database = database;+ copy.dbUser = dbUser;+ copy.resultFormat = resultFormat;+ copy.status = status;+ copy.error = error;+ copy.createdAt = createdAt;+ copy.updatedAt = updatedAt;+ copy.durationNanos = durationNanos;+ copy.hasResultSet = hasResultSet;+ copy.resultRows = resultRows;+ copy.resultSize = resultSize;+ if (withResults) {+ copy.columnMetadata = columnMetadata;+ copy.rows = rows == null ? null : List.copyOf(rows);+ copy.subStatements = subStatements == null ? null : subStatements.stream().map(StoredStatement::snapshot).toList();+ }+ copy.accountId = accountId;+ copy.region = region;+ copy.resourceArn = resourceArn;+ copy.principal = principal;+ copy.withEvent = withEvent;+ copy.expiresAt = expiresAt;+ return copy;+ }}}diff --git a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.javaindex 82671421b2..41940514c9 100644--- a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java+++ b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.scheduler;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;@@ -23,7 +24,6 @@ import io.github.hectorvent.floci.services.sns.SnsService;import io.github.hectorvent.floci.services.sqs.SqsService;import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;-import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;@@ -36,10 +36,6 @@ import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.UUID;-import java.util.concurrent.Executor;-import java.util.concurrent.ExecutorService;-import java.util.concurrent.Executors;-import java.util.function.Supplier;/*** Delivers an EventBridge Scheduler target invocation to the underlying service.@@ -71,7 +67,6 @@ public class ScheduleInvoker {private final String baseUrl;private final String defaultRegion;private final ServiceRegistry serviceRegistry;- private final Executor redshiftDataExecutor;@Injectpublic ScheduleInvoker(SqsService sqsService,@@ -84,21 +79,6 @@ public class ScheduleInvoker {ObjectMapper objectMapper,EmulatorConfig config,ServiceRegistry serviceRegistry) {- this(sqsService, lambdaService, snsService, eventBridgeService, ecsService, stepFunctionsService,- redshiftDataService, objectMapper, config, serviceRegistry, newRedshiftDataExecutor());- }-- ScheduleInvoker(SqsService sqsService,- LambdaService lambdaService,- SnsService snsService,- EventBridgeService eventBridgeService,- EcsService ecsService,- StepFunctionsService stepFunctionsService,- RedshiftDataService redshiftDataService,- ObjectMapper objectMapper,- EmulatorConfig config,- ServiceRegistry serviceRegistry,- Executor redshiftDataExecutor) {this.sqsService = sqsService;this.lambdaService = lambdaService;this.snsService = snsService;@@ -110,22 +90,6 @@ public class ScheduleInvoker {this.baseUrl = config.baseUrl();this.defaultRegion = config.defaultRegion();this.serviceRegistry = serviceRegistry;- this.redshiftDataExecutor = redshiftDataExecutor;- }-- private static ExecutorService newRedshiftDataExecutor() {- return Executors.newCachedThreadPool(r -> {- Thread t = new Thread(r, "scheduler-redshift-data");- t.setDaemon(true);- return t;- });- }-- @PreDestroy- void shutdown() {- if (redshiftDataExecutor instanceof ExecutorService service) {- service.shutdownNow();- }}/**@@ -152,7 +116,7 @@ public class ScheduleInvoker {String input = contextualInput(schedule, scheduledAt, executionId, attemptNumber);if (isUniversalTarget(arn)) {invokeUniversalTarget(arn.substring(arn.indexOf(":aws-sdk:") + ":aws-sdk:".length()),- input, region, schedule.getAccountId());+ input, region, schedule.getAccountId(), target.getRoleArn());return universalTargetRequest(input);}@@ -450,7 +414,7 @@ public class ScheduleInvoker {* statement storage is account-aware and the dispatcher thread carries no request account.*/private void invokeUniversalTarget(String serviceAction, String input, String region,- String scheduleAccountId) {+ String scheduleAccountId, String principal) {JsonNode params;try {params = objectMapper.readTree(input == null || input.isBlank() ? "{}" : input);@@ -485,12 +449,16 @@ public class ScheduleInvoker {}case "redshiftdata:executeStatement" -> {requireRedshiftData();- submitRedshiftData(scheduleAccountId, region, () -> redshiftDataService.submitStatement(params, region));+ JsonNode executeParams = withoutWait(params);+ RequestScopes.callAs(scheduleAccountId, region,+ () -> redshiftDataService.executeStatement(executeParams, region, principal));LOG.debugv("Scheduler submitted to the Redshift Data API (universal target): {0}", serviceAction);}case "redshiftdata:batchExecuteStatement" -> {requireRedshiftData();- submitRedshiftData(scheduleAccountId, region, () -> redshiftDataService.submitBatch(params, region));+ JsonNode batchParams = withoutWait(params);+ RequestScopes.callAs(scheduleAccountId, region,+ () -> redshiftDataService.batchExecuteStatement(batchParams, region, principal));LOG.debugv("Scheduler submitted to the Redshift Data API (universal target): {0}", serviceAction);}default -> throw new UnsupportedOperationException(@@ -498,29 +466,22 @@ public class ScheduleInvoker {}}+ // The shared dispatcher thread must never wait for a statement to finish.+ private static JsonNode withoutWait(JsonNode params) {+ if (params instanceof ObjectNode object && object.has("WaitTimeSeconds")) {+ ObjectNode copy = object.deepCopy();+ copy.remove("WaitTimeSeconds");+ return copy;+ }+ return params;+ }+private void requireRedshiftData() {if (!serviceRegistry.isServiceEnabled("redshift-data")) {throw new AwsException("ServiceNotAvailableException", "Service redshift-data is not enabled.", 400);}}- /**- * The Data API is asynchronous: AWS returns once the statement is submitted. The request check- * runs here, so a rejected request still fails the occurrence and is retried, while the SQL runs- * on its own thread so a slow statement cannot hold up the single dispatcher thread. Both halves- * run as the schedule's account, which neither thread carries.- */- private void submitRedshiftData(String scheduleAccountId, String region, Supplier<Runnable> submit) {- Runnable statement = RequestScopes.callAs(scheduleAccountId, region, submit);- redshiftDataExecutor.execute(() -> {- try {- RequestScopes.runAs(scheduleAccountId, region, statement);- } catch (RuntimeException e) {- LOG.warnv("Scheduled Redshift Data statement failed: {0}", e.getMessage());- }- });- }-private static Map<String, MessageAttributeValue> parseUniversalSqsMessageAttributes(JsonNode attrsNode) {Map<String, MessageAttributeValue> attributes = new HashMap<>();if (attrsNode == null || !attrsNode.isObject()) {diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex bc36c96f70..f639b9608d 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -491,6 +491,9 @@ floci:redshift-data:enabled: trueresult-ttl-hours: 24+ max-concurrent-statements: ${FLOCI_SERVICES_REDSHIFT_DATA_MAX_CONCURRENT_STATEMENTS:10}+ queue-capacity: ${FLOCI_SERVICES_REDSHIFT_DATA_QUEUE_CAPACITY:100}+ shutdown-timeout-seconds: ${FLOCI_SERVICES_REDSHIFT_DATA_SHUTDOWN_TIMEOUT_SECONDS:10}redshift-serverless:enabled: trueneptune:diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftSpectrumIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftSpectrumIntegrationTest.javaindex e04a7a5e03..f123ed8d94 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftSpectrumIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftSpectrumIntegrationTest.java@@ -255,7 +255,7 @@ class RedshiftSpectrumIntegrationTest {RestAssuredJsonUtils.configureAwsContentTypes();String queryId = RestAssuredJsonUtils.awsAction("RedshiftData", "ExecuteStatement", """{"ClusterIdentifier":"%s","DbUser":"admin","Database":"dev",- "Sql":"SELECT SUM(amount) AS total FROM lake.events"}+ "Sql":"SELECT SUM(amount) AS total FROM lake.events","WaitTimeSeconds":30}""".formatted(clusterId)).then().statusCode(200).extract().path("Id");assertEquals("FINISHED", RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement","{\"Id\":\"" + queryId + "\"}").then().statusCode(200).extract().path("Status"));diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.javaindex a4acc99808..5e66ec0b30 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.java@@ -18,6 +18,7 @@ import java.util.List;import static org.hamcrest.Matchers.containsString;import static org.hamcrest.Matchers.hasItem;+import static org.hamcrest.Matchers.is;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -73,10 +74,11 @@ class RedshiftDataApiIntegrationTest {private void awaitFinished(String id) {Awaitility.await().atMost(Duration.ofSeconds(30)).pollDelay(Duration.ZERO).pollInterval(Duration.ofMillis(250)).until(() -> {- String status = RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement",- "{\"Id\":\"" + id + "\"}").then().statusCode(200).extract().path("Status");+ ExtractableResponse<Response> described = RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement",+ "{\"Id\":\"" + id + "\"}").then().statusCode(200).extract();+ String status = described.path("Status");assertTrue(!"FAILED".equals(status) && !"ABORTED".equals(status),- () -> "statement " + id + " ended " + status);+ () -> "statement " + id + " ended " + status + ": " + described.path("Error"));return "FINISHED".equals(status);});}@@ -85,6 +87,11 @@ class RedshiftDataApiIntegrationTest {return ((Number) value).intValue();}+ @Test+ void vacuumPreservesAutoCommitExecution() {+ executeAndWait("VACUUM");+ }+@Testvoid executeDescribeGetResultRoundTrip() {executeAndWait("CREATE TABLE t (id int, name varchar(20))");@@ -190,4 +197,44 @@ class RedshiftDataApiIntegrationTest {"FAILED".equals(RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement","{\"Id\":\"" + id + "\"}").then().statusCode(200).extract().path("Status")));}++ @Test+ void slowSqlReturnsEarlyAndCanBeCancelled() {+ long started = System.nanoTime();+ String id = RestAssuredJsonUtils.awsAction("RedshiftData", "ExecuteStatement", """+ {"Sql":"SELECT pg_sleep(20)","ClusterIdentifier":"%s","DbUser":"admin","Database":"dev"}+ """.formatted(clusterId)).then().statusCode(200).extract().path("Id");+ assertTrue(Duration.ofNanos(System.nanoTime() - started).compareTo(Duration.ofSeconds(10)) < 0);+ Awaitility.await().atMost(Duration.ofSeconds(10)).until(() -> "STARTED".equals(+ RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement", "{\"Id\":\"" + id + "\"}")+ .then().statusCode(200).extract().path("Status")));+ RestAssuredJsonUtils.awsAction("RedshiftData", "CancelStatement", "{\"Id\":\"" + id + "\"}")+ .then().statusCode(200).body("Status", is(true));+ Awaitility.await().atMost(Duration.ofSeconds(10)).until(() -> "ABORTED".equals(+ RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement", "{\"Id\":\"" + id + "\"}")+ .then().statusCode(200).extract().path("Status")));+ }++ @Test+ void cancellingBatchRollsBackEarlierWrites() {+ executeAndWait("CREATE TABLE cancel_batch (id int)");+ String id = RestAssuredJsonUtils.awsAction("RedshiftData", "BatchExecuteStatement", """+ {"Sqls":["INSERT INTO cancel_batch VALUES (1)","SELECT pg_sleep(20)",+ "INSERT INTO cancel_batch VALUES (2)"],"ClusterIdentifier":"%s","DbUser":"admin","Database":"dev"}+ """.formatted(clusterId)).then().statusCode(200).extract().path("Id");+ Awaitility.await().atMost(Duration.ofSeconds(10)).until(() -> {+ List<?> children = RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement", "{\"Id\":\"" + id + "\"}")+ .then().statusCode(200).extract().path("SubStatements");+ return children != null && !children.isEmpty();+ });+ RestAssuredJsonUtils.awsAction("RedshiftData", "CancelStatement", "{\"Id\":\"" + id + "\"}")+ .then().statusCode(200).body("Status", is(true));+ Awaitility.await().atMost(Duration.ofSeconds(10)).until(() -> "ABORTED".equals(+ RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement", "{\"Id\":\"" + id + "\"}")+ .then().statusCode(200).extract().path("Status")));+ String countId = executeAndWait("SELECT count(*) FROM cancel_batch");+ Object count = RestAssuredJsonUtils.awsAction("RedshiftData", "GetStatementResult", "{\"Id\":\"" + countId + "\"}")+ .then().statusCode(200).extract().path("Records[0][0].longValue");+ assertEquals(0, asInt(count));+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataAsyncServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataAsyncServiceTest.javanew file mode 100644index 0000000000..bd2828a0d1--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataAsyncServiceTest.java@@ -0,0 +1,187 @@+package io.github.hectorvent.floci.services.redshiftdata;++import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.common.AwsException;+import org.h2.Driver;+import org.junit.jupiter.api.Test;++import java.sql.Connection;+import java.sql.DriverManager;+import java.sql.PreparedStatement;+import java.sql.SQLException;+import java.time.Clock;+import java.time.Duration;+import java.util.UUID;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicReference;++import static org.awaitility.Awaitility.await;+import static org.junit.jupiter.api.Assertions.*;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.Mockito.*;++class RedshiftDataAsyncServiceTest {++ @Test+ void cancellationDuringBindingDoesNotStartJdbcQuery() throws Exception {+ ObjectMapper mapper = new ObjectMapper();+ RedshiftDataResourceResolver resolver = mock(RedshiftDataResourceResolver.class);+ when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget(+ "arn:aws:redshift:us-east-1:000000000000:cluster:wh", "localhost", 5439, "dev", "admin", "password"));+ RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class);+ Connection connection = mock(Connection.class);+ PreparedStatement statement = mock(PreparedStatement.class);+ when(factory.open(any())).thenReturn(connection);+ when(connection.prepareStatement(anyString())).thenReturn(statement);+ CountDownLatch binding = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ doAnswer(invocation -> {+ binding.countDown();+ assertTrue(release.await(5, TimeUnit.SECONDS));+ return null;+ }).when(statement).setString(eq(1), eq("20"));+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ RedshiftDataService service = new RedshiftDataService(resolver, factory,+ new RedshiftDataStatementStore(24, Clock.systemUTC()), mapper, null, executor, null);+ ObjectNode request = mapper.createObjectNode().put("Sql", "SELECT pg_sleep(:seconds)");+ request.putArray("Parameters").addObject().put("name", "seconds").put("value", "20");+ String id = service.executeStatement(request, "us-east-1").path("Id").asText();+ assertTrue(binding.await(5, TimeUnit.SECONDS));+ try {+ assertTrue(service.cancelStatement(mapper.createObjectNode().put("Id", id)).path("Status").asBoolean());+ } finally {+ release.countDown();+ }+ await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> assertEquals("ABORTED",+ service.describeStatement(mapper.createObjectNode().put("Id", id)).path("Status").asText()));+ verify(statement, never()).execute();+ verify(connection, never()).commit();+ verify(connection).rollback();+ }+ }++ @Test+ void batchEmitsOnlyParentEventAndFalseOrOmittedWithEventEmitsNone() throws Exception {+ ObjectMapper mapper = new ObjectMapper();+ RedshiftDataResourceResolver resolver = mock(RedshiftDataResourceResolver.class);+ when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget(+ "arn:aws:redshift:us-east-1:000000000000:cluster:wh", "localhost", 5439, "dev", "admin", "password"));+ RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class);+ Driver.load();+ String jdbcUrl = "jdbc:h2:mem:" + UUID.randomUUID() + ";DB_CLOSE_DELAY=-1";+ when(factory.open(any())).thenAnswer(invocation -> DriverManager.getConnection(jdbcUrl));+ RedshiftDataEventPublisher publisher = mock(RedshiftDataEventPublisher.class);+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 4, 1)) {+ RedshiftDataStatementStore store = new RedshiftDataStatementStore(24, Clock.systemUTC());+ RedshiftDataService service = new RedshiftDataService(resolver, factory, store, mapper, null, executor, publisher);+ ObjectNode request = mapper.createObjectNode().put("WithEvent", true).put("WaitTimeSeconds", 5);+ request.putArray("Sqls").add("SELECT 1").add("SELECT 2");+ String id = service.batchExecuteStatement(request, "us-east-1").path("Id").asText();+ assertEquals(RedshiftDataStatementStore.Status.FINISHED, store.getForAccount("000000000000", id).status,+ () -> store.getForAccount("000000000000", id).error);+ await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> verify(publisher).publish(argThat(statement ->+ id.equals(statement.id) && statement.status == RedshiftDataStatementStore.Status.FINISHED)));+ assertEquals(RedshiftDataStatementStore.Status.FINISHED, store.getForAccount("000000000000", id).status);+ service.executeStatement(mapper.createObjectNode().put("Sql", "SELECT 3").put("WaitTimeSeconds", 5), "us-east-1");+ service.executeStatement(mapper.createObjectNode().put("Sql", "SELECT 4").put("WaitTimeSeconds", 5)+ .put("WithEvent", false), "us-east-1");+ executor.close();+ verify(publisher, times(1)).publish(any());+ }+ }++ @Test+ void queuedCancellationPublishesOneAbortedEventAfterTerminalStorage() throws Exception {+ ObjectMapper mapper = new ObjectMapper();+ RedshiftDataResourceResolver resolver = mock(RedshiftDataResourceResolver.class);+ when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget(+ "arn:aws-cn:redshift:cn-north-1:111111111111:cluster:wh", "localhost", 5439, "dev", "admin", "password"));+ RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class);+ RedshiftDataEventPublisher publisher = mock(RedshiftDataEventPublisher.class);+ RedshiftDataStatementStore store = new RedshiftDataStatementStore(24, Clock.systemUTC());+ AtomicReference<RedshiftDataStatementStore.StoredStatement> delivered = new AtomicReference<>();+ doAnswer(invocation -> {+ RedshiftDataStatementStore.StoredStatement statement = invocation.getArgument(0);+ assertEquals(RedshiftDataStatementStore.Status.ABORTED,+ store.getForAccount(statement.accountId, statement.id).status);+ delivered.set(statement);+ return null;+ }).when(publisher).publish(any());+ CountDownLatch started = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 2, 1)) {+ executor.submit("111111111111", "cn-north-1", "blocking", execution -> {+ started.countDown();+ try {+ release.await(5, TimeUnit.SECONDS);+ } catch (InterruptedException expected) {+ Thread.currentThread().interrupt();+ }+ });+ assertTrue(started.await(5, TimeUnit.SECONDS));+ RedshiftDataService service = new RedshiftDataService(resolver, factory, store, mapper, null, executor, publisher);+ String id = service.executeStatement(mapper.createObjectNode().put("Sql", "SELECT 1")+ .put("WithEvent", true).put("StatementName", "etl"), "cn-north-1", "role-principal").path("Id").asText();+ try {+ assertTrue(executor.cancel("111111111111", id));+ await().atMost(Duration.ofSeconds(5)).until(() -> delivered.get() != null);+ assertEquals("111111111111", delivered.get().accountId);+ assertEquals("cn-north-1", delivered.get().region);+ assertEquals("role-principal", delivered.get().principal);+ assertEquals("etl", delivered.get().statementName);+ assertEquals(RedshiftDataStatementStore.Status.ABORTED, delivered.get().status);+ verifyNoInteractions(factory);+ assertFalse(executor.cancel("111111111111", id));+ verify(publisher, times(1)).publish(any());+ } finally {+ release.countDown();+ }+ }+ }++ @Test+ void returnsIdBeforeDatabaseConnectionCompletes() throws Exception {+ ObjectMapper mapper = new ObjectMapper();+ RedshiftDataResourceResolver resolver = mock(RedshiftDataResourceResolver.class);+ when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget(+ "arn:aws:redshift:us-east-1:000000000000:cluster:wh", "localhost", 5439, "dev", "admin", "password"));+ RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class);+ CountDownLatch started = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ when(factory.open(any())).thenAnswer(invocation -> {+ started.countDown();+ assertTrue(release.await(5, TimeUnit.SECONDS));+ throw new SQLException("connection refused");+ });+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ RedshiftDataService service = new RedshiftDataService(resolver, factory,+ new RedshiftDataStatementStore(24, Clock.systemUTC()), mapper, null, executor, null);+ ObjectNode request = mapper.createObjectNode().put("Sql", "select 1").put("ClusterIdentifier", "wh");+ ObjectNode response = service.executeStatement(request, "us-east-1");+ assertTrue(started.await(5, TimeUnit.SECONDS));+ ObjectNode id = mapper.createObjectNode().put("Id", response.path("Id").asText());+ assertEquals("STARTED", service.describeStatement(id).path("Status").asText());+ assertThrows(AwsException.class, () -> service.getStatementResult(id));+ release.countDown();+ await().atMost(Duration.ofSeconds(5)).untilAsserted(() ->+ assertEquals("FAILED", service.describeStatement(id).path("Status").asText()));+ AwsException rejected = assertThrows(AwsException.class, () -> service.cancelStatement(id));+ assertEquals("ValidationException", rejected.getErrorCode());+ }+ }++ @Test+ void validatesWaitTimeBeforeAcceptingWork() {+ ObjectMapper mapper = new ObjectMapper();+ RedshiftDataResourceResolver resolver = mock(RedshiftDataResourceResolver.class);+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ RedshiftDataService service = new RedshiftDataService(resolver, mock(RedshiftDataConnectionFactory.class),+ new RedshiftDataStatementStore(24, Clock.systemUTC()), mapper, null, executor, null);+ ObjectNode request = mapper.createObjectNode().put("Sql", "select 1").put("WaitTimeSeconds", 31);+ assertThrows(AwsException.class, () -> service.executeStatement(request, "us-east-1"));+ verifyNoInteractions(resolver);+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventPublisherTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventPublisherTest.javanew file mode 100644index 0000000000..3421ff5fe7--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventPublisherTest.java@@ -0,0 +1,76 @@+package io.github.hectorvent.floci.services.redshiftdata;++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.ServiceRegistry;+import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;+import org.junit.jupiter.api.Test;+import org.mockito.ArgumentCaptor;++import java.time.Instant;+import java.util.List;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.*;+import static org.mockito.ArgumentMatchers.*;+import static org.mockito.Mockito.*;++class RedshiftDataEventPublisherTest {++ @Test+ @SuppressWarnings("unchecked")+ void publishesAwsEnvelopeInResourceAccountAndRegion() throws Exception {+ ObjectMapper mapper = new ObjectMapper();+ EventBridgeService events = mock(EventBridgeService.class);+ ServiceRegistry registry = mock(ServiceRegistry.class);+ when(registry.isServiceEnabled("eventbridge")).thenReturn(true);+ when(events.putEvents(anyList(), anyString(), anyString()))+ .thenReturn(new EventBridgeService.PutEventsResult(0, List.of()));+ RedshiftDataEventPublisher publisher = new RedshiftDataEventPublisher(events, mapper, registry);+ RedshiftDataStatementStore.StoredStatement statement = new RedshiftDataStatementStore.StoredStatement();+ statement.id = "statement-id";+ statement.accountId = "111111111111";+ statement.region = "cn-north-1";+ statement.resourceArn = "arn:aws-cn:redshift-serverless:cn-north-1:111111111111:workgroup/group-id";+ statement.principal = "arn:aws-cn:iam::111111111111:role/etl";+ statement.statementName = "etl";+ statement.status = RedshiftDataStatementStore.Status.FINISHED;+ statement.resultRows = 3;+ statement.expiresAt = Instant.parse("2026-10-11T00:00:00Z");+ publisher.publish(statement);+ ArgumentCaptor<List<Map<String, Object>>> entries = ArgumentCaptor.forClass(List.class);+ verify(events).putEvents(entries.capture(), eq("cn-north-1"), eq("111111111111"));+ Map<String, Object> entry = entries.getValue().getFirst();+ assertEquals("aws.redshift-data", entry.get("Source"));+ assertEquals("Redshift Data Statement Status Change", entry.get("DetailType"));+ assertEquals(statement.resourceArn, mapper.valueToTree(entry.get("Resources")).get(0).asText());+ ObjectNode detail = (ObjectNode) mapper.readTree((String) entry.get("Detail"));+ assertEquals("FINISHED", detail.path("state").asText());+ assertEquals(3, detail.path("rows").asInt());+ assertEquals(statement.principal, detail.path("principal").asText());+ assertEquals(statement.expiresAt.getEpochSecond(), detail.path("expireAt").asLong());+ assertEquals(statement.id.hashCode() & Integer.MAX_VALUE, detail.path("redshiftQueryId").asLong());+ }++ @Test+ void rejectedEntriesAndRefusalsDoNotFailSqlCompletion() {+ EventBridgeService events = mock(EventBridgeService.class);+ ServiceRegistry registry = mock(ServiceRegistry.class);+ when(registry.isServiceEnabled("eventbridge")).thenReturn(true);+ RedshiftDataEventPublisher publisher = new RedshiftDataEventPublisher(events, new ObjectMapper(), registry);+ RedshiftDataStatementStore.StoredStatement statement = new RedshiftDataStatementStore.StoredStatement();+ statement.id = "id";+ statement.accountId = "111111111111";+ statement.region = "us-east-1";+ statement.status = RedshiftDataStatementStore.Status.FAILED;+ statement.expiresAt = Instant.now();+ when(events.putEvents(anyList(), anyString(), anyString()))+ .thenReturn(new EventBridgeService.PutEventsResult(1, List.of(Map.of("ErrorCode", "InternalFailure"))))+ .thenThrow(new AwsException("InvalidEventPatternException", "refused", 400))+ .thenThrow(new IllegalStateException("programming error"));+ assertDoesNotThrow(() -> publisher.publish(statement));+ assertDoesNotThrow(() -> publisher.publish(statement));+ assertThrows(IllegalStateException.class, () -> publisher.publish(statement));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventsIntegrationTest.javanew file mode 100644index 0000000000..41ea1053c6--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataEventsIntegrationTest.java@@ -0,0 +1,107 @@+package io.github.hectorvent.floci.services.redshiftdata;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.core.common.RequestScopes;+import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;+import io.github.hectorvent.floci.services.eventbridge.model.RuleState;+import io.github.hectorvent.floci.services.eventbridge.model.Target;+import io.github.hectorvent.floci.services.sqs.SqsService;+import io.github.hectorvent.floci.services.sqs.model.Message;+import io.quarkus.test.InjectMock;+import io.quarkus.test.junit.QuarkusTest;+import jakarta.inject.Inject;+import org.junit.jupiter.api.Test;++import java.sql.SQLException;+import java.time.Duration;+import java.util.List;+import java.util.Map;+import java.util.UUID;+import java.util.concurrent.atomic.AtomicReference;++import static org.awaitility.Awaitility.await;+import static org.junit.jupiter.api.Assertions.*;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.Mockito.when;++@QuarkusTest+class RedshiftDataEventsIntegrationTest {+ private static final String ACCOUNT = "111122223333";+ private static final String OTHER_ACCOUNT = "000000000000";+ private static final String REGION = "us-east-1";++ @Inject+ RedshiftDataService service;+ @Inject+ EventBridgeService events;+ @Inject+ SqsService sqs;+ @Inject+ ObjectMapper mapper;+ @InjectMock+ RedshiftDataResourceResolver resolver;+ @InjectMock+ RedshiftDataConnectionFactory factory;++ @Test+ void statusEventReachesOnlyTheCapturedAccountRule() throws Exception {+ String name = "rsdata-event-" + UUID.randomUUID();+ String pattern = mapper.writeValueAsString(Map.of("source", List.of("aws.redshift-data"),+ "detail", Map.of("statementName", List.of(name))));+ String queue = createRuleAndQueue(ACCOUNT, name, pattern);+ String otherQueue = createRuleAndQueue(OTHER_ACCOUNT, name, pattern);+ when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget(+ "arn:aws:redshift:us-east-1:" + ACCOUNT + ":cluster:analytics",+ "localhost", 5439, "dev", "admin", "password"));+ when(factory.open(any())).thenThrow(new SQLException("connection refused"));+ try {+ String id = RequestScopes.callAs(ACCOUNT, REGION, () -> service.executeStatement(+ mapper.createObjectNode().put("Sql", "SELECT 1").put("WithEvent", true)+ .put("StatementName", name), REGION, "arn:aws:iam::" + ACCOUNT + ":role/etl"))+ .path("Id").asText();+ AtomicReference<JsonNode> delivered = new AtomicReference<>();+ await().atMost(Duration.ofSeconds(10)).until(() -> {+ List<Message> messages = RequestScopes.callAs(ACCOUNT, REGION,+ () -> sqs.receiveMessage(queue, 1, 30, 0, REGION));+ if (messages.isEmpty()) {+ return false;+ }+ delivered.set(mapper.readTree(messages.getFirst().getBody()));+ return true;+ });+ assertEquals(ACCOUNT, delivered.get().path("account").asText());+ assertEquals(REGION, delivered.get().path("region").asText());+ assertEquals(id, delivered.get().path("detail").path("statementId").asText());+ assertEquals("FAILED", delivered.get().path("detail").path("state").asText());+ assertEquals("arn:aws:iam::" + ACCOUNT + ":role/etl",+ delivered.get().path("detail").path("principal").asText());+ assertEquals("FAILED", RequestScopes.callAs(ACCOUNT, REGION, () ->+ service.describeStatement(mapper.createObjectNode().put("Id", id))).path("Status").asText());+ assertTrue(RequestScopes.callAs(OTHER_ACCOUNT, REGION,+ () -> sqs.receiveMessage(otherQueue, 1, 30, 0, REGION)).isEmpty());+ } finally {+ deleteRuleAndQueue(ACCOUNT, name, queue);+ deleteRuleAndQueue(OTHER_ACCOUNT, name, otherQueue);+ service.clear();+ }+ }++ private String createRuleAndQueue(String account, String name, String pattern) {+ return RequestScopes.callAs(account, REGION, () -> {+ String queue = sqs.createQueue(name, Map.of(), REGION).getQueueUrl();+ events.putRule(name, "default", pattern, null, RuleState.ENABLED, null, null, Map.of(), REGION);+ events.putTargets(name, "default", List.of(new Target("queue",+ "arn:aws:sqs:" + REGION + ":" + account + ":" + name, null, null)), REGION);+ return queue;+ });+ }++ private void deleteRuleAndQueue(String account, String name, String queue) {+ RequestScopes.runAs(account, REGION, () -> {+ events.removeTargets(name, "default", List.of("queue"), REGION);+ events.deleteRule(name, "default", REGION);+ sqs.deleteQueue(queue, REGION);+ });+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutionTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutionTest.javanew file mode 100644index 0000000000..c2a3915051--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutionTest.java@@ -0,0 +1,67 @@+package io.github.hectorvent.floci.services.redshiftdata;++import org.junit.jupiter.api.Test;++import java.sql.SQLException;+import java.sql.Statement;+import java.time.Duration;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicReference;++import static org.junit.jupiter.api.Assertions.*;+import static org.mockito.Mockito.*;++class RedshiftDataExecutionTest {++ @Test+ void runningCancellationCancelsJdbcAndPreventsCommit() throws Exception {+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ AtomicReference<Throwable> workerFailure = new AtomicReference<>();+ Statement statement = mock(Statement.class);+ CountDownLatch started = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ RedshiftDataExecution handle = executor.submit("111111111111", "us-east-1", "query", execution -> {+ try {+ execution.track(statement);+ started.countDown();+ assertTrue(release.await(5, TimeUnit.SECONDS));+ assertThrows(SQLException.class, () -> execution.commit(() -> fail("must not commit")));+ } catch (Throwable failure) {+ workerFailure.set(failure);+ }+ });+ assertTrue(started.await(5, TimeUnit.SECONDS));+ assertTrue(executor.cancel("111111111111", "query"));+ verify(statement).cancel();+ release.countDown();+ handle.await(Duration.ofSeconds(5));+ assertTrue(handle.isDone());+ assertNull(workerFailure.get());+ assertFalse(executor.cancel("111111111111", "query"));+ }+ }++ @Test+ void successfulCommitWinsOverCancellation() throws Exception {+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ AtomicReference<Throwable> workerFailure = new AtomicReference<>();+ CountDownLatch committed = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ RedshiftDataExecution handle = executor.submit("111111111111", "us-east-1", "query", execution -> {+ try {+ execution.commit(committed::countDown);+ assertTrue(release.await(5, TimeUnit.SECONDS));+ } catch (Throwable failure) {+ workerFailure.set(failure);+ }+ });+ assertTrue(committed.await(5, TimeUnit.SECONDS));+ assertFalse(executor.cancel("111111111111", "query"));+ release.countDown();+ handle.await(Duration.ofSeconds(5));+ assertTrue(handle.isDone());+ assertNull(workerFailure.get());+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutorTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutorTest.javanew file mode 100644index 0000000000..fef015e859--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutorTest.java@@ -0,0 +1,148 @@+package io.github.hectorvent.floci.services.redshiftdata;++import io.github.hectorvent.floci.core.common.AwsException;+import org.junit.jupiter.api.Test;++import java.time.Duration;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicBoolean;++import static org.junit.jupiter.api.Assertions.*;++class RedshiftDataExecutorTest {++ @Test+ void admissionReturnsWhileSqlIsRunningAndRejectsOverflow() throws Exception {+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ CountDownLatch started = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ RedshiftDataExecution first = executor.submit("111111111111", "us-east-1", "first", execution -> {+ started.countDown();+ await(release);+ });+ assertTrue(started.await(5, TimeUnit.SECONDS));+ assertFalse(first.isDone());+ executor.submit("111111111111", "us-east-1", "second", execution -> {});+ AwsException failure = assertThrows(AwsException.class,+ () -> executor.submit("111111111111", "us-east-1", "third", execution -> {}));+ assertEquals("ActiveStatementsExceededException", failure.getErrorCode());+ release.countDown();+ first.await(Duration.ofSeconds(5));+ assertTrue(first.isDone());+ assertFalse(executor.cancel("222222222222", "second"));+ }+ }++ @Test+ void queuedCancellationSkipsSqlAndCompletesImmediately() throws Exception {+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ CountDownLatch started = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ executor.submit("111111111111", "us-east-1", "first", execution -> {+ started.countDown();+ await(release);+ });+ assertTrue(started.await(5, TimeUnit.SECONDS));+ AtomicBoolean cancelled = new AtomicBoolean();+ RedshiftDataExecution second = executor.submit("111111111111", "us-east-1", "second",+ execution -> cancelled.set(execution.isCancellationRequested()));+ assertTrue(executor.cancel("111111111111", "second"));+ second.await(Duration.ofSeconds(5));+ assertTrue(cancelled.get());+ assertFalse(executor.cancel("111111111111", "second"));+ release.countDown();+ }+ }++ @Test+ void invalidLimitsAreRejected() {+ assertThrows(IllegalArgumentException.class, () -> new RedshiftDataExecutor(0, 1, 1));+ assertThrows(IllegalArgumentException.class, () -> new RedshiftDataExecutor(1, 0, 1));+ assertThrows(IllegalArgumentException.class, () -> new RedshiftDataExecutor(1, 1, -1));+ }++ @Test+ void resetSuppressesEventsQueuedByAnAlreadyCompletedExecution() throws Exception {+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 4, 1)) {+ RedshiftDataExecution completed = executor.submit("111111111111", "us-east-1", "query", execution -> {});+ completed.await(Duration.ofSeconds(5));+ CountDownLatch delivering = new CountDownLatch(2);+ CountDownLatch release = new CountDownLatch(1);+ Runnable blockedDelivery = () -> {+ delivering.countDown();+ await(release);+ };+ executor.deliver(completed, "111111111111", "us-east-1", "query", blockedDelivery);+ executor.deliver(completed, "111111111111", "us-east-1", "query", blockedDelivery);+ assertTrue(delivering.await(5, TimeUnit.SECONDS));+ AtomicBoolean publishedAfterClear = new AtomicBoolean();+ executor.deliver(completed, "111111111111", "us-east-1", "query", () -> publishedAfterClear.set(true));+ executor.clear(() -> {});+ release.countDown();+ executor.close();+ assertFalse(publishedAfterClear.get());+ }+ }++ @Test+ void shutdownReturnsWithinBudgetWhileCommitIsBlocked() throws Exception {+ RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 0);+ CountDownLatch committing = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ CountDownLatch closed = new CountDownLatch(1);+ executor.submit("111111111111", "us-east-1", "query", execution -> {+ try {+ execution.commit(() -> {+ committing.countDown();+ boolean waiting = true;+ while (waiting) {+ try {+ release.await();+ waiting = false;+ } catch (InterruptedException ignored) {+ // Model a JDBC commit that does not respond to thread interruption.+ }+ }+ });+ } catch (Exception failure) {+ throw new AssertionError(failure);+ }+ });+ assertTrue(committing.await(5, TimeUnit.SECONDS));+ Thread closer = Thread.startVirtualThread(() -> {+ executor.close();+ closed.countDown();+ });+ try {+ assertTrue(closed.await(1, TimeUnit.SECONDS), "shutdown must not wait on the JDBC commit monitor");+ } finally {+ release.countDown();+ closer.join(5000);+ }+ }++ private static void await(CountDownLatch latch) {+ try {+ assertTrue(latch.await(10, TimeUnit.SECONDS));+ } catch (InterruptedException expected) {+ Thread.currentThread().interrupt();+ throw new AssertionError(expected);+ }+ }++ @Test+ void unexpectedWorkerFailureReachesTheFailureHandler() throws Exception {+ try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) {+ AtomicBoolean recovered = new AtomicBoolean();+ RedshiftDataExecution execution = executor.submit("111111111111", "us-east-1", "broken", () -> { },+ ignored -> {+ throw new IllegalStateException("storage unavailable");+ },+ (failed, failure) -> recovered.set("storage unavailable".equals(failure.getMessage())));+ execution.await(Duration.ofSeconds(5));+ assertTrue(execution.isDone());+ assertTrue(recovered.get());+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.javaindex dc2b599b3b..5152ca86c5 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java@@ -8,16 +8,23 @@ import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumQueryPreparation;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumReadException;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumSession;+import org.awaitility.Awaitility;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.ValueSource;import java.sql.DriverManager;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.time.Clock;+import java.time.Duration;import java.util.List;import java.util.UUID;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;@@ -43,6 +50,7 @@ class RedshiftDataServiceTest {private String jdbcUrl;private RedshiftDataService service;private SpectrumQueryPreparation preparation;+ private RedshiftDataExecutor executor;@BeforeEachvoid setUp() throws Exception {@@ -57,12 +65,18 @@ class RedshiftDataServiceTest {when(factory.open(any())).thenAnswer(inv -> DriverManager.getConnection(jdbcUrl, "sa", ""));preparation = mock(SpectrumQueryPreparation.class);+ executor = new RedshiftDataExecutor(2, 100, 1);service = new RedshiftDataService(resolver, factory,- new RedshiftDataStatementStore(24, Clock.systemUTC()), om, preparation);+ new RedshiftDataStatementStore(24, Clock.systemUTC()), om, preparation, executor, null);+ }++ @AfterEach+ void stopExecutor() {+ executor.close();}private ObjectNode req(String sql) {- ObjectNode r = om.createObjectNode();+ ObjectNode r = om.createObjectNode().put("WaitTimeSeconds", 5);r.put("Sql", sql);r.put("ClusterIdentifier", "wh");r.put("DbUser", "admin");@@ -74,6 +88,40 @@ class RedshiftDataServiceTest {return om.createObjectNode().put("Id", id);}+ @Test+ void rolledBackBatchDoesNotExposeChildResults() {+ ObjectNode request = req("unused");+ request.putArray("Sqls").add("SELECT 1").add("SELECT * FROM nonexistent_batch_table");+ String id = service.batchExecuteStatement(request, REGION).path("Id").asText();+ assertEquals("FAILED", service.describeStatement(idOf(id)).path("Status").asText());+ assertThrows(AwsException.class, () -> service.getStatementResult(idOf(id + ":1")));+ }++ @Test+ void spectrumDdlOwnsCompletionBeforeAutoCommittedWrites() throws Exception {+ String sql = "CREATE EXTERNAL SCHEMA lake";+ when(preparation.handlesDdl(eq(sql), any())).thenReturn(true);+ CountDownLatch writing = new CountDownLatch(1);+ CountDownLatch release = new CountDownLatch(1);+ when(preparation.prepare(eq(sql), any(), any())).thenAnswer(invocation -> {+ writing.countDown();+ assertTrue(release.await(5, TimeUnit.SECONDS));+ return true;+ });+ ObjectNode request = req(sql);+ request.remove("WaitTimeSeconds");+ String id = service.executeStatement(request, REGION).path("Id").asText();+ assertTrue(writing.await(5, TimeUnit.SECONDS));+ try {+ assertFalse(service.cancelStatement(idOf(id)).path("Status").asBoolean(),+ "auto-committed external writes must not be reported as aborted");+ } finally {+ release.countDown();+ }+ Awaitility.await().atMost(Duration.ofSeconds(5)).untilAsserted(() ->+ assertEquals("FINISHED", service.describeStatement(idOf(id)).path("Status").asText()));+ }+@Testvoid preparationFailureIsStoredAsFailedStatement() {doThrow(new SpectrumReadException("42501", "S3 access denied")).when(preparation)@@ -94,7 +142,7 @@ class RedshiftDataServiceTest {@Testvoid aWorkgroupStatementNamesItsWorkgroupAndOmitsTheClusterIdentifier() {- ObjectNode request = om.createObjectNode();+ ObjectNode request = om.createObjectNode().put("WaitTimeSeconds", 5);request.put("Sql", "select 1");request.put("WorkgroupName", "wg-1");request.put("Database", "dev");@@ -122,7 +170,7 @@ class RedshiftDataServiceTest {@Testvoid aWorkgroupBatchPassesItsWorkgroupToEverySubStatement() {- ObjectNode request = om.createObjectNode();+ ObjectNode request = om.createObjectNode().put("WaitTimeSeconds", 5);request.putArray("Sqls").add("select 1").add("select 2");request.put("WorkgroupName", "wg-1");request.put("Database", "dev");@@ -226,7 +274,7 @@ class RedshiftDataServiceTest {}private ObjectNode batchReq(String... sqls) {- ObjectNode r = om.createObjectNode();+ ObjectNode r = om.createObjectNode().put("WaitTimeSeconds", 5);r.put("ClusterIdentifier", "wh");r.put("DbUser", "admin");r.put("Database", "dev");@@ -283,10 +331,12 @@ class RedshiftDataServiceTest {}@Test- void cancelOnFinishedStatementReturnsTrueWithoutChangingIt() {+ void cancelOnFinishedStatementIsRejectedWithoutChangingIt() {service.executeStatement(req("create table cf (id int)"), REGION);String id = service.executeStatement(req("select id from cf"), REGION).get("Id").asText();- assertTrue(service.cancelStatement(idOf(id)).get("Status").asBoolean());+ AwsException rejected = assertThrows(AwsException.class, () -> service.cancelStatement(idOf(id)));+ assertEquals("ValidationException", rejected.getErrorCode());+ assertEquals("Could not cancel a query that is already in FINISHED state with ID: " + id, rejected.getMessage());assertEquals("FINISHED", service.describeStatement(idOf(id)).get("Status").asText());}@@ -297,7 +347,7 @@ class RedshiftDataServiceTest {}private ObjectNode targetReq() {- ObjectNode r = om.createObjectNode();+ ObjectNode r = om.createObjectNode().put("WaitTimeSeconds", 5);r.put("ClusterIdentifier", "wh");r.put("DbUser", "admin");r.put("Database", "dev");@@ -375,10 +425,11 @@ class RedshiftDataServiceTest {}@Test- void cancelOnFailedStatementLeavesItFailed() {+ void cancelOnFailedStatementIsRejectedAndLeavesItFailed() {String id = service.executeStatement(req("select * from missing_table"), REGION).get("Id").asText();assertEquals("FAILED", service.describeStatement(idOf(id)).get("Status").asText());- assertTrue(service.cancelStatement(idOf(id)).get("Status").asBoolean());+ AwsException rejected = assertThrows(AwsException.class, () -> service.cancelStatement(idOf(id)));+ assertEquals("Could not cancel a query that is already in FAILED state with ID: " + id, rejected.getMessage());assertEquals("FAILED", service.describeStatement(idOf(id)).get("Status").asText());}@@ -409,4 +460,20 @@ class RedshiftDataServiceTest {assertEquals(1, described.get("ColumnList").size());assertEquals("x", described.get("ColumnList").get(0).get("name").asText().toLowerCase());}++ @ParameterizedTest+ @ValueSource(strings = {+ "VACUUM t", " -- note\n create database d", "CREATE INDEX CONCURRENTLY i ON t (c)",+ "create unique index concurrently i on t (c)", "DROP INDEX CONCURRENTLY i",+ "REINDEX TABLE CONCURRENTLY t", "ALTER SYSTEM SET work_mem = '8MB'"})+ void transactionIncompatibleCommandsRunOutsideATransaction(String sql) {+ assertTrue(RedshiftDataService.AUTO_COMMIT_SQL.matcher(sql).find(), sql);+ }++ @ParameterizedTest+ @ValueSource(strings = {+ "CREATE INDEX i ON t (c)", "REINDEX TABLE t", "select 'vacuum'", "CREATE TABLE concurrently (a int)"})+ void ordinaryCommandsStayInsideATransaction(String sql) {+ assertFalse(RedshiftDataService.AUTO_COMMIT_SQL.matcher(sql).find(), sql);+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStoreTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStoreTest.javaindex bbb83791fb..436f7b3661 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStoreTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStoreTest.java@@ -21,6 +21,46 @@ import static org.junit.jupiter.api.Assertions.assertTrue;class RedshiftDataStatementStoreTest {+ @Test+ void recoveryFailsInterruptedWorkWithoutChangingTerminalResults() {+ RedshiftDataStatementStore store = new RedshiftDataStatementStore(24, Clock.systemUTC());+ RedshiftDataStatementStore.StoredStatement active = statement("active", Instant.now());+ active.status = RedshiftDataStatementStore.Status.STARTED;+ store.put(active);+ store.put(statement("finished", Instant.now()));+ store.start();+ try {+ assertEquals(RedshiftDataStatementStore.Status.FAILED, store.get("active").status);+ assertEquals("Execution interrupted by emulator restart", store.get("active").error);+ assertEquals(RedshiftDataStatementStore.Status.FINISHED, store.get("finished").status);+ } finally {+ store.stop();+ }+ }++ @Test+ void sweepRetainsActiveStatementsOlderThanTtl() {+ Instant now = Instant.parse("2026-10-10T00:00:00Z");+ RedshiftDataStatementStore store = new RedshiftDataStatementStore(24, Clock.fixed(now, ZoneOffset.UTC));+ RedshiftDataStatementStore.StoredStatement active = statement("active", now.minus(Duration.ofHours(25)));+ active.status = RedshiftDataStatementStore.Status.SUBMITTED;+ store.put(active);+ store.sweep();+ assertNotNull(store.get("active"));+ }++ @Test+ void storedSnapshotDoesNotChangeWhenWorkerMutatesItsRecord() {+ RedshiftDataStatementStore store = new RedshiftDataStatementStore(24, Clock.systemUTC());+ RedshiftDataStatementStore.StoredStatement original = statement("snapshot", Instant.now());+ original.status = RedshiftDataStatementStore.Status.STARTED;+ store.put(original);+ original.status = RedshiftDataStatementStore.Status.FINISHED;+ assertEquals(RedshiftDataStatementStore.Status.STARTED, store.get("snapshot").status);+ store.get("snapshot").status = RedshiftDataStatementStore.Status.FAILED;+ assertEquals(RedshiftDataStatementStore.Status.STARTED, store.get("snapshot").status);+ }+private static RedshiftDataStatementStore.StoredStatement statement(String id, Instant createdAt) {RedshiftDataStatementStore.StoredStatement s = new RedshiftDataStatementStore.StoredStatement();s.id = id;diff --git a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.javaindex 74c3b41a3d..c1b2039b71 100644--- a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java@@ -66,7 +66,6 @@ class ScheduleInvokerTest {private EcsService ecsService;private StepFunctionsService stepFunctionsService;private RedshiftDataService redshiftDataService;- private final List<Runnable> submittedStatements = new ArrayList<>();private ScheduleInvoker invoker;@BeforeEach@@ -86,10 +85,9 @@ class ScheduleInvokerTest {when(config.baseUrl()).thenReturn("http://localhost:4566");ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(redshiftEnabled && redshiftDataEnabled);- // Queue instead of run, so a test can see that the invoker returns before the SQL runs.return new ScheduleInvoker(sqsService, lambdaService, snsService,eventBridgeService, ecsService, stepFunctionsService, redshiftDataService,- new ObjectMapper(), config, serviceRegistry, submittedStatements::add);+ new ObjectMapper(), config, serviceRegistry);}/** Delivers one occurrence of a schedule in {@code region} whose target is {@code target}. */@@ -385,7 +383,7 @@ class ScheduleInvokerTest {invoke(target, "eu-west-1");ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);- verify(redshiftDataService).submitStatement(request.capture(), eq("eu-west-1"));+ verify(redshiftDataService).executeStatement(request.capture(), eq("eu-west-1"), eq("arn:aws:iam::000000000000:role/x"));assertEquals("select 1", request.getValue().path("Sql").asText());assertEquals("analytics", request.getValue().path("ClusterIdentifier").asText());}@@ -400,7 +398,7 @@ class ScheduleInvokerTest {invoke(target, "us-east-1");ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);- verify(redshiftDataService).submitStatement(request.capture(), eq("us-east-1"));+ verify(redshiftDataService).executeStatement(request.capture(), eq("us-east-1"), eq("arn:aws:iam::000000000000:role/x"));assertEquals("wg-1", request.getValue().path("WorkgroupName").asText());assertTrue(request.getValue().path("ClusterIdentifier").isMissingNode());}@@ -416,7 +414,7 @@ class ScheduleInvokerTest {invoke(target, "us-east-1");ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);- verify(redshiftDataService).submitBatch(request.capture(), eq("us-east-1"));+ verify(redshiftDataService).batchExecuteStatement(request.capture(), eq("us-east-1"), eq("arn:aws:iam::000000000000:role/x"));assertEquals(2, request.getValue().path("Sqls").size());}@@ -426,42 +424,21 @@ class ScheduleInvokerTest {target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");target.setRoleArn("arn:aws:iam::000000000000:role/x");target.setInput("{\"Sql\":\"select 1\"}");- when(redshiftDataService.submitStatement(any(JsonNode.class), anyString()))+ when(redshiftDataService.executeStatement(any(JsonNode.class), anyString(), anyString())).thenThrow(new AwsException("ValidationException", "no cluster", 400));assertThrows(AwsException.class, () -> invoke(target, "us-east-1"));- assertTrue(submittedStatements.isEmpty());}@Test- void universalRedshiftDataRunsTheStatementOffTheCallingThread() {+ void universalRedshiftDataDelegatesToSharedAdmission() {Target target = new Target();target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");target.setRoleArn("arn:aws:iam::000000000000:role/x");target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}");- Runnable statement = mock(Runnable.class);- when(redshiftDataService.submitStatement(any(JsonNode.class), anyString())).thenReturn(statement);-- invoke(target, "us-east-1");-- verifyNoInteractions(statement);- assertEquals(1, submittedStatements.size());- submittedStatements.get(0).run();- verify(statement).run();- }-- @Test- void universalRedshiftDataFailedStatementDoesNotEscapeTheExecutor() {- Target target = new Target();- target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");- target.setRoleArn("arn:aws:iam::000000000000:role/x");- target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}");- when(redshiftDataService.submitStatement(any(JsonNode.class), anyString()))- .thenReturn(() -> { throw new IllegalStateException("boom"); });-invoke(target, "us-east-1");-- submittedStatements.get(0).run();+ verify(redshiftDataService).executeStatement(any(JsonNode.class), eq("us-east-1"),+ eq("arn:aws:iam::000000000000:role/x"));}@ParameterizedTest@@ -853,4 +830,21 @@ class ScheduleInvokerTest {verify(sqsService).sendMessage(anyString(), body.capture(), eq(0), isNull(), isNull(), eq("us-east-1"));assertTrue(body.getValue().matches("[0-9a-f]{16}/1"), body.getValue());}++ @Test+ void universalRedshiftDataDoesNotWaitOnTheSharedDispatcherThread() {+ Target target = new Target();+ target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");+ target.setRoleArn("arn:aws:iam::000000000000:role/x");+ target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\","+ + "\"WaitTimeSeconds\":30}");++ invoke(target, "us-east-1");++ ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);+ verify(redshiftDataService).executeStatement(request.capture(), eq("us-east-1"),+ eq("arn:aws:iam::000000000000:role/x"));+ assertTrue(request.getValue().path("WaitTimeSeconds").isMissingNode());+ assertEquals("select 1", request.getValue().path("Sql").asText());+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.javaindex b9dc0f5bf2..c6b04ad445 100644--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.java@@ -65,8 +65,16 @@ class StepFunctionsRedshiftDataDockerIntegrationTest {"Type": "Task","Resource": "arn:aws:states:::aws-sdk:redshiftdata:describeStatement","Arguments": {"Id": "{% $states.input.Id %}"},- "End": true- }+ "Output": "{% $states.result %}",+ "Next": "Check"+ },+ "Check": {+ "Type": "Choice",+ "Choices": [{"Condition": "{% $states.input.Status = 'FINISHED' or $states.input.Status = 'FAILED' or $states.input.Status = 'ABORTED' %}", "Next": "Done"}],+ "Default": "Wait"+ },+ "Wait": {"Type": "Wait", "Seconds": 1, "Next": "Describe"},+ "Done": {"Type": "Succeed"}}}""".replace("CLUSTER", CLUSTER_ID);@@ -95,8 +103,16 @@ class StepFunctionsRedshiftDataDockerIntegrationTest {"Type": "Task","Resource": "arn:aws:states:::aws-sdk:redshiftdata:describeStatement","Arguments": {"Id": "{% $states.input.Id %}"},- "End": true- }+ "Output": "{% $states.result %}",+ "Next": "Check"+ },+ "Check": {+ "Type": "Choice",+ "Choices": [{"Condition": "{% $states.input.Status = 'FINISHED' or $states.input.Status = 'FAILED' or $states.input.Status = 'ABORTED' %}", "Next": "Done"}],+ "Default": "Wait"+ },+ "Wait": {"Type": "Wait", "Seconds": 1, "Next": "Describe"},+ "Done": {"Type": "Succeed"}}}""".replace("CLUSTER", CLUSTER_ID);diff --git a/src/test/resources/application.yml b/src/test/resources/application.ymlindex 5b73768483..fa3e7fcbcf 100644--- a/src/test/resources/application.yml+++ b/src/test/resources/application.yml@@ -222,6 +222,9 @@ floci:redshift-data:enabled: trueresult-ttl-hours: 24+ max-concurrent-statements: 10+ queue-capacity: 100+ shutdown-timeout-seconds: 10redshift-serverless:enabled: trueneptune: