Skip to content

Commit

Permalink
statistics: fix LoadStatsFromJSONNoUpdate func for CPUNum = 1 (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 authored Dec 12, 2023
1 parent 4b1cccb commit 2ca7121
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
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

0 comments on commit 2ca7121

Please sign in to comment.