mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(pipes): share SQS records with Lambda pollers (#8604)
EventBridge Pipes and Lambda event source mappings now build their SQS records through one SqsEventRecordBuilder, the record Lambda already sent. Pipes gains the FIFO attributes SequenceNumber, MessageGroupId and MessageDeduplicationId, md5OfMessageAttributes when present, and a default dataType for message attributes. A message without a first-receive timestamp takes the Pipes poller's clock instead of wall time, without writing it back to the message. Lambda's record is unchanged. Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
bb6b308fdb
commit
c12db764e6
4 files changed
+157
-77
No files matched your search
@@ -13,6 +13,7 @@ import io.floci.aws.services.lambda.model.InvocationType;
|
||||
import io.floci.aws.services.lambda.model.InvokeResult;
|
||||
import io.floci.aws.services.lambda.model.LambdaFunction;
|
||||
import io.floci.aws.services.pipes.PipesFilterMatcher;
|
||||
import io.floci.aws.services.sqs.SqsEventRecordBuilder;
|
||||
import io.floci.aws.services.sqs.SqsService;
|
||||
import io.floci.aws.services.sqs.model.Message;
|
||||
import io.vertx.core.Vertx;
|
||||
@@ -22,7 +23,6 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
@@ -435,51 +435,7 @@ public class SqsEventSourcePoller implements Resettable {
|
||||
* {@code body}; the matcher auto-reparses a JSON body).
|
||||
*/
|
||||
private ObjectNode buildSqsRecordNode(Message msg, EventSourceMapping esm) {
|
||||
ObjectNode record = objectMapper.createObjectNode();
|
||||
record.put("messageId", msg.getMessageId());
|
||||
record.put("receiptHandle", msg.getReceiptHandle());
|
||||
record.put("body", msg.getBody());
|
||||
ObjectNode attrs = record.putObject("attributes");
|
||||
attrs.put("ApproximateReceiveCount", String.valueOf(msg.getReceiveCount()));
|
||||
attrs.put("SentTimestamp", String.valueOf(msg.getSentTimestamp().toEpochMilli()));
|
||||
attrs.put("SenderId", AwsArnUtils.accountOrDefault(esm.getEventSourceArn(), "000000000000"));
|
||||
attrs.put("ApproximateFirstReceiveTimestamp",
|
||||
String.valueOf(msg.getFirstReceiveTimestamp() != null
|
||||
? msg.getFirstReceiveTimestamp().toEpochMilli()
|
||||
: clockMs.getAsLong()));
|
||||
if (msg.getSequenceNumber() > 0) {
|
||||
attrs.put("SequenceNumber", String.valueOf(msg.getSequenceNumber()));
|
||||
}
|
||||
if (msg.getMessageGroupId() != null) {
|
||||
attrs.put("MessageGroupId", msg.getMessageGroupId());
|
||||
}
|
||||
if (msg.getMessageDeduplicationId() != null) {
|
||||
attrs.put("MessageDeduplicationId", msg.getMessageDeduplicationId());
|
||||
}
|
||||
// Populate messageAttributes from the message model
|
||||
ObjectNode msgAttrs = record.putObject("messageAttributes");
|
||||
if (msg.getMessageAttributes() != null) {
|
||||
msg.getMessageAttributes().forEach((name, val) -> {
|
||||
ObjectNode attrNode = msgAttrs.putObject(name);
|
||||
attrNode.put("dataType", val.getDataType() != null ? val.getDataType() : "String");
|
||||
if (val.getBinaryValue() != null) {
|
||||
attrNode.put("binaryValue",
|
||||
Base64.getEncoder().encodeToString(val.getBinaryValue()));
|
||||
} else if (val.getStringValue() != null) {
|
||||
attrNode.put("stringValue", val.getStringValue());
|
||||
}
|
||||
attrNode.putArray("stringListValues");
|
||||
attrNode.putArray("binaryListValues");
|
||||
});
|
||||
}
|
||||
record.put("md5OfBody", msg.getMd5OfBody() != null ? msg.getMd5OfBody() : "");
|
||||
if (msg.getMd5OfMessageAttributes() != null) {
|
||||
record.put("md5OfMessageAttributes", msg.getMd5OfMessageAttributes());
|
||||
}
|
||||
record.put("eventSource", "aws:sqs");
|
||||
record.put("eventSourceARN", esm.getEventSourceArn());
|
||||
record.put("awsRegion", esm.getRegion());
|
||||
return record;
|
||||
return SqsEventRecordBuilder.build(objectMapper, msg, esm.getEventSourceArn(), esm.getRegion(), clockMs);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,9 +15,9 @@ import io.floci.aws.services.kinesis.KinesisService;
|
||||
import io.floci.aws.services.kinesis.model.KinesisRecord;
|
||||
import io.floci.aws.services.pipes.model.DesiredState;
|
||||
import io.floci.aws.services.pipes.model.Pipe;
|
||||
import io.floci.aws.services.sqs.SqsEventRecordBuilder;
|
||||
import io.floci.aws.services.sqs.SqsService;
|
||||
import io.floci.aws.services.sqs.model.Message;
|
||||
import io.floci.aws.services.sqs.model.MessageAttributeValue;
|
||||
import io.vertx.core.Vertx;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -35,6 +35,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
@ApplicationScoped
|
||||
public class PipesPoller implements Resettable {
|
||||
@@ -58,6 +59,7 @@ public class PipesPoller implements Resettable {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final String baseUrl;
|
||||
private final String defaultRegion;
|
||||
private volatile LongSupplier clockMs = System::currentTimeMillis;
|
||||
private final ConcurrentHashMap<String, Long> timerIds = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Boolean> activePolls = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, String> kinesisIterators = new ConcurrentHashMap<>();
|
||||
@@ -91,6 +93,10 @@ public class PipesPoller implements Resettable {
|
||||
this.defaultRegion = config.defaultRegion();
|
||||
}
|
||||
|
||||
void setClockForTest(LongSupplier clockMs) {
|
||||
this.clockMs = clockMs;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void shutdown() {
|
||||
pollExecutor.shutdownNow();
|
||||
@@ -750,36 +756,8 @@ public class PipesPoller implements Resettable {
|
||||
private List<ObjectNode> buildSqsRecordNodes(List<Message> messages, Pipe pipe) {
|
||||
List<ObjectNode> nodes = new ArrayList<>();
|
||||
for (Message msg : messages) {
|
||||
ObjectNode record = objectMapper.createObjectNode();
|
||||
record.put("messageId", msg.getMessageId());
|
||||
record.put("receiptHandle", msg.getReceiptHandle());
|
||||
record.put("body", msg.getBody());
|
||||
ObjectNode attrs = record.putObject("attributes");
|
||||
attrs.put("ApproximateReceiveCount", String.valueOf(msg.getReceiveCount()));
|
||||
attrs.put("SentTimestamp", String.valueOf(msg.getSentTimestamp().toEpochMilli()));
|
||||
attrs.put("SenderId", AwsArnUtils.accountOrDefault(pipe.getSource(), "000000000000"));
|
||||
attrs.put("ApproximateFirstReceiveTimestamp",
|
||||
String.valueOf(msg.getFirstReceiveTimestamp() != null
|
||||
? msg.getFirstReceiveTimestamp().toEpochMilli()
|
||||
: System.currentTimeMillis()));
|
||||
ObjectNode msgAttrs = record.putObject("messageAttributes");
|
||||
for (Map.Entry<String, MessageAttributeValue> entry : msg.getMessageAttributes().entrySet()) {
|
||||
ObjectNode attrNode = msgAttrs.putObject(entry.getKey());
|
||||
MessageAttributeValue val = entry.getValue();
|
||||
if (val.getBinaryValue() != null) {
|
||||
attrNode.put("binaryValue", Base64.getEncoder().encodeToString(val.getBinaryValue()));
|
||||
} else if (val.getStringValue() != null) {
|
||||
attrNode.put("stringValue", val.getStringValue());
|
||||
}
|
||||
attrNode.putArray("stringListValues");
|
||||
attrNode.putArray("binaryListValues");
|
||||
attrNode.put("dataType", val.getDataType());
|
||||
}
|
||||
record.put("md5OfBody", msg.getMd5OfBody() != null ? msg.getMd5OfBody() : "");
|
||||
record.put("eventSource", "aws:sqs");
|
||||
record.put("eventSourceARN", pipe.getSource());
|
||||
record.put("awsRegion", extractRegionFromArn(pipe.getSource()));
|
||||
nodes.add(record);
|
||||
nodes.add(SqsEventRecordBuilder.build(objectMapper, msg, pipe.getSource(),
|
||||
extractRegionFromArn(pipe.getSource()), clockMs));
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package io.floci.aws.services.sqs;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.services.sqs.model.Message;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
/** The SQS record shared by Lambda event source mappings and EventBridge Pipes. */
|
||||
public final class SqsEventRecordBuilder {
|
||||
|
||||
private SqsEventRecordBuilder() {
|
||||
}
|
||||
|
||||
public static ObjectNode build(ObjectMapper mapper, Message msg, String queueArn,
|
||||
String region, LongSupplier clockMs) {
|
||||
ObjectNode record = mapper.createObjectNode();
|
||||
record.put("messageId", msg.getMessageId());
|
||||
record.put("receiptHandle", msg.getReceiptHandle());
|
||||
record.put("body", msg.getBody());
|
||||
ObjectNode attrs = record.putObject("attributes");
|
||||
attrs.put("ApproximateReceiveCount", String.valueOf(msg.getReceiveCount()));
|
||||
attrs.put("SentTimestamp", String.valueOf(msg.getSentTimestamp().toEpochMilli()));
|
||||
attrs.put("SenderId", AwsArnUtils.accountOrDefault(queueArn, "000000000000"));
|
||||
attrs.put("ApproximateFirstReceiveTimestamp",
|
||||
String.valueOf(msg.getFirstReceiveTimestamp() != null
|
||||
? msg.getFirstReceiveTimestamp().toEpochMilli()
|
||||
: clockMs.getAsLong()));
|
||||
if (msg.getSequenceNumber() > 0) {
|
||||
attrs.put("SequenceNumber", String.valueOf(msg.getSequenceNumber()));
|
||||
}
|
||||
if (msg.getMessageGroupId() != null) {
|
||||
attrs.put("MessageGroupId", msg.getMessageGroupId());
|
||||
}
|
||||
if (msg.getMessageDeduplicationId() != null) {
|
||||
attrs.put("MessageDeduplicationId", msg.getMessageDeduplicationId());
|
||||
}
|
||||
// Populate messageAttributes from the message model
|
||||
ObjectNode msgAttrs = record.putObject("messageAttributes");
|
||||
if (msg.getMessageAttributes() != null) {
|
||||
msg.getMessageAttributes().forEach((name, val) -> {
|
||||
ObjectNode attrNode = msgAttrs.putObject(name);
|
||||
attrNode.put("dataType", val.getDataType() != null ? val.getDataType() : "String");
|
||||
if (val.getBinaryValue() != null) {
|
||||
attrNode.put("binaryValue",
|
||||
Base64.getEncoder().encodeToString(val.getBinaryValue()));
|
||||
} else if (val.getStringValue() != null) {
|
||||
attrNode.put("stringValue", val.getStringValue());
|
||||
}
|
||||
attrNode.putArray("stringListValues");
|
||||
attrNode.putArray("binaryListValues");
|
||||
});
|
||||
}
|
||||
record.put("md5OfBody", msg.getMd5OfBody() != null ? msg.getMd5OfBody() : "");
|
||||
if (msg.getMd5OfMessageAttributes() != null) {
|
||||
record.put("md5OfMessageAttributes", msg.getMd5OfMessageAttributes());
|
||||
}
|
||||
record.put("eventSource", "aws:sqs");
|
||||
record.put("eventSourceARN", queueArn);
|
||||
record.put("awsRegion", region);
|
||||
return record;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user