From 85235b367200a877076d9cff6f66dfd77162676d Mon Sep 17 00:00:00 2001 From: Ruihao Chen Date: Tue, 20 Aug 2024 17:13:49 +0800 Subject: [PATCH] domain: change groupSize in splitForConcurrentFetch (#55518) ref pingcap/tidb#50959 --- pkg/domain/BUILD.bazel | 1 + pkg/domain/domain.go | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pkg/domain/BUILD.bazel b/pkg/domain/BUILD.bazel index 2fdf3a0df3bdc..d369511566fd9 100644 --- a/pkg/domain/BUILD.bazel +++ b/pkg/domain/BUILD.bazel @@ -84,6 +84,7 @@ go_library( "//pkg/util/globalconn", "//pkg/util/intest", "//pkg/util/logutil", + "//pkg/util/mathutil", "//pkg/util/memory", "//pkg/util/memoryusagealarm", "//pkg/util/printer", diff --git a/pkg/domain/domain.go b/pkg/domain/domain.go index 621ded193d162..239bab3ff1be8 100644 --- a/pkg/domain/domain.go +++ b/pkg/domain/domain.go @@ -87,6 +87,7 @@ import ( "github.com/pingcap/tidb/pkg/util/globalconn" "github.com/pingcap/tidb/pkg/util/intest" "github.com/pingcap/tidb/pkg/util/logutil" + "github.com/pingcap/tidb/pkg/util/mathutil" "github.com/pingcap/tidb/pkg/util/memory" "github.com/pingcap/tidb/pkg/util/memoryusagealarm" "github.com/pingcap/tidb/pkg/util/replayer" @@ -462,20 +463,22 @@ func (do *Domain) fetchAllSchemasWithTables(m *meta.Meta) ([]*model.DBInfo, erro const fetchSchemaConcurrency = 1 func (*Domain) splitForConcurrentFetch(schemas []*model.DBInfo) [][]*model.DBInfo { - groupSize := (len(schemas) + fetchSchemaConcurrency - 1) / fetchSchemaConcurrency - if variable.SchemaCacheSize.Load() > 0 && len(schemas) > 1000 { + groupCnt := fetchSchemaConcurrency + schemaCnt := len(schemas) + if variable.SchemaCacheSize.Load() > 0 && schemaCnt > 1000 { // TODO: Temporary solution to speed up when too many databases, will refactor it later. - groupSize = 8 + groupCnt = 8 } - splitted := make([][]*model.DBInfo, 0, fetchSchemaConcurrency) - schemaCnt := len(schemas) - for i := 0; i < schemaCnt; i += groupSize { - end := i + groupSize - if end > schemaCnt { - end = schemaCnt - } - splitted = append(splitted, schemas[i:end]) + + splitted := make([][]*model.DBInfo, 0, groupCnt) + groupSizes := mathutil.Divide2Batches(schemaCnt, groupCnt) + + start := 0 + for _, groupSize := range groupSizes { + splitted = append(splitted, schemas[start:start+groupSize]) + start += groupSize } + return splitted }