From daa6f35bb522c3d66fc41a15a0e8e557e2df9eb2 Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Sat, 28 Nov 2026 12:31:01 -0600 Subject: [PATCH] fix(eventbridge): contain secondary dispatch failures (#9655) A failure while EventBridge recorded a failed target delivery escaped the dispatcher. In PutEvents it failed the whole call, and in the scheduled retry tick it made scheduleWithFixedDelay suppress every later run, so no retry ran again until a restart. The dispatcher now logs such a failure and drops only that delivery, so the request completes and the other pending deliveries still run. The delivery and dead-letter warnings keep the exception's stack trace and cause for an unexpected error, and stay one line for an AwsException, such as a throttled API destination being retried. --- .../eventbridge/TargetDispatcher.java | 20 ++- .../eventbridge/TargetDispatcherTest.java | 137 ++++++++++++++++++ 2 files changed, 153 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/floci/aws/services/eventbridge/TargetDispatcher.java b/src/main/java/io/floci/aws/services/eventbridge/TargetDispatcher.java index dd6a3b8acd..d2b13e08d7 100644 --- a/src/main/java/io/floci/aws/services/eventbridge/TargetDispatcher.java +++ b/src/main/java/io/floci/aws/services/eventbridge/TargetDispatcher.java @@ -102,7 +102,13 @@ public class TargetDispatcher implements Resettable { attempt(delivery, target, now); } } catch (RuntimeException e) { - handleFailure(delivery, target, now, e); + LOG.warnv(e, "EventBridge rule {0} could not dispatch to target {1}: {2}", + ruleArn, target.getId(), e.getMessage()); + try { + handleFailure(delivery, target, now, e); + } catch (RuntimeException failure) { + LOG.warnv(failure, "EventBridge could not record delivery failure for rule {0}", ruleArn); + } } } @@ -124,7 +130,13 @@ public class TargetDispatcher implements Resettable { retry(delivery, now); } } catch (RuntimeException e) { - handleRetryScopeFailure(delivery, now, e); + LOG.warnv(e, "EventBridge retry for rule {0} target {1} failed: {2}", + delivery.ruleArn(), delivery.targetId(), e.getMessage()); + try { + handleRetryScopeFailure(delivery, now, e); + } catch (RuntimeException failure) { + LOG.warnv(failure, "EventBridge could not record delivery failure for rule {0}", delivery.ruleArn()); + } } } } @@ -185,7 +197,7 @@ public class TargetDispatcher implements Resettable { String message = e.getMessage() != null && !e.getMessage().isBlank() ? e.getMessage() : e.getClass().getSimpleName(); - LOG.warnv("EventBridge rule {0} target {1} delivery failed with {2}: {3}", + LOG.warnv(e instanceof AwsException ? null : e, "EventBridge rule {0} target {1} delivery failed with {2}: {3}", delivery.ruleArn(), target.getId(), errorCode, message); long backoffSeconds = Math.min(MAXIMUM_BACKOFF_SECONDS, 1L << Math.min(delivery.retryAttempts(), 9)); Instant backoff = now.plusSeconds(backoffSeconds); @@ -249,7 +261,7 @@ public class TargetDispatcher implements Resettable { sqsService.sendMessage(AwsArnUtils.arnToQueueUrl(queueArn, baseUrl), delivery.eventJson(), 0, null, null, attributes, delivery.region()); } catch (RuntimeException e) { - LOG.warnv("EventBridge rule {0} target {1} dead-letter delivery to {2} failed: {3}", + LOG.warnv(e instanceof AwsException ? null : e, "EventBridge rule {0} target {1} dead-letter delivery to {2} failed: {3}", delivery.ruleArn(), target.getId(), queueArn, e.getMessage()); } } diff --git a/src/test/java/io/floci/aws/services/eventbridge/TargetDispatcherTest.java b/src/test/java/io/floci/aws/services/eventbridge/TargetDispatcherTest.java index a0a84d451f..54a5f9d253 100644 --- a/src/test/java/io/floci/aws/services/eventbridge/TargetDispatcherTest.java +++ b/src/test/java/io/floci/aws/services/eventbridge/TargetDispatcherTest.java @@ -11,6 +11,7 @@ import io.floci.aws.services.sns.SnsService; import io.floci.aws.services.sqs.SqsService; import io.floci.aws.services.sqs.model.MessageAttributeValue; import io.floci.aws.testing.ParallelLocks; +import io.floci.aws.testutil.LogCapture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.ResourceLock; @@ -28,7 +29,9 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.logging.LogRecord; +import static io.floci.aws.testing.InlineMocks.inlineMock; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -412,6 +415,140 @@ class TargetDispatcherTest { verifyNoDeadLetter(); } + @Test + void throttledDeliveryLogsEachAttemptWithoutAStackTrace() { + AwsException failure = new AwsException("ThrottlingException", "Request limit reached.", 429); + when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION)) + .thenThrow(failure).thenThrow(failure).thenReturn(null); + Target target = sqsTarget(null, DLQ_ARN); + + List records = LogCapture.capture(TargetDispatcher.class, () -> { + dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> List.of(target)); + tickAt(T0.plusSeconds(1)); + tickAt(T0.plusSeconds(3)); + }); + + List warnings = records.stream() + .filter(record -> "EventBridge rule {0} target {1} delivery failed with {2}: {3}" + .equals(record.getMessage())).toList(); + assertEquals(2, warnings.size()); + for (LogRecord warning : warnings) { + assertNull(warning.getThrown()); + assertArrayEquals(new Object[]{RULE_ARN, target.getId(), "THROTTLING", failure.getMessage()}, + warning.getParameters()); + } + verify(sqsService, times(3)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION); + verifyNoDeadLetter(); + } + + @Test + void deadLetterAwsRefusalLogsWithoutAStackTrace() { + AwsException failure = new AwsException("AccessDeniedException", "queue denied", 403); + when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION)) + .thenThrow(new AwsException("AccessDeniedException", "target denied", 403)); + when(sqsService.sendMessage(eq(DLQ_URL), eq(EVENT), eq(0), isNull(), isNull(), anyMap(), eq(REGION))) + .thenThrow(failure); + Target target = sqsTarget(null, DLQ_ARN); + + List records = LogCapture.capture(TargetDispatcher.class, + () -> dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> List.of(target))); + tickAt(T0.plusSeconds(10)); + + LogRecord warning = records.stream() + .filter(record -> "EventBridge rule {0} target {1} dead-letter delivery to {2} failed: {3}" + .equals(record.getMessage())).findFirst().orElseThrow(); + assertNull(warning.getThrown()); + assertArrayEquals(new Object[]{RULE_ARN, target.getId(), DLQ_ARN, failure.getMessage()}, + warning.getParameters()); + verify(sqsService).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION); + verify(sqsService).sendMessage(eq(DLQ_URL), eq(EVENT), eq(0), isNull(), isNull(), anyMap(), eq(REGION)); + } + + @Test + void deliveryFailureRetainsCauseAndRetryOutcome() { + RuntimeException failure = new IllegalStateException((String) null, new IllegalArgumentException("nested")); + when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION)) + .thenThrow(failure).thenReturn(null); + Target target = sqsTarget(null, DLQ_ARN); + + List records = LogCapture.capture(TargetDispatcher.class, + () -> dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> List.of(target))); + tickAt(T0.plusSeconds(1)); + + verify(sqsService, times(2)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION); + verifyNoDeadLetter(); + assertCause(records, "EventBridge rule {0} target {1} delivery failed with {2}: {3}", failure, + RULE_ARN, target.getId(), "INTERNAL_ERROR", "IllegalStateException"); + } + + @Test + void deadLetterFailureRetainsCauseAndDoesNotRetryPermanentRefusal() { + RuntimeException failure = new IllegalStateException("dlq unavailable", new IllegalArgumentException("nested")); + when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION)) + .thenThrow(new AwsException("AccessDeniedException", "denied", 403)); + when(sqsService.sendMessage(eq(DLQ_URL), eq(EVENT), eq(0), isNull(), isNull(), anyMap(), eq(REGION))) + .thenThrow(failure); + Target target = sqsTarget(null, DLQ_ARN); + + List records = LogCapture.capture(TargetDispatcher.class, + () -> dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> List.of(target))); + tickAt(T0.plusSeconds(10)); + + verify(sqsService).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION); + Map attributes = deadLetterAttributes(); + assertAttribute(attributes, "ERROR_CODE", "NO_PERMISSIONS"); + assertAttribute(attributes, "ERROR_MESSAGE", "denied"); + assertAttribute(attributes, "RETRY_ATTEMPTS", "0"); + assertCause(records, "EventBridge rule {0} target {1} dead-letter delivery to {2} failed: {3}", + failure, RULE_ARN, target.getId(), DLQ_ARN, failure.getMessage()); + } + + @Test + void retryLookupFailureRetainsCauseAndAllowsOtherPendingDelivery() { + RuntimeException failure = new IllegalStateException("lookup unavailable", new IllegalArgumentException("nested")); + when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION)) + .thenThrow(unavailable()).thenThrow(unavailable()).thenReturn(null); + Target target = sqsTarget(null, DLQ_ARN); + dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> { throw failure; }); + dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> List.of(target)); + + List records = LogCapture.capture(TargetDispatcher.class, () -> tickAt(T0.plusSeconds(1))); + verify(sqsService, times(3)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION); + tickAt(T0.plusSeconds(10)); + + verify(sqsService, times(3)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION); + verifyNoDeadLetter(); + assertCause(records, "EventBridge retry for rule {0} target {1} failed: {2}", + failure, RULE_ARN, target.getId(), failure.getMessage()); + } + + @Test + void dispatchFailureRetainsCauseWithoutEscapingCaller() { + RuntimeException failure = new IllegalStateException("policy unavailable", new IllegalArgumentException("nested")); + Target.RetryPolicy retryPolicy = inlineMock(Target.RetryPolicy.class); + when(retryPolicy.maximumEventAgeInSeconds()).thenThrow(failure); + Target target = sqsTarget(retryPolicy, DLQ_ARN); + when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION)).thenThrow(unavailable()); + + List records = LogCapture.capture(TargetDispatcher.class, + () -> assertDoesNotThrow(() -> dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> List.of(target)))); + tickAt(T0.plusSeconds(10)); + + verify(sqsService).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION); + verifyNoDeadLetter(); + assertCause(records, "EventBridge rule {0} could not dispatch to target {1}: {2}", + failure, RULE_ARN, target.getId(), failure.getMessage()); + } + + private static void assertCause(List records, String template, RuntimeException failure, + Object... parameters) { + LogRecord record = records.stream().filter(entry -> template.equals(entry.getMessage())) + .findFirst().orElseThrow(); + assertArrayEquals(parameters, record.getParameters()); + assertSame(failure, record.getThrown()); + assertSame(failure.getCause(), record.getThrown().getCause()); + } + @Test void dispatchEstablishesRuleAccountScopeBeforeFirstAttempt() { Target target = sqsTarget(null, null);