Skip to content

Commit

Permalink
make chunkRefMap free threshold relative to size
Browse files Browse the repository at this point in the history
Signed-off-by: Mauro Stettler <mauro.stettler@gmail.com>
  • Loading branch information
replay committed Feb 8, 2022
1 parent 20f3746 commit 6448d4b
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions tsdb/chunks/chunk_write_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

const (
chunkRefMapFreeThreshold = 10
chunkRefMapMinFreeInterval = 10 * time.Minute
)

Expand All @@ -44,10 +43,11 @@ type chunkWriteJob struct {
type chunkWriteQueue struct {
jobs chan chunkWriteJob

chunkRefMapMtx sync.RWMutex
chunkRefMap map[ChunkDiskMapperRef]chunkenc.Chunk
chunkRefMapPeak int
chunkRefMapLastFree time.Time
chunkRefMapMtx sync.RWMutex
chunkRefMap map[ChunkDiskMapperRef]chunkenc.Chunk
chunkRefMapPeak int
chunkRefMapFreeThres int
chunkRefMapLastFree time.Time

isRunningMtx sync.Mutex // Protects the isRunning property.
isRunning bool // Used to prevent that new jobs get added to the queue when the chan is already closed.
Expand Down Expand Up @@ -76,10 +76,11 @@ func newChunkWriteQueue(reg prometheus.Registerer, size int, writeChunk writeChu
)

q := &chunkWriteQueue{
jobs: make(chan chunkWriteJob, size),
chunkRefMap: make(map[ChunkDiskMapperRef]chunkenc.Chunk),
chunkRefMapLastFree: time.Now(),
writeChunk: writeChunk,
jobs: make(chan chunkWriteJob, size),
chunkRefMap: make(map[ChunkDiskMapperRef]chunkenc.Chunk),
chunkRefMapFreeThres: size / 10,
chunkRefMapLastFree: time.Now(),
writeChunk: writeChunk,

adds: counters.WithLabelValues("add"),
gets: counters.WithLabelValues("get"),
Expand Down Expand Up @@ -122,7 +123,7 @@ func (c *chunkWriteQueue) processJob(job chunkWriteJob) {

c.completed.Inc()

if len(c.chunkRefMap) == 0 && c.chunkRefMapPeak > chunkRefMapFreeThreshold && time.Since(c.chunkRefMapLastFree) > chunkRefMapMinFreeInterval {
if len(c.chunkRefMap) == 0 && c.chunkRefMapPeak > c.chunkRefMapFreeThres && time.Since(c.chunkRefMapLastFree) > chunkRefMapMinFreeInterval {
// Re-initialize the chunk ref map to half of the peak size that was in use since the last re-init event.
// By setting it to half of the peak we try to minimize the number of allocations required for a "normal" usage
// while ensuring that if its usage has decreased we shrink it over time.
Expand Down

0 comments on commit 6448d4b

Please sign in to comment.