Skip to content

fix: enforce single half-open circuit breaker probe - #7802

Merged
Aias00 merged 2 commits into
apache:masterfrom
IT-Althusser:fix/7760-circuit-breaker-half-open
Oct 20, 2026
Merged

Aias00 merged 2 commits into
apache:masterfrom
IT-Althusser:fix/7760-circuit-breaker-half-open

Conversation

@IT-Althusser

@IT-Althusser IT-Althusser commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The built-in upstream circuit breaker had two issues across the breaker and divide plugin path.

After isRequestAllowed() was restricted to the CLOSED state, an OPEN upstream whose half-open wait window had elapsed was no longer admitted as ordinary traffic. However, DividePlugin.filterByCircuitBreaker() only attempted a half-open probe when no upstream was allowed. If another upstream under the same selector remained healthy, the recovered upstream was never probed and could remain out of rotation indefinitely.

A probe request cancelled by the client could also leave the breaker permanently stuck in HALF_OPEN because no terminal outcome was recorded.

Fix

  • Keep isRequestAllowed() limited to the CLOSED state.
  • Attempt half-open probe acquisition per upstream in one pass so each blocked upstream can recover.
  • Return an acquired probe as a singleton candidate, ensuring the request reaches the recovering upstream and its outcome is recorded instead of being lost to load balancing.
  • Preserve SPECIFY_DOMAIN routing: the specified upstream is preferred for probe acquisition; if it is unavailable and no closed upstream is available, an eligible upstream may be used as a fallback probe.
  • Reopen the breaker when a request that actually acquired the half-open probe is cancelled.
  • Leave ordinary cancellation of a CLOSED request uncounted.
  • Keep retry filtering based on isRequestAllowed() so a retry does not consume a second half-open probe.

Validation

  • DividePluginCircuitBreakerTest: 9/9 passed
  • UpstreamCircuitBreakerTest: 14/14 passed
  • DividePluginTest: 12/12 passed
  • Divide module Checkstyle: 0 violations
  • git diff --check: passed

Tests were run locally on JDK 25 with the project-compatible Byte Buddy setting. JaCoCo/Byte Buddy class-version warnings were environment warnings; no test assertions failed.

Closes #7760

@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 invariant change itself is correct — isRequestAllowed returning true for an OPEN breaker past its wait window did let an unbounded number of concurrent requests through before any probe token was taken, which defeats the point of half-open. Confining it to State.CLOSED is the right fix, and tryAcquireHalfOpen atomically granting exactly one probe is a clean mechanism.

However the change introduces an availability regression at the only caller that matters, so I cannot approve as is.

A recovered upstream never rejoins rotation

DividePlugin.filterByCircuitBreaker only attempts a probe when nothing is allowed:

for (Upstream upstream : upstreamList) {
    if (UpstreamCircuitBreaker.getInstance().isRequestAllowed(buildKey(selectorId, upstream))) {
        allowed.add(upstream);
    }
}
if (CollectionUtils.isNotEmpty(allowed)) {
    return allowed;                       // <-- probe loop below never runs
}
for (Upstream upstream : upstreamList) {
    if (UpstreamCircuitBreaker.getInstance().tryAcquireHalfOpenProbe(buildKey(selectorId, upstream))) {
        return Collections.singletonList(upstream);
    }
}

With two or more upstreams behind a selector, one breaker tripping leaves the others CLOSED, so allowed is non-empty and the probe loop is skipped. Before this PR the tripped upstream was re-admitted as soon as openedAtMillis + halfOpenWaitMillis elapsed, received traffic, and recordSuccess moved it back to CLOSED. After this PR it is excluded forever, because nothing ever calls tryAcquireHalfOpenProbe for it. Recovery now requires every sibling to trip as well, which is the opposite of resilient.

That is a worse failure mode than the one being fixed: double-probing briefly overloads a struggling upstream, whereas never probing silently removes a healthy instance from rotation until a config refresh.

Suggested shape

Attempt the probe per upstream in the same loop, so each blocked upstream has one chance to be the probe:

private List<Upstream> filterByCircuitBreaker(final String selectorId, final List<Upstream> upstreamList) {
    List<Upstream> allowed = new ArrayList<>(upstreamList.size());
    for (Upstream upstream : upstreamList) {
        final String key = UpstreamCircuitBreaker.buildKey(selectorId, upstream);
        if (UpstreamCircuitBreaker.getInstance().isRequestAllowed(key)
                || UpstreamCircuitBreaker.getInstance().tryAcquireHalfOpenProbe(key)) {
            allowed.add(upstream);
        }
    }
    return allowed;
}

This keeps the property you want (a CLOSED breaker short-circuits before tryAcquireHalfOpenProbe, an OPEN breaker inside the wait window still fails both checks, and only one caller can win the probe because tryAcquireHalfOpen flips OPEN → HALF_OPEN on the first success) while restoring per-upstream recovery. The Collections.singletonList early return then disappears.

Also worth a look

DefaultRetryStrategy filters retry candidates with isRequestAllowed alone:

.filter(data -> UpstreamCircuitBreaker.getInstance().isRequestAllowed(buildKey(selectorId, data)))

That is defensible if the initial selection already granted the probe — a retry must not consume a second one. Just confirm that is the intent, because it means a failed probe is never retried against that upstream until the next full half-open cycle, which is fine but should not be accidental.

Test coverage

The added assertion assertFalse(breaker.isRequestAllowed(KEY)) after the wait window pins the new invariant well. What is missing is a case at the DividePlugin level — two upstreams, one OPEN past its window — asserting that the blocked one is still selectable once the window elapses. That is exactly the regression above and the existing unit test cannot see it.

@IT-Althusser

IT-Althusser commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. You were right that the previous early return prevented an OPEN upstream from recovering while another upstream under the same selector remained CLOSED.

I adopted the per-upstream probe attempt from your suggestion, while keeping an acquired probe exclusive to the recovering upstream. The updated behavior is:

  • Each upstream is checked in a single pass.
  • CLOSED upstreams are added to the normal candidate list.
  • A blocked upstream can acquire a half-open probe.
  • If a probe is acquired, that upstream is returned as a singleton candidate, ensuring that the request reaches the recovering upstream and its outcome is recorded.
  • If no probe is acquired, the CLOSED upstreams are returned as usual.

The per-upstream probe attempt follows the core of your suggestion. I kept an acquired probe exclusive to the recovering upstream because adding it to the normal allowed list would separate the probe token from the request. If a healthy sibling were selected by the load balancer, the recovering upstream would receive no terminal signal and could remain stuck in HALF_OPEN indefinitely. Returning the probe as a singleton keeps the token and request together while still restoring per-upstream recovery.

I also preserved SPECIFY_DOMAIN behavior. The specified upstream is preferred for probe acquisition. If it is unavailable and no CLOSED upstream is available, an eligible upstream may be selected as a fallback probe.

Cancellation of a request that actually acquired the half-open probe now reopens the corresponding breaker, preventing it from remaining stuck in HALF_OPEN. Cancellation of an ordinary CLOSED request does not change the breaker state.

Retry filtering still uses isRequestAllowed only, so a retry does not consume a second half-open probe.

Regression coverage includes:

  • recovery with a healthy sibling upstream;
  • exclusive routing of an acquired probe;
  • specified-domain routing and fallback;
  • reopening after probe cancellation;
  • preserving the CLOSED state after ordinary request cancellation.

Validation:

  • DividePluginCircuitBreakerTest: 9/9 passed
  • UpstreamCircuitBreakerTest: 14/14 passed
  • DividePluginTest: 12/12 passed
  • Checkstyle: 0 violations
  • git diff --check: passed

Could you please take another look when you have a chance?

@Aias00

Aias00 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

pls add my wechat: aias00

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

Re-review: #7802 fix: enforce single half-open circuit breaker probe

Re-reviewed after the update. My previous CHANGES_REQUESTED pointed out that a tripped upstream behind healthy siblings would never be probed (the probe loop was skipped whenever allowed was non-empty), silently removing a recovering instance from rotation.

The new filterByCircuitBreaker now acquires the half-open probe inside the same loop (tryAcquireHalfOpenProbe in the else if branch), so even when other upstreams are CLOSED and selectable, the tripped upstream still gets its single probe. The selected set is the probe singleton when a probe was acquired, otherwise the healthy allowed list — so:

  • the recovery path works again (the original regression is fixed),
  • while the probe is in flight, subsequent requests fall back to the healthy upstreams (the probe state is HALF_OPEN, so isRequestAllowed is false and no second probe is taken),
  • only one caller can win the probe because tryAcquireHalfOpen flips OPEN → HALF_OPEN atomically.

Additionally, a recordProbeCancellation path re-opens the breaker if a granted probe request is cancelled, and recordOutcome now threads probeGranted so cancellation is handled correctly. Tests added: testHalfOpenProbeIsSelectedWhileAnotherUpstreamIsHealthy, testSpecifiedHealthyUpstreamIsNotOverriddenByAnotherProbe, testUnavailableSpecifiedUpstreamFallsBackToAvailableProbe, testCancelledHalfOpenProbeReopensBreaker, and testCancelledClosedRequestLeavesBreakerClosed.

The DefaultRetryStrategy still filters retry candidates with isRequestAllowed alone, which is fine given the initial selection now grants the probe as intended.

Verdict

APPROVE — the availability regression is fixed and the half-open semantics are correct.

@Aias00
Aias00 merged commit 0e6216d into apache:master Oct 20, 2026
55 checks passed
@IT-Althusser
IT-Althusser deleted the fix/7760-circuit-breaker-half-open branch October 26, 2026 16:00
@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] Built-in upstream circuit breaker does not enforce a single half-open probe

2 participants