Skip to content

fix: add lightweight circuit breaker to divide retry path - #7507

Merged
Aias00 merged 1 commit into
apache:masterfrom
wy471x:fix_circuit-breaker-retry-path
Oct 10, 2026
Merged

Aias00 merged 1 commit into
apache:masterfrom
wy471x:fix_circuit-breaker-retry-path

Conversation

@wy471x

@wy471x wy471x commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

[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=true locally

Summary

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:

  1. UpstreamCircuitBreaker.java (new, shenyu-plugin/shenyu-plugin-base/.../base/circuitbreaker/UpstreamCircuitBreaker.java) — lightweight per-selector-upstream breaker stored in a ConcurrentHashMap (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.
  2. 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 with CANNOT_FIND_HEALTHY_UPSTREAM_URL instead of being forwarded. The forwarding tail was extracted to forwardWithCircuitBreaker, which records the request outcome (ON_COMPLETE → success, ON_ERROR → failure) via UpstreamCircuitBreaker.recordOutcome.
  3. DefaultRetryStrategy.java (shenyu-plugin/shenyu-plugin-httpclient) — the failover resend path 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, recordOutcome signal handling, key building, reset and per-key independence.
  • DividePluginCircuitBreakerTest.java (new, shenyu-plugin-divide) — end-to-end through DividePlugin.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 with CANNOT_FIND_HEALTHY_UPSTREAM_URL_AFTER_FAILOVER without 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

@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.

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 UpstreamCircuitBreaker is a clean, well-scoped CLOSED/OPEN/HALF_OPEN implementation keyed per selectorId:upstream via ConcurrentHashMap + computeIfAbsent; state transitions are synchronized, so per-key state is thread-safe. Defaults (failureThreshold=3, halfOpenWait=10s) are sensible.
  • Integration points are correct:
    • DividePlugin filters blocked upstreams before load-balancing, fails fast with CANNOT_FIND_HEALTHY_UPSTREAM_URL when all are blocked, and grants a single half-open probe so the breaker can recover.
    • DefaultRetryStrategy.resend skips breaker-blocked upstreams and records outcomes via recordOutcome, so failures/successes feed the breaker.
    • forwardWithCircuitBreaker preserves 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), and DividePluginCircuitBreakerTest (fail-fast, consecutive-failure open, half-open recovery, healthy bypass). build + integrated tests pass.

Non-blocking suggestions

  • The breakers map grows per selector:upstream and is only cleared via reset() (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.recordOutcome is a static method bound to the INSTANCE singleton (correct for production). In UpstreamCircuitBreakerTest.testRecordOutcomeRecordsTerminalSignals a create() 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.

@Aias00
Aias00 merged commit 1797bd3 into apache:master Oct 10, 2026
48 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] No circuit breaker in the proxy/retry path by default — no fast-fail when all upstreams are down

2 participants