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

session: support leader-and-follower for tidb_replica_read #14761

Merged
merged 12 commits into from
Feb 14, 2020
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
6 changes: 3 additions & 3 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ const (
ReplicaReadLeader ReplicaReadType = 1 << iota
// ReplicaReadFollower stands for 'read from follower'.
ReplicaReadFollower
// ReplicaReadLearner stands for 'read from learner'.
ReplicaReadLearner
// ReplicaReadMixed stands for 'read from leader and follower and learner'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lysu @hicqu Seems the comment is not correct.

ReplicaReadMixed
)

// IsFollowerRead checks if leader is going to be used to read data.
func (r ReplicaReadType) IsFollowerRead() bool {
return r == ReplicaReadFollower
return r != ReplicaReadLeader
}

// Those limits is enforced to make sure the transaction can be well handled by TiKV.
Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
case TiDBReplicaRead:
if strings.EqualFold(val, "follower") {
s.SetReplicaRead(kv.ReplicaReadFollower)
} else if strings.EqualFold(val, "leader-and-follower") {
s.SetReplicaRead(kv.ReplicaReadMixed)
} else if strings.EqualFold(val, "leader") || len(val) == 0 {
s.SetReplicaRead(kv.ReplicaReadLeader)
}
Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
case TiDBReplicaRead:
if strings.EqualFold(value, "follower") {
return "follower", nil
} else if strings.EqualFold(value, "leader-and-follower") {
return "leader-and-follower", nil
} else if strings.EqualFold(value, "leader") || len(value) == 0 {
return "leader", nil
}
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/varsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) {
c.Assert(err, IsNil)
c.Assert(val, Equals, "leader")
c.Assert(v.GetReplicaRead(), Equals, kv.ReplicaReadLeader)
SetSessionSystemVar(v, TiDBReplicaRead, types.NewStringDatum("leader-and-follower"))
val, err = GetSessionSystemVar(v, TiDBReplicaRead)
c.Assert(err, IsNil)
c.Assert(val, Equals, "leader-and-follower")
c.Assert(v.GetReplicaRead(), Equals, kv.ReplicaReadMixed)

SetSessionSystemVar(v, TiDBEnableStmtSummary, types.NewStringDatum("on"))
val, err = GetSessionSystemVar(v, TiDBEnableStmtSummary)
Expand Down
32 changes: 32 additions & 0 deletions store/tikv/region_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ func (r *RegionStore) follower(seed uint32) int32 {
return r.workTiKVIdx
}

// return next leader or follower store's index
func (r *RegionStore) peer(seed uint32) int32 {
l := uint32(len(r.stores))
if l <= 1 {
return r.workTiKVIdx
}

candidates := make([]int32, 0, len(r.stores))
for i := 0; i < len(r.stores); i++ {
if r.stores[i].storeType != kv.TiKV {
continue
}
if r.storeFails[i] != atomic.LoadUint32(&r.stores[i].fail) {
continue
}
candidates = append(candidates, int32(i))
}

if len(candidates) == 0 {
return r.workTiKVIdx
} else {
return int32(seed) % int32(len(candidates))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stores value are [leader, tiflash, tiflash, follower, follower]
seed be 2, len(candidates) be 3 and this method will return idx = 2 then r.getStorePeer will return a tiflash node in AnyStorePeer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or stores value are [leader, follower(fail), follower(fail), follower(ok), follower(ok)] have same problem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. PTAL.

}
}

// init initializes region after constructed.
func (r *Region) init(c *RegionCache) {
// region store pull used store from global store map
Expand Down Expand Up @@ -306,6 +331,8 @@ func (c *RegionCache) GetTiKVRPCContext(bo *Backoffer, id RegionVerID, replicaRe
switch replicaRead {
case kv.ReplicaReadFollower:
store, peer, storeIdx = cachedRegion.FollowerStorePeer(regionStore, followerStoreSeed)
case kv.ReplicaReadMixed:
store, peer, storeIdx = cachedRegion.AnyStorePeer(regionStore, followerStoreSeed)
default:
store, peer, storeIdx = cachedRegion.WorkStorePeer(regionStore)
}
Expand Down Expand Up @@ -1055,6 +1082,11 @@ func (r *Region) FollowerStorePeer(rs *RegionStore, followerStoreSeed uint32) (*
return r.getStorePeer(rs, rs.follower(followerStoreSeed))
}

// AnyStorePeer returns a leader or follower store with the associated peer.
func (r *Region) AnyStorePeer(rs *RegionStore, followerStoreSeed uint32) (*Store, *metapb.Peer, int) {
return r.getStorePeer(rs, rs.peer(followerStoreSeed))
}

// RegionVerID is a unique ID that can identify a Region at a specific version.
type RegionVerID struct {
id uint64
Expand Down