Repository navigation
fix: pair P2C inflight accounting for singleton upstream selection - #7980
Conversation
AbstractLoadBalancer.select short-circuits a singleton candidate list and returns the only upstream without calling P2cLoadBalancer.doSelect, so the inflight increment is skipped. Divide's P2C completion callback still decrements the selected upstream, so repeated singleton requests drive the inflight counter below its baseline and eventually negative, which P2C then uses in its load calculation. Override select in P2cLoadBalancer to perform the same increment (and lastPicked update) that doSelect does when the candidate list is a singleton, keeping start/completion accounting paired. Add regression tests covering the single increment and the paired select/decrement loop from the issue reproduction.
Aias00
left a comment
There was a problem hiding this comment.
Review: #7980 fix: pair P2C inflight accounting for singleton upstream selection
Verdict: approved. The diagnosis is exactly right and the fix is in the only place it can be.
Verified
AbstractLoadBalancer.selectshort-circuits beforedoSelectwhenupstreamList.size() == 1(shenyu-loadbalancer/.../spi/AbstractLoadBalancer.java: empty →null, single →upstreamList.get(0), otherwisedoSelect). SoP2cLoadBalancer.doSelect— the only place that doespicked.getInflight().incrementAndGet()— never runs for a singleton list, whileDividePlugin's response trigger still decrements. That is a guaranteed negative drift, and it is not "a few units": with a one-instance selector every single request walks the counter down.- The override cannot double-count.
super.selectonly reachesdoSelectforsize() > 1, and the new block is gated onupstreamList.size() == 1, so the two increments are mutually exclusive. Objects.nonNull(picked)also covers the empty-list case, wheresuper.selectreturnsnull.testSingletonSelectionStaysPairedWithCompletionDecrementmodels the real contract (select then decrement, twice) and asserts the counter returns to baseline, which is the property that matters — not just "went up by one".
Merge order — please merge this one first
#7982 releases the P2C increment taken when DefaultRetryStrategy re-selects an upstream during failover. On a singleton retry list that release has nothing to release unless this PR is already in: #7982 would decrement a counter this PR is the one that increments, and inflight would go negative on every singleton failover. #7982 must not land before this one.
Non-blocking
- The pairing assumption is now broader than Divide. The increment is unconditional for singleton lists, but the matching decrement lives in
DividePlugin's completion callback. Any other caller that reachesP2cLoadBalancer.selectwith a singleton list and never decrements will now leak one unit per request — previously it leaked nothing on the singleton path (it leaked on the multi-candidate path already, so the assumption is at least consistent). Worth one line in the PR description naming Divide as the counterparty, so a future caller knows what it is signing up for. picked.setLastPicked(System.currentTimeMillis())is not accounting — it is the "force a stale node to be picked" bookkeeping. With a single candidate the force-gap logic is meaningless, so setting it here only refreshes a timestamp nobody reads. Harmless, but it makes the diff look like it is doing two things; if it is deliberate, a half-line comment would say why.size() == 1is used as a proxy for "doSelectdid not run". That is true today becauseAbstractLoadBalanceris the only entry point; ifselectis ever overridden again the proxy breaks silently. Comparing against a boolean the base class returns would be more robust, but that is a bigger refactor than this fix deserves.
DefaultRetryStrategy.resend re-selects a retry upstream through LoadbalancerUtils.getForExchange, which increments the P2C inflight counter of the newly picked upstream. Divide's completion callback only captures the initially selected upstream, so nothing released the retry attempt's increment. Repeated failovers therefore inflated inflight on retry destinations and distorted subsequent P2C load estimates. Attach a doFinally cleanup that decrements the retry upstream's inflight on success, failure and cancellation, guarded to the P2C algorithm (the only balancer that increments on selection), so every P2C attempt keeps paired accounting on its own selected upstream. Stacked on the #7974 singleton-selection fix so the release is also correct when a single retry candidate remains and P2C selection is short-circuited. Fixes #7975 Co-authored-by: Luke Haochao Zhuang <[email protected]>
Aias00
left a comment
There was a problem hiding this comment.
Approving the accounting fix; the branch needs a rebase before it can merge.
P2cLoadBalancer.selectpairing the singleton increment is correct:AbstractLoadBalancer.selectreturns beforedoSelectfor a single candidate, so without this the completion callback droveinflightnegative.setLastPickedis also refreshed, matching thedoSelectpath.DefaultRetryStrategy.resendreleasing the retry upstream ondoFinallycovers success, error and cancellation, which is what #7975 needs.
Rebase note: master now has a LEAST_ACTIVITY branch in resend that increments and decrements the same counter. Keep both branches independent and make sure the P2C branch only decrements — the increment already happened inside the load balancer. Please rebase and re-run the build.
|
hi @dmsolr please resolve the conflicts, thanks |
|
/hold |
|
@dmsolr Heads-up: this PR is approved and every check is green, so the only thing blocking the merge is the merge conflict GitHub reports ( Please rebase onto current |
Resolve semantic conflict in DefaultRetryStrategy.resend(): keep the P2C inflight release (#7975/#7982) and master's leastActive reserve/release (#7986) as mutually exclusive branches keyed on the load-balance algorithm. RetryStrategyTest retains all three failover tests (two P2C + one leastActive).
Aias00
left a comment
There was a problem hiding this comment.
Approved. This correctly pairs P2C inflight accounting: select() now increments on the singleton short-circuit path where doSelect is skipped (so the prior increment was missed), and DefaultRetryStrategy releases the failover-picked upstream via doFinally so inflight stays paired across success/failure/cancellation. Tests cover both #7974 and #7975. Solid fix.
What this PR does
Fixes a P2C
inflightaccounting leak on the singleton selection path.AbstractLoadBalancer.selectshort-circuits a candidate list of size 1 and returns the only upstream without callingP2cLoadBalancer.doSelect, so theinflight.incrementAndGet()indoSelectis skipped. Divide's P2C completion callback (DividePlugin.responseTriggerviadoFinally) still decrements the selected upstream unconditionally. Repeated singleton requests therefore driveinflightbelow its baseline (1) and eventually negative, and P2C then uses that negative value inload = lag * inflight.This affects single-upstream deployments and any request where candidate filtering leaves exactly one eligible upstream. The half-open probe path is unaffected (it does its own increment).
The fix overrides
selectinP2cLoadBalancerto perform the same increment +lastPickedupdate thatdoSelectperforms, so start/completion accounting stays paired for a singleton list.Related Issue
Fixes #7974
Related: #7975 (failover retry path, separate PR), #6733 (prior cancellation-cleanup fix)
Type of change
How was this tested
./mvnw test -pl shenyu-loadbalancerpassed (70 tests, no regression)./mvnw checkstyle:check -pl shenyu-loadbalancerpassed (0 violations)Checklist