From 952dec060f1e0e63ef7c41bced55b69df5ee1c6b Mon Sep 17 00:00:00 2001 From: Konda Balaji Rao Date: Fri, 26 Jun 2026 05:41:51 +0530 Subject: [PATCH] fix(lambda): use persisted firstReceiveTimestamp in SQS ESM (#1548) * fix: return XML error for missing action and add volume action fallbacks * fix(eventbridge): sync EventBridge tags to ResourceGroupsTaggingService EventBridge stored tags on rules, buses, and archives in its own stores but never propagated them to the central ResourceGroupsTaggingService. This caused GetResources with --resource-type-filters events:rule to return an empty list. Inject ResourceGroupsTaggingService into EventBridgeService (following the existing GlueService pattern) and sync tags during putRule, tagResource, untagResource, and deleteRule operations. Closes #1343 * fix(lambda): use persisted firstReceiveTimestamp in SQS ESM and Pipes event payloads Both SqsEventSourcePoller.buildSqsEvent() and PipesPoller.buildSqsRecordNodes() were using System.currentTimeMillis() for ApproximateFirstReceiveTimestamp, causing the value to change on every poll cycle. AWS SQS defines this as the epoch time when the message was first received from the queue, and it must remain constant across retries. The SQS service layer already tracks this correctly via Message.firstReceiveTimestamp (set once in GuardedMessageQueue.tryClaim()), but both pollers bypassed the stored value. Changes: - SqsEventSourcePoller: read msg.getFirstReceiveTimestamp() with null fallback - PipesPoller: same fix for the identical code path - SqsEventSourcePollerTest: set firstReceiveTimestamp on test messages and assert the output matches the stored value; add a regression test proving the timestamp remains stable across simulated retries Closes #1497 --------- Signed-off-by: Hector Ventura Co-authored-by: Hector Ventura --- .../services/lambda/SqsEventSourcePoller.java | 5 ++- .../floci/services/pipes/PipesPoller.java | 5 ++- .../lambda/SqsEventSourcePollerTest.java | 32 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java index 0704a64cca..637113e401 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java @@ -284,7 +284,10 @@ public class SqsEventSourcePoller { 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(System.currentTimeMillis())); + attrs.put("ApproximateFirstReceiveTimestamp", + String.valueOf(msg.getFirstReceiveTimestamp() != null + ? msg.getFirstReceiveTimestamp().toEpochMilli() + : System.currentTimeMillis())); if (msg.getSequenceNumber() > 0) { attrs.put("SequenceNumber", String.valueOf(msg.getSequenceNumber())); } diff --git a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java index 112b441a0e..225c22983a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java +++ b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java @@ -410,7 +410,10 @@ public class PipesPoller { 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(System.currentTimeMillis())); + attrs.put("ApproximateFirstReceiveTimestamp", + String.valueOf(msg.getFirstReceiveTimestamp() != null + ? msg.getFirstReceiveTimestamp().toEpochMilli() + : System.currentTimeMillis())); ObjectNode msgAttrs = record.putObject("messageAttributes"); for (Map.Entry entry : msg.getMessageAttributes().entrySet()) { ObjectNode attrNode = msgAttrs.putObject(entry.getKey()); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java index e06ac50df6..1f3c24949a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java @@ -63,9 +63,11 @@ class SqsEventSourcePollerTest { @Test void buildSqsEventIncludesAllRequiredAttributes() throws Exception { + Instant firstReceived = Instant.parse("2026-01-15T10:31:00Z"); Message msg = new Message(); msg.setBody("{\"key\":\"value\"}"); msg.setSentTimestamp(Instant.parse("2026-01-15T10:30:00Z")); + msg.setFirstReceiveTimestamp(firstReceived); EventSourceMapping esm = new EventSourceMapping(); esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:my-queue"); @@ -84,6 +86,8 @@ class SqsEventSourcePollerTest { assertEquals("123456789012", attrs.get("SenderId").asText()); assertEquals(String.valueOf(Instant.parse("2026-01-15T10:30:00Z").toEpochMilli()), attrs.get("SentTimestamp").asText()); + assertEquals(String.valueOf(firstReceived.toEpochMilli()), + attrs.get("ApproximateFirstReceiveTimestamp").asText()); assertEquals("aws:sqs", record.get("eventSource").asText()); assertEquals("arn:aws:sqs:us-east-1:123456789012:my-queue", record.get("eventSourceARN").asText()); assertEquals("us-east-1", record.get("awsRegion").asText()); @@ -280,6 +284,7 @@ class SqsEventSourcePollerTest { msg.setReceiptHandle("rh-" + id); msg.setBody("body-" + id); msg.setSentTimestamp(Instant.now()); + msg.setFirstReceiveTimestamp(Instant.now()); return msg; } @@ -362,4 +367,31 @@ class SqsEventSourcePollerTest { verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1"); verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any()); } + + @Test + void firstReceiveTimestampRemainsStableAcrossRetries() throws Exception { + Instant firstReceived = Instant.parse("2026-01-15T10:31:00Z"); + Message msg = new Message(); + msg.setBody("retry-body"); + msg.setSentTimestamp(Instant.parse("2026-01-15T10:30:00Z")); + msg.setFirstReceiveTimestamp(firstReceived); + msg.setReceiveCount(3); + + EventSourceMapping esm = new EventSourceMapping(); + esm.setEventSourceArn("arn:aws:sqs:us-east-1:000000000000:test-queue"); + esm.setRegion("us-east-1"); + + String firstCall = poller.buildSqsEvent(List.of(msg), esm); + Thread.sleep(10); + String secondCall = poller.buildSqsEvent(List.of(msg), esm); + + JsonNode firstTs = OBJECT_MAPPER.readTree(firstCall) + .get("Records").get(0).get("attributes").get("ApproximateFirstReceiveTimestamp"); + JsonNode secondTs = OBJECT_MAPPER.readTree(secondCall) + .get("Records").get(0).get("attributes").get("ApproximateFirstReceiveTimestamp"); + + assertEquals(String.valueOf(firstReceived.toEpochMilli()), firstTs.asText()); + assertEquals(firstTs.asText(), secondTs.asText(), + "ApproximateFirstReceiveTimestamp must not change between retries"); + } }