Documentation ¶
Overview ¶
Package tikv provides tcp connection to kvserver.
Index ¶
- Constants
- Variables
- func NewBackoffFn(base, cap, jitter int) func() int
- func NewMockTikvStore(options ...MockTiKVStoreOption) (kv.Storage, error)
- func ParseEtcdAddr(path string) (etcdAddrs []string, err error)
- func RunGCJob(ctx goctx.Context, store kv.Storage, safePoint uint64, identifier string) error
- type Backoffer
- type CachedRegion
- type Client
- type CopClient
- type Driver
- type EtcdSafePointKV
- type GCWorker
- type KeyLocation
- type Lock
- type LockResolver
- type MockDriver
- type MockGCWorker
- type MockSafePointKV
- type MockTiKVStoreOption
- func WithCluster(cluster *mocktikv.Cluster) MockTiKVStoreOption
- func WithHijackClient(wrap func(Client) Client) MockTiKVStoreOption
- func WithHijackPDClient(wrap func(pd.Client) pd.Client) MockTiKVStoreOption
- func WithMVCCStore(store mocktikv.MVCCStore) MockTiKVStoreOption
- func WithPath(path string) MockTiKVStoreOption
- type RPCContext
- type RawKVClient
- func (c *RawKVClient) Close() error
- func (c *RawKVClient) ClusterID() uint64
- func (c *RawKVClient) Delete(key []byte) error
- func (c *RawKVClient) Get(key []byte) ([]byte, error)
- func (c *RawKVClient) Put(key, value []byte) error
- func (c *RawKVClient) Scan(startKey []byte, limit int) (keys [][]byte, values [][]byte, err error)
- type Region
- func (r *Region) Contains(key []byte) bool
- func (r *Region) EndKey() []byte
- func (r *Region) GetContext() *kvrpcpb.Context
- func (r *Region) GetID() uint64
- func (r *Region) OnRequestFail(storeID uint64) bool
- func (r *Region) StartKey() []byte
- func (r *Region) SwitchPeer(storeID uint64) bool
- func (r *Region) VerID() RegionVerID
- type RegionCache
- func (c *RegionCache) ClearStoreByID(id uint64)
- func (c *RegionCache) DropRegion(id RegionVerID)
- func (c *RegionCache) GetCachedRegion(id RegionVerID) *Region
- func (c *RegionCache) GetRPCContext(bo *Backoffer, id RegionVerID) (*RPCContext, error)
- func (c *RegionCache) GetStoreAddr(bo *Backoffer, id uint64) (string, error)
- func (c *RegionCache) GroupKeysByRegion(bo *Backoffer, keys [][]byte) (map[RegionVerID][][]byte, RegionVerID, error)
- func (c *RegionCache) ListRegionIDsInKeyRange(bo *Backoffer, startKey, endKey []byte) (regionIDs []uint64, err error)
- func (c *RegionCache) LocateKey(bo *Backoffer, key []byte) (*KeyLocation, error)
- func (c *RegionCache) LocateRegionByID(bo *Backoffer, regionID uint64) (*KeyLocation, error)
- func (c *RegionCache) OnRegionStale(ctx *RPCContext, newRegions []*metapb.Region) error
- func (c *RegionCache) OnRequestFail(ctx *RPCContext, err error)
- func (c *RegionCache) PDClient() pd.Client
- func (c *RegionCache) ReloadStoreAddr(bo *Backoffer, id uint64) (string, error)
- func (c *RegionCache) UpdateLeader(regionID RegionVerID, leaderStoreID uint64)
- type RegionRequestSender
- type RegionVerID
- type SafePointKV
- type Scanner
- type Store
- type TxnStatus
Constants ¶
const ( // NoJitter makes the backoff sequence strict exponential. NoJitter = 1 + iota // FullJitter applies random factors to strict exponential. FullJitter // EqualJitter is also randomized, but prevents very short sleeps. EqualJitter // DecorrJitter increases the maximum jitter based on the last random value. DecorrJitter )
Variables ¶
var ( ErrTiKVServerTimeout = terror.ClassTiKV.New(mysql.ErrTiKVServerTimeout, mysql.MySQLErrName[mysql.ErrTiKVServerTimeout]+txnRetryableMark) ErrResolveLockTimeout = terror.ClassTiKV.New(mysql.ErrResolveLockTimeout, mysql.MySQLErrName[mysql.ErrResolveLockTimeout]+txnRetryableMark) ErrPDServerTimeout = terror.ClassTiKV.New(mysql.ErrPDServerTimeout, mysql.MySQLErrName[mysql.ErrPDServerTimeout]+"%v") ErrTiKVServerBusy = terror.ClassTiKV.New(mysql.ErrTiKVServerBusy, mysql.MySQLErrName[mysql.ErrTiKVServerBusy]+txnRetryableMark) ErrGCTooEarly = terror.ClassTiKV.New(mysql.ErrGCTooEarly, mysql.MySQLErrName[mysql.ErrGCTooEarly]) )
MySQL error instances.
var ( // MaxRawKVScanLimit is the maximum scan limit for rawkv Scan. MaxRawKVScanLimit = 10240 // ErrMaxScanLimitExceeded is returned when the limit for rawkv Scan is to large. ErrMaxScanLimitExceeded = errors.New("limit should be less than MaxRawKVScanLimit") )
Functions ¶
func NewBackoffFn ¶
NewBackoffFn creates a backoff func which implements exponential backoff with optional jitters. See http://www.awsarchitectureblog.com/2015/03/backoff.html
func NewMockTikvStore ¶
func NewMockTikvStore(options ...MockTiKVStoreOption) (kv.Storage, error)
NewMockTikvStore creates a mocked tikv store, the path is the file path to store the data. If path is an empty string, a memory storage will be created.
func ParseEtcdAddr ¶
ParseEtcdAddr parses path to etcd address list
Types ¶
type Backoffer ¶
type Backoffer struct {
// contains filtered or unexported fields
}
Backoffer is a utility for retrying queries.
func NewBackoffer ¶
NewBackoffer creates a Backoffer with maximum sleep time(in ms).
func (*Backoffer) Backoff ¶
Backoff sleeps a while base on the backoffType and records the error message. It returns a retryable error if total sleep time exceeds maxSleep.
func (*Backoffer) Clone ¶
Clone creates a new Backoffer which keeps current Backoffer's sleep time and errors, and shares current Backoffer's context.
type CachedRegion ¶
type CachedRegion struct {
// contains filtered or unexported fields
}
CachedRegion encapsulates {Region, TTL}
type Client ¶
type Client interface { // Close should release all data. Close() error // SendReq sends Request. SendReq(ctx goctx.Context, addr string, req *tikvrpc.Request) (*tikvrpc.Response, error) }
Client is a client that sends RPC. It should not be used after calling Close().
type CopClient ¶
type CopClient struct {
// contains filtered or unexported fields
}
CopClient is coprocessor client.
func (*CopClient) IsRequestTypeSupported ¶
IsRequestTypeSupported checks whether reqType is supported.
type EtcdSafePointKV ¶
type EtcdSafePointKV struct {
// contains filtered or unexported fields
}
EtcdSafePointKV implements SafePointKV at runtime
func NewEtcdSafePointKV ¶
func NewEtcdSafePointKV(addrs []string) (*EtcdSafePointKV, error)
NewEtcdSafePointKV creates an instance of EtcdSafePointKV
type GCWorker ¶
type GCWorker struct {
// contains filtered or unexported fields
}
GCWorker periodically triggers GC process on tikv server.
func NewGCWorker ¶
NewGCWorker creates a GCWorker instance.
func (*GCWorker) StartSafePointChecker ¶
func (w *GCWorker) StartSafePointChecker()
StartSafePointChecker triggers SafePoint Checker on each tidb
type KeyLocation ¶
type KeyLocation struct { Region RegionVerID StartKey []byte EndKey []byte }
KeyLocation is the region and range that a key is located.
func (*KeyLocation) Contains ¶
func (l *KeyLocation) Contains(key []byte) bool
Contains checks if key is in [StartKey, EndKey).
type LockResolver ¶
type LockResolver struct {
// contains filtered or unexported fields
}
LockResolver resolves locks and also caches resolved txn status.
func NewLockResolver ¶
func NewLockResolver(etcdAddrs []string) (*LockResolver, error)
NewLockResolver creates a LockResolver. It is exported for other services to use. For instance, binlog service needs to determine a transaction's commit state.
func (*LockResolver) GetTxnStatus ¶
func (lr *LockResolver) GetTxnStatus(txnID uint64, primary []byte) (TxnStatus, error)
GetTxnStatus queries tikv-server for a txn's status (commit/rollback). If the primary key is still locked, it will launch a Rollback to abort it. To avoid unnecessarily aborting too many txns, it is wiser to wait a few seconds before calling it after Prewrite.
func (*LockResolver) ResolveLocks ¶
func (lr *LockResolver) ResolveLocks(bo *Backoffer, locks []*Lock) (ok bool, err error)
ResolveLocks tries to resolve Locks. The resolving process is in 3 steps:
- Use the `lockTTL` to pick up all expired locks. Only locks that are too old are considered orphan locks and will be handled later. If all locks are expired then all locks will be resolved so the returned `ok` will be true, otherwise caller should sleep a while before retry.
- For each lock, query the primary key to get txn(which left the lock)'s commit status.
- Send `ResolveLock` cmd to the lock's region to resolve all locks belong to the same transaction.
type MockGCWorker ¶
type MockGCWorker struct {
// contains filtered or unexported fields
}
MockGCWorker is for test.
func NewMockGCWorker ¶
func NewMockGCWorker(store kv.Storage) (*MockGCWorker, error)
NewMockGCWorker creates a MockGCWorker instance ONLY for test.
func (*MockGCWorker) DeleteRanges ¶
func (w *MockGCWorker) DeleteRanges(ctx goctx.Context, safePoint uint64) error
DeleteRanges call deleteRanges internally, just for test.
type MockSafePointKV ¶
type MockSafePointKV struct {
// contains filtered or unexported fields
}
MockSafePointKV implements SafePointKV at mock test
func NewMockSafePointKV ¶
func NewMockSafePointKV() *MockSafePointKV
NewMockSafePointKV creates an instance of MockSafePointKV
type MockTiKVStoreOption ¶
type MockTiKVStoreOption func(*mockOptions)
MockTiKVStoreOption is used to control some behavior of mock tikv.
func WithCluster ¶
func WithCluster(cluster *mocktikv.Cluster) MockTiKVStoreOption
WithCluster provides the customized cluster.
func WithHijackClient ¶
func WithHijackClient(wrap func(Client) Client) MockTiKVStoreOption
WithHijackClient hijacks KV client's behavior, makes it easy to simulate the network problem between TiDB and TiKV.
func WithHijackPDClient ¶
func WithHijackPDClient(wrap func(pd.Client) pd.Client) MockTiKVStoreOption
WithHijackPDClient hijacks PD client's behavior, makes it easy to simulate the network problem between TiDB and PD, such as GetTS too slow, GetStore or GetRegion fail.
func WithMVCCStore ¶
func WithMVCCStore(store mocktikv.MVCCStore) MockTiKVStoreOption
WithMVCCStore provides the customized mvcc store.
func WithPath ¶
func WithPath(path string) MockTiKVStoreOption
WithPath specifies the mocktikv path.
type RPCContext ¶
RPCContext contains data that is needed to send RPC to a region.
func (*RPCContext) GetStoreID ¶
func (c *RPCContext) GetStoreID() uint64
GetStoreID returns StoreID.
type RawKVClient ¶
type RawKVClient struct {
// contains filtered or unexported fields
}
RawKVClient is a client of TiKV server which is used as a key-value storage, only GET/PUT/DELETE commands are supported.
func NewRawKVClient ¶
func NewRawKVClient(pdAddrs []string) (*RawKVClient, error)
NewRawKVClient creates a client with PD cluster addrs.
func (*RawKVClient) ClusterID ¶
func (c *RawKVClient) ClusterID() uint64
ClusterID returns the TiKV cluster ID.
func (*RawKVClient) Delete ¶
func (c *RawKVClient) Delete(key []byte) error
Delete deletes a key-value pair from TiKV.
func (*RawKVClient) Get ¶
func (c *RawKVClient) Get(key []byte) ([]byte, error)
Get queries value with the key. When the key does not exist, it returns `nil, nil`.
func (*RawKVClient) Put ¶
func (c *RawKVClient) Put(key, value []byte) error
Put stores a key-value pair to TiKV.
type Region ¶
type Region struct {
// contains filtered or unexported fields
}
Region stores region's meta and its leader peer.
func (*Region) Contains ¶
Contains checks whether the key is in the region, for the maximum region endKey is empty. startKey <= key < endKey.
func (*Region) GetContext ¶
GetContext constructs kvprotopb.Context from region info.
func (*Region) OnRequestFail ¶
OnRequestFail records unreachable peer and tries to select another valid peer. It returns false if all peers are unreachable.
func (*Region) SwitchPeer ¶
SwitchPeer switches current peer to the one on specific store. It returns false if no peer matches the storeID.
type RegionCache ¶
type RegionCache struct {
// contains filtered or unexported fields
}
RegionCache caches Regions loaded from PD.
func NewRegionCache ¶
func NewRegionCache(pdClient pd.Client) *RegionCache
NewRegionCache creates a RegionCache.
func (*RegionCache) ClearStoreByID ¶
func (c *RegionCache) ClearStoreByID(id uint64)
ClearStoreByID clears store from cache with storeID.
func (*RegionCache) DropRegion ¶
func (c *RegionCache) DropRegion(id RegionVerID)
DropRegion removes a cached Region.
func (*RegionCache) GetCachedRegion ¶
func (c *RegionCache) GetCachedRegion(id RegionVerID) *Region
GetCachedRegion returns a valid region
func (*RegionCache) GetRPCContext ¶
func (c *RegionCache) GetRPCContext(bo *Backoffer, id RegionVerID) (*RPCContext, error)
GetRPCContext returns RPCContext for a region. If it returns nil, the region must be out of date and already dropped from cache.
func (*RegionCache) GetStoreAddr ¶
func (c *RegionCache) GetStoreAddr(bo *Backoffer, id uint64) (string, error)
GetStoreAddr returns a tikv server's address by its storeID. It checks cache first, sends request to pd server when necessary.
func (*RegionCache) GroupKeysByRegion ¶
func (c *RegionCache) GroupKeysByRegion(bo *Backoffer, keys [][]byte) (map[RegionVerID][][]byte, RegionVerID, error)
GroupKeysByRegion separates keys into groups by their belonging Regions. Specially it also returns the first key's region which may be used as the 'PrimaryLockKey' and should be committed ahead of others.
func (*RegionCache) ListRegionIDsInKeyRange ¶
func (c *RegionCache) ListRegionIDsInKeyRange(bo *Backoffer, startKey, endKey []byte) (regionIDs []uint64, err error)
ListRegionIDsInKeyRange lists ids of regions in [start_key,end_key].
func (*RegionCache) LocateKey ¶
func (c *RegionCache) LocateKey(bo *Backoffer, key []byte) (*KeyLocation, error)
LocateKey searches for the region and range that the key is located.
func (*RegionCache) LocateRegionByID ¶
func (c *RegionCache) LocateRegionByID(bo *Backoffer, regionID uint64) (*KeyLocation, error)
LocateRegionByID searches for the region with ID
func (*RegionCache) OnRegionStale ¶
func (c *RegionCache) OnRegionStale(ctx *RPCContext, newRegions []*metapb.Region) error
OnRegionStale removes the old region and inserts new regions into the cache.
func (*RegionCache) OnRequestFail ¶
func (c *RegionCache) OnRequestFail(ctx *RPCContext, err error)
OnRequestFail is used for clearing cache when a tikv server does not respond.
func (*RegionCache) PDClient ¶
func (c *RegionCache) PDClient() pd.Client
PDClient returns the pd.Client in RegionCache.
func (*RegionCache) ReloadStoreAddr ¶
func (c *RegionCache) ReloadStoreAddr(bo *Backoffer, id uint64) (string, error)
ReloadStoreAddr reloads store's address.
func (*RegionCache) UpdateLeader ¶
func (c *RegionCache) UpdateLeader(regionID RegionVerID, leaderStoreID uint64)
UpdateLeader update some region cache with newer leader info.
type RegionRequestSender ¶
type RegionRequestSender struct {
// contains filtered or unexported fields
}
RegionRequestSender sends KV/Cop requests to tikv server. It handles network errors and some region errors internally.
Typically, a KV/Cop request is bind to a region, all keys that are involved in the request should be located in the region. The sending process begins with looking for the address of leader store's address of the target region from cache, and the request is then sent to the destination tikv server over TCP connection. If region is updated, can be caused by leader transfer, region split, region merge, or region balance, tikv server may not able to process request and send back a RegionError. RegionRequestSender takes care of errors that does not relevant to region range, such as 'I/O timeout', 'NotLeader', and 'ServerIsBusy'. For other errors, since region range have changed, the request may need to split, so we simply return the error to caller.
func NewRegionRequestSender ¶
func NewRegionRequestSender(regionCache *RegionCache, client Client) *RegionRequestSender
NewRegionRequestSender creates a new sender.
type RegionVerID ¶
type RegionVerID struct {
// contains filtered or unexported fields
}
RegionVerID is a unique ID that can identify a Region at a specific version.
type SafePointKV ¶
SafePointKV is used for a seamingless integration for mockTest and runtime.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner support tikv scan
type TxnStatus ¶
type TxnStatus uint64
TxnStatus represents a txn's final status. It should be Commit or Rollback.
func (TxnStatus) CommitTS ¶
CommitTS returns the txn's commitTS. It is valid iff `IsCommitted` is true.
func (TxnStatus) IsCommitted ¶
IsCommitted returns true if the txn's final status is Commit.