Skip to content

Commit

Permalink
fix race
Browse files Browse the repository at this point in the history
Signed-off-by: you06 <[email protected]>
  • Loading branch information
you06 committed Aug 2, 2023
1 parent 10c11e0 commit 9bbea85
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions internal/locate/region_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,12 @@ func (c *RegionCache) Close() {
c.cancelFunc()
}

var reloadRegionInterval = 10 * time.Second
var reloadRegionInterval = int64(10 * time.Second)

// asyncCheckAndResolveLoop with
func (c *RegionCache) asyncCheckAndResolveLoop(interval time.Duration) {
ticker := time.NewTicker(interval)
reloadRegionTicker := time.NewTicker(reloadRegionInterval)
reloadRegionTicker := time.NewTicker(time.Duration(atomic.LoadInt64(&reloadRegionInterval)))
defer func() {
ticker.Stop()
reloadRegionTicker.Stop()
Expand Down
12 changes: 6 additions & 6 deletions internal/locate/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ func (state *tryFollower) next(bo *retry.Backoffer, selector *replicaSelector) (
return rpcCtx, err
}
if state.fallbackFromLeader {
replicaRead := true
staleRead := false
rpcCtx.contextPatcher.staleRead = &staleRead
replicaRead := true
rpcCtx.contextPatcher.replicaRead = &replicaRead
}
return rpcCtx, nil
Expand Down Expand Up @@ -602,7 +602,9 @@ func (state *accessFollower) next(bo *retry.Backoffer, selector *replicaSelector
selector.targetIdx = idx
break
}
if selectReplica.isEpochStale() {
if selectReplica.isEpochStale() &&
selectReplica.store.getResolveState() == resolved &&
selectReplica.store.getLivenessState() == reachable {
reloadRegion = true
}
}
Expand Down Expand Up @@ -657,11 +659,9 @@ func (state *accessFollower) isCandidate(idx AccessIndex, replica *replica) bool
if replica.isEpochStale() || replica.isExhausted(1) || replica.store.getLivenessState() == unreachable {
return false
}
if state.option.leaderOnly {
if state.option.leaderOnly && idx == state.leaderIdx {
// The request can only be sent to the leader.
if idx == state.leaderIdx {
return true
}
return true
} else if !state.tryLeader && idx == state.leaderIdx {
// The request cannot be sent to leader.
return false
Expand Down
4 changes: 2 additions & 2 deletions internal/locate/region_request_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ func (s *testRegionCacheStaleReadSuite) setTimeout(id uint64) {
}

func TestRegionCacheStaleRead(t *testing.T) {
originReloadRegionInterval := reloadRegionInterval
originReloadRegionInterval := atomic.LoadInt64(&reloadRegionInterval)
originBoTiKVServerBusy := retry.BoTiKVServerBusy
defer func() {
reloadRegionInterval = originReloadRegionInterval
retry.BoTiKVServerBusy = originBoTiKVServerBusy
}()
reloadRegionInterval = 24 * time.Hour // disable reload region
atomic.StoreInt64(&reloadRegionInterval, int64(24*time.Hour)) // disable reload region
retry.BoTiKVServerBusy = retry.NewConfig("tikvServerBusy", &metrics.BackoffHistogramServerBusy, retry.NewBackoffFnCfg(2, 10, retry.EqualJitter), tikverr.ErrTiKVServerBusy)
regionCacheTestCases := []RegionCacheTestCase{
{
Expand Down
16 changes: 8 additions & 8 deletions internal/mockstore/mocktikv/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ var _ cluster.Cluster = &Cluster{}

// Cluster simulates a TiKV cluster. It focuses on management and the change of
// meta data. A Cluster mainly includes following 3 kinds of meta data:
// 1. Region: A Region is a fragment of TiKV's data whose range is [start, end).
// The data of a Region is duplicated to multiple Peers and distributed in
// multiple Stores.
// 2. Peer: A Peer is a replica of a Region's data. All peers of a Region form
// a group, each group elects a Leader to provide services.
// 3. Store: A Store is a storage/service node. Try to think it as a TiKV server
// process. Only the store with request's Region's leader Peer could respond
// to client's request.
// 1) Region: A Region is a fragment of TiKV's data whose range is [start, end).
// The data of a Region is duplicated to multiple Peers and distributed in
// multiple Stores.
// 2) Peer: A Peer is a replica of a Region's data. All peers of a Region form
// a group, each group elects a Leader to provide services.
// 3) Store: A Store is a storage/service node. Try to think it as a TiKV server
// process. Only the store with request's Region's leader Peer could respond
// to client's request.
type Cluster struct {
sync.RWMutex
id uint64
Expand Down

0 comments on commit 9bbea85

Please sign in to comment.