Documentation ¶
Index ¶
- Constants
- Variables
- func CalcNeededBatchSize(base, n, increment, offset int64, isUnsigned bool) int64
- func CalcSequenceBatchSize(base, size, increment, offset, MIN, MAX int64) (int64, error)
- func DecodeCmpUintToInt(u uint64) int64
- func EncodeIntToCmpUint(v int64) uint64
- func GetStep() int64
- func NextStep(curStep int64, consumeDur time.Duration) int64
- func SeekToFirstAutoIDSigned(base, increment, offset int64) int64
- func SeekToFirstAutoIDUnSigned(base, increment, offset uint64) uint64
- func SeekToFirstSequenceValue(base, increment, offset, MIN, MAX int64) (int64, bool)
- func SetStep(s int64)
- type AllocOption
- type Allocator
- type AllocatorType
- type Allocators
- type CustomAutoIncCacheOption
Constants ¶
const ( // SystemSchemaIDFlag is the system schema/table id flag, uses the highest bit position as system schema ID flag, it's exports for test. SystemSchemaIDFlag = 1 << 62 // InformationSchemaDBID is the information_schema schema id, it's exports for test. InformationSchemaDBID int64 = SystemSchemaIDFlag | 1 // PerformanceSchemaDBID is the performance_schema schema id, it's exports for test. PerformanceSchemaDBID int64 = SystemSchemaIDFlag | 10000 // MetricSchemaDBID is the metrics_schema schema id, it's exported for test. MetricSchemaDBID int64 = SystemSchemaIDFlag | 20000 )
Attention: For reading cluster TiDB memory tables, the system schema/table should be same. Once the system schema/table id been allocated, it can't be changed any more. Change the system schema/table id may have the compatibility problem.
const ( // AutoRandomPKisNotHandleErrMsg indicates the auto_random column attribute is defined on a non-primary key column, or the table's primary key is not a single integer column. AutoRandomPKisNotHandleErrMsg = "column %s is not the single integer primary key, or alter-primary-key is enabled" // AutoRandomExperimentalDisabledErrMsg is reported when the experimental option allow-auto-random is not enabled. AutoRandomExperimentalDisabledErrMsg = "" /* 139-byte string literal not displayed */ // AutoRandomIncompatibleWithAutoIncErrMsg is reported when auto_random and auto_increment are specified on the same column. AutoRandomIncompatibleWithAutoIncErrMsg = "auto_random is incompatible with auto_increment" // AutoRandomIncompatibleWithDefaultValueErrMsg is reported when auto_random and default are specified on the same column. AutoRandomIncompatibleWithDefaultValueErrMsg = "auto_random is incompatible with default" // AutoRandomOverflowErrMsg is reported when auto_random is greater than max length of a MySQL data type. AutoRandomOverflowErrMsg = "Bits of column `%s` is %d, but auto_random bits is %d. Max allowed auto_random bits for column `%s` is %d" // AutoRandomModifyColTypeErrMsg is reported when a user is trying to modify the type of a column specified with auto_random. AutoRandomModifyColTypeErrMsg = "modifying the auto_random column type is not supported" // AutoRandomAlterErrMsg is reported when a user is trying to add/drop/modify the value of auto_random attribute. AutoRandomAlterErrMsg = "adding/dropping/modifying auto_random is not supported" // AutoRandomNonPositive is reported then a user specifies a non-positive value for auto_random. AutoRandomNonPositive = "the value of auto_random should be positive" // AutoRandomAvailableAllocTimesNote is reported when a table containing auto_random is created. AutoRandomAvailableAllocTimesNote = "Available implicit allocation times: %d" )
const DefaultAutoRandomBits = 5
DefaultAutoRandomBits is the default value of auto sharding.
const RowIDBitLength = 64
RowIDBitLength is the bit number of a row id in TiDB.
Variables ¶
var ( ErrAutoincReadFailed = terror.ClassAutoid.New(mysql.ErrAutoincReadFailed, mysql.MySQLErrName[mysql.ErrAutoincReadFailed]) ErrWrongAutoKey = terror.ClassAutoid.New(mysql.ErrWrongAutoKey, mysql.MySQLErrName[mysql.ErrWrongAutoKey]) ErrAutoRandReadFailed = terror.ClassAutoid.New(mysql.ErrAutoRandReadFailed, mysql.MySQLErrName[mysql.ErrAutoRandReadFailed]) )
Error instances.
Functions ¶
func CalcNeededBatchSize ¶
CalcNeededBatchSize is used to calculate batch size for autoID allocation. It firstly seeks to the first valid position based on increment and offset, then plus the length remained, which could be (n-1) * increment.
func CalcSequenceBatchSize ¶
CalcSequenceBatchSize calculate the next sequence batch size.
func DecodeCmpUintToInt ¶
DecodeCmpUintToInt decodes the u that encoded by EncodeIntToCmpUint
func EncodeIntToCmpUint ¶
EncodeIntToCmpUint make int v to comparable uint type
func SeekToFirstAutoIDSigned ¶
SeekToFirstAutoIDSigned seeks to the next valid signed position.
func SeekToFirstAutoIDUnSigned ¶
SeekToFirstAutoIDUnSigned seeks to the next valid unsigned position.
func SeekToFirstSequenceValue ¶
SeekToFirstSequenceValue seeks to the next valid value (must be in range of [MIN, MAX]), the bool indicates whether the first value is got. The seeking formula is describe as below:
nr := (base + increment - offset) / increment
first := nr*increment + offset Because formula computation will overflow Int64, so we transfer it to uint64 for distance computation.
Types ¶
type AllocOption ¶
type AllocOption interface {
ApplyOn(*allocator)
}
AllocOption is a interface to define allocator custom options coming in future.
type Allocator ¶
type Allocator interface { // Alloc allocs N consecutive autoID for table with tableID, returning (min, max] of the allocated autoID batch. // It gets a batch of autoIDs at a time. So it does not need to access storage for each call. // The consecutive feature is used to insert multiple rows in a statement. // increment & offset is used to validate the start position (the allocator's base is not always the last allocated id). // The returned range is (min, max]: // case increment=1 & offset=1: you can derive the ids like min+1, min+2... max. // case increment=x & offset=y: you firstly need to seek to firstID by `SeekToFirstAutoIDXXX`, then derive the IDs like firstID, firstID + increment * 2... in the caller. Alloc(tableID int64, n uint64, increment, offset int64) (int64, int64, error) // AllocSeqCache allocs sequence batch value cached in table level(rather than in alloc), the returned range covering // the size of sequence cache with it's increment. The returned round indicates the sequence cycle times if it is with // cycle option. AllocSeqCache(sequenceID int64) (min int64, max int64, round int64, err error) // Rebase rebases the autoID base for table with tableID and the new base value. // If allocIDs is true, it will allocate some IDs and save to the cache. // If allocIDs is false, it will not allocate IDs. Rebase(tableID, newBase int64, allocIDs bool) error // RebaseSeq rebases the sequence value in number axis with tableID and the new base value. RebaseSeq(table, newBase int64) (int64, bool, error) // Base return the current base of Allocator. Base() int64 // End is only used for test. End() int64 // NextGlobalAutoID returns the next global autoID. NextGlobalAutoID(tableID int64) (int64, error) GetType() AllocatorType }
Allocator is an auto increment id generator. Just keep id unique actually.
func NewAllocator ¶
func NewAllocator(store kv.Storage, dbID int64, isUnsigned bool, allocType AllocatorType, opts ...AllocOption) Allocator
NewAllocator returns a new auto increment id generator on the store.
func NewSequenceAllocator ¶
NewSequenceAllocator returns a new sequence value generator on the store.
type AllocatorType ¶
type AllocatorType = uint8
AllocatorType is the type of allocator for generating auto-id. Different type of allocators use different key-value pairs.
const ( // RowIDAllocType indicates the allocator is used to allocate row id. RowIDAllocType AllocatorType = iota // AutoIncrementType indicates the allocator is used to allocate auto increment value. AutoIncrementType // AutoRandomType indicates the allocator is used to allocate auto-shard id. AutoRandomType // SequenceType indicates the allocator is used to allocate sequence value. SequenceType )
type Allocators ¶
type Allocators []Allocator
Allocators represents a set of `Allocator`s.
func NewAllocators ¶
func NewAllocators(allocators ...Allocator) Allocators
NewAllocators packs multiple `Allocator`s into Allocators.
func NewAllocatorsFromTblInfo ¶
func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.TableInfo) Allocators
NewAllocatorsFromTblInfo creates an array of allocators of different types with the information of model.TableInfo.
func (Allocators) Get ¶
func (all Allocators) Get(allocType AllocatorType) Allocator
Get returns the Allocator according to the AllocatorType.
type CustomAutoIncCacheOption ¶
type CustomAutoIncCacheOption int64
CustomAutoIncCacheOption is one kind of AllocOption to customize the allocator step length.
func (CustomAutoIncCacheOption) ApplyOn ¶
func (step CustomAutoIncCacheOption) ApplyOn(alloc *allocator)
ApplyOn is implement the AllocOption interface.