From 80d392be064cdd3a9fd4af2efe3ea079f228ccfa Mon Sep 17 00:00:00 2001 From: tangenta Date: Mon, 1 Jul 2024 21:53:56 +0800 Subject: [PATCH] metrics: add infoschema v2 cache size and limit (#54364) ref pingcap/tidb#50959 --- pkg/infoschema/metrics.go | 14 +++++ pkg/infoschema/sieve.go | 9 +++ pkg/metrics/grafana/tidb.json | 111 +++++++++++++++++++++++++++++++++- pkg/metrics/infoschema.go | 18 ++++++ pkg/metrics/metrics.go | 2 + 5 files changed, 152 insertions(+), 2 deletions(-) diff --git a/pkg/infoschema/metrics.go b/pkg/infoschema/metrics.go index 1b3b316b91..687ebd4f9d 100644 --- a/pkg/infoschema/metrics.go +++ b/pkg/infoschema/metrics.go @@ -23,6 +23,9 @@ type sieveStatusHookImpl struct { evict prometheus.Counter hit prometheus.Counter miss prometheus.Counter + + memUsage prometheus.Gauge + memLimit prometheus.Gauge } func newSieveStatusHookImpl() *sieveStatusHookImpl { @@ -30,6 +33,9 @@ func newSieveStatusHookImpl() *sieveStatusHookImpl { evict: metrics.InfoSchemaV2CacheCounter.WithLabelValues("evict"), hit: metrics.InfoSchemaV2CacheCounter.WithLabelValues("hit"), miss: metrics.InfoSchemaV2CacheCounter.WithLabelValues("miss"), + + memUsage: metrics.InfoSchemaV2CacheMemUsage, + memLimit: metrics.InfoSchemaV2CacheMemLimit, } } @@ -44,3 +50,11 @@ func (s *sieveStatusHookImpl) onHit() { func (s *sieveStatusHookImpl) onMiss() { s.miss.Inc() } + +func (s *sieveStatusHookImpl) onUpdateSize(size uint64) { + s.memUsage.Set(float64(size)) +} + +func (s *sieveStatusHookImpl) onUpdateLimit(limit uint64) { + s.memLimit.Set(float64(limit)) +} diff --git a/pkg/infoschema/sieve.go b/pkg/infoschema/sieve.go index af6dd942e3..de9db48c74 100644 --- a/pkg/infoschema/sieve.go +++ b/pkg/infoschema/sieve.go @@ -61,6 +61,8 @@ type sieveStatusHook interface { onHit() onMiss() onEvict() + onUpdateSize(size uint64) + onUpdateLimit(limit uint64) } type emptySieveStatusHook struct{} @@ -71,6 +73,10 @@ func (e *emptySieveStatusHook) onMiss() {} func (e *emptySieveStatusHook) onEvict() {} +func (e *emptySieveStatusHook) onUpdateSize(_ uint64) {} + +func (e *emptySieveStatusHook) onUpdateLimit(_ uint64) {} + func newSieve[K comparable, V any](capacity uint64) *Sieve[K, V] { ctx, cancel := context.WithCancel(context.Background()) @@ -94,6 +100,7 @@ func (s *Sieve[K, V]) SetCapacity(capacity uint64) { s.mu.Lock() defer s.mu.Unlock() s.capacity = capacity + s.hook.onUpdateLimit(capacity) } func (s *Sieve[K, V]) Capacity() uint64 { @@ -121,6 +128,7 @@ func (s *Sieve[K, V]) Set(key K, value V) { value: value, } s.size += e.Size() // calculate the size first without putting to the list. + s.hook.onUpdateSize(s.size) e.element = s.ll.PushFront(key) s.items[key] = e @@ -210,6 +218,7 @@ func (s *Sieve[K, V]) removeEntry(e *entry[K, V]) { s.ll.Remove(e.element) delete(s.items, e.key) s.size -= e.Size() + s.hook.onUpdateSize(s.size) } func (s *Sieve[K, V]) evict() { diff --git a/pkg/metrics/grafana/tidb.json b/pkg/metrics/grafana/tidb.json index f44f6822ba..703c8e3fe1 100644 --- a/pkg/metrics/grafana/tidb.json +++ b/pkg/metrics/grafana/tidb.json @@ -13907,7 +13907,7 @@ "aliasColors": {}, "dashLength": 10, "datasource": "${DS_TEST-CLUSTER}", - "description": "Infoschema cache v2 hit, evict and miss number", + "description": "Infoschema v2 cache hit, evict and miss number", "fieldConfig": { "defaults": {}, "overrides": [] @@ -13972,7 +13972,7 @@ ], "thresholds": [], "timeRegions": [], - "title": "Infoschema Cache v2", + "title": "Infoschema v2 Cache Operation", "tooltip": { "shared": true, "sort": 2, @@ -14020,6 +14020,113 @@ "steppedLine": false, "timeFrom": null, "timeShift": null + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_TEST-CLUSTER}", + "description": "Memory size of infoschema cache v2", + "fieldConfig": { + "defaults": {}, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 38 + }, + "hiddenSeries": false, + "id": 23763572014, + "legend": { + "alignAsTable": false, + "avg": false, + "current": true, + "max": false, + "min": false, + "rightSide": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 2, + "nullPointMode": "null", + "options": { + "alertThreshold": false + }, + "percentage": false, + "pluginVersion": "7.5.17", + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "exemplar": true, + "expr": "tidb_domain_infoschema_v2_cache_size{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\"}", + "interval": "", + "legendFormat": "used", + "refId": "A" + }, + { + "exemplar": true, + "expr": "tidb_domain_infoschema_v2_cache_limit{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\"}", + "hide": false, + "interval": "", + "legendFormat": "limit", + "refId": "B" + } + ], + "thresholds": [], + "timeFrom": null, + "timeRegions": [], + "timeShift": null, + "title": "Infoschema v2 Cache Size", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "$$hashKey": "object:90", + "format": "bytes", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "$$hashKey": "object:91", + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": false + } + ], + "yaxis": { + "align": false, + "alignLevel": null + } } ], "repeat": null, diff --git a/pkg/metrics/infoschema.go b/pkg/metrics/infoschema.go index 0933754961..c47fe18c26 100644 --- a/pkg/metrics/infoschema.go +++ b/pkg/metrics/infoschema.go @@ -21,6 +21,10 @@ import ( var ( // InfoSchemaV2CacheCounter records the counter of infoschema v2 cache hit/miss/evict. InfoSchemaV2CacheCounter *prometheus.CounterVec + // InfoSchemaV2CacheMemUsage records the memory size of infoschema v2 cache. + InfoSchemaV2CacheMemUsage prometheus.Gauge + // InfoSchemaV2CacheMemLimit records the memory limit of infoschema v2 cache. + InfoSchemaV2CacheMemLimit prometheus.Gauge ) // InitInfoSchemaV2Metrics intializes infoschema v2 related metrics. @@ -32,4 +36,18 @@ func InitInfoSchemaV2Metrics() { Name: "infoschema_v2_cache", Help: "infoschema cache v2 hit, evict and miss number", }, []string{LblType}) + InfoSchemaV2CacheMemUsage = NewGauge( + prometheus.GaugeOpts{ + Namespace: "tidb", + Subsystem: "domain", + Name: "infoschema_v2_cache_size", + Help: "infoschema cache v2 size", + }) + InfoSchemaV2CacheMemLimit = NewGauge( + prometheus.GaugeOpts{ + Namespace: "tidb", + Subsystem: "domain", + Name: "infoschema_v2_cache_limit", + Help: "infoschema cache v2 limit", + }) } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 266ef094b0..990f407e1a 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -282,6 +282,8 @@ func RegisterMetrics() { prometheus.MustRegister(AddIndexScanRate) prometheus.MustRegister(InfoSchemaV2CacheCounter) + prometheus.MustRegister(InfoSchemaV2CacheMemUsage) + prometheus.MustRegister(InfoSchemaV2CacheMemLimit) prometheus.MustRegister(BindingCacheHitCounter) prometheus.MustRegister(BindingCacheMissCounter)