stmtstats

package
v0.0.0-...-503c688 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAggregator

func CloseAggregator()

CloseAggregator is used to stop the background aggregator goroutine of the stmtstats module. SetupAggregator is **not** thread-safe.

func RegisterCollector

func RegisterCollector(collector Collector)

RegisterCollector binds a Collector to globalAggregator. RegisterCollector is thread-safe.

func SetupAggregator

func SetupAggregator()

SetupAggregator is used to initialize the background aggregator goroutine of the stmtstats module. SetupAggregator is **not** thread-safe.

func UnregisterCollector

func UnregisterCollector(collector Collector)

UnregisterCollector removes Collector from globalAggregator. UnregisterCollector is thread-safe.

Types

type BinaryDigest

type BinaryDigest string

BinaryDigest is converted from parser.Digest.Bytes(), and the purpose is to be used as the key of the map.

type Collector

type Collector interface {
	// CollectStmtStatsMap is used to collect StatementStatsMap.
	CollectStmtStatsMap(StatementStatsMap)
}

Collector is used to collect StatementStatsMap.

type KvExecCounter

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

KvExecCounter is used to count the number of SQL executions of the kv layer. It internally calls addKvExecCount of StatementStats at the right time, to ensure the semantic of "SQL execution count of TiKV".

func (*KvExecCounter) RPCInterceptor

func (c *KvExecCounter) RPCInterceptor() interceptor.RPCInterceptor

RPCInterceptor returns an interceptor.RPCInterceptor for client-go. The returned interceptor is generally expected to be bind to transaction or snapshot. In this way, the logic preset by KvExecCounter will be executed before each RPC request is initiated, in order to count the number of SQL executions of the TiKV dimension.

type KvStatementStatsItem

type KvStatementStatsItem struct {
	// KvExecCount represents the number of SQL executions of TiKV.
	KvExecCount map[string]uint64
}

KvStatementStatsItem is part of StatementStatsItem, it only contains indicators of kv layer.

func NewKvStatementStatsItem

func NewKvStatementStatsItem() KvStatementStatsItem

NewKvStatementStatsItem creates an empty KvStatementStatsItem.

func (*KvStatementStatsItem) Merge

Merge merges other into KvStatementStatsItem.

After executing Merge, some pointers in other may be referenced by i. So after calling Merge, it is best not to continue to use other unless you understand what you are doing.

If you add additional indicators, you need to add their merge code here.

type SQLPlanDigest

type SQLPlanDigest struct {
	SQLDigest  BinaryDigest
	PlanDigest BinaryDigest
}

SQLPlanDigest is used as the key of StatementStatsMap to distinguish different sql.

type StatementObserver

type StatementObserver interface {
	// OnExecutionBegin should be called before statement execution.
	OnExecutionBegin(sqlDigest, planDigest []byte)

	// OnExecutionFinished should be called after the statement is executed.
	// WARNING: Currently Only call StatementObserver API when TopSQL is enabled,
	// there is no guarantee that both OnExecutionBegin and OnExecutionFinished will be called for a SQL,
	// such as TopSQL is enabled during a SQL execution.
	OnExecutionFinished(sqlDigest, planDigest []byte, execDuration time.Duration)
}

StatementObserver is an abstract interface as a callback to the corresponding position of TiDB's SQL statement execution process. StatementStats implements StatementObserver and performs counting such as SQLExecCount/SQLDuration internally. The caller only needs to be responsible for calling different methods at the corresponding locations, without paying attention to implementation details.

type StatementStats

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

StatementStats is a counter used locally in each session. We can use StatementStats to count data such as "the number of SQL executions", and it is expected that these statistics will eventually be collected and merged in the background.

func CreateStatementStats

func CreateStatementStats() *StatementStats

CreateStatementStats try to create and register an StatementStats.

func (*StatementStats) CreateKvExecCounter

func (s *StatementStats) CreateKvExecCounter(sqlDigest, planDigest []byte) *KvExecCounter

CreateKvExecCounter creates an associated KvExecCounter from StatementStats. The created KvExecCounter can only be used during a single statement execution and cannot be reused.

func (*StatementStats) Finished

func (s *StatementStats) Finished() bool

Finished returns whether the StatementStats has been finished.

func (*StatementStats) GetOrCreateStatementStatsItem

func (s *StatementStats) GetOrCreateStatementStatsItem(sqlDigest, planDigest []byte) *StatementStatsItem

GetOrCreateStatementStatsItem creates the corresponding StatementStatsItem for the specified SQLPlanDigest and timestamp if it does not exist before. GetOrCreateStatementStatsItem is just a helper function, not responsible for concurrency control, so GetOrCreateStatementStatsItem is **not** thread-safe.

func (*StatementStats) OnExecutionBegin

func (s *StatementStats) OnExecutionBegin(sqlDigest, planDigest []byte)

OnExecutionBegin implements StatementObserver.OnExecutionBegin.

func (*StatementStats) OnExecutionFinished

func (s *StatementStats) OnExecutionFinished(sqlDigest, planDigest []byte, execDuration time.Duration)

OnExecutionFinished implements StatementObserver.OnExecutionFinished.

func (*StatementStats) SetFinished

func (s *StatementStats) SetFinished()

SetFinished marks this StatementStats as "finished" and no more counting or aggregation should happen. Associated resources will be cleaned up, like background aggregators. Generally, as the StatementStats is created when a session starts, SetFinished should be called when the session ends.

func (*StatementStats) Take

Take takes out all existing StatementStatsMap data from StatementStats. Take is thread-safe.

type StatementStatsItem

type StatementStatsItem struct {
	// KvStatsItem contains all indicators of kv layer.
	KvStatsItem KvStatementStatsItem
	// ExecCount represents the number of SQL executions of TiDB.
	ExecCount uint64
	// SumDurationNs is the total number of durations in nanoseconds.
	SumDurationNs uint64
	// DurationCount represents the number of SQL executions specially
	// used to calculate SQLDuration.
	DurationCount uint64
}

StatementStatsItem represents a set of mergeable statistics. StatementStatsItem is used in a larger data structure to represent the stats of a certain SQLPlanDigest under a certain timestamp. If there are more indicators that need to be added in the future, please add it in StatementStatsItem and implement its aggregation in the Merge method.

func NewStatementStatsItem

func NewStatementStatsItem() *StatementStatsItem

NewStatementStatsItem creates an empty StatementStatsItem.

func (*StatementStatsItem) Merge

func (i *StatementStatsItem) Merge(other *StatementStatsItem)

Merge merges other into StatementStatsItem.

After executing Merge, some pointers in other may be referenced by i. So after calling Merge, it is best not to continue to use other unless you understand what you are doing.

If you add additional indicators, you need to add their merge code here.

type StatementStatsMap

type StatementStatsMap map[SQLPlanDigest]*StatementStatsItem

StatementStatsMap is the local data type of StatementStats.

func (StatementStatsMap) Merge

func (m StatementStatsMap) Merge(other StatementStatsMap)

Merge merges other into StatementStatsMap. Values with the same SQLPlanDigest will be merged.

After executing Merge, some pointers in other may be referenced by m. So after calling Merge, it is best not to continue to use other unless you understand what you are doing.

Jump to

Keyboard shortcuts

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