15
15
package statistics
16
16
17
17
import (
18
+ "sync"
18
19
"sync/atomic"
19
20
"time"
20
21
@@ -44,6 +45,8 @@ type topnStatsMergeWorker struct {
44
45
respCh chan <- * TopnStatsMergeResponse
45
46
// the stats in the wrapper should only be read during the worker
46
47
statsWrapper * StatsWrapper
48
+ // shardMutex is used to protect `statsWrapper.AllHg`
49
+ shardMutex []sync.Mutex
47
50
}
48
51
49
52
// NewTopnStatsMergeWorker returns topn merge worker
@@ -57,6 +60,7 @@ func NewTopnStatsMergeWorker(
57
60
respCh : respCh ,
58
61
}
59
62
worker .statsWrapper = wrapper
63
+ worker .shardMutex = make ([]sync.Mutex , len (wrapper .AllHg ))
60
64
worker .killed = killed
61
65
return worker
62
66
}
@@ -77,10 +81,9 @@ func NewTopnStatsMergeTask(start, end int) *TopnStatsMergeTask {
77
81
78
82
// TopnStatsMergeResponse indicates topn merge worker response
79
83
type TopnStatsMergeResponse struct {
80
- TopN * TopN
81
- PopedTopn []TopNMeta
82
- RemoveVals [][]TopNMeta
83
- Err error
84
+ Err error
85
+ TopN * TopN
86
+ PopedTopn []TopNMeta
84
87
}
85
88
86
89
// Run runs topn merge like statistics.MergePartTopN2GlobalTopN
@@ -99,7 +102,6 @@ func (worker *topnStatsMergeWorker) Run(timeZone *time.Location, isIndex bool,
99
102
return
100
103
}
101
104
partNum := len (allTopNs )
102
- removeVals := make ([][]TopNMeta , partNum )
103
105
// Different TopN structures may hold the same value, we have to merge them.
104
106
counter := make (map [hack.MutableString ]float64 )
105
107
// datumMap is used to store the mapping from the string type to datum type.
@@ -168,13 +170,13 @@ func (worker *topnStatsMergeWorker) Run(timeZone *time.Location, isIndex bool,
168
170
if count != 0 {
169
171
counter [encodedVal ] += count
170
172
// Remove the value corresponding to encodedVal from the histogram.
171
- removeVals [j ] = append (removeVals [j ], TopNMeta {Encoded : datum .GetBytes (), Count : uint64 (count )})
173
+ worker .shardMutex [j ].Lock ()
174
+ worker .statsWrapper .AllHg [j ].BinarySearchRemoveVal (TopNMeta {Encoded : datum .GetBytes (), Count : uint64 (count )})
175
+ worker .shardMutex [j ].Unlock ()
172
176
}
173
177
}
174
178
}
175
179
}
176
- // record remove values
177
- resp .RemoveVals = removeVals
178
180
179
181
numTop := len (counter )
180
182
if numTop == 0 {
0 commit comments