Skip to content
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

tikvrpc: avoid attaching ctx twice #1528

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/locate/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,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 @@ -818,6 +818,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
Loading