Compact TSDB head chunks after being cut, to reduce inuse memory

Signed-off-by: Marco Pracucci <marco@pracucci.com>
pull/6687/head
Marco Pracucci 5 years ago
parent 492414542e
commit 0703dae7cc
No known key found for this signature in database
GPG Key ID: 74C1BD403D2DF9B5

@ -49,6 +49,7 @@ type Chunk interface {
// be re-used or a new iterator can be allocated. // be re-used or a new iterator can be allocated.
Iterator(Iterator) Iterator Iterator(Iterator) Iterator
NumSamples() int NumSamples() int
Compact()
} }
// Appender adds sample pairs to a chunk. // Appender adds sample pairs to a chunk.

@ -49,6 +49,10 @@ import (
"math/bits" "math/bits"
) )
const (
chunkCompactCapacityThreshold = 32
)
// XORChunk holds XOR encoded sample data. // XORChunk holds XOR encoded sample data.
type XORChunk struct { type XORChunk struct {
b bstream b bstream
@ -75,6 +79,14 @@ func (c *XORChunk) NumSamples() int {
return int(binary.BigEndian.Uint16(c.Bytes())) return int(binary.BigEndian.Uint16(c.Bytes()))
} }
func (c *XORChunk) Compact() {
if l := len(c.b.stream); cap(c.b.stream) > l+chunkCompactCapacityThreshold {
buf := make([]byte, l)
copy(buf, c.b.stream)
c.b.stream = buf
}
}
// Appender implements the Chunk interface. // Appender implements the Chunk interface.
func (c *XORChunk) Appender() (Appender, error) { func (c *XORChunk) Appender() (Appender, error) {
it := c.iterator(nil) it := c.iterator(nil)

@ -1691,6 +1691,11 @@ func (s *memSeries) cut(mint int64) *memChunk {
s.chunks = append(s.chunks, c) s.chunks = append(s.chunks, c)
s.headChunk = c s.headChunk = c
// Remove exceeding capacity from the previous chunk byte slice to save memory.
if l := len(s.chunks); l > 1 {
s.chunks[l-2].chunk.Compact()
}
// Set upper bound on when the next chunk must be started. An earlier timestamp // Set upper bound on when the next chunk must be started. An earlier timestamp
// may be chosen dynamically at a later point. // may be chosen dynamically at a later point.
s.nextAt = rangeForTimestamp(mint, s.chunkRange) s.nextAt = rangeForTimestamp(mint, s.chunkRange)

Loading…
Cancel
Save