-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathpostings_codec.go
581 lines (490 loc) · 16 KB
/
postings_codec.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package store
import (
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"github.com/golang/snappy"
"github.com/klauspost/compress/s2"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/encoding"
"github.com/prometheus/prometheus/tsdb/index"
extsnappy "github.com/thanos-io/thanos/pkg/extgrpc/snappy"
"github.com/thanos-io/thanos/pkg/pool"
)
// This file implements encoding and decoding of postings using diff (or delta) + varint
// number encoding. On top of that, we apply Snappy compression.
//
// On its own, Snappy compressing raw postings doesn't really help, because there is no
// repetition in raw data. Using diff (delta) between postings entries makes values small,
// and Varint is very efficient at encoding small values (values < 128 are encoded as
// single byte, values < 16384 are encoded as two bytes). Diff + varint reduces postings size
// significantly (to about 20% of original), snappy then halves it to ~10% of the original.
const (
codecHeaderSnappy = "dvs" // As in "diff+varint+snappy".
codecHeaderStreamedSnappy = "dss" // As in "diffvarint+streamed snappy".
)
func decodePostings(input []byte) (closeablePostings, error) {
var df func([]byte, bool) (closeablePostings, error)
switch {
case isDiffVarintSnappyEncodedPostings(input):
df = diffVarintSnappyDecode
case isDiffVarintSnappyStreamedEncodedPostings(input):
df = diffVarintSnappyStreamedDecode
default:
return nil, fmt.Errorf("unrecognize postings format")
}
return df(input, false)
}
// isDiffVarintSnappyEncodedPostings returns true, if input looks like it has been encoded by diff+varint+snappy codec.
func isDiffVarintSnappyEncodedPostings(input []byte) bool {
return bytes.HasPrefix(input, []byte(codecHeaderSnappy))
}
// isDiffVarintSnappyStreamedEncodedPostings returns true, if input looks like it has been encoded by diff+varint+snappy streamed codec.
func isDiffVarintSnappyStreamedEncodedPostings(input []byte) bool {
return bytes.HasPrefix(input, []byte(codecHeaderStreamedSnappy))
}
// estimateSnappyStreamSize estimates the number of bytes
// needed for encoding length postings. Note that in reality
// the number of bytes needed could be much bigger if postings
// different by a lot. Practically, stddev=64 is used.
func estimateSnappyStreamSize(length int) int {
// Snappy stream writes data in chunks up to 65536 in size.
// The stream begins with bytes 0xff 0x06 0x00 0x00 's' 'N' 'a' 'P' 'p' 'Y'.
// Our encoded data also needs a header.
// Each encoded (or uncompressed) chunk needs tag (chunk type 1B + chunk len 3B) + checksum 4B.
// Mark for encoded data.
ret := len(codecHeaderStreamedSnappy)
// Magic snappy stream start.
ret += 10
const maxBlockSize = 65536
length = 5 * length / 4 // estimate 1.25B per posting.
blocks := length / maxBlockSize
ret += blocks * snappy.MaxEncodedLen(maxBlockSize)
length -= blocks * maxBlockSize
if length > 0 {
ret += snappy.MaxEncodedLen(length)
}
return ret
}
func diffVarintSnappyStreamedEncode(p index.Postings, length int) ([]byte, error) {
compressedBuf := bytes.NewBuffer(make([]byte, 0, estimateSnappyStreamSize(length)))
if n, err := compressedBuf.WriteString(codecHeaderStreamedSnappy); err != nil {
return nil, fmt.Errorf("writing streamed snappy header")
} else if n != len(codecHeaderStreamedSnappy) {
return nil, fmt.Errorf("short-write streamed snappy header")
}
uvarintEncodeBuf := make([]byte, binary.MaxVarintLen64)
sw, err := extsnappy.Compressor.Compress(compressedBuf)
if err != nil {
return nil, fmt.Errorf("creating snappy compressor: %w", err)
}
prev := storage.SeriesRef(0)
for p.Next() {
v := p.At()
if v < prev {
return nil, errors.Errorf("postings entries must be in increasing order, current: %d, previous: %d", v, prev)
}
uvarintSize := binary.PutUvarint(uvarintEncodeBuf, uint64(v-prev))
if written, err := sw.Write(uvarintEncodeBuf[:uvarintSize]); err != nil {
return nil, errors.Wrap(err, "writing uvarint encoded byte")
} else if written != uvarintSize {
return nil, errors.Wrap(err, "short-write for uvarint encoded byte")
}
prev = v
}
if p.Err() != nil {
return nil, p.Err()
}
if err := sw.Close(); err != nil {
return nil, errors.Wrap(err, "closing snappy stream writer")
}
return compressedBuf.Bytes(), nil
}
func diffVarintSnappyStreamedDecode(input []byte, disablePooling bool) (closeablePostings, error) {
if !isDiffVarintSnappyStreamedEncodedPostings(input) {
return nil, errors.New("header not found")
}
return newStreamedDiffVarintPostings(input[len(codecHeaderStreamedSnappy):], disablePooling)
}
type streamedDiffVarintPostings struct {
curSeries storage.SeriesRef
err error
input, buf []byte
maximumDecodedLen int
db *encoding.Decbuf
readSnappyIdentifier bool
disablePooling bool
}
const (
chunkTypeCompressedData = 0x00
chunkTypeUncompressedData = 0x01
chunkTypeStreamIdentifier = 0xff
chunkTypePadding = 0xfe
checksumSize = 4
)
func maximumDecodedLenSnappyStreamed(in []byte) (int, error) {
maxDecodedLen := -1
for len(in) > 0 {
// Chunk type.
chunkType := in[0]
in = in[1:]
chunkLen := int(in[0]) | int(in[1])<<8 | int(in[2])<<16
in = in[3:]
switch chunkType {
case chunkTypeCompressedData:
bl := in[:chunkLen]
// NOTE: checksum will be checked later on.
decodedLen, err := s2.DecodedLen(bl[checksumSize:])
if err != nil {
return 0, err
}
if decodedLen > maxDecodedLen {
maxDecodedLen = decodedLen
}
case chunkTypeUncompressedData:
// NOTE: checksum will be checked later on.
n := chunkLen - checksumSize
if n > maxDecodedLen {
maxDecodedLen = n
}
}
in = in[chunkLen:]
}
return maxDecodedLen, nil
}
var decodedBufPool = pool.MustNewBucketedPool[byte](1024, 65536, 2, 0)
func newStreamedDiffVarintPostings(input []byte, disablePooling bool) (closeablePostings, error) {
// We can't use the regular s2.Reader because it assumes a stream.
// We already everything in memory so let's avoid copying.
// Algorithm:
// 1. Step through all chunks all get maximum decoded len.
// 2. Read into decoded step by step. For decoding call s2.Decode(r.decoded, buf).
maximumDecodedLen, err := maximumDecodedLenSnappyStreamed(input)
if err != nil {
return nil, err
}
return &streamedDiffVarintPostings{
input: input,
maximumDecodedLen: maximumDecodedLen,
db: &encoding.Decbuf{},
disablePooling: disablePooling,
}, nil
}
func (it *streamedDiffVarintPostings) close() {
if it.buf == nil {
return
}
if it.disablePooling {
return
}
decodedBufPool.Put(&it.buf)
}
func (it *streamedDiffVarintPostings) At() storage.SeriesRef {
return it.curSeries
}
func (it *streamedDiffVarintPostings) readNextChunk(remainder []byte) bool {
// Normal EOF.
if len(it.input) == 0 {
return false
}
// Read next chunk into it.db.B.
chunkType := it.input[0]
it.input = it.input[1:]
if len(it.input) < 3 {
it.err = io.ErrUnexpectedEOF
return false
}
chunkLen := int(it.input[0]) | int(it.input[1])<<8 | int(it.input[2])<<16
it.input = it.input[3:]
switch chunkType {
case chunkTypeStreamIdentifier:
const magicBody = "sNaPpY"
if chunkLen != len(magicBody) {
it.err = fmt.Errorf("corrupted identifier")
return false
}
if string(it.input[:len(magicBody)]) != magicBody {
it.err = fmt.Errorf("got bad identifier %s", string(it.input[:6]))
return false
}
it.input = it.input[6:]
it.readSnappyIdentifier = true
return it.readNextChunk(nil)
case chunkTypeCompressedData:
if !it.readSnappyIdentifier {
it.err = fmt.Errorf("missing magic snappy marker")
return false
}
if len(it.input) < 4 {
it.err = io.ErrUnexpectedEOF
return false
}
checksum := uint32(it.input[0]) | uint32(it.input[1])<<8 | uint32(it.input[2])<<16 | uint32(it.input[3])<<24
if len(it.input) < chunkLen {
it.err = io.ErrUnexpectedEOF
return false
}
if it.buf == nil {
if it.disablePooling {
it.buf = make([]byte, it.maximumDecodedLen)
} else {
b, err := decodedBufPool.Get(it.maximumDecodedLen)
if err != nil {
it.err = err
return false
}
it.buf = *b
}
}
encodedBuf := it.input[:chunkLen]
// NOTE(GiedriusS): we can probably optimize this better but this should be rare enough
// and not cause any problems.
if len(remainder) > 0 {
remainderCopy := make([]byte, 0, len(remainder))
remainderCopy = append(remainderCopy, remainder...)
remainder = remainderCopy
}
decoded, err := s2.Decode(it.buf, encodedBuf[checksumSize:])
if err != nil {
it.err = err
return false
}
if crc(decoded) != checksum {
it.err = fmt.Errorf("mismatched checksum (got %v, expected %v)", crc(decoded), checksum)
return false
}
if len(remainder) > 0 {
it.db.B = append(remainder, decoded...)
} else {
it.db.B = decoded
}
case chunkTypeUncompressedData:
if !it.readSnappyIdentifier {
it.err = fmt.Errorf("missing magic snappy marker")
return false
}
if len(it.input) < 4 {
it.err = io.ErrUnexpectedEOF
return false
}
checksum := uint32(it.input[0]) | uint32(it.input[1])<<8 | uint32(it.input[2])<<16 | uint32(it.input[3])<<24
if len(it.input) < chunkLen {
it.err = io.ErrUnexpectedEOF
return false
}
uncompressedData := it.input[checksumSize:chunkLen]
if crc(uncompressedData) != checksum {
it.err = fmt.Errorf("mismatched checksum (got %v, expected %v)", crc(uncompressedData), checksum)
return false
}
// NOTE(GiedriusS): we can probably optimize this better but this should be rare enough
// and not cause any problems.
if len(remainder) > 0 {
remainderCopy := make([]byte, 0, len(remainder))
remainderCopy = append(remainderCopy, remainder...)
remainder = remainderCopy
}
if len(remainder) > 0 {
it.db.B = append(remainder, uncompressedData...)
} else {
it.db.B = uncompressedData
}
default:
if chunkType <= 0x7f {
it.err = fmt.Errorf("unsupported chunk type %v", chunkType)
return false
}
if chunkType > 0xfd {
it.err = fmt.Errorf("invalid chunk type %v", chunkType)
return false
}
}
it.input = it.input[chunkLen:]
return true
}
func (it *streamedDiffVarintPostings) Next() bool {
// Continue reading next chunks until there is at least binary.MaxVarintLen64.
// If we cannot add any more chunks then return false.
for {
val := it.db.Uvarint64()
if it.db.Err() != nil {
if !it.readNextChunk(it.db.B) {
return false
}
it.db.E = nil
continue
}
it.curSeries = it.curSeries + storage.SeriesRef(val)
return true
}
}
func (it *streamedDiffVarintPostings) Err() error {
return it.err
}
func (it *streamedDiffVarintPostings) Seek(x storage.SeriesRef) bool {
if it.curSeries >= x {
return true
}
// We cannot do any search due to how values are stored,
// so we simply advance until we find the right value.
for it.Next() {
if it.At() >= x {
return true
}
}
return false
}
// diffVarintSnappyEncode encodes postings into diff+varint representation,
// and applies snappy compression on the result.
// Returned byte slice starts with codecHeaderSnappy header.
// Length argument is expected number of postings, used for preallocating buffer.
// TODO(GiedriusS): remove for v1.0.
func diffVarintSnappyEncode(p index.Postings, length int) ([]byte, error) {
buf, err := diffVarintEncodeNoHeader(p, length)
if err != nil {
return nil, err
}
// Make result buffer large enough to hold our header and compressed block.
result := make([]byte, len(codecHeaderSnappy)+snappy.MaxEncodedLen(len(buf)))
copy(result, codecHeaderSnappy)
compressed := snappy.Encode(result[len(codecHeaderSnappy):], buf)
// Slice result buffer based on compressed size.
result = result[:len(codecHeaderSnappy)+len(compressed)]
return result, nil
}
// diffVarintEncodeNoHeader encodes postings into diff+varint representation.
// It doesn't add any header to the output bytes.
// Length argument is expected number of postings, used for preallocating buffer.
func diffVarintEncodeNoHeader(p index.Postings, length int) ([]byte, error) {
buf := encoding.Encbuf{}
// This encoding uses around ~1 bytes per posting, but let's use
// conservative 1.25 bytes per posting to avoid extra allocations.
if length > 0 {
buf.B = make([]byte, 0, 5*length/4)
}
prev := storage.SeriesRef(0)
for p.Next() {
v := p.At()
if v < prev {
return nil, errors.Errorf("postings entries must be in increasing order, current: %d, previous: %d", v, prev)
}
// This is the 'diff' part -- compute difference from previous value.
buf.PutUvarint64(uint64(v - prev))
prev = v
}
if p.Err() != nil {
return nil, p.Err()
}
return buf.B, nil
}
// Creating 15 buckets from 1k to 32mb.
var snappyDecodePool = pool.MustNewBucketedPool[byte](1024, 32*1024*1024, 2, 0)
type closeablePostings interface {
index.Postings
close()
}
// alias returns true if given slices have the same both backing array.
// See: https://groups.google.com/g/golang-nuts/c/C6ufGl73Uzk.
func alias(x, y []byte) bool {
return cap(x) > 0 && cap(y) > 0 && &x[0:cap(x)][cap(x)-1] == &y[0:cap(y)][cap(y)-1]
}
// TODO(GiedriusS): remove for v1.0.
func diffVarintSnappyDecode(input []byte, disablePooling bool) (closeablePostings, error) {
if !isDiffVarintSnappyEncodedPostings(input) {
return nil, errors.New("header not found")
}
toFree := make([][]byte, 0, 2)
var dstBuf []byte
if !disablePooling {
if len, err := s2.DecodedLen(input[len(codecHeaderSnappy):]); err == nil {
if decodeBuf, err := snappyDecodePool.Get(len); err == nil && decodeBuf != nil {
dstBuf = *decodeBuf
toFree = append(toFree, dstBuf)
}
}
}
raw, err := s2.Decode(dstBuf, input[len(codecHeaderSnappy):])
if err != nil {
return nil, errors.Wrap(err, "snappy decode")
}
if !alias(raw, dstBuf) && !disablePooling {
toFree = append(toFree, raw)
}
return newDiffVarintPostings(raw, toFree), nil
}
func newDiffVarintPostings(input []byte, freeSlices [][]byte) *diffVarintPostings {
return &diffVarintPostings{freeSlices: freeSlices, buf: &encoding.Decbuf{B: input}}
}
// diffVarintPostings is an implementation of index.Postings based on diff+varint encoded data.
type diffVarintPostings struct {
buf *encoding.Decbuf
cur storage.SeriesRef
freeSlices [][]byte
}
func (it *diffVarintPostings) close() {
for i := range it.freeSlices {
snappyDecodePool.Put(&it.freeSlices[i])
}
}
func (it *diffVarintPostings) At() storage.SeriesRef {
return it.cur
}
func (it *diffVarintPostings) Next() bool {
if it.buf.Err() != nil || it.buf.Len() == 0 {
return false
}
val := it.buf.Uvarint64()
if it.buf.Err() != nil {
return false
}
it.cur = it.cur + storage.SeriesRef(val)
return true
}
func (it *diffVarintPostings) Seek(x storage.SeriesRef) bool {
if it.cur >= x {
return true
}
// We cannot do any search due to how values are stored,
// so we simply advance until we find the right value.
for it.Next() {
if it.At() >= x {
return true
}
}
return false
}
func (it *diffVarintPostings) Err() error {
return it.buf.Err()
}
func snappyStreamedEncode(postingsLength int, diffVarintPostings []byte) ([]byte, error) {
compressedBuf := bytes.NewBuffer(make([]byte, 0, estimateSnappyStreamSize(postingsLength)))
if n, err := compressedBuf.WriteString(codecHeaderStreamedSnappy); err != nil {
return nil, fmt.Errorf("writing streamed snappy header")
} else if n != len(codecHeaderStreamedSnappy) {
return nil, fmt.Errorf("short-write streamed snappy header")
}
sw, err := extsnappy.Compressor.Compress(compressedBuf)
if err != nil {
return nil, fmt.Errorf("creating snappy compressor: %w", err)
}
_, err = sw.Write(diffVarintPostings)
if err != nil {
return nil, err
}
if err := sw.Close(); err != nil {
return nil, errors.Wrap(err, "closing snappy stream writer")
}
return compressedBuf.Bytes(), nil
}
var crcTable = crc32.MakeTable(crc32.Castagnoli)
// crc implements the checksum specified in section 3 of
// https://github.com/google/snappy/blob/master/framing_format.txt
func crc(b []byte) uint32 {
c := crc32.Update(0, crcTable, b)
return c>>15 | c<<17 + 0xa282ead8
}