mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(eventbridge): retry target dispatch scope failures (#7201)
This commit is contained in:
1 parent
39a5a60edc
commit
1aa1ac6005
2 files changed
+125
-36
No files matched your search
+54
-36
@@ -102,8 +102,7 @@ public class TargetDispatcher implements Resettable {
|
||||
attempt(delivery, target, now);
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("EventBridge rule {0} could not dispatch to target {1}: {2}",
|
||||
ruleArn, target.getId(), e.getMessage());
|
||||
handleFailure(delivery, target, now, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,27 +124,22 @@ public class TargetDispatcher implements Resettable {
|
||||
retry(delivery, now);
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("EventBridge retry for rule {0} target {1} failed: {2}",
|
||||
delivery.ruleArn(), delivery.targetId(), e.getMessage());
|
||||
handleRetryScopeFailure(delivery, now, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void retry(Delivery delivery, Instant now) {
|
||||
if (delivery.generation() != generation.get()) {
|
||||
return;
|
||||
}
|
||||
Target target = delivery.currentTargets().get().stream()
|
||||
.filter(candidate -> delivery.targetId().equals(candidate.getId()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
private void handleRetryScopeFailure(Delivery delivery, Instant now, RuntimeException e) {
|
||||
Target target = retryTarget(delivery, now);
|
||||
if (target == null) {
|
||||
LOG.debugv("Dropped pending retry for rule {0} target {1}: target or rule removed",
|
||||
delivery.ruleArn(), delivery.targetId());
|
||||
return;
|
||||
}
|
||||
if (!now.isBefore(expiresAt(delivery, target))) {
|
||||
deadLetter(delivery, target, EXHAUSTED_BY_AGE);
|
||||
handleFailure(delivery.nextRetry(), target, now, e);
|
||||
}
|
||||
|
||||
private void retry(Delivery delivery, Instant now) {
|
||||
Target target = retryTarget(delivery, now);
|
||||
if (target == null) {
|
||||
return;
|
||||
}
|
||||
if (delivery.retryAttempts() >= maximumRetryAttempts(target)) {
|
||||
@@ -155,30 +149,54 @@ public class TargetDispatcher implements Resettable {
|
||||
attempt(delivery.nextRetry(), target, now);
|
||||
}
|
||||
|
||||
private Target retryTarget(Delivery delivery, Instant now) {
|
||||
if (delivery.generation() != generation.get()) {
|
||||
return null;
|
||||
}
|
||||
Target target = delivery.currentTargets().get().stream()
|
||||
.filter(candidate -> delivery.targetId().equals(candidate.getId()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (target == null) {
|
||||
LOG.debugv("Dropped pending retry for rule {0} target {1}: target or rule removed",
|
||||
delivery.ruleArn(), delivery.targetId());
|
||||
return null;
|
||||
}
|
||||
if (!now.isBefore(expiresAt(delivery, target))) {
|
||||
deadLetter(delivery, target, EXHAUSTED_BY_AGE);
|
||||
return null;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
private void attempt(Delivery delivery, Target target, Instant now) {
|
||||
try {
|
||||
invoker.invokeTarget(target, delivery.eventJson(), delivery.region(), delivery.ruleArn());
|
||||
} catch (Exception e) {
|
||||
if (delivery.generation() != generation.get()) {
|
||||
return;
|
||||
}
|
||||
String errorCode = classify(e);
|
||||
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}",
|
||||
delivery.ruleArn(), target.getId(), errorCode, message);
|
||||
long backoffSeconds = Math.min(MAXIMUM_BACKOFF_SECONDS, 1L << Math.min(delivery.retryAttempts(), 9));
|
||||
Instant backoff = now.plusSeconds(backoffSeconds);
|
||||
Instant expiresAt = expiresAt(delivery, target);
|
||||
Delivery failed = delivery.failed(errorCode, message, backoff.isBefore(expiresAt) ? backoff : expiresAt);
|
||||
if (!RETRYABLE_ERROR_CODES.contains(errorCode)) {
|
||||
deadLetter(failed, target, null);
|
||||
} else if (failed.retryAttempts() >= maximumRetryAttempts(target)) {
|
||||
deadLetter(failed, target, EXHAUSTED_BY_ATTEMPTS);
|
||||
} else {
|
||||
pending.add(failed);
|
||||
}
|
||||
handleFailure(delivery, target, now, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleFailure(Delivery delivery, Target target, Instant now, Exception e) {
|
||||
if (delivery.generation() != generation.get()) {
|
||||
return;
|
||||
}
|
||||
String errorCode = classify(e);
|
||||
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}",
|
||||
delivery.ruleArn(), target.getId(), errorCode, message);
|
||||
long backoffSeconds = Math.min(MAXIMUM_BACKOFF_SECONDS, 1L << Math.min(delivery.retryAttempts(), 9));
|
||||
Instant backoff = now.plusSeconds(backoffSeconds);
|
||||
Instant expiresAt = expiresAt(delivery, target);
|
||||
Delivery failed = delivery.failed(errorCode, message, backoff.isBefore(expiresAt) ? backoff : expiresAt);
|
||||
if (!RETRYABLE_ERROR_CODES.contains(errorCode)) {
|
||||
deadLetter(failed, target, null);
|
||||
} else if (failed.retryAttempts() >= maximumRetryAttempts(target)) {
|
||||
deadLetter(failed, target, EXHAUSTED_BY_ATTEMPTS);
|
||||
} else {
|
||||
pending.add(failed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+71
@@ -428,6 +428,77 @@ class TargetDispatcherTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void failureToEstablishRuleAccountScopeIsRetried() {
|
||||
Target target = sqsTarget(null, DLQ_ARN);
|
||||
AtomicReference<Boolean> failFirstScope = new AtomicReference<>(true);
|
||||
try (MockedStatic<RequestScopes> scopes =
|
||||
mockStatic(RequestScopes.class, withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
scopes.when(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
if (failFirstScope.getAndSet(false)) {
|
||||
throw new IllegalStateException("Request scope is not active");
|
||||
}
|
||||
invocation.<Runnable>getArgument(1).run();
|
||||
return null;
|
||||
});
|
||||
|
||||
dispatcher.dispatch("arn:aws:events:us-east-1:123456789012:rule/orders", target, EVENT, REGION,
|
||||
() -> List.of(target));
|
||||
verify(sqsService, never()).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
|
||||
tickAt(T0.plusSeconds(1));
|
||||
|
||||
scopes.verify(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)), times(2));
|
||||
verify(sqsService, times(1)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
verifyNoDeadLetter();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatedFailureToEstablishRuleAccountScopeIsDeadLetteredAtAttemptLimit() {
|
||||
Target target = sqsTarget(new Target.RetryPolicy(1, 3600), DLQ_ARN);
|
||||
try (MockedStatic<RequestScopes> scopes =
|
||||
mockStatic(RequestScopes.class, withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
scopes.when(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)))
|
||||
.thenThrow(new IllegalStateException("Request scope is not active"));
|
||||
|
||||
dispatcher.dispatch("arn:aws:events:us-east-1:123456789012:rule/orders", target, EVENT, REGION,
|
||||
() -> List.of(target));
|
||||
verify(sqsService, never()).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
verifyNoDeadLetter();
|
||||
|
||||
tickAt(T0.plusSeconds(1));
|
||||
|
||||
scopes.verify(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)), times(2));
|
||||
Map<String, MessageAttributeValue> attributes = deadLetterAttributes();
|
||||
assertAttribute(attributes, "EXHAUSTED_RETRY_CONDITION", "MaximumRetryAttempts");
|
||||
assertAttribute(attributes, "RETRY_ATTEMPTS", "1");
|
||||
assertAttribute(attributes, "ERROR_CODE", "INTERNAL_ERROR");
|
||||
verify(sqsService, never()).sendMessage(eq(TARGET_URL), eq(EVENT), eq(0), isNull(), isNull(), eq(REGION));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatedFailureToEstablishRuleAccountScopeStopsAtEventAgeLimit() {
|
||||
Target target = sqsTarget(new Target.RetryPolicy(5, 2), DLQ_ARN);
|
||||
try (MockedStatic<RequestScopes> scopes =
|
||||
mockStatic(RequestScopes.class, withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
scopes.when(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)))
|
||||
.thenThrow(new IllegalStateException("Request scope is not active"));
|
||||
|
||||
dispatcher.dispatch("arn:aws:events:us-east-1:123456789012:rule/orders", target, EVENT, REGION,
|
||||
() -> List.of(target));
|
||||
tickAt(T0.plusSeconds(1));
|
||||
tickAt(T0.plusSeconds(2));
|
||||
|
||||
scopes.verify(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)), times(3));
|
||||
Map<String, MessageAttributeValue> attributes = deadLetterAttributes();
|
||||
assertAttribute(attributes, "EXHAUSTED_RETRY_CONDITION", "MaximumEventAgeInSeconds");
|
||||
assertAttribute(attributes, "RETRY_ATTEMPTS", "1");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void retryEstablishesRuleAccountScope() {
|
||||
Target target = sqsTarget(null, null);
|
||||
|
||||
Reference in new issue
Block a user