Skip to content

Commit

Permalink
planner: remove unused binding metrics (#51665)
Browse files Browse the repository at this point in the history
ref #51347
  • Loading branch information
qw4990 authored Mar 11, 2024
1 parent 2b63184 commit a632277
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 83 deletions.
40 changes: 0 additions & 40 deletions pkg/bindinfo/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"time"
"unsafe"

"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 @@ -224,47 +223,8 @@ func (br Bindings) size() float64 {
return mem
}

var statusIndex = map[string]int{
Enabled: 0,
deleted: 1,
Invalid: 2,
}

func bindingMetrics(br Bindings) ([]float64, []int) {
sizes := make([]float64, len(statusIndex))
count := make([]int, len(statusIndex))
if br == nil {
return sizes, count
}
commonLength := float64(0)
// We treat it as deleted if there are no bindings. It could only occur in session handles.
if len(br) == 0 {
sizes[statusIndex[deleted]] = commonLength
count[statusIndex[deleted]] = 1
return sizes, count
}
// Make the common length counted in the first binding.
sizes[statusIndex[br[0].Status]] = commonLength
for _, binding := range br {
sizes[statusIndex[binding.Status]] += binding.size()
count[statusIndex[binding.Status]]++
}
return sizes, count
}

// size calculates the memory size of a bind info.
func (b *Binding) size() float64 {
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)
return float64(res)
}

func updateMetrics(scope string, before Bindings, after Bindings, sizeOnly bool) {
beforeSize, beforeCount := bindingMetrics(before)
afterSize, afterCount := bindingMetrics(after)
for status, index := range statusIndex {
metrics.BindMemoryUsage.WithLabelValues(scope, status).Add(afterSize[index] - beforeSize[index])
if !sizeOnly {
metrics.BindTotalGauge.WithLabelValues(scope, status).Add(float64(afterCount[index] - beforeCount[index]))
}
}
}
2 changes: 0 additions & 2 deletions pkg/bindinfo/global_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/pingcap/errors"
"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 @@ -243,7 +242,6 @@ func (h *globalBindingHandle) LoadFromStorageToCache(fullLoad bool) (err error)
} else {
newCache.RemoveBinding(sqlDigest)
}
updateMetrics(metrics.ScopeGlobal, oldBinding, newCache.GetBinding(sqlDigest), true)
}
return nil
})
Expand Down
4 changes: 0 additions & 4 deletions pkg/bindinfo/global_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ 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"
sessiontypes "github.com/pingcap/tidb/pkg/session/types"
"github.com/pingcap/tidb/pkg/testkit"
Expand Down Expand Up @@ -425,9 +424,6 @@ func TestGlobalBinding(t *testing.T) {
tk.MustExec("create table t1(i int, s varchar(20))")
tk.MustExec("create index index_t on t(i,s)")

metrics.BindTotalGauge.Reset()
metrics.BindMemoryUsage.Reset()

_, err := tk.Exec("create global " + testSQL.createSQL)
require.NoError(t, err, "err %v", err)

Expand Down
7 changes: 1 addition & 6 deletions pkg/bindinfo/session_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

"github.com/pingcap/errors"
"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/mysql"
Expand Down Expand Up @@ -68,12 +67,10 @@ func NewSessionBindingHandle() SessionBindingHandle {
// appendSessionBinding adds the Bindings to the cache, all the stale bindMetas are
// removed from the cache after this operation.
func (h *sessionBindingHandle) appendSessionBinding(sqlDigest string, meta Bindings) {
oldBindings := h.ch.GetBinding(sqlDigest)
err := h.ch.SetBinding(sqlDigest, meta)
if err != nil {
logutil.BgLogger().Warn("SessionHandle.appendSessionBinding", zap.String("category", "sql-bind"), zap.Error(err))
}
updateMetrics(metrics.ScopeSession, oldBindings, meta, false)
}

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

// Close closes the session handle.
func (h *sessionBindingHandle) Close() {
updateMetrics(metrics.ScopeSession, h.ch.GetAllBindings(), nil, false)
}
func (*sessionBindingHandle) Close() {}

// sessionBindInfoKeyType is a dummy type to avoid naming collision in context.
type sessionBindInfoKeyType int
Expand Down
11 changes: 0 additions & 11 deletions pkg/bindinfo/session_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ func TestSessionBinding(t *testing.T) {
tk.MustExec("create table t1(i int, s varchar(20))")
tk.MustExec("create index index_t on t(i,s)")

metrics.BindTotalGauge.Reset()
metrics.BindMemoryUsage.Reset()

_, err := tk.Exec("create session " + testSQL.createSQL)
require.NoError(t, err, "err %v", err)

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

pb := &dto.Metric{}
err = metrics.BindTotalGauge.WithLabelValues(metrics.ScopeSession, bindinfo.Enabled).Write(pb)
require.NoError(t, err)
require.Equal(t, float64(1), pb.GetGauge().GetValue())
err = metrics.BindMemoryUsage.WithLabelValues(metrics.ScopeSession, bindinfo.Enabled).Write(pb)
require.NoError(t, err)
require.Equal(t, testSQL.memoryUsage, pb.GetGauge().GetValue())

handle := tk.Session().Value(bindinfo.SessionBindInfoKeyType).(bindinfo.SessionBindingHandle)
stmt, err := parser.New().ParseOneStmt(testSQL.originSQL, "", "")
require.NoError(t, err)
Expand Down
18 changes: 0 additions & 18 deletions pkg/metrics/bindinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import "github.com/prometheus/client_golang/prometheus"
// bindinfo metrics.
var (
BindUsageCounter *prometheus.CounterVec
BindTotalGauge *prometheus.GaugeVec
BindMemoryUsage *prometheus.GaugeVec
)

// InitBindInfoMetrics initializes bindinfo metrics.
Expand All @@ -32,20 +30,4 @@ func InitBindInfoMetrics() {
Name: "bind_usage_counter",
Help: "Counter of query using sql bind",
}, []string{LabelScope})

BindTotalGauge = NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "bindinfo",
Name: "bind_total_gauge",
Help: "Total number of sql bind",
}, []string{LabelScope, LblType})

BindMemoryUsage = NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "bindinfo",
Name: "bind_memory_usage",
Help: "Memory usage of sql bind",
}, []string{LabelScope, LblType})
}
2 changes: 0 additions & 2 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ func RegisterMetrics() {
prometheus.MustRegister(AutoIDHistogram)
prometheus.MustRegister(BatchAddIdxHistogram)
prometheus.MustRegister(BindUsageCounter)
prometheus.MustRegister(BindTotalGauge)
prometheus.MustRegister(BindMemoryUsage)
prometheus.MustRegister(CampaignOwnerCounter)
prometheus.MustRegister(ConnGauge)
prometheus.MustRegister(DisconnectionCounter)
Expand Down

0 comments on commit a632277

Please sign in to comment.