sandboxconn

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 15 Imported by: 2

Documentation

Overview

Package sandboxconn provides a fake QueryService implementation for tests. It can return real results, and simulate error cases.

Index

Constants

This section is empty.

Variables

View Source
var SandboxSQRowCount = int64(10)

SandboxSQRowCount is the default number of fake splits returned.

View Source
var SingleRowResult = &sqltypes.Result{
	Fields: []*querypb.Field{
		{Name: "id", Type: sqltypes.Int32, Charset: collations.CollationBinaryID, Flags: uint32(querypb.MySqlFlag_NUM_FLAG)},
		{Name: "value", Type: sqltypes.VarChar, Charset: collations.CollationUtf8mb4ID},
	},
	InsertID: 0,
	Rows: [][]sqltypes.Value{{
		sqltypes.NewInt32(1),
		sqltypes.NewVarChar("foo"),
	}},
	StatusFlags: sqltypes.ServerStatusAutocommit,
}

SingleRowResult is returned when there is no pre-stored result.

View Source
var StreamRowResult = &sqltypes.Result{
	Fields: []*querypb.Field{
		{Name: "id", Type: sqltypes.Int32, Charset: collations.CollationBinaryID, Flags: uint32(querypb.MySqlFlag_NUM_FLAG)},
		{Name: "value", Type: sqltypes.VarChar, Charset: collations.CollationUtf8mb4ID},
	},
	Rows: [][]sqltypes.Value{{
		sqltypes.NewInt32(1),
		sqltypes.NewVarChar("foo"),
	}},
}

StreamRowResult is SingleRowResult with RowsAffected set to 0.

Functions

This section is empty.

Types

type SandboxConn

type SandboxConn struct {

	// These errors work for all functions.
	MustFailCodes map[vtrpcpb.Code]int

	// ServingKeyspaces is a list of serving keyspaces
	ServingKeyspaces []string

	// These errors are triggered only for specific functions.
	// For now these are just for the 2PC functions.
	MustFailPrepare             int
	MustFailCommitPrepared      int
	MustFailRollbackPrepared    int
	MustFailCreateTransaction   int
	MustFailStartCommit         int
	MustFailSetRollback         int
	MustFailConcludeTransaction int
	// MustFailExecute is keyed by the statement type and stores the number
	// of times to fail when it sees that statement type.
	// Once, exhausted it will start returning non-error response.
	MustFailExecute map[sqlparser.StatementType]int

	// These Count vars report how often the corresponding
	// functions were called.
	ExecCount                   atomic.Int64
	BeginCount                  atomic.Int64
	CommitCount                 atomic.Int64
	RollbackCount               atomic.Int64
	AsTransactionCount          atomic.Int64
	PrepareCount                atomic.Int64
	CommitPreparedCount         atomic.Int64
	RollbackPreparedCount       atomic.Int64
	CreateTransactionCount      atomic.Int64
	StartCommitCount            atomic.Int64
	SetRollbackCount            atomic.Int64
	ConcludeTransactionCount    atomic.Int64
	ReadTransactionCount        atomic.Int64
	UnresolvedTransactionsCount atomic.Int64
	ReserveCount                atomic.Int64
	ReleaseCount                atomic.Int64
	GetSchemaCount              atomic.Int64

	// Queries stores the non-batch requests received.
	Queries []*querypb.BoundQuery

	// BatchQueries stores the batch requests received
	// Each batch request is inlined as a slice of Queries.
	BatchQueries [][]*querypb.BoundQuery

	// Options stores the options received by all calls.
	Options []*querypb.ExecuteOptions

	// ReadTransactionResults is used for returning results for ReadTransaction.
	ReadTransactionResults []*querypb.TransactionMetadata

	// UnresolvedTransactionsResult is used for returning results for UnresolvedTransactions.
	UnresolvedTransactionsResult []*querypb.TransactionMetadata

	MessageIDs []*querypb.Value

	// vstream expectations.
	StartPos      string
	VStreamEvents [][]*binlogdatapb.VEvent
	VStreamErrors []error
	VStreamCh     chan *binlogdatapb.VEvent

	// transaction id generator
	TransactionID atomic.Int64

	// reserve id generator
	ReserveID atomic.Int64

	// this error will only happen once
	EphemeralShardErr error

	NotServing bool
	// contains filtered or unexported fields
}

SandboxConn satisfies the QueryService interface

func NewSandboxConn

func NewSandboxConn(t *topodatapb.Tablet) *SandboxConn

NewSandboxConn returns a new SandboxConn targeted to the provided tablet.

func (*SandboxConn) AddVStreamEvents

func (sbc *SandboxConn) AddVStreamEvents(events []*binlogdatapb.VEvent, err error)

AddVStreamEvents adds a set of VStream events to be returned.

func (*SandboxConn) Begin

Begin is part of the QueryService interface.

func (*SandboxConn) BeginExecute

func (sbc *SandboxConn) BeginExecute(ctx context.Context, target *querypb.Target, preQueries []string, query string, bindVars map[string]*querypb.BindVariable, reservedID int64, options *querypb.ExecuteOptions) (queryservice.TransactionState, *sqltypes.Result, error)

BeginExecute is part of the QueryService interface.

func (*SandboxConn) BeginStreamExecute added in v0.13.0

func (sbc *SandboxConn) BeginStreamExecute(ctx context.Context, target *querypb.Target, preQueries []string, sql string, bindVariables map[string]*querypb.BindVariable, reservedID int64, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) (queryservice.TransactionState, error)

BeginStreamExecute is part of the QueryService interface.

func (*SandboxConn) ChangeTabletType added in v0.10.2

func (sbc *SandboxConn) ChangeTabletType(typ topodatapb.TabletType)

ChangeTabletType changes the tablet type.

func (*SandboxConn) ClearQueries added in v0.19.0

func (sbc *SandboxConn) ClearQueries()

ClearQueries clears the Queries in sandboxconn.

func (*SandboxConn) Close

func (sbc *SandboxConn) Close(ctx context.Context) error

Close does not change ExecCount

func (*SandboxConn) Commit

func (sbc *SandboxConn) Commit(ctx context.Context, target *querypb.Target, transactionID int64) (int64, error)

Commit is part of the QueryService interface.

func (*SandboxConn) CommitPrepared

func (sbc *SandboxConn) CommitPrepared(ctx context.Context, target *querypb.Target, dtid string) (err error)

CommitPrepared commits the prepared transaction.

func (*SandboxConn) ConcludeTransaction

func (sbc *SandboxConn) ConcludeTransaction(ctx context.Context, target *querypb.Target, dtid string) (err error)

ConcludeTransaction deletes the 2pc transaction metadata essentially resolving it.

func (*SandboxConn) CreateTransaction

func (sbc *SandboxConn) CreateTransaction(ctx context.Context, target *querypb.Target, dtid string, participants []*querypb.Target) (err error)

CreateTransaction creates the metadata for a 2PC transaction.

func (*SandboxConn) Execute

func (sbc *SandboxConn) Execute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]*querypb.BindVariable, transactionID, reservedID int64, options *querypb.ExecuteOptions) (*sqltypes.Result, error)

Execute is part of the QueryService interface.

func (*SandboxConn) ExpectVStreamStartPos

func (sbc *SandboxConn) ExpectVStreamStartPos(startPos string)

ExpectVStreamStartPos makes the conn verify that that the next vstream request has the right startPos.

func (*SandboxConn) GetQueries added in v0.19.0

func (sbc *SandboxConn) GetQueries() []*querypb.BoundQuery

GetQueries gets the Queries from sandboxconn.

func (*SandboxConn) GetSchema added in v0.16.0

func (sbc *SandboxConn) GetSchema(ctx context.Context, target *querypb.Target, tableType querypb.SchemaTableType, tableNames []string, callback func(schemaRes *querypb.GetSchemaResponse) error) error

GetSchema implements the QueryService interface

func (*SandboxConn) GetServingKeyspaces added in v0.18.0

func (sbc *SandboxConn) GetServingKeyspaces() []string

GetServingKeyspaces returns list of serving keyspaces.

func (*SandboxConn) HandlePanic

func (sbc *SandboxConn) HandlePanic(err *error)

HandlePanic is part of the QueryService interface.

func (*SandboxConn) MessageAck

func (sbc *SandboxConn) MessageAck(ctx context.Context, target *querypb.Target, name string, ids []*querypb.Value) (count int64, err error)

MessageAck is part of the QueryService interface.

func (*SandboxConn) MessageStream

func (sbc *SandboxConn) MessageStream(ctx context.Context, target *querypb.Target, name string, callback func(*sqltypes.Result) error) (err error)

MessageStream is part of the QueryService interface.

func (*SandboxConn) Prepare

func (sbc *SandboxConn) Prepare(ctx context.Context, target *querypb.Target, transactionID int64, dtid string) (err error)

Prepare prepares the specified transaction.

func (*SandboxConn) QueryServiceByAlias

QueryServiceByAlias is part of the Gateway interface.

func (*SandboxConn) ReadTransaction

func (sbc *SandboxConn) ReadTransaction(ctx context.Context, target *querypb.Target, dtid string) (metadata *querypb.TransactionMetadata, err error)

ReadTransaction returns the metadata for the specified dtid.

func (*SandboxConn) Release

func (sbc *SandboxConn) Release(ctx context.Context, target *querypb.Target, transactionID, reservedID int64) error

Release implements the QueryService interface

func (*SandboxConn) RequireQueriesLocking added in v0.19.0

func (sbc *SandboxConn) RequireQueriesLocking()

RequireQueriesLocking sets the sandboxconn to require locking the access of Queries field.

func (*SandboxConn) ReserveBeginExecute

func (sbc *SandboxConn) ReserveBeginExecute(ctx context.Context, target *querypb.Target, preQueries []string, postBeginQueries []string, sql string, bindVariables map[string]*querypb.BindVariable, options *querypb.ExecuteOptions) (queryservice.ReservedTransactionState, *sqltypes.Result, error)

ReserveBeginExecute implements the QueryService interface

func (*SandboxConn) ReserveBeginStreamExecute added in v0.13.0

func (sbc *SandboxConn) ReserveBeginStreamExecute(ctx context.Context, target *querypb.Target, preQueries []string, postBeginQueries []string, sql string, bindVariables map[string]*querypb.BindVariable, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) (queryservice.ReservedTransactionState, error)

ReserveBeginStreamExecute is part of the QueryService interface.

func (*SandboxConn) ReserveExecute

func (sbc *SandboxConn) ReserveExecute(ctx context.Context, target *querypb.Target, preQueries []string, sql string, bindVariables map[string]*querypb.BindVariable, transactionID int64, options *querypb.ExecuteOptions) (queryservice.ReservedState, *sqltypes.Result, error)

ReserveExecute implements the QueryService interface

func (*SandboxConn) ReserveStreamExecute added in v0.13.0

func (sbc *SandboxConn) ReserveStreamExecute(ctx context.Context, target *querypb.Target, preQueries []string, sql string, bindVariables map[string]*querypb.BindVariable, transactionID int64, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) (queryservice.ReservedState, error)

ReserveStreamExecute is part of the QueryService interface.

func (*SandboxConn) Rollback

func (sbc *SandboxConn) Rollback(ctx context.Context, target *querypb.Target, transactionID int64) (int64, error)

Rollback is part of the QueryService interface.

func (*SandboxConn) RollbackPrepared

func (sbc *SandboxConn) RollbackPrepared(ctx context.Context, target *querypb.Target, dtid string, originalID int64) (err error)

RollbackPrepared rolls back the prepared transaction.

func (*SandboxConn) SetPanic added in v0.20.0

func (sbc *SandboxConn) SetPanic(i interface{})

func (*SandboxConn) SetResults

func (sbc *SandboxConn) SetResults(r []*sqltypes.Result)

SetResults sets what this con should return next time.

func (*SandboxConn) SetRollback

func (sbc *SandboxConn) SetRollback(ctx context.Context, target *querypb.Target, dtid string, transactionID int64) (err error)

SetRollback transitions the 2pc transaction to the Rollback state. If a transaction id is provided, that transaction is also rolled back.

func (*SandboxConn) SetSchemaResult added in v0.16.0

func (sbc *SandboxConn) SetSchemaResult(r []SchemaResult)

SetSchemaResult sets what GetSchema should return on each call.

func (*SandboxConn) SetStreamHealthResponse added in v0.17.7

func (sbc *SandboxConn) SetStreamHealthResponse(res *querypb.StreamHealthResponse)

SetStreamHealthResponse sets the StreamHealthResponse to be returned in StreamHealth.

func (*SandboxConn) StartCommit

func (sbc *SandboxConn) StartCommit(ctx context.Context, target *querypb.Target, transactionID int64, dtid string) (err error)

StartCommit atomically commits the transaction along with the decision to commit the associated 2pc transaction.

func (*SandboxConn) StreamExecute

func (sbc *SandboxConn) StreamExecute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]*querypb.BindVariable, transactionID int64, reservedID int64, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) error

StreamExecute is part of the QueryService interface.

func (*SandboxConn) StreamHealth

func (sbc *SandboxConn) StreamHealth(ctx context.Context, callback func(*querypb.StreamHealthResponse) error) error

StreamHealth always mocks a "healthy" result by default. If you want to override this behavior you can call SetStreamHealthResponse.

func (*SandboxConn) StringQueries

func (sbc *SandboxConn) StringQueries() []string

StringQueries returns the queries executed as a slice of strings

func (*SandboxConn) Tablet

func (sbc *SandboxConn) Tablet() *topodatapb.Tablet

Tablet is part of the QueryService interface.

func (*SandboxConn) UnresolvedTransactions added in v0.21.0

func (sbc *SandboxConn) UnresolvedTransactions(context.Context, *querypb.Target, int64) ([]*querypb.TransactionMetadata, error)

UnresolvedTransactions is part of the QueryService interface.

func (*SandboxConn) VStream

func (sbc *SandboxConn) VStream(ctx context.Context, request *binlogdatapb.VStreamRequest, send func([]*binlogdatapb.VEvent) error) error

VStream is part of the QueryService interface.

func (*SandboxConn) VStreamResults

func (sbc *SandboxConn) VStreamResults(ctx context.Context, target *querypb.Target, query string, send func(*binlogdatapb.VStreamResultsResponse) error) error

VStreamResults is part of the QueryService interface.

func (*SandboxConn) VStreamRows

VStreamRows is part of the QueryService interface.

func (*SandboxConn) VStreamTables added in v0.18.0

func (sbc *SandboxConn) VStreamTables(ctx context.Context, request *binlogdatapb.VStreamTablesRequest, send func(response *binlogdatapb.VStreamTablesResponse) error) error

VStreamTables is part of the QueryService interface.

type SchemaResult added in v0.20.0

type SchemaResult struct {
	TablesAndViews map[string]string
	UDFs           []*querypb.UDFInfo
}

Jump to

Keyboard shortcuts

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