refactor(promql): simplify native execution memory estimates

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
discord9
2026-10-21 13:47:15 +08:00
parent 3630ba0488
commit 43856a7dbf
5 changed files with 658 additions and 1337 deletions
@@ -80,18 +80,10 @@ impl FloatHistogram {
})
}
/// The bytes this histogram's own heap buffers hold, by capacity.
/// Estimates the heap held by the bucket lists, custom bounds and preserved span layout.
///
/// Everything a histogram keeps besides its scalars is on the heap: the bucket lists of both
/// sides, the custom bounds, and the span layout of a histogram read from a view, with the
/// `[Vec; 2]` array that holds the two layout vectors. No scalar of the struct shows any of it:
/// a histogram of 4096 positive buckets is 64 KiB of buckets, and a window that retains one
/// histogram per grid point retains that much per point.
///
/// The layout array is stored inline in the histogram, so a ledger charging
/// `size_of::<FloatHistogram>()` per slot already pays for those 48 bytes. Counting the whole
/// footprint as one figure here costs a header of over-estimate on a reservation, against a
/// payload of tens of kilobytes that would otherwise be missing from it entirely.
/// A histogram can own tens of kilobytes of buckets that its struct size does not include.
/// Capacity slack and small header overestimates are acceptable for window reservations.
pub(crate) fn heap_bytes(&self) -> usize {
fn buckets(side: &Buckets) -> usize {
side.capacity() * std::mem::size_of::<(i32, f64)>()
File diff suppressed because it is too large Load Diff
+207 -511
View File
@@ -34,11 +34,10 @@ use datatypes::series::time::{EvalGrid, GridRange, GridSpan, GridSpec, TimeAxis}
use datatypes::series::{Label, MetricName, SeriesOrdering};
use super::{
ChargedAllocations, Emitted, History, NativeChunk, NativeExecutionState, NativeExecutor,
NodeBuffer, NodeRuntime, QueuedCharge, Run, absorb_rebuilds, label_bytes, map_chunk, raw_bytes,
Emitted, NativeChunk, NativeExecutionState, NativeExecutor, NodeBuffer, NodeRuntime,
absorb_rebuilds, map_chunk, raw_bytes,
};
use crate::evaluated::EvaluatedChunk;
use crate::grid_series::{LabelSet, label_set};
use crate::map::MathOp;
use crate::native::metrics::{NodeMetric, NodeMetrics, NodePath};
use crate::native::plan::{
@@ -345,7 +344,7 @@ fn raw_shared_histogram_chunk(
.unwrap()
}
/// A source node's runtime: the queue, its accounting and the reservation the two are held for.
/// A source node's runtime: the queue and the reservation it is held for.
fn source_runtime(pool: &Arc<dyn MemoryPool>) -> NodeRuntime {
NodeRuntime {
buffer: NodeBuffer::new(
@@ -354,7 +353,6 @@ fn source_runtime(pool: &Arc<dyn MemoryPool>) -> NodeRuntime {
pool,
),
queued: VecDeque::new(),
queued_charge: QueuedCharge::default(),
window: None,
map: None,
emitted: None,
@@ -1052,6 +1050,54 @@ fn an_error_releases_the_reservation() {
assert_eq!(pool.reserved(), 0);
}
/// A chunk the pool cannot admit fails the execution and gives back everything it already held.
///
/// The window holds the first chunk's samples, so the second one is refused against a reservation
/// that is already standing: the failure has to hand that reservation back too, rather than leaving
/// the pool full for as long as the caller keeps the stopped executor alive.
#[test]
fn a_pool_that_cannot_admit_a_chunk_releases_what_it_held() {
let tree = NativeTree::try_new(window(input(), grid(0, 60_000, 4))).unwrap();
let schema = raw_schema();
let set = ExecutionPlanMetricsSet::new();
// Room for the first chunk and what the window keeps of it, not for the second one.
let pool = bounded_pool(16 << 10);
let mut execution = executor(&tree, Arc::clone(&pool), &set);
execution
.push_raw(raw_chunk(&schema, &[0], &[1.0], false))
.unwrap();
drop(execution.next_chunk().unwrap());
let held = pool.reserved();
assert!(held > 0, "the open series has to be reserved for");
// A hundred thousand samples are hundreds of kilobytes whatever the pool already holds.
let timestamps: Vec<i64> = (0..100_000).collect();
let values = vec![1.0; 100_000];
let error = execution
.push_raw(raw_chunk(&schema, &timestamps, &values, true))
.expect_err("a chunk larger than the pool was admitted");
assert!(
matches!(error, DataFusionError::ResourcesExhausted(_)),
"{error}"
);
// The chunk never entered the queue, and everything the execution held went back to the pool
// while the executor is still alive.
assert_eq!(execution.state().reserved_bytes(), 0);
assert_eq!(pool.reserved(), 0);
let current = NodePath::root().metric_name(NodeMetric::CURRENT_RESERVED_BYTES);
assert_eq!(gauge(&set, &current), 0);
let peak = NodePath::root().partitioned_metric_name(NodeMetric::PEAK_RESERVED_BYTES, 0);
assert!(gauge(&set, &peak) >= held);
// A stopped execution is not resumed.
let again = execution.next_chunk().unwrap_err();
assert!(
again.to_string().contains("stopped after an earlier error"),
"{again}"
);
}
/// A map that drops the metric name cannot hand two series on under one label set.
///
/// `a{job="x"}` and `b{job="x"}` arrive whole, so each says it is the last chunk of its own series,
@@ -1411,6 +1457,71 @@ fn two_open_series_that_share_a_label_set_are_accepted_when_no_position_collides
);
}
/// A `NaN` is a value and a missing sample is not, so only the `NaN` occupies its position.
///
/// `abs` drops the metric name, so the two series below are one label set to the map, which is where
/// the two are read against each other. A position holding no sample is a position the series never
/// claimed, and another series may fill it; a position holding a `NaN` is decided and holds a value,
/// like any other, so a second value there is the collision no merge resolves.
#[test]
fn a_nan_occupies_its_position_where_a_missing_sample_does_not() {
let map_grid = grid(0, 60_000, 1);
let tree = || {
NativeTree::try_new(map(
evaluated_input_with_ordering(Arc::clone(&map_grid), SeriesOrdering::Contiguous),
vec![MathOp::Abs],
))
.unwrap()
};
let schema = evaluated_schema(Arc::clone(&map_grid));
// The first series' sample at position 0, the second series' sample there, and whether the two
// can share the label set `abs` leaves them.
let cases = [
(vec![Some(f64::NAN)], vec![Some(1.0)], false),
(vec![Some(1.0)], vec![Some(f64::NAN)], false),
(vec![Some(f64::NAN)], vec![None], true),
(vec![Some(1.0)], vec![None], true),
(vec![None], vec![Some(f64::NAN)], true),
];
// Both chunks are open, so the label set is still open when the second series arrives: a closed
// set refuses every second series, and this test is about what occupies a position.
let drain = |execution: &mut NativeExecutor| -> Result<(), DataFusionError> {
while execution.next_chunk()?.is_some() {}
Ok(())
};
for (first, second, accepted) in cases {
let mut execution = executor(&tree(), pool(), &ExecutionPlanMetricsSet::new());
execution
.push_evaluated(evaluated_chunk_of(
&schema,
named_metric("a", "x"),
0,
first.clone(),
false,
))
.unwrap();
drain(&mut execution).expect("the first open chunk is handed on");
execution
.push_evaluated(evaluated_chunk_of(
&schema,
named_metric("b", "x"),
0,
second.clone(),
false,
))
.unwrap();
let outcome = drain(&mut execution);
assert_eq!(
outcome.is_ok(),
accepted,
"{first:?} and {second:?} were {}: {:?}",
if accepted { "refused" } else { "handed on" },
outcome.err(),
);
}
}
/// One series arriving in pieces is not two series: a chunk that continues where its label set
/// ended is handed on.
///
@@ -1466,15 +1577,12 @@ fn a_series_that_continues_where_it_ended_is_not_a_second_series() {
/// A window reserves what it retains, not what it has consumed.
///
/// A long series is fed one sample per chunk to a pool with a fixed budget, pulling and dropping
/// the result of every chunk and closing only on the last one. The same one-sample-per-chunk input
/// leaves the evaluator's storage at the same size however many chunks it arrives in, so the
/// reservation reaches the same figure whether the data is split into 64 chunks or into 1024.
///
/// What the reservation has to be is not left to a constant: after every pull it is compared with
/// what the same window's evaluator says it retains and what the label sets it handed out hold. A
/// figure that merely did not grow with the chunk count would fail that, as would one that let the
/// emitted table go.
/// A long series is fed one sample per chunk to a pool with a fixed budget, pulling and dropping the
/// result of every chunk and closing only on the last one. The same one-sample-per-chunk input
/// leaves the evaluator's storage at the same size however many chunks it arrives in, so the peak
/// reservation is the same whether the data is split into 64 chunks or into 1024, and it stays a
/// small constant rather than the running total of what arrived. A figure charged per chunk
/// consumed would grow with the chunk count and exhaust the budget the first chunks fit in.
#[test]
fn a_window_reserves_what_it_retains_not_what_it_consumed() {
// A 1 ms window on a 1 ms grid, so every chunk's sample is out of the window of the next one.
@@ -1507,23 +1615,20 @@ fn a_window_reserves_what_it_retains_not_what_it_consumed() {
});
// The output of every chunk is dropped: a consumer reading the series as it arrives
// never holds it, and neither may the node.
let pulled = execution.next_chunk().unwrap_or_else(|error| {
drop(execution.next_chunk().unwrap_or_else(|error| {
panic!("chunk {index} of the {count}-chunk series did not fit: {error}")
});
drop(pulled);
}));
let held = execution.state().reserved_bytes();
let (retained, emitted) = execution.state().window_charge(&NodePath::root());
assert_eq!(
held,
retained.saturating_add(emitted),
"chunk {index} of {count} holds {held} bytes, but the window retains {retained} \
and handed out {emitted}"
pool.reserved(),
"chunk {index} of {count}: the state and the pool disagree on what is held"
);
assert!(
held <= (4 << 10),
held <= (8 << 10),
"chunk {index} of {count} holds {held} bytes of a 1 ms window over one sample"
);
peak = peak.max(held).max(pool.reserved());
peak = peak.max(held);
}
peak
};
@@ -1538,11 +1643,9 @@ fn a_window_reserves_what_it_retains_not_what_it_consumed() {
/// A window's retention is per field, as the evaluator's storage is.
///
/// The same one-sample-per-chunk input goes to a window over one float field, over two and over
/// three, and the reservation is compared with what each window retains after every pull. Every
/// field costs its own timestamps, values and row slot, so the figure grows by the same amount per
/// field; a figure counting one timestamp and one float per row would not. The widths are compared
/// by their increments rather than as a multiple of one: the identity of the open series is part of
/// the reservation and is counted once however many fields the series carries.
/// three. Every field costs its own timestamps, values and row slot, so the reservation a window
/// reaches grows with the width of the series; a figure counting one timestamp and one float per
/// row, or one field per series, would not.
#[test]
fn a_window_retains_the_samples_of_every_field() {
// The most the window ever retained, over the same input and the same short window.
@@ -1561,31 +1664,20 @@ fn a_window_retains_the_samples_of_every_field() {
))
.unwrap();
drop(execution.next_chunk().unwrap());
let (retained, emitted) = execution.state().window_charge(&NodePath::root());
let held = execution.state().reserved_bytes();
assert_eq!(
held,
retained.saturating_add(emitted),
"{width} field(s), chunk {index} of {count}: {held} bytes reserved, but the \
window retains {retained} and handed out {emitted}"
);
peak = peak.max(retained);
peak = peak.max(execution.state().reserved_bytes());
}
peak
};
let (one, two, three) = (peak(1), peak(2), peak(3));
assert!(
two > one,
"a window over two fields retained {two} bytes, no more than the {one} bytes a window over \
one field retains"
"a window over two fields reserved {two} bytes, no more than the {one} bytes a window over \
one field reserves"
);
assert_eq!(
two - one,
three - two,
"a third field costs {} bytes where a second cost {}: the retention is not one field's \
storage per field",
three - two,
two - one
assert!(
three > two,
"a window over three fields reserved {three} bytes, no more than the {two} bytes a window \
over two fields reserves"
);
}
@@ -1606,7 +1698,8 @@ fn a_chunk_with_no_output_keeps_charging_the_emitted_table() {
))
.unwrap();
let schema = raw_schema();
let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new());
let pool = pool();
let mut execution = executor(&tree, Arc::clone(&pool), &ExecutionPlanMetricsSet::new());
execution
.push_raw(raw_chunk(&schema, &[0, 60_000], &[1.0, 2.0], false))
@@ -1617,16 +1710,12 @@ fn a_chunk_with_no_output_keeps_charging_the_emitted_table() {
.expect("the first chunk decided both points");
assert_eq!(sample_values(&first), vec![Some(1.0), Some(2.0)]);
drop(first);
let (retained, emitted) = execution.state().window_charge(&NodePath::root());
let after_output = execution.state().reserved_bytes();
assert!(
emitted > 0,
after_output > 0,
"the window handed out a position, which it has to hold"
);
assert_eq!(
execution.state().reserved_bytes(),
retained.saturating_add(emitted),
"the reservation does not cover the {emitted} bytes the window handed out"
);
assert_eq!(after_output, pool.reserved());
// Both points are decided, so this chunk decides nothing and the window yields no chunk.
execution
@@ -1636,16 +1725,11 @@ fn a_chunk_with_no_output_keeps_charging_the_emitted_table() {
execution.next_chunk().unwrap().is_none(),
"a chunk after every point was decided produced a chunk"
);
let (retained, emitted) = execution.state().window_charge(&NodePath::root());
assert!(
emitted > 0,
"the window's emitted table emptied itself between chunks"
);
assert_eq!(
execution.state().reserved_bytes(),
retained.saturating_add(emitted),
"a chunk with no output released the {emitted} bytes the emitted table still holds"
execution.state().reserved_bytes() > 0,
"a chunk with no output released the bytes the emitted table still holds"
);
assert_eq!(execution.state().reserved_bytes(), pool.reserved());
}
/// A window cannot run over an interleaved input, and the runtime says so before reading a chunk.
@@ -1731,52 +1815,6 @@ fn an_interleaved_input_is_refused_without_a_window() {
assert!(message.contains("root.input"), "{message}");
}
/// The charge for the label sets a node has handed out does not depend on how it is read.
///
/// The table only grows, so the figure is folded in as each entry changes rather than walked after
/// every output. Folding it wrong would show up as a running total that drifts from the same figure
/// recounted in full, so both are compared after every chunk: distinct label sets add entries, and
/// later chunks of one set grow its run vectors and the table's own capacity.
#[test]
fn the_emitted_charge_is_the_same_folded_in_as_walked() {
let grid = grid(0, 60_000, 4);
let tree =
NativeTree::try_new(map(evaluated_input(Arc::clone(&grid)), vec![MathOp::Abs])).unwrap();
let schema = evaluated_schema(Arc::clone(&grid));
let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new());
let chunks = [
("a", "x", 0, vec![Some(-1.0), None, Some(-2.0), None], false),
("a", "x", 0, vec![None, Some(-3.0), None, None], true),
("b", "y", 0, vec![None, None, None, Some(-4.0)], true),
(
"c",
"z",
0,
vec![Some(-5.0), Some(-6.0), Some(-7.0), Some(-8.0)],
true,
),
];
for (name, job, start, values, is_last) in chunks {
execution
.push_evaluated(evaluated_chunk_of(
&schema,
named_metric(name, job),
start,
values,
is_last,
))
.unwrap();
while execution.next_chunk().unwrap().is_some() {}
let (folded, walked) = execution.state().emitted_charge_pair(&NodePath::root());
assert!(folded > 0, "the node handed out nothing to charge for");
assert_eq!(
folded, walked,
"the folded charge of {folded} bytes is not the {walked} bytes the table holds"
);
}
}
/// A series that produced values under a label set, followed by one that produced none under the
/// same label set, is not a collision: the second contributes no sample, so the answer is just the
/// first one's values.
@@ -2175,14 +2213,12 @@ fn an_input_that_ends_mid_series_is_reported_without_a_window() {
}
}
/// A window over native histograms reserves what it retains, payload included, and the label-set
/// ledger it charges beside that agrees with a full walk of the table.
/// A window over native histograms reserves what it retains, payload included.
///
/// The chunk accounting walks the samples a chunk holds instead of charging one float per field and
/// row, so a histogram field -- which holds no float at all -- reaches both figures as the table its
/// indices point into. Neither may drift from what the node says it holds: the reservation is the
/// retained bytes plus the emitted ones, and the running total of the emitted table is the same
/// figure recounted in full.
/// A chunk is estimated from the samples it holds rather than from one float per field and row, so a
/// histogram field -- which holds no float at all -- reaches the figure as the table its indices point
/// into. A window over such a series therefore reserves for the payload it keeps, and a one-sample
/// histogram chunk is worth more than a one-sample float chunk of the same shape.
#[test]
fn a_window_over_histograms_reserves_what_it_retains() {
let out = grid(0, 10_000, 2);
@@ -2195,383 +2231,120 @@ fn a_window_over_histograms_reserves_what_it_retains() {
))
.unwrap();
let schema = raw_schema();
let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new());
let pool = pool();
let mut execution = executor(&tree, Arc::clone(&pool), &ExecutionPlanMetricsSet::new());
for (timestamp, is_last) in [(0, false), (10_000, true)] {
execution
.push_raw(raw_histogram_chunk(&schema, &[timestamp], 2, is_last))
.unwrap();
while execution.next_chunk().unwrap().is_some() {}
let (retained, emitted) = execution.state().window_charge(&NodePath::root());
assert!(
emitted > 0,
"the window handed nothing out, so the ledger below charges for nothing"
);
assert_eq!(
execution.state().reserved_bytes(),
retained.saturating_add(emitted),
"the reservation is not what the window retains ({retained} bytes) plus what it handed \
out ({emitted} bytes)",
pool.reserved(),
"the state and the pool disagree on what the window holds"
);
let (folded, walked) = execution.state().emitted_charge_pair(&NodePath::root());
assert_eq!(
folded, walked,
"the folded charge of {folded} bytes is not the {walked} bytes the table holds",
assert!(
pool.reserved() > 0,
"a window over a histogram series reserves for nothing"
);
}
}
/// One histogram table is charged once, however many chunks of the series reference it.
///
/// The chunks of one series can share the immutable table their histogram indices point into, and a
/// node holding both of them holds one table: the source charges its queue as one accounting pass,
/// which counts the table the first time it meets it and the address the `Arc` holds is what makes
/// two clones one table.
#[test]
fn a_histogram_table_is_charged_once_per_pass() {
let schema = raw_schema();
let mut builder = HistogramBatchBuilder::new();
let index = push_histogram(&mut builder, 2);
let table = builder.finish().unwrap();
let chunk = |timestamp: i64, is_last: bool| {
let samples =
Samples::try_from_histogram(UInt32Array::from(vec![Some(index)]), Arc::clone(&table))
.unwrap();
RawSeriesChunk::try_new(
&schema,
Buffer::from_slice_ref(b"series-a"),
Some(metric()),
RawTimestamps::try_new(ScalarBuffer::from(vec![timestamp])).unwrap(),
SeriesFields::one(samples),
is_last,
)
.unwrap()
};
let (first, second) = (chunk(0, false), chunk(10_000, true));
let mut charged = ChargedAllocations::default();
let queue = raw_bytes(&first, &mut charged).saturating_add(raw_bytes(&second, &mut charged));
let alone = raw_bytes(&first, &mut ChargedAllocations::default())
.saturating_add(raw_bytes(&second, &mut ChargedAllocations::default()));
// The table the indices point into is what the field costs, so the same one-sample chunk is
// worth more as a histogram than as a float.
let histogram = raw_histogram_chunk(&schema, &[0], 2, true);
let float = raw_chunk(&schema, &[0], &[1.0], true);
assert!(
queue < alone,
"a queue of two chunks sharing one table is charged {queue} bytes, no less than the \
{alone} bytes they are charged apart: the table was counted twice",
);
// What the source reserves is exactly that one pass: it holds its queue and nothing else, and
// nothing has been pulled yet.
let tree = NativeTree::try_new(window(input(), grid(0, 10_000, 1))).unwrap();
let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new());
execution.push_raw(first).unwrap();
execution.push_raw(second).unwrap();
assert_eq!(
execution.state().reserved_bytes(),
queue,
"the queue of two chunks sharing one table was not charged as one pass",
raw_bytes(&histogram) > raw_bytes(&float),
"a histogram chunk is estimated at {} bytes, no more than the {} bytes of a float chunk of \
the same shape",
raw_bytes(&histogram),
raw_bytes(&float),
);
}
/// A queue of two chunks that share one table charges it once, and the running total is what one
/// pass over the queue counts.
/// A source reserves the estimate of every queued chunk, and gives each one back as it leaves.
///
/// The figure the node reserves while both chunks are queued is the incremental one: the table is
/// met by the first chunk that references it and is not charged again by the second. Charging it
/// per chunk fails the record check, and recounting the queue per enqueue fails the count of
/// chunks walked in the test below.
/// The figure is symmetric: what a chunk added when it entered is what comes out when it leaves, so a
/// queue that empties returns its reservation to the pool exactly. Two chunks of one series may share
/// one immutable histogram table, and each of them is charged for it: the estimate is per chunk, which
/// costs a bounded over-count of a shared table rather than a pass over the queue per operation.
#[test]
fn a_queued_allocation_is_charged_once() {
fn a_source_charges_its_queue_and_gives_it_back() {
let pool = pool();
let mut source = source_runtime(&pool);
let schema = raw_schema();
let mut builder = HistogramBatchBuilder::new();
let index = push_histogram(&mut builder, 2);
let table = builder.finish().unwrap();
let chunk = |timestamp: i64, is_last: bool| {
raw_shared_histogram_chunk(&schema, &table, index, timestamp, is_last)
};
let (first, second) = (chunk(0, false), chunk(10_000, true));
// What the two chunks cost charged apart, and what the second one costs on its own: the
// difference between the two is what the shared table is worth.
let apart = raw_bytes(&first, &mut ChargedAllocations::default())
.saturating_add(raw_bytes(&second, &mut ChargedAllocations::default()));
let second_alone = raw_bytes(&second, &mut ChargedAllocations::default());
let first = raw_shared_histogram_chunk(&schema, &table, index, 0, false);
let second = raw_shared_histogram_chunk(&schema, &table, index, 10_000, true);
let (first_bytes, second_bytes) = (raw_bytes(&first), raw_bytes(&second));
source.enqueue(NativeChunk::Raw(first)).unwrap();
let one = source.queued_charge.bytes;
assert_eq!(
one,
source.queued_bytes_recomputed(),
"one queued chunk is not charged what one pass over it counts"
);
assert_eq!(
source.buffer.held, one,
"the reservation is not the queue's figure"
);
assert_eq!(pool.reserved(), one);
assert_eq!(source.buffer.reservation.size(), first_bytes);
assert_eq!(pool.reserved(), first_bytes);
source.enqueue(NativeChunk::Raw(second)).unwrap();
let two = source.queued_charge.bytes;
let counted = *source
.queued_charge
.tables
.get(&(Arc::as_ptr(&table) as usize))
.expect("the queue did not count the table both chunks index into");
assert_eq!(
counted.chunks, 2,
"two queued chunks reference the table, but the queue counts {}",
counted.chunks
source.buffer.reservation.size(),
first_bytes + second_bytes,
"the queue is not the sum of the estimates it holds"
);
assert_eq!(
two,
source.queued_bytes_recomputed(),
"the running total is not what one pass over the queue counts"
);
assert_eq!(
two - one,
second_alone - counted.bytes,
"the chunk that entered charged the shared table a second time"
);
assert!(
two < apart,
"a queue of two chunks sharing one table is charged {two} bytes, no less than the {apart} \
bytes they are charged apart: the table was counted twice",
);
assert_eq!(source.buffer.held, two);
assert_eq!(pool.reserved(), two);
}
assert_eq!(pool.reserved(), first_bytes + second_bytes);
/// A shared table is given back with the last queued chunk that references it, not the first.
///
/// A leak here shows as a reservation that never returns: the first pop takes one reference away
/// and the table stays charged, the second takes the last one and the table goes with it.
#[test]
fn a_queued_allocation_is_given_back_with_its_last_chunk() {
let pool = pool();
let mut source = source_runtime(&pool);
let schema = raw_schema();
let mut builder = HistogramBatchBuilder::new();
let index = push_histogram(&mut builder, 2);
let table = builder.finish().unwrap();
let address = Arc::as_ptr(&table) as usize;
let chunk = |timestamp: i64, is_last: bool| {
raw_shared_histogram_chunk(&schema, &table, index, timestamp, is_last)
};
source.enqueue(NativeChunk::Raw(chunk(0, false))).unwrap();
source
.enqueue(NativeChunk::Raw(chunk(10_000, true)))
.unwrap();
assert_eq!(source.queued_charge.tables[&address].chunks, 2);
let first = source
let popped = source
.pop_source()
.unwrap()
.expect("the queue held two chunks");
assert!(!first.is_last());
assert!(!popped.is_last());
// One table, two queued chunks, two shares of its estimate: what leaves is what the chunk that
// left was charged, and the other chunk's share stays.
assert_eq!(
source.queued_charge.tables[&address].chunks, 1,
"the chunk left in the queue still references the table, so it is still charged"
source.buffer.reservation.size(),
second_bytes,
"the chunk that left did not give its estimate back"
);
assert!(
source.queued_charge.bytes > 0,
"the queue still holds a chunk, but charges for nothing"
);
assert_eq!(
source.queued_charge.bytes,
source.queued_bytes_recomputed(),
"the pop did not leave the running total at one pass over the queue"
);
assert_eq!(source.buffer.held, source.queued_charge.bytes);
assert_eq!(pool.reserved(), second_bytes);
source
.pop_source()
.unwrap()
.expect("the queue held two chunks");
assert!(
!source.queued_charge.tables.contains_key(&address),
"the last chunk referencing the table left, but the queue still counts it"
);
assert!(
source.queued_charge.buffers.is_empty(),
"a buffer outlived the chunks that referenced it"
);
assert_eq!(source.buffer.reservation.size(), 0);
assert_eq!(
source.queued_charge.bytes, 0,
"the queue is empty, but it is still charged for"
pool.reserved(),
0,
"the queue emptied, but the pool holds its bytes"
);
assert_eq!(
source.buffer.held, 0,
"the reservation did not come back with the queue"
);
assert_eq!(pool.reserved(), 0);
assert!(source.pop_source().unwrap().is_none());
}
/// Queueing a chunk accounts for that chunk alone, however deep the queue is.
/// Releasing a node drops its queue and returns the reservation once.
///
/// The figure is the number of chunks the accounting walked, not wall time: `N` enqueues have to
/// walk `N` chunks and `N` pops another `N`, whatever the queue held. Recounting the queue per
/// operation -- the defect this refuses -- walks `N * (N + 1) / 2` chunks for the enqueues alone.
/// A node stopped with chunks still queued holds nothing afterwards: the queue goes, and the bytes it
/// was reserved for go back to the pool rather than staying held for as long as the caller keeps the
/// stopped executor alive.
#[test]
fn queueing_a_chunk_walks_one_chunk() {
let pool = pool();
let mut source = source_runtime(&pool);
let schema = raw_schema();
let chunks = 64;
for index in 0..chunks {
let timestamp = i64::try_from(index).unwrap() * 10_000;
let chunk = raw_chunk(&schema, &[timestamp], &[1.0], index + 1 == chunks);
source.enqueue(NativeChunk::Raw(chunk)).unwrap();
}
assert_eq!(
source.queued_charge.walked, chunks,
"queueing {chunks} chunks walked {} chunk(s): the figure is recounted from the queue",
source.queued_charge.walked,
);
assert_eq!(
source.queued_charge.bytes,
source.queued_bytes_recomputed(),
"the running total is not what one pass over the queue counts"
);
let mut popped = 0;
while source.pop_source().unwrap().is_some() {
popped += 1;
}
assert_eq!(popped, chunks, "the queue did not hand back every chunk");
assert_eq!(
source.queued_charge.walked,
2 * chunks,
"popping {chunks} chunks walked {} chunk(s): the figure is recounted from the queue",
source.queued_charge.walked,
);
assert_eq!(source.queued_charge.bytes, 0);
}
/// A mixed sequence of pushes and pulls leaves the queue's figure equal to one pass over what is
/// left in it, and the reservation equal to that figure.
///
/// The chunks mix ones that share a table with ones that share nothing, so an allocation is
/// referenced by one, two or no queued chunk as the sequence runs. Both the node's own reservation
/// and the pool's are checked, so a total that drifts from the queue is caught by the figure the
/// memory pool actually holds.
#[test]
fn a_queue_figure_survives_a_mixed_push_and_pull_sequence() {
fn releasing_a_node_drops_its_queue() {
let pool = pool();
let mut source = source_runtime(&pool);
let schema = raw_schema();
let mut builder = HistogramBatchBuilder::new();
let index = push_histogram(&mut builder, 2);
let table = builder.finish().unwrap();
for round in 0..6i64 {
for step in 0..2 {
let chunk =
raw_shared_histogram_chunk(&schema, &table, index, round * 10_000 + step, false);
source.enqueue(NativeChunk::Raw(chunk)).unwrap();
}
let chunk = raw_chunk(&schema, &[round * 10_000 + 2], &[1.0], false);
source.enqueue(NativeChunk::Raw(chunk)).unwrap();
let folded = source.queued_charge.bytes;
assert_eq!(
folded,
source.queued_bytes_recomputed(),
"round {round}: the running total is not one pass over the queue"
);
assert_eq!(source.buffer.held, folded);
assert_eq!(pool.reserved(), folded);
for _ in 0..2 {
if source.pop_source().unwrap().is_none() {
break;
}
}
let folded = source.queued_charge.bytes;
assert_eq!(
folded,
source.queued_bytes_recomputed(),
"round {round}: the running total is not one pass over what is left"
);
assert_eq!(source.buffer.held, folded);
assert_eq!(pool.reserved(), folded);
for timestamp in [0, 10_000] {
source
.enqueue(NativeChunk::Raw(raw_shared_histogram_chunk(
&schema, &table, index, timestamp, false,
)))
.unwrap();
}
while source.pop_source().unwrap().is_some() {}
assert_eq!(source.queued_charge.bytes, 0);
assert!(
source.queued_charge.tables.is_empty(),
"a table outlived the chunks that referenced it"
);
assert!(
source.queued_charge.buffers.is_empty(),
"a buffer outlived the chunks that referenced it"
);
assert_eq!(source.buffer.held, 0);
assert_eq!(pool.reserved(), 0);
}
/// What a source reserves through the executor is the figure one pass over its queue counts.
#[test]
fn the_executor_reserves_what_one_pass_over_the_queue_counts() {
let tree = NativeTree::try_new(window(input(), grid(0, 10_000, 4))).unwrap();
let schema = raw_schema();
let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new());
for step in 0..3 {
let chunk = raw_chunk(&schema, &[step * 10_000], &[1.0], false);
execution.push_raw(chunk).unwrap();
}
let (folded, walked) = execution
.state()
.queued_charge_pair(&NodePath::root().input());
assert_eq!(
folded, walked,
"the executor's queue is not charged what one pass over it counts"
);
assert_eq!(
execution.state().reserved_bytes(),
folded,
"the source's reservation is not its queue's figure"
);
}
/// Releasing a node drops the queue's counts with the queue and returns the reservation once.
///
/// A count left behind would be folded onto by the next chunk the node queues, and the reservation
/// shown as returned while the counts still hold the bytes is the leak this refuses.
#[test]
fn releasing_a_node_drops_the_queue_and_its_counts() {
let pool = pool();
let mut source = source_runtime(&pool);
let schema = raw_schema();
let mut builder = HistogramBatchBuilder::new();
let index = push_histogram(&mut builder, 2);
let table = builder.finish().unwrap();
source
.enqueue(NativeChunk::Raw(raw_shared_histogram_chunk(
&schema, &table, index, 0, false,
)))
.unwrap();
source
.enqueue(NativeChunk::Raw(raw_shared_histogram_chunk(
&schema, &table, index, 10_000, true,
)))
.unwrap();
assert_eq!(pool.reserved(), source.queued_charge.bytes);
assert!(pool.reserved() > 0);
source.release();
assert!(source.queued.is_empty(), "the queue outlived its release");
assert!(
source.queued_charge.tables.is_empty(),
"the queue's table counts outlived the queue"
);
assert!(
source.queued_charge.buffers.is_empty(),
"the queue's buffer counts outlived the queue"
);
assert_eq!(source.queued_charge.bytes, 0);
assert_eq!(source.buffer.held, 0);
assert_eq!(source.buffer.reservation.size(), 0);
assert_eq!(pool.reserved(), 0, "the reservation was not returned");
assert!(source.pop_source().unwrap().is_none());
}
@@ -2608,83 +2381,6 @@ fn a_series_whose_axis_and_fields_disagree_is_rejected() {
);
}
/// One entry's figure is the heap it owns, and its `History` header is charged once, by its slot.
///
/// The `History` of a label set lives in the map's slot, so a `size_of::<History>()` term in the
/// entry's own figure billed one header twice: once here and once in the capacity term beside it.
/// The two assertions below are the definition of the whole figure -- the labels, the vectors and
/// their runs, the owner's name and tags, and one label-set slot plus one history slot per entry --
/// so either header counted in the wrong place fails them.
#[test]
fn a_history_entry_is_charged_once_for_its_header() {
let grid = grid(0, 10_000, 2);
let schema = evaluated_schema(Arc::clone(&grid));
let metric = named_metric("requests", "job");
let labels = label_set(&metric);
// Not the closing chunk: a closed label set drops its runs, and the runs are what the entry's
// own figure is.
let chunk = evaluated_chunk_of(
&schema,
metric.clone(),
0,
vec![Some(1.0), Some(2.0)],
false,
);
let mut emitted = Emitted::default();
emitted
.occupy(&NodePath::root(), Some(&metric), &chunk)
.unwrap();
let history = emitted
.sets
.get(&labels)
.expect("the chunk recorded its label set");
// The owner the entry keeps is the series that filled it, so its name and each tag cost the
// same bytes the label set of the chunk did.
let owner = metric.name().map_or(0, |name| name.len())
+ std::mem::size_of_val(metric.tags())
+ metric
.tags()
.iter()
.map(|tag| tag.name.len() + tag.value.len())
.sum::<usize>();
let entry = label_bytes(&labels)
.saturating_add(history.fields.capacity() * std::mem::size_of::<Vec<Run>>())
.saturating_add(
history
.fields
.iter()
.map(|runs| runs.capacity() * std::mem::size_of::<Run>())
.sum::<usize>(),
)
.saturating_add(owner);
assert_eq!(
history.owner.as_ref(),
Some(&metric),
"the entry does not name the series that filled it"
);
assert!(
history.fields.iter().any(|runs| !runs.is_empty()),
"the entry kept no run, so it has no heap of its own to charge for"
);
assert_eq!(
emitted.entries,
entry,
"the entry's own bytes are not its labels, its runs and its owner: a `History` header in \
the figure would add the {} bytes its slot already charges",
std::mem::size_of::<History>(),
);
assert_eq!(
emitted.bytes(),
entry.saturating_add(
emitted.sets.capacity()
* (std::mem::size_of::<LabelSet>() + std::mem::size_of::<History>())
),
"the table's capacity is where a label-set slot and one `History` per entry are charged",
);
}
/// A sparse series that only ever moves forward is appended to its history, not merged into it again
/// chunk after chunk.
///
+82 -108
View File
@@ -192,26 +192,12 @@ impl PendingBatch {
/// The bytes this batch's own vectors hold, by capacity.
fn bytes(&self) -> usize {
let spans = self
.spans
.iter()
.map(|spans| spans.capacity() * std::mem::size_of::<std::ops::Range<usize>>())
.fold(0usize, usize::saturating_add);
let columns = self
.columns
.iter()
.map(|column| column.capacity() * std::mem::size_of::<Option<f64>>())
.fold(0usize, usize::saturating_add);
self.indices
.capacity()
.saturating_mul(std::mem::size_of::<usize>())
.saturating_add(
self.eval_ts
.capacity()
.saturating_mul(std::mem::size_of::<i64>()),
)
.saturating_add(spans)
.saturating_add(columns)
let spans: usize = self.spans.iter().map(|spans| spans.capacity()).sum();
let columns: usize = self.columns.iter().map(|column| column.capacity()).sum();
self.indices.capacity() * std::mem::size_of::<usize>()
+ self.eval_ts.capacity() * std::mem::size_of::<i64>()
+ spans * std::mem::size_of::<Range<usize>>()
+ columns * std::mem::size_of::<Option<f64>>()
}
/// Drops the accumulated points, keeping the allocations.
@@ -303,34 +289,23 @@ struct SeriesState {
impl SeriesState {
/// The bytes this series' identity holds, which the rows handed out do not cover.
fn identity_bytes(&self) -> usize {
// A scan slices the primary key out of a decoded dictionary-values buffer, so the bytes the
// key shows are not the bytes it keeps: a key of a few bytes pins the whole buffer it was
// sliced out of, which `capacity` reports for the allocation behind the slice. Charging
// the key's length instead would show a handful of bytes for an allocation the size of a
// decoded batch. (A buffer that does not own its memory reports no capacity, and then the
// bytes it shows are all there is.)
//
// No dedupe is needed against a second slice of the same allocation: one evaluator holds
// one series at a time, and a window over an evaluated input holds no key at all, so a
// pass over this state charges one slice of one allocation rather than one per slice.
// A scan slices the primary key out of a decoded buffer, so the bytes the key shows are not
// the bytes it keeps: a key of a few bytes pins the allocation it was sliced out of.
// Charging the slice length instead would show a handful of bytes for an allocation the
// size of a decoded batch. One evaluator holds one series at a time, and an evaluated input
// holds no key at all, so there is no second slice of the same allocation to dedupe.
let primary_key = self
.primary_key
.as_ref()
.map_or(0, |key| key.capacity().max(key.len()));
let metric: usize = self
.metric
.name()
.map_or(0, |name| name.len())
.saturating_add(
self.metric
.tags()
.iter()
.map(|tag| tag.name.len() + tag.value.len())
.fold(0usize, usize::saturating_add),
);
std::mem::size_of::<SeriesState>()
.saturating_add(primary_key)
.saturating_add(metric)
let metric: usize = self.metric.name().map_or(0, |name| name.len())
+ self
.metric
.tags()
.iter()
.map(|tag| tag.name.len() + tag.value.len())
.sum::<usize>();
std::mem::size_of::<SeriesState>() + primary_key + metric
}
}
@@ -356,7 +331,17 @@ struct FieldSamples {
head: usize,
/// Native histogram samples, kept apart from the floats as Prometheus does.
histograms: Vec<(i64, FloatHistogram)>,
/// Histograms before `histogram_head` are behind every remaining window; their payloads are
/// released by [`advance_histograms`](Self::advance_histograms).
histogram_head: usize,
/// Estimated payload of the histograms this field still holds: the bucket lists and other
/// buffers the samples own, which no `size_of` of a slot shows.
///
/// Measured for a histogram when it is admitted and released with it, so a retained sample is
/// not walked again on every push. A histogram is never modified once admitted, so the payload
/// its release subtracts is the one its admission added. Samples already behind every window
/// keep their payload in the figure until the drain releases them.
histogram_bytes: usize,
/// Sequence number of `values[0]`. Absolute and monotonic across the series this evaluator
/// evaluates, so a reset keeps its position when `head` samples are dropped instead of every
/// index being renumbered. An empty field holds the number of samples already dropped, which
@@ -392,37 +377,14 @@ fn resets_between(resets: &[CounterReset], start: u64, end: u64) -> &[CounterRes
}
impl FieldSamples {
/// The bytes this field's buffers hold, by capacity.
///
/// The histogram term is the slot of every retained sample plus the payload the histogram in
/// that slot owns: a slot is one `(i64, FloatHistogram)` worth of struct, and the buckets the
/// histogram keeps are on the heap behind it, up to tens of kilobytes per sample. Only the
/// retained slice is walked, which is the storage the windows still read; the samples before
/// `head` are gone, their payloads with them.
/// The bytes this field's buffers hold, by capacity: the samples, the history behind `head`
/// included, plus the estimated payload of the histograms still held.
fn bytes(&self) -> usize {
let histograms = self.histograms[self.histogram_head..]
.iter()
.map(|(_, histogram)| histogram.heap_bytes())
.fold(0usize, usize::saturating_add);
self.timestamps
.capacity()
.saturating_mul(std::mem::size_of::<i64>())
.saturating_add(
self.values
.capacity()
.saturating_mul(std::mem::size_of::<f64>()),
)
.saturating_add(
self.histograms
.capacity()
.saturating_mul(std::mem::size_of::<(i64, FloatHistogram)>()),
)
.saturating_add(histograms)
.saturating_add(
self.resets
.capacity()
.saturating_mul(std::mem::size_of::<CounterReset>()),
)
self.timestamps.capacity() * std::mem::size_of::<i64>()
+ self.values.capacity() * std::mem::size_of::<f64>()
+ self.histograms.capacity() * std::mem::size_of::<(i64, FloatHistogram)>()
+ self.resets.capacity() * std::mem::size_of::<CounterReset>()
+ self.histogram_bytes
}
fn new(index_resets: bool) -> Self {
@@ -498,6 +460,23 @@ impl FieldSamples {
self.resets.drain(..dropped);
}
/// Drops the histograms before `histogram_head`, releasing the payload they held.
///
/// The dropped samples are measured as they go, which costs one measurement per dropped
/// histogram in total: the retained ones are never scanned again.
fn advance_histograms(&mut self) {
let head = std::mem::take(&mut self.histogram_head);
if head == 0 {
return;
}
let dropped: usize = self.histograms[..head]
.iter()
.map(|(_, histogram)| histogram.heap_bytes())
.sum();
self.histogram_bytes = self.histogram_bytes.saturating_sub(dropped);
self.histograms.drain(..head);
}
/// The resets inside the window holding `len` samples from `head`.
fn resets_in(&self, len: usize) -> &[CounterReset] {
let start = self.base + self.head as u64;
@@ -507,22 +486,20 @@ impl FieldSamples {
/// The bytes the histograms a row holds box, beyond the slots they sit in.
///
/// A slot is an `Option<GridSample>`: a pointer and a tag, which is all
/// `size_of::<Option<GridSample>>()` is. A result that is a native histogram does not live in that
/// slot itself: the slot points at a `FloatHistogram` on the heap, which owns its bucket
/// lists in turn, and both are what the row keeps alive between pushes. Only slots that hold a
/// histogram are counted, and only those the last evaluated point left in place: a push clears the
/// row, so the slots past its length hold nothing.
/// A slot is an `Option<GridSample>`: a pointer and a tag. A result that is a native histogram
/// points at a [`FloatHistogram`] on the heap, which owns its bucket lists in turn, and both stay
/// alive between pushes. Only slots the last evaluated point left in place are counted: a push
/// clears the row.
fn boxed_bytes(row: &[Option<GridSample>]) -> usize {
row.iter()
.flatten()
.filter_map(|sample| match sample {
.map(|sample| match sample {
GridSample::Histogram(histogram) => {
Some(std::mem::size_of::<FloatHistogram>().saturating_add(histogram.heap_bytes()))
std::mem::size_of::<FloatHistogram>() + histogram.heap_bytes()
}
GridSample::Float(_) => None,
GridSample::Float(_) => 0,
})
.fold(0usize, usize::saturating_add)
.sum()
}
impl RangeEvaluator {
@@ -645,34 +622,30 @@ impl RangeEvaluator {
self.batch_calls
}
/// An accounted lower bound on the bytes this evaluator keeps between pushes.
/// An estimate of the bytes this evaluator keeps between pushes.
///
/// Counted by capacity, so it is what the storage holds rather than what it uses: the retained
/// samples per field (timestamps, values and the native histograms with the payload each of
/// them owns), the reset index, the row a push fills with the histograms it boxes, and the
/// pending batch's own vectors. Left out are the kernel's scratch buffers, this struct's `Vec`
/// headers and the grid, which every chunk addressed on it shares.
/// samples per field (timestamps, values, the reset index and the native histograms with the
/// payload each of them owns), the row a push fills with the histograms it boxes, the identity
/// of the open series and the pending batch's own vectors. Left out are the kernel's scratch
/// buffers, this struct's `Vec` headers and the grid, which every chunk addressed on it shares.
///
/// The figure is an estimate, not a ledger of every allocation: a histogram's payload is
/// measured once when it is admitted and released with it, so neither the retained samples nor
/// the transient allocations of a push are walked again.
///
/// A push is the only caller that also holds the incoming chunk, and it charges that itself for
/// as long as the call runs; vector growth during a push is neither reserved nor bounded by one
/// doubling, so this is the storage's size, not a peak over the push. The figure must not be
/// per-chunk either: charging every chunk consumed would make the charge grow with the chunk
/// count, so a fixed window over a long series would read as an allocation failure even though
/// the window holds no more samples.
/// as long as the call runs. The figure must not be per-chunk either: charging every chunk
/// consumed would make the charge grow with the chunk count, so a fixed window over a long
/// series would read as an allocation failure even though the window holds no more samples.
pub(crate) fn retained_bytes(&self) -> usize {
let per_field = self
.fields
.iter()
.map(FieldSamples::bytes)
.fold(0usize, usize::saturating_add);
let fields: usize = self.fields.iter().map(FieldSamples::bytes).sum();
let row = self.row.capacity() * std::mem::size_of::<Option<GridSample>>();
let boxed = boxed_bytes(&self.row);
let series = self.series.as_ref().map_or(0, SeriesState::identity_bytes);
per_field
.saturating_add(row)
.saturating_add(boxed)
.saturating_add(series)
.saturating_add(self.pending.bytes())
fields
+ row
+ boxed_bytes(&self.row)
+ self.series.as_ref().map_or(0, SeriesState::identity_bytes)
+ self.pending.bytes()
}
/// Schema of the evaluated series: the input fields on the evaluation grid.
@@ -1054,6 +1027,8 @@ impl RangeEvaluator {
if !raw && h.reset_hint != CounterResetHint::Gauge {
h.reset_hint = CounterResetHint::Unknown;
}
field.histogram_bytes =
field.histogram_bytes.saturating_add(h.heap_bytes());
field.histograms.push((shifted, h));
}
}
@@ -1223,8 +1198,7 @@ impl RangeEvaluator {
field.histogram_head = field.histograms.len();
}
field.advance();
field.histograms.drain(..field.histogram_head);
field.histogram_head = 0;
field.advance_histograms();
}
Ok(())
}
+111 -7
View File
@@ -831,13 +831,15 @@ fn histogram_chunk(schema: &SeriesSchema, buckets: i32, ts: i64) -> RawSeriesChu
.unwrap()
}
/// A window has to reserve what a native histogram it retains owns, not only the slot it sits in.
/// A window retaining a native histogram is charged for the payload it owns, not only the slot.
///
/// The histogram sample below stays ahead of the second grid point's window, so the evaluator keeps
/// it, and the two runs differ in nothing but the number of buckets on the positive side: 4 and
/// 4096. Those buckets are `(index, count)` pairs on the heap, which no `size_of` of the struct
/// shows -- 64 KiB for the wider one -- so the bytes the evaluator reports have to grow by at least
/// that payload between the two. A figure counting slots alone would report one number for both.
/// shows -- about 64 KiB for the wider one. The property is that this payload dominates the
/// estimate, not that it is counted to the byte: the wider histogram alone has to be reported as at
/// least half the physical width of its buckets, and the narrow one has to stay far below that. A
/// figure counting slots alone would report one number for both.
#[test]
fn a_retained_histogram_is_charged_for_its_buckets() {
let retained = |buckets: i32| -> usize {
@@ -864,12 +866,114 @@ fn a_retained_histogram_is_charged_for_its_buckets() {
evaluator.retained_bytes()
};
let (few, many) = (4, 4096);
let payload = usize::try_from(many - few).unwrap() * std::mem::size_of::<(i32, f64)>();
let physical = usize::try_from(many).unwrap() * std::mem::size_of::<(i32, f64)>();
let (narrow, wide) = (retained(few), retained(many));
assert!(
wide >= narrow + payload,
"a window retaining one histogram of {many} buckets reports {wide} bytes, not the {narrow} \
it reports for {few} buckets plus the {payload} bytes of bucket payload between them",
wide >= physical / 2,
"a window retaining one histogram of {many} buckets reports {wide} bytes, not at least half \
the {physical} bytes its buckets occupy",
);
assert!(
narrow < wide / 2,
"a window retaining one histogram of {few} buckets reports {narrow} bytes, not far below \
the {wide} bytes it reports for {many} buckets: the bucket payload is not what the \
estimate follows",
);
}
/// The payload a retained histogram is charged for is released with the sample.
///
/// The histogram below is most of what the evaluator holds, and it stays inside the second grid
/// point's window: a push that only reaches the first point retains it. The chunk at 30 s is beyond
/// the window of every later point, so the evaluation that push triggers drops the histogram while
/// the series is still open. The estimate must come back down by its payload: one that charges at
/// admission and never releases it would keep reporting the history of every histogram the series
/// carried. The closing chunk then drains what is left and ends the series, so the estimate falls
/// back to the field and row buffers' capacity, with no series identity and no histogram payload.
#[test]
fn an_evicted_histogram_releases_its_payload() {
let schema = raw_schema(1);
let buckets = 4096;
let payload = usize::try_from(buckets).unwrap() * std::mem::size_of::<(i32, f64)>();
let mut evaluator = RangeEvaluator::try_new(
&schema,
grid(0, 10_000, 3),
// 20 s of window over 10 s of step: the histogram at 0 is inside the window of the second
// point but outside the third's, so the second push evicts it.
RangeWindow {
range_ms: 20_000,
offset_ms: 0,
anchor_ms: None,
},
RangeFunction::CountOverTime,
)
.unwrap();
evaluator
.push(&histogram_chunk(&schema, buckets, 0))
.unwrap();
let retained = evaluator.retained_bytes();
assert!(
retained >= payload,
"a window retaining one histogram of {buckets} buckets reports {retained} bytes, less than \
the {payload} bytes of buckets it holds",
);
// The float chunk keeps the series open, so only the window drops the histogram.
evaluator
.push(&chunk(&[(30_000, vec![Some(1.0)])], 1, 0, false))
.unwrap();
let evicted = evaluator.retained_bytes();
assert!(
retained.saturating_sub(evicted) >= payload / 2,
"dropping one histogram of {payload} bytes left the estimate at {evicted} bytes, down from \
{retained}: the payload it owned is still counted",
);
evaluator
.push(&chunk(&[(40_000, vec![Some(2.0)])], 1, 0, true))
.unwrap();
let closed = evaluator.retained_bytes();
assert!(
closed < payload / 2,
"a closed series still reports {closed} bytes, of which the {payload} bytes of bucket \
payload were released with their samples",
);
}
/// A fixed-size window over a long series of histograms reports a fixed size.
///
/// Each push adds one histogram and the next point's window drops the one before it, so the
/// payload charged at admission has to come back at eviction. A counter that only grew would make
/// the estimate follow the length of the series instead of the window over it.
#[test]
fn a_fixed_window_over_histograms_keeps_the_estimate_bounded() {
let peak_retained = |chunks: i64| -> usize {
let schema = raw_schema(1);
let mut evaluator = RangeEvaluator::try_new(
&schema,
grid(0, 1, 1024),
// One millisecond of window over a one millisecond step: the next point evicts the
// sample of this one.
RangeWindow {
range_ms: 1,
offset_ms: 0,
anchor_ms: None,
},
RangeFunction::CountOverTime,
)
.unwrap();
let mut peak = 0;
for ts in 0..chunks {
// Left open: the window, not the close, is what releases the sample.
evaluator.push(&histogram_chunk(&schema, 64, ts)).unwrap();
peak = peak.max(evaluator.retained_bytes());
}
peak
};
assert_eq!(
peak_retained(8),
peak_retained(64),
"a fixed-size window over a series of histograms grew with the length of the series"
);
}