From 128eb0b42b1a076958375943aa3eed91ab9f5616 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 8 Jan 2025 11:20:24 +0800 Subject: [PATCH 1/5] infoschema,metrics: add a metric for infoschema v2 cache objects count --- pkg/infoschema/metrics.go | 5 +- pkg/infoschema/sieve.go | 11 ++-- pkg/metrics/grafana/tidb.json | 99 ++++++++++++++++++++++++++++++++++- pkg/metrics/infoschema.go | 9 ++++ pkg/metrics/metrics.go | 1 + 5 files changed, 119 insertions(+), 6 deletions(-) diff --git a/pkg/infoschema/metrics.go b/pkg/infoschema/metrics.go index 687ebd4f9db43..8d1bcd37fa369 100644 --- a/pkg/infoschema/metrics.go +++ b/pkg/infoschema/metrics.go @@ -24,6 +24,7 @@ type sieveStatusHookImpl struct { hit prometheus.Counter miss prometheus.Counter + objCnt prometheus.Gauge memUsage prometheus.Gauge memLimit prometheus.Gauge } @@ -34,6 +35,7 @@ func newSieveStatusHookImpl() *sieveStatusHookImpl { hit: metrics.InfoSchemaV2CacheCounter.WithLabelValues("hit"), miss: metrics.InfoSchemaV2CacheCounter.WithLabelValues("miss"), + objCnt: metrics.InfoSchemaV2CacheObjCnt, memUsage: metrics.InfoSchemaV2CacheMemUsage, memLimit: metrics.InfoSchemaV2CacheMemLimit, } @@ -51,8 +53,9 @@ func (s *sieveStatusHookImpl) onMiss() { s.miss.Inc() } -func (s *sieveStatusHookImpl) onUpdateSize(size uint64) { +func (s *sieveStatusHookImpl) onUpdate(size uint64, count uint64) { s.memUsage.Set(float64(size)) + s.objCnt.Set(float64(count)) } func (s *sieveStatusHookImpl) onUpdateLimit(limit uint64) { diff --git a/pkg/infoschema/sieve.go b/pkg/infoschema/sieve.go index f61e776b4713b..07030652779b8 100644 --- a/pkg/infoschema/sieve.go +++ b/pkg/infoschema/sieve.go @@ -49,6 +49,7 @@ type Sieve[K comparable, V any] struct { ctx context.Context cancel context.CancelFunc mu sync.Mutex + count uint64 size uint64 // capacity can be set to zero for disabling infoschema v2 capacity uint64 @@ -63,7 +64,7 @@ type sieveStatusHook interface { onHit() onMiss() onEvict() - onUpdateSize(size uint64) + onUpdate(size uint64, count uint64) onUpdateLimit(limit uint64) } @@ -75,7 +76,7 @@ func (e *emptySieveStatusHook) onMiss() {} func (e *emptySieveStatusHook) onEvict() {} -func (e *emptySieveStatusHook) onUpdateSize(_ uint64) {} +func (e *emptySieveStatusHook) onUpdate(_, _ uint64) {} func (e *emptySieveStatusHook) onUpdateLimit(_ uint64) {} @@ -145,7 +146,8 @@ 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) + s.count += 1 + s.hook.onUpdate(s.size, s.count) e.element = s.ll.PushFront(key) s.items[key] = e @@ -239,7 +241,8 @@ 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) + s.count -= 1 + s.hook.onUpdate(s.size, s.count) } func (s *Sieve[K, V]) evict() { diff --git a/pkg/metrics/grafana/tidb.json b/pkg/metrics/grafana/tidb.json index 096e2facfd2e7..3f390f84bd188 100644 --- a/pkg/metrics/grafana/tidb.json +++ b/pkg/metrics/grafana/tidb.json @@ -14604,7 +14604,104 @@ "align": false, "alignLevel": null } - } + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_TEST-CLUSTER}", + "description": "Cached table count of infoschema cache v2", + "fieldConfig": { + "defaults": {}, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 46 + }, + "hiddenSeries": false, + "id": 23763572016, + "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_count{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\"}", + "interval": "", + "legendFormat": "used", + "refId": "A" + } + ], + "thresholds": [], + "timeFrom": null, + "timeRegions": [], + "timeShift": null, + "title": "Infoschema v2 Cache Table Count", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": false + } + ], + "yaxis": { + "align": false, + "alignLevel": null + } + } ], "repeat": null, "title": "Schema Load", diff --git a/pkg/metrics/infoschema.go b/pkg/metrics/infoschema.go index f5c467540f6e1..4c369f11f2ac1 100644 --- a/pkg/metrics/infoschema.go +++ b/pkg/metrics/infoschema.go @@ -23,6 +23,8 @@ var ( InfoSchemaV2CacheCounter *prometheus.CounterVec // InfoSchemaV2CacheMemUsage records the memory size of infoschema v2 cache. InfoSchemaV2CacheMemUsage prometheus.Gauge + // InfoSchemaV2CacheObjCnt records the table count of infoschema v2 cache. + InfoSchemaV2CacheObjCnt prometheus.Gauge // InfoSchemaV2CacheMemLimit records the memory limit of infoschema v2 cache. InfoSchemaV2CacheMemLimit prometheus.Gauge // TableByNameDuration records the duration of TableByName API for infoschema v2. @@ -42,6 +44,13 @@ func InitInfoSchemaV2Metrics() { Name: "infoschema_v2_cache", Help: "infoschema cache v2 hit, evict and miss number", }, []string{LblType}) + InfoSchemaV2CacheObjCnt = NewGauge( + prometheus.GaugeOpts{ + Namespace: "tidb", + Subsystem: "domain", + Name: "infoschema_v2_cache_count", + Help: "infoschema cache v2 table count", + }) InfoSchemaV2CacheMemUsage = NewGauge( prometheus.GaugeOpts{ Namespace: "tidb", diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 17670041c2820..0c6123c422adf 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -288,6 +288,7 @@ func RegisterMetrics() { prometheus.MustRegister(InfoSchemaV2CacheCounter) prometheus.MustRegister(InfoSchemaV2CacheMemUsage) prometheus.MustRegister(InfoSchemaV2CacheMemLimit) + prometheus.MustRegister(InfoSchemaV2CacheObjCnt) prometheus.MustRegister(TableByNameDuration) prometheus.MustRegister(BindingCacheHitCounter) From de299a28175ddb57c8e771105a522d81a0d62187 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 8 Jan 2025 12:30:33 +0800 Subject: [PATCH 2/5] fix build --- pkg/infoschema/infoschemav2_cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/infoschema/infoschemav2_cache_test.go b/pkg/infoschema/infoschemav2_cache_test.go index 14b2fb2928bb9..7c8f050cce024 100644 --- a/pkg/infoschema/infoschemav2_cache_test.go +++ b/pkg/infoschema/infoschemav2_cache_test.go @@ -126,7 +126,7 @@ func (m *mockStatusHook) onMiss() { func (m *mockStatusHook) onEvict() { m.Called() } -func (m *mockStatusHook) onUpdateSize(size uint64) { +func (m *mockStatusHook) onUpdate(size uint64, count uint64) { m.Called() } func (m *mockStatusHook) onUpdateLimit(limit uint64) { From 2b6bdeaf1e46ad5563c8098de01aba6bd5ebae35 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 10 Jan 2025 15:58:53 +0800 Subject: [PATCH 3/5] make lint happy --- pkg/metrics/grafana/tidb.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/metrics/grafana/tidb.json b/pkg/metrics/grafana/tidb.json index 7cacaa52ef4c9..24d58b86744cf 100644 --- a/pkg/metrics/grafana/tidb.json +++ b/pkg/metrics/grafana/tidb.json @@ -14724,13 +14724,13 @@ "fill": 1, "fillGradient": 0, "gridPos": { - "h": 8, + "h": 7, "w": 12, "x": 0, "y": 46 }, "hiddenSeries": false, - "id": 23763572016, + "id": 23763572018, "legend": { "alignAsTable": false, "avg": false, @@ -14770,7 +14770,7 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "Infoschema v2 Cache Table Count", + "title": "Infoschema V2 Cache Table Count", "tooltip": { "shared": true, "sort": 0, From 74a1d277e4007e0eb43553a0b7ba08b277a57e04 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 10 Jan 2025 17:19:43 +0800 Subject: [PATCH 4/5] fix test --- pkg/infoschema/infoschemav2_cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/infoschema/infoschemav2_cache_test.go b/pkg/infoschema/infoschemav2_cache_test.go index 7c8f050cce024..522e383aaa0a1 100644 --- a/pkg/infoschema/infoschemav2_cache_test.go +++ b/pkg/infoschema/infoschemav2_cache_test.go @@ -152,7 +152,7 @@ func (tc *testCase) run(t *testing.T) { data := NewData() data.tableCache.SetStatusHook(tc.statusHook) - tc.statusHook.On("onUpdateSize").Return() + tc.statusHook.On("onUpdate").Return() tableSize := mockTableSize(tc.r, t) tc.statusHook.On("onUpdateLimit").Return().Once() From 8aaafb4155cb121a826cb69158d2a8469be265a7 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 14 Jan 2025 12:05:16 +0800 Subject: [PATCH 5/5] Update pkg/metrics/grafana/tidb.json Co-authored-by: Weizhen Wang --- pkg/metrics/grafana/tidb.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/metrics/grafana/tidb.json b/pkg/metrics/grafana/tidb.json index 24d58b86744cf..fc6aebba8ac2d 100644 --- a/pkg/metrics/grafana/tidb.json +++ b/pkg/metrics/grafana/tidb.json @@ -14721,7 +14721,7 @@ "defaults": {}, "overrides": [] }, - "fill": 1, + "fill": 0, "fillGradient": 0, "gridPos": { "h": 7,