Skip to content

Commit

Permalink
metrics: add infoschema v2 cache hit/miss/evict (#54268)
Browse files Browse the repository at this point in the history
ref #50959
  • Loading branch information
tangenta authored Jun 30, 2024
1 parent 3731796 commit 4381699
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/infoschema/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"infoschema_v2.go",
"interface.go",
"metric_table_def.go",
"metrics.go",
"metrics_schema.go",
"sieve.go",
"tables.go",
Expand All @@ -31,6 +32,7 @@ go_library(
"//pkg/kv",
"//pkg/meta",
"//pkg/meta/autoid",
"//pkg/metrics",
"//pkg/parser/auth",
"//pkg/parser/charset",
"//pkg/parser/model",
Expand Down Expand Up @@ -61,6 +63,7 @@ go_library(
"@com_github_pingcap_kvproto//pkg/diagnosticspb",
"@com_github_pingcap_kvproto//pkg/metapb",
"@com_github_pingcap_log//:log",
"@com_github_prometheus_client_golang//prometheus",
"@com_github_tidwall_btree//:btree",
"@com_github_tikv_client_go_v2//tikv",
"@com_github_tikv_pd_client//http",
Expand Down
1 change: 1 addition & 0 deletions pkg/infoschema/infoschema_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func NewData() *Data {
pid2tid: btree.NewBTreeG[partitionItem](comparePartitionItem),
tableInfoResident: btree.NewBTreeG[tableInfoItem](compareTableInfoItem),
}
ret.tableCache.SetStatusHook(newSieveStatusHookImpl())
return ret
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/infoschema/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package infoschema

import (
"github.com/pingcap/tidb/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)

type sieveStatusHookImpl struct {
evict prometheus.Counter
hit prometheus.Counter
miss prometheus.Counter
}

func newSieveStatusHookImpl() *sieveStatusHookImpl {
return &sieveStatusHookImpl{
evict: metrics.InfoSchemaV2CacheCounter.WithLabelValues("evict"),
hit: metrics.InfoSchemaV2CacheCounter.WithLabelValues("hit"),
miss: metrics.InfoSchemaV2CacheCounter.WithLabelValues("miss"),
}
}

func (s *sieveStatusHookImpl) onEvict() {
s.evict.Inc()
}

func (s *sieveStatusHookImpl) onHit() {
s.hit.Inc()
}

func (s *sieveStatusHookImpl) onMiss() {
s.miss.Inc()
}
25 changes: 24 additions & 1 deletion pkg/infoschema/sieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,24 @@ type Sieve[K comparable, V any] struct {
items map[K]*entry[K, V]
ll *list.List
hand *list.Element

hook sieveStatusHook
}

type sieveStatusHook interface {
onHit()
onMiss()
onEvict()
}

type emptySieveStatusHook struct{}

func (e *emptySieveStatusHook) onHit() {}

func (e *emptySieveStatusHook) onMiss() {}

func (e *emptySieveStatusHook) onEvict() {}

func newSieve[K comparable, V any](capacity uint64) *Sieve[K, V] {
ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -64,11 +80,16 @@ func newSieve[K comparable, V any](capacity uint64) *Sieve[K, V] {
capacity: capacity,
items: make(map[K]*entry[K, V]),
ll: list.New(),
hook: &emptySieveStatusHook{},
}

return cache
}

func (s *Sieve[K, V]) SetStatusHook(hook sieveStatusHook) {
s.hook = hook
}

func (s *Sieve[K, V]) SetCapacity(capacity uint64) {
s.mu.Lock()
defer s.mu.Unlock()
Expand Down Expand Up @@ -110,9 +131,10 @@ func (s *Sieve[K, V]) Get(key K) (value V, ok bool) {
defer s.mu.Unlock()
if e, ok := s.items[key]; ok {
e.visited = true
s.hook.onHit()
return e.value, true
}

s.hook.onMiss()
return
}

Expand Down Expand Up @@ -217,4 +239,5 @@ func (s *Sieve[K, V]) evict() {

s.hand = o.Prev()
s.removeEntry(el)
s.hook.onEvict()
}
1 change: 1 addition & 0 deletions pkg/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"gc_worker.go",
"globalsort.go",
"import.go",
"infoschema.go",
"log_backup.go",
"meta.go",
"metrics.go",
Expand Down
118 changes: 118 additions & 0 deletions pkg/metrics/grafana/tidb.json
Original file line number Diff line number Diff line change
Expand Up @@ -13902,6 +13902,124 @@
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"dashLength": 10,
"datasource": "${DS_TEST-CLUSTER}",
"description": "Infoschema cache v2 hit, evict and miss number",
"fieldConfig": {
"defaults": {},
"overrides": []
},
"fill": 1,
"gridPos": {
"h": 7,
"w": 12,
"x": 12,
"y": 31
},
"id": 23763572012,
"legend": {
"alignAsTable": true,
"avg": true,
"current": true,
"hideEmpty": false,
"hideZero": false,
"max": false,
"min": false,
"rightSide": true,
"show": true,
"sideWidth": null,
"total": false,
"values": true
},
"lines": true,
"linewidth": 2,
"nullPointMode": "null as zero",
"options": {
"alertThreshold": true
},
"pluginVersion": "7.5.17",
"pointradius": 2,
"renderer": "flot",
"seriesOverrides": [
{
"alias": "hit/(hit+miss)",
"yaxis": 2
}
],
"spaceLength": 10,
"targets": [
{
"expr": "sum(rate(tidb_domain_infoschema_v2_cache{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\"}[1m])) by (type)",
"legendFormat": "{{type}}",
"interval": "",
"exemplar": true,
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
"step": 40
},
{
"expr": "sum(rate(tidb_domain_infoschema_v2_cache{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\", type=\"hit\"}[1m]))/(sum(rate(tidb_domain_infoschema_v2_cache{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\", type=\"hit\"}[1m]))+sum(rate(tidb_domain_infoschema_v2_cache{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\", type=\"miss\"}[1m])))",
"legendFormat": "hit/(hit+miss)",
"interval": "",
"exemplar": true,
"refId": "B",
"hide": false
}
],
"thresholds": [],
"timeRegions": [],
"title": "Infoschema Cache v2",
"tooltip": {
"shared": true,
"sort": 2,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"$$hashKey": "object:80",
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": 0,
"show": true
},
{
"$$hashKey": "object:81",
"format": "percentunit",
"label": "",
"logBase": 1,
"max": "1",
"min": "0",
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
},
"bars": false,
"dashes": false,
"fillGradient": 0,
"hiddenSeries": false,
"percentage": false,
"points": false,
"stack": false,
"steppedLine": false,
"timeFrom": null,
"timeShift": null
}
],
"repeat": null,
Expand Down
35 changes: 35 additions & 0 deletions pkg/metrics/infoschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics

import (
"github.com/prometheus/client_golang/prometheus"
)

var (
// InfoSchemaV2CacheCounter records the counter of infoschema v2 cache hit/miss/evict.
InfoSchemaV2CacheCounter *prometheus.CounterVec
)

// InitInfoSchemaV2Metrics intializes infoschema v2 related metrics.
func InitInfoSchemaV2Metrics() {
InfoSchemaV2CacheCounter = NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "domain",
Name: "infoschema_v2_cache",
Help: "infoschema cache v2 hit, evict and miss number",
}, []string{LblType})
}
3 changes: 3 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func InitMetrics() {
InitDistTaskMetrics()
InitResourceGroupMetrics()
InitGlobalSortMetrics()
InitInfoSchemaV2Metrics()
timermetrics.InitTimerMetrics()

PanicCounter = NewCounterVec(
Expand Down Expand Up @@ -280,6 +281,8 @@ func RegisterMetrics() {
prometheus.MustRegister(GlobalSortUploadWorkerCount)
prometheus.MustRegister(AddIndexScanRate)

prometheus.MustRegister(InfoSchemaV2CacheCounter)

prometheus.MustRegister(BindingCacheHitCounter)
prometheus.MustRegister(BindingCacheMissCounter)
prometheus.MustRegister(BindingCacheMemUsage)
Expand Down

0 comments on commit 4381699

Please sign in to comment.