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/tikv: resolve storeType dependency #22854

Merged
merged 7 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
82 changes: 22 additions & 60 deletions store/tikv/region_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/tidb/ddl/placement"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv/config"
"github.com/pingcap/tidb/store/tikv/logutil"
Expand Down Expand Up @@ -63,29 +62,6 @@ type Region struct {
lastAccess int64 // last region access time, see checkRegionCacheTTL
}

// AccessMode uses to index stores for different region cache access requirements.
type AccessMode int

const (
// TiKvOnly indicates stores list that use for TiKv access(include both leader request and follower read).
TiKvOnly AccessMode = iota
// TiFlashOnly indicates stores list that use for TiFlash request.
TiFlashOnly
// NumAccessMode reserved to keep max access mode value.
NumAccessMode
)

func (a AccessMode) String() string {
switch a {
case TiKvOnly:
return "TiKvOnly"
case TiFlashOnly:
return "TiFlashOnly"
default:
return fmt.Sprintf("%d", a)
}
}

// AccessIndex represent the index for accessIndex array
type AccessIndex int

Expand Down Expand Up @@ -127,7 +103,7 @@ func (r *RegionStore) clone() *RegionStore {

// return next follower store's index
func (r *RegionStore) follower(seed uint32, op *storeSelectorOp) AccessIndex {
l := uint32(r.accessStoreNum(TiKvOnly))
l := uint32(r.accessStoreNum(TiKVOnly))
if l <= 1 {
return r.workTiKVIdx
}
Expand All @@ -137,7 +113,7 @@ func (r *RegionStore) follower(seed uint32, op *storeSelectorOp) AccessIndex {
if followerIdx >= r.workTiKVIdx {
followerIdx++
}
storeIdx, s := r.accessStore(TiKvOnly, followerIdx)
storeIdx, s := r.accessStore(TiKVOnly, followerIdx)
if r.storeEpochs[storeIdx] == atomic.LoadUint32(&s.epoch) && r.filterStoreCandidate(followerIdx, op) {
return followerIdx
}
Expand All @@ -148,9 +124,9 @@ func (r *RegionStore) follower(seed uint32, op *storeSelectorOp) AccessIndex {

// return next leader or follower store's index
func (r *RegionStore) kvPeer(seed uint32, op *storeSelectorOp) AccessIndex {
candidates := make([]AccessIndex, 0, r.accessStoreNum(TiKvOnly))
for i := 0; i < r.accessStoreNum(TiKvOnly); i++ {
storeIdx, s := r.accessStore(TiKvOnly, AccessIndex(i))
candidates := make([]AccessIndex, 0, r.accessStoreNum(TiKVOnly))
for i := 0; i < r.accessStoreNum(TiKVOnly); i++ {
storeIdx, s := r.accessStore(TiKVOnly, AccessIndex(i))
if r.storeEpochs[storeIdx] != atomic.LoadUint32(&s.epoch) || !r.filterStoreCandidate(AccessIndex(i), op) {
continue
}
Expand All @@ -163,7 +139,7 @@ func (r *RegionStore) kvPeer(seed uint32, op *storeSelectorOp) AccessIndex {
}

func (r *RegionStore) filterStoreCandidate(aidx AccessIndex, op *storeSelectorOp) bool {
_, s := r.accessStore(TiKvOnly, aidx)
_, s := r.accessStore(TiKVOnly, aidx)
// filter label unmatched store
return s.IsLabelsMatch(op.labels)
}
Expand All @@ -190,9 +166,9 @@ func (r *Region) init(c *RegionCache) error {
return err
}
switch store.storeType {
case kv.TiKV:
rs.accessIndex[TiKvOnly] = append(rs.accessIndex[TiKvOnly], len(rs.stores))
case kv.TiFlash:
case TiKV:
rs.accessIndex[TiKVOnly] = append(rs.accessIndex[TiKVOnly], len(rs.stores))
case TiFlash:
rs.accessIndex[TiFlashOnly] = append(rs.accessIndex[TiFlashOnly], len(rs.stores))
}
rs.stores = append(rs.stores, store)
Expand Down Expand Up @@ -444,7 +420,7 @@ func (c *RegionCache) GetTiKVRPCContext(bo *Backoffer, id RegionVerID, replicaRe
AccessIdx: accessIdx,
Store: store,
Addr: addr,
AccessMode: TiKvOnly,
AccessMode: TiKVOnly,
}, nil
}

Expand Down Expand Up @@ -599,7 +575,7 @@ func (c *RegionCache) OnSendFail(bo *Backoffer, ctx *RPCContext, scheduleReload

// send fail but store is reachable, keep retry current peer for replica leader request.
// but we still need switch peer for follower-read or learner-read(i.e. tiflash)
if ctx.Store.storeType == kv.TiKV && !followerRead && s.requestLiveness(bo) == reachable {
if ctx.Store.storeType == TiKV && !followerRead && s.requestLiveness(bo) == reachable {
return
}

Expand All @@ -615,7 +591,7 @@ func (c *RegionCache) OnSendFail(bo *Backoffer, ctx *RPCContext, scheduleReload
}

// try next peer to found new leader.
if ctx.AccessMode == TiKvOnly {
if ctx.AccessMode == TiKVOnly {
rs.switchNextTiKVPeer(r, ctx.AccessIdx)
} else {
rs.switchNextFlashPeer(r, ctx.AccessIdx)
Expand Down Expand Up @@ -1219,7 +1195,7 @@ func (c *RegionCache) OnRegionEpochNotMatch(bo *Backoffer, ctx *RPCContext, curr
return err
}
var initLeader uint64
if ctx.Store.storeType == kv.TiFlash {
if ctx.Store.storeType == TiFlash {
initLeader = region.findElectableStoreID()
} else {
initLeader = ctx.Store.storeID
Expand Down Expand Up @@ -1250,7 +1226,7 @@ func (c *RegionCache) GetTiFlashStoreAddrs() []string {
defer c.storeMu.RUnlock()
var addrs []string
for _, s := range c.storeMu.stores {
if s.storeType == kv.TiFlash {
if s.storeType == TiFlash {
addrs = append(addrs, s.addr)
}
}
Expand Down Expand Up @@ -1293,25 +1269,25 @@ func (r *Region) GetMeta() *metapb.Region {
// GetLeaderPeerID returns leader peer ID.
func (r *Region) GetLeaderPeerID() uint64 {
store := r.getStore()
if int(store.workTiKVIdx) >= store.accessStoreNum(TiKvOnly) {
if int(store.workTiKVIdx) >= store.accessStoreNum(TiKVOnly) {
return 0
}
storeIdx, _ := store.accessStore(TiKvOnly, store.workTiKVIdx)
storeIdx, _ := store.accessStore(TiKVOnly, store.workTiKVIdx)
return r.meta.Peers[storeIdx].Id
}

// GetLeaderStoreID returns the store ID of the leader region.
func (r *Region) GetLeaderStoreID() uint64 {
store := r.getStore()
if int(store.workTiKVIdx) >= store.accessStoreNum(TiKvOnly) {
if int(store.workTiKVIdx) >= store.accessStoreNum(TiKVOnly) {
return 0
}
storeIdx, _ := store.accessStore(TiKvOnly, store.workTiKVIdx)
storeIdx, _ := store.accessStore(TiKVOnly, store.workTiKVIdx)
return r.meta.Peers[storeIdx].StoreId
}

func (r *Region) getKvStorePeer(rs *RegionStore, aidx AccessIndex) (store *Store, peer *metapb.Peer, accessIdx AccessIndex, storeIdx int) {
storeIdx, store = rs.accessStore(TiKvOnly, aidx)
storeIdx, store = rs.accessStore(TiKVOnly, aidx)
peer = r.meta.Peers[storeIdx]
accessIdx = aidx
return
Expand Down Expand Up @@ -1381,7 +1357,7 @@ retry:
// switch to new leader.
oldRegionStore := r.getStore()
var leaderIdx AccessIndex
for i, gIdx := range oldRegionStore.accessIndex[TiKvOnly] {
for i, gIdx := range oldRegionStore.accessIndex[TiKVOnly] {
if gIdx == globalStoreIdx {
leaderIdx = AccessIndex(i)
}
Expand All @@ -1408,7 +1384,7 @@ func (r *RegionStore) switchNextTiKVPeer(rr *Region, currentPeerIdx AccessIndex)
if r.workTiKVIdx != currentPeerIdx {
return
}
nextIdx := (currentPeerIdx + 1) % AccessIndex(r.accessStoreNum(TiKvOnly))
nextIdx := (currentPeerIdx + 1) % AccessIndex(r.accessStoreNum(TiKVOnly))
newRegionStore := r.clone()
newRegionStore.workTiKVIdx = nextIdx
rr.compareAndSwapStore(r, newRegionStore)
Expand Down Expand Up @@ -1464,7 +1440,7 @@ type Store struct {
labels []*metapb.StoreLabel // stored store labels
resolveMutex sync.Mutex // protect pd from concurrent init requests
epoch uint32 // store fail epoch, see RegionStore.storeEpochs
storeType kv.StoreType // type of the store
storeType StoreType // type of the store
tokenCount atomic2.Int64 // used store token count
}

Expand Down Expand Up @@ -1526,20 +1502,6 @@ func (s *Store) initResolve(bo *Backoffer, c *RegionCache) (addr string, err err
}
}

// GetStoreTypeByMeta gets store type by store meta pb.
func GetStoreTypeByMeta(store *metapb.Store) kv.StoreType {
tp := kv.TiKV
for _, label := range store.Labels {
if label.Key == placement.EngineLabelKey {
if label.Value == placement.EngineLabelTiFlash {
tp = kv.TiFlash
}
break
}
}
return tp
}

// reResolve try to resolve addr for store that need check.
func (s *Store) reResolve(c *RegionCache) {
var addr string
Expand Down
2 changes: 1 addition & 1 deletion store/tikv/region_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ func BenchmarkOnRequestFail(b *testing.B) {
AccessIdx: accessIdx,
Peer: peer,
Store: store,
AccessMode: TiKvOnly,
AccessMode: TiKVOnly,
}
r := cache.getCachedRegionWithRLock(rpcCtx.Region)
if r != nil {
Expand Down
4 changes: 2 additions & 2 deletions store/tikv/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ func (s *RegionRequestSender) onSendFail(bo *Backoffer, ctx *RPCContext, err err
// When a store is not available, the leader of related region should be elected quickly.
// TODO: the number of retry time should be limited:since region may be unavailable
// when some unrecoverable disaster happened.
if ctx.Store != nil && ctx.Store.storeType == kv.TiFlash {
if ctx.Store != nil && ctx.Store.storeType == TiFlash {
err = bo.Backoff(BoTiFlashRPC, errors.Errorf("send tiflash request error: %v, ctx: %v, try next peer later", err, ctx))
} else {
err = bo.Backoff(BoTiKVRPC, errors.Errorf("send tikv request error: %v, ctx: %v, try next peer later", err, ctx))
Expand Down Expand Up @@ -635,7 +635,7 @@ func (s *RegionRequestSender) onRegionError(bo *Backoffer, ctx *RPCContext, seed
logutil.BgLogger().Warn("tikv reports `ServerIsBusy` retry later",
zap.String("reason", regionErr.GetServerIsBusy().GetReason()),
zap.Stringer("ctx", ctx))
if ctx != nil && ctx.Store != nil && ctx.Store.storeType == kv.TiFlash {
if ctx != nil && ctx.Store != nil && ctx.Store.storeType == TiFlash {
err = bo.Backoff(boTiFlashServerBusy, errors.Errorf("server is busy, ctx: %v", ctx))
} else {
err = bo.Backoff(boTiKVServerBusy, errors.Errorf("server is busy, ctx: %v", ctx))
Expand Down
83 changes: 83 additions & 0 deletions store/tikv/store_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package tikv

import (
"fmt"

"github.com/pingcap/kvproto/pkg/metapb"
)

// AccessMode uses to index stores for different region cache access requirements.
type AccessMode int

const (
// TiKVOnly indicates stores list that use for TiKv access(include both leader request and follower read).
TiKVOnly AccessMode = iota
// TiFlashOnly indicates stores list that use for TiFlash request.
TiFlashOnly
// NumAccessMode reserved to keep max access mode value.
NumAccessMode
)

func (a AccessMode) String() string {
switch a {
case TiKVOnly:
return "TiKvOnly"
case TiFlashOnly:
return "TiFlashOnly"
default:
return fmt.Sprintf("%d", a)
}
}

// Constants to determine engine type.
// They should be synced with PD.
const (
engineLabelKey = "engine"
engineLabelTiFlash = "tiflash"
)

// GetStoreTypeByMeta gets store type by store meta pb.
func GetStoreTypeByMeta(store *metapb.Store) StoreType {
tp := TiKV
for _, label := range store.Labels {
if label.Key == engineLabelKey {
if label.Value == engineLabelTiFlash {
tp = TiFlash
}
break
}
}
return tp
}

// StoreType represents the type of a store.
type StoreType uint8

// Store type enums.
const (
TiKV StoreType = iota
TiFlash
)

// Name returns the name of store type.
func (t StoreType) Name() string {
if t == TiFlash {
return "tiflash"
} else if t == TiKV {
return "tikv"
}
return "unspecified"
}