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

tikv: avoid switch peer when batchRequest be cancelled (#10822) #10850

Merged
merged 2 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions store/tikv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ import (
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
"google.golang.org/grpc"
gcodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
gstatus "google.golang.org/grpc/status"
)

// MaxSendMsgSize set max gRPC request message size sent to server. If any request message size is larger than
Expand Down Expand Up @@ -618,8 +616,9 @@ func sendBatchRequest(
select {
case connArray.batchCommandsCh <- entry:
case <-ctx1.Done():
logutil.Logger(context.Background()).Warn("send request is timeout", zap.String("to", addr))
return nil, errors.Trace(gstatus.Error(gcodes.DeadlineExceeded, "Canceled or timeout"))
logutil.Logger(context.Background()).Warn("send request is cancelled",
zap.String("to", addr), zap.String("cause", ctx1.Err().Error()))
return nil, errors.Trace(ctx1.Err())
}

select {
Expand All @@ -630,8 +629,9 @@ func sendBatchRequest(
return tikvrpc.FromBatchCommandsResponse(res), nil
case <-ctx1.Done():
atomic.StoreInt32(&entry.canceled, 1)
logutil.Logger(context.Background()).Warn("send request is canceled", zap.String("to", addr))
return nil, errors.Trace(gstatus.Error(gcodes.DeadlineExceeded, "Canceled or timeout"))
logutil.Logger(context.Background()).Warn("wait response is cancelled",
zap.String("to", addr), zap.String("cause", ctx1.Err().Error()))
return nil, errors.Trace(ctx1.Err())
}
}

Expand Down
16 changes: 16 additions & 0 deletions store/tikv/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
package tikv

import (
"context"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/tikvpb"
"github.com/pingcap/tidb/config"
)
Expand Down Expand Up @@ -77,3 +80,16 @@ func (s *testClientSuite) TestRemoveCanceledRequests(c *C) {
newEntryPtr := &entries[0]
c.Assert(entryPtr, Equals, newEntryPtr)
}

func (s *testClientSuite) TestCancelTimeoutRetErr(c *C) {
req := new(tikvpb.BatchCommandsRequest_Request)
a := &connArray{batchCommandsCh: make(chan *batchCommandsEntry, 1)}

ctx, cancel := context.WithCancel(context.TODO())
cancel()
_, err := sendBatchRequest(ctx, "", a, req, 2*time.Second)
c.Assert(errors.Cause(err), Equals, context.Canceled)

_, err = sendBatchRequest(context.Background(), "", a, req, 0)
c.Assert(errors.Cause(err), Equals, context.DeadlineExceeded)
}