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

*: fix panic when to enable lite-init-stats and concurrently-init-stats #52226

Merged
merged 22 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3ccc217
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
31f7bef
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
3f35ae7
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
4a95c2b
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
a33de6c
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
bff288d
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
b266039
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
7bc28a6
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
b9d7178
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
5b39b5c
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
2b16a02
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
191aef5
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
9e0aa57
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
d6d4268
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
b5d28c7
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
2dd1f9a
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
bef092c
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
29e9abc
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 29, 2024
39d7433
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 30, 2024
369ca7c
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 30, 2024
bb531c3
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 30, 2024
78081f9
*: fix panic when to enable lite-init-stats and concurrently-init-stats
hawkingrei Mar 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/statistics/handle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ go_library(
"//pkg/types",
"//pkg/util/chunk",
"//pkg/util/logutil",
"//pkg/util/sqlexec",
"@com_github_pingcap_errors//:errors",
"@org_uber_go_zap//:zap",
],
Expand Down
131 changes: 84 additions & 47 deletions pkg/statistics/handle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/logutil"
"github.com/pingcap/tidb/pkg/util/sqlexec"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -78,12 +79,6 @@ func (h *Handle) initStatsMeta(is infoschema.InfoSchema) (statstypes.StatsCache,
if err != nil {
return nil, err
}
if config.GetGlobalConfig().Performance.ConcurrentlyInitStats {
ls := initstats.NewWorker(rc.Next, h.initStatsMeta4Chunk)
ls.LoadStats(is, tables, rc)
ls.Wait()
return tables, nil
}
req := rc.NewChunk(nil)
iter := chunk.NewIterator4Chunk(req)
for {
Expand Down Expand Up @@ -242,12 +237,6 @@ func (h *Handle) initStatsHistogramsLite(is infoschema.InfoSchema, cache statsty
return errors.Trace(err)
}
defer terror.Call(rc.Close)
if config.GetGlobalConfig().Performance.ConcurrentlyInitStats {
ls := initstats.NewWorker(rc.Next, h.initStatsHistograms4ChunkLite)
ls.LoadStats(is, cache, rc)
ls.Wait()
return nil
}
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
req := rc.NewChunk(nil)
iter := chunk.NewIterator4Chunk(req)
Expand All @@ -271,12 +260,29 @@ func (h *Handle) initStatsHistograms(is infoschema.InfoSchema, cache statstypes.
return errors.Trace(err)
}
defer terror.Call(rc.Close)
if config.GetGlobalConfig().Performance.ConcurrentlyInitStats {
ls := initstats.NewWorker(rc.Next, h.initStatsHistograms4Chunk)
ls.LoadStats(is, cache, rc)
ls.Wait()
return nil
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
req := rc.NewChunk(nil)
iter := chunk.NewIterator4Chunk(req)
for {
err := rc.Next(ctx, req)
if err != nil {
return errors.Trace(err)
}
if req.NumRows() == 0 {
break
}
h.initStatsHistograms4Chunk(is, cache, iter)
}
return nil
}

func (h *Handle) initStatsHistogramsByPaging(is infoschema.InfoSchema, cache statstypes.StatsCache, task initstats.Task) error {
sql := "select HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation, flag, last_analyze_pos from mysql.stats_histograms where table_id >= %? and table_id < %?"
rc, err := util.Exec(h.initStatsCtx, sql, task.StartTid, task.EndTid)
if err != nil {
return errors.Trace(err)
}
defer terror.Call(rc.Close)
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
req := rc.NewChunk(nil)
iter := chunk.NewIterator4Chunk(req)
Expand All @@ -293,6 +299,46 @@ func (h *Handle) initStatsHistograms(is infoschema.InfoSchema, cache statstypes.
return nil
}

func (h *Handle) initStatsHistogramsConcurrency(is infoschema.InfoSchema, cache statstypes.StatsCache) error {
sql := "select HIGH_PRIORITY min(table_id), max(table_id) from mysql.stats_histograms"
rs, err := util.Exec(h.initStatsCtx, sql)
if err != nil {
return err
}
var rows []chunk.Row
rows, err = sqlexec.DrainRecordSet(context.Background(), rs, 64)
if err != nil {
return err
}
err = rs.Close()
if err != nil {
return err
}
var maxTid, minTid int64
if len(rows) > 0 {
minTid = rows[0].GetInt64(0)
maxTid = rows[0].GetInt64(1)
}
ls := initstats.NewWorker(func(task initstats.Task) error {
return h.initStatsHistogramsByPaging(is, cache, task)
})
ls.LoadStats()
tid := minTid
for tid <= maxTid {
ls.SendTask(initstats.Task{
StartTid: tid,
EndTid: tid + 1000,
})
tid += 1000
}
ls.SendTask(initstats.Task{
StartTid: tid,
EndTid: tid + 1000,
})
ls.Wait()
return nil
}

func (*Handle) initStatsTopN4Chunk(cache statstypes.StatsCache, iter *chunk.Iterator4Chunk) {
affectedIndexes := make(map[*statistics.Index]struct{})
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
Expand Down Expand Up @@ -326,14 +372,6 @@ func (h *Handle) initStatsTopN(cache statstypes.StatsCache) error {
return errors.Trace(err)
}
defer terror.Call(rc.Close)
if config.GetGlobalConfig().Performance.ConcurrentlyInitStats {
ls := initstats.NewWorker(rc.Next, func(_ infoschema.InfoSchema, cache statstypes.StatsCache, iter *chunk.Iterator4Chunk) {
h.initStatsTopN4Chunk(cache, iter)
})
ls.LoadStats(nil, cache, rc)
ls.Wait()
return nil
}
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
req := rc.NewChunk(nil)
iter := chunk.NewIterator4Chunk(req)
Expand Down Expand Up @@ -458,26 +496,18 @@ func (h *Handle) initStatsBuckets(cache statstypes.StatsCache) error {
return errors.Trace(err)
}
defer terror.Call(rc.Close)
if config.GetGlobalConfig().Performance.ConcurrentlyInitStats {
ls := initstats.NewWorker(rc.Next, func(_ infoschema.InfoSchema, cache statstypes.StatsCache, iter *chunk.Iterator4Chunk) {
h.initStatsBuckets4Chunk(cache, iter)
})
ls.LoadStats(nil, cache, rc)
ls.Wait()
} else {
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
req := rc.NewChunk(nil)
iter := chunk.NewIterator4Chunk(req)
for {
err := rc.Next(ctx, req)
if err != nil {
return errors.Trace(err)
}
if req.NumRows() == 0 {
break
}
h.initStatsBuckets4Chunk(cache, iter)
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
req := rc.NewChunk(nil)
iter := chunk.NewIterator4Chunk(req)
for {
err := rc.Next(ctx, req)
if err != nil {
return errors.Trace(err)
}
if req.NumRows() == 0 {
break
}
h.initStatsBuckets4Chunk(cache, iter)
}
tables := cache.Values()
for _, table := range tables {
Expand Down Expand Up @@ -544,9 +574,16 @@ func (h *Handle) InitStats(is infoschema.InfoSchema) (err error) {
if err != nil {
return errors.Trace(err)
}
err = h.initStatsHistograms(is, cache)
if err != nil {
return errors.Trace(err)
if config.GetGlobalConfig().Performance.ConcurrentlyInitStats {
err = h.initStatsHistogramsConcurrency(is, cache)
if err != nil {
return errors.Trace(err)
}
} else {
err = h.initStatsHistograms(is, cache)
if err != nil {
return errors.Trace(err)
}
}
err = h.initStatsTopN(cache)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions pkg/statistics/handle/initstats/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ go_library(
importpath = "github.com/pingcap/tidb/pkg/statistics/handle/initstats",
visibility = ["//visibility:public"],
deps = [
"//pkg/infoschema",
"//pkg/kv",
"//pkg/statistics/handle/logutil",
"//pkg/statistics/handle/types",
"//pkg/util",
"//pkg/util/chunk",
"//pkg/util/sqlexec",
"//pkg/util/logutil",
"@org_uber_go_zap//:zap",
],
)
58 changes: 23 additions & 35 deletions pkg/statistics/handle/initstats/load_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,60 @@
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"
statstypes "github.com/pingcap/tidb/pkg/statistics/handle/types"
"github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/sqlexec"
"github.com/pingcap/tidb/pkg/util/logutil"
"go.uber.org/zap"
)

// Task represents the range of the table for loading stats.
type Task struct {
StartTid int64
EndTid int64
}

// 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 statstypes.StatsCache, iter *chunk.Iterator4Chunk)
mu sync.Mutex
wg util.WaitGroupWrapper
dealFunc func(task Task) error
taskChan chan Task

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 statstypes.StatsCache, iter *chunk.Iterator4Chunk)) *Worker {
func NewWorker(dealFunc func(task Task) error) *Worker {
return &Worker{
taskFunc: taskFunc,
dealFunc: dealFunc,
taskChan: make(chan Task, 1),
}
}

// LoadStats loads stats concurrently when to init stats
func (ls *Worker) LoadStats(is infoschema.InfoSchema, cache statstypes.StatsCache, rc sqlexec.RecordSet) {
func (ls *Worker) LoadStats() {
concurrency := runtime.GOMAXPROCS(0)
for n := 0; n < concurrency; n++ {
ls.wg.Run(func() {
req := rc.NewChunk(nil)
ls.loadStats(is, cache, req)
ls.loadStats()
})
}
}

func (ls *Worker) loadStats(is infoschema.InfoSchema, cache statstypes.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
func (ls *Worker) loadStats() {
for task := range ls.taskChan {
if err := ls.dealFunc(task); err != nil {
logutil.BgLogger().Error("load stats failed", zap.Error(err))
}
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)
// SendTask sends a task to the load stats worker.
func (ls *Worker) SendTask(task Task) {
ls.taskChan <- task
}

// Wait closes the load stats worker.
func (ls *Worker) Wait() {
close(ls.taskChan)
ls.wg.Wait()
}