-
Notifications
You must be signed in to change notification settings - Fork 781
/
bigchunk.go
348 lines (303 loc) · 7.2 KB
/
bigchunk.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
package encoding
import (
"bytes"
"encoding/binary"
"errors"
"io"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/tsdb/chunkenc"
)
const samplesPerChunk = 120
var errOutOfBounds = errors.New("out of bounds")
type smallChunk struct {
chunkenc.XORChunk
start int64
}
// bigchunk is a set of prometheus/tsdb chunks. It grows over time and has no
// upperbound on number of samples it can contain.
type bigchunk struct {
chunks []smallChunk
appender chunkenc.Appender
remainingSamples int
}
func newBigchunk() *bigchunk {
return &bigchunk{}
}
func (b *bigchunk) Add(sample model.SamplePair) (Chunk, error) {
if b.remainingSamples == 0 {
if bigchunkSizeCapBytes > 0 && b.Size() > bigchunkSizeCapBytes {
return addToOverflowChunk(sample)
}
if err := b.addNextChunk(sample.Timestamp); err != nil {
return nil, err
}
}
b.appender.Append(int64(sample.Timestamp), float64(sample.Value))
b.remainingSamples--
return nil, nil
}
// addNextChunk adds a new XOR "subchunk" to the internal list of chunks.
func (b *bigchunk) addNextChunk(start model.Time) error {
// To save memory, we "compact" the previous chunk - the array backing the slice
// will be upto 2x too big, and we can save this space.
const chunkCapacityExcess = 32 // don't bother copying if it's within this range
if l := len(b.chunks); l > 0 {
oldBuf := b.chunks[l-1].XORChunk.Bytes()
if cap(oldBuf) > len(oldBuf)+chunkCapacityExcess {
buf := make([]byte, len(oldBuf))
copy(buf, oldBuf)
compacted, err := chunkenc.FromData(chunkenc.EncXOR, buf)
if err != nil {
return err
}
b.chunks[l-1].XORChunk = *compacted.(*chunkenc.XORChunk)
}
}
// Explicitly reallocate slice to avoid up to 2x overhead if we let append() do it
if len(b.chunks)+1 > cap(b.chunks) {
newChunks := make([]smallChunk, len(b.chunks), len(b.chunks)+1)
copy(newChunks, b.chunks)
b.chunks = newChunks
}
b.chunks = append(b.chunks, smallChunk{
XORChunk: *chunkenc.NewXORChunk(),
start: int64(start),
})
appender, err := b.chunks[len(b.chunks)-1].Appender()
if err != nil {
return err
}
b.appender = appender
b.remainingSamples = samplesPerChunk
return nil
}
func (b *bigchunk) Marshal(wio io.Writer) error {
w := writer{wio}
if err := w.WriteVarInt16(uint16(len(b.chunks))); err != nil {
return err
}
for _, chunk := range b.chunks {
buf := chunk.Bytes()
if err := w.WriteVarInt16(uint16(len(buf))); err != nil {
return err
}
if _, err := w.Write(buf); err != nil {
return err
}
}
return nil
}
func (b *bigchunk) MarshalToBuf(buf []byte) error {
writer := bytes.NewBuffer(buf)
return b.Marshal(writer)
}
func (b *bigchunk) UnmarshalFromBuf(buf []byte) error {
r := reader{buf: buf}
numChunks, err := r.ReadUint16()
if err != nil {
return err
}
b.chunks = make([]smallChunk, 0, numChunks+1) // allow one extra space in case we want to add new data
var reuseIter chunkenc.Iterator
for i := uint16(0); i < numChunks; i++ {
chunkLen, err := r.ReadUint16()
if err != nil {
return err
}
chunkBuf, err := r.ReadBytes(int(chunkLen))
if err != nil {
return err
}
chunk, err := chunkenc.FromData(chunkenc.EncXOR, chunkBuf)
if err != nil {
return err
}
var start int64
start, reuseIter, err = firstTime(chunk, reuseIter)
if err != nil {
return err
}
b.chunks = append(b.chunks, smallChunk{
XORChunk: *chunk.(*chunkenc.XORChunk),
start: int64(start),
})
}
return nil
}
func (b *bigchunk) Encoding() Encoding {
return Bigchunk
}
func (b *bigchunk) Utilization() float64 {
return 1.0
}
func (b *bigchunk) Len() int {
sum := 0
for _, c := range b.chunks {
sum += c.NumSamples()
}
return sum
}
func (b *bigchunk) Size() int {
sum := 2 // For the number of sub chunks.
for _, c := range b.chunks {
sum += 2 // For the length of the sub chunk.
sum += len(c.Bytes())
}
return sum
}
func (b *bigchunk) NewIterator(reuseIter Iterator) Iterator {
if bci, ok := reuseIter.(*bigchunkIterator); ok {
bci.bigchunk = b
bci.i = 0
if len(b.chunks) > 0 {
bci.curr = b.chunks[0].Iterator(bci.curr)
} else {
bci.curr = chunkenc.NewNopIterator()
}
return bci
}
var it chunkenc.Iterator
if len(b.chunks) > 0 {
it = b.chunks[0].Iterator(it)
} else {
it = chunkenc.NewNopIterator()
}
return &bigchunkIterator{
bigchunk: b,
curr: it,
}
}
func (b *bigchunk) Slice(start, end model.Time) Chunk {
i, j := 0, len(b.chunks)
for k := 0; k < len(b.chunks); k++ {
if b.chunks[k].start <= int64(start) {
i = k
}
if b.chunks[k].start > int64(end) {
j = k
break
}
}
return &bigchunk{
chunks: b.chunks[i:j],
}
}
type writer struct {
io.Writer
}
func (w writer) WriteVarInt16(i uint16) error {
var b [2]byte
binary.LittleEndian.PutUint16(b[:], i)
_, err := w.Write(b[:])
return err
}
type reader struct {
i int
buf []byte
}
func (r *reader) ReadUint16() (uint16, error) {
if r.i+2 > len(r.buf) {
return 0, errOutOfBounds
}
result := binary.LittleEndian.Uint16(r.buf[r.i:])
r.i += 2
return result, nil
}
func (r *reader) ReadBytes(count int) ([]byte, error) {
if r.i+count > len(r.buf) {
return nil, errOutOfBounds
}
result := r.buf[r.i : r.i+count]
r.i += count
return result, nil
}
type bigchunkIterator struct {
*bigchunk
curr chunkenc.Iterator
i int
}
func (it *bigchunkIterator) FindAtOrAfter(target model.Time) bool {
if it.i >= len(it.chunks) {
return false
}
// If the seek is outside the current chunk, use the index to find the right
// chunk.
if int64(target) < it.chunks[it.i].start ||
(it.i+1 < len(it.chunks) && int64(target) >= it.chunks[it.i+1].start) {
it.curr = nil
for it.i = 0; it.i+1 < len(it.chunks) && int64(target) >= it.chunks[it.i+1].start; it.i++ {
}
}
if it.curr == nil {
it.curr = it.chunks[it.i].Iterator(it.curr)
} else if t, _ := it.curr.At(); int64(target) <= t {
it.curr = it.chunks[it.i].Iterator(it.curr)
}
for it.curr.Next() {
t, _ := it.curr.At()
if t >= int64(target) {
return true
}
}
// Timestamp is after the end of that chunk - if there is another chunk
// then the position we need is at the beginning of it.
if it.i+1 < len(it.chunks) {
it.i++
it.curr = it.chunks[it.i].Iterator(it.curr)
it.curr.Next()
return true
}
return false
}
func (it *bigchunkIterator) Scan() bool {
if it.curr.Next() {
return true
}
if err := it.curr.Err(); err != nil {
return false
}
for it.i < len(it.chunks)-1 {
it.i++
it.curr = it.chunks[it.i].Iterator(it.curr)
if it.curr.Next() {
return true
}
}
return false
}
func (it *bigchunkIterator) Value() model.SamplePair {
t, v := it.curr.At()
return model.SamplePair{
Timestamp: model.Time(t),
Value: model.SampleValue(v),
}
}
func (it *bigchunkIterator) Batch(size int) Batch {
var result Batch
j := 0
for j < size {
t, v := it.curr.At()
result.Timestamps[j] = t
result.Values[j] = v
j++
if j < size && !it.Scan() {
break
}
}
result.Length = j
return result
}
func (it *bigchunkIterator) Err() error {
if it.curr != nil {
return it.curr.Err()
}
return nil
}
func firstTime(c chunkenc.Chunk, iter chunkenc.Iterator) (int64, chunkenc.Iterator, error) {
var first int64
iter = c.Iterator(iter)
if iter.Next() {
first, _ = iter.At()
}
return first, iter, iter.Err()
}