From f0f4dea8aeda7dd9d8ac20f78d29d800bb9e7c92 Mon Sep 17 00:00:00 2001 From: crazycs Date: Mon, 4 Sep 2023 18:14:21 +0800 Subject: [PATCH 1/6] metrics: refine metrics of infoschema (#46612) ref pingcap/tidb#46524 --- domain/BUILD.bazel | 1 + domain/domain.go | 9 ++- infoschema/metrics/metrics.go | 58 +++++++++++++++ metrics/domain.go | 4 +- metrics/grafana/tidb.json | 132 +++++++++++++++++++++++++++++++--- 5 files changed, 191 insertions(+), 13 deletions(-) create mode 100644 infoschema/metrics/metrics.go diff --git a/domain/BUILD.bazel b/domain/BUILD.bazel index bc0166c7f2d00..604c703571831 100644 --- a/domain/BUILD.bazel +++ b/domain/BUILD.bazel @@ -31,6 +31,7 @@ go_library( "//domain/infosync", "//errno", "//infoschema", + "//infoschema/metrics", "//infoschema/perfschema", "//kv", "//meta", diff --git a/domain/domain.go b/domain/domain.go index f57f769509966..cca23e1d3fdc1 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -44,6 +44,7 @@ import ( "github.com/pingcap/tidb/domain/infosync" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/infoschema" + infoschema_metrics "github.com/pingcap/tidb/infoschema/metrics" "github.com/pingcap/tidb/infoschema/perfschema" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" @@ -172,6 +173,10 @@ func (do *Domain) EtcdClient() *clientv3.Client { // 4. the changed table IDs if it is not full load // 5. an error if any func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, int64, *transaction.RelatedSchemaChange, error) { + beginTime := time.Now() + defer func() { + infoschema_metrics.LoadSchemaDurationTotal.Observe(time.Since(beginTime).Seconds()) + }() snapshot := do.store.GetSnapshot(kv.NewVersion(startTS)) m := meta.NewSnapshotMeta(snapshot) neededSchemaVersion, err := m.GetSchemaVersionWithNonEmptyDiff() @@ -207,6 +212,7 @@ func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, i if currentSchemaVersion != 0 && neededSchemaVersion > currentSchemaVersion && neededSchemaVersion-currentSchemaVersion < 100 { is, relatedChanges, err := do.tryLoadSchemaDiffs(m, currentSchemaVersion, neededSchemaVersion) if err == nil { + infoschema_metrics.LoadSchemaDurationLoadDiff.Observe(time.Since(startTime).Seconds()) do.infoCache.Insert(is, uint64(schemaTs)) logutil.BgLogger().Info("diff load InfoSchema success", zap.Int64("currentSchemaVersion", currentSchemaVersion), @@ -234,6 +240,7 @@ func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, i if err != nil { return nil, false, currentSchemaVersion, nil, err } + infoschema_metrics.LoadSchemaDurationLoadAll.Observe(time.Since(startTime).Seconds()) logutil.BgLogger().Info("full load InfoSchema success", zap.Int64("currentSchemaVersion", currentSchemaVersion), zap.Int64("neededSchemaVersion", neededSchemaVersion), @@ -415,6 +422,7 @@ func (do *Domain) GetSnapshotInfoSchema(snapshotTS uint64) (infoschema.InfoSchem return is, nil } is, _, _, _, err := do.loadInfoSchema(snapshotTS) + infoschema_metrics.LoadSchemaCounterSnapshot.Inc() return is, err } @@ -496,7 +504,6 @@ func (do *Domain) Reload() error { } is, hitCache, oldSchemaVersion, changes, err := do.loadInfoSchema(ver.Ver) - metrics.LoadSchemaDuration.Observe(time.Since(startTime).Seconds()) if err != nil { metrics.LoadSchemaCounter.WithLabelValues("failed").Inc() return err diff --git a/infoschema/metrics/metrics.go b/infoschema/metrics/metrics.go new file mode 100644 index 0000000000000..6e21b8de198e3 --- /dev/null +++ b/infoschema/metrics/metrics.go @@ -0,0 +1,58 @@ +// Copyright 2023 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/pingcap/tidb/metrics" + "github.com/prometheus/client_golang/prometheus" +) + +// infoschema metrics vars +var ( + GetLatestCounter prometheus.Counter + GetTSCounter prometheus.Counter + GetVersionCounter prometheus.Counter + + HitLatestCounter prometheus.Counter + HitTSCounter prometheus.Counter + HitVersionCounter prometheus.Counter + + LoadSchemaCounterSnapshot prometheus.Counter + + LoadSchemaDurationTotal prometheus.Observer + LoadSchemaDurationLoadDiff prometheus.Observer + LoadSchemaDurationLoadAll prometheus.Observer +) + +func init() { + InitMetricsVars() +} + +// InitMetricsVars init infoschema metrics vars. +func InitMetricsVars() { + GetLatestCounter = metrics.InfoCacheCounters.WithLabelValues("get", "latest") + GetTSCounter = metrics.InfoCacheCounters.WithLabelValues("get", "ts") + GetVersionCounter = metrics.InfoCacheCounters.WithLabelValues("get", "version") + + HitLatestCounter = metrics.InfoCacheCounters.WithLabelValues("hit", "latest") + HitTSCounter = metrics.InfoCacheCounters.WithLabelValues("hit", "ts") + HitVersionCounter = metrics.InfoCacheCounters.WithLabelValues("hit", "version") + + LoadSchemaCounterSnapshot = metrics.LoadSchemaCounter.WithLabelValues("snapshot") + + LoadSchemaDurationTotal = metrics.LoadSchemaDuration.WithLabelValues("total") + LoadSchemaDurationLoadDiff = metrics.LoadSchemaDuration.WithLabelValues("load-diff") + LoadSchemaDurationLoadAll = metrics.LoadSchemaDuration.WithLabelValues("load-all") +} diff --git a/metrics/domain.go b/metrics/domain.go index ab6d9df5ae0d8..a76062a811991 100644 --- a/metrics/domain.go +++ b/metrics/domain.go @@ -30,14 +30,14 @@ var ( }, []string{LblType}) // LoadSchemaDuration records the duration of load schema. - LoadSchemaDuration = prometheus.NewHistogram( + LoadSchemaDuration = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Namespace: "tidb", Subsystem: "domain", Name: "load_schema_duration_seconds", Help: "Bucketed histogram of processing time (s) in load schema.", Buckets: prometheus.ExponentialBuckets(0.001, 2, 20), // 1ms ~ 524s - }) + }, []string{LblAction}) // InfoCacheCounters are the counters of get/hit. InfoCacheCounters = prometheus.NewCounterVec( diff --git a/metrics/grafana/tidb.json b/metrics/grafana/tidb.json index 9e2f362c74c84..6ee35f83bbb85 100644 --- a/metrics/grafana/tidb.json +++ b/metrics/grafana/tidb.json @@ -11670,13 +11670,15 @@ "legend": { "alignAsTable": true, "avg": false, - "current": false, - "max": false, + "current": true, + "max": true, "min": false, "rightSide": true, "show": true, + "sort": "current", + "sortDesc": true, "total": false, - "values": false + "values": true }, "lines": true, "linewidth": 1, @@ -11696,10 +11698,10 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(0.99, sum(rate(tidb_domain_load_schema_duration_seconds_bucket{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\"}[1m])) by (le, instance))", + "expr": "histogram_quantile(0.99, sum(rate(tidb_domain_load_schema_duration_seconds_bucket{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\"}[1m])) by (le, action))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "{{instance}}", + "legendFormat": "{{action}}", "metric": "", "refId": "A", "step": 10 @@ -11709,7 +11711,7 @@ "timeFrom": null, "timeRegions": [], "timeShift": null, - "title": "Load Schema Duration", + "title": "Load Schema Action Duration", "tooltip": { "msResolution": false, "shared": true, @@ -11881,13 +11883,15 @@ "legend": { "alignAsTable": true, "avg": false, - "current": false, - "max": false, + "current": true, + "max": true, "min": false, "rightSide": true, "show": true, + "sort": "current", + "sortDesc": true, "total": false, - "values": false + "values": true }, "lines": true, "linewidth": 1, @@ -11944,7 +11948,7 @@ { "format": "short", "label": null, - "logBase": 10, + "logBase": 1, "max": null, "min": null, "show": true @@ -12075,6 +12079,114 @@ "align": false, "alignLevel": null } + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_TEST-CLUSTER}", + "description": "TiDB schema cache operations per second.", + "editable": true, + "error": false, + "fieldConfig": { + "defaults": {}, + "overrides": [] + }, + "fill": 0, + "fillGradient": 0, + "grid": {}, + "gridPos": { + "h": 7, + "w": 12, + "x": 12, + "y": 24 + }, + "hiddenSeries": false, + "id": 314, + "legend": { + "alignAsTable": true, + "avg": false, + "current": true, + "max": true, + "min": false, + "rightSide": true, + "show": true, + "sort": "current", + "sortDesc": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null as zero", + "options": { + "alertThreshold": true + }, + "percentage": false, + "pluginVersion": "7.5.11", + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "exemplar": true, + "expr": "sum(rate(tidb_domain_infocache_counters{k8s_cluster=\"$k8s_cluster\", tidb_cluster=\"$tidb_cluster\", instance=~\"$instance\"}[1m])) by (action,type)", + "format": "time_series", + "interval": "", + "intervalFactor": 2, + "legendFormat": "{{action}}-{{type}}", + "metric": "", + "refId": "A", + "step": 10 + } + ], + "thresholds": [], + "timeFrom": null, + "timeRegions": [], + "timeShift": null, + "title": "Schema Cache OPS", + "tooltip": { + "msResolution": false, + "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": true + } + ], + "yaxis": { + "align": false, + "alignLevel": null + } } ], "repeat": null, From 2b0af39ef16606a1f259bfcf16f55cb6b1f80a01 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 5 Sep 2023 16:50:21 +0800 Subject: [PATCH 2/6] refien Signed-off-by: crazycs520 --- domain/domain.go | 16 +++++++--- infoschema/metrics/metrics.go | 58 ----------------------------------- 2 files changed, 11 insertions(+), 63 deletions(-) delete mode 100644 infoschema/metrics/metrics.go diff --git a/domain/domain.go b/domain/domain.go index cca23e1d3fdc1..7dbb12c611026 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -44,7 +44,6 @@ import ( "github.com/pingcap/tidb/domain/infosync" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/infoschema" - infoschema_metrics "github.com/pingcap/tidb/infoschema/metrics" "github.com/pingcap/tidb/infoschema/perfschema" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" @@ -165,6 +164,13 @@ func (do *Domain) EtcdClient() *clientv3.Client { return do.etcdClient } +var ( + loadSchemaCounterSnapshot = metrics.LoadSchemaCounter.WithLabelValues("snapshot") + loadSchemaDurationTotal = metrics.LoadSchemaDuration.WithLabelValues("total") + loadSchemaDurationLoadDiff = metrics.LoadSchemaDuration.WithLabelValues("load-diff") + loadSchemaDurationLoadAll = metrics.LoadSchemaDuration.WithLabelValues("load-all") +) + // loadInfoSchema loads infoschema at startTS. // It returns: // 1. the needed infoschema @@ -175,7 +181,7 @@ func (do *Domain) EtcdClient() *clientv3.Client { func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, int64, *transaction.RelatedSchemaChange, error) { beginTime := time.Now() defer func() { - infoschema_metrics.LoadSchemaDurationTotal.Observe(time.Since(beginTime).Seconds()) + loadSchemaDurationTotal.Observe(time.Since(beginTime).Seconds()) }() snapshot := do.store.GetSnapshot(kv.NewVersion(startTS)) m := meta.NewSnapshotMeta(snapshot) @@ -212,7 +218,7 @@ func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, i if currentSchemaVersion != 0 && neededSchemaVersion > currentSchemaVersion && neededSchemaVersion-currentSchemaVersion < 100 { is, relatedChanges, err := do.tryLoadSchemaDiffs(m, currentSchemaVersion, neededSchemaVersion) if err == nil { - infoschema_metrics.LoadSchemaDurationLoadDiff.Observe(time.Since(startTime).Seconds()) + loadSchemaDurationLoadDiff.Observe(time.Since(startTime).Seconds()) do.infoCache.Insert(is, uint64(schemaTs)) logutil.BgLogger().Info("diff load InfoSchema success", zap.Int64("currentSchemaVersion", currentSchemaVersion), @@ -240,7 +246,7 @@ func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, i if err != nil { return nil, false, currentSchemaVersion, nil, err } - infoschema_metrics.LoadSchemaDurationLoadAll.Observe(time.Since(startTime).Seconds()) + loadSchemaDurationLoadAll.Observe(time.Since(startTime).Seconds()) logutil.BgLogger().Info("full load InfoSchema success", zap.Int64("currentSchemaVersion", currentSchemaVersion), zap.Int64("neededSchemaVersion", neededSchemaVersion), @@ -422,7 +428,7 @@ func (do *Domain) GetSnapshotInfoSchema(snapshotTS uint64) (infoschema.InfoSchem return is, nil } is, _, _, _, err := do.loadInfoSchema(snapshotTS) - infoschema_metrics.LoadSchemaCounterSnapshot.Inc() + loadSchemaCounterSnapshot.Inc() return is, err } diff --git a/infoschema/metrics/metrics.go b/infoschema/metrics/metrics.go deleted file mode 100644 index 6e21b8de198e3..0000000000000 --- a/infoschema/metrics/metrics.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2023 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/pingcap/tidb/metrics" - "github.com/prometheus/client_golang/prometheus" -) - -// infoschema metrics vars -var ( - GetLatestCounter prometheus.Counter - GetTSCounter prometheus.Counter - GetVersionCounter prometheus.Counter - - HitLatestCounter prometheus.Counter - HitTSCounter prometheus.Counter - HitVersionCounter prometheus.Counter - - LoadSchemaCounterSnapshot prometheus.Counter - - LoadSchemaDurationTotal prometheus.Observer - LoadSchemaDurationLoadDiff prometheus.Observer - LoadSchemaDurationLoadAll prometheus.Observer -) - -func init() { - InitMetricsVars() -} - -// InitMetricsVars init infoschema metrics vars. -func InitMetricsVars() { - GetLatestCounter = metrics.InfoCacheCounters.WithLabelValues("get", "latest") - GetTSCounter = metrics.InfoCacheCounters.WithLabelValues("get", "ts") - GetVersionCounter = metrics.InfoCacheCounters.WithLabelValues("get", "version") - - HitLatestCounter = metrics.InfoCacheCounters.WithLabelValues("hit", "latest") - HitTSCounter = metrics.InfoCacheCounters.WithLabelValues("hit", "ts") - HitVersionCounter = metrics.InfoCacheCounters.WithLabelValues("hit", "version") - - LoadSchemaCounterSnapshot = metrics.LoadSchemaCounter.WithLabelValues("snapshot") - - LoadSchemaDurationTotal = metrics.LoadSchemaDuration.WithLabelValues("total") - LoadSchemaDurationLoadDiff = metrics.LoadSchemaDuration.WithLabelValues("load-diff") - LoadSchemaDurationLoadAll = metrics.LoadSchemaDuration.WithLabelValues("load-all") -} From a5ec8b57a6e5fd5c7f5bf943fc140f4c643f08d2 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 5 Sep 2023 16:50:49 +0800 Subject: [PATCH 3/6] refine Signed-off-by: crazycs520 --- domain/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/domain/BUILD.bazel b/domain/BUILD.bazel index 604c703571831..bc0166c7f2d00 100644 --- a/domain/BUILD.bazel +++ b/domain/BUILD.bazel @@ -31,7 +31,6 @@ go_library( "//domain/infosync", "//errno", "//infoschema", - "//infoschema/metrics", "//infoschema/perfschema", "//kv", "//meta", From 3fe29f877de5126d8c1793e7c4c89039824dcc0a Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 5 Sep 2023 16:56:56 +0800 Subject: [PATCH 4/6] add global variable tidb_info_schema_cache_size to control infoschema cache size Signed-off-by: crazycs520 --- domain/domain.go | 2 +- domain/sysvar_cache.go | 1 + executor/set_test.go | 17 ++++++++++++++ infoschema/cache.go | 24 +++++++++++++++++++ infoschema/cache_test.go | 34 +++++++++++++++++++++++++++ infoschema/test/cachetest/BUILD.bazel | 18 ++++++++++++++ sessionctx/variable/sysvar.go | 5 ++++ sessionctx/variable/tidb_vars.go | 4 ++++ 8 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 infoschema/test/cachetest/BUILD.bazel diff --git a/domain/domain.go b/domain/domain.go index 7dbb12c611026..c5c81768c9f92 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -939,7 +939,7 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio exit: make(chan struct{}), sysSessionPool: newSessionPool(capacity, factory), statsLease: statsLease, - infoCache: infoschema.NewCache(16), + infoCache: infoschema.NewCache(int(variable.InfoSchemaCacheSize.Load())), slowQuery: newTopNSlowQueries(30, time.Hour*24*7, 500), indexUsageSyncLease: idxUsageSyncLease, dumpFileGcChecker: &dumpFileGcChecker{gcLease: dumpFileGcLease, paths: []string{GetPlanReplayerDirName(), GetOptimizerTraceDirName()}}, diff --git a/domain/sysvar_cache.go b/domain/sysvar_cache.go index 370260a67c02a..32ba4f0d93a58 100644 --- a/domain/sysvar_cache.go +++ b/domain/sysvar_cache.go @@ -158,5 +158,6 @@ func (do *Domain) rebuildSysVarCache(ctx sessionctx.Context) error { defer do.sysVarCache.Unlock() do.sysVarCache.session = newSessionCache do.sysVarCache.global = newGlobalCache + do.infoCache.ReSize(int(variable.InfoSchemaCacheSize.Load())) return nil } diff --git a/executor/set_test.go b/executor/set_test.go index 734fdab8750fe..bf3e8f5cc9252 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -870,6 +870,23 @@ func TestSetVar(t *testing.T) { require.Equal(t, uint64(2), tk.Session().GetSessionVars().CDCWriteSource) tk.MustExec("set @@session.tidb_cdc_write_source = 0") require.Equal(t, uint64(0), tk.Session().GetSessionVars().CDCWriteSource) + + // test tidb_info_schema_cache_size + tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("16")) + tk.MustExec("set @@global.tidb_info_schema_cache_size=64;") + tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("64")) + tk.MustExec("set @@global.tidb_info_schema_cache_size=2;") + tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("2")) + tk.MustExec("set @@global.tidb_info_schema_cache_size=256;") + tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_info_schema_cache_size value: '256'")) + tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("255")) + tk.MustExec("set @@global.tidb_info_schema_cache_size=0;") + tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_info_schema_cache_size value: '0'")) + tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("2")) + tk.MustGetErrMsg("set @@global.tidb_info_schema_cache_size='x';", "[variable:1232]Incorrect argument type to variable 'tidb_info_schema_cache_size'") + tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("2")) + tk.MustExec("set @@global.tidb_info_schema_cache_size=64;") + tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("64")) } func TestGetSetNoopVars(t *testing.T) { diff --git a/infoschema/cache.go b/infoschema/cache.go index eb9fbc6c4857b..8446f240431d7 100644 --- a/infoschema/cache.go +++ b/infoschema/cache.go @@ -54,6 +54,30 @@ func NewCache(capacity int) *InfoCache { } } +// ReSize re-size the cache. +func (h *InfoCache) ReSize(capacity int) { + h.mu.Lock() + defer h.mu.Unlock() + if cap(h.cache) == capacity { + return + } + oldCache := h.cache + h.cache = make([]schemaAndTimestamp, 0, capacity) + for i, v := range oldCache { + if i >= capacity { + break + } + h.cache = append(h.cache, v) + } +} + +// Size returns the size of the cache, export for test. +func (h *InfoCache) Size() int { + h.mu.Lock() + defer h.mu.Unlock() + return len(h.cache) +} + // Reset resets the cache. func (h *InfoCache) Reset(capacity int) { h.mu.Lock() diff --git a/infoschema/cache_test.go b/infoschema/cache_test.go index 60aad8bbdd985..337ede36f5b9d 100644 --- a/infoschema/cache_test.go +++ b/infoschema/cache_test.go @@ -177,3 +177,37 @@ func TestGetByTimestamp(t *testing.T) { require.Equal(t, is3, ic.GetBySnapshotTS(3)) require.Equal(t, 3, ic.Len()) } + +func TestReSize(t *testing.T) { + ic := infoschema.NewCache(2) + require.NotNil(t, ic) + is1 := infoschema.MockInfoSchemaWithSchemaVer(nil, 1) + ic.Insert(is1, 1) + is2 := infoschema.MockInfoSchemaWithSchemaVer(nil, 2) + ic.Insert(is2, 2) + + ic.ReSize(3) + require.Equal(t, 2, ic.Size()) + require.Equal(t, is1, ic.GetByVersion(1)) + require.Equal(t, is2, ic.GetByVersion(2)) + is3 := infoschema.MockInfoSchemaWithSchemaVer(nil, 3) + require.True(t, ic.Insert(is3, 3)) + require.Equal(t, is1, ic.GetByVersion(1)) + require.Equal(t, is2, ic.GetByVersion(2)) + require.Equal(t, is3, ic.GetByVersion(3)) + + ic.ReSize(1) + require.Equal(t, 1, ic.Size()) + require.Nil(t, ic.GetByVersion(1)) + require.Nil(t, ic.GetByVersion(2)) + require.Equal(t, is3, ic.GetByVersion(3)) + require.False(t, ic.Insert(is2, 2)) + require.Equal(t, 1, ic.Size()) + is4 := infoschema.MockInfoSchemaWithSchemaVer(nil, 4) + require.True(t, ic.Insert(is4, 4)) + require.Equal(t, 1, ic.Size()) + require.Nil(t, ic.GetByVersion(1)) + require.Nil(t, ic.GetByVersion(2)) + require.Nil(t, ic.GetByVersion(3)) + require.Equal(t, is4, ic.GetByVersion(4)) +} diff --git a/infoschema/test/cachetest/BUILD.bazel b/infoschema/test/cachetest/BUILD.bazel new file mode 100644 index 0000000000000..4cd2112cd6b95 --- /dev/null +++ b/infoschema/test/cachetest/BUILD.bazel @@ -0,0 +1,18 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") + +go_test( + name = "cachetest_test", + timeout = "short", + srcs = [ + "cache_test.go", + "main_test.go", + ], + flaky = True, + shard_count = 6, + deps = [ + "//infoschema", + "//testkit/testsetup", + "@com_github_stretchr_testify//require", + "@org_uber_go_goleak//:goleak", + ], +) diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index b45c13e90ef45..9e5a8e3e52a3e 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -2297,6 +2297,11 @@ var defaultSysVars = []*SysVar{ return nil }, }, + {Scope: ScopeGlobal, Name: TiDBInfoSchemaCacheSize, Value: strconv.Itoa(DefTiDBInfoSchemaCacheSize), Type: TypeInt, MinValue: 2, MaxValue: math.MaxUint8, AllowEmpty: true, + SetGlobal: func(_ context.Context, s *SessionVars, val string) error { + InfoSchemaCacheSize.Store(TidbOptInt64(val, DefTiDBInfoSchemaCacheSize)) + return nil + }}, } // FeedbackProbability points to the FeedbackProbability in statistics package. diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 086f32a685839..8a43b0104c79b 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -907,6 +907,8 @@ const ( PasswordReuseHistory = "password_history" // PasswordReuseTime limit how long passwords can be reused. PasswordReuseTime = "password_reuse_interval" + // TiDBInfoSchemaCacheSize defines the capacity size of domain infoSchema cache. + TiDBInfoSchemaCacheSize = "tidb_info_schema_cache_size" ) // TiDB intentional limits @@ -1167,6 +1169,7 @@ const ( DefTiDBTTLJobScheduleWindowEndTime = "23:59 +0000" DefTiDBTTLScanWorkerCount = 4 DefTiDBTTLDeleteWorkerCount = 4 + DefTiDBInfoSchemaCacheSize = 64 ) // Process global variables. @@ -1242,6 +1245,7 @@ var ( PasswordReuseInterval = atomic.NewInt64(DefPasswordReuseTime) IsSandBoxModeEnabled = atomic.NewBool(false) MaxPreparedStmtCountValue = atomic.NewInt64(DefMaxPreparedStmtCount) + InfoSchemaCacheSize = atomic.NewInt64(DefTiDBInfoSchemaCacheSize) ) var ( From f7d8eee549f48352bf7c1f662cc379b332fa387a Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 5 Sep 2023 17:40:31 +0800 Subject: [PATCH 5/6] refine Signed-off-by: crazycs520 --- infoschema/test/cachetest/BUILD.bazel | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 infoschema/test/cachetest/BUILD.bazel diff --git a/infoschema/test/cachetest/BUILD.bazel b/infoschema/test/cachetest/BUILD.bazel deleted file mode 100644 index 4cd2112cd6b95..0000000000000 --- a/infoschema/test/cachetest/BUILD.bazel +++ /dev/null @@ -1,18 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_test") - -go_test( - name = "cachetest_test", - timeout = "short", - srcs = [ - "cache_test.go", - "main_test.go", - ], - flaky = True, - shard_count = 6, - deps = [ - "//infoschema", - "//testkit/testsetup", - "@com_github_stretchr_testify//require", - "@org_uber_go_goleak//:goleak", - ], -) From 80ce5f664392ed66bdc890639da20672d7ac5c91 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 6 Sep 2023 22:17:37 +0800 Subject: [PATCH 6/6] rename to tidb_schema_version_cache_limit Signed-off-by: crazycs520 --- domain/domain.go | 2 +- domain/sysvar_cache.go | 2 +- executor/set_test.go | 32 ++++++++++++++++---------------- sessionctx/variable/sysvar.go | 4 ++-- sessionctx/variable/tidb_vars.go | 8 ++++---- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/domain/domain.go b/domain/domain.go index c5c81768c9f92..969b68a5b0567 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -939,7 +939,7 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio exit: make(chan struct{}), sysSessionPool: newSessionPool(capacity, factory), statsLease: statsLease, - infoCache: infoschema.NewCache(int(variable.InfoSchemaCacheSize.Load())), + infoCache: infoschema.NewCache(int(variable.SchemaVersionCacheLimit.Load())), slowQuery: newTopNSlowQueries(30, time.Hour*24*7, 500), indexUsageSyncLease: idxUsageSyncLease, dumpFileGcChecker: &dumpFileGcChecker{gcLease: dumpFileGcLease, paths: []string{GetPlanReplayerDirName(), GetOptimizerTraceDirName()}}, diff --git a/domain/sysvar_cache.go b/domain/sysvar_cache.go index 32ba4f0d93a58..9c50c779fb461 100644 --- a/domain/sysvar_cache.go +++ b/domain/sysvar_cache.go @@ -158,6 +158,6 @@ func (do *Domain) rebuildSysVarCache(ctx sessionctx.Context) error { defer do.sysVarCache.Unlock() do.sysVarCache.session = newSessionCache do.sysVarCache.global = newGlobalCache - do.infoCache.ReSize(int(variable.InfoSchemaCacheSize.Load())) + do.infoCache.ReSize(int(variable.SchemaVersionCacheLimit.Load())) return nil } diff --git a/executor/set_test.go b/executor/set_test.go index bf3e8f5cc9252..850042f6dc431 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -871,22 +871,22 @@ func TestSetVar(t *testing.T) { tk.MustExec("set @@session.tidb_cdc_write_source = 0") require.Equal(t, uint64(0), tk.Session().GetSessionVars().CDCWriteSource) - // test tidb_info_schema_cache_size - tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("16")) - tk.MustExec("set @@global.tidb_info_schema_cache_size=64;") - tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("64")) - tk.MustExec("set @@global.tidb_info_schema_cache_size=2;") - tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("2")) - tk.MustExec("set @@global.tidb_info_schema_cache_size=256;") - tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_info_schema_cache_size value: '256'")) - tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("255")) - tk.MustExec("set @@global.tidb_info_schema_cache_size=0;") - tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_info_schema_cache_size value: '0'")) - tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("2")) - tk.MustGetErrMsg("set @@global.tidb_info_schema_cache_size='x';", "[variable:1232]Incorrect argument type to variable 'tidb_info_schema_cache_size'") - tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("2")) - tk.MustExec("set @@global.tidb_info_schema_cache_size=64;") - tk.MustQuery("select @@global.tidb_info_schema_cache_size").Check(testkit.Rows("64")) + // test tidb_schema_version_cache_limit + tk.MustQuery("select @@global.tidb_schema_version_cache_limit").Check(testkit.Rows("64")) + tk.MustExec("set @@global.tidb_schema_version_cache_limit=64;") + tk.MustQuery("select @@global.tidb_schema_version_cache_limit").Check(testkit.Rows("64")) + tk.MustExec("set @@global.tidb_schema_version_cache_limit=2;") + tk.MustQuery("select @@global.tidb_schema_version_cache_limit").Check(testkit.Rows("2")) + tk.MustExec("set @@global.tidb_schema_version_cache_limit=256;") + tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_schema_version_cache_limit value: '256'")) + tk.MustQuery("select @@global.tidb_schema_version_cache_limit").Check(testkit.Rows("255")) + tk.MustExec("set @@global.tidb_schema_version_cache_limit=0;") + tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_schema_version_cache_limit value: '0'")) + tk.MustQuery("select @@global.tidb_schema_version_cache_limit").Check(testkit.Rows("2")) + tk.MustGetErrMsg("set @@global.tidb_schema_version_cache_limit='x';", "[variable:1232]Incorrect argument type to variable 'tidb_schema_version_cache_limit'") + tk.MustQuery("select @@global.tidb_schema_version_cache_limit").Check(testkit.Rows("2")) + tk.MustExec("set @@global.tidb_schema_version_cache_limit=64;") + tk.MustQuery("select @@global.tidb_schema_version_cache_limit").Check(testkit.Rows("64")) } func TestGetSetNoopVars(t *testing.T) { diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 9e5a8e3e52a3e..f0d3d56eab09b 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -2297,9 +2297,9 @@ var defaultSysVars = []*SysVar{ return nil }, }, - {Scope: ScopeGlobal, Name: TiDBInfoSchemaCacheSize, Value: strconv.Itoa(DefTiDBInfoSchemaCacheSize), Type: TypeInt, MinValue: 2, MaxValue: math.MaxUint8, AllowEmpty: true, + {Scope: ScopeGlobal, Name: TiDBSchemaVersionCacheLimit, Value: strconv.Itoa(DefTiDBSchemaVersionCacheLimit), Type: TypeInt, MinValue: 2, MaxValue: math.MaxUint8, AllowEmpty: true, SetGlobal: func(_ context.Context, s *SessionVars, val string) error { - InfoSchemaCacheSize.Store(TidbOptInt64(val, DefTiDBInfoSchemaCacheSize)) + SchemaVersionCacheLimit.Store(TidbOptInt64(val, DefTiDBSchemaVersionCacheLimit)) return nil }}, } diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 8a43b0104c79b..9daa390456a03 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -907,8 +907,8 @@ const ( PasswordReuseHistory = "password_history" // PasswordReuseTime limit how long passwords can be reused. PasswordReuseTime = "password_reuse_interval" - // TiDBInfoSchemaCacheSize defines the capacity size of domain infoSchema cache. - TiDBInfoSchemaCacheSize = "tidb_info_schema_cache_size" + // TiDBSchemaVersionCacheLimit defines the capacity size of domain infoSchema cache. + TiDBSchemaVersionCacheLimit = "tidb_schema_version_cache_limit" ) // TiDB intentional limits @@ -1169,7 +1169,7 @@ const ( DefTiDBTTLJobScheduleWindowEndTime = "23:59 +0000" DefTiDBTTLScanWorkerCount = 4 DefTiDBTTLDeleteWorkerCount = 4 - DefTiDBInfoSchemaCacheSize = 64 + DefTiDBSchemaVersionCacheLimit = 64 ) // Process global variables. @@ -1245,7 +1245,7 @@ var ( PasswordReuseInterval = atomic.NewInt64(DefPasswordReuseTime) IsSandBoxModeEnabled = atomic.NewBool(false) MaxPreparedStmtCountValue = atomic.NewInt64(DefMaxPreparedStmtCount) - InfoSchemaCacheSize = atomic.NewInt64(DefTiDBInfoSchemaCacheSize) + SchemaVersionCacheLimit = atomic.NewInt64(DefTiDBSchemaVersionCacheLimit) ) var (