Skip to content

Commit f2a7fe7

Browse files
fix inconsistent store pointers between the region-cache and store-ca…
X-Original-Commit: 3c2a4f5cca9cbab6350652da29057bd54cd90874
1 parent 7a0d830 commit f2a7fe7

7 files changed

Lines changed: 256 additions & 159 deletions

File tree

internal/locate/region_cache.go

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func newRegion(bo *retry.Backoffer, c *RegionCache, pdRegion *pd.Region) (*Regio
348348
leaderAccessIdx = AccessIndex(len(rs.accessIndex[tiKVOnly]))
349349
}
350350
availablePeers = append(availablePeers, p)
351-
switch store.storeType {
351+
switch store.StoreType() {
352352
case tikvrpc.TiKV:
353353
rs.accessIndex[tiKVOnly] = append(rs.accessIndex[tiKVOnly], len(rs.stores))
354354
case tikvrpc.TiFlash:
@@ -702,7 +702,7 @@ func NewRegionCache(pdClient pd.Client, opt ...RegionCacheOpt) *RegionCache {
702702
c.bg.scheduleWithTrigger(func(ctx context.Context, t time.Time) bool {
703703
// check and resolve normal stores periodically by default.
704704
filter := func(state resolveState) bool {
705-
return state != unresolved && state != tombstone && state != deleted
705+
return state != unresolved && state != tombstone
706706
}
707707
if t.IsZero() {
708708
// check and resolve needCheck stores because it's triggered by a CheckStoreEvent this time.
@@ -757,13 +757,9 @@ func refreshFullStoreList(ctx context.Context, stores storeCache) {
757757
continue
758758
}
759759
s := stores.getOrInsertDefault(store.GetId())
760-
// TODO: maybe refactor this, together with other places initializing Store
761-
s.addr = addr
762-
s.peerAddr = store.GetPeerAddress()
763-
s.saddr = store.GetStatusAddress()
764-
s.storeType = tikvrpc.GetStoreTypeByMeta(store)
765-
s.labels = store.GetLabels()
766-
s.changeResolveStateTo(unresolved, resolved)
760+
if err := s.initResolveLite(store); err != nil {
761+
continue
762+
}
767763
}
768764
}
769765

@@ -805,7 +801,7 @@ func (c *RegionCache) checkAndResolve(needCheckStores []*Store, needCheck func(*
805801

806802
needCheckStores = c.stores.filter(needCheckStores, needCheck)
807803
for _, store := range needCheckStores {
808-
_, err := store.reResolve(c.stores, c.bg)
804+
_, err := store.reResolve(c.stores)
809805
tikverr.Log(err)
810806
}
811807
return needCheckStores
@@ -841,12 +837,12 @@ type RPCContext struct {
841837
func (c *RPCContext) String() string {
842838
var runStoreType string
843839
if c.Store != nil {
844-
runStoreType = c.Store.storeType.Name()
840+
runStoreType = c.Store.StoreType().Name()
845841
}
846842
res := fmt.Sprintf("region ID: %d, meta: %s, peer: %s, addr: %s, idx: %d, reqStoreType: %s, runStoreType: %s",
847843
c.Region.GetID(), c.Meta, c.Peer, c.Addr, c.AccessIdx, c.AccessMode, runStoreType)
848844
if c.ProxyStore != nil {
849-
res += fmt.Sprintf(", proxy store id: %d, proxy addr: %s", c.ProxyStore.storeID, c.ProxyStore.addr)
845+
res += fmt.Sprintf(", proxy store id: %d, proxy addr: %s", c.ProxyStore.storeID, c.ProxyStore.GetAddr())
850846
}
851847
return res
852848
}
@@ -942,7 +938,7 @@ func (c *RegionCache) GetTiKVRPCContext(bo *retry.Backoffer, id RegionVerID, rep
942938
cachedRegion.invalidate(Other)
943939
logutil.Logger(bo.GetCtx()).Info("invalidate current region, because others failed on same store",
944940
zap.Uint64("region", id.GetID()),
945-
zap.String("store", store.addr))
941+
zap.String("store", store.GetAddr()))
946942
return nil, nil
947943
}
948944

@@ -1011,7 +1007,7 @@ func (c *RegionCache) GetAllValidTiFlashStores(id RegionVerID, currentStore *Sto
10111007
if storeFailEpoch != regionStore.storeEpochs[storeIdx] {
10121008
continue
10131009
}
1014-
if !labelFilter(store.labels) {
1010+
if !labelFilter(store.GetLabels()) {
10151011
continue
10161012
}
10171013
allStores = append(allStores, store.storeID)
@@ -1046,7 +1042,7 @@ func (c *RegionCache) GetTiFlashRPCContext(bo *retry.Backoffer, id RegionVerID,
10461042
for i := 0; i < regionStore.accessStoreNum(tiFlashOnly); i++ {
10471043
accessIdx := AccessIndex((sIdx + i) % regionStore.accessStoreNum(tiFlashOnly))
10481044
storeIdx, store := regionStore.accessStore(tiFlashOnly, accessIdx)
1049-
if !labelFilter(store.labels) {
1045+
if !labelFilter(store.GetLabels()) {
10501046
continue
10511047
}
10521048
addr, err := c.getStoreAddr(bo, cachedRegion, store)
@@ -1058,7 +1054,7 @@ func (c *RegionCache) GetTiFlashRPCContext(bo *retry.Backoffer, id RegionVerID,
10581054
return nil, nil
10591055
}
10601056
if store.getResolveState() == needCheck {
1061-
_, err := store.reResolve(c.stores, c.bg)
1057+
_, err := store.reResolve(c.stores)
10621058
tikverr.Log(err)
10631059
}
10641060
regionStore.workTiFlashIdx.Store(int32(accessIdx))
@@ -1068,7 +1064,7 @@ func (c *RegionCache) GetTiFlashRPCContext(bo *retry.Backoffer, id RegionVerID,
10681064
cachedRegion.invalidate(Other)
10691065
logutil.Logger(bo.GetCtx()).Info("invalidate current region, because others failed on same store",
10701066
zap.Uint64("region", id.GetID()),
1071-
zap.String("store", store.addr))
1067+
zap.String("store", store.GetAddr()))
10721068
// TiFlash will always try to find out a valid peer, avoiding to retry too many times.
10731069
continue
10741070
}
@@ -1602,7 +1598,7 @@ func (c *RegionCache) markRegionNeedBeRefill(s *Store, storeIdx int, rs *regionS
16021598
// invalidate regions in store.
16031599
epoch := rs.storeEpochs[storeIdx]
16041600
if atomic.CompareAndSwapUint32(&s.epoch, epoch, epoch+1) {
1605-
logutil.BgLogger().Info("mark store's regions need be refill", zap.String("store", s.addr))
1601+
logutil.BgLogger().Info("mark store's regions need be refill", zap.String("store", s.GetAddr()))
16061602
incEpochStoreIdx = storeIdx
16071603
metrics.RegionCacheCounterWithInvalidateStoreRegionsOK.Inc()
16081604
}
@@ -1993,14 +1989,14 @@ func (c *RegionCache) searchCachedRegionByID(regionID uint64) (*Region, bool) {
19931989
// GetStoresByType gets stores by type `typ`
19941990
func (c *RegionCache) GetStoresByType(typ tikvrpc.EndpointType) []*Store {
19951991
return c.stores.filter(nil, func(s *Store) bool {
1996-
return s.getResolveState() == resolved && s.storeType == typ
1992+
return s.getResolveState() == resolved && s.StoreType() == typ
19971993
})
19981994
}
19991995

20001996
// GetAllStores gets TiKV and TiFlash stores.
20011997
func (c *RegionCache) GetAllStores() []*Store {
20021998
return c.stores.filter(nil, func(s *Store) bool {
2003-
return s.getResolveState() == resolved && (s.storeType == tikvrpc.TiKV || s.storeType == tikvrpc.TiFlash)
1999+
return s.getResolveState() == resolved && (s.StoreType() == tikvrpc.TiKV || s.StoreType() == tikvrpc.TiFlash)
20042000
})
20052001
}
20062002

@@ -2448,14 +2444,11 @@ func (c *RegionCache) getStoreAddr(bo *retry.Backoffer, region *Region, store *S
24482444
state := store.getResolveState()
24492445
switch state {
24502446
case resolved, needCheck:
2451-
addr = store.addr
2447+
addr = store.GetAddr()
24522448
return
24532449
case unresolved:
24542450
addr, err = store.initResolve(bo, c.stores)
24552451
return
2456-
case deleted:
2457-
addr = c.changeToActiveStore(region, store.storeID)
2458-
return
24592452
case tombstone:
24602453
return "", nil
24612454
default:
@@ -2464,7 +2457,7 @@ func (c *RegionCache) getStoreAddr(bo *retry.Backoffer, region *Region, store *S
24642457
}
24652458

24662459
func (c *RegionCache) getProxyStore(region *Region, store *Store, rs *regionStore, workStoreIdx AccessIndex) (proxyStore *Store, proxyAccessIdx AccessIndex, proxyStoreIdx int) {
2467-
if !c.enableForwarding || store.storeType != tikvrpc.TiKV || store.getLivenessState() == reachable {
2460+
if !c.enableForwarding || store.StoreType() != tikvrpc.TiKV || store.getLivenessState() == reachable {
24682461
return
24692462
}
24702463

@@ -2505,29 +2498,6 @@ func (c *RegionCache) getProxyStore(region *Region, store *Store, rs *regionStor
25052498
return nil, 0, 0
25062499
}
25072500

2508-
// changeToActiveStore replace the deleted store in the region by an up-to-date store in the stores map.
2509-
// The order is guaranteed by reResolve() which adds the new store before marking old store deleted.
2510-
func (c *RegionCache) changeToActiveStore(region *Region, storeID uint64) (addr string) {
2511-
store, _ := c.stores.get(storeID)
2512-
for {
2513-
oldRegionStore := region.getStore()
2514-
newRegionStore := oldRegionStore.clone()
2515-
newRegionStore.stores = make([]*Store, 0, len(oldRegionStore.stores))
2516-
for _, s := range oldRegionStore.stores {
2517-
if s.storeID == store.storeID {
2518-
newRegionStore.stores = append(newRegionStore.stores, store)
2519-
} else {
2520-
newRegionStore.stores = append(newRegionStore.stores, s)
2521-
}
2522-
}
2523-
if region.compareAndSwapStore(oldRegionStore, newRegionStore) {
2524-
break
2525-
}
2526-
}
2527-
addr = store.addr
2528-
return
2529-
}
2530-
25312501
// OnBucketVersionNotMatch removes the old buckets meta if the version is stale.
25322502
func (c *RegionCache) OnBucketVersionNotMatch(ctx *RPCContext, version uint64, keys [][]byte) {
25332503
r := c.GetCachedRegionWithRLock(ctx.Region)
@@ -2586,7 +2556,7 @@ func (c *RegionCache) OnRegionEpochNotMatch(bo *retry.Backoffer, ctx *RPCContext
25862556
return false, err
25872557
}
25882558
var initLeaderStoreID uint64
2589-
if ctx.Store.storeType == tikvrpc.TiFlash {
2559+
if ctx.Store.StoreType() == tikvrpc.TiFlash {
25902560
initLeaderStoreID = region.findElectableStoreID()
25912561
} else {
25922562
initLeaderStoreID = ctx.Store.storeID
@@ -2619,7 +2589,7 @@ func (c *RegionCache) PDClient() pd.Client {
26192589
// stores so that users won't be bothered by tombstones. (related issue: https://github.com/pingcap/tidb/issues/46602)
26202590
func (c *RegionCache) GetTiFlashStores(labelFilter LabelFilter) []*Store {
26212591
return c.stores.filter(nil, func(s *Store) bool {
2622-
return s.storeType == tikvrpc.TiFlash && labelFilter(s.labels) && s.getResolveState() == resolved
2592+
return s.StoreType() == tikvrpc.TiFlash && labelFilter(s.GetLabels()) && s.getResolveState() == resolved
26232593
})
26242594
}
26252595

@@ -3085,7 +3055,7 @@ func (c *RegionCache) checkAndUpdateStoreHealthStatus(ctx context.Context, now t
30853055
func (c *RegionCache) reportStoreReplicaFlows() {
30863056
c.stores.forEach(func(store *Store) {
30873057
for destType := toLeader; destType < numReplicaFlowsType; destType++ {
3088-
metrics.TiKVPreferLeaderFlowsGauge.WithLabelValues(destType.String(), store.addr).Set(float64(store.getReplicaFlowsStats(destType)))
3058+
metrics.TiKVPreferLeaderFlowsGauge.WithLabelValues(destType.String(), store.GetAddr()).Set(float64(store.getReplicaFlowsStats(destType)))
30893059
store.resetReplicaFlowsStats(destType)
30903060
}
30913061
})

0 commit comments

Comments
 (0)