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

planner: add more metrics for binding #51739

Merged
merged 7 commits into from
Mar 13, 2024
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless, just remove it

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",
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
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