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: remove unused binding metrics #51665

Merged
merged 3 commits into from
Mar 11, 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
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