mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(firehose): run the Lambda processor of a Redshift destination (#7230)
This commit is contained in:
1 parent
1cbf58a8ae
commit
8c32e9f6ba
13 files changed
+370
-60
No files matched your search
+1
-1
@@ -391,7 +391,7 @@ class LeafCfnProvisionerTest {
|
||||
assertEquals(Boolean.TRUE, destination.getCloudWatchLoggingOptions().getEnabled());
|
||||
assertEquals("g", destination.getCloudWatchLoggingOptions().getLogGroupName());
|
||||
assertEquals("st", destination.getCloudWatchLoggingOptions().getLogStreamName());
|
||||
assertTrue(destination.isProcessingEnabled());
|
||||
assertEquals(Boolean.TRUE, destination.getProcessingConfiguration().getEnabled());
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:f", destination.getProcessingConfiguration()
|
||||
.getProcessors().get(0).getParameters().get(0).getParameterValue());
|
||||
}
|
||||
|
||||
+49
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescript
|
||||
import io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Processor;
|
||||
import io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.ProcessorParameter;
|
||||
import io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.S3Destination;
|
||||
import io.github.hectorvent.floci.services.firehose.model.RedshiftDestination;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
@@ -80,6 +81,22 @@ class FirehoseLambdaTransformerTest {
|
||||
return new DeliveryStreamDescription("stream", "arn:aws:firehose:us-west-2:000000000000:deliverystream/stream", s3);
|
||||
}
|
||||
|
||||
/** A stream whose Lambda is configured on its Redshift destination, as AWS models it, not on its S3 location. */
|
||||
private DeliveryStreamDescription redshiftStream() throws Exception {
|
||||
RedshiftDestination redshift = mapper.readValue("""
|
||||
{"RoleARN": "arn:aws:iam::000000000000:role/firehose",
|
||||
"ClusterJDBCURL": "jdbc:redshift://c1.abc.us-east-1.redshift.amazonaws.com:5439/dev",
|
||||
"CopyCommand": {"DataTableName": "events"},
|
||||
"S3Configuration": {"RoleARN": "arn:aws:iam::000000000000:role/firehose",
|
||||
"BucketARN": "arn:aws:s3:::results",
|
||||
"ErrorOutputPrefix": "errors/!{firehose:error-output-type}/"},
|
||||
"ProcessingConfiguration": {"Enabled": true, "Processors": [{"Type": "Lambda",
|
||||
"Parameters": [{"ParameterName": "LambdaArn", "ParameterValue": "%s"}]}]}}
|
||||
""".formatted(FUNCTION_ARN), RedshiftDestination.class);
|
||||
return new DeliveryStreamDescription("stream",
|
||||
"arn:aws:firehose:us-west-2:000000000000:deliverystream/stream", null, redshift, null);
|
||||
}
|
||||
|
||||
private static Processor lambdaProcessor(String... parameters) {
|
||||
List<ProcessorParameter> list = new ArrayList<>();
|
||||
list.add(parameter("LambdaArn", FUNCTION_ARN));
|
||||
@@ -202,6 +219,38 @@ class FirehoseLambdaTransformerTest {
|
||||
verifyNoInteractions(lambdaService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftStreamIsTransformedByTheLambdaOfItsDestination() throws Exception {
|
||||
replyWith(ids -> records(ok(ids.get(0), "one"), outcome(ids.get(1), "Dropped")));
|
||||
|
||||
FirehoseLambdaTransformer.Outcome outcome = transform(redshiftStream(), record("a"), record("b"));
|
||||
|
||||
assertEquals(List.of("one"), delivered(outcome));
|
||||
assertEquals(1, outcome.droppedRecords());
|
||||
verify(lambdaService).invokeArn(eq(FUNCTION_ARN), any(), eq(InvocationType.RequestResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftStreamsFailedRecordsGoToTheErrorOutputOfItsIntermediateS3Location() throws Exception {
|
||||
replyWith(ids -> records(outcome(ids.get(0), "ProcessingFailed")));
|
||||
|
||||
FirehoseLambdaTransformer.Outcome outcome = transform(redshiftStream(), record("a"));
|
||||
|
||||
assertEquals(1, outcome.failedRecords());
|
||||
assertEquals(1, errorLines().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftStreamWithADisabledProcessingConfigurationIsNotTransformed() throws Exception {
|
||||
DeliveryStreamDescription stream = redshiftStream();
|
||||
stream.processingConfiguration().setEnabled(false);
|
||||
|
||||
FirehoseLambdaTransformer.Outcome outcome = transform(stream, record("a"));
|
||||
|
||||
assertEquals(List.of("a"), delivered(outcome));
|
||||
verifyNoInteractions(lambdaService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aProcessorTypeFlociDoesNotApplyLeavesTheRecordsAlone() {
|
||||
Processor appendDelimiter = new Processor();
|
||||
|
||||
-3
@@ -27,7 +27,6 @@ import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -363,7 +362,6 @@ class FirehoseProcessingConfigurationTest {
|
||||
|
||||
S3Destination stored = service.describeDeliveryStream("stream").s3Destination();
|
||||
assertEquals(false, stored.getProcessingConfiguration().getEnabled());
|
||||
assertFalse(stored.isProcessingEnabled());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -399,7 +397,6 @@ class FirehoseProcessingConfigurationTest {
|
||||
stored.applyDefaults();
|
||||
|
||||
assertEquals(false, processing.getEnabled());
|
||||
assertFalse(stored.isProcessingEnabled());
|
||||
}
|
||||
|
||||
private static String parameterValue(List<ProcessorParameter> parameters, String name) {
|
||||
|
||||
+167
-1
@@ -2,9 +2,11 @@ package io.github.hectorvent.floci.services.firehose;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription;
|
||||
import io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.S3Destination;
|
||||
import io.github.hectorvent.floci.services.firehose.model.Record;
|
||||
import io.github.hectorvent.floci.services.firehose.model.RedshiftDestination;
|
||||
@@ -30,6 +32,7 @@ import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
@@ -39,6 +42,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -53,6 +57,7 @@ class FirehoseRedshiftServiceTest {
|
||||
private FirehoseService service;
|
||||
private S3Service s3Service;
|
||||
private FirehoseRedshiftDelivery delivery;
|
||||
private FirehoseLambdaTransformer transformer;
|
||||
private MutableClock clock;
|
||||
private StorageFactory storageFactory;
|
||||
private RegionResolver regionResolver;
|
||||
@@ -68,6 +73,7 @@ class FirehoseRedshiftServiceTest {
|
||||
k -> AccountAwareStorageBackend.inMemory("000000000000")));
|
||||
s3Service = Mockito.mock(S3Service.class);
|
||||
delivery = Mockito.mock(FirehoseRedshiftDelivery.class);
|
||||
transformer = Mockito.mock(FirehoseLambdaTransformer.class);
|
||||
clock = new MutableClock();
|
||||
|
||||
EmulatorConfig.FirehoseServiceConfig firehoseCfg = mock(EmulatorConfig.FirehoseServiceConfig.class);
|
||||
@@ -82,7 +88,167 @@ class FirehoseRedshiftServiceTest {
|
||||
|
||||
service = new FirehoseService(storageFactory, s3Service, new KinesisService(storageFactory, regionResolver),
|
||||
regionResolver, clock, config, mock(FirehoseParquetConverter.class),
|
||||
mock(FirehoseLambdaTransformer.class), delivery, Runnable::run);
|
||||
transformer, delivery, Runnable::run);
|
||||
}
|
||||
|
||||
private static final String LAMBDA_PROCESSING = ", \"ProcessingConfiguration\": {\"Enabled\": true, "
|
||||
+ "\"Processors\": [{\"Type\": \"Lambda\", \"Parameters\": [{\"ParameterName\": \"LambdaArn\", "
|
||||
+ "\"ParameterValue\": \"arn:aws:lambda:us-east-1:000000000000:function:xform\"}]}]}";
|
||||
|
||||
@Test
|
||||
void recordsAreTransformedByTheRedshiftDestinationsLambdaBeforeStaging() throws Exception {
|
||||
createStream(LAMBDA_PROCESSING);
|
||||
when(transformer.transform(any(), eq("stage"), any(), any())).thenReturn(new FirehoseLambdaTransformer.Outcome(
|
||||
List.of("{\"id\":2}".getBytes(StandardCharsets.UTF_8)), 0, 0, null));
|
||||
|
||||
service.flush(STREAM);
|
||||
|
||||
ArgumentCaptor<byte[]> body = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(s3Service).putObject(eq("stage"), anyString(), body.capture(), anyString(), anyMap(), any());
|
||||
assertEquals("{\"id\":2}\n", new String(body.getValue(), StandardCharsets.UTF_8));
|
||||
verify(delivery, times(1)).copy(eq("000000000000"), eq("us-east-1"), any(), eq("stage"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftLambdaProcessorIsDescribedWithItsDefaultParameters() throws Exception {
|
||||
createStream(LAMBDA_PROCESSING);
|
||||
|
||||
List<DeliveryStreamDescription.ProcessorParameter> parameters = service.describeDeliveryStream(STREAM)
|
||||
.getDestinations().get(0).getRedshiftDestinationDescription().getProcessingConfiguration()
|
||||
.getProcessors().get(0).getParameters();
|
||||
|
||||
Map<String, String> byName = new HashMap<>();
|
||||
parameters.forEach(parameter -> byName.put(parameter.getParameterName(), parameter.getParameterValue()));
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:xform", byName.get("LambdaArn"));
|
||||
assertEquals("3", byName.get("NumberOfRetries"));
|
||||
assertEquals("arn:aws:iam::000000000000:role/firehose", byName.get("RoleArn"));
|
||||
assertEquals("1", byName.get("BufferSizeInMBs"));
|
||||
assertEquals("60", byName.get("BufferIntervalInSeconds"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDisabledProcessingConfigurationDoesNotTransform() throws Exception {
|
||||
createStream(LAMBDA_PROCESSING.replace("\"Enabled\": true", "\"Enabled\": false"));
|
||||
|
||||
service.flush(STREAM);
|
||||
|
||||
verify(transformer, never()).transform(any(), anyString(), any(), any());
|
||||
verifyStagedTimes(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenTheLambdaDropsEveryRecordNothingIsStagedOrLoaded() throws Exception {
|
||||
createStream(LAMBDA_PROCESSING);
|
||||
when(transformer.transform(any(), eq("stage"), any(), any())).thenReturn(
|
||||
new FirehoseLambdaTransformer.Outcome(List.of(), 1, 0, null));
|
||||
|
||||
service.flush(STREAM);
|
||||
|
||||
verifyStagedTimes(0);
|
||||
verify(delivery, never()).copy(anyString(), anyString(), any(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void backupEnabledWithLambdaTransformKeepsTheUntransformedSourceRecords() throws Exception {
|
||||
createStream(BACKUP_ENABLED + LAMBDA_PROCESSING);
|
||||
when(transformer.transform(any(), eq("stage"), any(), any())).thenReturn(new FirehoseLambdaTransformer.Outcome(
|
||||
List.of("{\"id\":2}".getBytes(StandardCharsets.UTF_8)), 0, 0, null));
|
||||
|
||||
service.flush(STREAM);
|
||||
|
||||
verifyStagedTimes(1);
|
||||
ArgumentCaptor<byte[]> staged = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(s3Service).putObject(eq("stage"), anyString(), staged.capture(), anyString(), anyMap(), any());
|
||||
assertEquals("{\"id\":2}\n", new String(staged.getValue(), StandardCharsets.UTF_8));
|
||||
|
||||
ArgumentCaptor<byte[]> backup = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(s3Service).putObject(eq("bk"), anyString(), backup.capture(), anyString(), anyMap(), any());
|
||||
assertEquals("{\"id\":1}\n", new String(backup.getValue(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void backupEnabledWhenLambdaDropsEveryRecordStillWritesTheSourceRecordsToBackup() throws Exception {
|
||||
createStream(BACKUP_ENABLED + LAMBDA_PROCESSING);
|
||||
when(transformer.transform(any(), eq("stage"), any(), any())).thenReturn(
|
||||
new FirehoseLambdaTransformer.Outcome(List.of(), 1, 0, null));
|
||||
|
||||
service.flush(STREAM);
|
||||
|
||||
verifyStagedTimes(0);
|
||||
verify(delivery, never()).copy(anyString(), anyString(), any(), anyString(), anyString());
|
||||
|
||||
ArgumentCaptor<byte[]> backup = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(s3Service).putObject(eq("bk"), anyString(), backup.capture(), anyString(), anyMap(), any());
|
||||
assertEquals("{\"id\":1}\n", new String(backup.getValue(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void backupWriteFailureWhenLambdaDropsEveryRecordLeavesTheBatchBufferedForRetry() throws Exception {
|
||||
createStream(BACKUP_ENABLED + LAMBDA_PROCESSING);
|
||||
when(transformer.transform(any(), eq("stage"), any(), any())).thenReturn(
|
||||
new FirehoseLambdaTransformer.Outcome(List.of(), 1, 0, null));
|
||||
doThrow(new IllegalStateException("s3 unavailable")).when(s3Service)
|
||||
.putObject(eq("bk"), anyString(), any(byte[].class), anyString(), anyMap(), any());
|
||||
|
||||
service.flush(STREAM);
|
||||
|
||||
verifyStagedTimes(0);
|
||||
verify(delivery, never()).copy(anyString(), anyString(), any(), anyString(), anyString());
|
||||
|
||||
// The failed backup kept the batch buffered: a second flush retries the backup write
|
||||
reset(s3Service);
|
||||
service.flush(STREAM);
|
||||
|
||||
ArgumentCaptor<byte[]> backup = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(s3Service).putObject(eq("bk"), anyString(), backup.capture(), anyString(), anyMap(), any());
|
||||
assertEquals("{\"id\":1}\n", new String(backup.getValue(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftProcessingConfigurationWithTwoLambdaProcessorsIsRejected() throws Exception {
|
||||
String lambda = "{\"Type\": \"Lambda\", \"Parameters\": [{\"ParameterName\": \"LambdaArn\", "
|
||||
+ "\"ParameterValue\": \"arn:aws:lambda:us-east-1:000000000000:function:xform\"}]}";
|
||||
String twoProcessors = ", \"ProcessingConfiguration\": {\"Enabled\": true, \"Processors\": ["
|
||||
+ lambda + "," + lambda + "]}";
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> createStream(twoProcessors));
|
||||
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftLambdaProcessorWithoutALambdaArnIsRejected() throws Exception {
|
||||
String withoutArn = ", \"ProcessingConfiguration\": {\"Enabled\": true, \"Processors\": ["
|
||||
+ "{\"Type\": \"Lambda\", \"Parameters\": []}]}";
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> createStream(withoutArn));
|
||||
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updatingARedshiftDestinationValidatesAndAppliesItsProcessingConfiguration() throws Exception {
|
||||
createStream("");
|
||||
String destinationId = service.describeDeliveryStream(STREAM).getDestinations().get(0).getDestinationId();
|
||||
RedshiftDestination invalid = mapper.readValue("{" + LAMBDA_PROCESSING.substring(2)
|
||||
.replace("\"Parameters\": [{\"ParameterName\": \"LambdaArn\", \"ParameterValue\": "
|
||||
+ "\"arn:aws:lambda:us-east-1:000000000000:function:xform\"}]", "\"Parameters\": []") + "}",
|
||||
RedshiftDestination.class);
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.updateRedshiftDestination(STREAM, "1", destinationId, invalid));
|
||||
|
||||
RedshiftDestination valid = mapper.readValue("{" + LAMBDA_PROCESSING.substring(2) + "}",
|
||||
RedshiftDestination.class);
|
||||
service.updateRedshiftDestination(STREAM, "1", destinationId, valid);
|
||||
when(transformer.transform(any(), eq("stage"), any(), any())).thenReturn(new FirehoseLambdaTransformer.Outcome(
|
||||
List.of("{\"id\":3}".getBytes(StandardCharsets.UTF_8)), 0, 0, null));
|
||||
|
||||
service.flush(STREAM);
|
||||
|
||||
verify(transformer, times(1)).transform(any(), eq("stage"), any(), any());
|
||||
ArgumentCaptor<byte[]> body = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(s3Service).putObject(eq("stage"), anyString(), body.capture(), anyString(), anyMap(), any());
|
||||
assertEquals("{\"id\":3}\n", new String(body.getValue(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private RedshiftDestination destination(String extraMembers) throws Exception {
|
||||
|
||||
+31
@@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
class RedshiftDestinationModelTest {
|
||||
|
||||
@@ -81,6 +82,36 @@ class RedshiftDestinationModelTest {
|
||||
assertEquals("stage", reloaded.s3Destination().bucketName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftStreamsProcessingConfigurationIsTheDestinationsOwn() throws Exception {
|
||||
RedshiftDestination dest = mapper.readValue("""
|
||||
{"S3Configuration": {"RoleARN": "arn:aws:iam::000000000000:role/r", "BucketARN": "arn:aws:s3:::stage"},
|
||||
"ProcessingConfiguration": {"Enabled": true, "Processors": [{"Type": "Lambda"}]}}""",
|
||||
RedshiftDestination.class);
|
||||
|
||||
DeliveryStreamDescription stream = new DeliveryStreamDescription("s", "arn", null, dest, null);
|
||||
|
||||
assertSame(dest.getProcessingConfiguration(), stream.processingConfiguration());
|
||||
assertNull(stream.s3Destination().getProcessingConfiguration());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftStreamWithoutProcessingHasNoProcessingConfiguration() throws Exception {
|
||||
RedshiftDestination dest = mapper.readValue(CONFIG, RedshiftDestination.class);
|
||||
|
||||
assertNull(new DeliveryStreamDescription("s", "arn", null, dest, null).processingConfiguration());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anS3StreamsProcessingConfigurationIsTheS3Destinations() {
|
||||
DeliveryStreamDescription.S3Destination s3 = new DeliveryStreamDescription.S3Destination();
|
||||
DeliveryStreamDescription.ProcessingConfiguration processing =
|
||||
new DeliveryStreamDescription.ProcessingConfiguration();
|
||||
s3.setProcessingConfiguration(processing);
|
||||
|
||||
assertSame(processing, new DeliveryStreamDescription("s", "arn", s3).processingConfiguration());
|
||||
}
|
||||
|
||||
@Test
|
||||
void redshiftStreamWithoutBucketHasNoBucketName() throws Exception {
|
||||
RedshiftDestination dest = mapper.readValue(
|
||||
|
||||
Reference in new issue
Block a user