← Back to Pull requests

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

Merged
dmsolr merged 4 files (125 lines) on Oct 26, 2026 05:55 UTC
View on GitHub
dmsolr dmsolr commented on Oct 22, 2026 07:09 UTC
Author

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
  • Diff: 125 lines (+119 / -6) across 4 files Reviewer: Aias00 Review duration: 5685m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.36) × (1 − Review Confidence (0.80)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.36 · 125 lines diff across 4 files
    0.36
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.75 · 4 substantive comment(s)
    0.75
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    79%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.289
    Real PR Conversation Timeline
    8 items
    dmsolr dmsolr commented on Oct 22, 2026 07:40 UTC
    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 commented on Oct 22, 2026 10:52 UTC
    Reviewer

    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.

    Aias00 Aias00 commented on Oct 23, 2026 06:16 UTC
    Reviewer

    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 yuluo-yx commented on Oct 24, 2026 03:34 UTC
    Contributor

    hi @dmsolr please resolve the conflicts, thanks

    yuluo-yx yuluo-yx commented on Oct 24, 2026 03:38 UTC
    Contributor

    /hold

    Aias00 Aias00 commented on Oct 24, 2026 07:48 UTC
    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.

    Aias00 Aias00 approved these changes on Oct 26, 2026 05:27 UTC
    Approved Reviewer

    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.

    Reviewers
    Assignees
    dmsolr dmsolr
    Labels
    risk: low confidence: 79.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 36.3% (MEDIUM) · Higher = larger blast radius
    36.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 79.8% · Lower = rushed or shallow review
    79.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.3% · Safe to merge
    7.3%
    Repository