lib/storage: idle rawRowsShard buffers are never released, heap grows with the number of partitions written
Describe the bug
This looks like #2546 (closed in 2023), which is still reproducible on v1.115.0 and in current master
(lib/storage/raw_row.go): appendRawRowsToFlush keeps the buffer capacity with rrs.rows = rrs.rows[:0].
Retention is not a workaround for us: we backfill 13 years of satellite telemetry and recompute downsampled series over it,
so a single process legitimately writes into ~160 monthly partitions.
Every partition keeps rawRowsShards with cgroup.AvailableCPUs() shards. The first write to a shard allocates
newRawRows() (8 MiB). After the periodic flush, appendRawRowsToFlush truncates the slice with
rrs.rows = rrs.rows[:0], so the 8 MiB capacity stays allocated until the partition is closed (process restart).
When a single process writes samples into many historical monthly partitions (backfill, or recomputing downsampled
series over many years), every touched partition pins up to numShards * 8 MiB. On a 32-core host that is 256 MiB
per partition and the heap grows without bound while old partitions receive no more samples.
Observed on v1.115.0 (the same code is in master lib/storage/raw_row.go):
- heap profile:
lib/storage.newRawRows17.2 GB of 18.5 GB in-use (93%), pathprometheusimport.InsertHandler -> InsertCtx.FlushBufs -> newRawRows vm_cache_size_bytestotal 1.9 GB,vm_pending_rows0- process restart releases the memory immediately (20.5 GB -> 0.2 GB anon)
To Reproduce
Run with --cpus 4, import 5,000 samples per request, 8 requests per month for 60 consecutive months, wait 40s,
then GET /debug/pprof/heap?gc=1:
| build | heap in-use after GC |
|---|---|
| v1.115.0 | 1,935 MiB (60 partitions x 4 shards x 8 MiB) |
| with the patch below | 15 MiB |
Sample counts are identical (count_over_time with nocache=1).
Proposed fix
(The fix discussed in #2546 was releasing the slice after flush; releasing it only after the shard stayed empty for a while avoids re-allocations for the partition that receives the live stream.)
Release the buffer of a shard that stayed empty for maxIdleFlushes (15) consecutive flush ticks (~30s).
The delay keeps bursty ingestion into the current partition from re-allocating on every tick: 175k rows written in
bursts over 20s allocated 67 MiB with the patch vs 80 MiB without.
Patch against v1.115.0 (lib/storage tests pass):
diff --git a/lib/storage/partition.go b/lib/storage/partition.go
index 38eb147..ef29523 100644
--- a/lib/storage/partition.go
+++ b/lib/storage/partition.go
@@ -512,8 +512,19 @@ type rawRowsShardNopad struct {
mu sync.Mutex
rows []rawRow
+
+ // idleFlushes is the number of consecutive flush ticks the shard stayed empty.
+ // The buffer is released after maxIdleFlushes such ticks - see appendRawRowsToFlush.
+ idleFlushes int
}
+// maxIdleFlushes is the number of consecutive empty flush ticks after which an idle shard releases its buffer.
+//
+// Without the release every partition, which ever received samples, pins numShards*maxRawRowsPerShard*sizeof(rawRow)
+// bytes until the process restarts. Backfilling many historical months grows the heap without bound this way.
+// The delay avoids re-allocating the buffer on every tick for partitions with bursty ingestion.
+const maxIdleFlushes = 15
+
type rawRowsShard struct {
rawRowsShardNopad
@@ -533,6 +544,7 @@ func (rrs *rawRowsShard) addRows(rows []rawRow) ([]rawRow, []rawRow) {
var rowsToFlush []rawRow
rrs.mu.Lock()
+ rrs.idleFlushes = 0
if cap(rrs.rows) == 0 {
rrs.rows = newRawRows()
}
@@ -1144,8 +1156,15 @@ func (rrs *rawRowsShard) appendRawRowsToFlush(dst [][]rawRow, currentTimeMs int6
// Slow path - move rrs.rows to dst.
rrs.mu.Lock()
- dst = appendRawRowss(dst, rrs.rows)
- rrs.rows = rrs.rows[:0]
+ if len(rrs.rows) == 0 && !isFinal {
+ rrs.idleFlushes++
+ if rrs.idleFlushes >= maxIdleFlushes {
+ rrs.rows = nil
+ }
+ } else {
+ dst = appendRawRowss(dst, rrs.rows)
+ rrs.rows = rrs.rows[:0]
+ }
rrs.mu.Unlock()
return dstVersion
victoria-metrics v1.115.0, single-node, linux/amd64.
Source: VictoriaMetrics/VictoriaMetrics