fix: pair P2C inflight accounting for singleton upstream selection #7980
Heads-up: #7982 (Fixes #7975) is stacked on this branch, since the failover retry cleanup must also release the singleton-select increment added here. Merge this first and #7982 will retarget to master automatically.
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.select short-circuits before doSelect when upstreamList.size() == 1 (shenyu-loadbalancer/.../spi/AbstractLoadBalancer.java: empty → null, single → upstreamList.get(0), otherwise doSelect). So P2cLoadBalancer.doSelect — the only place that does picked.getInflight().incrementAndGet() — never runs for a singleton list, while DividePlugin'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.select only reaches doSelect for size() > 1, and the new block is gated on upstreamList.size() == 1, so the two increments are mutually exclusive.
Objects.nonNull(picked) also covers the empty-list case, where super.select returns null.
testSingletonSelectionStaysPairedWithCompletionDecrement models 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
1. 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 reaches P2cLoadBalancer.select with 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.
2. 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.
3. size() == 1 is used as a proxy for "doSelect did not run". That is true today because AbstractLoadBalancer is the only entry point; if select is 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.
Approving the accounting fix; the branch needs a rebase before it can merge.
P2cLoadBalancer.select pairing the singleton increment is correct: AbstractLoadBalancer.select returns before doSelect for a single candidate, so without this the completion callback drove inflight negative. setLastPicked is also refreshed, matching the doSelect path.DefaultRetryStrategy.resend releasing the retry upstream on doFinally covers 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 (mergeable: false).
Please rebase onto current master and push. I'll merge as soon as the PR reports clean again.
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.
Aias00
dmsolr
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