Skip to content

fix(httpclient): budget deadlines for complete retry sequences - #7266

Merged
Aias00 merged 3 commits into
apache:masterfrom
dengliming:fix-6583-budget-retry-chain-timeouts
Sep 30, 2026
Merged

Aias00 merged 3 commits into
apache:masterfrom
dengliming:fix-6583-budget-retry-chain-timeouts

Conversation

@dengliming

Copy link
Copy Markdown
Member

Summary

  • Give fixed/exponential retries a sequence deadline covering every configured attempt plus the strategy's maximum backoff, instead of reusing the single-attempt timeout.
  • Add the same bounded sequence deadline to the default current-upstream strategy, retaining per-attempt timeouts and existing retry eligibility/error mapping.
  • Saturate overflow when calculating a deadline for extreme configurations.

Fixes #6583

Validation

  • ./mvnw -pl shenyu-plugin/shenyu-plugin-httpclient -am -Dtest=RetryTimeoutBudgetTest,RetryStrategyTest,RequestBodyReplayRetryTest -DfailIfNoTests=false test
  • Checkstyle enabled. Virtual-time regressions verify all three strategies complete three retries despite backoff exceeding the attempt timeout, and bound a never-completing sequence.
  • Existing request-body replay regressions pass.

The total bound intentionally includes worst-case configured retries/backoff; large retry counts can still produce long deadlines. This does not introduce a new, separately configurable SLA or alter failover selection.

Aias00
Aias00 previously approved these changes Sep 26, 2026

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved - this removes the aggregate timeout that starved the configured retries, and it does it in the only place where it can be done correctly.

Root cause, verified rather than assumed

AbstractHttpClientPlugin:91 already applies .timeout(duration, ...) to the Mono before the strategy sees it:

final Mono<R> response = doRequest(...)
        .timeout(duration, Mono.error(() -> new TimeoutException("Response took longer than timeout: " + duration)))
        .doOnError(e -> LOG.error(e.getMessage(), e));
...
Mono<R> retriedResponse = strategy.execute(response, exchange, duration, retryTimes);

Because retryWhen resubscribes its source, that inner timeout re-arms on every attempt - so per-attempt protection was never missing. What the strategies additionally did was wrap the whole retried sequence in another timeout with the same one-attempt budget:

return response.retryWhen(retrySpec)
        .timeout(duration, Mono.error(() -> new TimeoutException(...)));

so with, say, a 5s attempt timeout and three attempts, the sequence was killed at 5s regardless of how many retries were configured. Replacing that outer cap with RetryTimeoutUtils.totalTimeout is exactly the missing part.

The budget actually matches the configured backoff

strategy max backoff configured constant passed to totalTimeout
DefaultRetryStrategy (current) Retry.backoff(...).maxBackoff(Duration.ofSeconds(20)) (:69) Duration.ofSeconds(20) (:78)
ExponentialRetryBackoffStrategy maxBackoff(Duration.ofSeconds(5)) (:60) Duration.ofSeconds(5) (:51)
FixedRetryStrategy Retry.fixedDelay(retryTimes, Duration.ofSeconds(2)) (:56) Duration.ofSeconds(2) (:52)

Jitter (jitter(0.5d)) only ever shortens a computed delay, never exceeds maxBackoff, so (attempts * duration) + (retries * maxBackoff) really is the worst case. Overflow is saturated to Duration.ofNanos(Long.MAX_VALUE) and Duration#toNanos is inside the try (it throws its own ArithmeticException), which the new unit test pins.

The changed expectation in RetryStrategyTest is the regression test. testFixedRetryStrategyExecute uses duration = 5s with retryTimes = 3 at a 2s fixed delay: under the old code that 6s sequence hit the 5s cap and reported TimeoutException; it now reports retry exhaustion and had to change to Exceptions::isRetryExhausted. That flip is the bug being fixed, made visible - much better than asserting the new happy path only. Together with the virtual-time tests that prove all three strategies complete three retries despite backoff exceeding the attempt timeout, and that a never-completing source is still bounded, the coverage is real.

Non-blocking - two things worth writing down

  1. The worst-case latency envelope grew by design. With the default HTTP_TIME_OUT of 3000 ms and retryTimes = 3, the "current" strategy budget is 4 x 3s + 3 x 20s = 72s before the caller sees anything. Previously DefaultRetryStrategy had no aggregate cap at all (only the per-attempt one), so a hanging upstream kept the request open until the client gave up; now it terminates as GATEWAY_TIMEOUT after 72s. Both are defensible, but this number should be visible in the docs/release note - it is the largest change an operator will actually feel.
  2. Status mapping differs by failure mode (DefaultRetryStrategy:81-82): blowing the sequence budget maps to 504 GATEWAY_TIMEOUT, whereas exhausting retries maps to ShenyuTimeoutException -> 408 REQUEST_TIMEOUT. Documenting that distinction (and the message text "Retry sequence took longer than timeout: ...") will save support engineers a lot of guessing.

CI is green on the head commit.

});
Duration totalTimeout = RetryTimeoutUtils.totalTimeout(duration, retryTimes, Duration.ofSeconds(20));
return clientResponse.retryWhen(retryBackoffSpec)
.timeout(totalTimeout, Mono.error(() -> new TimeoutException("Retry sequence took longer than timeout: " + totalTimeout)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, please write into the docs/release note: this is the first aggregate cap the default strategy has ever had (before this change it only had the per-attempt timeout applied in AbstractHttpClientPlugin.java:91), and it noticeably widens the worst-case latency.

With the defaults - HTTP_TIME_OUT = 3000 ms (AbstractHttpClientPlugin.java:80) and three retries - the budget here is 4 x 3s + 3 x 20s = 72s. Previously a hanging upstream stayed open until the client gave up; now it terminates after 72s. Bounded is better, I just want the number to be findable.

Also note the two failure modes map to different statuses below: blowing this budget becomes 504 GATEWAY_TIMEOUT, exhausting retries becomes ShenyuTimeoutException -> 408 REQUEST_TIMEOUT. The message text Retry sequence took longer than timeout: ... is what separates them in the logs - worth quoting in the docs.

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The retry-budget implementation and targeted tests are sound, but the current head is unmergeable because RELEASE-NOTES.md conflicts with the current master Unreleased section. Please rebase/merge current master, preserve both behavior-change entries, and rerun CI on the resolved head.

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Budgeting a deadline across the whole retry sequence (via RetryTimeoutUtils, with per-strategy defaults of 20s/5s/2s) is the right shape: it bounds total latency instead of re-arming a full per-attempt timeout. RetryTimeoutBudgetTest covers the arithmetic. The earlier RELEASE-NOTES conflict is resolved and CI is green.

@Aias00
Aias00 merged commit d6db65a into apache:master Sep 30, 2026
20 checks passed
@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Exponential/Fixed retry strategies: outer timeout neuters configured retry count

2 participants