Repository navigation
fix: add lightweight circuit breaker to divide retry path - #7507
Merged
Merged
Conversation
Aias00
approved these changes
Oct 10, 2026
Aias00
left a comment
Contributor
There was a problem hiding this comment.
Review: fix — add lightweight circuit breaker to divide retry path
Reviewed the current revision (head 899d1b9). Verified DefaultRetryStrategy.resend has selectorId in scope (line 112) and DividePlugin already imports CollectionUtils.
Why approve
- New
UpstreamCircuitBreakeris a clean, well-scoped CLOSED/OPEN/HALF_OPEN implementation keyed perselectorId:upstreamviaConcurrentHashMap+computeIfAbsent; state transitions aresynchronized, so per-key state is thread-safe. Defaults (failureThreshold=3, halfOpenWait=10s) are sensible. - Integration points are correct:
DividePluginfilters blocked upstreams before load-balancing, fails fast withCANNOT_FIND_HEALTHY_UPSTREAM_URLwhen all are blocked, and grants a single half-open probe so the breaker can recover.DefaultRetryStrategy.resendskips breaker-blocked upstreams and records outcomes viarecordOutcome, so failures/successes feed the breaker.forwardWithCircuitBreakerpreserves the existing P2C / SHORTEST_RESPONSE triggers while wrapping with outcome recording.
- Tests are thorough:
UpstreamCircuitBreakerTest(incl. concurrent failure/half-open probes),RetryStrategyTest(skip-blocked, record failure/success outcome), andDividePluginCircuitBreakerTest(fail-fast, consecutive-failure open, half-open recovery, healthy bypass).build+ integrated tests pass.
Non-blocking suggestions
- The
breakersmap grows per selector:upstream and is only cleared viareset()(used by tests); under heavy upstream churn without successful recovery this could accumulate stale entries. A TTL/eviction on idle closed breakers would be a nice follow-up, but it's not a blocker. UpstreamCircuitBreaker.recordOutcomeis a static method bound to theINSTANCEsingleton (correct for production). InUpstreamCircuitBreakerTest.testRecordOutcomeRecordsTerminalSignalsacreate()instance is mixed with the static recorder — the assertion still passes, but the comment about "reset by the success" is slightly misleading since the local instance isn't the one the static recorder writes to. Cosmetic only.
LGTM — approving.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[X] I have read the Contributor Guide
[X] I have added test cases for my changes
[X] I have run
./mvnw clean install -Dmaven.javadoc.skip=truelocallySummary
Fixes #6584 — when every upstream behind a divide selector is down, each request still walked the full retry path (every attempt waiting for its full timeout) before failing. This PR adds a lightweight, built-in circuit breaker to the divide/HTTP proxy and its failover retry path so that requests fail fast once an upstream's consecutive failures reach the threshold, with a half-open probe to recover.
Changes:
UpstreamCircuitBreaker.java(new,shenyu-plugin/shenyu-plugin-base/.../base/circuitbreaker/UpstreamCircuitBreaker.java) — lightweight per-selector-upstream breaker stored in aConcurrentHashMap(thread-safe). State machine: CLOSED → (3 consecutive failures by default) → OPEN (requests fail fast, no real traffic) → after a 10s wait window a single half-open probe is granted atomically (tryAcquireHalfOpenProbe) → probe success closes the breaker, probe failure re-opens it. With no failures the happy path only performs map lookups, so the overhead is negligible, and it is fully independent of the optional resilience4j plugin.DividePlugin.java(shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide) — after fetching the cached upstream list, upstreams still blocked by the breaker are filtered out before load balancing (filterByCircuitBreaker); if every upstream is blocked, exactly one half-open probe request is granted, otherwise the request fails fast withCANNOT_FIND_HEALTHY_UPSTREAM_URLinstead of being forwarded. The forwarding tail was extracted toforwardWithCircuitBreaker, which records the request outcome (ON_COMPLETE→ success,ON_ERROR→ failure) viaUpstreamCircuitBreaker.recordOutcome.DefaultRetryStrategy.java(shenyu-plugin/shenyu-plugin-httpclient) — the failoverresendpath now skips upstreams blocked by the breaker (fast fail instead of paying a full timeout per retry) and records each resend attempt's outcome against the retried upstream.Test Cases:
UpstreamCircuitBreakerTest.java(new,shenyu-plugin/shenyu-plugin-base) — opens after the consecutive-failure threshold, success resets/closes, blocking within the wait window, half-open probe granted exactly once (including a 16-thread concurrent acquisition race where only one caller wins), failed probe re-opens, successful probe closes, 32-thread concurrent failure recording,recordOutcomesignal handling, key building, reset and per-key independence.DividePluginCircuitBreakerTest.java(new,shenyu-plugin-divide) — end-to-end throughDividePlugin.doExecute: fail fast without invoking the chain when all upstreams are blocked; 3 consecutive failed requests open the breaker and the 4th request fails fast; after the wait window a half-open probe is forwarded and its success closes the breaker; healthy upstreams still receive traffic when only one upstream is blocked.RetryStrategyTest.java(extended,shenyu-plugin-httpclient) — failover skips a blocked standby upstream and exhausts withCANNOT_FIND_HEALTHY_UPSTREAM_URL_AFTER_FAILOVERwithout calling the client; a failed resend records a failure for that upstream; a successful resend resets its failure counter.Verification
./mvnw -pl shenyu-plugin/shenyu-plugin-base,shenyu-plugin/shenyu-plugin-httpclient,shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide -am validate— checkstyle and Apache RAT pass../mvnw -pl shenyu-plugin/shenyu-plugin-base,shenyu-plugin/shenyu-plugin-httpclient,shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide -am test(no skips, JDK 21) — all tests pass, including the 28 new/extended breaker test cases above.Fixes #6584