Skip to content

Commit 18e05ba

Browse files
bborehamalvinlin123
authored andcommitted
MergeIterator: allocate less memory at first (cortexproject#4341)
* MergeIterator: allocate less memory at first We were allocating 24x the number of streams of batches, where each batch holds up to 12 samples. By allowing `c.batches` to reallocate when needed, we avoid the need to pre-allocate enough memory for all possible scenarios. * chunk_test: fix innacurate end time on chunks The `through` time is supposed to be the last time in the chunk, and having it one step higher was throwing off other tests and benchmarks. * MergeIterator benchmark: add more realistic sizes At 15-second scrape intervals a chunk covers 30 minutes, so 1,000 chunks is about three weeks, a highly un-representative test. Instant queries, such as those done by the ruler, will only fetch one chunk from each ingester. Signed-off-by: Bryan Boreham <[email protected]> Signed-off-by: Alvin Lin <[email protected]>
1 parent da68878 commit 18e05ba

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [CHANGE] Querier / ruler: Change `-querier.max-fetched-chunks-per-query` configuration to limit to maximum number of chunks that can be fetched in a single query. The number of chunks fetched by ingesters AND long-term storare combined should not exceed the value configured on `-querier.max-fetched-chunks-per-query`. #4260
66
* [ENHANCEMENT] Add timeout for waiting on compactor to become ACTIVE in the ring. #4262
7+
* [ENHANCEMENT] Reduce memory used by streaming queries, particularly in ruler. #4341
78
* [BUGFIX] HA Tracker: when cleaning up obsolete elected replicas from KV store, tracker didn't update number of cluster per user correctly. #4336
89
* [FEATURE] Add shuffle sharding grouper and planner within compactor to allow further work towards parallelizing compaction #4318
910

pkg/querier/batch/batch_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func BenchmarkNewChunkMergeIterator_CreateAndIterate(b *testing.B) {
2727
{numChunks: 1000, numSamplesPerChunk: 100, duplicationFactor: 3, enc: promchunk.DoubleDelta},
2828
{numChunks: 1000, numSamplesPerChunk: 100, duplicationFactor: 1, enc: promchunk.PrometheusXorChunk},
2929
{numChunks: 1000, numSamplesPerChunk: 100, duplicationFactor: 3, enc: promchunk.PrometheusXorChunk},
30+
{numChunks: 100, numSamplesPerChunk: 100, duplicationFactor: 1, enc: promchunk.PrometheusXorChunk},
31+
{numChunks: 100, numSamplesPerChunk: 100, duplicationFactor: 3, enc: promchunk.PrometheusXorChunk},
32+
{numChunks: 1, numSamplesPerChunk: 100, duplicationFactor: 1, enc: promchunk.PrometheusXorChunk},
33+
{numChunks: 1, numSamplesPerChunk: 100, duplicationFactor: 3, enc: promchunk.PrometheusXorChunk},
3034
}
3135

3236
for _, scenario := range scenarios {

pkg/querier/batch/chunk_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func mkChunk(t require.TestingT, from model.Time, points int, enc promchunk.Enco
5959
require.Nil(t, npc)
6060
ts = ts.Add(step)
6161
}
62+
ts = ts.Add(-step) // undo the add that we did just before exiting the loop
6263
return chunk.NewChunk(userID, fp, metric, pc, from, ts)
6364
}
6465

pkg/querier/batch/merge.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func newMergeIterator(cs []GenericChunk) *mergeIterator {
3131
c := &mergeIterator{
3232
its: its,
3333
h: make(iteratorHeap, 0, len(its)),
34-
batches: make(batchStream, 0, len(its)*2*promchunk.BatchSize),
35-
batchesBuf: make(batchStream, len(its)*2*promchunk.BatchSize),
34+
batches: make(batchStream, 0, len(its)),
35+
batchesBuf: make(batchStream, len(its)),
3636
}
3737

3838
for _, iter := range c.its {
@@ -112,8 +112,7 @@ func (c *mergeIterator) buildNextBatch(size int) bool {
112112
for len(c.h) > 0 && (len(c.batches) == 0 || c.nextBatchEndTime() >= c.h[0].AtTime()) {
113113
c.nextBatchBuf[0] = c.h[0].Batch()
114114
c.batchesBuf = mergeStreams(c.batches, c.nextBatchBuf[:], c.batchesBuf, size)
115-
copy(c.batches[:len(c.batchesBuf)], c.batchesBuf)
116-
c.batches = c.batches[:len(c.batchesBuf)]
115+
c.batches = append(c.batches[:0], c.batchesBuf...)
117116

118117
if c.h[0].Next(size) {
119118
heap.Fix(&c.h, 0)

0 commit comments

Comments
 (0)