-
Notifications
You must be signed in to change notification settings - Fork 451
/
stream.go
361 lines (316 loc) · 9.56 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cm
import (
"math"
"github.com/m3db/m3/src/x/pool"
)
const (
minSamplesToCompress = 3
)
var (
nan = math.NaN()
)
// acquireSampleFn acquires a new sample.
type acquireSampleFn func() *Sample
// releaseSampleFn releases a sample.
type releaseSampleFn func(*Sample)
// stream represents a data stream.
type stream struct {
eps float64 // desired epsilon for errors
quantiles []float64 // sorted target quantiles
capacity int // stream capacity
insertAndCompressEvery int // stream insertion and compression frequency
flushEvery int // stream flushing frequency
streamPool StreamPool // pool of streams
floatsPool pool.FloatsPool // pool of float64 slices
acquireSampleFn acquireSampleFn // function to acquire samples
releaseSampleFn releaseSampleFn // function to release samples
closed bool // whether the stream is closed
insertAndCompressCounter int // insertion and compression counter
flushCounter int // flush frequency counter
numValues int64 // number of values inserted into the sorted stream
bufLess minHeap // sample buffer whose value is less than that at the insertion cursor
bufMore minHeap // sample buffer whose value is more than that at the insertion cursor
samples sampleList // sample list
insertCursor *Sample // insertion cursor
compressCursor *Sample // compression cursor
compressMinRank int64 // compression min rank
}
// NewStream creates a new sample stream.
func NewStream(quantiles []float64, opts Options) Stream {
if opts == nil {
opts = NewOptions()
}
var (
acquireSampleFn acquireSampleFn
releaseSampleFn releaseSampleFn
)
if samplePool := opts.SamplePool(); samplePool != nil {
acquireSampleFn = func() *Sample {
sample := samplePool.Get()
sample.reset()
return sample
}
releaseSampleFn = func(sample *Sample) {
samplePool.Put(sample)
}
} else {
acquireSampleFn = newSample
releaseSampleFn = func(*Sample) {}
}
s := &stream{
eps: opts.Eps(),
capacity: opts.Capacity(),
flushEvery: opts.FlushEvery(),
streamPool: opts.StreamPool(),
floatsPool: opts.FloatsPool(),
acquireSampleFn: acquireSampleFn,
releaseSampleFn: releaseSampleFn,
}
s.ResetSetData(quantiles)
return s
}
func (s *stream) Add(value float64) {
s.addToBuffer(value)
s.insertAndCompressCounter++
if s.insertAndCompressCounter == s.insertAndCompressEvery {
for i := 0; i < s.insertAndCompressEvery; i++ {
s.insert()
s.compress()
}
s.insertAndCompressCounter = 0
}
s.flushCounter++
if s.flushCounter == s.flushEvery {
s.Flush()
s.flushCounter = 0
}
}
func (s *stream) Flush() {
for s.bufLess.Len() > 0 || s.bufMore.Len() > 0 {
if s.bufMore.Len() == 0 {
s.resetInsertCursor()
}
s.insert()
s.compress()
}
}
func (s *stream) Min() float64 {
return s.Quantile(0.0)
}
func (s *stream) Max() float64 {
return s.Quantile(1.0)
}
func (s *stream) Quantile(q float64) float64 {
if q < 0.0 || q > 1.0 {
return nan
}
if s.samples.Empty() {
return 0.0
}
if q == 0.0 {
return s.samples.Front().value
}
if q == 1.0 {
return s.samples.Back().value
}
var (
minRank int64
maxRank int64
prev = s.samples.Front()
curr = s.samples.Front()
rank = int64(math.Ceil(q * float64(s.numValues)))
threshold = int64(math.Ceil(float64(s.threshold(rank)) / 2.0))
)
for curr != nil {
maxRank = minRank + curr.numRanks + curr.delta
if maxRank > rank+threshold || minRank > rank {
break
}
minRank += curr.numRanks
prev = curr
curr = curr.next
}
return prev.value
}
func (s *stream) ResetSetData(quantiles []float64) {
s.quantiles = quantiles
s.closed = false
s.insertAndCompressCounter = 0
s.flushCounter = 0
s.numValues = 0
s.bufLess = minHeap(s.floatsPool.Get(s.capacity))
s.bufMore = minHeap(s.floatsPool.Get(s.capacity))
s.samples.Reset()
s.insertCursor = nil
s.compressCursor = nil
s.compressMinRank = 0
}
func (s *stream) Close() {
if s.closed {
return
}
s.closed = true
// Returning resources back to pools.
s.floatsPool.Put(s.bufLess)
s.floatsPool.Put(s.bufMore)
sample := s.samples.Front()
for sample != nil {
next := sample.next
s.releaseSampleFn(sample)
sample = next
}
// Clear out slices/lists/pointer to reduce GC overhead.
s.bufLess = nil
s.bufMore = nil
s.samples.Reset()
s.insertCursor = nil
s.compressCursor = nil
s.streamPool.Put(s)
}
// addToBuffer adds a new sample to the buffer.
func (s *stream) addToBuffer(value float64) {
if s.numValues > 0 && value < s.insertPointValue() {
s.addToMinHeap(&s.bufLess, value)
} else {
s.addToMinHeap(&s.bufMore, value)
}
}
// insert inserts a sample into the stream.
func (s *stream) insert() {
if s.samples.Len() == 0 {
if s.bufMore.Len() == 0 {
return
}
sample := s.acquireSampleFn()
sample.setData(s.bufMore.Pop(), 1, 0)
s.samples.PushBack(sample)
s.numValues++
s.insertCursor = s.samples.Front()
return
}
if s.insertCursor == nil {
s.insertCursor = s.samples.Front()
}
incrementSize := s.cursorIncrement()
for i := 0; i < incrementSize && s.insertCursor != nil; i++ {
for s.bufMore.Len() > 0 && s.bufMore.Min() <= s.insertPointValue() {
sample := s.acquireSampleFn()
sample.setData(s.bufMore.Pop(), 1, s.insertCursor.numRanks+s.insertCursor.delta-1)
s.samples.InsertBefore(sample, s.insertCursor)
s.numValues++
if s.compressCursor != nil && s.compressCursor.value >= sample.value {
s.compressMinRank++
}
}
s.insertCursor = s.insertCursor.next
}
if s.insertCursor != nil {
return
}
for s.bufMore.Len() > 0 && s.bufMore.Min() >= s.samples.Back().value {
sample := s.acquireSampleFn()
sample.setData(s.bufMore.Pop(), 1, 0)
s.samples.PushBack(sample)
s.numValues++
}
s.resetInsertCursor()
}
// compress compresses the samples in the stream.
func (s *stream) compress() {
// Bail early if there is nothing to compress.
if s.samples.Len() < minSamplesToCompress {
return
}
if s.compressCursor == nil {
s.compressCursor = s.samples.Back().prev
s.compressMinRank = s.numValues - 1 - s.compressCursor.numRanks
s.compressCursor = s.compressCursor.prev
}
incrementSize := s.cursorIncrement()
for i := 0; i < incrementSize && s.compressCursor != s.samples.Front(); i++ {
next := s.compressCursor.next
maxRank := s.compressMinRank + s.compressCursor.numRanks + s.compressCursor.delta
s.compressMinRank -= s.compressCursor.numRanks
threshold := s.threshold(maxRank)
testVal := s.compressCursor.numRanks + next.numRanks + next.delta
if testVal <= threshold {
if s.insertCursor == s.compressCursor {
s.insertCursor = next
}
next.numRanks += s.compressCursor.numRanks
prev := s.compressCursor.prev
s.samples.Remove(s.compressCursor)
s.releaseSampleFn(s.compressCursor)
s.compressCursor = prev
} else {
s.compressCursor = s.compressCursor.prev
}
}
if s.compressCursor == s.samples.Front() {
s.compressCursor = nil
}
}
// threshold computes the minimum threshold value.
func (s *stream) threshold(rank int64) int64 {
minVal := int64(math.MaxInt64)
for _, quantile := range s.quantiles {
var quantileMin int64
if float64(rank) >= quantile*float64(s.numValues) {
quantileMin = int64(2 * s.eps * float64(rank) / quantile)
} else {
quantileMin = int64(2 * s.eps * float64(s.numValues-rank) / (1 - quantile))
}
if quantileMin < minVal {
minVal = quantileMin
}
}
return minVal
}
// resetInsertCursor resets the insert cursor.
func (s *stream) resetInsertCursor() {
s.bufLess, s.bufMore = s.bufMore, s.bufLess
s.insertCursor = nil
}
// cursorIncrement computes the number of items to process.
func (s *stream) cursorIncrement() int {
return int(math.Ceil(float64(s.samples.Len()) * s.eps))
}
// insertPointValue returns the value under the insertion cursor.
func (s *stream) insertPointValue() float64 {
if s.insertCursor == nil {
return 0.0
}
return s.insertCursor.value
}
// addToMinHeap adds a value to a min heap.
func (s *stream) addToMinHeap(heap *minHeap, value float64) {
curr := *heap
// If we are at capacity, get a bigger heap from the pool
// and return the current heap to the pool.
if len(curr) == cap(curr) {
newHeap := s.floatsPool.Get(2 * len(curr))
newHeap = append(newHeap, curr...)
s.floatsPool.Put(curr)
*heap = newHeap
}
heap.Push(value)
}