forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
statistics: concurrency init stats (pingcap#51403) (pingcap#52261)
close pingcap#52102
- Loading branch information
1 parent
7d16cc7
commit 2cd930e
Showing
7 changed files
with
207 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "initstats", | ||
srcs = ["load_stats.go"], | ||
importpath = "github.com/pingcap/tidb/pkg/statistics/handle/initstats", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/infoschema", | ||
"//pkg/kv", | ||
"//pkg/statistics/handle/logutil", | ||
"//pkg/statistics/handle/util", | ||
"//pkg/util", | ||
"//pkg/util/chunk", | ||
"//pkg/util/sqlexec", | ||
"@org_uber_go_zap//:zap", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2024 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 initstats | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
"sync" | ||
|
||
"github.com/pingcap/tidb/pkg/infoschema" | ||
"github.com/pingcap/tidb/pkg/kv" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/logutil" | ||
statsutil "github.com/pingcap/tidb/pkg/statistics/handle/util" | ||
"github.com/pingcap/tidb/pkg/util" | ||
"github.com/pingcap/tidb/pkg/util/chunk" | ||
"github.com/pingcap/tidb/pkg/util/sqlexec" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Worker is used to load stats concurrently. | ||
type Worker struct { | ||
taskFunc func(ctx context.Context, req *chunk.Chunk) error | ||
dealFunc func(is infoschema.InfoSchema, cache statsutil.StatsCache, iter *chunk.Iterator4Chunk) | ||
mu sync.Mutex | ||
wg util.WaitGroupWrapper | ||
} | ||
|
||
// NewWorker creates a new Worker. | ||
func NewWorker( | ||
taskFunc func(ctx context.Context, req *chunk.Chunk) error, | ||
dealFunc func(is infoschema.InfoSchema, cache statsutil.StatsCache, iter *chunk.Iterator4Chunk)) *Worker { | ||
return &Worker{ | ||
taskFunc: taskFunc, | ||
dealFunc: dealFunc, | ||
} | ||
} | ||
|
||
// LoadStats loads stats concurrently when to init stats | ||
func (ls *Worker) LoadStats(is infoschema.InfoSchema, cache statsutil.StatsCache, rc sqlexec.RecordSet) { | ||
concurrency := runtime.GOMAXPROCS(0) | ||
for n := 0; n < concurrency; n++ { | ||
ls.wg.Run(func() { | ||
req := rc.NewChunk(nil) | ||
ls.loadStats(is, cache, req) | ||
}) | ||
} | ||
} | ||
|
||
func (ls *Worker) loadStats(is infoschema.InfoSchema, cache statsutil.StatsCache, req *chunk.Chunk) { | ||
iter := chunk.NewIterator4Chunk(req) | ||
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats) | ||
for { | ||
err := ls.getTask(ctx, req) | ||
if err != nil { | ||
logutil.StatsLogger().Error("load stats failed", zap.Error(err)) | ||
return | ||
} | ||
if req.NumRows() == 0 { | ||
return | ||
} | ||
ls.dealFunc(is, cache, iter) | ||
} | ||
} | ||
|
||
func (ls *Worker) getTask(ctx context.Context, req *chunk.Chunk) error { | ||
ls.mu.Lock() | ||
defer ls.mu.Unlock() | ||
return ls.taskFunc(ctx, req) | ||
} | ||
|
||
// Wait closes the load stats worker. | ||
func (ls *Worker) Wait() { | ||
ls.wg.Wait() | ||
} |