diff --git a/docs/services/redshift-data.md b/docs/services/redshift-data.md index 0ff6337ba9..4628485dbb 100644 --- a/docs/services/redshift-data.md +++ b/docs/services/redshift-data.md @@ -43,6 +43,69 @@ A request identifies its target cluster or serverless workgroup one of three way A statement run against a workgroup reports `WorkgroupName` and omits `ClusterIdentifier`, and the reverse holds for a cluster statement. `ListDatabases`, `ListSchemas`, `ListTables` and `DescribeTable` accept `WorkgroupName` the same way. +## Session reuse + +`ExecuteStatement` and `BatchExecuteStatement` accept `SessionKeepAliveSeconds`, an integer from +0 to 86400. A positive value retains the JDBC connection after the statement completes and +returns its `SessionId`. Subsequent execute or batch calls can send just that `SessionId` and +their SQL. The database, cluster or workgroup, database user and secret are inherited from the +first request. Optional target fields on a reuse request must match the original session. +Temporary tables and session settings survive between calls, and Execute and Batch can share +the same session. Metadata operations such as `ListTables` still open independent connections. + +`BEGIN` or `START TRANSACTION` opens an explicit transaction for later Execute calls; +`COMMIT`/`END` commits it, and `ROLLBACK`/`ABORT` rolls it back. SQL errors and accepted running cancellation +leave an explicit transaction aborted: later Execute calls fail until COMMIT or ROLLBACK ends +the block, rolling back its changes. A batch remains a transaction boundary: its success commits the +connection and its failure rolls it back, including any transaction opened by earlier Execute +calls on that session. Transaction-control modifiers beyond optional `WORK` or `TRANSACTION` +are rejected before execution. These cross-call transaction details have not been verified against live AWS. + +An omitted keep-alive on a reuse request preserves the previous idle timeout. A supplied value +replaces it after successful admission, and the idle deadline starts when the query completes. +Sending 0 with an existing `SessionId` closes the connection after that query. Omitting both +fields, or sending 0 without a session id, keeps the existing connection-per-request behavior. + +Only one query can be admitted to a session at a time, including while that query waits for a +worker. A different query against a busy session returns `ValidationException`; it is not queued +behind the first query. Retrying the first query's `ClientToken` returns its original statement +and session ids. Queue or admission-storage failures release their reservation without changing +an existing session's idle deadline. Each cluster or workgroup supports up to 500 retained +sessions, shared across callers and database names. + +Sessions are scoped to the signing account, Region and caller independently of `WithEvent`. +Floci resolves the caller's IAM ARN when possible and otherwise uses the access key identity; +an internal caller can supply its captured principal. A foreign, expired or reset session returns +`ResourceNotFoundException`. These exact error choices and optional-field matching rules have +not been compared with live AWS. A running session lasts at most 24 hours from creation; +a one-second sweep cancels active work and closes connections at that deadline. + +Session ids appear in Execute, Batch, DescribeStatement and ListStatements responses. The id +is saved with statement metadata and idempotency receipts, but live connections are runtime-only: +reset and shutdown close them, and restarting Floci never reopens a session. A session request +with an existing receipt can still replay its original response after the connection expires or +after restart, subject to the normal receipt lifetime and caller ownership. `ListSessions` is +not implemented. + +Unexpected synchronous connection-cleanup failures are reported to the affected operation. +Reset still attempts to close the remaining sessions and clear statement and receipt state before +reporting cleanup errors. Background expiration failures are logged without stopping later sweeps. +An already-closed connection (`SQLSTATE 08003`) is a benign cleanup outcome. + +```python +created = data.execute_statement( + WorkgroupName="analytics", Database="dev", SessionKeepAliveSeconds=60, + Sql="CREATE TEMP TABLE staging(id integer)") +# Poll DescribeStatement until FINISHED before submitting another query in this session. +session_id = created["SessionId"] +inserted = data.execute_statement(SessionId=session_id, Sql="INSERT INTO staging VALUES (42)") +# Poll inserted["Id"] before the next call. +selected = data.execute_statement(SessionId=session_id, Sql="SELECT * FROM staging") +``` + +See [AWS session reuse guidance](https://docs.aws.amazon.com/redshift/latest/mgmt/data-api.html) +for the idle timeout, 24-hour lifetime and single-query constraints. + ## Compatibility Notes - **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`. @@ -72,7 +135,7 @@ reported storage errors and queue-admission failures do not consume the token. A admission does consume it. `WaitTimeSeconds` on a retry can wait for the original execution without submitting another one. -Request validation and database-target resolution run before admission coordination. Only requests +Request validation and database-target resolution run before admission coordination for new connections. Only requests sharing an account, region, execute operation and token share an admission lock; tokenless requests use no token lock. The executor reserves queue capacity before writing admission state, so a full queue cannot create a receipt that reappears after restart. Workers run SQL only after admission @@ -91,8 +154,10 @@ The token deadline is independent of result retention: if a configured result TT 8 hours removes the statement, its receipt still returns the original admission response until the token expires, without restoring results. -Floci scopes tokens by account, region and execute operation. It validates the request and resolves -the database target before looking up a receipt, including on retries. Within that scope, reusing +Floci scopes tokens by account, region and execute operation. Requests for new connections validate +and resolve the database target before looking up a receipt, including on retries. SessionId-only +retries check an existing receipt first so a closed connection does not prevent replay; receipts +for retained sessions additionally check caller ownership. Within that scope, reusing a token with changed SQL, parameters or event settings replays the original operation; it does not change that operation. The precise AWS behavior for changed payloads, different IAM principals, cross-operation reuse and responses after result eviction has not been verified against a live @@ -121,8 +186,7 @@ failure rolls back the batch. External read or preparation failures are stored a reported 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. -Data API session reuse is not supported. The legacy Phase 1 catalog -is not the Glue-backed relational path. +The legacy Phase 1 catalog is not the Glue-backed relational path. ## Configuration 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.java index 582dd51580..24e2080736 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecution.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecution.java @@ -95,13 +95,10 @@ final class RedshiftDataExecution { return true; } - void signalCancellation() { - Statement activeStatement; - Connection activeConnection; - synchronized (this) { - activeStatement = statement; - activeConnection = connection; - } + synchronized void signalCancellation() { + // terminal() must wait until the old cancellation reaches JDBC before the connection can be reused. + Statement activeStatement = statement; + Connection activeConnection = connection; try { if (activeStatement != null) { activeStatement.cancel(); diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataIdempotencyStore.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataIdempotencyStore.java index ec8a92a1c6..b35562c50f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataIdempotencyStore.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataIdempotencyStore.java @@ -62,10 +62,15 @@ class RedshiftDataIdempotencyStore { } void put(Scope scope, ObjectNode response) { + put(scope, response, null); + } + + void put(Scope scope, ObjectNode response, String sessionOwner) { Receipt receipt = new Receipt(); receipt.acceptedAt = clock.instant(); receipt.expiresAt = receipt.acceptedAt.plus(TOKEN_TTL); receipt.response = response.deepCopy(); + receipt.sessionOwner = sessionOwner; mutations.readLock().lock(); try { receipts.putAllAtomicallyForAccount(scope.accountId(), Map.of(scope.key(), receipt)); @@ -137,12 +142,14 @@ class RedshiftDataIdempotencyStore { Instant acceptedAt; Instant expiresAt; ObjectNode response; + String sessionOwner; Receipt snapshot() { Receipt copy = new Receipt(); copy.acceptedAt = acceptedAt; copy.expiresAt = expiresAt; copy.response = response.deepCopy(); + copy.sessionOwner = sessionOwner; return copy; } } 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.java index 9919789763..335464f487 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 @@ -32,11 +32,14 @@ import java.util.ArrayList; import java.util.Base64; import java.util.Comparator; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Consumer; +import java.util.function.BiConsumer; +import java.util.regex.Matcher; import java.util.regex.Pattern; @ApplicationScoped @@ -48,6 +51,10 @@ public class RedshiftDataService implements Resettable { + "(?: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 static final Pattern TRANSACTION_CONTROL = Pattern.compile( + "(?is)\\A(BEGIN|START\\s+TRANSACTION|COMMIT|END|ROLLBACK|ABORT)(?:\\s+(?:WORK|TRANSACTION))?\\s*;?\\s*\\z"); + private static final Pattern TRANSACTION_PREFIX = Pattern.compile("(?is)\\A(?:BEGIN|START|COMMIT|END|ROLLBACK|ABORT)\\b"); + private static final Pattern TRANSACTION_COMMENTS = Pattern.compile("(?s)/\\*.*?\\*/|--[^\\r\\n]*"); private final RedshiftDataResourceResolver resolver; private final RedshiftDataConnectionFactory connectionFactory; @@ -59,6 +66,7 @@ public class RedshiftDataService implements Resettable { private final RequestContext requestContext; private final IamService iamService; private final RedshiftDataIdempotencyStore idempotency; + private final RedshiftDataSessions sessions; private final ConcurrentHashMap admissionLocks = new ConcurrentHashMap<>(); RedshiftDataService(RedshiftDataResourceResolver resolver, RedshiftDataConnectionFactory connectionFactory, @@ -69,11 +77,20 @@ public class RedshiftDataService implements Resettable { Clock.systemUTC())); } + RedshiftDataService(RedshiftDataResourceResolver resolver, RedshiftDataConnectionFactory connectionFactory, + RedshiftDataStatementStore store, ObjectMapper objectMapper, SpectrumQueryPreparation preparation, + RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher, + RequestContext requestContext, IamService iamService, RedshiftDataIdempotencyStore idempotency) { + this(resolver, connectionFactory, store, objectMapper, preparation, executor, publisher, + requestContext, iamService, idempotency, new RedshiftDataSessions()); + } + @Inject public RedshiftDataService(RedshiftDataResourceResolver resolver, RedshiftDataConnectionFactory connectionFactory, RedshiftDataStatementStore store, ObjectMapper objectMapper, SpectrumQueryPreparation preparation, RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher, - RequestContext requestContext, IamService iamService, RedshiftDataIdempotencyStore idempotency) { + RequestContext requestContext, IamService iamService, RedshiftDataIdempotencyStore idempotency, + RedshiftDataSessions sessions) { this.resolver = resolver; this.connectionFactory = connectionFactory; this.store = store; @@ -84,23 +101,33 @@ public class RedshiftDataService implements Resettable { this.requestContext = requestContext; this.iamService = iamService; this.idempotency = idempotency; + this.sessions = sessions; } @Override public void clear() { executor.clear(() -> { RuntimeException failure = null; + try { + sessions.clear(); + } catch (RuntimeException sessionFailure) { + failure = sessionFailure; + } try { store.clear(); } catch (RuntimeException storageFailure) { - failure = storageFailure; + if (failure == null) { + failure = storageFailure; + } else if (failure != storageFailure) { + failure.addSuppressed(storageFailure); + } } try { idempotency.clear(); } catch (RuntimeException receiptFailure) { if (failure == null) { failure = receiptFailure; - } else { + } else if (failure != receiptFailure) { failure.addSuppressed(receiptFailure); } } @@ -123,12 +150,31 @@ public class RedshiftDataService implements Resettable { private ObjectNode execute(JsonNode request, String region, String principal, boolean batch) { String token = RedshiftDataIdempotencyStore.clientToken(request); int waitSeconds = waitSeconds(request); - PendingStatement pending = batch ? prepareBatch(request, region) : prepareStatement(request, region); + RedshiftDataSessions.validate(request); + boolean retained = request.has("SessionId") || request.path("SessionKeepAliveSeconds").asInt() > 0; + RedshiftDataSessions.Scope owner = retained ? sessionScope(region, principal) + : new RedshiftDataSessions.Scope(store.accountId(), region, null); + if (token != null && request.has("SessionId")) { + RedshiftDataIdempotencyStore.Scope retryScope = new RedshiftDataIdempotencyStore.Scope( + owner.accountId(), region, batch ? "BatchExecuteStatement" : "ExecuteStatement", token); + Admission replay = executor.admitting(() -> { + RedshiftDataIdempotencyStore.Receipt receipt = idempotency.find(retryScope); + return receipt == null ? null : replay(receipt, retryScope, owner, principal); + }); + if (replay != null) { + return awaitAdmission(replay, waitSeconds); + } + } + PendingStatement pending = batch ? prepareBatch(request, region, owner) : prepareStatement(request, region, owner); RedshiftDataStatementStore.StoredStatement stored = pending.statement(); RedshiftDataIdempotencyStore.Scope scope = token == null ? null : new RedshiftDataIdempotencyStore.Scope( stored.accountId, region, batch ? "BatchExecuteStatement" : "ExecuteStatement", token); Admission admission = executor.admitting(() -> scope == null ? submit(pending, principal, null) : admitScoped(pending, principal, scope)); + return awaitAdmission(admission, waitSeconds); + } + + private ObjectNode awaitAdmission(Admission admission, int waitSeconds) { if (waitSeconds > 0 && admission.execution() != null) { admission.execution().await(Duration.ofSeconds(waitSeconds)); RedshiftDataStatementStore.StoredStatement current = store.getForAccount(admission.accountId(), admission.id()); @@ -139,6 +185,30 @@ public class RedshiftDataService implements Resettable { return admission.response(); } + private RedshiftDataSessions.Scope sessionScope(String region, String principal) { + String caller = principal; + if (caller == null && requestContext != null && requestContext.getAccessKeyId() != null) { + String accessKey = requestContext.getAccessKeyId(); + caller = iamService == null ? "access-key:" + accessKey + : iamService.resolveCallerArn(accessKey).orElse("access-key:" + accessKey); + } + return new RedshiftDataSessions.Scope(store.accountId(), region, caller == null ? "anonymous" : caller); + } + + private Admission replay(RedshiftDataIdempotencyStore.Receipt receipt, RedshiftDataIdempotencyStore.Scope scope, + RedshiftDataSessions.Scope owner, String principal) { + if (receipt.sessionOwner != null) { + String caller = owner.caller() == null ? sessionScope(scope.region(), principal).caller() : owner.caller(); + if (!Objects.equals(receipt.sessionOwner, caller)) { + throw new AwsException("ResourceNotFoundException", "Session was not found", 400); + } + } + String id = receipt.response.path("Id").asText(); + RedshiftDataStatementStore.StoredStatement current = store.getForAccount(scope.accountId(), id); + return new Admission(scope.accountId(), id, current == null ? receipt.response : executeResponse(current), + executor.execution(scope.accountId(), id)); + } + private record Admission(String accountId, String id, ObjectNode response, RedshiftDataExecution execution) { } private static final class AdmissionLock { @@ -158,10 +228,7 @@ public class RedshiftDataService implements Resettable { if (receipt == null) { return submit(pending, principal, scope); } - String id = receipt.response.path("Id").asText(); - RedshiftDataStatementStore.StoredStatement current = store.getForAccount(scope.accountId(), id); - return new Admission(scope.accountId(), id, current == null ? receipt.response : executeResponse(current), - executor.execution(scope.accountId(), id)); + return replay(receipt, scope, pending.owner(), principal); } finally { scoped.lock.unlock(); admissionLocks.compute(scope, (key, existing) -> --scoped.users == 0 ? null : scoped); @@ -173,6 +240,10 @@ public class RedshiftDataService implements Resettable { if (principal != null) { stored.principal = principal; } + RedshiftDataSessions.Reservation session = sessions.reserve(pending.owner(), pending.request(), pending.target()); + if (session != null) { + stored.sessionId = session.id(); + } ObjectNode response = executeResponse(stored.snapshot()); RedshiftDataExecution handle; try { @@ -182,23 +253,41 @@ public class RedshiftDataService implements Resettable { store.putForAccount(stored.accountId, stored); } else { store.admitForAccount(stored.accountId, stored); - idempotency.put(scope, response); + if (session == null) { + idempotency.put(scope, response); + } else { + idempotency.put(scope, response, pending.owner().caller()); + } + } + if (session != null) { + session.admitted(); } }, execution -> { try { execution.checkCancellation(); + if (session != null) { + session.bind(execution); + } stored.status = RedshiftDataStatementStore.Status.STARTED; executor.publish(execution, () -> store.putForAccount(stored.accountId, stored)); - pending.run().accept(execution); + pending.run().accept(execution, session); } catch (Exception failure) { stored.status = RedshiftDataStatementStore.Status.FAILED; stored.error = failure.getMessage(); + } finally { + execution.terminal(); + if (session != null) { + session.finish(); + } } complete(stored, execution); }, (execution, failure) -> recoverStatement(stored, execution, failure)); } catch (RuntimeException failure) { + if (session != null) { + session.abort(); + } try { if (scope != null) { idempotency.removeIfStatement(scope, stored.id); @@ -275,54 +364,121 @@ public class RedshiftDataService implements Resettable { } } - private PendingStatement prepareStatement(JsonNode request, String region) { + private PendingStatement prepareStatement(JsonNode request, String region, RedshiftDataSessions.Scope owner) { String sql = requiredText(request, "Sql"); rejectMultiStatement(sql); Map parameters = RedshiftDataSqlParameters.parseParameters(request, "Parameters"); - RedshiftDataResourceResolver.DatabaseTarget target = resolver.resolve(request, region); + ResolvedTarget resolved = resolveTarget(request, region, owner); + RedshiftDataResourceResolver.DatabaseTarget target = resolved.target(); RedshiftDataStatementStore.StoredStatement stored = newStatement(request); stored.sql = sql; stored.sqls = List.of(sql); - stored.clusterIdentifier = clusterIdentifierOrNull(request); - stored.workgroupName = workgroupNameOrNull(request); + stored.clusterIdentifier = clusterIdentifierOrNull(resolved.parameters()); + stored.workgroupName = workgroupNameOrNull(resolved.parameters()); stored.dbUser = target.user(); stored.database = target.database(); stored.resultFormat = request.path("ResultFormat").asText("JSON"); capture(stored, target, region, request); - return new PendingStatement(stored, execution -> runStatement(stored, target, sql, parameters, execution)); + return new PendingStatement(stored, (execution, session) -> runStatement(stored, target, sql, parameters, execution, session), + target, request.deepCopy(), owner); } private record PendingStatement(RedshiftDataStatementStore.StoredStatement statement, - Consumer run) { } + BiConsumer run, + RedshiftDataResourceResolver.DatabaseTarget target, JsonNode request, + RedshiftDataSessions.Scope owner) { } + + private record ResolvedTarget(RedshiftDataResourceResolver.DatabaseTarget target, JsonNode parameters) { } + + private ResolvedTarget resolveTarget(JsonNode request, String region, RedshiftDataSessions.Scope owner) { + if (request.has("SessionId")) { + RedshiftDataSessions.Session session = sessions.find(owner, request.get("SessionId").asText()); + return new ResolvedTarget(session.target(request), session.parameters()); + } + return new ResolvedTarget(resolver.resolve(request, region), request); + } + + private ConnectionUse connection(RedshiftDataResourceResolver.DatabaseTarget target, + RedshiftDataSessions.Reservation session) throws SQLException { + return new ConnectionUse(session == null ? connectionFactory.open(target) : session.connection(connectionFactory), session); + } + + private record ConnectionUse(Connection connection, RedshiftDataSessions.Reservation session) implements AutoCloseable { + @Override + public void close() throws SQLException { + if (session == null) { + connection.close(); + return; + } + try { + if (connection.isClosed()) { + session.discard(); + } else if (!session.inTransaction() && !connection.getAutoCommit()) { + // A failed batch or worker must not leak an open transaction to the next request. + connection.rollback(); + connection.setAutoCommit(true); + } + } catch (SQLException | RuntimeException failure) { + try { + session.discard(); + } catch (RuntimeException cleanupFailure) { + failure.addSuppressed(cleanupFailure); + } + throw failure; + } + } + } private void runStatement(RedshiftDataStatementStore.StoredStatement stored, RedshiftDataResourceResolver.DatabaseTarget target, - String sql, Map parameters, RedshiftDataExecution execution) { - try (Connection connection = connectionFactory.open(target)) { + String sql, Map parameters, RedshiftDataExecution execution, + RedshiftDataSessions.Reservation session) { + try (ConnectionUse use = connection(target, session)) { + Connection connection = use.connection(); execution.track(connection); try { + if (session != null && transactionControl(stored, sql, connection, session, execution)) { + return; + } + if (session != null && session.transactionFailed()) { + throw new SQLException("Current transaction is aborted; use COMMIT or ROLLBACK to end it", "25P02"); + } boolean autoCommit = AUTO_COMMIT_SQL.matcher(sql).find() || preparation != null && target.spectrum() != null && preparation.handlesDdl(sql, target.spectrum()); if (autoCommit) { + if (session != null && session.inTransaction()) { + throw new SQLException("Statement cannot run inside a transaction block", "25001"); + } 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()) { + if ((session == null || !session.inTransaction()) && !connection.getAutoCommit()) { connection.commit(); } }); } } catch (SQLException failure) { - if (!connection.getAutoCommit()) { + if (session != null && failure.getSQLState() != null && failure.getSQLState().startsWith("08")) { + session.discard(); + throw failure; + } + if (session != null && session.inTransaction()) { + session.failTransaction(); + } else if (!connection.getAutoCommit()) { connection.rollback(); } throw failure; + } catch (RuntimeException failure) { + if (session != null && session.inTransaction()) { + session.failTransaction(); + } + throw failure; } } catch (SQLException e) { stored.status = RedshiftDataStatementStore.Status.FAILED; @@ -331,6 +487,52 @@ public class RedshiftDataService implements Resettable { stored.updatedAt = Instant.now(); } + private boolean transactionControl(RedshiftDataStatementStore.StoredStatement stored, String sql, + Connection connection, RedshiftDataSessions.Reservation session, + RedshiftDataExecution execution) throws SQLException { + String normalized = TRANSACTION_COMMENTS.matcher(sql).replaceAll(" ").trim(); + Matcher control = TRANSACTION_CONTROL.matcher(normalized); + if (!control.matches()) { + if (TRANSACTION_PREFIX.matcher(normalized).find()) { + throw new SQLException("Transaction control modifiers are not supported", "0A000"); + } + return false; + } + String command = control.group(1).toUpperCase(Locale.ROOT).split("\\s+")[0]; + execution.commit(() -> { + switch (command) { + case "BEGIN", "START" -> { + if (session.transactionFailed()) { + throw new SQLException("Current transaction is aborted; use COMMIT or ROLLBACK to end it", "25P02"); + } + connection.setAutoCommit(false); + session.transaction(true); + } + case "COMMIT", "END" -> { + if (!connection.getAutoCommit()) { + if (session.transactionFailed()) { + connection.rollback(); + } else { + connection.commit(); + } + } + session.transaction(false); + connection.setAutoCommit(true); + } + default -> { + if (!connection.getAutoCommit()) { + connection.rollback(); + } + session.transaction(false); + connection.setAutoCommit(true); + } + } + }); + stored.status = RedshiftDataStatementStore.Status.FINISHED; + stored.updatedAt = Instant.now(); + return true; + } + /** * Redshift has no line, json or jsonb type and fails a statement that produces one with * exactly these messages (checked on a real cluster 2026-09-13). @@ -419,7 +621,11 @@ public class RedshiftDataService implements Resettable { response.putArray("DbGroups"); putWorkgroupName(response, stored); response.put("HasResultSet", stored.hasResultSet); - response.putNull("SessionId"); + if (stored.sessionId == null) { + response.putNull("SessionId"); + } else { + response.put("SessionId", stored.sessionId); + } return response; } @@ -428,6 +634,9 @@ public class RedshiftDataService implements Resettable { public ObjectNode describeStatement(JsonNode request) { RedshiftDataStatementStore.StoredStatement stored = require(request); ObjectNode response = objectMapper.createObjectNode(); + if (stored.sessionId != null) { + response.put("SessionId", stored.sessionId); + } response.put("Id", stored.id); response.put("Status", stored.status.name()); response.put("CreatedAt", epochSeconds(stored.createdAt)); @@ -566,7 +775,7 @@ public class RedshiftDataService implements Resettable { return execute(request, region, principal, true); } - private PendingStatement prepareBatch(JsonNode request, String region) { + private PendingStatement prepareBatch(JsonNode request, String region, RedshiftDataSessions.Scope owner) { JsonNode sqlsNode = request.get("Sqls"); if (sqlsNode == null || !sqlsNode.isArray() || sqlsNode.isEmpty()) { throw new AwsException("ValidationException", "Sqls must be a non-empty array of SQL statements.", 400); @@ -576,27 +785,40 @@ public class RedshiftDataService implements Resettable { sqls.add(n.asText()); } - RedshiftDataResourceResolver.DatabaseTarget target = resolver.resolve(request, region); + ResolvedTarget resolved = resolveTarget(request, region, owner); + RedshiftDataResourceResolver.DatabaseTarget target = resolved.target(); RedshiftDataStatementStore.StoredStatement parent = newStatement(request); parent.batch = true; parent.sql = String.join("; ", sqls); parent.sqls = sqls; - parent.clusterIdentifier = clusterIdentifierOrNull(request); - parent.workgroupName = workgroupNameOrNull(request); + parent.clusterIdentifier = clusterIdentifierOrNull(resolved.parameters()); + parent.workgroupName = workgroupNameOrNull(resolved.parameters()); parent.dbUser = target.user(); parent.database = target.database(); parent.resultFormat = request.path("ResultFormat").asText("JSON"); parent.subStatements = new ArrayList<>(); capture(parent, target, region, request); - return new PendingStatement(parent, execution -> runBatch(parent, sqls, target, execution)); + return new PendingStatement(parent, (execution, session) -> runBatch(parent, sqls, target, execution, session), + target, request.deepCopy(), owner); } private void runBatch(RedshiftDataStatementStore.StoredStatement parent, List sqls, - RedshiftDataResourceResolver.DatabaseTarget target, RedshiftDataExecution execution) { + RedshiftDataResourceResolver.DatabaseTarget target, RedshiftDataExecution execution, + RedshiftDataSessions.Reservation session) { long totalDuration = 0; - try (Connection connection = connectionFactory.open(target)) { + if (session != null) { + if (session.transactionFailed()) { + parent.status = RedshiftDataStatementStore.Status.FAILED; + parent.error = "Current transaction is aborted; use COMMIT or ROLLBACK to end it"; + parent.updatedAt = Instant.now(); + return; + } + session.transaction(false); + } + try (ConnectionUse use = connection(target, session)) { + Connection connection = use.connection(); execution.track(connection); BackendSql batchBackend = new RedshiftDataBackendSql(connection, execution); boolean committed = false; @@ -612,6 +834,7 @@ public class RedshiftDataService implements Resettable { sub.workgroupName = parent.workgroupName; sub.database = parent.database; sub.dbUser = parent.dbUser; + sub.sessionId = parent.sessionId; sub.resultFormat = parent.resultFormat; Instant now = Instant.now(); sub.createdAt = now; @@ -628,6 +851,10 @@ public class RedshiftDataService implements Resettable { totalDuration += sub.durationNanos; parent.status = RedshiftDataStatementStore.Status.FAILED; parent.error = e.getMessage(); + if (session != null && e.getSQLState() != null && e.getSQLState().startsWith("08")) { + session.discard(); + throw e; + } connection.rollback(); failed = true; break; @@ -703,6 +930,9 @@ public class RedshiftDataService implements Resettable { RedshiftDataStatementStore.StoredStatement s = all.get(i); ObjectNode node = statements.addObject(); node.put("Id", s.id); + if (s.sessionId != null) { + node.put("SessionId", s.sessionId); + } node.put("QueryString", s.sql); ArrayNode queryStrings = node.putArray("QueryStrings"); for (String sql : s.sqls != null ? s.sqls : List.of(s.sql)) { diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessions.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessions.java new file mode 100644 index 0000000000..2466343f5e --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessions.java @@ -0,0 +1,423 @@ +package io.github.hectorvent.floci.services.redshiftdata; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import org.jboss.logging.Logger; + +import java.sql.Connection; +import java.sql.SQLException; +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.regex.Pattern; + +@ApplicationScoped +class RedshiftDataSessions implements AutoCloseable { + private static final Logger LOG = Logger.getLogger(RedshiftDataSessions.class); + private static final Pattern SESSION_ID = Pattern.compile("[a-z0-9]{8}(-[a-z0-9]{4}){3}-[a-z0-9]{12}(:\\d{0,2})?"); + private static final Duration MAX_AGE = Duration.ofHours(24); + private static final List TARGET_FIELDS = List.of("ClusterIdentifier", "WorkgroupName", "Database", "DbUser", "SecretArn"); + + private final ConcurrentHashMap sessions = new ConcurrentHashMap<>(); + private final Clock clock; + private ScheduledExecutorService sweeper; + private boolean closed; + + RedshiftDataSessions() { + this(Clock.systemUTC()); + } + + RedshiftDataSessions(Clock clock) { + this.clock = clock; + } + + @PostConstruct + void start() { + sweeper = Executors.newSingleThreadScheduledExecutor(task -> { + Thread thread = new Thread(task, "redshift-data-session-sweep"); + thread.setDaemon(true); + return thread; + }); + sweeper.scheduleAtFixedRate(this::sweepSafely, 1, 1, TimeUnit.SECONDS); + } + + static void validate(JsonNode request) { + keepAlive(request); + JsonNode id = request.get("SessionId"); + if (id != null && (!id.isTextual() || !SESSION_ID.matcher(id.textValue()).matches())) { + throw validation("SessionId must be a valid session identifier"); + } + } + + private static Integer keepAlive(JsonNode request) { + JsonNode seconds = request.get("SessionKeepAliveSeconds"); + if (seconds == null) { + return null; + } + if (!seconds.isIntegralNumber() || !seconds.canConvertToInt() || seconds.intValue() < 0 || seconds.intValue() > 86400) { + throw validation("SessionKeepAliveSeconds must be an integer between 0 and 86400"); + } + return seconds.intValue(); + } + + Session find(Scope owner, String id) { + Session session = sessions.get(id); + if (session == null || !session.owner.equals(owner)) { + throw missing(); + } + synchronized (session) { + if (!session.closed && !expired(session)) { + return session; + } + } + retire(session); + throw missing(); + } + + Reservation reserve(Scope owner, JsonNode request, RedshiftDataResourceResolver.DatabaseTarget target) { + Integer seconds = keepAlive(request); + String id = request.has("SessionId") ? request.get("SessionId").asText() : null; + if (id == null && (seconds == null || seconds == 0)) { + return null; + } + Session session; + boolean created = id == null; + if (created) { + sweepSafely(); + synchronized (this) { + if (closed) { + throw validation("Data API sessions are shutting down"); + } + long count = sessions.values().stream().filter(existing -> existing.target.arn().equals(target.arn()) + && existing.owner.accountId().equals(owner.accountId()) && existing.owner.region().equals(owner.region())).count(); + if (count >= 500) { + throw new AwsException("ActiveSessionsExceededException", "Too many active sessions for this resource", 400); + } + session = new Session(owner, target, request, clock.instant()); + sessions.put(session.id, session); + } + } else { + session = find(owner, id); + } + synchronized (session) { + if (session.closed || expired(session)) { + throw missing(); + } + if (session.active != null) { + throw validation("Session is busy; wait for its current statement to finish"); + } + Reservation reservation = new Reservation(session, created, seconds == null ? session.keepAlive : seconds); + session.active = reservation; + return reservation; + } + } + + void sweep() { + RuntimeException failure = null; + for (Session session : sessions.values()) { + boolean expire; + synchronized (session) { + expire = expired(session); + } + if (expire) { + try { + retire(session); + } catch (RuntimeException cleanupFailure) { + failure = accumulate(failure, cleanupFailure); + } + } + } + if (failure != null) { + throw failure; + } + } + + private void sweepSafely() { + try { + sweep(); + } catch (RuntimeException failure) { + // Maintenance has no request to report to; keep future sweeps running and report the failure. + LOG.errorv(failure, "Failed to clean up expired Redshift Data API sessions"); + } + } + + private boolean expired(Session session) { + Instant now = clock.instant(); + return !now.isBefore(session.createdAt.plus(MAX_AGE)) + || session.active == null && !now.isBefore(session.idleUntil); + } + + private void retire(Session session) { + retire(session, true); + } + + private void retire(Session session, boolean cancel) { + Retirement retirement; + synchronized (session) { + retirement = retireLocked(session, cancel); + } + cleanUp(retirement); + } + + private Retirement retireLocked(Session session, boolean cancel) { + if (session.closed) { + return null; + } + session.closed = true; + sessions.remove(session.id, session); + Retirement retirement = new Retirement(session.id, session.connection, cancel ? session.execution : null); + session.active = null; + session.execution = null; + session.connection = null; + return retirement; + } + + private record Retirement(String id, Connection connection, RedshiftDataExecution execution) { } + + private static void cleanUp(Retirement retirement) { + if (retirement == null) { + return; + } + Runnable cleanup = () -> { + try { + if (retirement.execution() != null) { + retirement.execution().cancel(); + } + } finally { + closeConnection(retirement.connection(), retirement.id()); + } + }; + if (retirement.execution() == null) { + cleanup.run(); + } else { + Thread.startVirtualThread(() -> { + try { + cleanup.run(); + } catch (RuntimeException failure) { + // Expiration/reset cancellation finishes asynchronously after the session is unavailable. + LOG.errorv(failure, "Failed to clean up Redshift Data API session {0}", retirement.id()); + } + }); + } + } + + private static void closeConnection(Connection connection, String id) { + if (connection != null) { + try { + connection.close(); + } catch (SQLException | RuntimeException failure) { + if (failure instanceof SQLException sql && "08003".equals(sql.getSQLState())) { + LOG.debugv("Redshift Data API session {0} connection was already closed", id); + } else { + throw new IllegalStateException("Unable to close Redshift Data API session " + id + ": " + failure.getMessage(), failure); + } + } + } + } + + synchronized void clear() { + RuntimeException failure = null; + for (Session session : sessions.values()) { + try { + retire(session); + } catch (RuntimeException cleanupFailure) { + failure = accumulate(failure, cleanupFailure); + } + } + if (failure != null) { + throw failure; + } + } + + private static RuntimeException accumulate(RuntimeException first, RuntimeException next) { + if (first == null) { + return next; + } + if (first != next) { + first.addSuppressed(next); + } + return first; + } + + @Override + @PreDestroy + public synchronized void close() { + closed = true; + if (sweeper != null) { + sweeper.shutdownNow(); + } + clear(); + } + + record Scope(String accountId, String region, String caller) { } + + static final class Session { + private final String id = UUID.randomUUID().toString(); + private final Scope owner; + private final RedshiftDataResourceResolver.DatabaseTarget target; + private final ObjectNode parameters; + private final Instant createdAt; + private Instant idleUntil; + private int keepAlive; + private boolean closed; + private boolean transaction; + private boolean transactionFailed; + private Connection connection; + private RedshiftDataExecution execution; + private Reservation active; + + private Session(Scope owner, RedshiftDataResourceResolver.DatabaseTarget target, JsonNode request, Instant createdAt) { + this.owner = owner; + this.target = target; + this.parameters = ((ObjectNode) request).objectNode(); + for (String field : TARGET_FIELDS) { + if (request.has(field)) { + parameters.set(field, request.get(field).deepCopy()); + } + } + this.createdAt = createdAt; + this.idleUntil = createdAt.plus(MAX_AGE); + } + + RedshiftDataResourceResolver.DatabaseTarget target(JsonNode request) { + for (String field : TARGET_FIELDS) { + JsonNode value = request.get(field); + if (value != null && !Objects.equals(value, parameters.get(field)) + && !("WorkgroupName".equals(field) && value.isTextual() && target.arn().equals(value.textValue()))) { + throw validation(field + " does not match the session"); + } + } + return target; + } + + ObjectNode parameters() { + return parameters.deepCopy(); + } + } + + final class Reservation { + private final Session session; + private final boolean created; + private final int seconds; + private final AtomicBoolean released = new AtomicBoolean(); + private boolean admitted; + + private Reservation(Session session, boolean created, int seconds) { + this.session = session; + this.created = created; + this.seconds = seconds; + } + + String id() { + return session.id; + } + + void admitted() { + admitted = true; + } + + void bind(RedshiftDataExecution execution) throws SQLException { + synchronized (session) { + if (session.closed || expired(session)) { + throw new SQLException("Session has expired", "08003"); + } + session.execution = execution; + } + } + + Connection connection(RedshiftDataConnectionFactory factory) throws SQLException { + synchronized (session) { + if (session.closed) { + throw new SQLException("Session has expired", "08003"); + } + if (session.connection != null) { + return session.connection; + } + } + Connection opened = factory.open(session.target); + synchronized (session) { + if (!session.closed && !expired(session)) { + session.connection = opened; + return opened; + } + } + closeConnection(opened, session.id); + throw new SQLException("Session has expired", "08003"); + } + + boolean inTransaction() { + return session.transaction; + } + + void transaction(boolean active) { + session.transaction = active; + if (!active) { + session.transactionFailed = false; + } + } + + boolean transactionFailed() { + return session.transactionFailed; + } + + void failTransaction() { + session.transactionFailed = true; + } + + void abort() { + release(false); + } + + void finish() { + release(admitted); + } + + void discard() { + retire(session, false); + } + + private void release(boolean completed) { + if (!released.compareAndSet(false, true)) { + return; + } + Retirement retirement = null; + synchronized (session) { + if (session.active != this) { + return; + } + boolean remove = !completed && created || completed && (seconds == 0 || session.connection == null); + if (remove) { + retirement = retireLocked(session, false); + } else { + session.active = null; + session.execution = null; + if (completed) { + session.keepAlive = seconds; + session.idleUntil = clock.instant().plusSeconds(seconds); + } + } + } + cleanUp(retirement); + } + } + + private static AwsException validation(String message) { + return new AwsException("ValidationException", message, 400); + } + + private static AwsException missing() { + return new AwsException("ResourceNotFoundException", "Session was not found or is no longer available", 400); + } +} 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.java index c420032f81..879952d86f 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 @@ -130,6 +130,10 @@ class RedshiftDataStatementStore { statements.clear(); } + String accountId() { + return statements.accountId(); + } + void sweep() { if (idempotency != null) { try { @@ -171,6 +175,7 @@ class RedshiftDataStatementStore { String workgroupName; String database; String dbUser; + String sessionId; String resultFormat = "JSON"; Status status; String error; @@ -214,6 +219,7 @@ class RedshiftDataStatementStore { copy.workgroupName = workgroupName; copy.database = database; copy.dbUser = dbUser; + copy.sessionId = sessionId; copy.resultFormat = resultFormat; copy.status = status; copy.error = error; 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.java index 5e66ec0b30..1cab126483 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 @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.redshiftdata; import io.github.hectorvent.floci.services.redshift.RedshiftService; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.common.http.TestHTTPResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.ExtractableResponse; import io.restassured.response.Response; @@ -12,7 +13,14 @@ import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient; +import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementResponse; +import software.amazon.awssdk.services.redshiftdata.model.ResourceNotFoundException; +import java.net.URI; import java.time.Duration; import java.util.List; @@ -20,6 +28,8 @@ 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.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @QuarkusTest @@ -31,6 +41,9 @@ class RedshiftDataApiIntegrationTest { @Inject RedshiftService redshift; + @TestHTTPResource("/") + URI endpoint; + private String clusterId; @BeforeAll @@ -87,6 +100,34 @@ class RedshiftDataApiIntegrationTest { return ((Number) value).intValue(); } + @Test + void sdkSessionPreservesPostgresTemporaryTablesAcrossExecuteAndBatch() { + try (RedshiftDataClient data = RedshiftDataClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build()) { + ExecuteStatementResponse created = data.executeStatement(request -> request.clusterIdentifier(clusterId) + .dbUser("admin").database("dev").sessionKeepAliveSeconds(60) + .sql("CREATE TEMP TABLE session_rows(id integer)")); + assertNotNull(created.sessionId()); + awaitFinished(created.id()); + String session = created.sessionId(); + awaitFinished(data.batchExecuteStatement(request -> request.sessionId(session) + .sqls("INSERT INTO session_rows VALUES (41)", "INSERT INTO session_rows VALUES (42)")).id()); + awaitFinished(data.executeStatement(request -> request.sessionId(session).sql("VACUUM")).id()); + awaitFinished(data.executeStatement(request -> request.sessionId(session).sql("BEGIN")).id()); + awaitFinished(data.executeStatement(request -> request.sessionId(session).sql("INSERT INTO session_rows VALUES (99)")).id()); + awaitFinished(data.executeStatement(request -> request.sessionId(session).sql("ROLLBACK")).id()); + String query = data.executeStatement(request -> request.sessionId(session).sql("SELECT id FROM session_rows ORDER BY id")).id(); + awaitFinished(query); + assertEquals(session, data.describeStatement(request -> request.id(query)).sessionId()); + assertEquals(2L, data.getStatementResult(request -> request.id(query)).totalNumRows()); + assertEquals(41L, data.getStatementResult(request -> request.id(query)).records().getFirst().getFirst().longValue()); + String last = data.executeStatement(request -> request.sessionId(session).sessionKeepAliveSeconds(0).sql("SELECT 1")).id(); + awaitFinished(last); + assertThrows(ResourceNotFoundException.class, + () -> data.executeStatement(request -> request.sessionId(session).sql("SELECT 1"))); + } + } + @Test void vacuumPreservesAutoCommitExecution() { executeAndWait("VACUUM"); 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.java index c2a3915051..bbf32674cd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutionTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataExecutionTest.java @@ -42,6 +42,24 @@ class RedshiftDataExecutionTest { } } + @Test + void cancellationSignalHoldsExecutionMonitor() throws Exception { + RedshiftDataExecution execution = new RedshiftDataExecution(0); + Statement statement = mock(Statement.class); + execution.track(statement); + assertTrue(execution.requestCancellation()); + doAnswer(invocation -> { + assertTrue(Thread.holdsLock(execution)); + return null; + }).when(statement).cancel(); + + execution.signalCancellation(); + execution.terminal(); + execution.signalCancellation(); + verify(statement).cancel(); + execution.completed(); + } + @Test void successfulCommitWinsOverCancellation() throws Exception { try (RedshiftDataExecutor executor = new RedshiftDataExecutor(1, 1, 1)) { diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionIntegrationTest.java new file mode 100644 index 0000000000..4d9b4449d4 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionIntegrationTest.java @@ -0,0 +1,147 @@ +package io.github.hectorvent.floci.services.redshiftdata; + +import com.fasterxml.jackson.databind.JsonNode; +import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.quarkus.test.InjectMock; +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +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 software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient; +import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementResponse; +import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementRequest; +import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementResponse; +import software.amazon.awssdk.services.redshiftdata.model.ResourceNotFoundException; +import software.amazon.awssdk.services.redshiftdata.model.StatusString; +import software.amazon.awssdk.services.redshiftdata.model.ValidationException; + +import java.net.URI; +import java.sql.DriverManager; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@QuarkusTest +class RedshiftDataSessionIntegrationTest { + @TestHTTPResource("/") + URI endpoint; + @InjectMock + RedshiftDataResourceResolver resolver; + @InjectMock + RedshiftDataConnectionFactory factory; + @Inject + RedshiftDataService service; + @Inject + RegionResolver regions; + + private RedshiftDataClient data; + + @BeforeEach + void setup() throws Exception { + Driver.load(); + String url = "jdbc:h2:mem:" + UUID.randomUUID(); + when(factory.open(any())).thenAnswer(invocation -> DriverManager.getConnection(url)); + when(resolver.resolve(any(), any())).thenAnswer(invocation -> { + JsonNode request = invocation.getArgument(0); + String region = invocation.getArgument(1); + boolean serverless = request.has("WorkgroupName"); + String arn = AwsArnUtils.Arn.of(serverless ? "redshift-serverless" : "redshift", region, + regions.getAccountId(), serverless ? "workgroup/12345678-1234-1234-1234-123456789012" : "cluster:warehouse").toString(); + return new RedshiftDataResourceResolver.DatabaseTarget(arn, "localhost", 5439, "dev", "admin", "password"); + }); + data = client("test", Region.US_EAST_1); + } + + @AfterEach + void cleanup() { + service.clear(); + if (data != null) { + data.close(); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void sdkReusesSessionForSingleAndBatchStatements(boolean serverless) throws Exception { + ExecuteStatementRequest.Builder request = ExecuteStatementRequest.builder().waitTimeSeconds(30).database("dev") + .sessionKeepAliveSeconds(60).sql("CREATE LOCAL TEMPORARY TABLE staging(id integer)"); + if (serverless) { + request.workgroupName("analytics"); + } else { + request.clusterIdentifier("warehouse").dbUser("admin"); + } + ExecuteStatementResponse created = data.executeStatement(request.build()); + assertNotNull(created.sessionId()); + finished(created.id()); + String batch = data.batchExecuteStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId()) + .sqls("INSERT INTO staging VALUES (41)", "INSERT INTO staging VALUES (42)")).id(); + finished(batch); + ExecuteStatementResponse query = data.executeStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId()).sql("SELECT id FROM staging ORDER BY id")); + DescribeStatementResponse described = finished(query.id()); + + assertEquals(created.sessionId(), described.sessionId()); + assertEquals(created.sessionId(), query.sessionId()); + assertEquals(41L, data.getStatementResult(r -> r.id(query.id())).records().getFirst().getFirst().longValue()); + assertEquals(2L, data.getStatementResult(r -> r.id(query.id())).totalNumRows()); + assertTrue(data.listStatements(r -> r.maxResults(100)).statements().stream() + .anyMatch(statement -> created.sessionId().equals(statement.sessionId()))); + verify(factory).open(any()); + } + + @Test + void sdkSessionIsIsolatedByCallerAccountAndRegion() { + ExecuteStatementResponse created = data.executeStatement(r -> r.waitTimeSeconds(30).clusterIdentifier("warehouse").dbUser("admin") + .database("dev").sessionKeepAliveSeconds(60).sql("SELECT 1")); + finished(created.id()); + for (String key : new String[]{"another-caller", "111111111111"}) { + try (RedshiftDataClient other = client(key, Region.US_EAST_1)) { + assertThrows(ResourceNotFoundException.class, () -> other.executeStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId()).sql("SELECT 2"))); + } + } + try (RedshiftDataClient otherRegion = client("test", Region.US_WEST_2)) { + assertThrows(ResourceNotFoundException.class, () -> otherRegion.executeStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId()).sql("SELECT 2"))); + } + finished(data.executeStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId()).sql("SELECT 3")).id()); + } + + @Test + void sdkValidatesKeepAliveAndReplaysAClosedSessionReceipt() { + assertThrows(ValidationException.class, () -> data.executeStatement(r -> r.waitTimeSeconds(30).clusterIdentifier("warehouse") + .dbUser("admin").database("dev").sessionKeepAliveSeconds(86401).sql("SELECT 1"))); + ExecuteStatementResponse created = data.executeStatement(r -> r.waitTimeSeconds(30).clusterIdentifier("warehouse").dbUser("admin") + .database("dev").sessionKeepAliveSeconds(60).sql("SELECT 1")); + finished(created.id()); + ExecuteStatementRequest last = ExecuteStatementRequest.builder().waitTimeSeconds(30).sessionId(created.sessionId()).sessionKeepAliveSeconds(0) + .clientToken(UUID.randomUUID().toString()).sql("SELECT 2").build(); + ExecuteStatementResponse closed = data.executeStatement(last); + finished(closed.id()); + assertEquals(closed.id(), data.executeStatement(last).id()); + assertEquals(closed.sessionId(), data.executeStatement(last).sessionId()); + try (RedshiftDataClient other = client("another-caller", Region.US_EAST_1)) { + assertThrows(ResourceNotFoundException.class, () -> other.executeStatement(last)); + } + assertThrows(ResourceNotFoundException.class, () -> data.executeStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId()).sql("SELECT 3"))); + } + + private RedshiftDataClient client(String key, Region region) { + return RedshiftDataClient.builder().endpointOverride(endpoint).region(region) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(key, "test"))).build(); + } + + private DescribeStatementResponse finished(String id) { + DescribeStatementResponse statement = data.describeStatement(r -> r.id(id)); + assertEquals(StatusString.FINISHED, statement.status(), statement.error()); + return statement; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionServiceTest.java new file mode 100644 index 0000000000..cdca3ca55b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionServiceTest.java @@ -0,0 +1,537 @@ +package io.github.hectorvent.floci.services.redshiftdata; + +import com.fasterxml.jackson.databind.JsonNode; +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.RequestContext; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.services.iam.IamService; +import org.h2.Driver; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.time.Clock; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class RedshiftDataSessionServiceTest { + + @Test + void sessionOnlyRequestsShareTemporaryTablesAndReportSessionMetadata() throws Exception { + try (Fixture fixture = new Fixture()) { + ObjectNode created = fixture.service.executeStatement(fixture.request( + "CREATE LOCAL TEMPORARY TABLE staging(id integer)").put("SessionKeepAliveSeconds", 60), "us-east-1"); + assertTrue(created.path("SessionId").isTextual(), "A retained session must have an id"); + String session = created.path("SessionId").asText(); + fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO staging VALUES (42)"), "us-east-1"); + ObjectNode query = fixture.service.executeStatement(fixture.reuse(session, "SELECT id FROM staging"), "us-east-1"); + ObjectNode described = fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", query.path("Id").asText())); + + assertEquals("FINISHED", described.path("Status").asText(), described.toString()); + assertEquals(session, described.path("SessionId").asText()); + ObjectNode result = fixture.service.getStatementResult(fixture.mapper.createObjectNode().put("Id", query.path("Id").asText())); + assertEquals(42, result.path("Records").get(0).get(0).path("longValue").asInt()); + assertEquals(session, fixture.service.listStatements(fixture.mapper.createObjectNode()) + .path("Statements").get(0).path("SessionId").asText()); + verify(fixture.factory).open(any()); + } + } + + @Test + void batchAndSingleStatementCanShareOneSession() throws Exception { + try (Fixture fixture = new Fixture()) { + ObjectNode request = fixture.request("unused").put("SessionKeepAliveSeconds", 60); + request.remove("Sql"); + request.putArray("Sqls").add("CREATE LOCAL TEMPORARY TABLE staging(id integer)") + .add("INSERT INTO staging VALUES (7)"); + ObjectNode batch = fixture.service.batchExecuteStatement(request, "us-east-1"); + String session = batch.path("SessionId").asText(); + ObjectNode query = fixture.service.executeStatement(fixture.reuse(session, "SELECT id FROM staging"), "us-east-1"); + ObjectNode result = fixture.service.getStatementResult(fixture.mapper.createObjectNode().put("Id", query.path("Id").asText())); + + assertEquals(7, result.path("Records").get(0).get(0).path("longValue").asInt()); + verify(fixture.factory).open(any()); + } + } + + @Test + void clientTokenRetryKeepsTheOriginalSessionAndExecutesOnce() throws Exception { + try (Fixture fixture = new Fixture()) { + ObjectNode request = fixture.request("CREATE LOCAL TEMPORARY TABLE staging(id integer)") + .put("SessionKeepAliveSeconds", 60).put("ClientToken", "retained-session"); + ObjectNode first = fixture.service.executeStatement(request, "us-east-1"); + ObjectNode retry = fixture.service.executeStatement(request, "us-east-1"); + + assertTrue(first.path("SessionId").isTextual()); + assertEquals(first.path("SessionId"), retry.path("SessionId")); + assertEquals(first.path("Id"), retry.path("Id")); + verify(fixture.factory).open(any()); + } + } + + @Test + void explicitTransactionCanBeRolledBackAcrossCalls() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("CREATE TABLE changes(id integer)") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + fixture.service.executeStatement(fixture.reuse(session, "BEGIN"), "us-east-1"); + fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO changes VALUES (1)"), "us-east-1"); + assertEquals(0, fixture.count("changes")); + fixture.service.executeStatement(fixture.reuse(session, "ROLLBACK"), "us-east-1"); + assertEquals(0, fixture.count("changes")); + fixture.service.executeStatement(fixture.reuse(session, "BEGIN TRANSACTION"), "us-east-1"); + fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO changes VALUES (2)"), "us-east-1"); + fixture.service.executeStatement(fixture.reuse(session, "COMMIT"), "us-east-1"); + assertEquals(1, fixture.count("changes")); + fixture.service.executeStatement(fixture.reuse(session, "BEGIN"), "us-east-1"); + fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO changes VALUES (3)"), "us-east-1"); + ObjectNode batch = fixture.reuse(session, "unused"); + batch.remove("Sql"); + batch.putArray("Sqls").add("INSERT INTO changes VALUES (4)"); + fixture.service.batchExecuteStatement(batch, "us-east-1"); + assertEquals(3, fixture.count("changes")); + } + } + + @Test + void failedExplicitTransactionCannotAccidentallyCommitLaterStatements() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("CREATE TABLE changes(id integer)") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + fixture.service.executeStatement(fixture.reuse(session, "BEGIN -- keep this transaction open"), "us-east-1"); + fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO changes VALUES (1)"), "us-east-1"); + assertEquals(0, fixture.count("changes")); + fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO missing_table VALUES (2)"), "us-east-1"); + ObjectNode later = fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO changes VALUES (3)"), "us-east-1"); + assertEquals("FAILED", fixture.service.describeStatement(fixture.mapper.createObjectNode() + .put("Id", later.path("Id").asText())).path("Status").asText()); + ObjectNode batch = fixture.reuse(session, "unused"); + batch.remove("Sql"); + batch.putArray("Sqls").add("INSERT INTO changes VALUES (9)"); + String batchId = fixture.service.batchExecuteStatement(batch, "us-east-1").path("Id").asText(); + assertEquals("FAILED", fixture.service.describeStatement(fixture.mapper.createObjectNode() + .put("Id", batchId)).path("Status").asText()); + fixture.service.executeStatement(fixture.reuse(session, "ROLLBACK"), "us-east-1"); + assertEquals(0, fixture.count("changes")); + fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO changes VALUES (4)"), "us-east-1"); + assertEquals(1, fixture.count("changes")); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void connectionFailureRetiresTheSessionWithoutReportingCancellation(boolean batch) throws Exception { + try (Fixture fixture = new Fixture()) { + Connection connection = spy(DriverManager.getConnection(fixture.url)); + doThrow(new SQLException("broken connection", "08006")).when(connection).prepareStatement("SELECT broken"); + when(fixture.factory.open(any())).thenReturn(connection); + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + ObjectNode request = fixture.reuse(session, "SELECT broken"); + if (batch) { + request.remove("Sql"); + request.putArray("Sqls").add("SELECT broken"); + } + String failed = (batch ? fixture.service.batchExecuteStatement(request, "us-east-1") + : fixture.service.executeStatement(request, "us-east-1")).path("Id").asText(); + ObjectNode described = fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", failed)); + assertEquals("FAILED", described.path("Status").asText()); + assertTrue(connection.isClosed()); + assertThrows(AwsException.class, () -> fixture.service.executeStatement(fixture.reuse(session, "SELECT 2"), "us-east-1")); + } + } + + @Test + void unsupportedTransactionModesFailInsteadOfSilentlyCommitting() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + for (String sql : new String[]{"BEGIN ISOLATION LEVEL SERIALIZABLE", "START TRANSACTION READ ONLY", "COMMIT AND CHAIN"}) { + ObjectNode response = fixture.service.executeStatement(fixture.reuse(session, sql), "us-east-1"); + ObjectNode described = fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", response.path("Id").asText())); + assertEquals("FAILED", described.path("Status").asText()); + assertTrue(described.path("Error").asText().contains("not supported")); + } + } + } + + @Test + void failedBatchRollsBackWithoutClosingTheReusableConnection() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("CREATE LOCAL TEMPORARY TABLE staging(id integer)") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + ObjectNode batch = fixture.reuse(session, "unused"); + batch.remove("Sql"); + batch.putArray("Sqls").add("INSERT INTO staging VALUES (42)").add("INSERT INTO missing_table VALUES (1)"); + String id = fixture.service.batchExecuteStatement(batch, "us-east-1").path("Id").asText(); + assertEquals("FAILED", fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", id)) + .path("Status").asText()); + ObjectNode query = fixture.service.executeStatement(fixture.reuse(session, "SELECT COUNT(*) FROM staging"), "us-east-1"); + ObjectNode result = fixture.service.getStatementResult(fixture.mapper.createObjectNode().put("Id", query.path("Id").asText())); + assertEquals(0, result.path("Records").get(0).get(0).path("longValue").asInt()); + verify(fixture.factory).open(any()); + } + } + + @Test + void explicitSessionTerminationStillAllowsIdempotentReplay() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + ObjectNode request = fixture.reuse(session, "SELECT 2").put("SessionKeepAliveSeconds", 0).put("ClientToken", "last"); + ObjectNode first = fixture.service.executeStatement(request, "us-east-1"); + ObjectNode retry = fixture.service.executeStatement(request, "us-east-1"); + assertEquals(first.path("Id"), retry.path("Id")); + AwsException gone = assertThrows(AwsException.class, () -> fixture.service.executeStatement( + fixture.reuse(session, "SELECT 3"), "us-east-1")); + assertEquals("ResourceNotFoundException", gone.getErrorCode()); + } + } + + @Test + void busySessionRejectsNewQueriesButAllowsClientTokenRetriesAndQueuedCancellation() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + CountDownLatch started = new CountDownLatch(2); + CountDownLatch release = new CountDownLatch(1); + for (int n = 0; n < 2; n++) { + fixture.executor.submit("000000000000", "us-east-1", "block-" + n, execution -> { + started.countDown(); + try { + release.await(5, TimeUnit.SECONDS); + } catch (InterruptedException expected) { + Thread.currentThread().interrupt(); + } + }); + } + try { + assertTrue(started.await(5, TimeUnit.SECONDS)); + ObjectNode request = fixture.reuse(session, "SELECT 2").put("ClientToken", "queued"); + request.remove("WaitTimeSeconds"); + ObjectNode queued = fixture.service.executeStatement(request, "us-east-1"); + assertEquals(queued.path("Id"), fixture.service.executeStatement(request, "us-east-1").path("Id")); + AwsException busy = assertThrows(AwsException.class, () -> fixture.service.executeStatement( + fixture.reuse(session, "SELECT 3"), "us-east-1")); + assertEquals("ValidationException", busy.getErrorCode()); + assertTrue(fixture.service.cancelStatement(fixture.mapper.createObjectNode() + .put("Id", queued.path("Id").asText())).path("Status").asBoolean()); + } finally { + release.countDown(); + } + ObjectNode query = fixture.service.executeStatement(fixture.reuse(session, "SELECT 4"), "us-east-1"); + assertEquals("FINISHED", fixture.service.describeStatement(fixture.mapper.createObjectNode() + .put("Id", query.path("Id").asText())).path("Status").asText()); + } + } + + @Test + void mismatchedTargetsAndResetSessionsAreRejected() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + for (String field : new String[]{"Database", "DbUser", "ClusterIdentifier", "WorkgroupName", "SecretArn"}) { + assertThrows(AwsException.class, () -> fixture.service.executeStatement( + fixture.reuse(session, "SELECT 2").put(field, "different"), "us-east-1")); + } + fixture.service.clear(); + assertThrows(AwsException.class, () -> fixture.service.executeStatement(fixture.reuse(session, "SELECT 2"), "us-east-1")); + } + } + + @Test + void fullExecutorQueueDoesNotLeaveTheSessionBusy() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + CountDownLatch started = new CountDownLatch(2); + CountDownLatch release = new CountDownLatch(1); + List executions = new ArrayList<>(); + for (int n = 0; n < 2; n++) { + executions.add(fixture.executor.submit("000000000000", "us-east-1", "block-" + n, execution -> { + started.countDown(); + try { + release.await(5, TimeUnit.SECONDS); + } catch (InterruptedException expected) { + Thread.currentThread().interrupt(); + } + })); + } + try { + assertTrue(started.await(5, TimeUnit.SECONDS)); + for (int n = 0; n < 8; n++) { + executions.add(fixture.executor.submit("000000000000", "us-east-1", "queued-" + n, execution -> { })); + } + AwsException full = assertThrows(AwsException.class, () -> fixture.service.executeStatement( + fixture.reuse(session, "SELECT 2"), "us-east-1")); + assertEquals("ActiveStatementsExceededException", full.getErrorCode()); + } finally { + release.countDown(); + } + for (RedshiftDataExecution execution : executions) { + execution.await(Duration.ofSeconds(5)); + assertTrue(execution.isDone()); + } + String query = fixture.service.executeStatement(fixture.reuse(session, "SELECT 3"), "us-east-1").path("Id").asText(); + assertEquals("FINISHED", fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", query)).path("Status").asText()); + verify(fixture.factory).open(any()); + } + } + + @Test + void rejectedAdmissionReleasesAnExistingSessionWithoutOpeningAnotherConnection() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + doThrow(new IllegalStateException("statement storage unavailable")).when(fixture.statements) + .putForAccount(any(), any()); + assertThrows(IllegalStateException.class, () -> fixture.service.executeStatement( + fixture.reuse(session, "SELECT 2"), "us-east-1")); + doCallRealMethod().when(fixture.statements).putForAccount(any(), any()); + doThrow(new IllegalStateException("receipt storage unavailable")).when(fixture.receipts).put(any(), any(), any()); + assertThrows(IllegalStateException.class, () -> fixture.service.executeStatement( + fixture.reuse(session, "SELECT 2").put("ClientToken", "rejected"), "us-east-1")); + doCallRealMethod().when(fixture.receipts).put(any(), any(), any()); + ObjectNode retried = fixture.service.executeStatement(fixture.reuse(session, "SELECT 2") + .put("ClientToken", "rejected"), "us-east-1"); + assertEquals("FINISHED", fixture.service.describeStatement(fixture.mapper.createObjectNode() + .put("Id", retried.path("Id").asText())).path("Status").asText()); + verify(fixture.factory).open(any()); + } + } + + @Test + void receiptReplaysAfterLosingLiveSessionsAndEvictingStatementResults() throws Exception { + try (Fixture fixture = new Fixture()) { + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + ObjectNode request = fixture.reuse(session, "SELECT 2").put("ClientToken", "restart-replay"); + ObjectNode first = fixture.service.executeStatement(request, "us-east-1"); + fixture.statements.deleteForAccount("000000000000", first.path("Id").asText()); + RedshiftDataService restarted = new RedshiftDataService(fixture.resolver, fixture.factory, fixture.statements, + fixture.mapper, null, fixture.executor, null, null, null, fixture.receipts); + + ObjectNode replay = restarted.executeStatement(request, "us-east-1"); + assertEquals(first.path("Id"), replay.path("Id")); + assertEquals(first.path("SessionId"), replay.path("SessionId")); + verify(fixture.factory).open(any()); + } + } + + @Test + void uncheckedFailureAfterSqlExecutionAbortsTheExplicitTransaction() throws Exception { + try (Fixture fixture = new Fixture()) { + Connection connection = spy(DriverManager.getConnection(fixture.url)); + when(fixture.factory.open(any())).thenReturn(connection); + String session = fixture.service.executeStatement(fixture.request("CREATE TABLE changes(id integer)") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + PreparedStatement insert = spy(connection.prepareStatement("INSERT INTO changes VALUES (1)")); + doAnswer(invocation -> { + invocation.callRealMethod(); + throw new IllegalStateException("result conversion failed"); + }).when(insert).execute(); + doReturn(insert).when(connection).prepareStatement("INSERT INTO changes VALUES (1)"); + fixture.service.executeStatement(fixture.reuse(session, "BEGIN"), "us-east-1"); + String failed = fixture.service.executeStatement(fixture.reuse(session, "INSERT INTO changes VALUES (1)"), "us-east-1") + .path("Id").asText(); + ObjectNode described = fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", failed)); + assertEquals("FAILED", described.path("Status").asText()); + assertTrue(described.path("Error").asText().contains("result conversion failed"), described.toString()); + String committed = fixture.service.executeStatement(fixture.reuse(session, "COMMIT"), "us-east-1").path("Id").asText(); + assertEquals("FINISHED", fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", committed)) + .path("Status").asText()); + assertEquals(0, fixture.count("changes")); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void malformedSessionIdentifiersFailBeforeTargetLookup(boolean batch) throws Exception { + try (Fixture fixture = new Fixture()) { + for (String raw : new String[]{"\"malformed\"", "7", "true", "null", "[]", "{}"}) { + ObjectNode request = fixture.request("SELECT 1"); + JsonNode invalid = fixture.mapper.readTree(raw); + request.set("SessionId", invalid); + if (batch) { + request.remove("Sql"); + request.putArray("Sqls").add("SELECT 1"); + } + AwsException error = assertThrows(AwsException.class, () -> { + if (batch) { + fixture.service.batchExecuteStatement(request, "us-east-1"); + } else { + fixture.service.executeStatement(request, "us-east-1"); + } + }); + assertEquals("ValidationException", error.getErrorCode()); + } + verifyNoInteractions(fixture.factory, fixture.resolver); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void ordinaryRequestsDoNotResolveSessionCaller(boolean batch) throws Exception { + RequestContext context = new RequestContext(); + context.setAccessKeyId("caller-key"); + IamService iam = mock(IamService.class); + when(iam.resolveCallerArn("caller-key")).thenReturn(Optional.of("caller-arn")); + try (Fixture fixture = new Fixture(context, iam)) { + for (boolean token : new boolean[]{false, true}) { + ObjectNode request = fixture.request("SELECT 1").put("SessionKeepAliveSeconds", 0); + if (token) { + request.put("ClientToken", "ordinary"); + } + if (batch) { + request.remove("Sql"); + request.putArray("Sqls").add("SELECT 1"); + fixture.service.batchExecuteStatement(request, "us-east-1"); + } else { + fixture.service.executeStatement(request, "us-east-1"); + } + } + verifyNoInteractions(iam); + } + } + + @Test + void cleanupFailureIsReportedWithoutSkippingResetOfStatementAndReceiptState() throws Exception { + try (Fixture fixture = new Fixture(); Connection connection = spy(DriverManager.getConnection(fixture.url))) { + when(fixture.factory.open(any())).thenReturn(connection); + ObjectNode request = fixture.request("SELECT 1").put("ClientToken", "cleanup-reset").put("SessionKeepAliveSeconds", 60); + String id = fixture.service.executeStatement(request, "us-east-1").path("Id").asText(); + doThrow(new IllegalStateException("unexpected close failure")).doCallRealMethod().when(connection).close(); + + assertThrows(IllegalStateException.class, fixture.service::clear); + + assertNull(fixture.statements.getForAccount("000000000000", id)); + assertNull(fixture.receipts.find(new RedshiftDataIdempotencyStore.Scope("000000000000", "us-east-1", + "ExecuteStatement", "cleanup-reset"))); + } + } + + @Test + void terminatingSessionReportsUnexpectedConnectionCleanupFailure() throws Exception { + try (Fixture fixture = new Fixture(); Connection connection = spy(DriverManager.getConnection(fixture.url))) { + when(fixture.factory.open(any())).thenReturn(connection); + String session = fixture.service.executeStatement(fixture.request("SELECT 1") + .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText(); + doThrow(new IllegalStateException("unexpected close failure")).doCallRealMethod().when(connection).close(); + String id = fixture.service.executeStatement(fixture.reuse(session, "SELECT 2") + .put("SessionKeepAliveSeconds", 0), "us-east-1").path("Id").asText(); + ObjectNode described = fixture.service.describeStatement(fixture.mapper.createObjectNode().put("Id", id)); + assertEquals("FAILED", described.path("Status").asText()); + assertTrue(described.path("Error").asText().contains("unexpected close failure"), described.toString()); + } + } + + @Test + void replayWithoutSessionFieldsStillChecksRetainedReceiptOwnership() throws Exception { + RequestContext context = new RequestContext(); + context.setAccessKeyId("owner"); + IamService iam = mock(IamService.class); + when(iam.resolveCallerArn("owner")).thenReturn(Optional.of("owner-principal")); + when(iam.resolveCallerArn("other")).thenReturn(Optional.of("other-principal")); + try (Fixture fixture = new Fixture(context, iam)) { + ObjectNode request = fixture.request("SELECT 1").put("ClientToken", "retained-replay") + .put("SessionKeepAliveSeconds", 60); + String id = fixture.service.executeStatement(request, "us-east-1").path("Id").asText(); + request.remove("SessionKeepAliveSeconds"); + context.setAccessKeyId("other"); + AwsException denied = assertThrows(AwsException.class, () -> fixture.service.executeStatement(request, "us-east-1")); + assertEquals("ResourceNotFoundException", denied.getErrorCode()); + context.setAccessKeyId("owner"); + assertEquals(id, fixture.service.executeStatement(request, "us-east-1").path("Id").asText()); + verify(fixture.factory).open(any()); + } + } + + @Test + void malformedKeepAliveValuesAreRejectedBeforeOpeningAConnection() throws Exception { + try (Fixture fixture = new Fixture()) { + for (int seconds : new int[]{-1, 86401}) { + AwsException error = assertThrows(AwsException.class, () -> fixture.service.executeStatement( + fixture.request("SELECT 1").put("SessionKeepAliveSeconds", seconds), "us-east-1")); + assertEquals("ValidationException", error.getErrorCode()); + } + assertThrows(AwsException.class, () -> fixture.service.executeStatement( + fixture.request("SELECT 1").put("SessionKeepAliveSeconds", "60"), "us-east-1")); + assertThrows(AwsException.class, () -> fixture.service.executeStatement( + fixture.request("SELECT 1").put("SessionKeepAliveSeconds", 0.5), "us-east-1")); + verifyNoInteractions(fixture.factory); + } + } + + private static final class Fixture implements AutoCloseable { + private final ObjectMapper mapper = new ObjectMapper(); + private final RedshiftDataResourceResolver resolver = mock(RedshiftDataResourceResolver.class); + private final RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + private final RedshiftDataExecutor executor = new RedshiftDataExecutor(2, 8, 1); + private final RedshiftDataStatementStore statements = spy(new RedshiftDataStatementStore(24, Clock.systemUTC())); + private final RedshiftDataIdempotencyStore receipts = spy(new RedshiftDataIdempotencyStore( + AccountAwareStorageBackend.inMemory("000000000000"), Clock.systemUTC())); + private final RedshiftDataService service; + private final String url = "jdbc:h2:mem:" + UUID.randomUUID(); + + private Fixture() throws Exception { + this(null, null); + } + + private Fixture(RequestContext context, IamService iam) throws Exception { + Driver.load(); + when(resolver.resolve(any(), any())).thenAnswer(invocation -> { + ObjectNode request = invocation.getArgument(0); + if (!request.has("Database")) { + throw new AwsException("ValidationException", "Database is required", 400); + } + return new RedshiftDataResourceResolver.DatabaseTarget( + "arn:aws:redshift:us-east-1:000000000000:cluster:warehouse", "localhost", 5439, + "dev", "admin", "password"); + }); + when(factory.open(any())).thenAnswer(invocation -> DriverManager.getConnection(url)); + service = new RedshiftDataService(resolver, factory, statements, mapper, null, executor, null, + context, iam, receipts); + } + + private ObjectNode request(String sql) { + return mapper.createObjectNode().put("Sql", sql).put("WaitTimeSeconds", 5) + .put("ClusterIdentifier", "warehouse").put("Database", "dev").put("DbUser", "admin"); + } + + private ObjectNode reuse(String session, String sql) { + return mapper.createObjectNode().put("Sql", sql).put("SessionId", session).put("WaitTimeSeconds", 5); + } + + private int count(String table) throws Exception { + try (Connection connection = DriverManager.getConnection(url); + Statement statement = connection.createStatement(); + ResultSet rows = statement.executeQuery("SELECT COUNT(*) FROM " + table)) { + assertTrue(rows.next()); + return rows.getInt(1); + } + } + + @Override + public void close() { + service.clear(); + executor.close(); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionsTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionsTest.java new file mode 100644 index 0000000000..0bbb6e0473 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionsTest.java @@ -0,0 +1,295 @@ +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.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.sql.Connection; +import java.sql.SQLException; +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +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 RedshiftDataSessionsTest { + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final RedshiftDataSessions.Scope OWNER = new RedshiftDataSessions.Scope("000000000000", "us-east-1", "caller"); + private static final RedshiftDataResourceResolver.DatabaseTarget TARGET = new RedshiftDataResourceResolver.DatabaseTarget( + "arn:aws:redshift:us-east-1:000000000000:cluster:warehouse", "localhost", 5439, "dev", "admin", "password"); + + @Test + void failedConnectionOpeningCannotAdmitAReplacementReservation() throws Exception { + try (RedshiftDataSessions sessions = new RedshiftDataSessions(Clock.systemUTC()); + ExecutorService workers = Executors.newFixedThreadPool(2)) { + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenThrow(new SQLException("connection refused", "08001")); + for (int attempt = 0; attempt < 2000; attempt++) { + RedshiftDataSessions.Reservation first = sessions.reserve(OWNER, + request().put("SessionKeepAliveSeconds", 60), TARGET); + first.admitted(); + assertThrows(SQLException.class, () -> first.connection(factory)); + ObjectNode reuse = MAPPER.createObjectNode().put("SessionId", first.id()); + CountDownLatch start = new CountDownLatch(1); + Future finishing = workers.submit(() -> { + assertTrue(start.await(5, TimeUnit.SECONDS)); + first.finish(); + return null; + }); + Future admitted = workers.submit(() -> { + assertTrue(start.await(5, TimeUnit.SECONDS)); + while (!Thread.currentThread().isInterrupted()) { + try { + sessions.reserve(OWNER, reuse, TARGET); + return true; + } catch (AwsException expected) { + if ("ResourceNotFoundException".equals(expected.getErrorCode())) { + return false; + } + assertEquals("ValidationException", expected.getErrorCode()); + Thread.onSpinWait(); + } + } + return false; + }); + start.countDown(); + try { + assertFalse(admitted.get(5, TimeUnit.SECONDS), "A failed session admitted a new query at attempt " + attempt); + finishing.get(5, TimeUnit.SECONDS); + } finally { + admitted.cancel(true); + } + } + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void clearSurfacesUnexpectedCleanupFailuresAndStillClosesOtherConnections(boolean unchecked) throws Exception { + try (RedshiftDataSessions sessions = new RedshiftDataSessions(Clock.systemUTC())) { + Connection broken = mock(Connection.class); + Connection healthy = mock(Connection.class); + Throwable failure = unchecked ? new IllegalStateException("close failed") : new SQLException("close failed", "08006"); + doThrow(failure).when(broken).close(); + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenReturn(broken, healthy); + for (int n = 0; n < 2; n++) { + RedshiftDataSessions.Reservation reservation = sessions.reserve(OWNER, + request().put("SessionKeepAliveSeconds", 60), TARGET); + reservation.admitted(); + reservation.connection(factory); + reservation.finish(); + } + RuntimeException observed = assertThrows(RuntimeException.class, sessions::clear); + assertSame(failure, observed.getCause()); + verify(healthy).close(); + } + } + + @Test + void alreadyClosedConnectionIsABenignCleanupOutcome() throws Exception { + try (RedshiftDataSessions sessions = new RedshiftDataSessions(Clock.systemUTC())) { + Connection connection = mock(Connection.class); + doThrow(new SQLException("connection already closed", "08003")).when(connection).close(); + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenReturn(connection); + RedshiftDataSessions.Reservation reservation = sessions.reserve(OWNER, + request().put("SessionKeepAliveSeconds", 60), TARGET); + reservation.admitted(); + reservation.connection(factory); + reservation.finish(); + assertDoesNotThrow(sessions::clear); + verify(connection).close(); + } + } + + @Test + void clearClosesAConnectionThatFinishesOpeningAfterReset() throws Exception { + try (RedshiftDataSessions sessions = new RedshiftDataSessions(Clock.systemUTC()); + ExecutorService worker = Executors.newSingleThreadExecutor()) { + Connection connection = mock(Connection.class); + CountDownLatch opening = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenAnswer(invocation -> { + opening.countDown(); + assertTrue(release.await(5, TimeUnit.SECONDS)); + return connection; + }); + RedshiftDataSessions.Reservation reservation = sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 10), TARGET); + reservation.admitted(); + Future late = worker.submit(() -> reservation.connection(factory)); + try { + assertTrue(opening.await(5, TimeUnit.SECONDS)); + sessions.clear(); + release.countDown(); + assertThrows(ExecutionException.class, () -> late.get(5, TimeUnit.SECONDS)); + verify(connection).close(); + assertThrows(AwsException.class, () -> sessions.find(OWNER, reservation.id())); + } finally { + release.countDown(); + } + } + } + + @Test + void idleDeadlineStartsAfterCompletionAndUnspecifiedTimeoutIsRetained() throws Exception { + MutableClock clock = new MutableClock(); + try (RedshiftDataSessions sessions = new RedshiftDataSessions(clock)) { + Connection connection = mock(Connection.class); + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenReturn(connection); + RedshiftDataSessions.Reservation first = sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 10), TARGET); + first.admitted(); + first.connection(factory); + clock.advance(Duration.ofSeconds(20)); + sessions.sweep(); + verify(connection, never()).close(); + first.finish(); + clock.advance(Duration.ofSeconds(9)); + ObjectNode reuse = MAPPER.createObjectNode().put("SessionId", first.id()); + RedshiftDataSessions.Reservation next = sessions.reserve(OWNER, reuse, TARGET); + next.admitted(); + assertSame(connection, next.connection(factory)); + next.finish(); + clock.advance(Duration.ofSeconds(9)); + assertNotNull(sessions.find(OWNER, first.id())); + clock.advance(Duration.ofSeconds(1)); + sessions.sweep(); + verify(connection).close(); + assertThrows(AwsException.class, () -> sessions.find(OWNER, first.id())); + } + } + + @Test + void busySessionsRejectNewWorkAndFailedAdmissionDoesNotExtendIdleTime() throws Exception { + MutableClock clock = new MutableClock(); + try (RedshiftDataSessions sessions = new RedshiftDataSessions(clock)) { + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenReturn(mock(Connection.class)); + RedshiftDataSessions.Reservation first = sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 10), TARGET); + first.admitted(); + first.connection(factory); + ObjectNode reuse = MAPPER.createObjectNode().put("SessionId", first.id()); + assertThrows(AwsException.class, () -> sessions.reserve(OWNER, reuse, TARGET)); + first.finish(); + clock.advance(Duration.ofSeconds(9)); + RedshiftDataSessions.Reservation rejected = sessions.reserve(OWNER, reuse.put("SessionKeepAliveSeconds", 100), TARGET); + rejected.abort(); + clock.advance(Duration.ofSeconds(1)); + assertThrows(AwsException.class, () -> sessions.find(OWNER, first.id())); + } + } + + @Test + void sessionsAreScopedToAccountRegionAndCaller() throws Exception { + try (RedshiftDataSessions sessions = new RedshiftDataSessions(Clock.systemUTC())) { + RedshiftDataSessions.Reservation first = sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 10), TARGET); + for (RedshiftDataSessions.Scope foreign : new RedshiftDataSessions.Scope[]{ + new RedshiftDataSessions.Scope("111111111111", "us-east-1", "caller"), + new RedshiftDataSessions.Scope("000000000000", "us-west-2", "caller"), + new RedshiftDataSessions.Scope("000000000000", "us-east-1", "another-caller")}) { + assertThrows(AwsException.class, () -> sessions.find(foreign, first.id())); + } + assertNotNull(sessions.find(OWNER, first.id())); + first.abort(); + } + } + + @Test + void zeroTimeoutClosesTheSessionAfterItsLastQuery() throws Exception { + try (RedshiftDataSessions sessions = new RedshiftDataSessions(Clock.systemUTC())) { + Connection connection = mock(Connection.class); + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenReturn(connection); + RedshiftDataSessions.Reservation first = sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 10), TARGET); + first.admitted(); + first.connection(factory); + first.finish(); + RedshiftDataSessions.Reservation last = sessions.reserve(OWNER, + MAPPER.createObjectNode().put("SessionId", first.id()).put("SessionKeepAliveSeconds", 0), TARGET); + last.admitted(); + last.connection(factory); + last.finish(); + verify(connection).close(); + assertThrows(AwsException.class, () -> sessions.find(OWNER, first.id())); + } + } + + @Test + void absoluteDeadlineClosesAnActiveConnectionAndCannotBeExtended() throws Exception { + MutableClock clock = new MutableClock(); + try (RedshiftDataSessions sessions = new RedshiftDataSessions(clock)) { + Connection connection = mock(Connection.class); + RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class); + when(factory.open(any())).thenReturn(connection); + RedshiftDataSessions.Reservation first = sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 86400), TARGET); + first.admitted(); + RedshiftDataExecution execution = new RedshiftDataExecution(0); + first.bind(execution); + first.connection(factory); + clock.advance(Duration.ofHours(24)); + sessions.sweep(); + await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> verify(connection).close()); + execution.completed(); + assertThrows(AwsException.class, () -> sessions.find(OWNER, first.id())); + } + } + + @Test + void sessionLimitIsSharedAcrossCallersAndAbortedReservationsReleaseCapacity() { + try (RedshiftDataSessions sessions = new RedshiftDataSessions(Clock.systemUTC())) { + RedshiftDataSessions.Reservation first = sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 10), TARGET); + for (int n = 1; n < 500; n++) { + sessions.reserve(new RedshiftDataSessions.Scope("000000000000", "us-east-1", "caller-" + n), + request().put("SessionKeepAliveSeconds", 10), TARGET); + } + AwsException full = assertThrows(AwsException.class, () -> sessions.reserve(OWNER, + request().put("SessionKeepAliveSeconds", 10), TARGET)); + assertEquals("ActiveSessionsExceededException", full.getErrorCode()); + first.abort(); + assertNotNull(sessions.reserve(OWNER, request().put("SessionKeepAliveSeconds", 10), TARGET)); + } + } + + private static ObjectNode request() { + return MAPPER.createObjectNode().put("ClusterIdentifier", "warehouse").put("Database", "dev").put("DbUser", "admin"); + } + + private static final class MutableClock extends Clock { + private Instant now = Instant.parse("2026-10-24T00:00:00Z"); + + void advance(Duration duration) { + now = now.plus(duration); + } + + @Override + public ZoneId getZone() { + return ZoneOffset.UTC; + } + + @Override + public Clock withZone(ZoneId zone) { + return this; + } + + @Override + public Instant instant() { + return now; + } + } +}