mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
feat(redshift): send zero-ETL integration events to EventBridge (#6822)
This commit is contained in:
1 parent
a682400d45
commit
c0d1767ec0
8 files changed
+625
-18
No files matched your search
@@ -343,6 +343,32 @@ approximation to indicate that initial backfill is in progress); it becomes `act
|
||||
exhausted. The landing table is an append-only log, so an item changed while its table is still being
|
||||
backfilled can appear twice: once from the scan and once from the stream.
|
||||
|
||||
### EventBridge events
|
||||
|
||||
As an integration changes state, Floci sends `aws.redshift` events to the default EventBridge bus
|
||||
of the integration's account and Region, as AWS does for zero-ETL integrations. No subscription is
|
||||
needed: a rule on `source = aws.redshift` receives them. The `detail-type` is
|
||||
`Redshift Integration Monitoring`, and the event IDs, severities and messages follow the
|
||||
[AWS event table](https://docs.aws.amazon.com/redshift/latest/mgmt/integration-event-notifications.html).
|
||||
`resources` holds the integration ARN.
|
||||
|
||||
| Event ID | Severity | Sent when |
|
||||
| --- | --- | --- |
|
||||
| `REDSHIFT-INTEGRATION-EVENT-0003` | INFO | The integration starts or resumes synchronizing (`syncing`) |
|
||||
| `REDSHIFT-INTEGRATION-EVENT-0000` | INFO | The initial backfill finishes and the integration becomes `active` |
|
||||
| `REDSHIFT-INTEGRATION-EVENT-0013` | INFO | The integration returns to `active` after a failure |
|
||||
| `REDSHIFT-INTEGRATION-EVENT-0007` | ERROR | The consumer fails to write and the integration becomes `failed` |
|
||||
| `REDSHIFT-INTEGRATION-EVENT-0002` | INFO | `DeleteIntegration` starts |
|
||||
| `REDSHIFT-INTEGRATION-EVENT-0001` | INFO | `DeleteIntegration` finishes |
|
||||
|
||||
The `detail` object carries `eventId`, `severity`, `category`, `message` and `integrationArn`.
|
||||
AWS does not publish a JSON example of the `detail` of these events, so these field names are
|
||||
Floci's representation of the table's columns, not a claim of byte-for-byte AWS parity.
|
||||
Floci sends only the events its DynamoDB integration can raise: the Aurora, primary-key,
|
||||
data-type, pause and resume, serverless restore and modification events do not occur here. Delivery
|
||||
is best effort. It is skipped when the EventBridge service is disabled, a refused or failed
|
||||
delivery is logged, and neither changes the outcome of the Redshift operation.
|
||||
|
||||
Serverless Redshift targets, schema inference, and relational projection are not supported in this
|
||||
first implementation.
|
||||
|
||||
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package io.github.hectorvent.floci.services.redshift;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Integration;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The {@code aws.redshift} events AWS sends to the default EventBridge bus as a zero-ETL integration
|
||||
* changes state. The detail types, event IDs, severities and messages come from the Redshift
|
||||
* Management Guide's "Zero-ETL integration event notifications" table; only the events Floci's
|
||||
* DynamoDB integration can raise are implemented. AWS publishes no JSON example, so the field names of
|
||||
* the detail are Floci's own representation of the table's columns.
|
||||
* {@link RedshiftService} queues an event while it holds its monitor and publishes the queue once it
|
||||
* does not, so a rule's target that calls Redshift never waits on the call that made the change, and
|
||||
* events go out in the order their changes were made.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
class RedshiftIntegrationEventPublisher {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(RedshiftIntegrationEventPublisher.class);
|
||||
static final String SOURCE = "aws.redshift";
|
||||
|
||||
/** The implemented rows of the AWS table: category doubles as the detail type's suffix. */
|
||||
enum IntegrationEvent {
|
||||
CREATED_ACTIVE("0000", "Monitoring", "INFO",
|
||||
"Your zero-ETL Integration %s has been created and became ACTIVE at %s."),
|
||||
DELETED("0001", "Monitoring", "INFO", "Your zero-ETL Integration %s has been deleted at %s."),
|
||||
DELETION_INITIATED("0002", "Monitoring", "INFO",
|
||||
"Your zero-ETL Integration %s has initiated deletion at %s."),
|
||||
SYNCHRONIZING("0003", "Monitoring", "INFO",
|
||||
"Your zero-ETL Integration %s is synchronizing transactional data to the Amazon Redshift data warehouse."),
|
||||
FAILED("0007", "Monitoring", "ERROR",
|
||||
"Unable to load data due to an internal error. Delete and recreate the integration. "
|
||||
+ "If the error persists, contact AWS Support."),
|
||||
ACTIVE("0013", "Monitoring", "INFO", "Your zero-ETL Integration %s is now ACTIVE.");
|
||||
|
||||
private final String number;
|
||||
private final String category;
|
||||
private final String severity;
|
||||
private final String message;
|
||||
|
||||
IntegrationEvent(String number, String category, String severity, String message) {
|
||||
this.number = number;
|
||||
this.category = category;
|
||||
this.severity = severity;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
String eventId() {
|
||||
return "REDSHIFT-INTEGRATION-EVENT-" + number;
|
||||
}
|
||||
|
||||
String detailType() {
|
||||
return "Redshift Integration " + category;
|
||||
}
|
||||
|
||||
String render(String integrationName, String time) {
|
||||
return switch (this) {
|
||||
case CREATED_ACTIVE, DELETED, DELETION_INITIATED -> message.formatted(integrationName, time);
|
||||
case SYNCHRONIZING, ACTIVE -> message.formatted(integrationName);
|
||||
case FAILED -> message;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private record Queued(String detailType, String detail, String resourceArn, String region, String accountId) { }
|
||||
|
||||
private final EventBridgeService eventBridgeService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ServiceRegistry registry;
|
||||
private final Queue<Queued> queued = new ConcurrentLinkedQueue<>();
|
||||
private final AtomicBoolean publishing = new AtomicBoolean();
|
||||
|
||||
@Inject
|
||||
RedshiftIntegrationEventPublisher(EventBridgeService eventBridgeService, ObjectMapper objectMapper,
|
||||
RegionResolver regionResolver, ServiceRegistry registry) {
|
||||
this.eventBridgeService = eventBridgeService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.regionResolver = regionResolver;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/** Queues {@code event} for {@code integration}'s Region, in the calling account. */
|
||||
void queue(IntegrationEvent event, Integration integration, String region, Instant at) {
|
||||
ObjectNode detail = objectMapper.createObjectNode();
|
||||
detail.put("eventId", event.eventId());
|
||||
detail.put("severity", event.severity);
|
||||
detail.put("category", event.category);
|
||||
detail.put("message", event.render(integration.getIntegrationName(), RedshiftService.eventTime(at)));
|
||||
detail.put("integrationArn", integration.getIntegrationArn());
|
||||
queued.add(new Queued(event.detailType(), detail.toString(), integration.getIntegrationArn(), region,
|
||||
accountId(integration)));
|
||||
}
|
||||
|
||||
// The consumer thread has no request account, so the integration's own account wins.
|
||||
private String accountId(Integration integration) {
|
||||
return integration.getAccountId() != null ? integration.getAccountId() : regionResolver.getAccountId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes the queued events in order. One call publishes at a time: a call made while an event
|
||||
* is delivered leaves its events queued behind the rest and returns, and the call already
|
||||
* publishing sends them after.
|
||||
*/
|
||||
void publishQueued() {
|
||||
while (!queued.isEmpty() && publishing.compareAndSet(false, true)) {
|
||||
try {
|
||||
Queued next;
|
||||
while ((next = queued.poll()) != null) {
|
||||
publish(next);
|
||||
}
|
||||
} finally {
|
||||
publishing.set(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void publish(Queued event) {
|
||||
if (!registry.isServiceEnabled("events")) {
|
||||
LOG.debugv("EventBridge is disabled; dropping the {0} event for {1}", event.detailType(),
|
||||
event.resourceArn());
|
||||
return;
|
||||
}
|
||||
RequestScopes.runAs(event.accountId(), event.region(), () -> eventBridgeService.publishServiceEvent(
|
||||
SOURCE, event.detailType(), event.detail(), event.resourceArn(), event.region()));
|
||||
}
|
||||
}
|
||||
@@ -103,6 +103,7 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
private final DynamoDbStreamReader streamReader;
|
||||
private final RedshiftAuditLogPublisher auditLogPublisher;
|
||||
private final RedshiftEventService events;
|
||||
private final RedshiftIntegrationEventPublisher integrationEvents;
|
||||
// Proxy ports currently handed out, so allocateProxyPort never double-assigns within this JVM.
|
||||
private final Set<Integer> usedPorts = ConcurrentHashMap.newKeySet();
|
||||
private final ConcurrentHashMap<ResourceKey, ResourceLock> resourceLocks = new ConcurrentHashMap<>();
|
||||
@@ -155,11 +156,15 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
return mutation.get();
|
||||
}
|
||||
|
||||
private static final DateTimeFormatter EVENT_MESSAGE_TIME =
|
||||
static final DateTimeFormatter EVENT_MESSAGE_TIME =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(ZoneOffset.UTC);
|
||||
|
||||
static String eventTime(Instant at) {
|
||||
return EVENT_MESSAGE_TIME.format(at) + " UTC";
|
||||
}
|
||||
|
||||
private static String eventTime() {
|
||||
return EVENT_MESSAGE_TIME.format(Instant.now()) + " UTC";
|
||||
return eventTime(Instant.now());
|
||||
}
|
||||
|
||||
private void emitEvent(EventInfo info, String sourceIdentifier, String message) {
|
||||
@@ -181,7 +186,7 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
RedshiftCredentialBroker credentialBroker,
|
||||
SecretsManagerService secretsManagerService, ObjectMapper objectMapper,
|
||||
DynamoDbStreamReader streamReader, RedshiftAuditLogPublisher auditLogPublisher,
|
||||
RedshiftEventService events) {
|
||||
RedshiftEventService events, RedshiftIntegrationEventPublisher integrationEvents) {
|
||||
this.clusters = storageFactory.create("redshift", "redshift-clusters.json", new TypeReference<Map<String, Cluster>>() {});
|
||||
this.snapshots = storageFactory.create("redshift", "redshift-snapshots.json", new TypeReference<Map<String, Snapshot>>() {});
|
||||
this.parameterGroups = storageFactory.create("redshift", "redshift-parameter-groups.json", new TypeReference<Map<String, ClusterParameterGroup>>() {});
|
||||
@@ -202,6 +207,18 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
this.streamReader = streamReader;
|
||||
this.auditLogPublisher = auditLogPublisher;
|
||||
this.events = events;
|
||||
this.integrationEvents = integrationEvents;
|
||||
}
|
||||
|
||||
RedshiftService(StorageFactory storageFactory, RedshiftContainerManager containerManager,
|
||||
EmulatorConfig config, RegionResolver regionResolver, RedshiftProxyManager proxyManager,
|
||||
DockerHostResolver dockerHostResolver, RedshiftCredentialBroker credentialBroker,
|
||||
SecretsManagerService secretsManagerService, ObjectMapper objectMapper,
|
||||
DynamoDbStreamReader streamReader, RedshiftAuditLogPublisher auditLogPublisher,
|
||||
RedshiftEventService events) {
|
||||
this(storageFactory, containerManager, config, regionResolver, proxyManager, dockerHostResolver,
|
||||
credentialBroker, secretsManagerService, objectMapper, streamReader, auditLogPublisher, events,
|
||||
null);
|
||||
}
|
||||
|
||||
RedshiftService(StorageFactory storageFactory, RedshiftContainerManager containerManager,
|
||||
@@ -511,10 +528,20 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
// Metadata only: no data is replicated from the source. The shape and the lower case status
|
||||
// were captured from a live integration in us-west-2.
|
||||
|
||||
public synchronized Integration createIntegration(String integrationName, String sourceArn, String targetArn,
|
||||
String kmsKeyId, String description,
|
||||
Map<String, String> additionalEncryptionContext,
|
||||
Map<String, String> tags, String region) {
|
||||
public Integration createIntegration(String integrationName, String sourceArn, String targetArn,
|
||||
String kmsKeyId, String description,
|
||||
Map<String, String> additionalEncryptionContext,
|
||||
Map<String, String> tags, String region) {
|
||||
Integration integration = createIntegrationLocked(integrationName, sourceArn, targetArn, kmsKeyId,
|
||||
description, additionalEncryptionContext, tags, region);
|
||||
publishIntegrationEvents();
|
||||
return integration;
|
||||
}
|
||||
|
||||
private synchronized Integration createIntegrationLocked(String integrationName, String sourceArn,
|
||||
String targetArn, String kmsKeyId, String description,
|
||||
Map<String, String> additionalEncryptionContext,
|
||||
Map<String, String> tags, String region) {
|
||||
if (integrationName == null || integrationName.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "IntegrationName is required.", 400);
|
||||
}
|
||||
@@ -591,6 +618,7 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
integration.setTags(tags);
|
||||
integrations.put(integrationId, integration);
|
||||
LOG.infov("Created Redshift zero-ETL integration: {0}", integration.getIntegrationArn());
|
||||
queueIntegrationEvent(RedshiftIntegrationEventPublisher.IntegrationEvent.SYNCHRONIZING, integration);
|
||||
return integration;
|
||||
}
|
||||
|
||||
@@ -666,15 +694,23 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public synchronized void updateIntegrationRuntime(String accountId, String integrationArn,
|
||||
Map<String, String> shardSequenceNumbers,
|
||||
boolean successful, String error) {
|
||||
public void updateIntegrationRuntime(String accountId, String integrationArn,
|
||||
Map<String, String> shardSequenceNumbers,
|
||||
boolean successful, String error) {
|
||||
updateIntegrationRuntimeLocked(accountId, integrationArn, shardSequenceNumbers, successful, error);
|
||||
publishIntegrationEvents();
|
||||
}
|
||||
|
||||
private synchronized void updateIntegrationRuntimeLocked(String accountId, String integrationArn,
|
||||
Map<String, String> shardSequenceNumbers,
|
||||
boolean successful, String error) {
|
||||
for (String key : integrations.keysForAccount(accountId)) {
|
||||
Optional<Integration> stored = integrations.getForAccount(accountId, key);
|
||||
if (stored.isEmpty() || !integrationArn.equals(stored.get().getIntegrationArn())) {
|
||||
continue;
|
||||
}
|
||||
Integration integration = stored.get();
|
||||
String previousStatus = integration.getStatus();
|
||||
if (successful) {
|
||||
integration.setShardSequenceNumbers(shardSequenceNumbers);
|
||||
integration.setRetryCount(0);
|
||||
@@ -686,26 +722,41 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
integration.setStatus("failed");
|
||||
}
|
||||
integrations.putForAccount(accountId, key, integration);
|
||||
queueStatusChange(integration, previousStatus);
|
||||
return;
|
||||
}
|
||||
throw new AwsException("IntegrationNotFoundFault", "The requested integration doesn't exist.", 404);
|
||||
}
|
||||
|
||||
public synchronized void updateIntegrationBackfillProgress(String accountId, String integrationArn,
|
||||
String backfillLastEvaluatedKey,
|
||||
boolean backfillCompleted) {
|
||||
public void updateIntegrationBackfillProgress(String accountId, String integrationArn,
|
||||
String backfillLastEvaluatedKey, boolean backfillCompleted) {
|
||||
updateIntegrationBackfillProgressLocked(accountId, integrationArn, backfillLastEvaluatedKey,
|
||||
backfillCompleted);
|
||||
publishIntegrationEvents();
|
||||
}
|
||||
|
||||
private synchronized void updateIntegrationBackfillProgressLocked(String accountId, String integrationArn,
|
||||
String backfillLastEvaluatedKey,
|
||||
boolean backfillCompleted) {
|
||||
for (String key : integrations.keysForAccount(accountId)) {
|
||||
Optional<Integration> stored = integrations.getForAccount(accountId, key);
|
||||
if (stored.isEmpty() || !integrationArn.equals(stored.get().getIntegrationArn())) {
|
||||
continue;
|
||||
}
|
||||
Integration integration = stored.get();
|
||||
String previousStatus = integration.getStatus();
|
||||
boolean backfillJustCompleted = !integration.isBackfillCompleted() && backfillCompleted;
|
||||
integration.setBackfillLastEvaluatedKey(backfillLastEvaluatedKey);
|
||||
integration.setBackfillCompleted(backfillCompleted);
|
||||
integration.setRetryCount(0);
|
||||
integration.setLastError(null);
|
||||
integration.setStatus(backfillCompleted ? "active" : "syncing");
|
||||
integrations.putForAccount(accountId, key, integration);
|
||||
if (backfillJustCompleted) {
|
||||
queueIntegrationEvent(RedshiftIntegrationEventPublisher.IntegrationEvent.CREATED_ACTIVE, integration);
|
||||
} else {
|
||||
queueStatusChange(integration, previousStatus);
|
||||
}
|
||||
return;
|
||||
}
|
||||
throw new AwsException("IntegrationNotFoundFault", "The requested integration doesn't exist.", 404);
|
||||
@@ -752,7 +803,13 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
return maxRecords;
|
||||
}
|
||||
|
||||
public synchronized Integration deleteIntegration(String integrationArn) {
|
||||
public Integration deleteIntegration(String integrationArn) {
|
||||
Integration deleted = deleteIntegrationLocked(integrationArn);
|
||||
publishIntegrationEvents();
|
||||
return deleted;
|
||||
}
|
||||
|
||||
private synchronized Integration deleteIntegrationLocked(String integrationArn) {
|
||||
if (integrationArn == null || integrationArn.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "IntegrationArn is required.", 400);
|
||||
}
|
||||
@@ -761,12 +818,47 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
if (stored.isPresent() && integrationArn.equals(stored.get().getIntegrationArn())) {
|
||||
integrations.delete(key);
|
||||
LOG.infov("Deleted Redshift zero-ETL integration: {0}", integrationArn);
|
||||
// Deletion is immediate here, so both the start and the end of it are reported.
|
||||
queueIntegrationEvent(RedshiftIntegrationEventPublisher.IntegrationEvent.DELETION_INITIATED,
|
||||
stored.get());
|
||||
queueIntegrationEvent(RedshiftIntegrationEventPublisher.IntegrationEvent.DELETED, stored.get());
|
||||
return stored.get();
|
||||
}
|
||||
}
|
||||
throw new AwsException("IntegrationNotFoundFault", "The requested integration doesn't exist.", 404);
|
||||
}
|
||||
|
||||
private void queueIntegrationEvent(RedshiftIntegrationEventPublisher.IntegrationEvent event,
|
||||
Integration integration) {
|
||||
if (integrationEvents != null) {
|
||||
integrationEvents.queue(event, integration, AwsArnUtils.parse(integration.getIntegrationArn()).region(),
|
||||
Instant.now());
|
||||
}
|
||||
}
|
||||
|
||||
/** Queues the event for a move into syncing, active or failed; any other change is silent. */
|
||||
private void queueStatusChange(Integration integration, String previousStatus) {
|
||||
String status = integration.getStatus();
|
||||
if (status == null || status.equals(previousStatus)) {
|
||||
return;
|
||||
}
|
||||
switch (status) {
|
||||
case "syncing" -> queueIntegrationEvent(
|
||||
RedshiftIntegrationEventPublisher.IntegrationEvent.SYNCHRONIZING, integration);
|
||||
case "active" -> queueIntegrationEvent(
|
||||
RedshiftIntegrationEventPublisher.IntegrationEvent.ACTIVE, integration);
|
||||
case "failed" -> queueIntegrationEvent(
|
||||
RedshiftIntegrationEventPublisher.IntegrationEvent.FAILED, integration);
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
|
||||
private void publishIntegrationEvents() {
|
||||
if (integrationEvents != null) {
|
||||
integrationEvents.publishQueued();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Cluster> describeClusters(String identifier) {
|
||||
if (identifier != null) {
|
||||
Optional<Cluster> cluster = clusters.get(identifier);
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ class RedshiftDataEventPublisher {
|
||||
|
||||
void publish(RedshiftDataStatementStore.StoredStatement statement) {
|
||||
try {
|
||||
if (!registry.isServiceEnabled("eventbridge")) {
|
||||
if (!registry.isServiceEnabled("events")) {
|
||||
LOG.warnv("EventBridge disabled for statement {0}, account {1}, region {2}",
|
||||
statement.id, statement.accountId, statement.region);
|
||||
return;
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package io.github.hectorvent.floci.services.redshift;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ResolvedServiceCatalog;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.redshift.RedshiftIntegrationEventPublisher.IntegrationEvent;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Integration;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.contains;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class RedshiftIntegrationEventPublisherTest {
|
||||
|
||||
private static final String ARN = "arn:aws:redshift:eu-west-1:222233334444:integration:abc";
|
||||
private static final Instant AT = Instant.parse("2026-10-28T11:30:15Z");
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private EventBridgeService events;
|
||||
private ServiceRegistry registry;
|
||||
private RedshiftIntegrationEventPublisher publisher;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
events = mock(EventBridgeService.class);
|
||||
registry = mock(ServiceRegistry.class);
|
||||
when(registry.isServiceEnabled("events")).thenReturn(true);
|
||||
publisher = new RedshiftIntegrationEventPublisher(events, mapper,
|
||||
new RegionResolver("us-east-1", "111111111111"), registry);
|
||||
}
|
||||
|
||||
private static Integration integration() {
|
||||
Integration integration = new Integration();
|
||||
integration.setIntegrationArn(ARN);
|
||||
integration.setIntegrationName("orders-to-warehouse");
|
||||
integration.setAccountId("222233334444");
|
||||
return integration;
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishesTheAwsTableRowAsAnAwsRedshiftEvent() throws Exception {
|
||||
publisher.queue(IntegrationEvent.CREATED_ACTIVE, integration(), "eu-west-1", AT);
|
||||
publisher.publishQueued();
|
||||
|
||||
ArgumentCaptor<String> detail = ArgumentCaptor.forClass(String.class);
|
||||
verify(events).publishServiceEvent(eq("aws.redshift"), eq("Redshift Integration Monitoring"),
|
||||
detail.capture(), eq(ARN), eq("eu-west-1"));
|
||||
JsonNode body = mapper.readTree(detail.getValue());
|
||||
assertEquals("REDSHIFT-INTEGRATION-EVENT-0000", body.path("eventId").asText());
|
||||
assertEquals("INFO", body.path("severity").asText());
|
||||
assertEquals("Monitoring", body.path("category").asText());
|
||||
assertEquals(ARN, body.path("integrationArn").asText());
|
||||
assertEquals("Your zero-ETL Integration orders-to-warehouse has been created and became ACTIVE at "
|
||||
+ "2026-10-28 11:30 UTC.", body.path("message").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedEventIsAnErrorWithTheTablesMessage() throws Exception {
|
||||
publisher.queue(IntegrationEvent.FAILED, integration(), "eu-west-1", AT);
|
||||
publisher.publishQueued();
|
||||
|
||||
ArgumentCaptor<String> detail = ArgumentCaptor.forClass(String.class);
|
||||
verify(events).publishServiceEvent(eq("aws.redshift"), eq("Redshift Integration Monitoring"),
|
||||
detail.capture(), eq(ARN), eq("eu-west-1"));
|
||||
JsonNode body = mapper.readTree(detail.getValue());
|
||||
assertEquals("REDSHIFT-INTEGRATION-EVENT-0007", body.path("eventId").asText());
|
||||
assertEquals("ERROR", body.path("severity").asText());
|
||||
assertTrue(body.path("message").asText().startsWith("Unable to load data due to an internal error."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nothingIsPublishedUntilTheQueueIsFlushed() {
|
||||
publisher.queue(IntegrationEvent.SYNCHRONIZING, integration(), "eu-west-1", AT);
|
||||
|
||||
verify(events, never()).publishServiceEvent(anyString(), anyString(), anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventsGoOutInTheOrderTheyWereQueued() {
|
||||
publisher.queue(IntegrationEvent.DELETION_INITIATED, integration(), "eu-west-1", AT);
|
||||
publisher.queue(IntegrationEvent.DELETED, integration(), "eu-west-1", AT);
|
||||
publisher.publishQueued();
|
||||
|
||||
InOrder order = inOrder(events);
|
||||
order.verify(events).publishServiceEvent(eq("aws.redshift"), eq("Redshift Integration Monitoring"),
|
||||
contains("EVENT-0002"), eq(ARN), eq("eu-west-1"));
|
||||
order.verify(events).publishServiceEvent(eq("aws.redshift"), eq("Redshift Integration Monitoring"),
|
||||
contains("EVENT-0001"), eq(ARN), eq("eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDisabledEventBridgeDropsTheEventWithoutFailing() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().eventbridge().enabled()).thenReturn(false);
|
||||
ServiceRegistry disabledRegistry = new ServiceRegistry(new ResolvedServiceCatalog(config));
|
||||
publisher = new RedshiftIntegrationEventPublisher(events, mapper,
|
||||
new RegionResolver("us-east-1", "111111111111"), disabledRegistry);
|
||||
|
||||
publisher.queue(IntegrationEvent.ACTIVE, integration(), "eu-west-1", AT);
|
||||
publisher.publishQueued();
|
||||
|
||||
verify(events, never()).publishServiceEvent(anyString(), anyString(), anyString(), any(), anyString());
|
||||
}
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package io.github.hectorvent.floci.services.redshift;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.redshift.RedshiftIntegrationEventPublisher.IntegrationEvent;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Integration;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class RedshiftIntegrationEventsIntegrationTest {
|
||||
private static final String ACCOUNT = "111122223333";
|
||||
private static final String OTHER_ACCOUNT = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Inject
|
||||
RedshiftIntegrationEventPublisher publisher;
|
||||
@Inject
|
||||
EventBridgeService events;
|
||||
@Inject
|
||||
SqsService sqs;
|
||||
@Inject
|
||||
ObjectMapper mapper;
|
||||
|
||||
@Test
|
||||
void integrationEventReachesOnlyTheIntegrationsAccountRule() throws Exception {
|
||||
String name = "rs-integration-event-" + UUID.randomUUID();
|
||||
String pattern = mapper.writeValueAsString(Map.of("source", List.of("aws.redshift"),
|
||||
"detail-type", List.of("Redshift Integration Monitoring"),
|
||||
"detail", Map.of("eventId", List.of("REDSHIFT-INTEGRATION-EVENT-0000"))));
|
||||
String queue = createRuleAndQueue(ACCOUNT, name, pattern);
|
||||
String otherQueue = createRuleAndQueue(OTHER_ACCOUNT, name, pattern);
|
||||
Integration integration = new Integration();
|
||||
integration.setAccountId(ACCOUNT);
|
||||
integration.setIntegrationName(name);
|
||||
integration.setIntegrationArn("arn:aws:redshift:" + REGION + ":" + ACCOUNT + ":integration:" + name);
|
||||
try {
|
||||
publisher.queue(IntegrationEvent.SYNCHRONIZING, integration, REGION, Instant.now());
|
||||
publisher.queue(IntegrationEvent.CREATED_ACTIVE, integration, REGION, Instant.now());
|
||||
publisher.publishQueued();
|
||||
|
||||
AtomicReference<JsonNode> delivered = new AtomicReference<>();
|
||||
await().atMost(Duration.ofSeconds(10)).until(() -> {
|
||||
List<Message> messages = RequestScopes.callAs(ACCOUNT, REGION,
|
||||
() -> sqs.receiveMessage(queue, 1, 30, 0, REGION));
|
||||
if (messages.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
delivered.set(mapper.readTree(messages.getFirst().getBody()));
|
||||
return true;
|
||||
});
|
||||
assertEquals("aws.redshift", delivered.get().path("source").asText());
|
||||
assertEquals("Redshift Integration Monitoring", delivered.get().path("detail-type").asText());
|
||||
assertEquals(ACCOUNT, delivered.get().path("account").asText());
|
||||
assertEquals(integration.getIntegrationArn(), delivered.get().path("resources").get(0).asText());
|
||||
assertEquals("REDSHIFT-INTEGRATION-EVENT-0000", delivered.get().path("detail").path("eventId").asText());
|
||||
assertTrue(RequestScopes.callAs(OTHER_ACCOUNT, REGION,
|
||||
() -> sqs.receiveMessage(otherQueue, 1, 30, 0, REGION)).isEmpty());
|
||||
} finally {
|
||||
deleteRuleAndQueue(ACCOUNT, name, queue);
|
||||
deleteRuleAndQueue(OTHER_ACCOUNT, name, otherQueue);
|
||||
}
|
||||
}
|
||||
|
||||
private String createRuleAndQueue(String account, String name, String pattern) {
|
||||
return RequestScopes.callAs(account, REGION, () -> {
|
||||
String queue = sqs.createQueue(name, Map.of(), REGION).getQueueUrl();
|
||||
events.putRule(name, "default", pattern, null, RuleState.ENABLED, null, null, Map.of(), REGION);
|
||||
events.putTargets(name, "default", List.of(new Target("queue",
|
||||
"arn:aws:sqs:" + REGION + ":" + account + ":" + name, null, null)), REGION);
|
||||
return queue;
|
||||
});
|
||||
}
|
||||
|
||||
private void deleteRuleAndQueue(String account, String name, String queue) {
|
||||
RequestScopes.runAs(account, REGION, () -> {
|
||||
events.removeTargets(name, "default", List.of("queue"), REGION);
|
||||
events.deleteRule(name, "default", REGION);
|
||||
sqs.deleteQueue(queue, REGION);
|
||||
});
|
||||
}
|
||||
}
|
||||
+106
-1
@@ -579,6 +579,7 @@ class RedshiftServiceTest {
|
||||
private SecretsManagerService secretsManagerService;
|
||||
private RedshiftEventService events;
|
||||
private final Set<String> existingStreams = new HashSet<>();
|
||||
private RedshiftIntegrationEventPublisher integrationEvents;
|
||||
private RedshiftService service;
|
||||
|
||||
@BeforeEach
|
||||
@@ -642,9 +643,113 @@ class RedshiftServiceTest {
|
||||
Map.of());
|
||||
};
|
||||
|
||||
integrationEvents = mock(RedshiftIntegrationEventPublisher.class);
|
||||
service = new RedshiftService(sf, cm, config, regionResolver, proxyManager, dockerHostResolver,
|
||||
credentialBroker, secretsManagerService, mapper,
|
||||
new DynamoDbApiStreamReader(streams, CheckpointLifetime.PROCESS), null, events);
|
||||
new DynamoDbApiStreamReader(streams, CheckpointLifetime.PROCESS), null, events, integrationEvents);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createIntegrationQueuesSynchronizingAndPublishesAfterTheChange() {
|
||||
String streamArn = "arn:aws:dynamodb:us-east-1:111111111111:table/orders/stream/2026-09-18T00:00:00.000";
|
||||
existingStreams.add(streamArn);
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setClusterIdentifier("warehouse");
|
||||
when(clusterBackend.get("warehouse")).thenReturn(Optional.of(cluster));
|
||||
when(integrationBackend.scan(any())).thenReturn(List.of());
|
||||
|
||||
Integration integration = service.createIntegration("orders-to-warehouse", streamArn,
|
||||
"arn:aws:redshift:us-east-1:111111111111:cluster:warehouse", null, null, Map.of(), Map.of(),
|
||||
"us-east-1");
|
||||
|
||||
InOrder order = inOrder(integrationEvents);
|
||||
order.verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.SYNCHRONIZING),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
order.verify(integrationEvents).publishQueued();
|
||||
}
|
||||
|
||||
@Test
|
||||
void integrationStatusChangesQueueTheMatchingEventOnce() {
|
||||
String integrationArn = "arn:aws:redshift:us-east-1:111111111111:integration:one";
|
||||
Integration integration = new Integration();
|
||||
integration.setIntegrationArn(integrationArn);
|
||||
integration.setStatus("syncing");
|
||||
when(integrationBackend.keysForAccount("111111111111")).thenReturn(Set.of("one"));
|
||||
when(integrationBackend.getForAccount("111111111111", "one")).thenReturn(Optional.of(integration));
|
||||
|
||||
service.updateIntegrationBackfillProgress("111111111111", integrationArn, null, true);
|
||||
service.updateIntegrationBackfillProgress("111111111111", integrationArn, null, true);
|
||||
service.updateIntegrationRuntime("111111111111", integrationArn, Map.of(), false, "boom");
|
||||
service.updateIntegrationRuntime("111111111111", integrationArn, Map.of(), false, "boom");
|
||||
service.updateIntegrationRuntime("111111111111", integrationArn, Map.of(), true, null);
|
||||
|
||||
InOrder order = inOrder(integrationEvents);
|
||||
order.verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.CREATED_ACTIVE),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
order.verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.FAILED),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
order.verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.ACTIVE),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
verify(integrationEvents, times(3)).queue(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void backfillCompletionQueuesActiveAndAFurtherBatchQueuesNothing() {
|
||||
String integrationArn = "arn:aws:redshift:us-east-1:111111111111:integration:one";
|
||||
Integration integration = new Integration();
|
||||
integration.setIntegrationArn(integrationArn);
|
||||
integration.setStatus("syncing");
|
||||
when(integrationBackend.keysForAccount("111111111111")).thenReturn(Set.of("one"));
|
||||
when(integrationBackend.getForAccount("111111111111", "one")).thenReturn(Optional.of(integration));
|
||||
|
||||
service.updateIntegrationBackfillProgress("111111111111", integrationArn, "{\"id\":{\"S\":\"1\"}}", false);
|
||||
verify(integrationEvents, never()).queue(any(), any(), any(), any());
|
||||
|
||||
service.updateIntegrationBackfillProgress("111111111111", integrationArn, null, true);
|
||||
verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.CREATED_ACTIVE),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void backfillCompletionAfterFailureStillQueuesCreatedActive() {
|
||||
String integrationArn = "arn:aws:redshift:us-east-1:111111111111:integration:one";
|
||||
Integration integration = new Integration();
|
||||
integration.setIntegrationArn(integrationArn);
|
||||
integration.setStatus("syncing");
|
||||
when(integrationBackend.keysForAccount("111111111111")).thenReturn(Set.of("one"));
|
||||
when(integrationBackend.getForAccount("111111111111", "one")).thenReturn(Optional.of(integration));
|
||||
|
||||
service.updateIntegrationRuntime("111111111111", integrationArn, Map.of(), false, "boom");
|
||||
assertEquals("failed", integration.getStatus());
|
||||
|
||||
service.updateIntegrationBackfillProgress("111111111111", integrationArn, null, true);
|
||||
|
||||
InOrder order = inOrder(integrationEvents);
|
||||
order.verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.FAILED),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
order.verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.CREATED_ACTIVE),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
verify(integrationEvents, never()).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.ACTIVE),
|
||||
any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteIntegrationQueuesDeletionStartThenEnd() {
|
||||
String integrationArn = "arn:aws:redshift:us-east-1:111111111111:integration:one";
|
||||
Integration integration = new Integration();
|
||||
integration.setIntegrationArn(integrationArn);
|
||||
when(integrationBackend.keys()).thenReturn(Set.of("one"));
|
||||
when(integrationBackend.get("one")).thenReturn(Optional.of(integration));
|
||||
|
||||
service.deleteIntegration(integrationArn);
|
||||
|
||||
InOrder order = inOrder(integrationEvents);
|
||||
order.verify(integrationEvents).queue(
|
||||
eq(RedshiftIntegrationEventPublisher.IntegrationEvent.DELETION_INITIATED), eq(integration),
|
||||
eq("us-east-1"), any());
|
||||
order.verify(integrationEvents).queue(eq(RedshiftIntegrationEventPublisher.IntegrationEvent.DELETED),
|
||||
eq(integration), eq("us-east-1"), any());
|
||||
order.verify(integrationEvents).publishQueued();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+23
-2
@@ -2,7 +2,9 @@ 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.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.ResolvedServiceCatalog;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -24,7 +26,7 @@ class RedshiftDataEventPublisherTest {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
EventBridgeService events = mock(EventBridgeService.class);
|
||||
ServiceRegistry registry = mock(ServiceRegistry.class);
|
||||
when(registry.isServiceEnabled("eventbridge")).thenReturn(true);
|
||||
when(registry.isServiceEnabled("events")).thenReturn(true);
|
||||
when(events.putEvents(anyList(), anyString(), anyString()))
|
||||
.thenReturn(new EventBridgeService.PutEventsResult(0, List.of()));
|
||||
RedshiftDataEventPublisher publisher = new RedshiftDataEventPublisher(events, mapper, registry);
|
||||
@@ -57,7 +59,7 @@ class RedshiftDataEventPublisherTest {
|
||||
void rejectedEntriesAndRefusalsDoNotFailSqlCompletion() {
|
||||
EventBridgeService events = mock(EventBridgeService.class);
|
||||
ServiceRegistry registry = mock(ServiceRegistry.class);
|
||||
when(registry.isServiceEnabled("eventbridge")).thenReturn(true);
|
||||
when(registry.isServiceEnabled("events")).thenReturn(true);
|
||||
RedshiftDataEventPublisher publisher = new RedshiftDataEventPublisher(events, new ObjectMapper(), registry);
|
||||
RedshiftDataStatementStore.StoredStatement statement = new RedshiftDataStatementStore.StoredStatement();
|
||||
statement.id = "id";
|
||||
@@ -73,4 +75,23 @@ class RedshiftDataEventPublisherTest {
|
||||
assertDoesNotThrow(() -> publisher.publish(statement));
|
||||
assertThrows(IllegalStateException.class, () -> publisher.publish(statement));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDisabledEventBridgeDropsTheEventWithoutFailing() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().eventbridge().enabled()).thenReturn(false);
|
||||
ServiceRegistry registry = new ServiceRegistry(new ResolvedServiceCatalog(config));
|
||||
EventBridgeService events = mock(EventBridgeService.class);
|
||||
RedshiftDataEventPublisher publisher = new RedshiftDataEventPublisher(events, new ObjectMapper(), registry);
|
||||
RedshiftDataStatementStore.StoredStatement statement = new RedshiftDataStatementStore.StoredStatement();
|
||||
statement.id = "id";
|
||||
statement.accountId = "111111111111";
|
||||
statement.region = "us-east-1";
|
||||
statement.status = RedshiftDataStatementStore.Status.FINISHED;
|
||||
statement.expiresAt = Instant.now();
|
||||
|
||||
assertDoesNotThrow(() -> publisher.publish(statement));
|
||||
|
||||
verifyNoInteractions(events);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user