mocktikv

package
v0.0.0-...-4295780 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2017 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FlagIgnoreTruncate indicates if truncate error should be ignored.
	// Read-only statements should ignore truncate error, write statements should not ignore truncate error.
	FlagIgnoreTruncate uint64 = 1
	// FlagTruncateAsWarning indicates if truncate error should be returned as warning.
	// This flag only matters if FlagIgnoreTruncate is not set, in strict sql mode, truncate error should
	// be returned as error, in non-strict sql mode, truncate error should be saved as warning.
	FlagTruncateAsWarning uint64 = 1 << 1
)

Flags are used by tipb.SelectRequest.Flags to handle execution mode, like how to handle truncate error.

Variables

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

ErrInvalidEncodedKey describes parsing an invalid format of EncodedKey.

Functions

func BootstrapWithMultiRegions

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

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

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

BootstrapWithSingleStore initializes a Cluster with 1 Region and 1 Store.

func NewPDClient

func NewPDClient(cluster *Cluster) pd.Client

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

Types

type Cluster

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

func NewCluster() *Cluster

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

func (*Cluster) AddPeer

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

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

func (*Cluster) AddStore

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

AddStore add a new Store to the cluster.

func (*Cluster) AllocID

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

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

AllocIDs creates multiple IDs.

func (*Cluster) Bootstrap

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

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

func (*Cluster) CancelStore

func (c *Cluster) CancelStore(storeID uint64)

CancelStore makes the store with cancel state true.

func (*Cluster) ChangeLeader

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

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

func (*Cluster) GetAllRegions

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

GetAllRegions gets all the regions in the cluster.

func (*Cluster) GetAndCheckStoreByAddr

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

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

func (*Cluster) GetRegion

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

GetRegion returns a Region's meta and leader ID.

func (*Cluster) GetRegionByID

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

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

func (*Cluster) GetRegionByKey

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

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

GetStore returns a Store's meta.

func (*Cluster) GetStoreByAddr

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

GetStoreByAddr returns a Store's meta by an addr.

func (*Cluster) GiveUpLeader

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

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

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

func (*Cluster) RemovePeer

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

func (c *Cluster) RemoveStore(storeID uint64)

RemoveStore removes a Store from the cluster.

func (*Cluster) Split

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

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) SplitRaw

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

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

func (*Cluster) SplitTable

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

func (c *Cluster) StartStore(storeID uint64)

StartStore starts a store with storeID.

func (*Cluster) StopStore

func (c *Cluster) StopStore(storeID uint64)

StopStore stops a store with storeID.

func (*Cluster) UnCancelStore

func (c *Cluster) UnCancelStore(storeID uint64)

UnCancelStore makes the store with cancel state false.

func (*Cluster) UpdateStoreAddr

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

UpdateStoreAddr updates store address for cluster.

type ErrAbort

type ErrAbort string

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

func (ErrAbort) Error

func (e ErrAbort) Error() string

type ErrAlreadyCommitted

type ErrAlreadyCommitted uint64

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

func (ErrAlreadyCommitted) Error

func (e ErrAlreadyCommitted) Error() string

type ErrLocked

type ErrLocked struct {
	Key     MvccKey
	Primary []byte
	StartTS uint64
	TTL     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

func (e *ErrLocked) Error() string

Error formats the lock to a string.

type ErrRetryable

type ErrRetryable string

ErrRetryable suggests that client may restart the txn. e.g. write conflict.

func (ErrRetryable) Error

func (e ErrRetryable) Error() string

type Iterator

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

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

func (*Iterator) Next

func (iter *Iterator) Next()

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

func (*Iterator) Valid

func (iter *Iterator) Valid() bool

Valid returns whether the iterator is exhausted.

type MVCCDebugger

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

MVCCDebugger is for debugging.

type MVCCLevelDB

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

MVCCLevelDB implements the MVCCStore interface.

func NewMVCCLevelDB

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

NewMVCCLevelDB returns a new MVCCLevelDB object.

func (*MVCCLevelDB) BatchGet

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

BatchGet implements the MVCCStore interface.

func (*MVCCLevelDB) Cleanup

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

Cleanup implements the MVCCStore interface.

func (*MVCCLevelDB) Commit

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

Commit implements the MVCCStore interface.

func (*MVCCLevelDB) Get

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) Prewrite

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

Prewrite implements the MVCCStore interface.

func (*MVCCLevelDB) ResolveLock

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

ResolveLock implements the MVCCStore interface.

func (*MVCCLevelDB) ReverseScan

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

ReverseScan implements the MVCCStore interface.

func (*MVCCLevelDB) Rollback

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

Rollback implements the MVCCStore interface.

func (*MVCCLevelDB) Scan

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

Scan implements the MVCCStore interface.

func (*MVCCLevelDB) ScanLock

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

ScanLock implements the MVCCStore interface.

type MVCCStore

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
	Prewrite(mutations []*kvrpcpb.Mutation, primary []byte, startTS uint64, ttl uint64) []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)
	ResolveLock(startKey, endKey []byte, startTS, commitTS uint64) error
}

MVCCStore is a mvcc key-value storage.

type MvccKey

type MvccKey []byte

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

func NewMvccKey

func NewMvccKey(key []byte) MvccKey

NewMvccKey encodes a key into MvccKey.

func (MvccKey) Raw

func (key MvccKey) Raw() []byte

Raw decodes a MvccKey to original key.

type MvccStore

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

MvccStore is an in-memory, multi-versioned, transaction-supported kv storage.

func NewMvccStore

func NewMvccStore() *MvccStore

NewMvccStore creates a MvccStore.

func (*MvccStore) BatchGet

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

BatchGet gets values with keys and ts.

func (*MvccStore) Cleanup

func (s *MvccStore) Cleanup(key []byte, startTS uint64) error

Cleanup cleanups a lock, often used when resolving a expired lock.

func (*MvccStore) Commit

func (s *MvccStore) Commit(keys [][]byte, startTS, commitTS uint64) error

Commit commits the lock on a key. (2nd phase of 2PC).

func (*MvccStore) Get

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

Get reads a key by ts.

func (*MvccStore) MvccGetByKey

func (s *MvccStore) MvccGetByKey(key []byte) *kvrpcpb.MvccInfo

MvccGetByKey gets mvcc info for the key

func (*MvccStore) MvccGetByStartTS

func (s *MvccStore) MvccGetByStartTS(startKey, endKey []byte, starTS uint64) (*kvrpcpb.MvccInfo, []byte)

MvccGetByStartTS gets mvcc info for the primary key with startTS

func (*MvccStore) Prewrite

func (s *MvccStore) Prewrite(mutations []*kvrpcpb.Mutation, primary []byte, startTS uint64, ttl uint64) []error

Prewrite acquires a lock on a key. (1st phase of 2PC).

func (*MvccStore) RawDelete

func (s *MvccStore) RawDelete(key []byte)

RawDelete deletes a key-value pair.

func (*MvccStore) RawGet

func (s *MvccStore) RawGet(key []byte) []byte

RawGet queries value with the key.

func (*MvccStore) RawPut

func (s *MvccStore) RawPut(key, value []byte)

RawPut stores a key-value pair.

func (*MvccStore) RawScan

func (s *MvccStore) RawScan(startKey, endKey []byte, limit int) []Pair

RawScan reads up to a limited number of rawkv Pairs.

func (*MvccStore) ResolveLock

func (s *MvccStore) ResolveLock(startKey, endKey []byte, startTS, commitTS uint64) error

ResolveLock resolves all orphan locks belong to a transaction.

func (*MvccStore) ReverseScan

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

ReverseScan reads up to a limited number of Pairs that greater than or equal to startKey and less than endKey in descending order.

func (*MvccStore) Rollback

func (s *MvccStore) Rollback(keys [][]byte, startTS uint64) error

Rollback cleanups multiple locks, often used when rolling back a conflict txn.

func (*MvccStore) Scan

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

Scan reads up to a limited number of Pairs that greater than or equal to startKey and less than endKey.

func (*MvccStore) ScanLock

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

ScanLock scans all orphan locks in a Region.

type Pair

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

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

type RPCClient

type RPCClient struct {
	Cluster   *Cluster
	MvccStore MVCCStore
}

RPCClient sends kv RPC calls to mock cluster.

func NewRPCClient

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

NewRPCClient creates an RPCClient.

func (*RPCClient) Close

func (c *RPCClient) Close() error

Close closes the client.

func (*RPCClient) SendReq

func (c *RPCClient) SendReq(ctx goctx.Context, addr string, req *tikvrpc.Request) (*tikvrpc.Response, error)

SendReq sends a request to mock cluster.

type RawKV

type RawKV interface {
	RawGet(key []byte) []byte
	RawScan(startKey, endKey []byte, limit int) []Pair
	RawPut(key, value []byte)
	RawDelete(key []byte)
}

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

type Region

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

Region is the Region meta data.

type Store

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

Store is the Store's meta data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL