Skip to content

Commit

Permalink
planner: add more metrics for binding (#51739)
Browse files Browse the repository at this point in the history
ref #51347
  • Loading branch information
qw4990 authored Mar 13, 2024
1 parent 47a990e commit 834fd16
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 25 deletions.
2 changes: 0 additions & 2 deletions pkg/bindinfo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ go_test(
"//pkg/bindinfo/norm",
"//pkg/config",
"//pkg/domain",
"//pkg/metrics",
"//pkg/parser",
"//pkg/parser/ast",
"//pkg/parser/auth",
Expand All @@ -90,7 +89,6 @@ go_test(
"//pkg/util/stmtsummary",
"@com_github_ngaut_pools//:pools",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_prometheus_client_model//go",
"@com_github_stretchr_testify//require",
"@io_opencensus_go//stats/view",
"@org_uber_go_goleak//:goleak",
Expand Down
11 changes: 11 additions & 0 deletions pkg/bindinfo/binding_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/pingcap/tidb/pkg/bindinfo/internal/logutil"
"github.com/pingcap/tidb/pkg/bindinfo/norm"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/sessionctx"
Expand Down Expand Up @@ -84,11 +85,21 @@ func newFuzzyBindingCache(loadBindingFromStorageFunc func(sessionctx.Context, st
}
}

func (fbc *fuzzyBindingCache) shouldMetric() bool {
return fbc.loadBindingFromStorageFunc != nil // only metric for GlobalBindingCache, whose loadBindingFromStorageFunc is not nil.
}

func (fbc *fuzzyBindingCache) FuzzyMatchingBinding(sctx sessionctx.Context, fuzzyDigest string, tableNames []*ast.TableName) (matchedBinding Binding, isMatched bool) {
matchedBinding, isMatched, missingSQLDigest := fbc.getFromMemory(sctx, fuzzyDigest, tableNames)
if len(missingSQLDigest) == 0 {
if fbc.shouldMetric() && isMatched {
metrics.BindingCacheHitCounter.Inc()
}
return
}
if fbc.shouldMetric() {
metrics.BindingCacheMissCounter.Inc()
}
if fbc.loadBindingFromStorageFunc == nil {
return
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/bindinfo/global_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/bindinfo/internal/logutil"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/format"
Expand Down Expand Up @@ -210,6 +211,10 @@ func (h *globalBindingHandle) LoadFromStorageToCache(fullLoad bool) (err error)
defer func() {
h.setLastUpdateTime(lastUpdateTime)
h.setCache(newCache)

metrics.BindingCacheMemUsage.Set(float64(h.GetMemUsage()))
metrics.BindingCacheMemLimit.Set(float64(h.GetMemCapacity()))
metrics.BindingCacheNumBindings.Set(float64(h.Size()))
}()

for _, row := range rows {
Expand Down
10 changes: 0 additions & 10 deletions pkg/bindinfo/session_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ import (
"github.com/pingcap/tidb/pkg/bindinfo"
"github.com/pingcap/tidb/pkg/bindinfo/internal"
"github.com/pingcap/tidb/pkg/bindinfo/norm"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/auth"
"github.com/pingcap/tidb/pkg/server"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/util/stmtsummary"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)

Expand All @@ -48,14 +46,6 @@ func TestGlobalAndSessionBindingBothExist(t *testing.T) {

tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")

// Test bindingUsage, which indicates how many times the binding is used.
metrics.BindUsageCounter.Reset()
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "MergeJoin")
pb := &dto.Metric{}
err := metrics.BindUsageCounter.WithLabelValues(metrics.ScopeGlobal).Write(pb)
require.NoError(t, err)
require.Equal(t, float64(1), pb.GetCounter().GetValue())

// Test 'tidb_use_plan_baselines'
tk.MustExec("set @@tidb_use_plan_baselines = 0")
tk.MustHavePlan("SELECT * from t1,t2 where t1.id = t2.id", "HashJoin")
Expand Down
44 changes: 38 additions & 6 deletions pkg/metrics/bindinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,48 @@ import "github.com/prometheus/client_golang/prometheus"

// bindinfo metrics.
var (
BindUsageCounter *prometheus.CounterVec
BindingCacheHitCounter prometheus.Counter
BindingCacheMissCounter prometheus.Counter
BindingCacheMemUsage prometheus.Gauge
BindingCacheMemLimit prometheus.Gauge
BindingCacheNumBindings prometheus.Gauge
)

// InitBindInfoMetrics initializes bindinfo metrics.
func InitBindInfoMetrics() {
BindUsageCounter = NewCounterVec(
BindingCacheHitCounter = NewCounter(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "bindinfo",
Name: "bind_usage_counter",
Help: "Counter of query using sql bind",
}, []string{LabelScope})
Subsystem: "server",
Name: "binding_cache_hit_total",
Help: "Counter of binding cache hit.",
})
BindingCacheMissCounter = NewCounter(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "binding_cache_miss_total",
Help: "Counter of binding cache miss.",
})
BindingCacheMemUsage = NewGauge(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "binding_cache_mem_usage",
Help: "Memory usage of binding cache.",
})
BindingCacheMemLimit = NewGauge(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "binding_cache_mem_limit",
Help: "Memory limit of binding cache.",
})
BindingCacheNumBindings = NewGauge(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "binding_cache_num_bindings",
Help: "Number of bindings in binding cache.",
})
}
Loading

0 comments on commit 834fd16

Please sign in to comment.