context

package
v1.1.0-beta.0...-8e1a47d Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// WarnLevelError represents level "Error" for 'SHOW WARNINGS' syntax.
	WarnLevelError = "Error"
	// WarnLevelWarning represents level "Warning" for 'SHOW WARNINGS' syntax.
	WarnLevelWarning = "Warning"
	// WarnLevelNote represents level "Note" for 'SHOW WARNINGS' syntax.
	WarnLevelNote = "Note"
)

Variables

This section is empty.

Functions

func GenContextID

func GenContextID() uint64

GenContextID generates a unique context ID.

Types

type PlanCacheTracker

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

PlanCacheTracker PlanCacheTracker `PlanCacheTracker`. `PlanCacheTracker` is thread-safe.

func NewPlanCacheTracker

func NewPlanCacheTracker(warnHandler WarnAppender) PlanCacheTracker

NewPlanCacheTracker creates a new PlanCacheTracker.

func (*PlanCacheTracker) EnablePlanCache

func (h *PlanCacheTracker) EnablePlanCache()

EnablePlanCache sets to use plan cache.

func (*PlanCacheTracker) PlanCacheUnqualified

func (h *PlanCacheTracker) PlanCacheUnqualified() string

PlanCacheUnqualified returns the reason of why the plan cache is unqualified

func (*PlanCacheTracker) SetAlwaysWarnSkipCache

func (h *PlanCacheTracker) SetAlwaysWarnSkipCache(alwaysWarnSkipCache bool)

SetAlwaysWarnSkipCache sets whether to always warn when skip plan cache. By default, for `SessionNonPrepared`, we don't warn when skip plan cache. But in some cases, we want to warn even for `SessionNonPrepared`.

func (*PlanCacheTracker) SetCacheType

func (h *PlanCacheTracker) SetCacheType(cacheType PlanCacheType)

SetCacheType sets the cache type.

func (*PlanCacheTracker) SetForcePlanCache

func (h *PlanCacheTracker) SetForcePlanCache(forcePlanCache bool)

SetForcePlanCache sets whether to force the optimizer to use plan cache even if there is risky optimization.

func (*PlanCacheTracker) SetSkipPlanCache

func (h *PlanCacheTracker) SetSkipPlanCache(reason string)

SetSkipPlanCache sets to skip plan cache.

func (*PlanCacheTracker) UseCache

func (h *PlanCacheTracker) UseCache() bool

UseCache returns whether to use plan cache.

func (*PlanCacheTracker) WarnSkipPlanCache

func (h *PlanCacheTracker) WarnSkipPlanCache(reason string)

WarnSkipPlanCache output the reason why this query can't hit the plan cache.

type PlanCacheType

type PlanCacheType int

PlanCacheType is the flag of plan cache

const (
	// DefaultNoCache no cache
	DefaultNoCache PlanCacheType = iota
	// SessionPrepared session prepared plan cache
	SessionPrepared
	// SessionNonPrepared session non-prepared plan cache
	SessionNonPrepared
)

type RangeFallbackHandler

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

RangeFallbackHandler is used to handle range fallback. If there are too many ranges, it'll fallback and add a warning. RangeFallbackHandler is thread-safe.

func NewRangeFallbackHandler

func NewRangeFallbackHandler(planCacheTracker *PlanCacheTracker, warnHandler WarnAppender) RangeFallbackHandler

NewRangeFallbackHandler creates a new RangeFallbackHandler.

func (*RangeFallbackHandler) RecordRangeFallback

func (h *RangeFallbackHandler) RecordRangeFallback(rangeMaxSize int64)

RecordRangeFallback records the range fallback event.

type SQLWarn

type SQLWarn struct {
	Level string
	Err   error
}

SQLWarn relates a sql warning and it's level.

func (*SQLWarn) MarshalJSON

func (warn *SQLWarn) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler.MarshalJSON interface.

func (*SQLWarn) UnmarshalJSON

func (warn *SQLWarn) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the Unmarshaler.UnmarshalJSON interface.

type StaticWarnHandler

type StaticWarnHandler struct {
	sync.Mutex
	// contains filtered or unexported fields
}

StaticWarnHandler implements the WarnHandler interface.

func NewStaticWarnHandler

func NewStaticWarnHandler(sliceCap int) *StaticWarnHandler

NewStaticWarnHandler creates a new StaticWarnHandler.

func NewStaticWarnHandlerWithHandler

func NewStaticWarnHandlerWithHandler(h WarnHandler) *StaticWarnHandler

NewStaticWarnHandlerWithHandler creates a new StaticWarnHandler with copying the warnings from the given WarnHandler.

func (*StaticWarnHandler) AppendError

func (h *StaticWarnHandler) AppendError(warn error)

AppendError appends a warning with level 'Error'.

func (*StaticWarnHandler) AppendNote

func (h *StaticWarnHandler) AppendNote(warn error)

AppendNote appends a warning with level 'Note'.

func (*StaticWarnHandler) AppendWarning

func (h *StaticWarnHandler) AppendWarning(warn error)

AppendWarning implements the StaticWarnHandler.AppendWarning.

func (*StaticWarnHandler) AppendWarnings

func (h *StaticWarnHandler) AppendWarnings(warns []SQLWarn)

AppendWarnings appends multiple warnings

func (*StaticWarnHandler) CopyWarnings

func (h *StaticWarnHandler) CopyWarnings(dst []SQLWarn) []SQLWarn

CopyWarnings implements the StaticWarnHandler.CopyWarnings.

func (*StaticWarnHandler) GetWarnings

func (h *StaticWarnHandler) GetWarnings() []SQLWarn

GetWarnings returns all warnings in the handler. It's not safe to modify the returned slice.

func (*StaticWarnHandler) NumErrorWarnings

func (h *StaticWarnHandler) NumErrorWarnings() (uint16, int)

NumErrorWarnings returns the number of warnings with level 'Error' and the total number of warnings.

func (*StaticWarnHandler) Reset

func (h *StaticWarnHandler) Reset()

Reset resets the warnings of this handler.

func (*StaticWarnHandler) SetWarnings

func (h *StaticWarnHandler) SetWarnings(warns []SQLWarn)

SetWarnings sets the internal warnings directly.

func (*StaticWarnHandler) TruncateWarnings

func (h *StaticWarnHandler) TruncateWarnings(start int) []SQLWarn

TruncateWarnings implements the StaticWarnHandler.TruncateWarnings.

func (*StaticWarnHandler) WarningCount

func (h *StaticWarnHandler) WarningCount() int

WarningCount implements the StaticWarnHandler.WarningCount.

type ValueStoreContext

type ValueStoreContext interface {
	// SetValue saves a value associated with this context for key.
	SetValue(key fmt.Stringer, value any)

	// Value returns the value associated with this context for key.
	Value(key fmt.Stringer) any

	// ClearValue clears the value associated with this context for key.
	ClearValue(key fmt.Stringer)
}

ValueStoreContext is a context that can store values.

type WarnAppender

type WarnAppender interface {
	// AppendWarning appends a warning
	AppendWarning(err error)
	// AppendNote appends a warning with level 'Note'.
	AppendNote(msg error)
}

WarnAppender provides a function to add a warning. Using interface rather than a simple function/closure can avoid memory allocation in some cases. See https://github.com/pingcap/tidb/issues/49277

func NewFuncWarnAppenderForTest

func NewFuncWarnAppenderForTest(fn func(level string, err error)) WarnAppender

NewFuncWarnAppenderForTest creates a `WarnHandler` which will use the function to handle warn To have a better performance, it's not suggested to use this function in production.

type WarnHandler

type WarnHandler interface {
	WarnAppender
	// WarningCount gets warning count.
	WarningCount() int
	// TruncateWarnings truncates warnings begin from start and returns the truncated warnings.
	//
	// Deprecated: This method is deprecated. Because it's unsafe to read the warnings returned by `GetWarnings`
	// after truncate and append the warnings.
	//
	// Currently it's used in two cases and they all have better alternatives:
	// 1. Read warnings count, do some operation and truncate warnings to read new warnings. In this case, we
	//   can use a new temporary WarnHandler to do the operation and get the warnings without touching the
	//   global `WarnHandler` in the statement context.
	// 2. Read warnings count, do some operation and truncate warnings to see whether new warnings are appended.
	//   In this case, we can use a specially designed `WarnHandler` which doesn't actually record warnings, but
	//   just counts whether new warnings are appended.
	// It's understandable to use `TruncateWarnings` as it's not always easy to assign a new `WarnHandler` to the
	// context now.
	// TODO: Make it easier to assign a new `WarnHandler` to the context (of `table` and other packages) and remove
	// this method.
	TruncateWarnings(start int) []SQLWarn
	// CopyWarnings copies warnings to another slice.
	// The input argument provides target warnings that copy to.
	// If the dist capacity is not enough, it will allocate a new slice.
	CopyWarnings(dst []SQLWarn) []SQLWarn
}

WarnHandler provides a handler to append and get warnings.

var IgnoreWarn WarnHandler = &ignoreWarn{}

IgnoreWarn is WarnHandler which does nothing

type WarnHandlerExt

type WarnHandlerExt interface {
	WarnHandler

	// AppendWarnings appends multiple warnings
	AppendWarnings(warns []SQLWarn)
	// AppendNote appends a warning with level 'Note'.
	AppendNote(warn error)
	// AppendError appends a warning with level 'Error'.
	AppendError(warn error)

	// GetWarnings gets all warnings. The slice is not copied, so it should not be modified.
	GetWarnings() []SQLWarn
	// SetWarnings resets all warnings in the handler directly. The handler may ignore the given warnings.
	SetWarnings(warns []SQLWarn)
	// NumErrorWarnings returns the number of warnings with level 'Error' and the total number of warnings.
	NumErrorWarnings() (uint16, int)
}

WarnHandlerExt includes more methods for WarnHandler. It allows more detailed control over warnings. TODO: it's a standalone interface, because it's not necessary for all WarnHandler to implement these methods. However, it's still needed for many executors, so we'll see whether it's good to merge it with `WarnAppender` and `WarnHandler` in the future.

Jump to

Keyboard shortcuts

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