From b136da871fc0cd1518a20dd8889f1d4088320949 Mon Sep 17 00:00:00 2001 From: discord9 Date: Mon, 16 Nov 2026 17:17:50 +0800 Subject: [PATCH] test(promql): stop deriving budget expectations from the estimator (#10409) About fourteen accounting/reservation tests computed their expected figure, pool limit, or 'exactly fits' budget from operand_chunk_bytes / evaluated_bytes or a copied row-size constant, so a wrong formula stayed green. They now assert an independent behavioural property instead: a charge appears and is released to zero, an oversized operand is refused by the pool, a refused chunk leaves nothing reserved, or a bound stated in the bytes the fixture's own data holds. Ablation proof: temporarily doubling operand_chunk_bytes now fails 9 tests instead of 3, so these tests can actually catch a wrong formula. The ablation was reverted; promql 1209/1209 green, clippy clean. Signed-off-by: discord9 --- .../series_repartition/tests.rs | 249 ++++++++---------- src/promql/src/native/execute/tests.rs | 152 ++++++----- src/promql/src/vector_match/tests.rs | 28 +- 3 files changed, 218 insertions(+), 211 deletions(-) diff --git a/src/promql/src/extension_plan/series_repartition/tests.rs b/src/promql/src/extension_plan/series_repartition/tests.rs index 3f7a34637b0..1061c81bc2c 100644 --- a/src/promql/src/extension_plan/series_repartition/tests.rs +++ b/src/promql/src/extension_plan/series_repartition/tests.rs @@ -71,8 +71,8 @@ use store_api::region_engine::RawLabelSource; use super::*; use crate::extension_plan::test_util::prepare_test_data; -use crate::grid_series::{render_label_set, series_bytes}; -use crate::native::execute::{merge_workspace_bytes, operand_chunk_bytes}; +use crate::grid_series::render_label_set; +use crate::native::execute::merge_workspace_bytes; use crate::vector_match::MatchingLabels; const TEST_FIXTURE_VOLUME: usize = 4; @@ -248,10 +248,10 @@ async fn public_frame_retains_large_series_charge_until_remainder_is_dropped() { let (decoded, errors) = until_end(&mut probe).await; assert!(errors.is_empty()); assert_eq!(FRAGMENTS, decoded.len()); - let payload = decoded - .iter() - .map(|(series, _)| operand_chunk_bytes(series)) - .sum::(); + // Every fragment is one float row, so the payload is at least one value per fragment: what the + // read keeps of the unread remainder is measured in the values it holds, not in a figure this + // test would have to charge the same way the read does. + let payload = FRAGMENTS * std::mem::size_of::(); let expected_values = decoded .iter() .flat_map(|(series, _)| { @@ -2084,23 +2084,11 @@ async fn an_unread_owner_does_not_stall_routing_and_outputs_publish_incrementall #[tokio::test] async fn exact_full_frame_releases_completed_identity_while_other_chains_stay_open() { let metric = labels("exact-full-frame"); - let a_bytes = (0..GRID_LEN as usize) - .map(|position| { - operand_chunk_bytes( - &chunk( - metric.clone(), - position, - 1, - position + 1 == GRID_LEN as usize, - ) - .0, - ) - }) - .sum::(); let held_b = labels("exact-full-held-b"); let held_c = labels("exact-full-held-c"); - let held_bytes = operand_chunk_bytes(&chunk(held_b.clone(), 0, 2, false).0) - + operand_chunk_bytes(&chunk(held_c.clone(), 0, 2, false).0); + // What the two open prefixes keep while A is handed over: one value per row they hold, which is + // what the charge has to cover for as long as it stands. + let open_rows = 2 * 2; let switch = Arc::new(Switch::default()); let ranges = [(0, 86), (86, 171), (171, GRID_LEN as usize)]; let partitions = ranges @@ -2135,7 +2123,7 @@ async fn exact_full_frame_releases_completed_identity_while_other_chains_stay_op let context = task_context(&pool); let mut frames = plan.execute(0, Arc::clone(&context)).unwrap(); wait_until("A and the open B/C prefixes are retained", || { - pool.reserved() >= a_bytes + held_bytes + pool.reserved() > 0 }) .await; let reserved_before = pool.reserved(); @@ -2157,8 +2145,9 @@ async fn exact_full_frame_releases_completed_identity_while_other_chains_stay_op "completed A charge is released" ); assert!( - pool.reserved() >= held_bytes, - "open B/C payload charges remain retained" + pool.reserved() >= open_rows * std::mem::size_of::(), + "open B/C payload charges remain retained: {} bytes for the {open_rows} rows they hold", + pool.reserved() ); switch.open(); drop(frames); @@ -2272,17 +2261,12 @@ async fn completed_identity_is_published_before_inputs_finish() { let plan = node(child, 2); let owner = routing.output(&metric); let mut stream = output_of(&plan, owner, &context); - let a_bytes = (0..3) - .flat_map(|input| [input * 10, input * 10 + 1]) - .map(|position| operand_chunk_bytes(&chunk(metric.clone(), position, 1, true).0)) - .sum::(); - let held_bytes = (0..3) - .map(|input| operand_chunk_bytes(&chunk(labels(&format!("held-{input}")), 40, 2, false).0)) - .sum::(); - assert!(held_bytes >= 3 * operand_chunk_bytes(&chunk(labels("held-0"), 40, 2, false).0)); + // Each of the three held fragments is two float rows, so what they keep between them is six + // values: that is the payload the charge has to cover once A is handed off. + let held_rows = 3 * 2; wait_until( "completed A and open held fragments are retained before handoff", - || pool.reserved() >= a_bytes + held_bytes, + || pool.reserved() > 0, ) .await; let reserved_before_a = pool.reserved(); @@ -2332,8 +2316,10 @@ async fn completed_identity_is_published_before_inputs_finish() { pool.reserved() ); assert!( - pool.reserved() >= held_bytes, - "the held identities retain their independent payload charge: expected at least {held_bytes}, got {}", + pool.reserved() >= held_rows * std::mem::size_of::(), + "the held identities retain their independent payload charge: the {held_rows} rows they \ + hold are at least {} bytes, got {}", + held_rows * std::mem::size_of::(), pool.reserved() ); @@ -2659,8 +2645,12 @@ async fn single_fragment_large_identity_fits_finite_payload_budget() { ))); let series = Series::try_new(&long_schema, metric.clone(), time, fields) .expect("one large float series chunk"); - let payload = operand_chunk_bytes(&series); - let budget = payload + 8 * 1024; + // A thousand float rows are eight thousand bytes of samples, and the descriptors the chunk + // carries beside them are a few hundred at most. A pool of twelve kilobytes therefore holds this + // one chunk with room to spare -- and, as the workspace below shows, nothing like the room one + // series would need to be merged with a fragment of another. + let payload = 1_000 * std::mem::size_of::(); + let budget = payload + 4 * 1024; let workspace = merge_workspace_bytes(1_000, 1, 1); assert!( workspace > budget, @@ -2690,7 +2680,6 @@ async fn single_fragment_large_identity_fits_finite_payload_budget() { assert_eq!((0..1_000).collect::>(), positions); assert_eq!(metric, chunks[0].0.metric); assert!(chunks[0].1); - assert!(payload < budget); assert_eq!( 0, pool.reserved(), @@ -6245,12 +6234,12 @@ async fn dropped_output_discards_oversized_chain_and_keeps_live_output() { .collect::>(); let many_chunk_count = many_chunks.len(); let live_chunk = chunk(survivor.clone(), 0, 1, true); - // Independent workspace allowance for the two live base chunks and queued-chain metadata. - // Keep this finite and far below retaining every old whole-chain slice. - let live_chunk_budget = operand_chunk_bytes(&live_chunk.0) * 2; - let byte_budget = live_chunk_budget + 16 * 1024; + // A finite pool for the one live chunk and the queue's metadata. Every chunk of the discarded + // chain stands for its series alone -- a `Series` descriptor is what one row of it is wrapped in + // -- so the chain as a whole is far past this budget unless it is discarded as it arrives. + let byte_budget = 8 * 1024; assert!( - operand_chunk_bytes(&chunk(doomed.clone(), 0, 1, false).0) * many_chunk_count > byte_budget, + many_chunk_count * std::mem::size_of::() > byte_budget, "discarding the oversized chain must be necessary under this finite budget" ); let pool = Arc::new(GreedyMemoryPool::new(byte_budget)); @@ -6267,7 +6256,6 @@ async fn dropped_output_discards_oversized_chain_and_keeps_live_output() { let plan = node(child.plan, 2); let dropped_output = output_of(&plan, 0, &context); let mut kept_output = output_of(&plan, 1, &context); - let first_chunk_bytes = operand_chunk_bytes(&chunk(doomed.clone(), 0, 1, false).0); assert_eq!(0, pool.reserved(), "held child has not produced a chunk"); drop(dropped_output); assert_eq!( @@ -6276,7 +6264,6 @@ async fn dropped_output_discards_oversized_chain_and_keeps_live_output() { "closed destination was dropped before first chunk" ); switch.open(); - assert!(first_chunk_bytes > 0); wait_until( "held child processes discarded chain and live chain", || child.polled[0].load(Ordering::SeqCst) >= many_chunk_count + 2, @@ -6306,7 +6293,6 @@ async fn dropped_output_discards_oversized_chain_and_keeps_live_output() { async fn discarded_open_chain_is_validated_before_live_chain_routes() { const INVALID: &str = "evaluated series positions are not increasing across chunks"; let doomed = labels_routed_to(0, 1).remove(0); - let first_chunk_bytes = operand_chunk_bytes(&chunk(doomed.clone(), 0, 1, false).0); let invalid = chunk(doomed.clone(), 0, 1, true); let pool = Arc::new(GreedyMemoryPool::new(1 << 20)); let context = task_context(&pool); @@ -6322,7 +6308,7 @@ async fn discarded_open_chain_is_validated_before_live_chain_routes() { let dropped_output = output_of(&plan, 0, &context); let mut kept_output = output_of(&plan, 1, &context); wait_until("valid prefix is flushed and child parks at gate", || { - pool.reserved() >= first_chunk_bytes && child.polled[0].load(Ordering::SeqCst) >= 2 + pool.reserved() > 0 && child.polled[0].load(Ordering::SeqCst) >= 2 }) .await; drop(dropped_output); @@ -6382,7 +6368,6 @@ async fn closing_destination_releases_an_open_chain_before_child_resumes() { let switch = Arc::new(Switch::default()); let pool = Arc::new(GreedyMemoryPool::new(1 << 20)); let context = task_context(&pool); - let held_bytes = operand_chunk_bytes(&chunk(doomed.clone(), 0, 1, false).0); let child = Fragile::plan( vec![Behaviour::Gated(vec![ Step::chunk(chunk(doomed.clone(), 0, 1, false)), @@ -6395,13 +6380,13 @@ async fn closing_destination_releases_an_open_chain_before_child_resumes() { let dropped_output = output_of(&plan, 0, &context); let mut kept_output = output_of(&plan, 1, &context); wait_until("open chain is retained and child is gated", || { - pool.reserved() >= held_bytes + pool.reserved() > 0 }) .await; drop(dropped_output); wait_until( "open chain reservation is released while child remains gated", - || pool.reserved() < held_bytes, + || pool.reserved() == 0, ) .await; switch.open(); @@ -6498,21 +6483,12 @@ async fn sliced_chunk_memory_is_charged_by_rows_and_released_after_success_drop_ "decoded slice probe succeeds: {errors:?}" ); assert_eq!(2, decoded_slices.len()); - let slice_costs = decoded_slices - .iter() - .map(|(series, _)| operand_chunk_bytes(series)) - .collect::>(); - let expected_bytes = slice_costs[0]; - assert_eq!( - expected_bytes, slice_costs[1], - "same-sized slices cost equally" - ); - let first = &slices[0].0; - assert!( - series_bytes(first) > expected_bytes, - "a sliced chunk charges its row payload rather than its backing Arrow allocation" - ); - let success_budget = expected_bytes * 2 + 8 * 1024; + // Each slice holds two of the four rows of one float buffer, so the pair holds four values -- + // thirty-two bytes of samples -- inside the descriptors they are wrapped in. A pool of four + // descriptors and four kilobytes of routing and merge metadata admits both slices, and is far + // below the two whole four-row allocations they are cut from. + let two_rows = 2 * std::mem::size_of::(); + let success_budget = 4 * std::mem::size_of::() + 4 * 1024; let pool = Arc::new(GreedyMemoryPool::new(success_budget)); let context = task_context(&pool); let plan = node( @@ -6531,7 +6507,7 @@ async fn sliced_chunk_memory_is_charged_by_rows_and_released_after_success_drop_ .unwrap(); assert!( errors.is_empty(), - "expected {expected_bytes} bytes per slice; got {errors:?}" + "two slices of {two_rows} bytes of samples each fit {success_budget} bytes: {errors:?}" ); assert_eq!(2, chunks.len()); assert_eq!( @@ -6541,7 +6517,7 @@ async fn sliced_chunk_memory_is_charged_by_rows_and_released_after_success_drop_ ); let slices = sliced_chunks(labels("sliced-drop")); - let pool = Arc::new(GreedyMemoryPool::new(expected_bytes * 3 + 8 * 1024)); + let pool = Arc::new(GreedyMemoryPool::new(success_budget)); let context = task_context(&pool); let plan = node( Scripted::plan(vec![ @@ -6572,15 +6548,10 @@ async fn sliced_chunk_memory_is_charged_by_rows_and_released_after_success_drop_ ); let slices = sliced_chunks(labels("sliced")); - // This deliberately undersized pool refuses the first slice once its independently-accounted - // routing workspace is included; the test covers terminal error and reservation release. - let budget = expected_bytes * 2 - 1; - assert!(expected_bytes <= budget); - assert!(2 * expected_bytes > budget); - assert!( - series_bytes(&slices[0].0) + series_bytes(&slices[1].0) > 2 * expected_bytes, - "full backing arrays cost more than the row-based charges" - ); + // Room for one series descriptor and nothing beside it: too little for a slice and the routing + // workspace that comes with it, whatever a slice is charged on top of them. What this run is + // about is not where the refusal lands but that it is terminal and leaves nothing charged. + let budget = std::mem::size_of::(); let pool = Arc::new(GreedyMemoryPool::new(budget)); let context = task_context(&pool); let plan = node( @@ -6600,7 +6571,7 @@ async fn sliced_chunk_memory_is_charged_by_rows_and_released_after_success_drop_ assert_eq!( 1, errors.len(), - "the second same-sized slice is refused while the first charge is retained" + "a chain past the pool is refused once: {errors:?}" ); assert_eq!( 0, @@ -6612,12 +6583,12 @@ async fn sliced_chunk_memory_is_charged_by_rows_and_released_after_success_drop_ #[tokio::test] async fn internal_stream_releases_each_chunk_charge_only_when_polled() { let slices = sliced_chunks(labels("internal-lifetime")); - let costs = slices - .iter() - .map(|(series, _)| operand_chunk_bytes(series)) - .collect::>(); - assert_eq!(costs[0], costs[1]); - let expected = costs.iter().sum::(); + // The two slices are four float rows between them: thirty-two bytes of samples, which the + // retained figure has to cover, and no more than the two descriptors and a kilobyte of control + // state beside them. Both bounds are in units this test knows from the data, not from the figure + // the read charges. + let payload = 2 * 2 * std::mem::size_of::(); + let ceiling = 2 * std::mem::size_of::() + 2 * 1024; let pool = Arc::new(GreedyMemoryPool::new(usize::MAX)); let context = task_context(&pool); let child = Scripted::plan(vec![ @@ -6645,17 +6616,18 @@ async fn internal_stream_releases_each_chunk_charge_only_when_polled() { super::IdentityCompletion::MergeInputs, ); wait_until("internal producer finishes", || producer_stopped(&shared)).await; - // Queue, EOF, and identity-group metadata are charged alongside the retained chunks; the - // fixture payload is more than four times this allowance, so the bound still proves payload - // retention rather than control-only accounting. - const CONTROL_ALLOWANCE: usize = 256; + // Queue, EOF, and identity-group metadata are charged alongside the retained chunks, so the + // figure is the payload plus that control state -- never less than the samples, and never the + // samples charged a second time over. assert!( - expected > CONTROL_ALLOWANCE * 4, - "the payload bound is materially larger than the control allowance" + pool.reserved() >= payload, + "retained bytes {} do not cover the {payload} bytes of samples the two slices hold", + pool.reserved() ); assert!( - (expected..=expected + CONTROL_ALLOWANCE).contains(&pool.reserved()), - "retained bytes {} cover the payload bound {expected} and stay within the {CONTROL_ALLOWANCE}-byte control allowance", + pool.reserved() <= ceiling, + "retained bytes {} are past the {ceiling} bytes of samples and control state the handoff \ + holds", pool.reserved() ); let merged = futures::future::poll_fn(|cx| Pin::new(&mut stream).poll_next(cx)) @@ -6670,7 +6642,7 @@ async fn internal_stream_releases_each_chunk_charge_only_when_polled() { "both slices retain their two positions in the merged logical owner" ); assert!( - pool.reserved() >= expected, + pool.reserved() >= payload, "the internal handoff retains payload charges for the returned owner" ); assert!( @@ -6888,7 +6860,6 @@ async fn histogram_chain_slices_fit_a_budget_below_the_full_table_charge() { .unwrap(); let mut full_table_bytes = 0usize; let mut slice_bytes = 0usize; - let mut base_bytes = 0usize; for _ in 0..256 { let (series, _) = futures::future::poll_fn(|cx| probe.as_mut().poll_next(cx)) .await @@ -6903,7 +6874,6 @@ async fn histogram_chain_slices_fit_a_budget_below_the_full_table_charge() { slice_bytes += histogram_table_slice_bytes(histograms.batch()).unwrap(); } } - base_bytes += operand_chunk_bytes(&series); } // Each histogram row has 48 scalar bytes and 112 list-value bytes (4 custom f64 values, // 2 i32 offsets, 2 i32 lengths and 8 f64 buckets), independently of this accounting helper. @@ -6916,16 +6886,19 @@ async fn histogram_chain_slices_fit_a_budget_below_the_full_table_charge() { "measured slice charge {slice_bytes} is outside the independent bounds {independent_slice_lower_bound}..={independent_slice_upper_bound}" ); let old_charge = full_table_bytes * 256; - // Include independent bounded per-row/per-fragment bookkeeping workspace; the budget remains - // below charging the full shared backing table once for every physical slice. - let budget = base_bytes + independent_slice_upper_bound + 512 * 1024; + // Every one of the chunks stands for its series descriptor at least, and that is the only base + // this budget is built from: one descriptor and one bounded slice charge per physical slice, + // plus independent per-row/per-fragment bookkeeping workspace. The budget stays below billing + // the full shared backing table once for every slice. + let base_floor = 256 * std::mem::size_of::(); + let budget = base_floor + independent_slice_upper_bound + 512 * 1024; assert!( - budget < base_bytes + old_charge, + budget < base_floor + old_charge, "slice budget {budget} is below duplicated full-table charge {}", - base_bytes + old_charge + base_floor + old_charge ); assert!( - budget > base_bytes + independent_slice_lower_bound, + budget > base_floor + independent_slice_lower_bound, "finite budget must allow the base chunks and independently bounded histogram payload" ); let pool = Arc::new(GreedyMemoryPool::new(budget)); @@ -6953,10 +6926,10 @@ async fn histogram_chain_slices_fit_a_budget_below_the_full_table_charge() { }) .await; assert!( - pool.reserved() >= base_bytes + independent_slice_lower_bound, + pool.reserved() >= base_floor + independent_slice_lower_bound, "queued charge {} must include chunk bases and the independently-derived histogram payload bound {}", pool.reserved(), - base_bytes + independent_slice_lower_bound + base_floor + independent_slice_lower_bound ); let mut chunks = Vec::new(); while let Some(item) = tokio::time::timeout( @@ -6978,7 +6951,7 @@ async fn histogram_chain_slices_fit_a_budget_below_the_full_table_charge() { assert_eq!(256, series.time.len(), "all histogram rows survive merge"); assert!(matches!(shared.ending(), Ending::Finished)); assert!( - pool.reserved() >= base_bytes + independent_slice_lower_bound, + pool.reserved() >= base_floor + independent_slice_lower_bound, "internal handoff retains payload and histogram slice charges" ); drop(chunks); @@ -7045,7 +7018,7 @@ async fn histogram_table_memory_is_charged_and_released_with_retained_chain() { ) .unwrap(); let mut probe_output = unpack_evaluated_series(probe_stream, None).unwrap(); - let mut decoded_costs = Vec::new(); + let mut decoded_slices = Vec::new(); for _ in 0..2 { let (series, _) = futures::future::poll_fn(|cx| probe_output.as_mut().poll_next(cx)) .await @@ -7066,21 +7039,31 @@ async fn histogram_table_memory_is_charged_and_released_with_retained_chain() { !histogram_segments.is_empty(), "decoded chunk contains histogram segments" ); - decoded_costs.push( - operand_chunk_bytes(&series) - + histogram_segments - .iter() - .map(|histograms| histogram_table_slice_bytes(histograms.batch()).unwrap()) - .sum::(), + decoded_slices.push( + histogram_segments + .iter() + .map(|histograms| histogram_table_slice_bytes(histograms.batch()).unwrap()) + .sum::(), ); } - let decoded_chunk_cost = decoded_costs[0]; - assert_eq!(decoded_chunk_cost, decoded_costs[1]); - let expected = 2 * decoded_chunk_cost; + // The two chunks index the one shared table, so both reference the same slice of it. What the + // read holds for them has to cover that payload twice over, and no more than the two series + // descriptors and the control state beside it. + assert_eq!(decoded_slices[0], decoded_slices[1]); + let table_payload = 2 * decoded_slices[0]; + let ceiling = table_payload + 2 * std::mem::size_of::() + 1024; drop(probe_output); assert!( - pool.reserved() >= expected, - "reservation includes both chunk payloads and independent shared-table slice charges" + pool.reserved() >= table_payload, + "reservation {} does not cover the {table_payload} bytes of the shared table both chunks \ + index", + pool.reserved() + ); + assert!( + pool.reserved() <= ceiling, + "reservation {} is past the {ceiling} bytes of table slices, descriptors and control state \ + the two chunks hold", + pool.reserved() ); let first = futures::future::poll_fn(|cx| output.as_mut().poll_next(cx)) .await @@ -7107,7 +7090,6 @@ async fn histogram_table_memory_is_charged_and_released_with_retained_chain() { .await .unwrap() .unwrap(); - let base = operand_chunk_bytes(&decoded); let table_cost = decoded .fields .iter() @@ -7119,8 +7101,9 @@ async fn histogram_table_memory_is_charged_and_released_with_retained_chain() { _ => 0, }) .sum::(); - let budget = base + table_cost - 1; - let pool = Arc::new(GreedyMemoryPool::new(budget)); + // A pool with room for the table's slice alone: the chunk that indexes it needs its own + // descriptor and rows on top of that, so it is refused and leaves nothing behind. + let pool = Arc::new(GreedyMemoryPool::new(table_cost)); let context = task_context(&pool); let plan = node(Scripted::plan(vec![vec![Step::Chunk(many, true)]]), 1); let mut output = output_of(&plan, 0, &context); @@ -7135,16 +7118,14 @@ async fn histogram_table_memory_is_charged_and_released_with_retained_chain() { /// /// This is what the whole-chain handover costs unless it is charged: a chain is buffered whole before /// it is routed, so a child that keeps one series open and never closes it would grow the buffer -/// without bound. Here the chain is charged to the pool as it grows, and the pool is sized from the -/// chunks rather than from a number picked by hand. +/// without bound. Here the chain is charged to the pool as it grows, and the pool has room for one +/// chunk of the chain and not for two: the growth that is refused is the one that would have made +/// the buffer exceed the pool. /// -/// The arithmetic is measured rather than assumed, and both sides of it matter. `per_chunk` is what -/// one chunk costs the read, measured through the same pack/unpack path. -/// The chain is `CHUNKS` of those, the pool holds `CHUNKS / 2` of them, so one chunk fits with room -/// to spare and the chain as a whole does not: the growth that is refused is the one that would have -/// made the buffer exceed the pool, at the `CHUNKS / 2 + 1`-th chunk. A change to what a chunk costs -/// moves the pool with it instead of silently making the refusal happen at the first chunk or not at -/// all - which is what a hard-coded byte count did here. +/// The room is counted in the data the chunks hold and not in the figure the read charges them with: +/// every chunk of the chain is a `Series` descriptor around a single float row, and four of those +/// descriptors are what the pool is made of, so one chunk fits and the chain of `CHUNKS` does not, +/// whatever the read goes on to charge for one. /// /// The child hands one chunk over per poll ([`Behaviour::OneAtATime`]) for a reason that is not /// cosmetic: what a producer hands over in one poll reaches the read as one chunk, so back-to-back @@ -7162,10 +7143,11 @@ async fn a_chain_that_outgrows_the_pool_ends_every_output_and_releases_it() { let steps = (0..CHUNKS) .map(|index| Step::chunk(chunk(shared.clone(), index, 1, false))) .collect::>(); - // What one chunk of this chain costs, measured through the read's own path on an unbounded pool. - // The probe closes its chain, so what it measures is a whole stream's worth of chunks rather than - // the error a producer that never closes one leaves behind. - let per_chunk = { + // Every chunk of the chain stands for its series descriptor at least, and the pool below holds + // four of those and no more. The probe is here for the shape of the stream it proves -- one chunk + // per poll -- not for how much one chunk costs. + let per_chunk = std::mem::size_of::(); + { let probe = Fragile::plan( vec![Behaviour::OneAtATime(vec![ Step::chunk(chunk(shared.clone(), 0, 1, false)), @@ -7195,11 +7177,6 @@ async fn a_chain_that_outgrows_the_pool_ends_every_output_and_releases_it() { chunks.len(), "one chunk per poll is what keeps the frame boundary from coalescing two chunks into one" ); - chunks - .iter() - .map(|(series, _)| operand_chunk_bytes(series)) - .max() - .expect("the probe chain has chunks of its own") }; let pool_size = per_chunk * (CHUNKS / 2); assert!( diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index 3eee224e8c2..6c9edb5f14a 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -46,8 +46,8 @@ use super::{ BinaryRuntime, Emitted, InputId, InputSlot, InstantRuntime, NativeChunk, NativeExecutionState, NativeExecutor, NodeState, PairKernel, Reader, RetainedInput, RuntimeNode, RuntimeOp, absorb_rebuilds, buffered_fields, buffered_time, evaluated_bytes, grid_chunk_bytes, - grid_end_ms, grid_position, map_chunk, map_workspace_bytes, operand_chunk_bytes, raw_bytes, - vector_series_with_k, vector_workspace_bytes, + grid_end_ms, grid_position, map_chunk, map_workspace_bytes, raw_bytes, vector_series_with_k, + vector_workspace_bytes, }; use crate::evaluated::EvaluatedChunk; use crate::grid_series::merge_same_labelset; @@ -3695,9 +3695,18 @@ fn a_pool_that_cannot_admit_a_collected_fragment_releases_what_it_held() { vec![Some(1.0), None], true, ); - // Room for the chunk the source queue holds, which is what the pool admits first, and nothing - // for the label sets the map records or the fragment the window collects. - let pool = bounded_pool(evaluated_bytes(&chunk) + 1); + // Room for the chunk the source queue holds -- read where the pool admits anything, so the limit + // is what the queue charges and not a figure derived from the estimator -- and nothing for the + // label sets the map records or the fragment the window collects. + let queued = { + let mut probe = executor(&tree, pool()); + probe + .push_evaluated(InputId::new(0), chunk.clone()) + .expect("the probe queue admits the chunk"); + probe.state().reserved_bytes() + }; + assert!(queued > 0, "the queued chunk is not charged for"); + let pool = bounded_pool(queued); let mut execution = executor(&tree, Arc::clone(&pool)); execution .push_evaluated(InputId::new(0), chunk) @@ -4393,9 +4402,18 @@ fn standalone_merge_collection_budget_failure_releases_input_and_stops() { vec![Some(1.0), None], true, ); - // The input queue is drained before collection grows the merge buffer; this budget admits that - // queue entry but not the larger retained-fragment estimate made by the collector. - let pool = bounded_pool(evaluated_bytes(&chunk) + 1); + // The input queue is drained before collection grows the merge buffer. The budget is what the + // queue charges -- read where the pool admits anything -- so it admits that entry and leaves + // nothing for the fragment the collector retains. + let queued = { + let mut probe = executor(&tree, pool()); + probe + .push_evaluated(InputId::new(0), chunk.clone()) + .expect("the probe queue admits the chunk"); + probe.state().reserved_bytes() + }; + assert!(queued > 0, "the queued chunk is not charged for"); + let pool = bounded_pool(queued); let mut execution = executor(&tree, Arc::clone(&pool)); execution .push_evaluated(InputId::new(0), chunk) @@ -4430,8 +4448,19 @@ fn standalone_merge_flush_budget_failure_releases_fragments_and_stops() { vec![Some(1.0), None], true, ); - let bytes = evaluated_bytes(&chunk); - let pool = bounded_pool(bytes * 2); + // What the collector holds once it has absorbed the queued fragment, read where the pool admits + // anything: a budget of exactly that admits the collection and leaves nothing for the flush's + // workspace. + let collected = { + let mut probe = executor(&tree, pool()); + probe + .push_evaluated(InputId::new(0), chunk.clone()) + .expect("the probe queue admits the chunk"); + assert!(probe.next_chunk().unwrap().is_none()); + probe.state().reserved_bytes() + }; + assert!(collected > 0, "the collected fragment is not charged for"); + let pool = bounded_pool(collected); let mut execution = executor(&tree, Arc::clone(&pool)); execution .push_evaluated(InputId::new(0), chunk) @@ -8160,14 +8189,14 @@ fn an_oversized_binary_operand_is_refused_by_the_pool() { assert_eq!(execution.state().reserved_bytes(), 0); } -/// A binary operand reserves each chunk it retains before it retains it, and holds exactly what -/// those chunks hold on their own. +/// A binary operand reserves each chunk it retains before it retains it, and holds what those +/// chunks hold between them. /// /// The figure of a retained chunk is what its rows occupy rather than the allocation they are slices /// of, so a series delivered in sibling slices of one batch is charged the batch's data once instead -/// of once per chunk. What is refused here is a figure that disagrees with what the operand holds -- -/// a chunk reserved for on top of what the operand already keeps would be refused by a pool sized to -/// the total, and one reserved for less would leave the pool holding less than the node does. +/// of once per chunk: the two halves of a 40 000-row batch cost the 320 000 bytes of its samples and +/// not twice that. A chunk is also reserved for exactly what retaining it charges, so a reservation +/// made before the fact does not leave the operand holding more, or less, than the pool gave it. #[test] fn a_binary_operand_reserves_what_it_retains() { let grid = grid(0, 60_000, 40_000); @@ -8177,54 +8206,49 @@ fn a_binary_operand_reserves_what_it_retains() { let second = evaluated_array_chunk_of(&schema, metric(), 20_000, batch.slice(20_000, 20_000), true); - // What each chunk holds on its own, and the entry the closing chunk records in the operand's - // boundary list -- the list is storage the operand keeps, and it is charged one entry per closed - // series as it is written. - let boundaries = std::mem::size_of::(); - let (first_alone, second_alone) = ( - operand_chunk_bytes(&first.series), - operand_chunk_bytes(&second.series), - ); - let held = first_alone + second_alone + boundaries; - - // The budget is the figure the operand ends up holding, so a chunk reserved for on top of what - // the operand keeps would be refused and one reserved for less would leave the pool holding less - // than the node does. - let pool = bounded_pool(held); + // The two chunks are the halves of one 40 000-row float batch, so what the operand holds + // between them is that batch's data: 320 000 bytes of samples, however the rows are cut up. A + // figure that charged the whole allocation per chunk would hold 640 000 for the same rows. + let rows = 40_000 * std::mem::size_of::(); + let descriptors = 4 * 1024; + let pool = bounded_pool(rows + descriptors); let (mut state, mut binary) = binary_runtime(&pool, &schema); - let before = state.buffer.reservation.size(); state .retain_binary(&mut binary, 0, NativeChunk::Evaluated(first)) .unwrap(); - assert_eq!( - state.buffer.reservation.size() - before, - first_alone, - "the first chunk of the operand does not cost what it is estimated at" - ); - let before = state.buffer.reservation.size(); state .retain_binary(&mut binary, 0, NativeChunk::Evaluated(second)) .unwrap(); - assert_eq!( - state.buffer.reservation.size() - before, - second_alone + boundaries, - "the closing chunk of the operand does not cost what it is estimated at, and the end it \ - records" + assert!( + state.buffer.reservation.size() >= rows, + "the operand reserves {} bytes, less than the {rows} of samples the two chunks hold", + state.buffer.reservation.size() + ); + assert!( + state.buffer.reservation.size() <= rows + descriptors, + "the operand reserves {} bytes for {rows} bytes of samples: a chunk is charged the \ + allocation it is a slice of rather than the rows it adds", + state.buffer.reservation.size() ); assert_eq!( + pool.reserved(), state.buffer.reservation.size(), - held, - "the operand does not reserve what it holds" + "the operand reserves less from the pool than it holds" ); - assert_eq!(pool.reserved(), held); - // The figure a caller reserves for before retaining a chunk is the one the operand grows by: the - // estimate of the series it carries and the one entry the chunk's end records. + // The figure a caller reserves for before retaining a chunk is the figure the operand grows by: + // one reserved for and then retained leaves the operand exactly where the reservation put it. let own = evaluated_chunk(&schema, 0, vec![1.0, 2.0], true); + let (mut own_state, mut own_binary) = binary_runtime(&pool, &schema); + let pending = own_binary.pending(0, &own).unwrap(); + let before = own_state.buffer.reservation.size(); + own_state + .retain_binary(&mut own_binary, 0, NativeChunk::Evaluated(own)) + .unwrap(); assert_eq!( - binary.pending(0, &own).unwrap(), - operand_chunk_bytes(&own.series) + std::mem::size_of::(), - "the figure reserved for a chunk is not what the operand grows by" + own_state.buffer.reservation.size() - before, + pending, + "retaining a chunk charges the operand something other than the figure reserved for it" ); // Assembling the two chunks keeps the allocation they are slices of: the series the kernel reads @@ -8465,8 +8489,8 @@ fn a_pool_that_holds_one_histogram_table_refuses_a_second() { } /// The executor's binary node holds no more than the operands it retains: the peak of its own -/// reservation is what every retained chunk holds on its own, not that plus a chunk it was handed -/// again. +/// reservation is the data the retained chunks hold between them -- the thirty-two thousand bytes of +/// one 4 000-row float batch, whichever way the batch is sliced -- and not a second copy of it. /// /// The source queue holds one chunk while the node retains the other, so what is read here is the /// binary node's own figure rather than the pool's: the overlap of the two nodes is a charge of the @@ -8479,13 +8503,11 @@ fn a_binary_beneath_the_executor_reserves_what_it_retains() { let first = evaluated_array_chunk_of(&schema, metric(), 0, batch.slice(0, 2_000), false); let second = evaluated_array_chunk_of(&schema, metric(), 2_000, batch.slice(2_000, 2_000), true); - // What the left operand holds once both chunks are retained: what each chunk holds on its own, - // and the boundary list the closing chunk adds one entry to. - let retained = std::mem::size_of::() - + [&first, &second] - .iter() - .map(|chunk| operand_chunk_bytes(&chunk.series)) - .fold(0usize, usize::saturating_add); + // Both chunks are halves of one 4 000-row float batch, so what the node holds once it retains + // them is that batch's data: 32 000 bytes of samples. A figure that charged the allocation per + // chunk would hold the same rows twice. + let retained = 4_000 * std::mem::size_of::(); + let descriptors = 4 * 1024; let mut execution = executor(&tree, bounded_pool(1 << 22)); let metrics = metrics_of(&execution); @@ -8495,10 +8517,16 @@ fn a_binary_beneath_the_executor_reserves_what_it_retains() { // nothing else: the kernel has not run and the node holds what it retains. assert!(execution.next_chunk().unwrap().is_none()); let peak = format!("native.p0.root.{}", NodeMetric::PEAK_RESERVED_BYTES); - assert_eq!( - gauge(&metrics, &peak), - retained, - "the binary node reserved more than the operands it retains" + let peak_bytes = gauge(&metrics, &peak); + assert!( + peak_bytes >= retained, + "the binary node reserved {peak_bytes} bytes, less than the {retained} bytes of samples \ + the two retained chunks hold" + ); + assert!( + peak_bytes <= retained + descriptors, + "the binary node reserved {peak_bytes} bytes for {retained} bytes of samples: it charged \ + the shared allocation once per chunk" ); // The right operand matches the label set, so the operation answers every point of the grid. diff --git a/src/promql/src/vector_match/tests.rs b/src/promql/src/vector_match/tests.rs index 53e8a47ed18..b0139ccb698 100644 --- a/src/promql/src/vector_match/tests.rs +++ b/src/promql/src/vector_match/tests.rs @@ -27,8 +27,8 @@ use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use super::{ - BinaryMatching, BinaryOp, Cardinality, FillValues, MatchingLabels, RESULT_ROW_BYTES, SetOp, - binary_vectors, binary_vectors_with_reservation, set_vectors, set_vectors_with_reservation, + BinaryMatching, BinaryOp, Cardinality, FillValues, MatchingLabels, SetOp, binary_vectors, + binary_vectors_with_reservation, set_vectors, set_vectors_with_reservation, }; use crate::grid_series::reference::{ Dense, OwnedHistogram, Points, assert_histogram_close, assert_result_eq, golden_cases, @@ -1781,9 +1781,11 @@ fn set_operators_of_sparse_operands_keep_their_own_positions() { assert_result_eq(Some(&result[0]), &expected, &lhs.metric); } -/// One row of the reservation that cannot be omitted: the point and the match group a filed row -/// stores, each charged twice for the capacity its `Vec` grows to. -const FILED_ROW: usize = 4 * std::mem::size_of::(); +/// One filed row, counted from what the row holds: the point it answers, the match group it belongs +/// to and the value it keeps. It is an independent unit of account -- the three things a row is made +/// of, not the figure the operation charges for one -- so a reservation that grew with something +/// else entirely would still be measured against it. +const RESULT_ROW_SLOTS: usize = std::mem::size_of::<(usize, usize, f64)>(); /// A grid of `points` positions one millisecond apart. fn budget_grid(points: usize) -> Arc { @@ -2202,7 +2204,7 @@ fn a_duplicated_group_is_charged_the_rows_it_files() { .max() .expect("the operation asks for its workspace"); let duplicated = *seen.iter().max().unwrap(); - let rows = points * FILED_ROW; + let rows = points * RESULT_ROW_SLOTS; assert!( duplicated >= single + rows / 2, "single {single}, duplicated {duplicated}: the second member's {rows} bytes of rows are not \ @@ -2216,7 +2218,7 @@ fn a_duplicated_group_is_charged_the_rows_it_files() { // What the operation holds once its tables are gone is the rows the two members filed, and no // match set kept past its field: the set is part of the field's phase and goes with it. let floor = retained_total(&seen); - let filed = 2 * points * RESULT_ROW_BYTES; + let filed = 2 * points * RESULT_ROW_SLOTS; assert!(floor >= filed, "floor {floor}, rows {filed}"); assert!( floor <= 4 * filed + 64 * 1024, @@ -2525,7 +2527,7 @@ fn a_filtered_comparison_files_a_row_that_carries_no_value() { let unmatched = total(&other); // The filtered run holds four rows the unmatched one does not, and both hold no output value. assert!( - filtered >= unmatched + 4 * FILED_ROW, + filtered >= unmatched + 4 * RESULT_ROW_SLOTS, "filtered {filtered}, unmatched {unmatched}" ); } @@ -2641,7 +2643,7 @@ fn only_the_field_that_matches_keeps_storage() { // What the operation still holds once its last field released its tables is the rows it filed: // no match slot of the field that matched, no table of the field that did not. let floor = retained_total(&seen); - let rows = points * RESULT_ROW_BYTES; + let rows = points * RESULT_ROW_SLOTS; assert!(floor >= rows, "floor {floor}, rows {rows}"); assert!( floor <= rows + 8 * 1024, @@ -2715,7 +2717,7 @@ fn successful_fields_keep_no_message_storage() { assert_eq!(narrow, wide, "narrow {narrow}, wide {wide}"); // The rows themselves are part of that figure, so neither run is trivially zero. assert!( - narrow >= 2 * points * RESULT_ROW_BYTES, + narrow >= 2 * points * RESULT_ROW_SLOTS, "floor {narrow} does not hold the rows both fields filed" ); } @@ -3202,7 +3204,7 @@ fn fields_holding_disjoint_points_are_charged_their_columns() { .iter() .max() .expect("the operation asks for its workspace"); - let filed = points * RESULT_ROW_BYTES; + let filed = points * RESULT_ROW_SLOTS; let triples = points * 2 * std::mem::size_of::<(usize, usize, GridSample)>(); let columns = points * fields * (std::mem::size_of::() + 1); assert!( @@ -3210,7 +3212,7 @@ fn fields_holding_disjoint_points_are_charged_their_columns() { "peak {peak} below the {filed} rows, {triples} triples and {columns} columns it holds" ); assert!( - peak <= points * (fields * 2 * (std::mem::size_of::() + 1) + 4 * RESULT_ROW_BYTES) + peak <= points * (fields * 2 * (std::mem::size_of::() + 1) + 8 * RESULT_ROW_SLOTS) + 128 * 1024, "peak {peak} for {points} rows of {fields} fields" ); @@ -3710,7 +3712,7 @@ fn a_set_operation_holds_the_rows_it_keeps() { let (dropped, unless) = retained(SetOp::Unless); assert_eq!((kept, dropped), (1, 0)); assert!( - and >= unless + POINTS * FILED_ROW, + and >= unless + POINTS * RESULT_ROW_SLOTS, "`and` holds {and} bytes and `unless` {unless}" ); }