Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions go/test/endtoend/preparestmt/stmt_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ func TestSpecializedPlan(t *testing.T) {
dbo := Connect(t, "interpolateParams=false")
defer dbo.Close()

oMap := getVarValue[map[string]any](t, "OptimizedQueryExecutions", clusterInstance.VtgateProcess.GetVars)
initExecCount := getVarValue[float64](t, "Passthrough", func() map[string]any {
return oMap
})

queries := []struct {
query string
args []any
Expand All @@ -475,6 +480,11 @@ func TestSpecializedPlan(t *testing.T) {
}
require.NoError(t, stmt.Close())
}
oMap = getVarValue[map[string]any](t, "OptimizedQueryExecutions", clusterInstance.VtgateProcess.GetVars)
finalExecCount := getVarValue[float64](t, "Passthrough", func() map[string]any {
return oMap
})
require.EqualValues(t, 15, finalExecCount-initExecCount)

// Validate specialized plan.
p := getPlanWhenReady(t, queries[0].query, 100*time.Millisecond, clusterInstance.VtgateProcess.ReadQueryPlans)
Expand Down Expand Up @@ -519,3 +529,20 @@ func getPlanWhenReady(t *testing.T, sql string, timeout time.Duration, plansFunc
}
}
}

func getVarValue[T any](t *testing.T, key string, varFunc func() map[string]any) T {
t.Helper()

vars := varFunc()
require.NotNil(t, vars)

value, exists := vars[key]
if !exists {
return *new(T)
}
castValue, ok := value.(T)
if !ok {
t.Errorf("unexpected type, want: %T, got %T", new(T), value)
}
return castValue
}
2 changes: 1 addition & 1 deletion go/test/vschemawrapper/vschema_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewVschemaWrapper(
Collation: env.CollationEnv().DefaultConnectionCharset(),
DefaultTabletType: topodatapb.TabletType_PRIMARY,
SetVarEnabled: true,
})
}, nil)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions go/vt/vtexplain/vtexplain_vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
"vitess.io/vitess/go/vt/discovery"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/log"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vterrors"
Expand All @@ -42,11 +46,6 @@ import (
"vitess.io/vitess/go/vt/vtgate/logstats"
"vitess.io/vitess/go/vt/vtgate/vindexes"
"vitess.io/vitess/go/vt/vttablet/queryservice"

querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
)

func (vte *VTExplain) initVtgateExecutor(ctx context.Context, ts *topo.Server, vSchemaStr, ksShardMapStr string, opts *Options, srvTopoCounts *stats.CountersWithSingleLabel) error {
Expand Down Expand Up @@ -75,6 +74,7 @@ func (vte *VTExplain) initVtgateExecutor(ctx context.Context, ts *topo.Server, v
queryLogBufferSize := 10
plans := theine.NewStore[vtgate.PlanCacheKey, *engine.Plan](4*1024*1024, false)
eConfig := vtgate.ExecutorConfig{
Name: "TestExecutor",
Normalize: opts.Normalize,
StreamSize: streamSize,
AllowScatter: true,
Expand Down
10 changes: 10 additions & 0 deletions go/vt/vtgate/engine/fake_vcursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type noopVCursor struct {
inTx bool
}

func (t *noopVCursor) GetExecutionMetrics() *Metrics {
panic("implement me")
}

func (t *noopVCursor) SetExecQueryTimeout(timeout *int) {
panic("implement me")
}
Expand Down Expand Up @@ -467,6 +471,12 @@ type loggingVCursor struct {
onExecuteMultiShardFn func(context.Context, Primitive, []*srvtopo.ResolvedShard, []*querypb.BoundQuery, bool, bool)
onStreamExecuteMultiFn func(context.Context, Primitive, string, []*srvtopo.ResolvedShard, []map[string]*querypb.BindVariable, bool, bool, func(*sqltypes.Result) error)
onRecordMirrorStatsFn func(time.Duration, time.Duration, error)

metrics *Metrics
}

func (f *loggingVCursor) GetExecutionMetrics() *Metrics {
return f.metrics
}

func (f *loggingVCursor) HasCreatedTempTable() {
Expand Down
32 changes: 32 additions & 0 deletions go/vt/vtgate/engine/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2025 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package engine

import (
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/servenv"
)

type Metrics struct {
optimizedQueryExec *stats.CountersWithSingleLabel
}

func InitMetrics(exporter *servenv.Exporter) *Metrics {
return &Metrics{
optimizedQueryExec: exporter.NewCountersWithSingleLabel("OptimizedQueryExecutions", "Counts optimized queries executed at VTGate by plan type.", "Plan"),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"

"vitess.io/vitess/go/slice"

"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
)
Expand Down Expand Up @@ -83,6 +82,7 @@ func (s *PlanSwitcher) TryExecute(
wantfields bool,
) (*sqltypes.Result, error) {
if s.metCondition(bindVars) {
s.addOptimizedExecStats(vcursor)
return s.Optimized.TryExecute(ctx, vcursor, bindVars, wantfields)
}
if s.Baseline == nil {
Expand All @@ -99,6 +99,7 @@ func (s *PlanSwitcher) TryStreamExecute(
callback func(*sqltypes.Result) error,
) error {
if s.metCondition(bindVars) {
s.addOptimizedExecStats(vcursor)
return s.Optimized.TryStreamExecute(ctx, vcursor, bindVars, wantfields, callback)
}
if s.Baseline == nil {
Expand Down Expand Up @@ -152,4 +153,9 @@ func (s *PlanSwitcher) metCondition(bindVars map[string]*querypb.BindVariable) b
return true
}

func (s *PlanSwitcher) addOptimizedExecStats(vcursor VCursor) {
planType := getPlanType(s.Optimized)
vcursor.GetExecutionMetrics().optimizedQueryExec.Add(planType.String(), 1)
}

var _ Primitive = (*PlanSwitcher)(nil)
42 changes: 42 additions & 0 deletions go/vt/vtgate/engine/plan_switcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2025 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package engine

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/servenv"
)

// TestPlanSwitcherMetrics tests that the PlanSwitcher increments the correct metric
func TestPlanSwitcherMetrics(t *testing.T) {
p := &PlanSwitcher{
Optimized: &TransactionStatus{},
}

vc := &loggingVCursor{
metrics: InitMetrics(servenv.NewExporter("PlanTest", "")),
}
initial := vc.metrics.optimizedQueryExec.Counts()
_, err := p.TryExecute(context.Background(), vc, nil, false)
require.NoError(t, err)
after := vc.metrics.optimizedQueryExec.Counts()
require.EqualValues(t, 1, after["MultiShard"]-initial["MultiShard"])
}
2 changes: 2 additions & 0 deletions go/vt/vtgate/engine/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ type (
RecordMirrorStats(time.Duration, time.Duration, error)

SetLastInsertID(uint64)

GetExecutionMetrics() *Metrics
}

// SessionActions gives primitives ability to interact with the session state
Expand Down
23 changes: 21 additions & 2 deletions go/vt/vtgate/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func init() {
// the abilities of the underlying vttablets.
type (
ExecutorConfig struct {
Name string
Normalize bool
StreamSize int
// AllowScatter will fail planning if set to false and a plan contains any scatter queries
Expand All @@ -120,14 +121,16 @@ type (
}

Executor struct {
config ExecutorConfig
config ExecutorConfig
exporter *servenv.Exporter

env *vtenv.Environment
serv srvtopo.Server
cell string
resolver *Resolver
scatterConn *ScatterConn
txConn *TxConn
metrics econtext.Metrics

mu sync.Mutex
vschema *vindexes.VSchema
Expand All @@ -147,6 +150,10 @@ type (
vConfig econtext.VCursorConfig
ddlConfig dynamicconfig.DDL
}

Metrics struct {
engineMetrics *engine.Metrics
}
)

var executorOnce sync.Once
Expand Down Expand Up @@ -180,6 +187,7 @@ func NewExecutor(
) *Executor {
e := &Executor{
config: eConfig,
exporter: servenv.NewExporter(eConfig.Name, ""),
env: env,
serv: serv,
cell: cell,
Expand All @@ -194,6 +202,9 @@ func NewExecutor(
}
// setting the vcursor config.
e.initVConfig(warnOnShardedOnly, pv)
e.metrics = &Metrics{
engineMetrics: engine.InitMetrics(e.exporter),
}

// we subscribe to update from the VSchemaManager
e.vm = &VSchemaManager{
Expand Down Expand Up @@ -1117,7 +1128,7 @@ func (e *Executor) fetchOrCreatePlan(
}

query, comments := sqlparser.SplitMarginComments(queryString)
vcursor, _ = econtext.NewVCursorImpl(safeSession, comments, e, logStats, e.vm, e.VSchema(), e.resolver.resolver, e.serv, nullResultsObserver{}, e.vConfig)
vcursor, _ = e.newVCursor(safeSession, comments, logStats)

var setVarComment string
if e.vConfig.SetVarEnabled {
Expand Down Expand Up @@ -1163,6 +1174,10 @@ func (e *Executor) fetchOrCreatePlan(
return plan, vcursor, stmt, nil
}

func (e *Executor) newVCursor(safeSession *econtext.SafeSession, comments sqlparser.MarginComments, logStats *logstats.LogStats) (*econtext.VCursorImpl, error) {
return econtext.NewVCursorImpl(safeSession, comments, e, logStats, e.vm, e.VSchema(), e.resolver.resolver, e.serv, nullResultsObserver{}, e.vConfig, e.metrics)
}

func (e *Executor) tryOptimizedPlan(
ctx context.Context,
vcursor *econtext.VCursorImpl,
Expand Down Expand Up @@ -1801,3 +1816,7 @@ func fkMode(foreignkey string) vschemapb.Keyspace_ForeignKeyMode {
}
return vschemapb.Keyspace_unspecified
}

func (m *Metrics) GetExecutionMetrics() *engine.Metrics {
return m.engineMetrics
}
5 changes: 3 additions & 2 deletions go/vt/vtgate/executor_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

econtext "vitess.io/vitess/go/vt/vtgate/executorcontext"

"vitess.io/vitess/go/cache/theine"
"vitess.io/vitess/go/constants/sidecar"
"vitess.io/vitess/go/sqltypes"
Expand All @@ -45,6 +43,7 @@ import (
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/vtenv"
"vitess.io/vitess/go/vt/vtgate/engine"
econtext "vitess.io/vitess/go/vt/vtgate/executorcontext"
"vitess.io/vitess/go/vt/vtgate/logstats"
"vitess.io/vitess/go/vt/vtgate/vindexes"
"vitess.io/vitess/go/vt/vttablet/sandboxconn"
Expand Down Expand Up @@ -245,13 +244,15 @@ func createCustomExecutor(t testing.TB, vschema string, mysqlVersion string) (ex

func createExecutorConfig() ExecutorConfig {
return ExecutorConfig{
Name: "TestExecutor",
StreamSize: 10,
AllowScatter: true,
}
}

func createExecutorConfigWithNormalizer() ExecutorConfig {
return ExecutorConfig{
Name: "TestExecutor",
StreamSize: 10,
AllowScatter: true,
Normalize: true,
Expand Down
4 changes: 1 addition & 3 deletions go/vt/vtgate/executor_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ func TestDeferredOptimization(t *testing.T) {

resolver := newTestResolver(ctx, nil, nil, "")
executor := Executor{
config: ExecutorConfig{
AllowScatter: true,
},
config: createExecutorConfig(),
env: env,
resolver: resolver,
vschema: vindexes.BuildVSchema(result, parser),
Expand Down
Loading
Loading