Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add metric for queries run with optimizer #26981

Merged
merged 1 commit into from Jul 18, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/sql/conn_executor.go
Expand Up @@ -268,7 +268,9 @@ func NewServer(cfg *ExecutorConfig, pool *mon.BytesMonitor) *Server {
return &Server{
cfg: cfg,
EngineMetrics: EngineMetrics{
DistSQLSelectCount: metric.NewCounter(MetaDistSQLSelect),
DistSQLSelectCount: metric.NewCounter(MetaDistSQLSelect),
SQLOptCount: metric.NewCounter(MetaSQLOpt),
SQLOptFallbackCount: metric.NewCounter(MetaSQLOptFallback),
// TODO(mrtracy): See HistogramWindowInterval in server/config.go for the 6x factor.
DistSQLExecLatency: metric.NewLatency(MetaDistSQLExecLatency,
6*metricsSampleInterval),
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/conn_executor_exec.go
Expand Up @@ -656,7 +656,7 @@ func (ex *connExecutor) execStmtInParallel(
planner.statsCollector.PhaseTimes()[plannerEndExecStmt] = timeutil.Now()

ex.recordStatementSummary(
planner, stmt, false /* distSQLUsed*/, ex.extraTxnState.autoRetryCounter,
planner, stmt, false /* distSQLUsed*/, false /* optUsed */, ex.extraTxnState.autoRetryCounter,
res.RowsAffected(), err, &ex.server.EngineMetrics,
)
if ex.server.cfg.TestingKnobs.AfterExecute != nil {
Expand Down Expand Up @@ -807,8 +807,9 @@ func (ex *connExecutor) dispatchToExecutionEngine(
return err
}
ex.recordStatementSummary(
planner, stmt, useDistSQL, ex.extraTxnState.autoRetryCounter,
res.RowsAffected(), res.Err(), &ex.server.EngineMetrics,
planner, stmt, useDistSQL, optimizerPlanned,
ex.extraTxnState.autoRetryCounter, res.RowsAffected(), res.Err(),
&ex.server.EngineMetrics,
)
if ex.server.cfg.TestingKnobs.AfterExecute != nil {
ex.server.cfg.TestingKnobs.AfterExecute(ctx, stmt.String(), res.Err())
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql/exec_util.go
Expand Up @@ -176,6 +176,18 @@ var (
Measurement: "Latency",
Unit: metric.Unit_NANOSECONDS,
}
MetaSQLOpt = metric.Metadata{
Name: "sql.optimizer.count",
Help: "Number of statements which ran with the cost-based optimizer",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSQLOptFallback = metric.Metadata{
Name: "sql.optimizer.fallback.count",
Help: "Number of statements which the cost-based optimizer was unable to plan",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaDistSQLSelect = metric.Metadata{
Name: "sql.distsql.select.count",
Help: "Number of DistSQL SELECT statements",
Expand Down
17 changes: 16 additions & 1 deletion pkg/sql/executor_statement_metrics.go
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
)
Expand Down Expand Up @@ -64,7 +65,12 @@ type phaseTimes [sessionNumPhases]time.Time
// EngineMetrics groups a set of SQL metrics.
type EngineMetrics struct {
// The subset of SELECTs that are processed through DistSQL.
DistSQLSelectCount *metric.Counter
DistSQLSelectCount *metric.Counter
// The subset of queries that are processed by the cost-based optimizer.
SQLOptCount *metric.Counter
// The subset of queries which we attempted and failed to plan with the
// cost-based optimizer.
SQLOptFallbackCount *metric.Counter
DistSQLExecLatency *metric.Histogram
SQLExecLatency *metric.Histogram
DistSQLServiceLatency *metric.Histogram
Expand All @@ -89,6 +95,7 @@ func (ex *connExecutor) recordStatementSummary(
planner *planner,
stmt Statement,
distSQLUsed bool,
optUsed bool,
automaticRetryCount int,
rowsAffected int,
err error,
Expand Down Expand Up @@ -122,6 +129,14 @@ func (ex *connExecutor) recordStatementSummary(
execOverhead := svcLat - processingLat

if automaticRetryCount == 0 {
if optUsed {
m.SQLOptCount.Inc(1)
}

if !optUsed && planner.SessionData().OptimizerMode == sessiondata.OptimizerOn {
m.SQLOptFallbackCount.Inc(1)
}

if distSQLUsed {
if _, ok := stmt.AST.(*tree.Select); ok {
m.DistSQLSelectCount.Inc(1)
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/metric_test.go
Expand Up @@ -34,6 +34,8 @@ type queryCounter struct {
txnBeginCount int64
selectCount int64
distSQLSelectCount int64
optCount int64
fallbackCount int64
updateCount int64
insertCount int64
deleteCount int64
Expand All @@ -54,6 +56,7 @@ func TestQueryCounts(t *testing.T) {

var testcases = []queryCounter{
// The counts are deltas for each query.
{query: "SET EXPERIMENTAL_OPT = 'off'", miscCount: 1},
{query: "SET DISTSQL = 'off'", miscCount: 1},
{query: "BEGIN; END", txnBeginCount: 1, txnCommitCount: 1},
{query: "SELECT 1", selectCount: 1, txnCommitCount: 1},
Expand Down Expand Up @@ -81,6 +84,11 @@ func TestQueryCounts(t *testing.T) {
{query: "SET DISTSQL = 'off'", miscCount: 1},
{query: "DROP TABLE mt.n", ddlCount: 1},
{query: "SET database = system", miscCount: 1},
{query: "SET EXPERIMENTAL_OPT = 'on'", miscCount: 1, fallbackCount: 1},
{query: "SELECT 3", selectCount: 1, optCount: 1},
{query: "CREATE TABLE mt.n (num INTEGER PRIMARY KEY)", ddlCount: 1, fallbackCount: 1},
{query: "UPDATE mt.n SET num = num + 1", updateCount: 1, fallbackCount: 1},
{query: "SET EXPERIMENTAL_OPT = 'off'", miscCount: 1},
}

accum := initializeQueryCounter(s)
Expand Down Expand Up @@ -130,6 +138,12 @@ func TestQueryCounts(t *testing.T) {
if accum.failureCount, err = checkCounterDelta(s, sql.MetaFailure, accum.failureCount, tc.failureCount); err != nil {
t.Errorf("%q: %s", tc.query, err)
}
if accum.optCount, err = checkCounterDelta(s, sql.MetaSQLOpt, accum.optCount, tc.optCount); err != nil {
t.Errorf("%q: %s", tc.query, err)
}
if accum.fallbackCount, err = checkCounterDelta(s, sql.MetaSQLOptFallback, accum.fallbackCount, tc.fallbackCount); err != nil {
t.Errorf("%q: %s", tc.query, err)
}
})
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/metric_util_test.go
Expand Up @@ -29,6 +29,7 @@ func initializeQueryCounter(s serverutils.TestServerInterface) queryCounter {
return queryCounter{
txnBeginCount: s.MustGetSQLCounter(sql.MetaTxnBegin.Name),
selectCount: s.MustGetSQLCounter(sql.MetaSelect.Name),
optCount: s.MustGetSQLCounter(sql.MetaSQLOpt.Name),
distSQLSelectCount: s.MustGetSQLCounter(sql.MetaDistSQLSelect.Name),
updateCount: s.MustGetSQLCounter(sql.MetaUpdate.Name),
insertCount: s.MustGetSQLCounter(sql.MetaInsert.Name),
Expand Down