-
Notifications
You must be signed in to change notification settings - Fork 253
client: pre-encode request before sending to batch-send-loop #1841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: zyguan <zhongyangguan@gmail.com>
Signed-off-by: zyguan <zhongyangguan@gmail.com>
Signed-off-by: zyguan <zhongyangguan@gmail.com>
AndreMouche
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rest LGTM
| return nil | ||
| } | ||
| data := globalEncodedMsgDataPool.Get(req.Cmd.Size()) | ||
| n, err := req.Cmd.MarshalTo(data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible that req.Cmd could being modified concurrently by other goroutines?
maybe we should add some comments for this function for example :
// encodeRequestCmd encodes req.Cmd. The caller must ensure that req is not
// being modified concurrently by other goroutines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, encodeRequestCmd is used in batch client internally. It's called just after tikvrpc.Request.ToBatchCommandsRequest() and before constructing batchCommandsEntry. Note that the req here is the output of ToBatchCommandsRequest, which is a new object of tikvpb.BatchCommandsRequest_Request. So req.Cmd can be only modified inside the batch client by reuseRequestData after the req has been sent. No data race on req.Cmd via public API (SendRequest and SendRequestAsync).
|
|
||
| if err := encodeRequestCmd(batchReq); err != nil { | ||
| cb.Invoke(nil, err) | ||
| return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can early-return via cb.Invoke before entry.cb.Inject(...) is installed at L105, would it be a problem that regionRPC.End()/spanRPC.Finish() won’t run on this path or should we consider move encoding after the inject setup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's OK, if it early-returned, there is no RPC actually, besides, this function shouldn't return error in most of cases.
internal/client/client_batch.go
Outdated
| var globalEncodedMsgDataPool grpc.SharedBufferPool | ||
|
|
||
| func init() { | ||
| globalEncodedMsgDataPool = grpc.NewSharedBufferPool() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a experimental API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and it has been moved to mem package in new version.
| globalEncodedMsgDataPool = grpc.NewSharedBufferPool() | ||
| } | ||
|
|
||
| type encodedBatchCmd struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about implementing isBatchCommandsRequest_Request_Cmd directly to avoid embedding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, you cannot implement an unexported interface method from a different package in golang.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may implement it in kvproto later.
Signed-off-by: zyguan <zhongyangguan@gmail.com>
📝 WalkthroughWalkthroughThis pull request introduces a batch request pre-encoding mechanism with object pooling. It adds an early encoding step to the async batch request flow that converts regular command objects to pre-encoded batch command objects, which are reused from a global pool to reduce per-request memory allocations and ensure validation prior to processing. Changes
Sequence Diagram(s)sequenceDiagram
participant Async as Async Client
participant Encoder as encodeRequestCmd
participant Pool as Encoded Pool
participant Batch as Batch Processor
Async->>Encoder: Convert Cmd to encodedBatchCmd
Encoder->>Pool: Acquire pre-encoded data buffer
Pool-->>Encoder: Return buffer (or error if exhausted)
Encoder->>Encoder: Validate & set encoding state
Encoder-->>Async: Return encodedBatchCmd or error
alt Encoding Success
Async->>Batch: Send batch with pre-encoded command
Batch->>Batch: Marshal and process
Batch->>Pool: Return encoded data (reuseRequestData)
else Encoding Error
Async->>Async: Invoke callback with error & return
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
🧰 Additional context used🧬 Code graph analysis (1)internal/client/client_test.go (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
🔇 Additional comments (18)
✏️ Tip: You can disable this entire section by setting Comment |
|
/retest |
cfzjywxk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: AndreMouche, cfzjywxk The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
[LGTM Timeline notifier]Timeline:
|
|
/cherry-pick tidb-8.5 |
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
|
@zyguan: new pull request created to branch DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Signed-off-by: zyguan <zhongyangguan@gmail.com>
This PR tries to prevent issues like #1839 by pre-encoding request.
The micro-bench results show that the overhead introduced by this PR is negligible. Besides, we can reduce the mem allocs of
AttachContextafter this PR.Summary by CodeRabbit
Bug Fixes
Chores
Tests
✏️ Tip: You can customize this high-level summary in your review settings.