Skip to content

Commit

Permalink
Merge pull request pingcap#7 from PatrickNicholas/2020-hackathon
Browse files Browse the repository at this point in the history
store: fix replica read type for adaptive follower read
  • Loading branch information
zhangjinpeng87 authored Jan 16, 2021
2 parents f268519 + 2e7ecb4 commit a0338d1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion executor/table_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,12 @@ func (e *TableReaderExecutor) buildResp(ctx context.Context, ranges []*ranger.Ra
factor := e.ctx.GetSessionVars().NetworkFactor
totalCost := 0.0
for _, plan := range e.plans {
totalCost += plan.StatsCount()*factor +
totalCost += plan.StatsCount() * factor *
plan.Stats().HistColl.GetAvgRowSize(e.ctx, plan.Schema().Columns, false, true)
}

logutil.Eventf(ctx, "table scan %s, total cost: %d",
e.table.Meta().Name.L, totalCost)
estimater := func(r *kv.KeyRange, totalRanges []kv.KeyRange) float64 {
return totalCost * 1.0 / (float64(len(totalRanges)) + 1.0)
}
Expand Down
2 changes: 1 addition & 1 deletion store/tikv/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (s *RegionRequestSender) getRPCContext(
if req.RecommendLocalScan {
// We need to avoid the affection of forced replica read type here, so that the
// AZ/DC label matching could take effect.
return s.regionCache.GetTiKVRPCContext(bo, regionID, kv.ReplicaReadMixed, seed, WithMatchAZ())
return s.regionCache.GetTiKVRPCContext(bo, regionID, req.ReplicaReadType, seed, WithMatchAZ())
} else {
return s.regionCache.GetTiKVRPCContext(bo, regionID, req.ReplicaReadType, seed)
}
Expand Down
2 changes: 1 addition & 1 deletion store/tikv/region_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *testRegionRequestToThreeStoresSuite) TestGetRPCContext(c *C) {
var seed uint32 = 0
var regionID = RegionVerID{s.regionID, 0, 0}

req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdGet, &kvrpcpb.GetRequest{}, kv.ReplicaReadLeader, &seed)
req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdGet, &kvrpcpb.GetRequest{}, kv.ReplicaReadLeader, &seed, false)
rpcCtx, err := s.regionRequestSender.getRPCContext(s.bo, req, regionID, kv.TiKV)
c.Assert(err, IsNil)
c.Assert(rpcCtx.Peer.Id, Equals, s.leaderPeer)
Expand Down
7 changes: 6 additions & 1 deletion store/tikv/tikvrpc/tikvrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,15 @@ func NewRequest(typ CmdType, pointer interface{}, ctxs ...kvrpcpb.Context) *Requ
// NewReplicaReadRequest returns new kv rpc request with replica read.
func NewReplicaReadRequest(typ CmdType, pointer interface{}, replicaReadType kv.ReplicaReadType, replicaReadSeed *uint32, recommendLocalScan bool, ctxs ...kvrpcpb.Context) *Request {
req := NewRequest(typ, pointer, ctxs...)
if replicaReadType == kv.ReplicaReadLeader && recommendLocalScan {
// We need to avoid the affection of forced replica read type here, so that the
// AZ/DC label matching could take effect.
replicaReadType = kv.ReplicaReadMixed
req.RecommendLocalScan = true
}
req.ReplicaRead = replicaReadType.IsFollowerRead()
req.ReplicaReadType = replicaReadType
req.ReplicaReadSeed = replicaReadSeed
req.RecommendLocalScan = recommendLocalScan
return req
}

Expand Down

0 comments on commit a0338d1

Please sign in to comment.