feat(redshift): send zero-ETL integration events to EventBridge (#6822)

This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-28 22:41:04 -04:00
1 parent a682400d45
commit c0d1767ec0
8 files changed
+625 -18

No files matched your search

@@ -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());
}
}
@@ -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);
});
}
}
@@ -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
@@ -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);
}
}