Skip to content

Commit 20f3746

Browse files
committed
add time factor
Signed-off-by: Mauro Stettler <[email protected]>
1 parent 8e3bf56 commit 20f3746

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

tsdb/chunks/chunk_write_queue.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ package chunks
1616
import (
1717
"errors"
1818
"sync"
19+
"time"
1920

2021
"github.com/prometheus/client_golang/prometheus"
2122

2223
"github.com/prometheus/prometheus/tsdb/chunkenc"
2324
)
2425

25-
const chunkRefMapFreeThreshold = 10
26+
const (
27+
chunkRefMapFreeThreshold = 10
28+
chunkRefMapMinFreeInterval = 10 * time.Minute
29+
)
2630

2731
type chunkWriteJob struct {
2832
cutFile bool
@@ -40,9 +44,10 @@ type chunkWriteJob struct {
4044
type chunkWriteQueue struct {
4145
jobs chan chunkWriteJob
4246

43-
chunkRefMapMtx sync.RWMutex
44-
chunkRefMap map[ChunkDiskMapperRef]chunkenc.Chunk
45-
chunkRefMapPeak int
47+
chunkRefMapMtx sync.RWMutex
48+
chunkRefMap map[ChunkDiskMapperRef]chunkenc.Chunk
49+
chunkRefMapPeak int
50+
chunkRefMapLastFree time.Time
4651

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

7378
q := &chunkWriteQueue{
74-
jobs: make(chan chunkWriteJob, size),
75-
chunkRefMap: make(map[ChunkDiskMapperRef]chunkenc.Chunk),
76-
writeChunk: writeChunk,
79+
jobs: make(chan chunkWriteJob, size),
80+
chunkRefMap: make(map[ChunkDiskMapperRef]chunkenc.Chunk),
81+
chunkRefMapLastFree: time.Now(),
82+
writeChunk: writeChunk,
7783

7884
adds: counters.WithLabelValues("add"),
7985
gets: counters.WithLabelValues("get"),
@@ -116,12 +122,13 @@ func (c *chunkWriteQueue) processJob(job chunkWriteJob) {
116122

117123
c.completed.Inc()
118124

119-
if len(c.chunkRefMap) == 0 && c.chunkRefMapPeak > chunkRefMapFreeThreshold {
125+
if len(c.chunkRefMap) == 0 && c.chunkRefMapPeak > chunkRefMapFreeThreshold && time.Since(c.chunkRefMapLastFree) > chunkRefMapMinFreeInterval {
120126
// Re-initialize the chunk ref map to half of the peak size that was in use since the last re-init event.
121127
// By setting it to half of the peak we try to minimize the number of allocations required for a "normal" usage
122128
// while ensuring that if its usage has decreased we shrink it over time.
123129
c.chunkRefMap = make(map[ChunkDiskMapperRef]chunkenc.Chunk, c.chunkRefMapPeak/2)
124130
c.chunkRefMapPeak = 0
131+
c.chunkRefMapLastFree = time.Now()
125132
}
126133
}
127134

0 commit comments

Comments
 (0)