session

package
v1.1.0-beta.0...-3b69f59 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 151 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// MockSimpleUpgradeToVerLatest is used to indicate the use of the simple mock bootstrapVersion, this is just a few simple DDL operations.
	MockSimpleUpgradeToVerLatest = 1
)
View Source
const StoreBootstrappedKey = "bootstrap"

StoreBootstrappedKey is used by store.G/SetOption to store related bootstrap context for kv.Storage.

View Source
const (

	// TidbNewCollationEnabled The variable name in mysql.tidb table and it will indicate if the new collations are enabled in the TiDB cluster.
	TidbNewCollationEnabled = "new_collation_enabled"
)

Variables

View Source
var (

	// DDLJobTables is a list of tables definitions used in concurrent DDL.
	DDLJobTables = []TableBasicInfo{
		{ID: metadef.TiDBDDLJobTableID, Name: "tidb_ddl_job", SQL: metadef.CreateTiDBDDLJobTable},
		{ID: metadef.TiDBDDLReorgTableID, Name: "tidb_ddl_reorg", SQL: metadef.CreateTiDBReorgTable},
		{ID: metadef.TiDBDDLHistoryTableID, Name: "tidb_ddl_history", SQL: metadef.CreateTiDBDDLHistoryTable},
	}
	// MDLTables is a list of tables definitions used for metadata lock.
	MDLTables = []TableBasicInfo{
		{ID: metadef.TiDBMDLInfoTableID, Name: "tidb_mdl_info", SQL: metadef.CreateTiDBMDLTable},
	}
	// BackfillTables is a list of tables definitions used in dist reorg DDL.
	BackfillTables = []TableBasicInfo{
		{ID: metadef.TiDBBackgroundSubtaskTableID, Name: "tidb_background_subtask", SQL: metadef.CreateTiDBBackgroundSubtaskTable},
		{ID: metadef.TiDBBackgroundSubtaskHistoryTableID, Name: "tidb_background_subtask_history", SQL: metadef.CreateTiDBBackgroundSubtaskHistoryTable},
	}
	// DDLNotifierTables contains the table definitions used in DDL notifier.
	// It only contains the notifier table.
	// Put it here to reuse a unified initialization function and make it easier to find.
	DDLNotifierTables = []TableBasicInfo{
		{ID: metadef.TiDBDDLNotifierTableID, Name: "tidb_ddl_notifier", SQL: metadef.CreateTiDBDDLNotifierTable},
	}
)
View Source
var (
	// GetBootstrapVersion is used in test
	GetBootstrapVersion = getBootstrapVersion
	// CurrentBootstrapVersion is used in test
	CurrentBootstrapVersion = currentBootstrapVersion
	// TiDBDDLTableVersionForTest is used in test
	TiDBDDLTableVersionForTest = tidbDDLTableVersion
)
View Source
var (
	ErrForUpdateCantRetry = dbterror.ClassSession.NewStd(errno.ErrForUpdateCantRetry)
)

Session errors.

View Source
var ErrNonTransactionalJobFailure = dbterror.ClassSession.NewStd(errno.ErrNonTransactionalJobFailure)

ErrNonTransactionalJobFailure is the error when a non-transactional job fails. The error is returned and following jobs are canceled.

View Source
var MockUpgradeToVerLatestKind = defaultMockUpgradeToVerLatest

MockUpgradeToVerLatestKind is used to indicate the use of different mock bootstrapVersion.

View Source
var SchemaChangedWithoutRetry uint32

SchemaChangedWithoutRetry is used for testing.

View Source
var (
	// SupportUpgradeHTTPOpVer is exported for testing.
	// The minimum version of the upgrade by paused user DDL can be notified through the HTTP API.
	SupportUpgradeHTTPOpVer int64 = version174
)
View Source
var TestHook = TestCallback{}

TestHook is exported for testing.

View Source
var WithMockUpgrade *bool

WithMockUpgrade is a flag identify whether tests run with mock upgrading.

Functions

func BootstrapSession

func BootstrapSession(store kv.Storage) (*domain.Domain, error)

BootstrapSession bootstrap session and domain.

func BootstrapSession4DistExecution

func BootstrapSession4DistExecution(store kv.Storage) (*domain.Domain, error)

BootstrapSession4DistExecution bootstrap session and dom for Distributed execution test, only for unit testing.

func CreateSession

func CreateSession(store kv.Storage) (sessionapi.Session, error)

CreateSession creates a new session environment.

func CreateSession4Test

func CreateSession4Test(store kv.Storage) (sessionapi.Session, error)

CreateSession4Test creates a new session environment for test.

func CreateSession4TestWithOpt

func CreateSession4TestWithOpt(store kv.Storage, opt *Opt) (sessionapi.Session, error)

CreateSession4TestWithOpt creates a new session environment for test.

func CreateSessionAndSetID

func CreateSessionAndSetID(t *testing.T, store kv.Storage) sessionapi.Session

CreateSessionAndSetID creates a session and set connection ID.

func CreateSessionWithDomain

func CreateSessionWithDomain(store kv.Storage, dom *domain.Domain) (*session, error)

CreateSessionWithDomain creates a new Session and binds it with a Domain. We need this because when we start DDL in Domain, the DDL need a session to change some system tables. But at that time, we have been already in a lock context, which cause we can't call createSession directly.

func CreateSessionWithOpt

func CreateSessionWithOpt(store kv.Storage, opt *Opt) (sessionapi.Session, error)

CreateSessionWithOpt creates a new session environment with option. Use default option if opt is nil.

func CreateStoreAndBootstrap

func CreateStoreAndBootstrap(t *testing.T) (kv.Storage, *domain.Domain)

CreateStoreAndBootstrap creates a mock store and bootstrap it.

func DisableRunBootstrapSQLFileInTest

func DisableRunBootstrapSQLFileInTest()

DisableRunBootstrapSQLFileInTest only used for test

func DisableStats4Test

func DisableStats4Test()

DisableStats4Test disables the stats for tests.

func ExecRestrictedStmt4Test

func ExecRestrictedStmt4Test(ctx context.Context, s sessionapi.Session,
	stmtNode ast.StmtNode, opts ...sqlexec.OptionFuncAlias) (
	[]chunk.Row, []*resolve.ResultField, error)

ExecRestrictedStmt4Test wrapper `(s *session) ExecRestrictedStmt` for test.

func GetDBNames

func GetDBNames(seVar *variable.SessionVars) []string

GetDBNames gets the sql layer database names from the session.

func GetDomain

func GetDomain(store kv.Storage) (*domain.Domain, error)

GetDomain gets the associated domain for store.

func GetOrCreateDomainWithFilter

func GetOrCreateDomainWithFilter(store kv.Storage, filter issyncer.Filter) (*domain.Domain, error)

GetOrCreateDomainWithFilter gets the associated domain for store. If domain not created, create a new one with the given schema filter.

func GetRows4Test

func GetRows4Test(ctx context.Context, _ sessionctx.Context, rs sqlexec.RecordSet) ([]chunk.Row, error)

GetRows4Test gets all the rows from a RecordSet, only used for test.

func GetStartTSFromSession

func GetStartTSFromSession(se any) (startTS, processInfoID uint64)

GetStartTSFromSession returns the startTS in the session `se`

func HandleNonTransactionalDML

func HandleNonTransactionalDML(ctx context.Context, stmt *ast.NonTransactionalDMLStmt, se sessionapi.Session) (sqlexec.RecordSet, error)

HandleNonTransactionalDML is the entry point for a non-transactional DML statement

func InitDDLTables

func InitDDLTables(store kv.Storage) error

InitDDLTables creates system tables that DDL uses. Because CREATE TABLE is also a DDL, we must directly modify KV data to create these tables.

func InitMDLVariable

func InitMDLVariable(store kv.Storage) error

InitMDLVariable initializes the metadata lock variable.

func InitMDLVariableForBootstrap

func InitMDLVariableForBootstrap(store kv.Storage) error

InitMDLVariableForBootstrap initializes the metadata lock variable.

func InitMDLVariableForUpgrade

func InitMDLVariableForUpgrade(store kv.Storage) (bool, error)

InitMDLVariableForUpgrade initializes the metadata lock variable.

func InitTiDBSchemaCacheSize

func InitTiDBSchemaCacheSize(store kv.Storage) error

InitTiDBSchemaCacheSize initializes the tidb schema cache size.

func IsUpgradingClusterState

func IsUpgradingClusterState(s sessionctx.Context) (bool, error)

IsUpgradingClusterState checks whether the global state is upgrading.

func KeyNeedToLock

func KeyNeedToLock(k, v []byte, flags kv.KeyFlags) bool

KeyNeedToLock returns true if the key need to lock.

func MustExec

func MustExec(t *testing.T, se sessionapi.Session, sql string, args ...any)

MustExec executes a sql statement and asserts no error occurs.

func MustExecToRecodeSet

func MustExecToRecodeSet(t *testing.T, se sessionapi.Session, sql string, args ...any) sqlexec.RecordSet

MustExecToRecodeSet executes a sql statement and asserts no error occurs.

func Parse

func Parse(ctx sessionctx.Context, src string) ([]ast.StmtNode, error)

Parse parses a query string to raw ast.StmtNode.

func ParseWithParams4Test

func ParseWithParams4Test(ctx context.Context, s sessionapi.Session,
	sql string, args ...any) (ast.StmtNode, error)

ParseWithParams4Test wrapper (s *session) ParseWithParams for test

func RegisterMockUpgradeFlag

func RegisterMockUpgradeFlag(fSet *flag.FlagSet)

RegisterMockUpgradeFlag registers the mock upgrade flag.

func ResetMockAutoRandIDRetryCount

func ResetMockAutoRandIDRetryCount(failTimes int64)

ResetMockAutoRandIDRetryCount set the number of occurrences of `kv.ErrTxnRetryable` when calling TxnState.Commit().

func ResetStoreForWithTiKVTest

func ResetStoreForWithTiKVTest(store kv.Storage)

ResetStoreForWithTiKVTest is only used in the test code. TODO: Remove domap and storeBootstrapped. Use store.SetOption() to do it.

func ResultSetToStringSlice

func ResultSetToStringSlice(ctx context.Context, s sessionapi.Session, rs sqlexec.RecordSet) ([][]string, error)

ResultSetToStringSlice changes the RecordSet to [][]string.

func RevertVersionAndVariables

func RevertVersionAndVariables(t *testing.T, se sessionapi.Session, ver int)

RevertVersionAndVariables reverts the version and variables in mysql.tidb and mysql.global_variables for testing upgrade/downgrade.

func SyncNormalRunning

func SyncNormalRunning(s sessionctx.Context) error

SyncNormalRunning syncs normal state to etcd.

func SyncUpgradeState

func SyncUpgradeState(s sessionctx.Context, timeout time.Duration) error

SyncUpgradeState syncs upgrade state to etcd.

Types

type BaseCallback

type BaseCallback struct{}

BaseCallback implements Callback interfaces.

func (*BaseCallback) OnBootstrap

func (*BaseCallback) OnBootstrap(sessionapi.Session)

OnBootstrap implements Callback interface.

func (*BaseCallback) OnBootstrapAfter

func (*BaseCallback) OnBootstrapAfter(sessionapi.Session)

OnBootstrapAfter implements Callback interface.

func (*BaseCallback) OnBootstrapBefore

func (*BaseCallback) OnBootstrapBefore(sessionapi.Session)

OnBootstrapBefore implements Callback interface.

type Callback

type Callback interface {
	// OnBootstrapBefore is called before doing bootstrap.
	OnBootstrapBefore(s sessionapi.Session)
	// OnBootstrap is called doing bootstrap.
	OnBootstrap(s sessionapi.Session)
	// OnBootstrapAfter is called after doing bootstrap.
	OnBootstrapAfter(s sessionapi.Session)
}

Callback is used for Test.

type DatabaseBasicInfo

type DatabaseBasicInfo struct {
	ID     int64
	Name   string
	Tables []TableBasicInfo
}

DatabaseBasicInfo contains the basic information of a database.

type ExecStmtVarKeyType

type ExecStmtVarKeyType int

ExecStmtVarKeyType is a dummy type to avoid naming collision in context.

const ExecStmtVarKey ExecStmtVarKeyType = 0

ExecStmtVarKey is a variable key for ExecStmt.

func (ExecStmtVarKeyType) String

func (ExecStmtVarKeyType) String() string

String defines a Stringer function for debugging and pretty printing.

type LazyTxn

type LazyTxn struct {
	// States of a LazyTxn should be one of the followings:
	// Invalid: kv.Transaction == nil && txnFuture == nil
	// Pending: kv.Transaction == nil && txnFuture != nil
	// Valid:	kv.Transaction != nil && txnFuture == nil
	kv.Transaction
	// contains filtered or unexported fields
}

LazyTxn wraps kv.Transaction to provide a new kv.Transaction. 1. It holds all statement related modification in the buffer before flush to the txn, so if execute statement meets error, the txn won't be made dirty. 2. It's a lazy transaction, that means it's a txnFuture before StartTS() is really need.

func (*LazyTxn) CacheTableInfo

func (txn *LazyTxn) CacheTableInfo(id int64, info *model.TableInfo)

CacheTableInfo caches the index name.

func (*LazyTxn) CancelFairLocking

func (txn *LazyTxn) CancelFairLocking(ctx context.Context) error

CancelFairLocking wraps the inner transaction to support using fair locking with lazy initialization.

func (*LazyTxn) Commit

func (txn *LazyTxn) Commit(ctx context.Context) error

Commit overrides the Transaction interface.

func (*LazyTxn) DoneFairLocking

func (txn *LazyTxn) DoneFairLocking(ctx context.Context) error

DoneFairLocking wraps the inner transaction to support using fair locking with lazy initialization.

func (*LazyTxn) GetOption

func (txn *LazyTxn) GetOption(opt int) any

GetOption implements the GetOption

func (*LazyTxn) GetTableInfo

func (txn *LazyTxn) GetTableInfo(id int64) *model.TableInfo

GetTableInfo returns the cached index name.

func (*LazyTxn) GoString

func (txn *LazyTxn) GoString() string

GoString implements the "%#v" format for fmt.Printf.

func (*LazyTxn) IsInFairLockingMode

func (txn *LazyTxn) IsInFairLockingMode() bool

IsInFairLockingMode wraps the inner transaction to support using fair locking with lazy initialization.

func (*LazyTxn) KeysNeedToLock

func (txn *LazyTxn) KeysNeedToLock() ([]kv.Key, error)

KeysNeedToLock returns the keys need to be locked.

func (*LazyTxn) LockKeys

func (txn *LazyTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keys ...kv.Key) error

LockKeys wraps the inner transaction's `LockKeys` to record the status

func (*LazyTxn) LockKeysFunc

func (txn *LazyTxn) LockKeysFunc(ctx context.Context, lockCtx *kv.LockCtx, fn func(), keys ...kv.Key) error

LockKeysFunc Wrap the inner transaction's `LockKeys` to record the status

func (*LazyTxn) Mem

func (txn *LazyTxn) Mem() uint64

Mem implements the MemBuffer interface.

func (*LazyTxn) MemHookSet

func (txn *LazyTxn) MemHookSet() bool

MemHookSet returns whether the memory footprint change hook is set.

func (*LazyTxn) RetryFairLocking

func (txn *LazyTxn) RetryFairLocking(ctx context.Context) error

RetryFairLocking wraps the inner transaction to support using fair locking with lazy initialization.

func (*LazyTxn) Rollback

func (txn *LazyTxn) Rollback() error

Rollback overrides the Transaction interface.

func (*LazyTxn) RollbackMemDBToCheckpoint

func (txn *LazyTxn) RollbackMemDBToCheckpoint(savepoint *tikv.MemDBCheckpoint)

RollbackMemDBToCheckpoint overrides the Transaction interface.

func (*LazyTxn) SetMemoryFootprintChangeHook

func (txn *LazyTxn) SetMemoryFootprintChangeHook(hook func(uint64))

SetMemoryFootprintChangeHook sets the hook to be called when the memory footprint of this transaction changes.

func (*LazyTxn) Size

func (txn *LazyTxn) Size() int

Size implements the MemBuffer interface.

func (*LazyTxn) StartFairLocking

func (txn *LazyTxn) StartFairLocking() error

StartFairLocking wraps the inner transaction to support using fair locking with lazy initialization.

func (*LazyTxn) String

func (txn *LazyTxn) String() string

func (*LazyTxn) Valid

func (txn *LazyTxn) Valid() bool

Valid implements the kv.Transaction interface.

func (*LazyTxn) Wait

func (txn *LazyTxn) Wait(ctx context.Context, sctx sessionctx.Context) (kv.Transaction, error)

Wait converts pending txn to valid

type Opt

type Opt struct {
	PreparedPlanCache sessionctx.SessionPlanCache
}

Opt describes the option for creating session

type StmtHistory

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

StmtHistory holds all histories of statements in a txn.

func GetHistory

func GetHistory(ctx sessionctx.Context) *StmtHistory

GetHistory get all stmtHistory in current txn. Exported only for test. If stmtHistory is nil, will create a new one for current txn.

func (*StmtHistory) Add

func (h *StmtHistory) Add(st sqlexec.Statement, stmtCtx *stmtctx.StatementContext)

Add appends a stmt to history list.

func (*StmtHistory) Count

func (h *StmtHistory) Count() int

Count returns the count of the history.

type TableBasicInfo

type TableBasicInfo struct {
	ID   int64
	Name string
	SQL  string
}

TableBasicInfo contains the basic information of a table used in DDL.

type TestCallback

type TestCallback struct {
	*BaseCallback

	Cnt                       *atomicutil.Int32
	OnBootstrapBeforeExported func(s sessionapi.Session)
	OnBootstrapExported       func(s sessionapi.Session)
	OnBootstrapAfterExported  func(s sessionapi.Session)
}

TestCallback is used to customize user callback themselves.

func (*TestCallback) OnBootstrap

func (tc *TestCallback) OnBootstrap(s sessionapi.Session)

OnBootstrap mocks the same behavior with the main bootstrap hook.

func (*TestCallback) OnBootstrapAfter

func (tc *TestCallback) OnBootstrapAfter(s sessionapi.Session)

OnBootstrapAfter mocks the same behavior with the main bootstrap hook.

func (*TestCallback) OnBootstrapBefore

func (tc *TestCallback) OnBootstrapBefore(s sessionapi.Session)

OnBootstrapBefore mocks the same behavior with the main bootstrap hook.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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