Repository navigation
fix: enforce single half-open circuit breaker probe - #7802
Conversation
Aias00
left a comment
There was a problem hiding this comment.
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.
|
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:
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 I also preserved Cancellation of a request that actually acquired the half-open probe now reopens the corresponding breaker, preventing it from remaining stuck in Retry filtering still uses Regression coverage includes:
Validation:
Could you please take another look when you have a chance? |
|
pls add my wechat: aias00 |
Aias00
left a comment
There was a problem hiding this comment.
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
isRequestAllowedis false and no second probe is taken), - only one caller can win the probe because
tryAcquireHalfOpenflips 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.
Problem
The built-in upstream circuit breaker had two issues across the breaker and divide plugin path.
After
isRequestAllowed()was restricted to theCLOSEDstate, anOPENupstream 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_OPENbecause no terminal outcome was recorded.Fix
isRequestAllowed()limited to theCLOSEDstate.SPECIFY_DOMAINrouting: 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.CLOSEDrequest uncounted.isRequestAllowed()so a retry does not consume a second half-open probe.Validation
DividePluginCircuitBreakerTest: 9/9 passedUpstreamCircuitBreakerTest: 14/14 passedDividePluginTest: 12/12 passedgit diff --check: passedTests 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