|
| 1 | +# Prometheus Compression |
| 2 | + |
| 3 | +Compression for data is in [tsdb/chunkenc](https://github.com/prometheus/prometheus/tree/master/tsdb/chunkenc). |
| 4 | + |
| 5 | +## Encode |
| 6 | + |
| 7 | +There is a `Appender` interface and two implementations, the second one is just a thread safe wrapper. |
| 8 | +The main logic is in [xorAppender.Append](https://github.com/prometheus/prometheus/blob/0703dae7cc4fcb1e051ab5fec89c47530e78c75a/tsdb/chunkenc/xor.go#L149) |
| 9 | +which contains the double delta logic for timestamp and deal with float64 in [xorAppender.writeVDelta](https://github.com/prometheus/prometheus/blob/0703dae7cc4fcb1e051ab5fec89c47530e78c75a/tsdb/chunkenc/xor.go#L206) |
| 10 | + |
| 11 | +```go |
| 12 | +// tsdb/chunenc/xor.go |
| 13 | + |
| 14 | +type xorAppender struct { |
| 15 | + b *bstream |
| 16 | + |
| 17 | + t int64 |
| 18 | + v float64 |
| 19 | + tDelta uint64 |
| 20 | + |
| 21 | + leading uint8 |
| 22 | + trailing uint8 |
| 23 | +} |
| 24 | + |
| 25 | +func (a *xorAppender) Append(t int64, v float64) { |
| 26 | + var tDelta uint64 |
| 27 | + num := binary.BigEndian.Uint16(a.b.bytes()) |
| 28 | + |
| 29 | + if num == 0 { |
| 30 | + buf := make([]byte, binary.MaxVarintLen64) |
| 31 | + for _, b := range buf[:binary.PutVarint(buf, t)] { |
| 32 | + a.b.writeByte(b) |
| 33 | + } |
| 34 | + a.b.writeBits(math.Float64bits(v), 64) |
| 35 | + |
| 36 | + } else if num == 1 { |
| 37 | + tDelta = uint64(t - a.t) |
| 38 | + |
| 39 | + buf := make([]byte, binary.MaxVarintLen64) |
| 40 | + for _, b := range buf[:binary.PutUvarint(buf, tDelta)] { |
| 41 | + a.b.writeByte(b) |
| 42 | + } |
| 43 | + |
| 44 | + a.writeVDelta(v) |
| 45 | + |
| 46 | + } else { |
| 47 | + tDelta = uint64(t - a.t) |
| 48 | + dod := int64(tDelta - a.tDelta) |
| 49 | + |
| 50 | + // Gorilla has a max resolution of seconds, Prometheus milliseconds. |
| 51 | + // Thus we use higher value range steps with larger bit size. |
| 52 | + switch { |
| 53 | + case dod == 0: |
| 54 | + a.b.writeBit(zero) |
| 55 | + case bitRange(dod, 14): |
| 56 | + a.b.writeBits(0x02, 2) // '10' |
| 57 | + a.b.writeBits(uint64(dod), 14) |
| 58 | + case bitRange(dod, 17): |
| 59 | + a.b.writeBits(0x06, 3) // '110' |
| 60 | + a.b.writeBits(uint64(dod), 17) |
| 61 | + case bitRange(dod, 20): |
| 62 | + a.b.writeBits(0x0e, 4) // '1110' |
| 63 | + a.b.writeBits(uint64(dod), 20) |
| 64 | + default: |
| 65 | + a.b.writeBits(0x0f, 4) // '1111' |
| 66 | + a.b.writeBits(uint64(dod), 64) |
| 67 | + } |
| 68 | + |
| 69 | + a.writeVDelta(v) |
| 70 | + } |
| 71 | + |
| 72 | + a.t = t |
| 73 | + a.v = v |
| 74 | + binary.BigEndian.PutUint16(a.b.bytes(), num+1) |
| 75 | + a.tDelta = tDelta |
| 76 | +} |
| 77 | + |
| 78 | +func (a *xorAppender) writeVDelta(v float64) { |
| 79 | + vDelta := math.Float64bits(v) ^ math.Float64bits(a.v) |
| 80 | + |
| 81 | + if vDelta == 0 { |
| 82 | + a.b.writeBit(zero) |
| 83 | + return |
| 84 | + } |
| 85 | + a.b.writeBit(one) |
| 86 | + |
| 87 | + leading := uint8(bits.LeadingZeros64(vDelta)) |
| 88 | + trailing := uint8(bits.TrailingZeros64(vDelta)) |
| 89 | + |
| 90 | + // Clamp number of leading zeros to avoid overflow when encoding. |
| 91 | + if leading >= 32 { |
| 92 | + leading = 31 |
| 93 | + } |
| 94 | + |
| 95 | + if a.leading != 0xff && leading >= a.leading && trailing >= a.trailing { |
| 96 | + a.b.writeBit(zero) |
| 97 | + a.b.writeBits(vDelta>>a.trailing, 64-int(a.leading)-int(a.trailing)) |
| 98 | + } else { |
| 99 | + a.leading, a.trailing = leading, trailing |
| 100 | + |
| 101 | + a.b.writeBit(one) |
| 102 | + a.b.writeBits(uint64(leading), 5) |
| 103 | + |
| 104 | + // Note that if leading == trailing == 0, then sigbits == 64. But that value doesn't actually fit into the 6 bits we have. |
| 105 | + // Luckily, we never need to encode 0 significant bits, since that would put us in the other case (vdelta == 0). |
| 106 | + // So instead we write out a 0 and adjust it back to 64 on unpacking. |
| 107 | + sigbits := 64 - leading - trailing |
| 108 | + a.b.writeBits(uint64(sigbits), 6) |
| 109 | + a.b.writeBits(vDelta>>trailing, int(sigbits)) |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
0 commit comments