Skip to content

Commit 0c0f4f0

Browse files
lhy1024ti-chi-bot
authored andcommitted
This is an automated cherry-pick of tikv#6080
ref tikv#5798, close tikv#6069 Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
1 parent 297cee8 commit 0c0f4f0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

pkg/movingaverage/median_filter.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,31 @@ import "github.com/elliotchance/pie/v2"
2020
// There are at most `size` data points for calculating.
2121
// References: https://en.wikipedia.org/wiki/Median_filter.
2222
type MedianFilter struct {
23+
<<<<<<< HEAD
2324
records []float64
2425
size uint64
2526
count uint64
2627
instantaneous float64
28+
=======
29+
// It is not thread safe to read and write records at the same time.
30+
// If there are concurrent read and write, the read may get an old value.
31+
// And we should avoid concurrent write.
32+
records []float64
33+
size uint64
34+
count uint64
35+
result float64
36+
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
2737
}
2838

2939
// NewMedianFilter returns a MedianFilter.
3040
func NewMedianFilter(size int) *MedianFilter {
3141
return &MedianFilter{
3242
records: make([]float64, size),
3343
size: uint64(size),
44+
<<<<<<< HEAD
45+
=======
46+
result: 0,
47+
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
3448
}
3549
}
3650

@@ -39,31 +53,51 @@ func (r *MedianFilter) Add(n float64) {
3953
r.instantaneous = n
4054
r.records[r.count%r.size] = n
4155
r.count++
56+
<<<<<<< HEAD
4257
}
4358

4459
// Get returns the median of the data set.
4560
func (r *MedianFilter) Get() float64 {
4661
if r.count == 0 {
4762
return 0
4863
}
64+
=======
65+
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
4966
records := r.records
5067
if r.count < r.size {
5168
records = r.records[:r.count]
5269
}
70+
<<<<<<< HEAD
5371
return pie.Median(records)
72+
=======
73+
r.result = pie.Median(records)
74+
}
75+
76+
// Get returns the median of the data set.
77+
func (r *MedianFilter) Get() float64 {
78+
return r.result
79+
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
5480
}
5581

5682
// Reset cleans the data set.
5783
func (r *MedianFilter) Reset() {
5884
r.instantaneous = 0
5985
r.count = 0
86+
<<<<<<< HEAD
87+
=======
88+
r.result = 0
89+
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
6090
}
6191

6292
// Set = Reset + Add.
6393
func (r *MedianFilter) Set(n float64) {
6494
r.instantaneous = n
6595
r.records[0] = n
6696
r.count = 1
97+
<<<<<<< HEAD
98+
=======
99+
r.result = n
100+
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
67101
}
68102

69103
// GetInstantaneous returns the value just added.
@@ -76,9 +110,16 @@ func (r *MedianFilter) Clone() *MedianFilter {
76110
records := make([]float64, len(r.records))
77111
copy(records, r.records)
78112
return &MedianFilter{
113+
<<<<<<< HEAD
79114
records: records,
80115
size: r.size,
81116
count: r.count,
82117
instantaneous: r.instantaneous,
118+
=======
119+
records: records,
120+
size: r.size,
121+
count: r.count,
122+
result: r.result,
123+
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
83124
}
84125
}

0 commit comments

Comments
 (0)