Skip to content

Commit a632277

Browse files
authored
planner: remove unused binding metrics (#51665)
ref #51347
1 parent 2b63184 commit a632277

7 files changed

+1
-83
lines changed

pkg/bindinfo/binding.go

-40
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"time"
1919
"unsafe"
2020

21-
"github.com/pingcap/tidb/pkg/metrics"
2221
"github.com/pingcap/tidb/pkg/parser"
2322
"github.com/pingcap/tidb/pkg/parser/ast"
2423
"github.com/pingcap/tidb/pkg/sessionctx"
@@ -224,47 +223,8 @@ func (br Bindings) size() float64 {
224223
return mem
225224
}
226225

227-
var statusIndex = map[string]int{
228-
Enabled: 0,
229-
deleted: 1,
230-
Invalid: 2,
231-
}
232-
233-
func bindingMetrics(br Bindings) ([]float64, []int) {
234-
sizes := make([]float64, len(statusIndex))
235-
count := make([]int, len(statusIndex))
236-
if br == nil {
237-
return sizes, count
238-
}
239-
commonLength := float64(0)
240-
// We treat it as deleted if there are no bindings. It could only occur in session handles.
241-
if len(br) == 0 {
242-
sizes[statusIndex[deleted]] = commonLength
243-
count[statusIndex[deleted]] = 1
244-
return sizes, count
245-
}
246-
// Make the common length counted in the first binding.
247-
sizes[statusIndex[br[0].Status]] = commonLength
248-
for _, binding := range br {
249-
sizes[statusIndex[binding.Status]] += binding.size()
250-
count[statusIndex[binding.Status]]++
251-
}
252-
return sizes, count
253-
}
254-
255226
// size calculates the memory size of a bind info.
256227
func (b *Binding) size() float64 {
257228
res := len(b.OriginalSQL) + len(b.Db) + len(b.BindSQL) + len(b.Status) + 2*int(unsafe.Sizeof(b.CreateTime)) + len(b.Charset) + len(b.Collation) + len(b.ID)
258229
return float64(res)
259230
}
260-
261-
func updateMetrics(scope string, before Bindings, after Bindings, sizeOnly bool) {
262-
beforeSize, beforeCount := bindingMetrics(before)
263-
afterSize, afterCount := bindingMetrics(after)
264-
for status, index := range statusIndex {
265-
metrics.BindMemoryUsage.WithLabelValues(scope, status).Add(afterSize[index] - beforeSize[index])
266-
if !sizeOnly {
267-
metrics.BindTotalGauge.WithLabelValues(scope, status).Add(float64(afterCount[index] - beforeCount[index]))
268-
}
269-
}
270-
}

pkg/bindinfo/global_handle.go

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"time"
2424

2525
"github.com/pingcap/errors"
26-
"github.com/pingcap/tidb/pkg/metrics"
2726
"github.com/pingcap/tidb/pkg/parser"
2827
"github.com/pingcap/tidb/pkg/parser/ast"
2928
"github.com/pingcap/tidb/pkg/parser/format"
@@ -243,7 +242,6 @@ func (h *globalBindingHandle) LoadFromStorageToCache(fullLoad bool) (err error)
243242
} else {
244243
newCache.RemoveBinding(sqlDigest)
245244
}
246-
updateMetrics(metrics.ScopeGlobal, oldBinding, newCache.GetBinding(sqlDigest), true)
247245
}
248246
return nil
249247
})

pkg/bindinfo/global_handle_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/pingcap/tidb/pkg/bindinfo"
2424
"github.com/pingcap/tidb/pkg/bindinfo/internal"
2525
"github.com/pingcap/tidb/pkg/bindinfo/norm"
26-
"github.com/pingcap/tidb/pkg/metrics"
2726
"github.com/pingcap/tidb/pkg/parser"
2827
sessiontypes "github.com/pingcap/tidb/pkg/session/types"
2928
"github.com/pingcap/tidb/pkg/testkit"
@@ -425,9 +424,6 @@ func TestGlobalBinding(t *testing.T) {
425424
tk.MustExec("create table t1(i int, s varchar(20))")
426425
tk.MustExec("create index index_t on t(i,s)")
427426

428-
metrics.BindTotalGauge.Reset()
429-
metrics.BindMemoryUsage.Reset()
430-
431427
_, err := tk.Exec("create global " + testSQL.createSQL)
432428
require.NoError(t, err, "err %v", err)
433429

pkg/bindinfo/session_handle.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"time"
2222

2323
"github.com/pingcap/errors"
24-
"github.com/pingcap/tidb/pkg/metrics"
2524
"github.com/pingcap/tidb/pkg/parser"
2625
"github.com/pingcap/tidb/pkg/parser/ast"
2726
"github.com/pingcap/tidb/pkg/parser/mysql"
@@ -68,12 +67,10 @@ func NewSessionBindingHandle() SessionBindingHandle {
6867
// appendSessionBinding adds the Bindings to the cache, all the stale bindMetas are
6968
// removed from the cache after this operation.
7069
func (h *sessionBindingHandle) appendSessionBinding(sqlDigest string, meta Bindings) {
71-
oldBindings := h.ch.GetBinding(sqlDigest)
7270
err := h.ch.SetBinding(sqlDigest, meta)
7371
if err != nil {
7472
logutil.BgLogger().Warn("SessionHandle.appendSessionBinding", zap.String("category", "sql-bind"), zap.Error(err))
7573
}
76-
updateMetrics(metrics.ScopeSession, oldBindings, meta, false)
7774
}
7875

7976
// CreateSessionBinding creates a Bindings to the cache.
@@ -146,9 +143,7 @@ func (h *sessionBindingHandle) DecodeSessionStates(_ context.Context, sctx sessi
146143
}
147144

148145
// Close closes the session handle.
149-
func (h *sessionBindingHandle) Close() {
150-
updateMetrics(metrics.ScopeSession, h.ch.GetAllBindings(), nil, false)
151-
}
146+
func (*sessionBindingHandle) Close() {}
152147

153148
// sessionBindInfoKeyType is a dummy type to avoid naming collision in context.
154149
type sessionBindInfoKeyType int

pkg/bindinfo/session_handle_test.go

-11
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ func TestSessionBinding(t *testing.T) {
9191
tk.MustExec("create table t1(i int, s varchar(20))")
9292
tk.MustExec("create index index_t on t(i,s)")
9393

94-
metrics.BindTotalGauge.Reset()
95-
metrics.BindMemoryUsage.Reset()
96-
9794
_, err := tk.Exec("create session " + testSQL.createSQL)
9895
require.NoError(t, err, "err %v", err)
9996

@@ -102,14 +99,6 @@ func TestSessionBinding(t *testing.T) {
10299
require.NoError(t, err)
103100
}
104101

105-
pb := &dto.Metric{}
106-
err = metrics.BindTotalGauge.WithLabelValues(metrics.ScopeSession, bindinfo.Enabled).Write(pb)
107-
require.NoError(t, err)
108-
require.Equal(t, float64(1), pb.GetGauge().GetValue())
109-
err = metrics.BindMemoryUsage.WithLabelValues(metrics.ScopeSession, bindinfo.Enabled).Write(pb)
110-
require.NoError(t, err)
111-
require.Equal(t, testSQL.memoryUsage, pb.GetGauge().GetValue())
112-
113102
handle := tk.Session().Value(bindinfo.SessionBindInfoKeyType).(bindinfo.SessionBindingHandle)
114103
stmt, err := parser.New().ParseOneStmt(testSQL.originSQL, "", "")
115104
require.NoError(t, err)

pkg/metrics/bindinfo.go

-18
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import "github.com/prometheus/client_golang/prometheus"
1919
// bindinfo metrics.
2020
var (
2121
BindUsageCounter *prometheus.CounterVec
22-
BindTotalGauge *prometheus.GaugeVec
23-
BindMemoryUsage *prometheus.GaugeVec
2422
)
2523

2624
// InitBindInfoMetrics initializes bindinfo metrics.
@@ -32,20 +30,4 @@ func InitBindInfoMetrics() {
3230
Name: "bind_usage_counter",
3331
Help: "Counter of query using sql bind",
3432
}, []string{LabelScope})
35-
36-
BindTotalGauge = NewGaugeVec(
37-
prometheus.GaugeOpts{
38-
Namespace: "tidb",
39-
Subsystem: "bindinfo",
40-
Name: "bind_total_gauge",
41-
Help: "Total number of sql bind",
42-
}, []string{LabelScope, LblType})
43-
44-
BindMemoryUsage = NewGaugeVec(
45-
prometheus.GaugeOpts{
46-
Namespace: "tidb",
47-
Subsystem: "bindinfo",
48-
Name: "bind_memory_usage",
49-
Help: "Memory usage of sql bind",
50-
}, []string{LabelScope, LblType})
5133
}

pkg/metrics/metrics.go

-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ func RegisterMetrics() {
124124
prometheus.MustRegister(AutoIDHistogram)
125125
prometheus.MustRegister(BatchAddIdxHistogram)
126126
prometheus.MustRegister(BindUsageCounter)
127-
prometheus.MustRegister(BindTotalGauge)
128-
prometheus.MustRegister(BindMemoryUsage)
129127
prometheus.MustRegister(CampaignOwnerCounter)
130128
prometheus.MustRegister(ConnGauge)
131129
prometheus.MustRegister(DisconnectionCounter)

0 commit comments

Comments
 (0)