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 metric_schema schema id, it's exported for test.
    MetricSchemaDBID int64 = SystemSchemaIDFlag | 20000
    // InspectionSchemaDBID is the inspection_schema id, it's exports for test.
    InspectionSchemaDBID int64 = SystemSchemaIDFlag | 30000

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 = "auto_random = %d will overflow. The max length of bits is %d"

    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"
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.


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.

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 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 SetStep Uses

func SetStep(s int64)

SetStep is only used by tests

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)

    // 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
    // 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) Allocator

NewAllocator returns a new auto increment id 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.

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.

Package autoid imports 15 packages (graph) and is imported by 302 packages. Updated 2020-02-15. Refresh now. Tools for package owners.