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: improve memory for mergeGlobalStatsTopNByConcurrency #45993

Merged
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: 1 addition & 4 deletions statistics/cmsketch_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ func benchmarkMergeGlobalStatsTopNByConcurrencyWithHists(partitions int, b *test
h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 40})
hists = append(hists, h)
}
wrapper := &statistics.StatsWrapper{
AllTopN: topNs,
AllHg: hists,
}
wrapper := statistics.NewStatsWrapper(hists, topNs)
const mergeConcurrency = 4
batchSize := len(wrapper.AllTopN) / mergeConcurrency
if batchSize < 1 {
Expand Down
19 changes: 2 additions & 17 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package handle

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -948,19 +947,15 @@

// handle Error
hasErr := false
errMsg := make([]string, 0)
for resp := range respCh {
if resp.Err != nil {
hasErr = true
errMsg = append(errMsg, resp.Err.Error())

Check warning on line 954 in statistics/handle/handle.go

View check run for this annotation

Codecov / codecov/patch

statistics/handle/handle.go#L954

Added line #L954 was not covered by tests
}
resps = append(resps, resp)
}
if hasErr {
errMsg := make([]string, 0)
for _, resp := range resps {
if resp.Err != nil {
errMsg = append(errMsg, resp.Err.Error())
}
}
return nil, nil, nil, errors.New(strings.Join(errMsg, ","))
}

Expand All @@ -972,16 +967,6 @@
sorted = append(sorted, resp.TopN.TopN...)
}
leftTopn = append(leftTopn, resp.PopedTopn...)
for i, removeTopn := range resp.RemoveVals {
// Remove the value from the Hists.
if len(removeTopn) > 0 {
tmp := removeTopn
slices.SortFunc(tmp, func(i, j statistics.TopNMeta) int {
return bytes.Compare(i.Encoded, j.Encoded)
})
wrapper.AllHg[i].RemoveVals(tmp)
}
}
}

globalTopN, popedTopn := statistics.GetMergedTopNFromSortedSlice(sorted, n)
Expand Down
18 changes: 10 additions & 8 deletions statistics/merge_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package statistics

import (
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -44,6 +45,8 @@
respCh chan<- *TopnStatsMergeResponse
// the stats in the wrapper should only be read during the worker
statsWrapper *StatsWrapper
// shardMutex is used to protect `statsWrapper.AllHg`
shardMutex []sync.Mutex
}

// NewTopnStatsMergeWorker returns topn merge worker
Expand All @@ -57,6 +60,7 @@
respCh: respCh,
}
worker.statsWrapper = wrapper
worker.shardMutex = make([]sync.Mutex, len(wrapper.AllHg))
worker.killed = killed
return worker
}
Expand All @@ -77,10 +81,9 @@

// TopnStatsMergeResponse indicates topn merge worker response
type TopnStatsMergeResponse struct {
Err error
TopN *TopN
PopedTopn []TopNMeta
RemoveVals [][]TopNMeta
Err error
TopN *TopN
PopedTopn []TopNMeta
}

// Run runs topn merge like statistics.MergePartTopN2GlobalTopN
Expand All @@ -99,7 +102,6 @@
return
}
partNum := len(allTopNs)
removeVals := make([][]TopNMeta, partNum)
// Different TopN structures may hold the same value, we have to merge them.
counter := make(map[hack.MutableString]float64)
// datumMap is used to store the mapping from the string type to datum type.
Expand Down Expand Up @@ -168,13 +170,13 @@
if count != 0 {
counter[encodedVal] += count
// Remove the value corresponding to encodedVal from the histogram.
removeVals[j] = append(removeVals[j], TopNMeta{Encoded: datum.GetBytes(), Count: uint64(count)})
worker.shardMutex[j].Lock()
worker.statsWrapper.AllHg[j].BinarySearchRemoveVal(TopNMeta{Encoded: datum.GetBytes(), Count: uint64(count)})
worker.shardMutex[j].Unlock()

Check warning on line 175 in statistics/merge_worker.go

View check run for this annotation

Codecov / codecov/patch

statistics/merge_worker.go#L173-L175

Added lines #L173 - L175 were not covered by tests
}
}
}
}
// record remove values
resp.RemoveVals = removeVals

numTop := len(counter)
if numTop == 0 {
Expand Down