Skip to content

Commit

Permalink
statistics: replace StatsLoad mutex with a dedicated mutex for statsC…
Browse files Browse the repository at this point in the history
…ache protection (#59778)

ref #55043
  • Loading branch information
Rustin170506 authored Mar 3, 2025
1 parent c73ae58 commit 448649e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pkg/statistics/handle/syncload/stats_syncload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
stderrors "errors"
"math/rand"
"runtime"
"sync"
"time"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -63,6 +64,10 @@ type statsSyncLoad struct {
statsHandle statstypes.StatsHandle
is infoschema.InfoSchema
StatsLoad statstypes.StatsLoad
// This mutex protects the statsCache from concurrent modifications by multiple workers.
// Since multiple workers may update the statsCache for the same table simultaneously,
// the mutex ensures thread-safety during these updates.
mutexForStatsCache sync.Mutex
}

var globalStatsSyncLoadSingleFlight singleflight.Group
Expand Down Expand Up @@ -564,8 +569,8 @@ func (*statsSyncLoad) writeToResultChan(resultCh chan stmtctx.StatsLoadResult, r

// updateCachedItem updates the column/index hist to global statsCache.
func (s *statsSyncLoad) updateCachedItem(tblInfo *model.TableInfo, item model.TableItemID, colHist *statistics.Column, idxHist *statistics.Index, fullLoaded bool) (updated bool) {
s.StatsLoad.Lock()
defer s.StatsLoad.Unlock()
s.mutexForStatsCache.Lock()
defer s.mutexForStatsCache.Unlock()
// Reload the latest stats cache, otherwise the `updateStatsCache` may fail with high probability, because functions
// like `GetPartitionStats` called in `fmSketchFromStorage` would have modified the stats cache already.
tbl, ok := s.statsHandle.Get(item.TableID)
Expand Down
2 changes: 0 additions & 2 deletions pkg/statistics/handle/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package types

import (
"context"
"sync"
"time"

"github.com/pingcap/tidb/pkg/ddl/notifier"
Expand Down Expand Up @@ -469,7 +468,6 @@ type NeededItemTask struct {
type StatsLoad struct {
NeededItemsCh chan *NeededItemTask
TimeoutItemsCh chan *NeededItemTask
sync.Mutex
}

// StatsSyncLoad implement the sync-load feature.
Expand Down

0 comments on commit 448649e

Please sign in to comment.