mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(redshift-data): emulate the Amazon Redshift Data API (#3186)
* feat(redshift-data): resource resolver, connection factory, statement store, service registration * feat(redshift-data): field mapper, column metadata, SQL parameter binding * feat(redshift-data): execute, describe, and paged result retrieval * feat(redshift-data): batch execution, list statements, cancel * feat(redshift-data): schema introspection operations * test(redshift-data): end-to-end Data API over a real cluster plus SDK compatibility test * docs(redshift-data): document the Redshift Data API surface * fix(redshift-data): address code review findings - rejectMultiStatement now reuses the SQL tokenizer so a semicolon inside a comment, string literal, or dollar-quoted block no longer rejects a valid single statement - GetStatementResultV2 gains unit coverage for the typed and CSV envelopes - CSV output is RFC 4180 quoted (embedded comma, quote, CR, LF) - batch rollback is asserted transactionally in the integration test - RedshiftDataFieldMapper logs a swallowed blob-read SQLException - RedshiftPid/RedshiftQueryId use a non-negative hash - NextToken that decodes to a negative offset is rejected - CancelStatement leaves a FAILED statement FAILED - DescribeTable paginates ColumnList and emits NextToken - DbUser is echoed on the SecretArn path - the statement store no longer builds its sweeper executor in the test constructor * fix(redshift-data): address PR #3186 review findings - SQL scanner handles PostgreSQL escape strings (E'...'): a backslash-escaped quote no longer ends the literal and mis-detects a following :name as a bind marker; plain '...' literals still treat backslash as an ordinary character - ListStatements clamps MaxResults to 1..100 so MaxResults=0 no longer emits the same NextToken forever - DescribeTable matches Table / Schema exactly (=) instead of LIKE, so a name containing _ or % cannot merge other tables' columns into ColumnList - register the service in the README category table - drop a stray em dash from the neighbouring services.yaml comment * fix(redshift-data): bind string parameters untyped so real PostgreSQL infers the column type CI ran the integration test against a real Redshift (PostgreSQL 15) cluster and parameterisedStatement failed: a string bound to an int column via setString raised "operator does not exist: integer = character varying". H2 in PostgreSQL mode coerces it, so the unit test missed it. - RedshiftDataConnectionFactory adds stringtype=unspecified to the JDBC URL, so setString sends the value untyped and the server infers the target type, matching how AWS binds Redshift Data API parameters - ListStatements now rejects MaxResults above 100 with ValidationException instead of silently clamping (PR #3186 re-review) - RedshiftDataOperationsTest uses JBoss Logging, not java.util.logging (PR #3186 re-review) * fix(redshift-data): back the statement store with StorageFactory The store held a bare ConcurrentHashMap outside the configured storage and lifecycle model. It now obtains an account-aware backend through StorageFactory ("redshift-data", statements.json), the same as Athena's query store, resolved to the global storage mode via the catalog descriptor. Default mode stays memory, so statements are still lost on restart as documented; the 24h TTL sweep now walks every account partition and deletes per account since the sweep thread has no request context. StoredStatement is annotated for reflection and field-visible serialization so persistent modes round-trip it. * test(redshift-data): cover StoredStatement serialization round-trip and values() The StorageFactory-backed store serializes StoredStatement in persistent modes. Adds a round-trip test through the exact mapper PersistentStorage builds, covering the ArrayNode, enum, Instant, and nested sub-statement fields plus the field-visibility annotation, and a values() test. * fix(redshift-data): register the nested Status enum for reflection PersistedModelReflectionTest walks every type reachable from a StorageFactory persistence root; StoredStatement.status pointed at an unregistered enum, so native-image deserialization of a persisted statement would fail on restart.
This commit is contained in:
1 parent
618f52e758
commit
b9039fef1d
30 files changed
+2805
-3
No files matched your search
+112
@@ -0,0 +1,112 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.redshift.RedshiftClient;
|
||||
import software.amazon.awssdk.services.redshift.model.CreateClusterRequest;
|
||||
import software.amazon.awssdk.services.redshift.model.DeleteClusterRequest;
|
||||
import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;
|
||||
import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementRequest;
|
||||
import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementResponse;
|
||||
import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementRequest;
|
||||
import software.amazon.awssdk.services.redshiftdata.model.GetStatementResultRequest;
|
||||
import software.amazon.awssdk.services.redshiftdata.model.GetStatementResultResponse;
|
||||
import software.amazon.awssdk.services.redshiftdata.model.StatusString;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("Redshift Data API Operations")
|
||||
class RedshiftDataOperationsTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(RedshiftDataOperationsTest.class);
|
||||
|
||||
private static final String USERNAME = "admin";
|
||||
private static final String PASSWORD = "password123";
|
||||
private static final String DATABASE = "dev";
|
||||
|
||||
private static RedshiftClient redshift;
|
||||
private static RedshiftDataClient data;
|
||||
private static String clusterId;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
redshift = TestFixtures.redshiftClient();
|
||||
data = TestFixtures.redshiftDataClient();
|
||||
clusterId = TestFixtures.uniqueName("rsdata-cluster");
|
||||
redshift.createCluster(CreateClusterRequest.builder()
|
||||
.clusterIdentifier(clusterId)
|
||||
.nodeType("dc2.large")
|
||||
.masterUsername(USERNAME)
|
||||
.masterUserPassword(PASSWORD)
|
||||
.build());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (redshift != null && clusterId != null) {
|
||||
try {
|
||||
redshift.deleteCluster(DeleteClusterRequest.builder().clusterIdentifier(clusterId).build());
|
||||
} catch (Exception e) {
|
||||
LOG.warnf(e, "Failed to clean up Redshift cluster %s", clusterId);
|
||||
}
|
||||
}
|
||||
if (data != null) {
|
||||
data.close();
|
||||
}
|
||||
if (redshift != null) {
|
||||
redshift.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("execute, describe, and get-statement-result round trip")
|
||||
void executeDescribeGetResult() throws Exception {
|
||||
run("CREATE TABLE compat_t (id int, name varchar(20))");
|
||||
String insertId = run("INSERT INTO compat_t VALUES (1, 'a'), (2, 'b')");
|
||||
assertThat(describe(insertId).resultRows()).isEqualTo(2L);
|
||||
|
||||
String selectId = run("SELECT id, name FROM compat_t ORDER BY id");
|
||||
GetStatementResultResponse result = data.getStatementResult(GetStatementResultRequest.builder()
|
||||
.id(selectId)
|
||||
.build());
|
||||
|
||||
assertThat(result.totalNumRows()).isEqualTo(2L);
|
||||
assertThat(result.columnMetadata()).extracting("name").containsExactly("id", "name");
|
||||
assertThat(result.records().get(0).get(0).longValue()).isEqualTo(1L);
|
||||
assertThat(result.records().get(0).get(1).stringValue()).isEqualTo("a");
|
||||
}
|
||||
|
||||
private String run(String sql) throws Exception {
|
||||
String id = data.executeStatement(ExecuteStatementRequest.builder()
|
||||
.clusterIdentifier(clusterId)
|
||||
.dbUser(USERNAME)
|
||||
.database(DATABASE)
|
||||
.sql(sql)
|
||||
.build())
|
||||
.id();
|
||||
Instant deadline = Instant.now().plus(Duration.ofSeconds(30));
|
||||
while (Instant.now().isBefore(deadline)) {
|
||||
DescribeStatementResponse describe = describe(id);
|
||||
StatusString status = describe.status();
|
||||
if (status == StatusString.FINISHED) {
|
||||
return id;
|
||||
}
|
||||
if (status == StatusString.FAILED || status == StatusString.ABORTED) {
|
||||
throw new IllegalStateException("Statement " + id + " ended " + status + ": " + describe.error());
|
||||
}
|
||||
Thread.sleep(250);
|
||||
}
|
||||
throw new IllegalStateException("Statement " + id + " did not finish in time");
|
||||
}
|
||||
|
||||
private DescribeStatementResponse describe(String id) {
|
||||
return data.describeStatement(DescribeStatementRequest.builder().id(id).build());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user