feat(eventbridge): execute target retry and dead-letter policies (#4736)

* feat(eventbridge): execute target retry and dead-letter policies

PutTargets now validates, stores and returns a target's RetryPolicy and
DeadLetterConfig. Failed deliveries from PutEvents and scheduled rules are
retried with backoff within the configured attempts and event age, and
exhausted or non-retryable events are sent to the standard SQS dead-letter
queue with the AWS failure attributes.

* fix(eventbridge): order target retries by due time and honour a lowered retry limit

Pending retries are kept in a queue ordered by their next attempt, so each wake-up
takes only what is due instead of scanning every pending retry. The clock is read
again for each retry, so a slow target cannot let a later retry run past its event
age, and a reset mid-tick stops the remaining retries. A retry now checks the
target's current MaximumRetryAttempts before invoking it, as it already did for
MaximumEventAgeInSeconds.

* fix(eventbridge): drop a target retry that outlives a reset

An attempt still running when the emulator resets can fail afterwards. Its retry
or dead-letter delivery is now dropped, so a rule and target recreated under the
same names after the reset do not receive a pre-reset event. A scheduled retry
wake-up keeps only its due time, not the event.

* fix(eventbridge): wake a target retry at its due time after a slow attempt

The retry delay was measured from when the failed attempt started but counted from
when it failed, so every retry after a slow attempt woke late by the attempt's
duration, and an expired event reached its dead-letter queue late.

* fix(eventbridge): drop a removed target's retries and drive retries from one tick

RemoveTargets now drops the removed targets' pending retries, so a target re-added
under the same Id does not receive an event from before its removal. Pending retries
are taken by one periodic tick, as in the scheduler's dispatcher, instead of a timer
per retry, so a reset leaves no timers behind.

* fix(eventbridge): warn about a blank dead-letter queue ARN

PutTargets accepts a whitespace-only DeadLetterConfig Arn, since AWS constrains only
its length. Delivery skipped it without a word; it now gets the same WARN as any other
dead-letter queue that is not a standard SQS queue in the rule's region.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-01 07:59:18 -04:00
1 parent 4716ac9992
commit 0a54d0708e
15 files changed
+1221 -46

No files matched your search

@@ -303,6 +303,84 @@ class EventBridgeTest {
assertThat(body).doesNotContain("orderId");
}
// ──────────────────────────── Retry and dead-letter ────────────────────────────
@Test
@Order(35)
void undeliverableEventIsSentToTheTargetDeadLetterQueue() {
String dlqUrl = sqs.createQueue(CreateQueueRequest.builder()
.queueName(TestFixtures.uniqueName("eb-dlq")).build()).queueUrl();
String dlqArn = sqs.getQueueAttributes(GetQueueAttributesRequest.builder()
.queueUrl(dlqUrl).attributeNamesWithStrings("QueueArn").build())
.attributesAsStrings().get("QueueArn");
String missingQueueArn = dlqArn + "-missing";
String source = TestFixtures.uniqueName("com.floci.dlq");
String dlqRuleName = TestFixtures.uniqueName("eb-dlq-rule");
String ruleArn = eb.putRule(PutRuleRequest.builder()
.name(dlqRuleName)
.eventPattern("{\"source\":[\"" + source + "\"]}")
.state(RuleState.ENABLED)
.build()).ruleArn();
try {
eb.putTargets(PutTargetsRequest.builder()
.rule(dlqRuleName)
.targets(Target.builder()
.id("missing-queue")
.arn(missingQueueArn)
.retryPolicy(RetryPolicy.builder()
.maximumRetryAttempts(2).maximumEventAgeInSeconds(60).build())
.deadLetterConfig(DeadLetterConfig.builder().arn(dlqArn).build())
.build())
.build());
Target stored = eb.listTargetsByRule(ListTargetsByRuleRequest.builder().rule(dlqRuleName).build())
.targets().get(0);
assertThat(stored.retryPolicy().maximumRetryAttempts()).isEqualTo(2);
assertThat(stored.retryPolicy().maximumEventAgeInSeconds()).isEqualTo(60);
assertThat(stored.deadLetterConfig().arn()).isEqualTo(dlqArn);
eb.putEvents(PutEventsRequest.builder()
.entries(PutEventsRequestEntry.builder()
.source(source).detailType("Undeliverable").detail("{\"id\":\"1\"}").build())
.build());
List<Message> messages = sqs.receiveMessage(ReceiveMessageRequest.builder()
.queueUrl(dlqUrl)
.messageAttributeNames("All")
.waitTimeSeconds(2)
.build()).messages();
assertThat(messages).hasSize(1);
assertThat(messages.get(0).body()).contains(source).contains("Undeliverable");
Map<String, MessageAttributeValue> attributes = messages.get(0).messageAttributes();
assertThat(attributes.get("RULE_ARN").stringValue()).isEqualTo(ruleArn);
assertThat(attributes.get("TARGET_ARN").stringValue()).isEqualTo(missingQueueArn);
assertThat(attributes.get("ERROR_CODE").stringValue()).isEqualTo("NO_RESOURCE");
assertThat(attributes.get("RETRY_ATTEMPTS").stringValue()).isEqualTo("0");
} finally {
eb.removeTargets(RemoveTargetsRequest.builder().rule(dlqRuleName).ids("missing-queue").build());
eb.deleteRule(DeleteRuleRequest.builder().name(dlqRuleName).build());
sqs.deleteQueue(DeleteQueueRequest.builder().queueUrl(dlqUrl).build());
}
}
@Test
@Order(36)
void putTargetsRejectsOutOfRangeRetryPolicy() {
assertThatThrownBy(() -> eb.putTargets(PutTargetsRequest.builder()
.rule(ruleName)
.targets(Target.builder()
.id("bad-retry")
.arn(sinkQueueArn)
.retryPolicy(RetryPolicy.builder().maximumEventAgeInSeconds(0).build())
.build())
.build()))
.isInstanceOfSatisfying(EventBridgeException.class, e -> {
assertThat(e.statusCode()).isEqualTo(400);
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
assertThat(e.getMessage()).contains("targets.1.member.retryPolicy.maximumEventAgeInSeconds");
});
}
// ──────────────────────────── Tags ────────────────────────────
@Test