Skip to content

Commit

Permalink
metrics,executor: add metrics for reading from table cache (pingcap#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao committed Feb 21, 2022
1 parent 09b5a8c commit bccb811
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,10 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, err error, hasMoreResults boo
sessVars.DurationParse = 0
// Clean the stale read flag when statement execution finish
sessVars.StmtCtx.IsStaleness = false

if sessVars.StmtCtx.ReadFromTableCache {
metrics.ReadFromTableCacheCounter.Inc()
}
}

// CloseRecordSet will finish the execution of current statement and do some record work
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func RegisterMetrics() {
prometheus.MustRegister(TopSQLReportDataHistogram)
prometheus.MustRegister(PDApiExecutionHistogram)
prometheus.MustRegister(CPUProfileCounter)
prometheus.MustRegister(ReadFromTableCacheCounter)

tikvmetrics.InitMetrics(TiDB, TiKVClient)
tikvmetrics.RegisterMetrics()
Expand Down
9 changes: 9 additions & 0 deletions metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ var (
Help: "Counter of query using plan cache.",
}, []string{LblType})

ReadFromTableCacheCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "read_from_tablecache_total",
Help: "Counter of query read from table cache.",
},
)

HandShakeErrorCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "tidb",
Expand Down
60 changes: 60 additions & 0 deletions table/tables/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"time"

"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/stmtsummary"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -530,3 +532,61 @@ func TestTableCacheLeaseVariable(t *testing.T) {
require.True(t, duration > time.Second)
require.True(t, duration < 3*time.Second)
}

func TestMetrics(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists test_metrics;")
tk.MustExec(`create table test_metrics(c0 int, c1 varchar(20), c2 varchar(20), unique key uk(c0));`)
tk.MustExec(`create table nt (c0 int, c1 varchar(20), c2 varchar(20), unique key uk(c0));`)
tk.MustExec(`insert into test_metrics(c0, c1, c2) values (1, null, 'green');`)
tk.MustExec(`alter table test_metrics cache;`)

tk.MustQuery("select * from test_metrics").Check(testkit.Rows("1 <nil> green"))
cached := false
for i := 0; i < 20; i++ {
if tk.HasPlan("select * from test_metrics", "UnionScan") {
cached = true
break
}
time.Sleep(50 * time.Millisecond)
}
require.True(t, cached)

counter := metrics.ReadFromTableCacheCounter
pb := &dto.Metric{}

queries := []string{
// Table scan
"select * from test_metrics",
// Index scan
"select c0 from test_metrics use index(uk) where c0 > 1",
// Index Lookup
"select c1 from test_metrics use index(uk) where c0 = 1",
// Point Get
"select c0 from test_metrics use index(uk) where c0 = 1",
// // Aggregation
"select count(*) from test_metrics",
// Join
"select * from test_metrics as a join test_metrics as b on a.c0 = b.c0 where a.c1 != 'xxx'",
}
counter.Write(pb)
i := pb.GetCounter().GetValue()

for _, query := range queries {
tk.MustQuery(query)
i++
counter.Write(pb)
hit := pb.GetCounter().GetValue()
require.Equal(t, i, hit)
}

// A counter-example that doesn't increase metrics.ReadFromTableCacheCounter.
tk.MustQuery("select * from nt")
counter.Write(pb)
hit := pb.GetCounter().GetValue()
require.Equal(t, i, hit)
}

0 comments on commit bccb811

Please sign in to comment.