-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Lambda Synchronous processor support #4700
Merged
dlvenable
merged 7 commits into
opensearch-project:main
from
srikanthjg:lambda-processor
Sep 24, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
30401dd
Add Lambda Processor Synchronous Mode support
srikanthjg edadb63
Make LambdaClientFactory common to sink and processor
srikanthjg bca9a57
Address Comments
srikanthjg 036de73
Address codec comment
srikanthjg 4391b2d
Address configuration syntax comment
srikanthjg 8b16eba
Address Lambda Payload invocation type
srikanthjg 21ee56e
Address comments on retries, logging, refactor
srikanthjg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...st/java/org/opensearch/dataprepper/plugins/lambda/processor/LambdaProcessorServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package org.opensearch.dataprepper.plugins.lambda.processor; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; | ||
import io.micrometer.core.instrument.Counter; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.Mock; | ||
import static org.mockito.Mockito.when; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier; | ||
import org.opensearch.dataprepper.expression.ExpressionEvaluator; | ||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.configuration.PluginSetting; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
import org.opensearch.dataprepper.model.log.JacksonLog; | ||
import org.opensearch.dataprepper.model.plugin.PluginFactory; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import org.opensearch.dataprepper.model.types.ByteCount; | ||
import org.opensearch.dataprepper.plugins.lambda.common.accumlator.BufferFactory; | ||
import org.opensearch.dataprepper.plugins.lambda.common.accumlator.InMemoryBufferFactory; | ||
import org.opensearch.dataprepper.plugins.lambda.common.config.AwsAuthenticationOptions; | ||
import org.opensearch.dataprepper.plugins.lambda.common.config.BatchOptions; | ||
import org.opensearch.dataprepper.plugins.lambda.common.config.ThresholdOptions; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.lambda.LambdaClient; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
|
||
public class LambdaProcessorServiceIT { | ||
|
||
private LambdaClient lambdaClient; | ||
private String functionName; | ||
private String lambdaRegion; | ||
private String role; | ||
private BufferFactory bufferFactory; | ||
@Mock | ||
private LambdaProcessorConfig lambdaProcessorConfig; | ||
@Mock | ||
private BatchOptions batchOptions; | ||
@Mock | ||
private ThresholdOptions thresholdOptions; | ||
@Mock | ||
private AwsAuthenticationOptions awsAuthenticationOptions; | ||
@Mock | ||
private AwsCredentialsSupplier awsCredentialsSupplier; | ||
@Mock | ||
private PluginMetrics pluginMetrics; | ||
@Mock | ||
private PluginFactory pluginFactory; | ||
@Mock | ||
private PluginSetting pluginSetting; | ||
@Mock | ||
private Counter numberOfRecordsSuccessCounter; | ||
@Mock | ||
private Counter numberOfRecordsFailedCounter; | ||
@Mock | ||
private ExpressionEvaluator expressionEvaluator; | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS)); | ||
|
||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.openMocks(this); | ||
lambdaRegion = System.getProperty("tests.lambda.processor.region"); | ||
functionName = System.getProperty("tests.lambda.processor.functionName"); | ||
role = System.getProperty("tests.lambda.processor.sts_role_arn"); | ||
|
||
final Region region = Region.of(lambdaRegion); | ||
|
||
lambdaClient = LambdaClient.builder() | ||
.region(Region.of(lambdaRegion)) | ||
.build(); | ||
|
||
bufferFactory = new InMemoryBufferFactory(); | ||
|
||
when(pluginMetrics.counter(LambdaProcessor.NUMBER_OF_RECORDS_FLUSHED_TO_LAMBDA_SUCCESS)). | ||
thenReturn(numberOfRecordsSuccessCounter); | ||
when(pluginMetrics.counter(LambdaProcessor.NUMBER_OF_RECORDS_FLUSHED_TO_LAMBDA_FAILED)). | ||
thenReturn(numberOfRecordsFailedCounter); | ||
} | ||
|
||
|
||
private static Record<Event> createRecord() { | ||
final JacksonEvent event = JacksonLog.builder().withData("[{\"name\":\"test\"}]").build(); | ||
return new Record<>(event); | ||
} | ||
|
||
public LambdaProcessor createObjectUnderTest(final String config) throws JsonProcessingException { | ||
|
||
final LambdaProcessorConfig lambdaProcessorConfig = objectMapper.readValue(config, LambdaProcessorConfig.class); | ||
return new LambdaProcessor(pluginMetrics,lambdaProcessorConfig,awsCredentialsSupplier,expressionEvaluator); | ||
} | ||
|
||
public LambdaProcessor createObjectUnderTest(LambdaProcessorConfig lambdaSinkConfig) throws JsonProcessingException { | ||
return new LambdaProcessor(pluginMetrics,lambdaSinkConfig,awsCredentialsSupplier,expressionEvaluator); | ||
} | ||
|
||
|
||
private static Collection<Record<Event>> generateRecords(int numberOfRecords) { | ||
List<Record<Event>> recordList = new ArrayList<>(); | ||
|
||
for (int rows = 1; rows <= numberOfRecords; rows++) { | ||
HashMap<String, String> eventData = new HashMap<>(); | ||
eventData.put("name", "Person" + rows); | ||
eventData.put("age", Integer.toString(rows)); | ||
|
||
Record<Event> eventRecord = new Record<>(JacksonEvent.builder().withData(eventData).withEventType("event").build()); | ||
recordList.add(eventRecord); | ||
} | ||
return recordList; | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {1,3}) | ||
void verify_records_to_lambda_success(final int recordCount) throws Exception { | ||
|
||
when(lambdaProcessorConfig.getFunctionName()).thenReturn(functionName); | ||
when(lambdaProcessorConfig.getMaxConnectionRetries()).thenReturn(3); | ||
when(lambdaProcessorConfig.getInvocationType()).thenReturn("RequestResponse"); | ||
|
||
LambdaProcessor objectUnderTest = createObjectUnderTest(lambdaProcessorConfig); | ||
|
||
Collection<Record<Event>> recordsData = generateRecords(recordCount); | ||
List<Record<Event>> recordsResult = (List<Record<Event>>) objectUnderTest.doExecute(recordsData); | ||
Thread.sleep(Duration.ofSeconds(10).toMillis()); | ||
|
||
assertEquals(recordsResult.size(),recordCount); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {1,3}) | ||
void verify_records_with_batching_to_lambda(final int recordCount) throws JsonProcessingException, InterruptedException { | ||
|
||
when(lambdaProcessorConfig.getFunctionName()).thenReturn(functionName); | ||
when(lambdaProcessorConfig.getMaxConnectionRetries()).thenReturn(3); | ||
when(lambdaProcessorConfig.getInvocationType()).thenReturn("RequestResponse"); | ||
when(thresholdOptions.getEventCount()).thenReturn(1); | ||
when(thresholdOptions.getMaximumSize()).thenReturn(ByteCount.parse("2mb")); | ||
when(thresholdOptions.getEventCollectTimeOut()).thenReturn(Duration.parse("PT10s")); | ||
when(batchOptions.getKeyName()).thenReturn("lambda_batch_key"); | ||
when(batchOptions.getThresholdOptions()).thenReturn(thresholdOptions); | ||
when(lambdaProcessorConfig.getBatchOptions()).thenReturn(batchOptions); | ||
|
||
LambdaProcessor objectUnderTest = createObjectUnderTest(lambdaProcessorConfig); | ||
Collection<Record<Event>> records = generateRecords(recordCount); | ||
Collection<Record<Event>> recordsResult = objectUnderTest.doExecute(records); | ||
Thread.sleep(Duration.ofSeconds(10).toMillis()); | ||
assertEquals(recordsResult.size(),recordCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider adding test for
InvocationType
EventThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invocation type event will be disabled for now, will be releasing event type with asynchronous support that requires additional infra changes. I have disabled it in the verification for now, ll fix the readme.