github.com/KinWaiYuen/client-go/v2@v2.5.4/tikv/region.go (about) 1 // Copyright 2021 TiKV Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/region.go 19 // 20 21 // Copyright 2021 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package tikv 36 37 import ( 38 "time" 39 40 "github.com/KinWaiYuen/client-go/v2/internal/client" 41 "github.com/KinWaiYuen/client-go/v2/internal/locate" 42 "github.com/KinWaiYuen/client-go/v2/tikvrpc" 43 "github.com/pingcap/kvproto/pkg/metapb" 44 pd "github.com/tikv/pd/client" 45 ) 46 47 // RPCContext contains data that is needed to send RPC to a region. 48 type RPCContext = locate.RPCContext 49 50 // RPCCanceller is rpc send cancelFunc collector. 51 type RPCCanceller = locate.RPCCanceller 52 53 // RegionVerID is a unique ID that can identify a Region at a specific version. 54 type RegionVerID = locate.RegionVerID 55 56 // RegionCache caches Regions loaded from PD. 57 type RegionCache = locate.RegionCache 58 59 // KeyLocation is the region and range that a key is located. 60 type KeyLocation = locate.KeyLocation 61 62 // RPCCancellerCtxKey is context key attach rpc send cancelFunc collector to ctx. 63 type RPCCancellerCtxKey = locate.RPCCancellerCtxKey 64 65 // RegionRequestSender sends KV/Cop requests to tikv server. It handles network 66 // errors and some region errors internally. 67 // 68 // Typically, a KV/Cop request is bind to a region, all keys that are involved 69 // in the request should be located in the region. 70 // The sending process begins with looking for the address of leader store's 71 // address of the target region from cache, and the request is then sent to the 72 // destination tikv server over TCP connection. 73 // If region is updated, can be caused by leader transfer, region split, region 74 // merge, or region balance, tikv server may not able to process request and 75 // send back a RegionError. 76 // RegionRequestSender takes care of errors that does not relevant to region 77 // range, such as 'I/O timeout', 'NotLeader', and 'ServerIsBusy'. For other 78 // errors, since region range have changed, the request may need to split, so we 79 // simply return the error to caller. 80 type RegionRequestSender = locate.RegionRequestSender 81 82 // StoreSelectorOption configures storeSelectorOp. 83 type StoreSelectorOption = locate.StoreSelectorOption 84 85 // RegionRequestRuntimeStats records the runtime stats of send region requests. 86 type RegionRequestRuntimeStats = locate.RegionRequestRuntimeStats 87 88 // RPCRuntimeStats indicates the RPC request count and consume time. 89 type RPCRuntimeStats = locate.RPCRuntimeStats 90 91 // CodecPDClient wraps a PD Client to decode the encoded keys in region meta. 92 type CodecPDClient = locate.CodecPDClient 93 94 // RecordRegionRequestRuntimeStats records request runtime stats. 95 func RecordRegionRequestRuntimeStats(stats map[tikvrpc.CmdType]*locate.RPCRuntimeStats, cmd tikvrpc.CmdType, d time.Duration) { 96 locate.RecordRegionRequestRuntimeStats(stats, cmd, d) 97 } 98 99 // Store contains a kv process's address. 100 type Store = locate.Store 101 102 // Region presents kv region 103 type Region = locate.Region 104 105 // EpochNotMatch indicates it's invalidated due to epoch not match 106 const EpochNotMatch = locate.EpochNotMatch 107 108 // NewRPCanceller creates RPCCanceller with init state. 109 func NewRPCanceller() *RPCCanceller { 110 return locate.NewRPCanceller() 111 } 112 113 // NewRegionVerID creates a region ver id, which used for invalidating regions. 114 func NewRegionVerID(id, confVer, ver uint64) RegionVerID { 115 return locate.NewRegionVerID(id, confVer, ver) 116 } 117 118 // GetStoreTypeByMeta gets store type by store meta pb. 119 func GetStoreTypeByMeta(store *metapb.Store) tikvrpc.EndpointType { 120 return tikvrpc.GetStoreTypeByMeta(store) 121 } 122 123 // NewRegionRequestSender creates a new sender. 124 func NewRegionRequestSender(regionCache *RegionCache, client client.Client) *RegionRequestSender { 125 return locate.NewRegionRequestSender(regionCache, client) 126 } 127 128 // LoadShuttingDown atomically loads ShuttingDown. 129 func LoadShuttingDown() uint32 { 130 return locate.LoadShuttingDown() 131 } 132 133 // StoreShuttingDown atomically stores ShuttingDown into v. 134 func StoreShuttingDown(v uint32) { 135 locate.StoreShuttingDown(v) 136 } 137 138 // WithMatchLabels indicates selecting stores with matched labels 139 func WithMatchLabels(labels []*metapb.StoreLabel) StoreSelectorOption { 140 return locate.WithMatchLabels(labels) 141 } 142 143 // NewRegionRequestRuntimeStats returns a new RegionRequestRuntimeStats. 144 func NewRegionRequestRuntimeStats() RegionRequestRuntimeStats { 145 return locate.NewRegionRequestRuntimeStats() 146 } 147 148 // SetRegionCacheTTLSec sets regionCacheTTLSec to t. 149 func SetRegionCacheTTLSec(t int64) { 150 locate.SetRegionCacheTTLSec(t) 151 } 152 153 // SetStoreLivenessTimeout sets storeLivenessTimeout to t. 154 func SetStoreLivenessTimeout(t time.Duration) { 155 locate.SetStoreLivenessTimeout(t) 156 } 157 158 // NewRegionCache creates a RegionCache. 159 func NewRegionCache(pdClient pd.Client) *locate.RegionCache { 160 return locate.NewRegionCache(pdClient) 161 }