Skip to content

fix(logservice): prevent connection ID refill convoys - #27119

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/issue-27114-connection-id-refill
Aug 13, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/issue-27114-connection-id-refill

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #27114

What this PR does / why we need it:

The failing debug job admitted exactly 92 of 1,000 Sysbench connections and
rejected the remaining 908. That matches the 92 IDs left in the local
100-ID connection batch. The rejected connections produced 908
failed to get connection ID from HAKeeper errors.

The apparent 5.28-second HAKeeper outage was mostly a client-side convoy:

  1. AllocateIDByKeyWithBatch held the managed client's single mutex across
    cached allocation, HAKeeper discovery, and the refill RPC.
  2. The first refill exhausted getConnID's hard-coded two-second deadline.
  3. Hundreds of callers had already passed context validation and then waited
    behind that mutex until their deadlines expired.
  4. A stale waiter could consequently reach MORPC with an expired deadline.
    MORPC returned invalid input: timeout has invalid deadline; the managed
    client treated that local error as a broken shared transport and reset it.
  5. The remaining stale waiters then serialized HAKeeper rediscovery under the
    same mutex. The job recorded 880 isHAKeeper:false results while that queue
    drained.

This change separates ID-batch state from managed-client state and gives each
allocation key its own refill coordination:

  • one caller refills an empty batch while same-key waiters wait on a refill
    channel with their own contexts;
  • cached IDs and other keys no longer wait behind a slow refill RPC;
  • a failed refiller wakes waiters so a still-valid caller can take over;
  • client snapshots are used outside the allocation lock and resets remain
    generation-safe;
  • expired contexts are rejected before discovery/RPC; and
  • connection-ID allocation uses the configured frontend ConnectTimeout
    (60 seconds by default) instead of an unrelated hard-coded two seconds.

The same refill primitive is used by unkeyed allocation so that path cannot
reintroduce cross-key blocking through the shared client mutex. No public API,
wire format, persistent state, or server-side ID allocation semantics change.

Validation

The deterministic regressions failed on the unmodified implementation:

  • a same-key waiter remained blocked after its 20 ms context expired and then
    incorrectly consumed an ID after the leader completed;
  • getConnID exposed a roughly two-second deadline despite a configured
    17-second connection timeout.

The fixed implementation passes:

  • 1,000 concurrent connection allocations starting with 92 cached IDs:
    1,000 unique contiguous IDs and exactly 10 refill RPCs;
  • context cancellation while waiting for a refill;
  • rejection of an already-expired context before RPC;
  • cached allocation for another key during a blocked refill;
  • waiter takeover after the refill owner fails;
  • client reuse after caller-scoped context errors;
  • complete batch-boundary consumption; and
  • propagation of the configured frontend connection timeout.

Commands/results:

  • focused regressions: pass;
  • every focused regression with -race -count=100: pass;
  • pkg/logservice owning-package tests: pass in 134.665s, excluding the
    independently reproduced baseline failure noted below;
  • pkg/frontend owning-package tests: pass in 16.418s;
  • pkg/logservice owning-package race tests: pass in 140.629s, with the same
    baseline exclusion;
  • pkg/frontend owning-package race tests: pass in 25.105s;
  • go build -mod=readonly ./pkg/logservice ./pkg/frontend: pass;
  • go vet -mod=readonly ./pkg/logservice ./pkg/frontend: pass; and
  • git diff --check: pass.

TestNewServiceClosesStoreOnReplicaStartFailure is currently a deterministic,
unrelated failure on this environment. On the clean main base
bdbd613fdece966769eb68481a3e58bfbc36b30c, the exact test failed 10/10 with
the same service_test.go:171: Expected nil, but got *logservice.Service
signature. This PR does not touch service construction or that test; it was the
only exclusion from the two package-level pkg/logservice runs above.

Risk

The normal cached path adds only a per-key mutex; refill RPCs are still
single-flight per key. The change intentionally allows independent keys to
allocate concurrently, which HAKeeper already has to support across distinct
clients and CNs. Refill channels are per generation, owned and closed exactly
once by the elected refiller, and no background goroutine or new cleanup owner
is introduced.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-review verdict: LGTM on exact head 6177edf (GitHub does not allow the author account to submit APPROVE on its own PR).

I traced the per-key allocation state machine, refill ownership, waiter cancellation/takeover, shared-client generation reset, frontend timeout propagation, and Q1-Q3 resource/wait/growth paths. The elected refiller is the sole channel closer; every normal success/error path wakes waiters; cancellation is independent of the refill RPC; failed refill ownership is retryable by a valid waiter; and a stale client failure cannot reset a replacement generation. ID uniqueness and full batch-boundary consumption are preserved, while cached allocations and unrelated keys no longer queue behind a slow refill.

I found no production-reachable correctness, availability, performance, or lifecycle blocker. MatrixOne UT/SCA/BVT/coverage checks are green. I deliberately did not rerun heavy local UT/BVT.

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

Deep-reviewed commit 6177edf.

The per-key refill state machine has a single refill owner, publishes the batch before waking waiters, lets independently-cancelled waiters exit or take over after owner failure, and keeps cached allocations for other keys off the refill/client locks. Client invalidation is generation-scoped, so stale failures cannot reset a replacement client. The frontend allocation deadline is now aligned with the configured connection/handshake timeout.

Verified the five new logservice concurrency/deadline tests and the frontend timeout propagation test with -race -count=100; all passed. No blocking findings.

@mergify mergify Bot added the queued label Aug 13, 2026
@mergify

mergify Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-13 15:27 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue — 2026-08-13 15:28 UTC · at 6177edfdda199adca7b536fb7d224258e3852423

This pull request spent 19 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/iceberg-connector.yml without workflows permission

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@mergify mergify Bot removed the queued label Aug 13, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…7119)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#27114

## What this PR does / why we need it:

The failing debug job admitted exactly 92 of 1,000 Sysbench connections
and
rejected the remaining 908. That matches the 92 IDs left in the local
100-ID connection batch. The rejected connections produced 908
`failed to get connection ID from HAKeeper` errors.

The apparent 5.28-second HAKeeper outage was mostly a client-side
convoy:

1. `AllocateIDByKeyWithBatch` held the managed client's single mutex
across
   cached allocation, HAKeeper discovery, and the refill RPC.
2. The first refill exhausted `getConnID`'s hard-coded two-second
deadline.
3. Hundreds of callers had already passed context validation and then
waited
   behind that mutex until their deadlines expired.
4. A stale waiter could consequently reach MORPC with an expired
deadline.
MORPC returned `invalid input: timeout has invalid deadline`; the
managed
client treated that local error as a broken shared transport and reset
it.
5. The remaining stale waiters then serialized HAKeeper rediscovery
under the
same mutex. The job recorded 880 `isHAKeeper:false` results while that
queue
   drained.

This change separates ID-batch state from managed-client state and gives
each
allocation key its own refill coordination:

- one caller refills an empty batch while same-key waiters wait on a
refill
  channel with their own contexts;
- cached IDs and other keys no longer wait behind a slow refill RPC;
- a failed refiller wakes waiters so a still-valid caller can take over;
- client snapshots are used outside the allocation lock and resets
remain
  generation-safe;
- expired contexts are rejected before discovery/RPC; and
- connection-ID allocation uses the configured frontend `ConnectTimeout`
(60 seconds by default) instead of an unrelated hard-coded two seconds.

The same refill primitive is used by unkeyed allocation so that path
cannot
reintroduce cross-key blocking through the shared client mutex. No
public API,
wire format, persistent state, or server-side ID allocation semantics
change.

## Validation

The deterministic regressions failed on the unmodified implementation:

- a same-key waiter remained blocked after its 20 ms context expired and
then
  incorrectly consumed an ID after the leader completed;
- `getConnID` exposed a roughly two-second deadline despite a configured
  17-second connection timeout.

The fixed implementation passes:

- 1,000 concurrent connection allocations starting with 92 cached IDs:
  1,000 unique contiguous IDs and exactly 10 refill RPCs;
- context cancellation while waiting for a refill;
- rejection of an already-expired context before RPC;
- cached allocation for another key during a blocked refill;
- waiter takeover after the refill owner fails;
- client reuse after caller-scoped context errors;
- complete batch-boundary consumption; and
- propagation of the configured frontend connection timeout.

Commands/results:

- focused regressions: pass;
- every focused regression with `-race -count=100`: pass;
- `pkg/logservice` owning-package tests: pass in 134.665s, excluding the
  independently reproduced baseline failure noted below;
- `pkg/frontend` owning-package tests: pass in 16.418s;
- `pkg/logservice` owning-package race tests: pass in 140.629s, with the
same
  baseline exclusion;
- `pkg/frontend` owning-package race tests: pass in 25.105s;
- `go build -mod=readonly ./pkg/logservice ./pkg/frontend`: pass;
- `go vet -mod=readonly ./pkg/logservice ./pkg/frontend`: pass; and
- `git diff --check`: pass.

`TestNewServiceClosesStoreOnReplicaStartFailure` is currently a
deterministic,
unrelated failure on this environment. On the clean `main` base
`bdbd613fdece966769eb68481a3e58bfbc36b30c`, the exact test failed 10/10
with
the same `service_test.go:171: Expected nil, but got
*logservice.Service`
signature. This PR does not touch service construction or that test; it
was the
only exclusion from the two package-level `pkg/logservice` runs above.

## Risk

The normal cached path adds only a per-key mutex; refill RPCs are still
single-flight per key. The change intentionally allows independent keys
to
allocate concurrently, which HAKeeper already has to support across
distinct
clients and CNs. Refill channels are per generation, owned and closed
exactly
once by the elected refiller, and no background goroutine or new cleanup
owner
is introduced.

Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…7119)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#27114

## What this PR does / why we need it:

The failing debug job admitted exactly 92 of 1,000 Sysbench connections
and
rejected the remaining 908. That matches the 92 IDs left in the local
100-ID connection batch. The rejected connections produced 908
`failed to get connection ID from HAKeeper` errors.

The apparent 5.28-second HAKeeper outage was mostly a client-side
convoy:

1. `AllocateIDByKeyWithBatch` held the managed client's single mutex
across
   cached allocation, HAKeeper discovery, and the refill RPC.
2. The first refill exhausted `getConnID`'s hard-coded two-second
deadline.
3. Hundreds of callers had already passed context validation and then
waited
   behind that mutex until their deadlines expired.
4. A stale waiter could consequently reach MORPC with an expired
deadline.
MORPC returned `invalid input: timeout has invalid deadline`; the
managed
client treated that local error as a broken shared transport and reset
it.
5. The remaining stale waiters then serialized HAKeeper rediscovery
under the
same mutex. The job recorded 880 `isHAKeeper:false` results while that
queue
   drained.

This change separates ID-batch state from managed-client state and gives
each
allocation key its own refill coordination:

- one caller refills an empty batch while same-key waiters wait on a
refill
  channel with their own contexts;
- cached IDs and other keys no longer wait behind a slow refill RPC;
- a failed refiller wakes waiters so a still-valid caller can take over;
- client snapshots are used outside the allocation lock and resets
remain
  generation-safe;
- expired contexts are rejected before discovery/RPC; and
- connection-ID allocation uses the configured frontend `ConnectTimeout`
(60 seconds by default) instead of an unrelated hard-coded two seconds.

The same refill primitive is used by unkeyed allocation so that path
cannot
reintroduce cross-key blocking through the shared client mutex. No
public API,
wire format, persistent state, or server-side ID allocation semantics
change.

## Validation

The deterministic regressions failed on the unmodified implementation:

- a same-key waiter remained blocked after its 20 ms context expired and
then
  incorrectly consumed an ID after the leader completed;
- `getConnID` exposed a roughly two-second deadline despite a configured
  17-second connection timeout.

The fixed implementation passes:

- 1,000 concurrent connection allocations starting with 92 cached IDs:
  1,000 unique contiguous IDs and exactly 10 refill RPCs;
- context cancellation while waiting for a refill;
- rejection of an already-expired context before RPC;
- cached allocation for another key during a blocked refill;
- waiter takeover after the refill owner fails;
- client reuse after caller-scoped context errors;
- complete batch-boundary consumption; and
- propagation of the configured frontend connection timeout.

Commands/results:

- focused regressions: pass;
- every focused regression with `-race -count=100`: pass;
- `pkg/logservice` owning-package tests: pass in 134.665s, excluding the
  independently reproduced baseline failure noted below;
- `pkg/frontend` owning-package tests: pass in 16.418s;
- `pkg/logservice` owning-package race tests: pass in 140.629s, with the
same
  baseline exclusion;
- `pkg/frontend` owning-package race tests: pass in 25.105s;
- `go build -mod=readonly ./pkg/logservice ./pkg/frontend`: pass;
- `go vet -mod=readonly ./pkg/logservice ./pkg/frontend`: pass; and
- `git diff --check`: pass.

`TestNewServiceClosesStoreOnReplicaStartFailure` is currently a
deterministic,
unrelated failure on this environment. On the clean `main` base
`bdbd613fdece966769eb68481a3e58bfbc36b30c`, the exact test failed 10/10
with
the same `service_test.go:171: Expected nil, but got
*logservice.Service`
signature. This PR does not touch service construction or that test; it
was the
only exclusion from the two package-level `pkg/logservice` runs above.

## Risk

The normal cached path adds only a per-key mutex; refill RPCs are still
single-flight per key. The change intentionally allows independent keys
to
allocate concurrently, which HAKeeper already has to support across
distinct
clients and CNs. Refill channels are per generation, owned and closed
exactly
once by the elected refiller, and no background goroutine or new cleanup
owner
is introduced.

Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…7119)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#27114

## What this PR does / why we need it:

The failing debug job admitted exactly 92 of 1,000 Sysbench connections
and
rejected the remaining 908. That matches the 92 IDs left in the local
100-ID connection batch. The rejected connections produced 908
`failed to get connection ID from HAKeeper` errors.

The apparent 5.28-second HAKeeper outage was mostly a client-side
convoy:

1. `AllocateIDByKeyWithBatch` held the managed client's single mutex
across
   cached allocation, HAKeeper discovery, and the refill RPC.
2. The first refill exhausted `getConnID`'s hard-coded two-second
deadline.
3. Hundreds of callers had already passed context validation and then
waited
   behind that mutex until their deadlines expired.
4. A stale waiter could consequently reach MORPC with an expired
deadline.
MORPC returned `invalid input: timeout has invalid deadline`; the
managed
client treated that local error as a broken shared transport and reset
it.
5. The remaining stale waiters then serialized HAKeeper rediscovery
under the
same mutex. The job recorded 880 `isHAKeeper:false` results while that
queue
   drained.

This change separates ID-batch state from managed-client state and gives
each
allocation key its own refill coordination:

- one caller refills an empty batch while same-key waiters wait on a
refill
  channel with their own contexts;
- cached IDs and other keys no longer wait behind a slow refill RPC;
- a failed refiller wakes waiters so a still-valid caller can take over;
- client snapshots are used outside the allocation lock and resets
remain
  generation-safe;
- expired contexts are rejected before discovery/RPC; and
- connection-ID allocation uses the configured frontend `ConnectTimeout`
(60 seconds by default) instead of an unrelated hard-coded two seconds.

The same refill primitive is used by unkeyed allocation so that path
cannot
reintroduce cross-key blocking through the shared client mutex. No
public API,
wire format, persistent state, or server-side ID allocation semantics
change.

## Validation

The deterministic regressions failed on the unmodified implementation:

- a same-key waiter remained blocked after its 20 ms context expired and
then
  incorrectly consumed an ID after the leader completed;
- `getConnID` exposed a roughly two-second deadline despite a configured
  17-second connection timeout.

The fixed implementation passes:

- 1,000 concurrent connection allocations starting with 92 cached IDs:
  1,000 unique contiguous IDs and exactly 10 refill RPCs;
- context cancellation while waiting for a refill;
- rejection of an already-expired context before RPC;
- cached allocation for another key during a blocked refill;
- waiter takeover after the refill owner fails;
- client reuse after caller-scoped context errors;
- complete batch-boundary consumption; and
- propagation of the configured frontend connection timeout.

Commands/results:

- focused regressions: pass;
- every focused regression with `-race -count=100`: pass;
- `pkg/logservice` owning-package tests: pass in 134.665s, excluding the
  independently reproduced baseline failure noted below;
- `pkg/frontend` owning-package tests: pass in 16.418s;
- `pkg/logservice` owning-package race tests: pass in 140.629s, with the
same
  baseline exclusion;
- `pkg/frontend` owning-package race tests: pass in 25.105s;
- `go build -mod=readonly ./pkg/logservice ./pkg/frontend`: pass;
- `go vet -mod=readonly ./pkg/logservice ./pkg/frontend`: pass; and
- `git diff --check`: pass.

`TestNewServiceClosesStoreOnReplicaStartFailure` is currently a
deterministic,
unrelated failure on this environment. On the clean `main` base
`bdbd613fdece966769eb68481a3e58bfbc36b30c`, the exact test failed 10/10
with
the same `service_test.go:171: Expected nil, but got
*logservice.Service`
signature. This PR does not touch service construction or that test; it
was the
only exclusion from the two package-level `pkg/logservice` runs above.

## Risk

The normal cached path adds only a per-key mutex; refill RPCs are still
single-flight per key. The change intentionally allows independent keys
to
allocate concurrently, which HAKeeper already has to support across
distinct
clients and CNs. Refill channels are per generation, owned and closed
exactly
once by the elected refiller, and no background goroutine or new cleanup
owner
is introduced.

Co-authored-by: XuPeng-SH <[email protected]>

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: burst connections fail when HAKeeper connection-ID batch refill times out

3 participants