Skip to content

fix(logservice): reuse pooled client for remote truncate - #25291

Closed
jiangxinmeng1 wants to merge 1 commit into
matrixorigin:mainfrom
jiangxinmeng1:fix-logservice-truncate-use-pool-client
Closed

jiangxinmeng1 wants to merge 1 commit into
matrixorigin:mainfrom
jiangxinmeng1:fix-logservice-truncate-use-pool-client

Conversation

@jiangxinmeng1

Copy link
Copy Markdown
Contributor

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #25276

What this PR does / why we need it:

This PR changes truncateFromRemote to borrow an existing client from clientPool instead of creating a new on-the-fly logservice client for every truncate operation.

Previously, remote truncate used GetOnFly(), which created a fresh client each time. When logservice client creation was slow because of unreachable or stale replica addresses, truncate could repeatedly trigger
expensive connection attempts.

Changes:

  • Replace clientPool.GetOnFly() with clientPool.Get() in truncateFromRemote.
  • Return the borrowed client to the pool with the existing Putback() flow.
  • Add a regression test verifying remote truncate does not create an extra client beyond the pool-initialized client.

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Fix remote logservice truncate to reuse pooled client

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Reuse clientPool clients for remote truncation to avoid repeated slow client creation.
• Return borrowed truncate client through existing Putback() path.
• Add regression test ensuring truncate does not create extra clients.
Diagram

graph TD
  A["LogServiceDriver"] --> B["truncateFromRemote"] --> C[("clientPool")] --> D["wrappedClient"] --> E{{"Logservice backend"}}
  C -. "creates during init" .-> F["ClientFactory"]

  subgraph Legend
    direction LR
    _svc["Service/Module"] ~~~ _pool[("Pool")] ~~~ _ext{{"External"}}
  end
Loading
High-Level Assessment

Reusing the existing clientPool via Get() is the simplest and most reliable fix: it prevents repeated expensive client creation under unreachable/stale replicas and aligns truncate with other backend operations already using pooled clients. Alternatives like maintaining a dedicated truncate-only client or enhancing GetOnFly() to cache connections add complexity without clear benefit.

Files changed (2) +34 / -1

Bug fix (1) +1 / -1
truncate.goBorrow client from pool for truncateFromRemote +1/-1

Borrow client from pool for truncateFromRemote

• Switches truncateFromRemote from GetOnFly() (per-call client creation) to Get() (borrow from pool). Keeps the existing deferred Putback() to return the borrowed client to the pool and propagates any Get() error.

pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go

Tests (1) +33 / -0
clientpool_test.goAdd regression test for pooled client usage in remote truncate +33/-0

Add regression test for pooled client usage in remote truncate

• Introduces TestTruncateFromRemoteUsesPooledClient to assert truncateFromRemote does not create additional clients beyond pool initialization. Uses an atomic counter in ClientFactory to validate only one client creation occurs.

pkg/vm/engine/tae/logstore/driver/logservicedriver/clientpool_test.go

ⓘ You are approaching your monthly quota for Qodo. Upgrade your plan

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Action required

1. Truncate path deadlocks 🐞 Bug ☼ Reliability
Description
truncateFromRemote now borrows a pooled client and defers Putback; when Truncate returns
ErrInvalidTruncateLsn it calls getTruncatedPSNFromBackend which borrows from the same pool again.
With ClientMaxCount=1, the second Get waits forever because the only client is still held, hanging
truncation.
Code

pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go[R165-168]

+	if client, err = d.clientPool.Get(); err != nil {
		return
	}
	defer client.Putback()
Evidence
truncateFromRemote holds one pooled client for the entire function (deferred Putback) and, on
ErrInvalidTruncateLsn, calls getTruncatedPSNFromBackend which tries to borrow another client from
the same pool. clientPool.Get blocks when no clients are available, so with a pool size of 1 this
becomes a self-deadlock. The ErrInvalidTruncateLsn path is reachable when the backend’s truncated
LSN is already >= psnIntent.

pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go[139-199]
pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go[203-228]
pkg/vm/engine/tae/logstore/driver/logservicedriver/clientpool.go[328-343]
pkg/vm/engine/tae/logstore/driver/logservicedriver/faker.go[211-219]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`truncateFromRemote` now checks out a pooled client via `clientPool.Get()` and defers `client.Putback()`. If the backend responds with `ErrInvalidTruncateLsn`, the function calls `getTruncatedPSNFromBackend`, which itself calls `clientPool.Get()` again. When `ClientMaxCount == 1`, this re-entrant borrow blocks forever because the first client is still checked out.

### Issue Context
The invalid-truncate branch is reachable when the remote has already truncated past `psnIntent`.

### Fix Focus Areas
- pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go[139-199]
- pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go[203-228]

### Suggested fix
In the `ErrInvalidTruncateLsn` branch, do not call `getTruncatedPSNFromBackend` while holding a pooled client. Instead:
- reuse the already-borrowed `client` to call `client.wrapped.GetTruncatedLsn(loopCtx)` (or extract a helper that accepts a `*wrappedClient`), OR
- `client.Putback()` before calling `getTruncatedPSNFromBackend` and ensure you don’t double-putback.

Add a regression test with `ClientMaxCount=1` where the backend returns `ErrInvalidTruncateLsn` (e.g., pre-set truncated LSN higher than `psnIntent`) and assert `truncateFromRemote` returns without hanging and reports the remote truncated LSN.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ You are approaching your monthly quota for Qodo. Upgrade your plan

Qodo Logo

Comment on lines +165 to 168
if client, err = d.clientPool.Get(); err != nil {
return
}
defer client.Putback()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Action required

1. Truncate path deadlocks 🐞 Bug ☼ Reliability

truncateFromRemote now borrows a pooled client and defers Putback; when Truncate returns
ErrInvalidTruncateLsn it calls getTruncatedPSNFromBackend which borrows from the same pool again.
With ClientMaxCount=1, the second Get waits forever because the only client is still held, hanging
truncation.
Agent Prompt
### Issue description
`truncateFromRemote` now checks out a pooled client via `clientPool.Get()` and defers `client.Putback()`. If the backend responds with `ErrInvalidTruncateLsn`, the function calls `getTruncatedPSNFromBackend`, which itself calls `clientPool.Get()` again. When `ClientMaxCount == 1`, this re-entrant borrow blocks forever because the first client is still checked out.

### Issue Context
The invalid-truncate branch is reachable when the remote has already truncated past `psnIntent`.

### Fix Focus Areas
- pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go[139-199]
- pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.go[203-228]

### Suggested fix
In the `ErrInvalidTruncateLsn` branch, do not call `getTruncatedPSNFromBackend` while holding a pooled client. Instead:
- reuse the already-borrowed `client` to call `client.wrapped.GetTruncatedLsn(loopCtx)` (or extract a helper that accepts a `*wrappedClient`), OR
- `client.Putback()` before calling `getTruncatedPSNFromBackend` and ensure you don’t double-putback.

Add a regression test with `ClientMaxCount=1` where the backend returns `ErrInvalidTruncateLsn` (e.g., pre-set truncated LSN higher than `psnIntent`) and assert `truncateFromRemote` returns without hanging and reports the remote truncated LSN.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

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/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants