Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: fix LoadStatsFromJSONNoUpdate func for CPUNum = 1 #49375

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/statistics/handle/storage/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -226,6 +227,10 @@ func TestLoadPartitionStats(t *testing.T) {
func TestLoadPartitionStatsErrPanic(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
val := runtime.GOMAXPROCS(1)
defer func() {
runtime.GOMAXPROCS(val)
}()
tk.MustExec("use test")
tk.MustExec("set @@tidb_analyze_version = 2")
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
Expand Down
14 changes: 6 additions & 8 deletions pkg/statistics/handle/storage/stats_read_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ type TestLoadStatsErr struct{}
// LoadStatsFromJSON will load statistic from JSONTable, and save it to the storage.
// In final, it will also udpate the stats cache.
func (s *statsReadWriter) LoadStatsFromJSON(ctx context.Context, is infoschema.InfoSchema,
jsonTbl *util.JSONTable, concurrencyForPartition uint8) error {
jsonTbl *util.JSONTable, concurrencyForPartition int) error {
if err := s.LoadStatsFromJSONNoUpdate(ctx, is, jsonTbl, concurrencyForPartition); err != nil {
return errors.Trace(err)
}
Expand All @@ -567,14 +567,12 @@ func (s *statsReadWriter) LoadStatsFromJSON(ctx context.Context, is infoschema.I

// LoadStatsFromJSONNoUpdate will load statistic from JSONTable, and save it to the storage.
func (s *statsReadWriter) LoadStatsFromJSONNoUpdate(ctx context.Context, is infoschema.InfoSchema,
jsonTbl *util.JSONTable, concurrencyForPartition uint8) error {
nCPU := uint8(runtime.GOMAXPROCS(0))
jsonTbl *util.JSONTable, concurrencyForPartition int) error {
nCPU := runtime.GOMAXPROCS(0)
if concurrencyForPartition == 0 {
concurrencyForPartition = nCPU / 2 // default
}
if concurrencyForPartition > nCPU {
concurrencyForPartition = nCPU // for safety
concurrencyForPartition = (nCPU + 1) / 2 // default
}
concurrencyForPartition = min(concurrencyForPartition, nCPU) // for safety

table, err := is.TableByName(model.NewCIStr(jsonTbl.DatabaseName), model.NewCIStr(jsonTbl.TableName))
if err != nil {
Expand All @@ -596,7 +594,7 @@ func (s *statsReadWriter) LoadStatsFromJSONNoUpdate(ctx context.Context, is info
close(taskCh)
var wg sync.WaitGroup
e := new(atomic.Pointer[error])
for i := 0; i < int(concurrencyForPartition); i++ {
for i := 0; i < concurrencyForPartition; i++ {
wg.Add(1)
s.statsHandler.GPool().Go(func() {
defer func() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/statistics/handle/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ type StatsReadWriter interface {

// LoadStatsFromJSON will load statistic from JSONTable, and save it to the storage.
// In final, it will also udpate the stats cache.
LoadStatsFromJSON(ctx context.Context, is infoschema.InfoSchema, jsonTbl *statsutil.JSONTable, concurrencyForPartition uint8) error
LoadStatsFromJSON(ctx context.Context, is infoschema.InfoSchema, jsonTbl *statsutil.JSONTable, concurrencyForPartition int) error

// LoadStatsFromJSONNoUpdate will load statistic from JSONTable, and save it to the storage.
LoadStatsFromJSONNoUpdate(ctx context.Context, is infoschema.InfoSchema, jsonTbl *statsutil.JSONTable, concurrencyForPartition uint8) error
LoadStatsFromJSONNoUpdate(ctx context.Context, is infoschema.InfoSchema, jsonTbl *statsutil.JSONTable, concurrencyForPartition int) error

// Methods for extended stast.

Expand Down