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

store: use RLock when reading snapshot.replicaRead snapshot.taskID (#21627) #21632

Merged
merged 2 commits into from
Dec 10, 2020
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/pingcap/kvproto v0.0.0-20200907074027-32a3a0accf7d
github.com/pingcap/log v0.0.0-20201112100606-8f1e84a3abc8
github.com/pingcap/parser v0.0.0-20201130080042-c3ddfec58248
github.com/pingcap/sysutil v0.0.0-20201130064824-f0c8aa6a6966
github.com/pingcap/sysutil v0.0.0-20201130064824-f0c8aa6a6966
github.com/pingcap/tidb-tools v4.0.6-0.20200828085514-03575b185007+incompatible
github.com/pingcap/tipb v0.0.0-20200618092958-4fad48b4c8c3
github.com/prometheus/client_golang v1.5.1
Expand Down
6 changes: 4 additions & 2 deletions store/tikv/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,13 @@ func (s *Scanner) getData(bo *Backoffer) error {
sreq.EndKey = reqStartKey
sreq.Reverse = true
}
req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdScan, sreq, s.snapshot.replicaRead, &s.snapshot.replicaReadSeed, pb.Context{
s.snapshot.mu.RLock()
req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdScan, sreq, s.snapshot.mu.replicaRead, &s.snapshot.replicaReadSeed, pb.Context{
Priority: s.snapshot.priority,
NotFillCache: s.snapshot.notFillCache,
TaskId: s.snapshot.taskID,
TaskId: s.snapshot.mu.taskID,
})
s.snapshot.mu.RUnlock()
resp, err := sender.SendReq(bo, req, loc.Region, ReadTimeoutMedium)
if err != nil {
return errors.Trace(err)
Expand Down
31 changes: 17 additions & 14 deletions store/tikv/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ type tikvSnapshot struct {
syncLog bool
keyOnly bool
vars *kv.Variables
replicaRead kv.ReplicaReadType
replicaReadSeed uint32
taskID uint64
minCommitTSPushed

// Cache the result of BatchGet.
Expand All @@ -73,9 +71,11 @@ type tikvSnapshot struct {
// It's OK as long as there are no zero-byte values in the protocol.
mu struct {
sync.RWMutex
cached map[string][]byte
cachedSize int
stats *SnapshotRuntimeStats
cached map[string][]byte
cachedSize int
stats *SnapshotRuntimeStats
replicaRead kv.ReplicaReadType
taskID uint64
}
}

Expand Down Expand Up @@ -275,14 +275,16 @@ func (s *tikvSnapshot) batchGetSingleRegion(bo *Backoffer, batch batchKeys, coll

pending := batch.keys
for {
s.mu.RLock()
req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdBatchGet, &pb.BatchGetRequest{
Keys: pending,
Version: s.version.Ver,
}, s.replicaRead, &s.replicaReadSeed, pb.Context{
}, s.mu.replicaRead, &s.replicaReadSeed, pb.Context{
Priority: s.priority,
NotFillCache: s.notFillCache,
TaskId: s.taskID,
TaskId: s.mu.taskID,
})
s.mu.RUnlock()

resp, _, _, err := cli.SendReqCtx(bo, req, batch.region, ReadTimeoutMedium, kv.TiKV, "")

Expand Down Expand Up @@ -400,17 +402,16 @@ func (s *tikvSnapshot) get(ctx context.Context, bo *Backoffer, k kv.Key) ([]byte
s.mergeRegionRequestStats(cli.Stats)
}()
}
s.mu.RUnlock()

req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdGet,
&pb.GetRequest{
Key: k,
Version: s.version.Ver,
}, s.replicaRead, &s.replicaReadSeed, pb.Context{
}, s.mu.replicaRead, &s.replicaReadSeed, pb.Context{
Priority: s.priority,
NotFillCache: s.notFillCache,
TaskId: s.taskID,
TaskId: s.mu.taskID,
})
s.mu.RUnlock()
for {
loc, err := s.store.regionCache.LocateKey(bo, k)
if err != nil {
Expand Down Expand Up @@ -475,13 +476,13 @@ func (s *tikvSnapshot) SetOption(opt kv.Option, val interface{}) {
switch opt {
case kv.ReplicaRead:
s.mu.Lock()
s.replicaRead = val.(kv.ReplicaReadType)
s.mu.replicaRead = val.(kv.ReplicaReadType)
s.mu.Unlock()
case kv.Priority:
s.priority = kvPriorityToCommandPri(val.(int))
case kv.TaskID:
s.mu.Lock()
s.taskID = val.(uint64)
s.mu.taskID = val.(uint64)
s.mu.Unlock()
case kv.CollectRuntimeStats:
s.mu.Lock()
Expand All @@ -494,7 +495,9 @@ func (s *tikvSnapshot) SetOption(opt kv.Option, val interface{}) {
func (s *tikvSnapshot) DelOption(opt kv.Option) {
switch opt {
case kv.ReplicaRead:
s.replicaRead = kv.ReplicaReadLeader
s.mu.Lock()
s.mu.replicaRead = kv.ReplicaReadLeader
s.mu.Unlock()
case kv.CollectRuntimeStats:
s.mu.Lock()
s.mu.stats = nil
Expand Down