From 3491770489fe8c725c72a5f74cdbf3c4b761748d Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 6 Dec 2022 11:27:11 +0800 Subject: [PATCH 1/5] resourcemanger: create cpu/memory monitor Signed-off-by: Weizhen Wang --- br/pkg/lightning/common/BUILD.bazel | 44 ++++++++++++- br/pkg/lightning/web/BUILD.bazel | 1 - build/nogo_config.json | 6 +- domain/domain.go | 2 - resourcemanager/BUILD.bazel | 13 ++++ resourcemanager/rm.go | 65 +++++++++++++++++++ .../featuretag/concurrencyddl/BUILD.bazel | 5 +- .../featuretag/distributereorg/BUILD.bazel | 5 +- tidb-server/BUILD.bazel | 1 + tidb-server/main.go | 3 + util/signal/BUILD.bazel | 31 +++++++-- util/sys/linux/BUILD.bazel | 43 +++++++++++- 12 files changed, 199 insertions(+), 20 deletions(-) create mode 100644 resourcemanager/BUILD.bazel create mode 100644 resourcemanager/rm.go diff --git a/br/pkg/lightning/common/BUILD.bazel b/br/pkg/lightning/common/BUILD.bazel index a5245356b2715..880186fe1d700 100644 --- a/br/pkg/lightning/common/BUILD.bazel +++ b/br/pkg/lightning/common/BUILD.bazel @@ -34,9 +34,49 @@ go_library( "@org_golang_google_grpc//codes", "@org_golang_google_grpc//credentials", "@org_golang_google_grpc//status", - "@org_golang_x_sys//unix", "@org_uber_go_zap//:zap", - ], + ] + select({ + "@io_bazel_rules_go//go/platform:aix": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:android": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:darwin": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:dragonfly": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:freebsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:illumos": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:ios": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:js": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:netbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:openbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:plan9": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:solaris": [ + "@org_golang_x_sys//unix", + ], + "//conditions:default": [], + }), ) go_test( diff --git a/br/pkg/lightning/web/BUILD.bazel b/br/pkg/lightning/web/BUILD.bazel index 842eb48fb3dd3..30dbd2437697b 100644 --- a/br/pkg/lightning/web/BUILD.bazel +++ b/br/pkg/lightning/web/BUILD.bazel @@ -4,7 +4,6 @@ go_library( name = "web", srcs = [ "progress.go", - "res.go", "res_vfsdata.go", ], importpath = "github.com/pingcap/tidb/br/pkg/lightning/web", diff --git a/build/nogo_config.json b/build/nogo_config.json index ce3715232b472..3aecc81b0519e 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -404,7 +404,8 @@ "util/": "util code", "parser/": "parser code", "meta/": "parser code", - "extension/": "extension code" + "extension/": "extension code", + "resourcemanager/": "resourcemanager code" } }, "shift": { @@ -765,7 +766,8 @@ "server/conn.go": "server/conn.go", "server/conn_stmt.go": "server/conn_stmt.go", "server/conn_test.go": "server/conn_test.go", - "extension/": "extension code" + "extension/": "extension code", + "resourcemanager/": "resourcemanager code" } }, "SA2000": { diff --git a/domain/domain.go b/domain/domain.go index 82963ed0cebdc..aafc366a47799 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -2016,7 +2016,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) loadFeedbackTicker := time.NewTicker(5 * lease) loadLockedTablesTicker := time.NewTicker(5 * lease) dumpColStatsUsageTicker := time.NewTicker(100 * lease) - readMemTricker := time.NewTicker(memory.ReadMemInterval) statsHandle := do.StatsHandle() defer func() { dumpColStatsUsageTicker.Stop() @@ -2024,7 +2023,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) dumpFeedbackTicker.Stop() gcStatsTicker.Stop() deltaUpdateTicker.Stop() - readMemTricker.Stop() do.SetStatsUpdating(false) logutil.BgLogger().Info("updateStatsWorker exited.") }() diff --git a/resourcemanager/BUILD.bazel b/resourcemanager/BUILD.bazel new file mode 100644 index 0000000000000..aceb236e5a9d0 --- /dev/null +++ b/resourcemanager/BUILD.bazel @@ -0,0 +1,13 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "resourcemanage", + srcs = ["rm.go"], + importpath = "github.com/pingcap/tidb/resourcemanager", + visibility = ["//visibility:public"], + deps = [ + "//util", + "//util/cpu", + "//util/memory", + ], +) diff --git a/resourcemanager/rm.go b/resourcemanager/rm.go new file mode 100644 index 0000000000000..41cac9cba85e7 --- /dev/null +++ b/resourcemanager/rm.go @@ -0,0 +1,65 @@ +// Copyright 2022 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 resourcemanager + +import ( + "time" + + tidbutil "github.com/pingcap/tidb/util" + "github.com/pingcap/tidb/util/cpu" + "github.com/pingcap/tidb/util/memory" +) + +// GlobalResourceManager is a global resource manager +var GlobalResourceManager = NewResourceManger() + +// ResourceManager is a resource manager +type ResourceManager struct { + cpuObserver *cpu.Observer + exitCh chan struct{} + wg tidbutil.WaitGroupWrapper +} + +// NewResourceManger is to create a new resource manager +func NewResourceManger() *ResourceManager { + return &ResourceManager{ + cpuObserver: cpu.NewCPUObserver(), + exitCh: make(chan struct{}), + } +} + +// Start is to start resource manager +func (r *ResourceManager) Start() { + r.wg.Run(r.cpuObserver.Start) + r.wg.Run(func() { + readMemTricker := time.NewTicker(memory.ReadMemInterval) + defer readMemTricker.Stop() + for { + select { + case <-readMemTricker.C: + memory.ForceReadMemStats() + case <-r.exitCh: + return + } + } + }) +} + +// Stop is to stop resource manager +func (r *ResourceManager) Stop() { + r.cpuObserver.Stop() + close(r.exitCh) + r.wg.Done() +} diff --git a/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel b/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel index 44c1cede3c2b7..37ac17a1804ec 100644 --- a/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel +++ b/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel @@ -2,10 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "concurrencyddl", - srcs = [ - "default.go", - "non_default.go", - ], + srcs = ["default.go"], importpath = "github.com/pingcap/tidb/sessionctx/variable/featuretag/concurrencyddl", visibility = ["//visibility:public"], ) diff --git a/sessionctx/variable/featuretag/distributereorg/BUILD.bazel b/sessionctx/variable/featuretag/distributereorg/BUILD.bazel index 153ce052ecbb2..fb3bfb4835c1a 100644 --- a/sessionctx/variable/featuretag/distributereorg/BUILD.bazel +++ b/sessionctx/variable/featuretag/distributereorg/BUILD.bazel @@ -2,10 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "distributereorg", - srcs = [ - "default.go", - "non_default.go", - ], + srcs = ["default.go"], importpath = "github.com/pingcap/tidb/sessionctx/variable/featuretag/distributereorg", visibility = ["//visibility:public"], ) diff --git a/tidb-server/BUILD.bazel b/tidb-server/BUILD.bazel index 960f68a2b597d..493eedfcccbfa 100644 --- a/tidb-server/BUILD.bazel +++ b/tidb-server/BUILD.bazel @@ -21,6 +21,7 @@ go_library( "//planner/core", "//plugin", "//privilege/privileges", + "//resourcemanager:resourcemanage", "//server", "//session", "//session/txninfo", diff --git a/tidb-server/main.go b/tidb-server/main.go index a15d102f9509e..d58e24a531f4c 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -46,6 +46,7 @@ import ( plannercore "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/plugin" "github.com/pingcap/tidb/privilege/privileges" + "github.com/pingcap/tidb/resourcemanager" "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/session/txninfo" @@ -210,6 +211,7 @@ func main() { printInfo() setupBinlogClient() setupMetrics() + resourcemanager.GlobalResourceManager.Start() storage, dom := createStoreAndDomain() svr := createServer(storage, dom) @@ -223,6 +225,7 @@ func main() { svr.Close() cleanup(svr, storage, dom, graceful) cpuprofile.StopCPUProfiler() + resourcemanager.GlobalResourceManager.Stop() close(exited) }) topsql.SetupTopSQL() diff --git a/util/signal/BUILD.bazel b/util/signal/BUILD.bazel index af19da427497c..e2963d6f554bb 100644 --- a/util/signal/BUILD.bazel +++ b/util/signal/BUILD.bazel @@ -9,8 +9,31 @@ go_library( ], importpath = "github.com/pingcap/tidb/util/signal", visibility = ["//visibility:public"], - deps = [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], + deps = select({ + "@io_bazel_rules_go//go/platform:android": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:darwin": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:freebsd": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:ios": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:windows": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "//conditions:default": [], + }), ) diff --git a/util/sys/linux/BUILD.bazel b/util/sys/linux/BUILD.bazel index 1212afe2fc3d9..f1363c2cb0d71 100644 --- a/util/sys/linux/BUILD.bazel +++ b/util/sys/linux/BUILD.bazel @@ -9,7 +9,48 @@ go_library( ], importpath = "github.com/pingcap/tidb/util/sys/linux", visibility = ["//visibility:public"], - deps = ["@org_golang_x_sys//unix"], + deps = select({ + "@io_bazel_rules_go//go/platform:aix": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:android": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:darwin": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:dragonfly": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:freebsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:illumos": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:ios": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:js": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:netbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:openbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:plan9": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:solaris": [ + "@org_golang_x_sys//unix", + ], + "//conditions:default": [], + }), ) go_test( From ea30120ee17574a68c8cf305e4109b8600f044c3 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 15 Dec 2022 15:00:26 +0800 Subject: [PATCH 2/5] resourcemanger: create cpu/memory monitor Signed-off-by: Weizhen Wang --- br/pkg/lightning/common/BUILD.bazel | 44 +------------------ br/pkg/lightning/web/BUILD.bazel | 1 + domain/domain.go | 4 -- .../featuretag/concurrencyddl/BUILD.bazel | 5 ++- .../featuretag/distributereorg/BUILD.bazel | 5 ++- util/signal/BUILD.bazel | 31 ++----------- util/sys/linux/BUILD.bazel | 43 +----------------- 7 files changed, 16 insertions(+), 117 deletions(-) diff --git a/br/pkg/lightning/common/BUILD.bazel b/br/pkg/lightning/common/BUILD.bazel index 880186fe1d700..a5245356b2715 100644 --- a/br/pkg/lightning/common/BUILD.bazel +++ b/br/pkg/lightning/common/BUILD.bazel @@ -34,49 +34,9 @@ go_library( "@org_golang_google_grpc//codes", "@org_golang_google_grpc//credentials", "@org_golang_google_grpc//status", + "@org_golang_x_sys//unix", "@org_uber_go_zap//:zap", - ] + select({ - "@io_bazel_rules_go//go/platform:aix": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:android": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:darwin": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:dragonfly": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:freebsd": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:illumos": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:ios": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:js": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:linux": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:netbsd": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:openbsd": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:plan9": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:solaris": [ - "@org_golang_x_sys//unix", - ], - "//conditions:default": [], - }), + ], ) go_test( diff --git a/br/pkg/lightning/web/BUILD.bazel b/br/pkg/lightning/web/BUILD.bazel index 30dbd2437697b..842eb48fb3dd3 100644 --- a/br/pkg/lightning/web/BUILD.bazel +++ b/br/pkg/lightning/web/BUILD.bazel @@ -4,6 +4,7 @@ go_library( name = "web", srcs = [ "progress.go", + "res.go", "res_vfsdata.go", ], importpath = "github.com/pingcap/tidb/br/pkg/lightning/web", diff --git a/domain/domain.go b/domain/domain.go index aafc366a47799..f548863d4aea3 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -67,7 +67,6 @@ import ( "github.com/pingcap/tidb/util/engine" "github.com/pingcap/tidb/util/expensivequery" "github.com/pingcap/tidb/util/logutil" - "github.com/pingcap/tidb/util/memory" "github.com/pingcap/tidb/util/memoryusagealarm" "github.com/pingcap/tidb/util/servermemorylimit" "github.com/pingcap/tidb/util/sqlexec" @@ -2076,9 +2075,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) if err != nil { logutil.BgLogger().Debug("dump column stats usage failed", zap.Error(err)) } - - case <-readMemTricker.C: - memory.ForceReadMemStats() } } } diff --git a/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel b/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel index 37ac17a1804ec..44c1cede3c2b7 100644 --- a/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel +++ b/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "concurrencyddl", - srcs = ["default.go"], + srcs = [ + "default.go", + "non_default.go", + ], importpath = "github.com/pingcap/tidb/sessionctx/variable/featuretag/concurrencyddl", visibility = ["//visibility:public"], ) diff --git a/sessionctx/variable/featuretag/distributereorg/BUILD.bazel b/sessionctx/variable/featuretag/distributereorg/BUILD.bazel index fb3bfb4835c1a..153ce052ecbb2 100644 --- a/sessionctx/variable/featuretag/distributereorg/BUILD.bazel +++ b/sessionctx/variable/featuretag/distributereorg/BUILD.bazel @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "distributereorg", - srcs = ["default.go"], + srcs = [ + "default.go", + "non_default.go", + ], importpath = "github.com/pingcap/tidb/sessionctx/variable/featuretag/distributereorg", visibility = ["//visibility:public"], ) diff --git a/util/signal/BUILD.bazel b/util/signal/BUILD.bazel index e2963d6f554bb..af19da427497c 100644 --- a/util/signal/BUILD.bazel +++ b/util/signal/BUILD.bazel @@ -9,31 +9,8 @@ go_library( ], importpath = "github.com/pingcap/tidb/util/signal", visibility = ["//visibility:public"], - deps = select({ - "@io_bazel_rules_go//go/platform:android": [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], - "@io_bazel_rules_go//go/platform:darwin": [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], - "@io_bazel_rules_go//go/platform:freebsd": [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], - "@io_bazel_rules_go//go/platform:ios": [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], - "@io_bazel_rules_go//go/platform:linux": [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], - "@io_bazel_rules_go//go/platform:windows": [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], - "//conditions:default": [], - }), + deps = [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], ) diff --git a/util/sys/linux/BUILD.bazel b/util/sys/linux/BUILD.bazel index f1363c2cb0d71..1212afe2fc3d9 100644 --- a/util/sys/linux/BUILD.bazel +++ b/util/sys/linux/BUILD.bazel @@ -9,48 +9,7 @@ go_library( ], importpath = "github.com/pingcap/tidb/util/sys/linux", visibility = ["//visibility:public"], - deps = select({ - "@io_bazel_rules_go//go/platform:aix": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:android": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:darwin": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:dragonfly": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:freebsd": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:illumos": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:ios": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:js": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:linux": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:netbsd": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:openbsd": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:plan9": [ - "@org_golang_x_sys//unix", - ], - "@io_bazel_rules_go//go/platform:solaris": [ - "@org_golang_x_sys//unix", - ], - "//conditions:default": [], - }), + deps = ["@org_golang_x_sys//unix"], ) go_test( From 23a14d1692af3e5cb073eccd88d410600aa65d8d Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 15 Dec 2022 15:05:55 +0800 Subject: [PATCH 3/5] resourcemanger: create cpu/memory monitor Signed-off-by: Weizhen Wang --- domain/domain.go | 10 ++++++---- resourcemanager/rm.go | 18 ------------------ 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/domain/domain.go b/domain/domain.go index f548863d4aea3..82055bffbe8a9 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -67,6 +67,7 @@ import ( "github.com/pingcap/tidb/util/engine" "github.com/pingcap/tidb/util/expensivequery" "github.com/pingcap/tidb/util/logutil" + "github.com/pingcap/tidb/util/memory" "github.com/pingcap/tidb/util/memoryusagealarm" "github.com/pingcap/tidb/util/servermemorylimit" "github.com/pingcap/tidb/util/sqlexec" @@ -1939,10 +1940,8 @@ func (do *Domain) loadStatsWorker() { lease = 3 * time.Second } loadTicker := time.NewTicker(lease) - updStatsHealthyTicker := time.NewTicker(20 * lease) defer func() { loadTicker.Stop() - updStatsHealthyTicker.Stop() logutil.BgLogger().Info("loadStatsWorker exited.") }() statsHandle := do.StatsHandle() @@ -1968,8 +1967,6 @@ func (do *Domain) loadStatsWorker() { if err != nil { logutil.BgLogger().Debug("load histograms failed", zap.Error(err)) } - case <-updStatsHealthyTicker.C: - statsHandle.UpdateStatsHealthyMetrics() case <-do.exit: return } @@ -2015,6 +2012,7 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) loadFeedbackTicker := time.NewTicker(5 * lease) loadLockedTablesTicker := time.NewTicker(5 * lease) dumpColStatsUsageTicker := time.NewTicker(100 * lease) + readMemTricker := time.NewTicker(memory.ReadMemInterval) statsHandle := do.StatsHandle() defer func() { dumpColStatsUsageTicker.Stop() @@ -2022,6 +2020,7 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) dumpFeedbackTicker.Stop() gcStatsTicker.Stop() deltaUpdateTicker.Stop() + readMemTricker.Stop() do.SetStatsUpdating(false) logutil.BgLogger().Info("updateStatsWorker exited.") }() @@ -2075,6 +2074,9 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) if err != nil { logutil.BgLogger().Debug("dump column stats usage failed", zap.Error(err)) } + + case <-readMemTricker.C: + memory.ForceReadMemStats() } } } diff --git a/resourcemanager/rm.go b/resourcemanager/rm.go index 41cac9cba85e7..54ae27f82a4f6 100644 --- a/resourcemanager/rm.go +++ b/resourcemanager/rm.go @@ -15,11 +15,8 @@ package resourcemanager import ( - "time" - tidbutil "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/cpu" - "github.com/pingcap/tidb/util/memory" ) // GlobalResourceManager is a global resource manager @@ -28,7 +25,6 @@ var GlobalResourceManager = NewResourceManger() // ResourceManager is a resource manager type ResourceManager struct { cpuObserver *cpu.Observer - exitCh chan struct{} wg tidbutil.WaitGroupWrapper } @@ -36,30 +32,16 @@ type ResourceManager struct { func NewResourceManger() *ResourceManager { return &ResourceManager{ cpuObserver: cpu.NewCPUObserver(), - exitCh: make(chan struct{}), } } // Start is to start resource manager func (r *ResourceManager) Start() { r.wg.Run(r.cpuObserver.Start) - r.wg.Run(func() { - readMemTricker := time.NewTicker(memory.ReadMemInterval) - defer readMemTricker.Stop() - for { - select { - case <-readMemTricker.C: - memory.ForceReadMemStats() - case <-r.exitCh: - return - } - } - }) } // Stop is to stop resource manager func (r *ResourceManager) Stop() { r.cpuObserver.Stop() - close(r.exitCh) r.wg.Done() } From 760618b75b1c97593d08707e88847a16aec6c356 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 15 Dec 2022 15:09:03 +0800 Subject: [PATCH 4/5] resourcemanger: create cpu/memory monitor Signed-off-by: Weizhen Wang --- domain/domain.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/domain/domain.go b/domain/domain.go index 82055bffbe8a9..82963ed0cebdc 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -1940,8 +1940,10 @@ func (do *Domain) loadStatsWorker() { lease = 3 * time.Second } loadTicker := time.NewTicker(lease) + updStatsHealthyTicker := time.NewTicker(20 * lease) defer func() { loadTicker.Stop() + updStatsHealthyTicker.Stop() logutil.BgLogger().Info("loadStatsWorker exited.") }() statsHandle := do.StatsHandle() @@ -1967,6 +1969,8 @@ func (do *Domain) loadStatsWorker() { if err != nil { logutil.BgLogger().Debug("load histograms failed", zap.Error(err)) } + case <-updStatsHealthyTicker.C: + statsHandle.UpdateStatsHealthyMetrics() case <-do.exit: return } From 926983c93f2b519e58851d271f80a3efbc8538a7 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 15 Dec 2022 16:15:18 +0800 Subject: [PATCH 5/5] resourcemanger: create cpu/memory monitor Signed-off-by: Weizhen Wang --- resourcemanager/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/resourcemanager/BUILD.bazel b/resourcemanager/BUILD.bazel index aceb236e5a9d0..2b97ccbd25bdd 100644 --- a/resourcemanager/BUILD.bazel +++ b/resourcemanager/BUILD.bazel @@ -8,6 +8,5 @@ go_library( deps = [ "//util", "//util/cpu", - "//util/memory", ], )