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: shard needsStatsMap #52183

Merged
merged 8 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion pkg/statistics/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *Column) MemoryUsage() CacheItemMemoryUsage {

// HistogramNeededItems stores the columns/indices whose Histograms need to be loaded from physical kv layer.
// Currently, we only load index/pk's Histogram from kv automatically. Columns' are loaded by needs.
var HistogramNeededItems = neededStatsMap{items: map[model.TableItemID]struct{}{}}
var HistogramNeededItems = newNeededStatsMap()

// ColumnStatsIsInvalid checks if this column is invalid.
// If this column has histogram but not loaded yet,
Expand Down
59 changes: 54 additions & 5 deletions pkg/statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,12 @@ func (t *Table) IndexIsLoadNeeded(id int64) (*Index, bool) {
return idx, false
}

type neededStatsMap struct {
type neededStatsInternalMap struct {
items map[model.TableItemID]struct{}
m sync.RWMutex
}

func (n *neededStatsMap) AllItems() []model.TableItemID {
func (n *neededStatsInternalMap) AllItems() []model.TableItemID {
n.m.RLock()
keys := make([]model.TableItemID, 0, len(n.items))
for key := range n.items {
Expand All @@ -668,24 +668,73 @@ func (n *neededStatsMap) AllItems() []model.TableItemID {
return keys
}

func (n *neededStatsMap) Insert(col model.TableItemID) {
func (n *neededStatsInternalMap) Insert(col model.TableItemID) {
n.m.Lock()
n.items[col] = struct{}{}
n.m.Unlock()
}

func (n *neededStatsMap) Delete(col model.TableItemID) {
func (n *neededStatsInternalMap) Delete(col model.TableItemID) {
n.m.Lock()
delete(n.items, col)
n.m.Unlock()
}

func (n *neededStatsMap) Length() int {
func (n *neededStatsInternalMap) Length() int {
n.m.RLock()
defer n.m.RUnlock()
return len(n.items)
}

const shardCnt = 128

type neededStatsMap struct {
items [shardCnt]neededStatsInternalMap
}

func getIdx(tbl model.TableItemID) int64 {
result := tbl.ID % shardCnt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just tbl.ID's negativeness before mod

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if result < 0 {
return -result
}
return result
}

func newNeededStatsMap() *neededStatsMap {
result := neededStatsMap{}
for i := 0; i < shardCnt; i++ {
result.items[i] = neededStatsInternalMap{
items: make(map[model.TableItemID]struct{}),
}
}
return &result
}

func (n *neededStatsMap) AllItems() []model.TableItemID {
var result []model.TableItemID
for i := 0; i < shardCnt; i++ {
keys := n.items[i].AllItems()
result = append(result, keys...)
}
return result
}

func (n *neededStatsMap) Insert(col model.TableItemID) {
n.items[getIdx(col)].Insert(col)
}

func (n *neededStatsMap) Delete(col model.TableItemID) {
n.items[getIdx(col)].Delete(col)
}

func (n *neededStatsMap) Length() int {
var result int
for i := 0; i < shardCnt; i++ {
result += n.items[i].Length()
}
return result
}

// RatioOfPseudoEstimate means if modifyCount / statsTblCount is greater than this ratio, we think the stats is invalid
// and use pseudo estimation.
var RatioOfPseudoEstimate = atomic.NewFloat64(0.7)
Expand Down