-
Notifications
You must be signed in to change notification settings - Fork 92
feat: add support for batch execution in parallel with custom Executor #1900
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.demo.batch.dynamo; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent; | ||
import com.amazonaws.services.lambda.runtime.events.StreamsEventResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder; | ||
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class DynamoDBStreamBatchHandlerParallel implements RequestHandler<DynamodbEvent, StreamsEventResponse> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBStreamBatchHandlerParallel.class); | ||
private final BatchMessageHandler<DynamodbEvent, StreamsEventResponse> handler; | ||
private final ExecutorService executor; | ||
|
||
public DynamoDBStreamBatchHandlerParallel() { | ||
handler = new BatchMessageHandlerBuilder() | ||
.withDynamoDbBatchHandler() | ||
.buildWithRawMessageHandler(this::processMessage); | ||
executor = Executors.newFixedThreadPool(2); | ||
} | ||
|
||
@Override | ||
public StreamsEventResponse handleRequest(DynamodbEvent ddbEvent, Context context) { | ||
return handler.processBatchInParallel(ddbEvent, context, executor); | ||
} | ||
|
||
private void processMessage(DynamodbEvent.DynamodbStreamRecord dynamodbStreamRecord, Context context) { | ||
Check failure on line 33 in examples/powertools-examples-batch/src/main/java/org/demo/batch/dynamo/DynamoDBStreamBatchHandlerParallel.java
|
||
LOGGER.info("Processing DynamoDB Stream Record" + dynamodbStreamRecord); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.demo.batch.kinesis; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.KinesisEvent; | ||
import com.amazonaws.services.lambda.runtime.events.StreamsEventResponse; | ||
import org.demo.batch.model.Product; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder; | ||
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class KinesisBatchHandlerParallel implements RequestHandler<KinesisEvent, StreamsEventResponse> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(KinesisBatchHandlerParallel.class); | ||
private final BatchMessageHandler<KinesisEvent, StreamsEventResponse> handler; | ||
private final ExecutorService executor; | ||
|
||
|
||
public KinesisBatchHandlerParallel() { | ||
handler = new BatchMessageHandlerBuilder() | ||
.withKinesisBatchHandler() | ||
.buildWithMessageHandler(this::processMessage, Product.class); | ||
executor = Executors.newFixedThreadPool(2); | ||
} | ||
|
||
@Override | ||
public StreamsEventResponse handleRequest(KinesisEvent kinesisEvent, Context context) { | ||
return handler.processBatchInParallel(kinesisEvent, context, executor); | ||
} | ||
|
||
private void processMessage(Product p, Context c) { | ||
Check failure on line 35 in examples/powertools-examples-batch/src/main/java/org/demo/batch/kinesis/KinesisBatchHandlerParallel.java
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove /**
* Builds a BatchMessageHandler that can be used to process batches, given
* a user-defined handler to process each item in the batch. This variant
* takes a function that consumes the deserialized body of the given message
* If deserialization fails, it will be treated as
* failure of the processing of that item in the batch.
* Note: If you don't need the Lambda context, use the variant of this function
* that does not require it.
*
* @param handler Processes the deserialized body of the message
* @return A BatchMessageHandler for processing the batch
*/
public <M> BatchMessageHandler<E, R> buildWithMessageHandler(Consumer<M> handler, Class<M> messageClass) {
return buildWithMessageHandler((f, c) -> handler.accept(f), messageClass);
} |
||
LOGGER.info("Processing product " + p); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.demo.batch.sqs; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.SQSBatchResponse; | ||
import com.amazonaws.services.lambda.runtime.events.SQSEvent; | ||
import org.demo.batch.model.Product; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder; | ||
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler; | ||
import software.amazon.lambda.powertools.logging.Logging; | ||
import software.amazon.lambda.powertools.tracing.Tracing; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class SqsBatchHandlerParallel extends AbstractSqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(SqsBatchHandlerParallel.class); | ||
private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler; | ||
private final ExecutorService executor; | ||
|
||
public SqsBatchHandlerParallel() { | ||
handler = new BatchMessageHandlerBuilder() | ||
.withSqsBatchHandler() | ||
.buildWithMessageHandler(this::processMessage, Product.class); | ||
executor = Executors.newFixedThreadPool(2); | ||
} | ||
|
||
@Logging | ||
@Tracing | ||
@Override | ||
public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) { | ||
LOGGER.info("Processing batch of {} messages", sqsEvent.getRecords().size()); | ||
return handler.processBatchInParallel(sqsEvent, context, executor); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,12 @@ | |
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.events.KinesisEvent; | ||
import com.amazonaws.services.lambda.runtime.events.StreamsEventResponse; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
|
@@ -77,7 +81,9 @@ | |
.parallelStream() // Parallel processing | ||
.map(eventRecord -> { | ||
multiThreadMDC.copyMDCToThread(Thread.currentThread().getName()); | ||
return processBatchItem(eventRecord, context); | ||
Optional<StreamsEventResponse.BatchItemFailure> failureOpt = processBatchItem(eventRecord, context); | ||
multiThreadMDC.removeThread(Thread.currentThread().getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch!! |
||
return failureOpt; | ||
}) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
|
@@ -86,6 +92,23 @@ | |
return StreamsEventResponse.builder().withBatchItemFailures(batchItemFailures).build(); | ||
} | ||
|
||
@Override | ||
public StreamsEventResponse processBatchInParallel(KinesisEvent event, Context context, Executor executor) { | ||
MultiThreadMDC multiThreadMDC = new MultiThreadMDC(); | ||
|
||
List<StreamsEventResponse.BatchItemFailure> batchItemFailures = new ArrayList<>(); | ||
List<CompletableFuture<Void>> futures = event.getRecords().stream() | ||
.map(eventRecord -> CompletableFuture.runAsync(() -> { | ||
multiThreadMDC.copyMDCToThread(Thread.currentThread().getName()); | ||
Optional<StreamsEventResponse.BatchItemFailure> failureOpt = processBatchItem(eventRecord, context); | ||
failureOpt.ifPresent(batchItemFailures::add); | ||
multiThreadMDC.removeThread(Thread.currentThread().getName()); | ||
}, executor)) | ||
.collect(Collectors.toList()); | ||
futures.forEach(CompletableFuture::join); | ||
return StreamsEventResponse.builder().withBatchItemFailures(batchItemFailures).build(); | ||
} | ||
|
||
private Optional<StreamsEventResponse.BatchItemFailure> processBatchItem(KinesisEvent.KinesisEventRecord eventRecord, Context context) { | ||
try { | ||
LOGGER.debug("Processing item {}", eventRecord.getEventID()); | ||
|
@@ -102,7 +125,7 @@ | |
this.successHandler.accept(eventRecord); | ||
} | ||
return Optional.empty(); | ||
} catch (Throwable t) { | ||
Check failure on line 128 in powertools-batch/src/main/java/software/amazon/lambda/powertools/batch/handler/KinesisStreamsBatchMessageHandler.java
|
||
String sequenceNumber = eventRecord.getEventID(); | ||
LOGGER.error("Error while processing record with eventID {}: {}, adding it to batch item failures", | ||
sequenceNumber, t.getMessage()); | ||
|
@@ -113,7 +136,7 @@ | |
// A failing failure handler is no reason to fail the batch | ||
try { | ||
this.failureHandler.accept(eventRecord, t); | ||
} catch (Throwable t2) { | ||
Check failure on line 139 in powertools-batch/src/main/java/software/amazon/lambda/powertools/batch/handler/KinesisStreamsBatchMessageHandler.java
|
||
LOGGER.warn("failureHandler threw handling failure", t2); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,11 @@ public void copyMDCToThread(String thread) { | |
mdcAwareThreads.add(thread); | ||
} | ||
} | ||
|
||
public void removeThread(String thread) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will be helpful if the same invocation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is good. I believe it was a small memory leak because we never removed the thread either. |
||
if (mdcAwareThreads.contains(thread)) { | ||
LOGGER.debug("Removing thread {}", thread); | ||
mdcAwareThreads.remove(thread); | ||
} | ||
} | ||
} |
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.
You can remove
Context c
here please. There is an overload for a message handler without context inAbstractBatchMessageHandlerBuilder.java