mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(firehose): deliver Redshift COPY loads to a Serverless workgroup (#7227)
* feat(firehose): deliver Redshift COPY loads to a Serverless workgroup A ClusterJDBCURL with a redshift-serverless host now resolves the workgroup in the stream's account and region and loads through its runtime, with its database and admin user. A URL naming another database fails the delivery through the existing retry path. Provisioned delivery is unchanged. * fix(firehose): route Redshift Serverless delivery through the workgroup proxy Connect Firehose Serverless delivery to the workgroup's proxy endpoint (127.0.0.1:<proxyPort>) instead of the internal PostgreSQL container, matching provisioned cluster delivery and enabling S3CopySimulator to intercept COPY FROM 's3://...'. * fix(firehose): drop managed admin secret override on proxy delivery
This commit is contained in:
1 parent
3f675c00fb
commit
1fe8eae011
5 files changed
+184
-32
No files matched your search
+57
-21
@@ -2,9 +2,11 @@ package io.github.hectorvent.floci.services.firehose;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.DatabaseSecretCredentials;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.firehose.model.RedshiftDestination;
|
||||
import io.github.hectorvent.floci.services.redshift.RedshiftService;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Cluster;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -18,9 +20,9 @@ import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Loads a delivered S3 object into a Floci Redshift cluster. The {@code COPY} runs over a JDBC
|
||||
* connection to the cluster's wire proxy in simple query mode, so the proxy's S3 COPY emulation
|
||||
* does the work exactly as it does for any other client.
|
||||
* Loads a delivered S3 object into a Floci Redshift cluster or Redshift Serverless workgroup. The
|
||||
* {@code COPY} runs over a JDBC connection to the cluster's wire proxy, or to the workgroup's runtime,
|
||||
* in simple query mode, so the S3 COPY emulation does the work exactly as it does for any other client.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class FirehoseRedshiftDelivery {
|
||||
@@ -34,20 +36,28 @@ public class FirehoseRedshiftDelivery {
|
||||
void execute(String jdbcUrl, String user, String password, String sql) throws SQLException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a delivery connects. {@code defaultUser} signs in when the destination names none.
|
||||
*/
|
||||
private record Backend(String description, String host, int port, String database, String defaultUser) {
|
||||
}
|
||||
|
||||
private final RedshiftService redshiftService;
|
||||
private final RedshiftServerlessService serverlessService;
|
||||
private final SecretsManagerService secretsManagerService;
|
||||
private final ObjectMapper mapper;
|
||||
private final SqlRunner runner;
|
||||
|
||||
@Inject
|
||||
public FirehoseRedshiftDelivery(RedshiftService redshiftService, SecretsManagerService secretsManagerService,
|
||||
ObjectMapper mapper) {
|
||||
this(redshiftService, secretsManagerService, mapper, FirehoseRedshiftDelivery::runJdbc);
|
||||
public FirehoseRedshiftDelivery(RedshiftService redshiftService, RedshiftServerlessService serverlessService,
|
||||
SecretsManagerService secretsManagerService, ObjectMapper mapper) {
|
||||
this(redshiftService, serverlessService, secretsManagerService, mapper, FirehoseRedshiftDelivery::runJdbc);
|
||||
}
|
||||
|
||||
FirehoseRedshiftDelivery(RedshiftService redshiftService, SecretsManagerService secretsManagerService,
|
||||
ObjectMapper mapper, SqlRunner runner) {
|
||||
FirehoseRedshiftDelivery(RedshiftService redshiftService, RedshiftServerlessService serverlessService,
|
||||
SecretsManagerService secretsManagerService, ObjectMapper mapper, SqlRunner runner) {
|
||||
this.redshiftService = redshiftService;
|
||||
this.serverlessService = serverlessService;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.mapper = mapper;
|
||||
this.runner = runner;
|
||||
@@ -56,31 +66,57 @@ public class FirehoseRedshiftDelivery {
|
||||
/**
|
||||
* Runs the destination's COPY for the object at {@code s3://bucket/key}.
|
||||
*
|
||||
* Throws {@code AwsException} when the cluster or the secret cannot be resolved.
|
||||
* Throws {@code AwsException} when the cluster, the workgroup or the secret cannot be resolved.
|
||||
*
|
||||
* @throws IllegalStateException when the statement itself fails; the message carries the table and SQL state
|
||||
*/
|
||||
public void copy(String accountId, String region, RedshiftDestination destination, String bucket, String key) {
|
||||
RedshiftJdbcUrl target = RedshiftJdbcUrl.parse(destination.getClusterJdbcUrl());
|
||||
Cluster cluster = redshiftService.describeClustersForAccount(accountId, target.clusterIdentifier()).get(0);
|
||||
String[] credentials = credentials(destination, cluster, region);
|
||||
Backend backend = target.serverless()
|
||||
? workgroupBackend(accountId, region, target)
|
||||
: clusterBackend(accountId, target);
|
||||
String[] credentials = credentials(destination, backend, region);
|
||||
RedshiftDestination.CopyCommand command = destination.getCopyCommand();
|
||||
String sql = RedshiftCopySql.build(command.getDataTableName(), command.getDataTableColumns(), bucket, key,
|
||||
command.getCopyOptions());
|
||||
// The database name in the URL is ignored: the emulated container database is always "dev".
|
||||
String jdbcUrl = "jdbc:postgresql://127.0.0.1:" + cluster.getEndpoint().getPort()
|
||||
+ "/dev?preferQueryMode=simple";
|
||||
String jdbcUrl = "jdbc:postgresql://" + backend.host() + ":" + backend.port() + "/" + backend.database()
|
||||
+ "?preferQueryMode=simple";
|
||||
try {
|
||||
runner.execute(jdbcUrl, credentials[0], credentials[1], sql);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("COPY into " + command.getDataTableName() + " on cluster "
|
||||
+ target.clusterIdentifier() + " failed (SQLSTATE " + e.getSQLState() + "): " + e.getMessage(), e);
|
||||
throw new IllegalStateException("COPY into " + command.getDataTableName() + " on "
|
||||
+ backend.description() + " failed (SQLSTATE " + e.getSQLState() + "): " + e.getMessage(), e);
|
||||
}
|
||||
LOG.debugv("Firehose copied s3://{0}/{1} into {2} on cluster {3}", bucket, key,
|
||||
command.getDataTableName(), target.clusterIdentifier());
|
||||
LOG.debugv("Firehose copied s3://{0}/{1} into {2} on {3}", bucket, key,
|
||||
command.getDataTableName(), backend.description());
|
||||
}
|
||||
|
||||
private String[] credentials(RedshiftDestination destination, Cluster cluster, String region) {
|
||||
private Backend clusterBackend(String accountId, RedshiftJdbcUrl target) {
|
||||
Cluster cluster = redshiftService.describeClustersForAccount(accountId, target.clusterIdentifier()).get(0);
|
||||
// The database name in the URL is ignored: the emulated container database is always "dev".
|
||||
return new Backend("cluster " + target.clusterIdentifier(), "127.0.0.1", cluster.getEndpoint().getPort(),
|
||||
"dev", cluster.getMasterUsername());
|
||||
}
|
||||
|
||||
/**
|
||||
* A workgroup has one database, so a URL that names another one cannot load anything: the same
|
||||
* refusal the Data API gives, here a failed delivery that goes through the retry path.
|
||||
*/
|
||||
private Backend workgroupBackend(String accountId, String region, RedshiftJdbcUrl target) {
|
||||
RedshiftServerlessService.WorkgroupTarget workgroup = RequestScopes.callAs(accountId, region,
|
||||
() -> serverlessService.getWorkgroupTarget(target.clusterIdentifier(), region));
|
||||
if (!target.database().equals(workgroup.database())) {
|
||||
throw new IllegalStateException("Database " + target.database() + " does not exist in workgroup "
|
||||
+ workgroup.workgroupName() + "; the only database is " + workgroup.database() + ".");
|
||||
}
|
||||
int port = workgroup.endpoint() != null && workgroup.endpoint().getPort() > 0
|
||||
? workgroup.endpoint().getPort()
|
||||
: workgroup.port();
|
||||
return new Backend("workgroup " + workgroup.workgroupName(), "127.0.0.1", port,
|
||||
workgroup.database(), workgroup.adminUsername());
|
||||
}
|
||||
|
||||
private String[] credentials(RedshiftDestination destination, Backend backend, String region) {
|
||||
RedshiftDestination.SecretsManagerConfiguration secrets = destination.getSecretsManagerConfiguration();
|
||||
if (secrets != null && Boolean.TRUE.equals(secrets.getEnabled())) {
|
||||
SecretVersion secret = secretsManagerService.getSecretValue(secrets.getSecretArn(), null, null, region);
|
||||
@@ -90,10 +126,10 @@ public class FirehoseRedshiftDelivery {
|
||||
return new String[] {credentials.username(), credentials.password()};
|
||||
}
|
||||
if (destination.getPassword() == null) {
|
||||
throw new IllegalStateException("The Redshift destination for cluster " + cluster.getClusterIdentifier()
|
||||
throw new IllegalStateException("The Redshift destination for " + backend.description()
|
||||
+ " has no Password and no enabled SecretsManagerConfiguration, so Firehose cannot sign in.");
|
||||
}
|
||||
String user = destination.getUsername() != null ? destination.getUsername() : cluster.getMasterUsername();
|
||||
String user = destination.getUsername() != null ? destination.getUsername() : backend.defaultUser();
|
||||
return new String[] {user, destination.getPassword()};
|
||||
}
|
||||
|
||||
|
||||
+12
-4
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.redshift.SnapshotDumpCopier;
|
||||
import io.github.hectorvent.floci.services.redshift.TempCredential;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessSnapshotCopyService.CopyRule;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
|
||||
@@ -1201,24 +1202,31 @@ public class RedshiftServerlessService implements Resettable {
|
||||
return new WorkgroupTarget(workgroup.getWorkgroupArn(), workgroup.getWorkgroupName(),
|
||||
workgroup.getRuntimeHost(), workgroup.getRuntimePort(), namespace.getDbName(),
|
||||
workgroup.getMasterUsername(), namespace.getAdminUserPassword(), adminUserOf(namespace),
|
||||
namespace.getIamRoles(), namespace.getAdminPasswordSecretArn());
|
||||
namespace.getIamRoles(), namespace.getAdminPasswordSecretArn(), workgroup.getEndpoint());
|
||||
}
|
||||
|
||||
/** Connection details of a workgroup's backend; {@code adminUsername} is the name clients log in with. */
|
||||
public record WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername,
|
||||
List<String> iamRoleArns, String adminPasswordSecretArn) {
|
||||
List<String> iamRoleArns, String adminPasswordSecretArn, Endpoint endpoint) {
|
||||
public WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername,
|
||||
List<String> iamRoleArns, String adminPasswordSecretArn) {
|
||||
this(arn, workgroupName, host, port, database, masterUsername, masterPassword, adminUsername,
|
||||
iamRoleArns, adminPasswordSecretArn, null);
|
||||
}
|
||||
|
||||
public WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername,
|
||||
List<String> iamRoleArns) {
|
||||
this(arn, workgroupName, host, port, database, masterUsername, masterPassword, adminUsername,
|
||||
iamRoleArns, null);
|
||||
iamRoleArns, null, null);
|
||||
}
|
||||
|
||||
public WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername) {
|
||||
this(arn, workgroupName, host, port, database, masterUsername, masterPassword, adminUsername,
|
||||
List.of(), null);
|
||||
List.of(), null, null);
|
||||
}
|
||||
|
||||
public WorkgroupTarget {
|
||||
|
||||
Reference in new issue
Block a user