tidb: github.com/pingcap/tidb/meta/autoid Index | Files

package autoid

import "github.com/pingcap/tidb/meta/autoid"


Package Files

autoid.go errors.go


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 = "column %s is not the single integer primary key, or alter-primary-key is enabled"

    AutoRandomExperimentalDisabledErrMsg = "" /* 141 byte string literal not displayed */

    AutoRandomIncompatibleWithAutoIncErrMsg = "auto_random is incompatible with auto_increment"

    AutoRandomIncompatibleWithDefaultValueErrMsg = "auto_random is incompatible with default"

    AutoRandomOverflowErrMsg = "max allowed auto_random bits is %d, but got %d on column `%s`"

    AutoRandomModifyColTypeErrMsg = "modifying the auto_random column type is not supported"

    AutoRandomAlterErrMsg = "adding/dropping/modifying auto_random is not supported"

    AutoRandomNonPositive = "the value of auto_random should be positive"

    AutoRandomAvailableAllocTimesNote = "Available implicit allocation times: %d"

    AutoRandomExplicitInsertDisabledErrMsg = "Explicit insertion on auto_random column is disabled. Try to set @@allow_auto_random_explicit_insert = true."

    AutoRandomOnNonBigIntColumn = "auto_random option must be defined on `bigint` column, but not on `%s` column"
const DefaultAutoRandomBits = 5

DefaultAutoRandomBits is the default value of auto sharding.

const MaxAutoRandomBits = 15

MaxAutoRandomBits is the max value of auto sharding.

const RowIDBitLength = 64

RowIDBitLength is the bit number of a row id in TiDB.


var (
    ErrAutoincReadFailed    = terror.ClassAutoid.New(mysql.ErrAutoincReadFailed, mysql.MySQLErrName[mysql.ErrAutoincReadFailed])
    ErrWrongAutoKey         = terror.ClassAutoid.New(mysql.ErrWrongAutoKey, mysql.MySQLErrName[mysql.ErrWrongAutoKey])
    ErrInvalidAllocatorType = terror.ClassAutoid.New(mysql.ErrUnknownAllocatorType, mysql.MySQLErrName[mysql.ErrUnknownAllocatorType])
    ErrAutoRandReadFailed   = terror.ClassAutoid.New(mysql.ErrAutoRandReadFailed, mysql.MySQLErrName[mysql.ErrAutoRandReadFailed])

Error instances.

func CalcNeededBatchSize Uses

func CalcNeededBatchSize(base, n, increment, offset int64, isUnsigned bool) int64

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 Uses

func CalcSequenceBatchSize(base, size, increment, offset, MIN, MAX int64) (int64, error)

CalcSequenceBatchSize calculate the next sequence batch size.

func DecodeCmpUintToInt Uses

func DecodeCmpUintToInt(u uint64) int64

DecodeCmpUintToInt decodes the u that encoded by EncodeIntToCmpUint

func EncodeIntToCmpUint Uses

func EncodeIntToCmpUint(v int64) uint64

EncodeIntToCmpUint make int v to comparable uint type

func GetStep Uses

func GetStep() int64

GetStep is only used by tests

func NextStep Uses

func NextStep(curStep int64, consumeDur time.Duration) int64

NextStep return new auto id step according to previous step and consuming time.

func SeekToFirstAutoIDSigned Uses

func SeekToFirstAutoIDSigned(base, increment, offset int64) int64

SeekToFirstAutoIDSigned seeks to the next valid signed position.

func SeekToFirstAutoIDUnSigned Uses

func SeekToFirstAutoIDUnSigned(base, increment, offset uint64) uint64

SeekToFirstAutoIDUnSigned seeks to the next valid unsigned position.

func SeekToFirstSequenceValue Uses

func SeekToFirstSequenceValue(base, increment, offset, MIN, MAX int64) (int64, bool)

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.

func SetStep Uses

func SetStep(s int64)

SetStep is only used by tests

type AllocOption Uses

type AllocOption interface {

AllocOption is a interface to define allocator custom options coming in future.

type Allocator Uses

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 Uses

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 Uses

func NewSequenceAllocator(store kv.Storage, dbID int64, info *model.SequenceInfo) Allocator

NewSequenceAllocator returns a new sequence value generator on the store.

type AllocatorType Uses

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.
    // AutoRandomType indicates the allocator is used to allocate auto-shard id.
    // SequenceType indicates the allocator is used to allocate sequence value.

type Allocators Uses

type Allocators []Allocator

Allocators represents a set of `Allocator`s.

func NewAllocators Uses

func NewAllocators(allocators ...Allocator) Allocators

NewAllocators packs multiple `Allocator`s into Allocators.

func NewAllocatorsFromTblInfo Uses

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 Uses

func (all Allocators) Get(allocType AllocatorType) Allocator

Get returns the Allocator according to the AllocatorType.

type AutoRandomIDLayout Uses

type AutoRandomIDLayout struct {
    FieldType *types.FieldType
    ShardBits uint64
    // Derived fields.
    TypeBitsLength  uint64
    IncrementalBits uint64
    HasSignBit      bool

AutoRandomIDLayout is used to calculate the bits length of different section in auto_random id. The primary key with auto_random can only be `bigint` column, the total layout length of auto random is 64 bits. These are two type of layout: 1. Signed bigint:

| [sign_bit] | [shard_bits] | [incremental_bits] |
sign_bit(1 fixed) + shard_bits(15 max) + incremental_bits(the rest) = total_layout_bits(64 fixed)

2. Unsigned bigint:

| [shard_bits] | [incremental_bits] |
shard_bits(15 max) + incremental_bits(the rest) = total_layout_bits(64 fixed)

Please always use NewAutoRandomIDLayout() to instantiate.

func NewAutoRandomIDLayout Uses

func NewAutoRandomIDLayout(fieldType *types.FieldType, shardBits uint64) *AutoRandomIDLayout

NewAutoRandomIDLayout create an instance of AutoRandomIDLayout.

func (*AutoRandomIDLayout) IncrementalBitsCapacity Uses

func (l *AutoRandomIDLayout) IncrementalBitsCapacity() uint64

IncrementalBitsCapacity returns the max capacity of incremental section of the current layout.

func (*AutoRandomIDLayout) IncrementalMask Uses

func (l *AutoRandomIDLayout) IncrementalMask() int64

IncrementalMask returns 00..0[11..1], where [xxx] is the incremental section of the current layout.

type CustomAutoIncCacheOption Uses

type CustomAutoIncCacheOption int64

CustomAutoIncCacheOption is one kind of AllocOption to customize the allocator step length.

func (CustomAutoIncCacheOption) ApplyOn Uses

func (step CustomAutoIncCacheOption) ApplyOn(alloc *allocator)

ApplyOn is implement the AllocOption interface.

Package autoid imports 17 packages (graph) and is imported by 300 packages. Updated 2020-05-28. Refresh now. Tools for package owners.