mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(lambda): honour ReportBatchItemFailures in SQS ESM (#208)
This commit is contained in:
1 parent
07a7a97b47
commit
55b2f292cc
5 files changed
+100
-12
No files matched your search
@@ -249,6 +249,10 @@ public class LambdaController {
|
||||
node.put("BatchSize", esm.getBatchSize());
|
||||
node.put("State", esm.getState());
|
||||
node.put("LastModified", (double) esm.getLastModified() / 1000.0);
|
||||
ArrayNode responseTypes = node.putArray("FunctionResponseTypes");
|
||||
if (esm.getFunctionResponseTypes() != null) {
|
||||
esm.getFunctionResponseTypes().forEach(responseTypes::add);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> result = objectMapper.convertValue(node, Map.class);
|
||||
return result;
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -262,6 +263,11 @@ public class LambdaService {
|
||||
int batchSize = toInt(request.get("BatchSize"), 10);
|
||||
boolean enabled = !Boolean.FALSE.equals(request.get("Enabled"));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> functionResponseTypes = request.get("FunctionResponseTypes") instanceof List
|
||||
? (List<String>) request.get("FunctionResponseTypes")
|
||||
: new ArrayList<>();
|
||||
|
||||
String queueUrl = eventSourceArn.contains(":sqs:") ? AwsArnUtils.arnToQueueUrl(eventSourceArn, config.effectiveBaseUrl()) : null;
|
||||
|
||||
EventSourceMapping esm = new EventSourceMapping();
|
||||
@@ -274,6 +280,7 @@ public class LambdaService {
|
||||
esm.setBatchSize(batchSize);
|
||||
esm.setEnabled(enabled);
|
||||
esm.setState(enabled ? "Enabled" : "Disabled");
|
||||
esm.setFunctionResponseTypes(functionResponseTypes);
|
||||
esm.setLastModified(System.currentTimeMillis());
|
||||
|
||||
esmStore.save(esm);
|
||||
|
||||
@@ -17,7 +17,9 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -148,9 +150,13 @@ public class SqsEventSourcePoller {
|
||||
fn, eventJson.getBytes(), InvocationType.RequestResponse);
|
||||
|
||||
if (result.getFunctionError() == null) {
|
||||
LOG.infov("ESM {0}: Lambda succeeded, deleting {1} message(s)",
|
||||
esm.getUuid(), messages.size());
|
||||
for (Message msg : messages) {
|
||||
Set<String> failedIds = extractBatchItemFailures(esm, result);
|
||||
List<Message> toDelete = failedIds.isEmpty()
|
||||
? messages
|
||||
: messages.stream().filter(m -> !failedIds.contains(m.getMessageId())).toList();
|
||||
LOG.infov("ESM {0}: Lambda succeeded, deleting {1} of {2} message(s) ({3} reported as failed)",
|
||||
esm.getUuid(), toDelete.size(), messages.size(), failedIds.size());
|
||||
for (Message msg : toDelete) {
|
||||
try {
|
||||
sqsService.deleteMessage(esm.getQueueUrl(),
|
||||
msg.getReceiptHandle(), esm.getRegion());
|
||||
@@ -172,6 +178,31 @@ public class SqsEventSourcePoller {
|
||||
});
|
||||
}
|
||||
|
||||
private Set<String> extractBatchItemFailures(EventSourceMapping esm, InvokeResult result) {
|
||||
if (!esm.isReportBatchItemFailures() || result.getPayload() == null || result.getPayload().length == 0) {
|
||||
return Set.of();
|
||||
}
|
||||
try {
|
||||
var root = objectMapper.readTree(result.getPayload());
|
||||
var failures = root.get("batchItemFailures");
|
||||
if (failures == null || !failures.isArray()) {
|
||||
return Set.of();
|
||||
}
|
||||
Set<String> failedIds = new HashSet<>();
|
||||
for (var item : failures) {
|
||||
var id = item.get("itemIdentifier");
|
||||
if (id != null && !id.isNull()) {
|
||||
failedIds.add(id.asText());
|
||||
}
|
||||
}
|
||||
return failedIds;
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("ESM {0}: failed to parse batchItemFailures from Lambda response: {1}",
|
||||
esm.getUuid(), e.getMessage());
|
||||
return Set.of();
|
||||
}
|
||||
}
|
||||
|
||||
private String buildSqsEvent(List<Message> messages, EventSourceMapping esm) {
|
||||
try {
|
||||
var records = objectMapper.createArrayNode();
|
||||
|
||||
+12
@@ -3,7 +3,9 @@ package io.github.hectorvent.floci.services.lambda.model;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@@ -20,6 +22,7 @@ public class EventSourceMapping {
|
||||
private int batchSize = 10;
|
||||
private String state = "Enabled";
|
||||
private long lastModified;
|
||||
private List<String> functionResponseTypes = new ArrayList<>();
|
||||
private Map<String, String> shardSequenceNumbers = new HashMap<>();
|
||||
|
||||
public EventSourceMapping() {
|
||||
@@ -55,6 +58,15 @@ public class EventSourceMapping {
|
||||
public long getLastModified() { return lastModified; }
|
||||
public void setLastModified(long lastModified) { this.lastModified = lastModified; }
|
||||
|
||||
public List<String> getFunctionResponseTypes() { return functionResponseTypes; }
|
||||
public void setFunctionResponseTypes(List<String> functionResponseTypes) {
|
||||
this.functionResponseTypes = functionResponseTypes != null ? functionResponseTypes : new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean isReportBatchItemFailures() {
|
||||
return functionResponseTypes != null && functionResponseTypes.contains("ReportBatchItemFailures");
|
||||
}
|
||||
|
||||
public Map<String, String> getShardSequenceNumbers() { return shardSequenceNumbers; }
|
||||
public void setShardSequenceNumbers(Map<String, String> shardSequenceNumbers) {
|
||||
this.shardSequenceNumbers = shardSequenceNumbers != null ? shardSequenceNumbers : new java.util.HashMap<>();
|
||||
|
||||
Reference in new issue
Block a user