Skip to content

fix: pair P2C inflight accounting for singleton upstream selection - #7980

Merged
dmsolr merged 3 commits into
masterfrom
fix/p2c-singleton-inflight
Oct 26, 2026
Merged

dmsolr merged 3 commits into
masterfrom
fix/p2c-singleton-inflight

Conversation

@dmsolr

@dmsolr dmsolr commented Oct 22, 2026

Copy link
Copy Markdown
Member

What this PR does

Fixes a P2C inflight accounting leak on the singleton selection path.

AbstractLoadBalancer.select short-circuits a candidate list of size 1 and returns the only upstream without calling P2cLoadBalancer.doSelect, so the inflight.incrementAndGet() in doSelect is skipped. Divide's P2C completion callback (DividePlugin.responseTrigger via doFinally) still decrements the selected upstream unconditionally. Repeated singleton requests therefore drive inflight below its baseline (1) and eventually negative, and P2C then uses that negative value in load = 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 select in P2cLoadBalancer to perform the same increment + lastPicked update that doSelect performs, 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

  • Bug fix
  • New feature / enhancement
  • Refactor (no functional change)
  • Documentation
  • Chore / build

How was this tested

  • Unit tests added: singleton select increments once; the select + completion-decrement loop from the issue reproduction stays at baseline and never goes negative
  • ./mvnw test -pl shenyu-loadbalancer passed (70 tests, no regression)
  • ./mvnw checkstyle:check -pl shenyu-loadbalancer passed (0 violations)

Checklist

  • Apache 2.0 license header present (no new files)
  • No breaking changes to public API / config / DB schema

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

dmsolr commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

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.

Aias00
Aias00 previously approved these changes Oct 22, 2026

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

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
Aias00 previously approved these changes Oct 23, 2026

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

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.

@yuluo-yx

Copy link
Copy Markdown
Member

hi @dmsolr please resolve the conflicts, thanks

@yuluo-yx

Copy link
Copy Markdown
Member

/hold

@github-actions github-actions Bot added the hold On hold, do not merge label Oct 24, 2026
@Aias00

Aias00 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

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

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

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.

@dmsolr
dmsolr merged commit 052a036 into master Oct 26, 2026
32 checks passed
@yuluo-yx
yuluo-yx deleted the fix/p2c-singleton-inflight branch November 4, 2026 15:14
@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

hold On hold, do not merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] P2C inflight becomes negative when only one upstream is eligible

3 participants