tidb: github.com/pingcap/tidb/store/mockstore/mocktikv Index | Files

package mocktikv

import "github.com/pingcap/tidb/store/mockstore/mocktikv"

Index

Package Files

aggregate.go analyze.go checksum.go cluster.go cluster_manipulate.go cop_handler_dag.go errors.go executor.go mock.go mvcc.go mvcc_leveldb.go pd.go rpc.go topn.go

Variables

var ErrInvalidEncodedKey = errors.New("invalid encoded key")

ErrInvalidEncodedKey describes parsing an invalid format of EncodedKey.

func BootstrapWithMultiRegions Uses

func BootstrapWithMultiRegions(cluster *Cluster, splitKeys ...[]byte) (storeID uint64, regionIDs, peerIDs []uint64)

BootstrapWithMultiRegions initializes a Cluster with multiple Regions and 1 Store. The number of Regions will be len(splitKeys) + 1.

func BootstrapWithMultiStores Uses

func BootstrapWithMultiStores(cluster *Cluster, n int) (storeIDs, peerIDs []uint64, regionID uint64, leaderPeer uint64)

BootstrapWithMultiStores initializes a Cluster with 1 Region and n Stores.

func BootstrapWithSingleStore Uses

func BootstrapWithSingleStore(cluster *Cluster) (storeID, peerID, regionID uint64)

BootstrapWithSingleStore initializes a Cluster with 1 Region and 1 Store.

func MockGRPCClientStream Uses

func MockGRPCClientStream() grpc.ClientStream

MockGRPCClientStream is exported for testing purpose.

func NewPDClient Uses

func NewPDClient(cluster *Cluster) pd.Client

NewPDClient creates a mock pd.Client that uses local timestamp and meta data from a Cluster.

type Cluster Uses

type Cluster struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

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.

func NewCluster Uses

func NewCluster() *Cluster

NewCluster creates an empty cluster. It needs to be bootstrapped before providing service.

func (*Cluster) AddPeer Uses

func (c *Cluster) AddPeer(regionID, storeID, peerID uint64)

AddPeer adds a new Peer for the Region on the Store.

func (*Cluster) AddStore Uses

func (c *Cluster) AddStore(storeID uint64, addr string)

AddStore add a new Store to the cluster.

func (*Cluster) AllocID Uses

func (c *Cluster) AllocID() uint64

AllocID creates an unique ID in cluster. The ID could be used as either StoreID, RegionID, or PeerID.

func (*Cluster) AllocIDs Uses

func (c *Cluster) AllocIDs(n int) []uint64

AllocIDs creates multiple IDs.

func (*Cluster) Bootstrap Uses

func (c *Cluster) Bootstrap(regionID uint64, storeIDs, peerIDs []uint64, leaderPeerID uint64)

Bootstrap creates the first Region. The Stores should be in the Cluster before bootstrap.

func (*Cluster) CancelStore Uses

func (c *Cluster) CancelStore(storeID uint64)

CancelStore makes the store with cancel state true.

func (*Cluster) ChangeLeader Uses

func (c *Cluster) ChangeLeader(regionID, leaderPeerID uint64)

ChangeLeader sets the Region's leader Peer. Caller should guarantee the Peer exists.

func (*Cluster) GetAllRegions Uses

func (c *Cluster) GetAllRegions() []*Region

GetAllRegions gets all the regions in the cluster.

func (*Cluster) GetAllStores Uses

func (c *Cluster) GetAllStores() []*metapb.Store

GetAllStores returns all Stores' meta.

func (*Cluster) GetAndCheckStoreByAddr Uses

func (c *Cluster) GetAndCheckStoreByAddr(addr string) (*metapb.Store, error)

GetAndCheckStoreByAddr checks and returns a Store's meta by an addr

func (*Cluster) GetPrevRegionByKey Uses

func (c *Cluster) GetPrevRegionByKey(key []byte) (*metapb.Region, *metapb.Peer)

GetPrevRegionByKey returns the previous Region and its leader whose range contains the key.

func (*Cluster) GetRegion Uses

func (c *Cluster) GetRegion(regionID uint64) (*metapb.Region, uint64)

GetRegion returns a Region's meta and leader ID.

func (*Cluster) GetRegionByID Uses

func (c *Cluster) GetRegionByID(regionID uint64) (*metapb.Region, *metapb.Peer)

GetRegionByID returns the Region and its leader whose ID is regionID.

func (*Cluster) GetRegionByKey Uses

func (c *Cluster) GetRegionByKey(key []byte) (*metapb.Region, *metapb.Peer)

GetRegionByKey returns the Region and its leader whose range contains the key.

func (*Cluster) GetStore Uses

func (c *Cluster) GetStore(storeID uint64) *metapb.Store

GetStore returns a Store's meta.

func (*Cluster) GetStoreByAddr Uses

func (c *Cluster) GetStoreByAddr(addr string) *metapb.Store

GetStoreByAddr returns a Store's meta by an addr.

func (*Cluster) GiveUpLeader Uses

func (c *Cluster) GiveUpLeader(regionID uint64)

GiveUpLeader sets the Region's leader to 0. The Region will have no leader before calling ChangeLeader().

func (*Cluster) Merge Uses

func (c *Cluster) Merge(regionID1, regionID2 uint64)

Merge merges 2 regions, their key ranges should be adjacent.

func (*Cluster) RemovePeer Uses

func (c *Cluster) RemovePeer(regionID, storeID uint64)

RemovePeer removes the Peer from the Region. Note that if the Peer is leader, the Region will have no leader before calling ChangeLeader().

func (*Cluster) RemoveStore Uses

func (c *Cluster) RemoveStore(storeID uint64)

RemoveStore removes a Store from the cluster.

func (*Cluster) ScanRegions Uses

func (c *Cluster) ScanRegions(key []byte, limit int) ([]*metapb.Region, []*metapb.Peer)

ScanRegions returns at most `limit` regions from given `key` and their leaders.

func (*Cluster) ScheduleDelay Uses

func (c *Cluster) ScheduleDelay(startTS, regionID uint64, dur time.Duration)

ScheduleDelay schedules a delay event for a transaction on a region.

func (*Cluster) Split Uses

func (c *Cluster) Split(regionID, newRegionID uint64, key []byte, peerIDs []uint64, leaderPeerID uint64)

Split splits a Region at the key (encoded) and creates new Region.

func (*Cluster) SplitIndex Uses

func (c *Cluster) SplitIndex(mvccStore MVCCStore, tableID, indexID int64, count int)

SplitIndex evenly splits the data in index into count regions. Only works for single store.

func (*Cluster) SplitKeys Uses

func (c *Cluster) SplitKeys(mvccStore MVCCStore, start, end kv.Key, count int)

SplitKeys evenly splits the start, end key into "count" regions. Only works for single store.

func (*Cluster) SplitRaw Uses

func (c *Cluster) SplitRaw(regionID, newRegionID uint64, rawKey []byte, peerIDs []uint64, leaderPeerID uint64) *Region

SplitRaw splits a Region at the key (not encoded) and creates new Region.

func (*Cluster) SplitTable Uses

func (c *Cluster) SplitTable(mvccStore MVCCStore, tableID int64, count int)

SplitTable evenly splits the data in table into count regions. Only works for single store.

func (*Cluster) StartStore Uses

func (c *Cluster) StartStore(storeID uint64)

StartStore starts a store with storeID.

func (*Cluster) StopStore Uses

func (c *Cluster) StopStore(storeID uint64)

StopStore stops a store with storeID.

func (*Cluster) UnCancelStore Uses

func (c *Cluster) UnCancelStore(storeID uint64)

UnCancelStore makes the store with cancel state false.

func (*Cluster) UpdateStoreAddr Uses

func (c *Cluster) UpdateStoreAddr(storeID uint64, addr string)

UpdateStoreAddr updates store address for cluster.

type ErrAbort Uses

type ErrAbort string

ErrAbort means something is wrong and client should abort the txn.

func (ErrAbort) Error Uses

func (e ErrAbort) Error() string

type ErrAlreadyCommitted Uses

type ErrAlreadyCommitted uint64

ErrAlreadyCommitted is returned specially when client tries to rollback a committed lock.

func (ErrAlreadyCommitted) Error Uses

func (e ErrAlreadyCommitted) Error() string

type ErrConflict Uses

type ErrConflict struct {
    StartTS    uint64
    ConflictTS uint64
    Key        []byte
}

ErrConflict is returned when the commitTS of key in the DB is greater than startTS.

func (*ErrConflict) Error Uses

func (e *ErrConflict) Error() string

type ErrDeadlock Uses

type ErrDeadlock struct {
    LockTS         uint64
    LockKey        []byte
    DealockKeyHash uint64
}

ErrDeadlock is returned when deadlock error is detected.

func (*ErrDeadlock) Error Uses

func (e *ErrDeadlock) Error() string

type ErrKeyAlreadyExist Uses

type ErrKeyAlreadyExist struct {
    Key []byte
}

ErrKeyAlreadyExist is returned when key exists but this key has a constraint that it should not exist. Client should return duplicated entry error.

func (*ErrKeyAlreadyExist) Error Uses

func (e *ErrKeyAlreadyExist) Error() string

type ErrLocked Uses

type ErrLocked struct {
    Key     MvccKey
    Primary []byte
    StartTS uint64
    TTL     uint64
    TxnSize uint64
}

ErrLocked is returned when trying to Read/Write on a locked key. Client should backoff or cleanup the lock then retry.

func (*ErrLocked) Error Uses

func (e *ErrLocked) Error() string

Error formats the lock to a string.

type ErrRetryable Uses

type ErrRetryable string

ErrRetryable suggests that client may restart the txn.

func (ErrRetryable) Error Uses

func (e ErrRetryable) Error() string

type Iterator Uses

type Iterator struct {
    iterator.Iterator
    // contains filtered or unexported fields
}

Iterator wraps iterator.Iterator to provide Valid() method.

func (*Iterator) Next Uses

func (iter *Iterator) Next()

Next moves the iterator to the next key/value pair.

func (*Iterator) Valid Uses

func (iter *Iterator) Valid() bool

Valid returns whether the iterator is exhausted.

type MVCCDebugger Uses

type MVCCDebugger interface {
    MvccGetByStartTS(starTS uint64) (*kvrpcpb.MvccInfo, []byte)
    MvccGetByKey(key []byte) *kvrpcpb.MvccInfo
}

MVCCDebugger is for debugging.

type MVCCLevelDB Uses

type MVCCLevelDB struct {
    // contains filtered or unexported fields
}

MVCCLevelDB implements the MVCCStore interface.

func NewMVCCLevelDB Uses

func NewMVCCLevelDB(path string) (*MVCCLevelDB, error)

NewMVCCLevelDB returns a new MVCCLevelDB object.

func (*MVCCLevelDB) BatchGet Uses

func (mvcc *MVCCLevelDB) BatchGet(ks [][]byte, startTS uint64, isoLevel kvrpcpb.IsolationLevel) []Pair

BatchGet implements the MVCCStore interface.

func (*MVCCLevelDB) BatchResolveLock Uses

func (mvcc *MVCCLevelDB) BatchResolveLock(startKey, endKey []byte, txnInfos map[uint64]uint64) error

BatchResolveLock implements the MVCCStore interface.

func (*MVCCLevelDB) CheckTxnStatus Uses

func (mvcc *MVCCLevelDB) CheckTxnStatus(primaryKey []byte, lockTS, callerStartTS, currentTS uint64) (uint64, uint64, error)

CheckTxnStatus checks the primary lock of a transaction to decide its status. The return values are (ttl, commitTS, err): If the transaction is active, this function returns the ttl of the lock; If the transaction is committed, this function returns the commitTS; If the transaction is rollbacked, this function returns (0, 0, nil) Note that CheckTxnStatus may also push forward the `minCommitTS` of the transaction, so it's not simply a read-only operation.

primaryKey + lockTS together could locate the primary lock. callerStartTS is the start ts of reader transaction. currentTS is the current ts, but it may be inaccurate. Just use it to check TTL.

func (*MVCCLevelDB) Cleanup Uses

func (mvcc *MVCCLevelDB) Cleanup(key []byte, startTS uint64) error

Cleanup implements the MVCCStore interface.

func (*MVCCLevelDB) Close Uses

func (mvcc *MVCCLevelDB) Close() error

Close calls leveldb's Close to free resources.

func (*MVCCLevelDB) Commit Uses

func (mvcc *MVCCLevelDB) Commit(keys [][]byte, startTS, commitTS uint64) error

Commit implements the MVCCStore interface.

func (*MVCCLevelDB) DeleteRange Uses

func (mvcc *MVCCLevelDB) DeleteRange(startKey, endKey []byte) error

DeleteRange implements the MVCCStore interface.

func (*MVCCLevelDB) GC Uses

func (mvcc *MVCCLevelDB) GC(startKey, endKey []byte, safePoint uint64) error

GC implements the MVCCStore interface

func (*MVCCLevelDB) Get Uses

func (mvcc *MVCCLevelDB) Get(key []byte, startTS uint64, isoLevel kvrpcpb.IsolationLevel) ([]byte, error)

Get implements the MVCCStore interface. key cannot be nil or []byte{}

func (*MVCCLevelDB) MvccGetByKey Uses

func (mvcc *MVCCLevelDB) MvccGetByKey(key []byte) *kvrpcpb.MvccInfo

MvccGetByKey implements the MVCCDebugger interface.

func (*MVCCLevelDB) MvccGetByStartTS Uses

func (mvcc *MVCCLevelDB) MvccGetByStartTS(starTS uint64) (*kvrpcpb.MvccInfo, []byte)

MvccGetByStartTS implements the MVCCDebugger interface.

func (*MVCCLevelDB) PessimisticLock Uses

func (mvcc *MVCCLevelDB) PessimisticLock(mutations []*kvrpcpb.Mutation, primary []byte, startTS, forUpdateTS uint64, ttl uint64) []error

PessimisticLock writes the pessimistic lock.

func (*MVCCLevelDB) PessimisticRollback Uses

func (mvcc *MVCCLevelDB) PessimisticRollback(keys [][]byte, startTS, forUpdateTS uint64) []error

PessimisticRollback implements the MVCCStore interface.

func (*MVCCLevelDB) Prewrite Uses

func (mvcc *MVCCLevelDB) Prewrite(req *kvrpcpb.PrewriteRequest) []error

Prewrite implements the MVCCStore interface.

func (*MVCCLevelDB) RawBatchDelete Uses

func (mvcc *MVCCLevelDB) RawBatchDelete(keys [][]byte)

RawBatchDelete implements the RawKV interface.

func (*MVCCLevelDB) RawBatchGet Uses

func (mvcc *MVCCLevelDB) RawBatchGet(keys [][]byte) [][]byte

RawBatchGet implements the RawKV interface.

func (*MVCCLevelDB) RawBatchPut Uses

func (mvcc *MVCCLevelDB) RawBatchPut(keys, values [][]byte)

RawBatchPut implements the RawKV interface

func (*MVCCLevelDB) RawDelete Uses

func (mvcc *MVCCLevelDB) RawDelete(key []byte)

RawDelete implements the RawKV interface.

func (*MVCCLevelDB) RawDeleteRange Uses

func (mvcc *MVCCLevelDB) RawDeleteRange(startKey, endKey []byte)

RawDeleteRange implements the RawKV interface.

func (*MVCCLevelDB) RawGet Uses

func (mvcc *MVCCLevelDB) RawGet(key []byte) []byte

RawGet implements the RawKV interface.

func (*MVCCLevelDB) RawPut Uses

func (mvcc *MVCCLevelDB) RawPut(key, value []byte)

RawPut implements the RawKV interface.

func (*MVCCLevelDB) RawReverseScan Uses

func (mvcc *MVCCLevelDB) RawReverseScan(startKey, endKey []byte, limit int) []Pair

RawReverseScan implements the RawKV interface. Scan the range of [endKey, startKey) It doesn't support Scanning from "", because locating the last Region is not yet implemented.

func (*MVCCLevelDB) RawScan Uses

func (mvcc *MVCCLevelDB) RawScan(startKey, endKey []byte, limit int) []Pair

RawScan implements the RawKV interface.

func (*MVCCLevelDB) ResolveLock Uses

func (mvcc *MVCCLevelDB) ResolveLock(startKey, endKey []byte, startTS, commitTS uint64) error

ResolveLock implements the MVCCStore interface.

func (*MVCCLevelDB) ReverseScan Uses

func (mvcc *MVCCLevelDB) ReverseScan(startKey, endKey []byte, limit int, startTS uint64, isoLevel kvrpcpb.IsolationLevel) []Pair

ReverseScan implements the MVCCStore interface. The search range is [startKey, endKey).

func (*MVCCLevelDB) Rollback Uses

func (mvcc *MVCCLevelDB) Rollback(keys [][]byte, startTS uint64) error

Rollback implements the MVCCStore interface.

func (*MVCCLevelDB) Scan Uses

func (mvcc *MVCCLevelDB) Scan(startKey, endKey []byte, limit int, startTS uint64, isoLevel kvrpcpb.IsolationLevel) []Pair

Scan implements the MVCCStore interface.

func (*MVCCLevelDB) ScanLock Uses

func (mvcc *MVCCLevelDB) ScanLock(startKey, endKey []byte, maxTS uint64) ([]*kvrpcpb.LockInfo, error)

ScanLock implements the MVCCStore interface.

func (*MVCCLevelDB) TxnHeartBeat Uses

func (mvcc *MVCCLevelDB) TxnHeartBeat(key []byte, startTS uint64, adviseTTL uint64) (uint64, error)

TxnHeartBeat implements the MVCCStore interface.

type MVCCStore Uses

type MVCCStore interface {
    Get(key []byte, startTS uint64, isoLevel kvrpcpb.IsolationLevel) ([]byte, error)
    Scan(startKey, endKey []byte, limit int, startTS uint64, isoLevel kvrpcpb.IsolationLevel) []Pair
    ReverseScan(startKey, endKey []byte, limit int, startTS uint64, isoLevel kvrpcpb.IsolationLevel) []Pair
    BatchGet(ks [][]byte, startTS uint64, isoLevel kvrpcpb.IsolationLevel) []Pair
    PessimisticLock(mutations []*kvrpcpb.Mutation, primary []byte, startTS, forUpdateTS uint64, ttl uint64) []error
    PessimisticRollback(keys [][]byte, startTS, forUpdateTS uint64) []error
    Prewrite(req *kvrpcpb.PrewriteRequest) []error
    Commit(keys [][]byte, startTS, commitTS uint64) error
    Rollback(keys [][]byte, startTS uint64) error
    Cleanup(key []byte, startTS uint64) error
    ScanLock(startKey, endKey []byte, maxTS uint64) ([]*kvrpcpb.LockInfo, error)
    TxnHeartBeat(primaryKey []byte, startTS uint64, adviseTTL uint64) (uint64, error)
    ResolveLock(startKey, endKey []byte, startTS, commitTS uint64) error
    BatchResolveLock(startKey, endKey []byte, txnInfos map[uint64]uint64) error
    GC(startKey, endKey []byte, safePoint uint64) error
    DeleteRange(startKey, endKey []byte) error
    CheckTxnStatus(primaryKey []byte, lockTS uint64, startTS, currentTS uint64) (ttl, commitTS uint64, err error)
    Close() error
}

MVCCStore is a mvcc key-value storage.

func MustNewMVCCStore Uses

func MustNewMVCCStore() MVCCStore

MustNewMVCCStore is used for testing, use NewMVCCLevelDB instead.

type MvccKey Uses

type MvccKey []byte

MvccKey is the encoded key type. On TiKV, keys are encoded before they are saved into storage engine.

func NewMvccKey Uses

func NewMvccKey(key []byte) MvccKey

NewMvccKey encodes a key into MvccKey.

func (MvccKey) Raw Uses

func (key MvccKey) Raw() []byte

Raw decodes a MvccKey to original key.

type Pair Uses

type Pair struct {
    Key   []byte
    Value []byte
    Err   error
}

Pair is a KV pair read from MvccStore or an error if any occurs.

type RPCClient Uses

type RPCClient struct {
    Cluster   *Cluster
    MvccStore MVCCStore
    // contains filtered or unexported fields
}

RPCClient sends kv RPC calls to mock cluster. RPCClient mocks the behavior of a rpc client at tikv's side.

func NewRPCClient Uses

func NewRPCClient(cluster *Cluster, mvccStore MVCCStore) *RPCClient

NewRPCClient creates an RPCClient. Note that close the RPCClient may close the underlying MvccStore.

func NewTiKVAndPDClient Uses

func NewTiKVAndPDClient(cluster *Cluster, mvccStore MVCCStore, path string) (*RPCClient, pd.Client, error)

NewTiKVAndPDClient creates a TiKV client and PD client from options.

func (*RPCClient) Close Uses

func (c *RPCClient) Close() error

Close closes the client.

func (*RPCClient) SendRequest Uses

func (c *RPCClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error)

SendRequest sends a request to mock cluster.

type RawKV Uses

type RawKV interface {
    RawGet(key []byte) []byte
    RawBatchGet(keys [][]byte) [][]byte
    RawScan(startKey, endKey []byte, limit int) []Pair        // Scan the range of [startKey, endKey)
    RawReverseScan(startKey, endKey []byte, limit int) []Pair // Scan the range of [endKey, startKey)
    RawPut(key, value []byte)
    RawBatchPut(keys, values [][]byte)
    RawDelete(key []byte)
    RawBatchDelete(keys [][]byte)
    RawDeleteRange(startKey, endKey []byte)
}

RawKV is a key-value storage. MVCCStore can be implemented upon it with timestamp encoded into key.

type Region Uses

type Region struct {
    Meta *metapb.Region
    // contains filtered or unexported fields
}

Region is the Region meta data.

type Store Uses

type Store struct {
    // contains filtered or unexported fields
}

Store is the Store's meta data.

Package mocktikv imports 52 packages (graph) and is imported by 5 packages. Updated 2019-09-15. Refresh now. Tools for package owners.