Skip to content

Commit

Permalink
tikvrpc: avoid attaching ctx twice
Browse files Browse the repository at this point in the history
Signed-off-by: zyguan <[email protected]>
  • Loading branch information
zyguan committed Dec 18, 2024
1 parent 05d115b commit 6bf98cf
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/locate/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,9 @@ func (s *RegionRequestSender) SendReqCtx(
if req.InputRequestSource != "" && s.replicaSelector != nil {
patchRequestSource(req, s.replicaSelector.replicaType())
}
if e := tikvrpc.SetContext(req, rpcCtx.Meta, rpcCtx.Peer); e != nil {
// Since RPCClient.SendRequest will call AttachContext (when EncodeRequest) to attach the context to the
// request, here we just update the req.Context to avoid attaching the ctx twice.
if e := tikvrpc.SetContextNoAttach(req, rpcCtx.Meta, rpcCtx.Peer); e != nil {
return nil, nil, retryTimes, err
}
if s.replicaSelector != nil {
Expand Down
91 changes: 91 additions & 0 deletions tikvrpc/cmds_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tikvrpc/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,25 @@ cat <<EOF >> $output
return true
}
EOF

cat <<EOF >> $output
func isValidReqType(cmd CmdType) bool {
switch cmd {
EOF

for cmd in "${cmds[@]}"; do
cat <<EOF >> $output
case Cmd${cmd}:
return true
EOF
done

cat <<EOF >> $output
case CmdCopStream, CmdMPPTask, CmdMPPConn, CmdMPPCancel, CmdMPPAlive, CmdEmpty:
return true
default:
return false
}
}
EOF
13 changes: 13 additions & 0 deletions tikvrpc/tikvrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,19 @@ func SetContext(req *Request, region *metapb.Region, peer *metapb.Peer) error {
return nil
}

// SetContextNoAttach likes SetContext, but it doesn't attach the context to the underlying request.
func SetContextNoAttach(req *Request, region *metapb.Region, peer *metapb.Peer) error {
if !isValidReqType(req.Type) {
return errors.Errorf("invalid request type %v", req.Type)
}
if region != nil {
req.Context.RegionId = region.Id
req.Context.RegionEpoch = region.RegionEpoch
}
req.Context.Peer = peer
return nil
}

// GenRegionErrorResp returns corresponding Response with specified RegionError
// according to the given req.
func GenRegionErrorResp(req *Request, e *errorpb.Error) (*Response, error) {
Expand Down

0 comments on commit 6bf98cf

Please sign in to comment.