From b30eaf5a7950ffc60cab4d59bd7f96647569b671 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:53:12 +0000 Subject: [PATCH 01/15] fix(cost): charge DDSketch states their expected bytes, KLL merges by k, output rows and carried labels as measured Rebased onto #1355's head with `column_magnitudes`: the PR's earlier commits (regret cases as failing tests, the fix, three tests kept under the new costs, the round-1 tests and fixes) applied as one, resolved against the base's declared value magnitudes, its no-wildcard rule in `operations_per_unit` and its restructured state-bytes tests. - A DDSketch state is charged the bytes it is expected to hold: the dense store over the span of its own rows under a named exponential spread (`exponential_values_magnitude_ratio`), not the library's worst case. - A KLL merge costs per byte of its input and merged state, a KLL estimate per byte, both fitted on the bytes the planner charges; the merged state is sized for the rows its group's states fold together (`state_input_rows`, carried by Stage 3's `logical_output` with the family). - A hash aggregate, a finalize and every estimate pay for each group's output row; a merge per output group; carried labels per row and label. - A summary state's row holds its labels and its own series identity (`state_row_bytes_beside_state`); `state_bytes_per_row` is the one rule for a state's bytes on an edge, compaction included. - Tests: the regret cases, a KLL merge within 2x of its measured time per series, the smallest offered KLL k selected, structural sharing, a disk-only objective, lognormal and narrow-gauge bands. The declared `column_magnitudes` range does not bound the DDSketch span yet; the next commits add that cap test-first. --- crates/cost/src/measured.rs | 6 + crates/cost/src/node/analytical.rs | 10 +- crates/cost/src/node/operations_per_unit.rs | 144 ++++++++-- crates/cost/src/node/state_bytes/mod.rs | 164 +++++++++-- crates/cost/src/node/state_bytes/tests.rs | 222 ++++++++++++--- crates/cost/src/node/usage/mod.rs | 135 +++++++-- crates/cost/src/node/usage/tests.rs | 267 ++++++++++++++++++ crates/cost/src/plan/cost.rs | 6 +- .../tests/default_cost_plan_ranking.rs | 114 ++++++++ .../integration-tests/tests/plan_sharing.rs | 52 +++- .../tests/promql_to_post_asap.rs | 32 ++- .../tests/stage3_objective.rs | 60 ++-- crates/stage3-plan-selection/src/cost.rs | 13 +- .../analytical-resource-cost.md | 16 +- 14 files changed, 1060 insertions(+), 181 deletions(-) diff --git a/crates/cost/src/measured.rs b/crates/cost/src/measured.rs index 3d2c368cd..f0468a63b 100644 --- a/crates/cost/src/measured.rs +++ b/crates/cost/src/measured.rs @@ -106,6 +106,12 @@ pub struct OperatorCost { #[serde(default, skip_serializing_if = "Option::is_none")] pub measure: Option, /// CPU nanoseconds per unit the operator processes ([`OperatorKind`]). + /// It includes what the formulas charge apart, at the measured shape: + /// the output rows a merge, an aggregate or an estimate builds per + /// group and the label columns a grouping carries per row. Example: a + /// `SummaryMerge` entry measured over 360 states per group holds its + /// per-group output row as a 360th of it per state; at 5 states per + /// group that row is under-charged. pub ns_per_unit: f64, /// CPU nanoseconds per computation of the node, whatever its units: /// per evaluation at query time, per second of arriving rows at diff --git a/crates/cost/src/node/analytical.rs b/crates/cost/src/node/analytical.rs index 3be752eac..d711bc438 100644 --- a/crates/cost/src/node/analytical.rs +++ b/crates/cost/src/node/analytical.rs @@ -4,7 +4,9 @@ //! separate. Plan selection (Stage 3) turns them into one objective with //! [`crate::ResourceUnitCost`]. -use crate::node::operations_per_unit::TIME_RANGE_OPERATIONS_PER_ROW; +use crate::node::operations_per_unit::{ + OUTPUT_ROW_OPERATIONS_PER_GROUP, TIME_RANGE_OPERATIONS_PER_ROW, +}; use crate::resources::ModeledCpu; use asap_types::summary_type::{SketchAlgorithm, SketchParams}; use serde::{Deserialize, Serialize}; @@ -468,7 +470,8 @@ pub(crate) fn estimate_operator( .. }, ) => ResourceEstimate::new( - checked_cpu_product(left.rows, operations_per_row)?, + checked_cpu_product(left.rows, operations_per_row)? + + *group_count as f64 * OUTPUT_ROW_OPERATIONS_PER_GROUP, checked_bytes(&[ *group_count, key_bytes @@ -2135,7 +2138,8 @@ mod tests { }, ) .unwrap(); - assert_eq!(ungrouped.cpu_ops(), 0.0); + // No row to fold; its one output row is still built. + assert_eq!(ungrouped.cpu_ops(), OUTPUT_ROW_OPERATIONS_PER_GROUP); assert_eq!(ungrouped.peak_memory_bytes(), 24); let grouped = estimate_operator( diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index fbde9c04c..a019daf10 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -1,6 +1,8 @@ //! The CPU operations the formulas charge per unit of work: per row a time //! range keeps, per row a hash aggregate groups, per row folded into a -//! summary state, per state merged, per group read out. +//! summary state, per row and label column a grouping carries, per state +//! merged and per byte of a KLL state merged or read, per output group of a +//! merge, per group read out and per output row built. //! //! One CPU operation is one nanosecond of the reference executor's work on //! one core: each count below is the executor's measured time for that @@ -89,14 +91,29 @@ pub(crate) const DDSKETCH_UPDATE_OPERATIONS_PER_ROW: f64 = 210.0; pub(crate) const KLL_UPDATE_OPERATIONS_PER_ROW: f64 = 95.0; /// One row folded into an exact accumulator that keeps one value (sum, -/// count, min, max): an exact Max accumulator built per series at query -/// time takes 40 ns per row. +/// count, min, max), beyond any label it carries +/// (`CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL`): over 1,000 series, an exact Max +/// built per series takes 35 ns per row at query time (median of 12 +/// runs, 300,000 to 36 M rows) and 44 at ingestion time, an exact Sum +/// grouped by `job` 30 at query time. pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 40.0; /// One row folded into an exact accumulator of a counter's increase (rate, /// increase, irate), which also tracks resets and the first and last -/// samples: 84 ns per row at query time, 84–89 at ingestion time. -pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 84.0; +/// samples, beyond any label it carries: built per series carrying a +/// `job` label it takes 77 ns per row at query time and 87 at ingestion +/// time, of which the label is `CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL` +/// (37). +pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 45.0; + +/// Per input row, for each label column a per-series state or group +/// carries beside its key (the `job` of a per-series sum that a later +/// `sum by (job)` reads): copying the label into the group. A per-series +/// exact `Aggregate` carrying `job` takes 51–77 ns per row where one +/// carrying nothing takes 18–38 at the same settings, and an exact Count +/// accumulator carrying it 79 ns where a Max carrying nothing takes 42: +/// 37 ns per row, the median difference. +pub(crate) const CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL: f64 = 37.0; /// One counter update of any other sketch, per counter its row updates /// ([`counters_updated_per_row`]), above @@ -118,9 +135,42 @@ pub(crate) const COUNTER_MERGE_OPERATIONS_PER_STATE: f64 = 350.0; /// state of 600 rows. pub(crate) const DDSKETCH_MERGE_OPERATIONS_PER_STATE: f64 = 2_500.0; -/// One KLL state merged: 5.4 µs per state of 10 rows, 8.8 µs per state of -/// 600 rows. -pub(crate) const KLL_MERGE_OPERATIONS_PER_STATE: f64 = 7_000.0; +/// One KLL state merged, beyond its bytes: the executor merges each input +/// state into a copy of its group's state, so a merge costs per byte of +/// the input state (`KLL_MERGE_OPERATIONS_PER_INPUT_BYTE`) and per byte of +/// the merged state (`KLL_MERGE_OPERATIONS_PER_MERGED_BYTE`), which grows +/// towards its `k`. The bytes are those the planner charges for each state +/// (`sketch_state_bytes` of the rows it folds), so the fit is over the +/// quantity it is applied to. Fitted over `asap_sketchlib`'s KLL at k = 200, +/// 547 and 1,404, states of 10 to 3,000 rows, 1 to 360 states per group +/// (1,000 groups, median of 3 runs), minimizing the squared log error: +/// within 2x of 20 of the 24 points, 0.41–2.4x. Example: merging 360 +/// states of 10 rows (872 B each) per group, charged 17.0 KB merged at +/// k = 547 and 33.5 KB at k = 1,404 (the library holds 8.9 and 33.5 KB), +/// is charged 7.7 and 14.9 µs per state, measured 4.3 and 13.0 (4.7 and +/// 13.3 in the executor): the charge past `k` is the item array's +/// capacity, so the smaller `k` is over-charged and the two differ by +/// 1.9x where they measure 3x. +pub(crate) const KLL_MERGE_OPERATIONS_PER_STATE: f64 = 360.0; + +/// Per byte of a KLL input state merged (see +/// `KLL_MERGE_OPERATIONS_PER_STATE`). +pub(crate) const KLL_MERGE_OPERATIONS_PER_INPUT_BYTE: f64 = 0.1; + +/// Per byte of the merged KLL state each input state is merged into (see +/// `KLL_MERGE_OPERATIONS_PER_STATE`). +pub(crate) const KLL_MERGE_OPERATIONS_PER_MERGED_BYTE: f64 = 0.43; + +/// Per output group of a `SummaryMerge`, beyond its input states' merges: +/// creating the group's state and its output row. Merging exact +/// accumulators over 1,000 groups costs 1.17 µs per group with one state +/// per group and 215 ns per state plus about 0.95 µs per group with 5 to +/// 360 (median of 20–26 runs per point). Less than a finalize's or an +/// estimate's output row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`, 2,500): a +/// merge's row takes its group's key columns and merged state as they are, +/// where a finalize or an estimate also reads each state into a new value +/// column. +const MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 950.0; /// Per byte of any other sketch state merged, above /// `EXACT_MERGE_OPERATIONS_PER_STATE`: merging adds every counter of the @@ -129,28 +179,42 @@ pub(crate) const KLL_MERGE_OPERATIONS_PER_STATE: f64 = 7_000.0; /// about 13 µs, 0.4–0.8 ns per byte. pub(crate) const SKETCH_MERGE_OPERATIONS_PER_BYTE: f64 = 1.0; -/// One group's answer read from a KLL state: 19–79 µs per group of states -/// folding 3,600 rows, 35 µs folding 36,000. -pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 35_000.0; +/// Per byte of the KLL state one group's answer is read from, before its +/// output row: the quantile sorts the items the state holds. The bytes are +/// those the planner charges for the state (`sketch_state_bytes`); 0.9 per +/// byte fits `asap_sketchlib`'s KLL at k = 200 to 1,404 over the same 24 +/// merged states as `KLL_MERGE_OPERATIONS_PER_STATE` (median of 3 runs over +/// 1,000 groups) within 2x at 21 of them. Example: a merged state of 360 +/// states of 10 rows is charged 15 µs at k = 547 (17.0 KB) and 30 µs at +/// k = 1,404 (33.5 KB), measured 23 and 42. +pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_BYTE: f64 = 0.9; -/// One group's answer read from any other sketch state: 0–2.6 µs per -/// DDSketch quantile, within the measurement's resolution (±3 ms per 1,000 +/// One group's answer read from any other sketch state, before its output +/// row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`): a DDSketch quantile estimate +/// measures 3.7 µs per group with its row (median of 11 nodes of 1,000 /// groups); `asap_sketchlib`'s own DDSketch quantile takes 0.15 µs and a /// Count-Min Sketch top-k query 0.5–8 µs. pub(crate) const SKETCH_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 1_000.0; -/// One exact accumulator finalized into its group's value: reading one -/// state, charged as folding one row into it -/// (`EXACT_UPDATE_OPERATIONS_PER_ROW`); measured within the resolution -/// above, so it costs no more than a hash aggregate's measure producing the -/// same value. -pub(crate) const FINALIZE_OPERATIONS_PER_GROUP: f64 = EXACT_UPDATE_OPERATIONS_PER_ROW; +/// One group's output row built from its value, with the group's labels +/// and series identity, by a hash aggregate, a finalize or an estimate (a +/// top-k estimate emits `k` rows per group and is charged one): a +/// finalize of exact accumulators measures 2.5 µs +/// per group (median over 135 nodes of 1,000 groups each, 1.8–15 µs). A +/// per-series hash aggregate builds the same rows: e.g. a per-series max +/// over 300,000 rows into 1,000 groups measures 11.3 ms, 8.1 ms of it its +/// rows' work (`hash_aggregate_operations_per_row`, 27 per row), about +/// 3 µs per group. +pub(crate) const OUTPUT_ROW_OPERATIONS_PER_GROUP: f64 = 2_500.0; /// Per state a tumbling-window chain completes and emits at the end of each /// window, beyond its rows' updates: fitting each window's time to its rows /// and its 1,000 states at 1 Hz (10 s windows of 10,000 rows) and at /// 10 Hz (60 s windows of 600,000 rows), 1.9 µs per exact Rate state, -/// 2.8 µs per KLL state and 3.4 µs per DDSketch state. +/// 2.8 µs per KLL state and 3.4 µs per DDSketch state. Measured apart from +/// `OUTPUT_ROW_OPERATIONS_PER_GROUP` and equal to it: both build one row per +/// group with its labels and series identity, here around a state, there +/// around a value. pub(crate) const CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 2_500.0; /// The operations one input row costs folded into a state of `summary_type` @@ -222,15 +286,25 @@ fn univmon_expected_layers(layers: u8) -> f64 { } /// The operations one input state of `family` holding `state_bytes` costs -/// merged into its group's state. -pub(crate) fn merge_operations_per_state(family: StateFamily<'_>, state_bytes: u64) -> f64 { +/// merged into its group's state, which holds `merged_state_bytes` once +/// merged. Example: a KLL state of 872 bytes merged into one of 33,512 +/// costs 360 + 0.1 × 872 + 0.43 × 33,512 = 14,857. +pub(crate) fn merge_operations_per_state( + family: StateFamily<'_>, + state_bytes: u64, + merged_state_bytes: u64, +) -> f64 { match family { StateFamily::Exact(kind) if kind.tracks_counter_increase() => { COUNTER_MERGE_OPERATIONS_PER_STATE } StateFamily::Exact(_) | StateFamily::None => EXACT_MERGE_OPERATIONS_PER_STATE, StateFamily::Sketch(SketchParams::DDSketch { .. }) => DDSKETCH_MERGE_OPERATIONS_PER_STATE, - StateFamily::Sketch(SketchParams::Kll { .. }) => KLL_MERGE_OPERATIONS_PER_STATE, + StateFamily::Sketch(SketchParams::Kll { .. }) => { + KLL_MERGE_OPERATIONS_PER_STATE + + KLL_MERGE_OPERATIONS_PER_INPUT_BYTE * state_bytes as f64 + + KLL_MERGE_OPERATIONS_PER_MERGED_BYTE * merged_state_bytes as f64 + } StateFamily::Sketch( SketchParams::Hydra(_) | SketchParams::Cms { .. } @@ -246,11 +320,22 @@ pub(crate) fn merge_operations_per_state(family: StateFamily<'_>, state_bytes: u } } +/// The operations a `SummaryMerge` with `output_groups` groups costs +/// beyond its input states' merges ([`merge_operations_per_state`]): each +/// group's state and output row. Example: 1,000 groups cost 950,000. +pub(crate) fn merge_output_operations(output_groups: u64) -> f64 { + output_groups as f64 * MERGE_OPERATIONS_PER_OUTPUT_GROUP +} + /// The operations reading one group's answer from a state of `family` -/// costs (an exact accumulator's: finalizing it). -pub(crate) fn estimate_operations_per_group(family: StateFamily<'_>) -> f64 { - match family { - StateFamily::Sketch(SketchParams::Kll { .. }) => KLL_ESTIMATE_OPERATIONS_PER_GROUP, +/// holding `state_bytes` costs, its output row included (an exact +/// accumulator's: finalizing it, the output row alone). Example: a +/// DDSketch quantile costs 1,000 + 2,500 = 3,500 per group. +pub(crate) fn estimate_operations_per_group(family: StateFamily<'_>, state_bytes: u64) -> f64 { + let read = match family { + StateFamily::Sketch(SketchParams::Kll { .. }) => { + KLL_ESTIMATE_OPERATIONS_PER_BYTE * state_bytes as f64 + } StateFamily::Sketch( SketchParams::DDSketch { .. } | SketchParams::Hydra(_) @@ -262,8 +347,9 @@ pub(crate) fn estimate_operations_per_group(family: StateFamily<'_>) -> f64 { | SketchParams::Hll { .. } | SketchParams::Kmv { .. }, ) => SKETCH_ESTIMATE_OPERATIONS_PER_GROUP, - StateFamily::Exact(_) | StateFamily::None => FINALIZE_OPERATIONS_PER_GROUP, - } + StateFamily::Exact(_) | StateFamily::None => 0.0, + }; + read + OUTPUT_ROW_OPERATIONS_PER_GROUP } /// The operations one input row costs a hash aggregate with diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index 8d67db565..a3d17efcf 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -2,25 +2,65 @@ //! per input row, from its parameters and the rows it folds. use crate::node::analytical::AnalyticalCostError; +use crate::node::operator_statistics::EdgeStatistics; use asap_types::ir::operator::AggIntent; -use asap_types::ir::schema::{DataType, FieldDataType, Schema}; +use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema, PROMQL_SERIES_IDENTITY}; use asap_types::summary_type::KLL_MAX_K; use asap_types::summary_type::{SketchParams, EXACT_ACCUMULATOR_STATE_BYTES}; use asap_types::workload::ValueMagnitudeRange; -/// Plain values are 8 bytes, strings 16; summary columns are sized apart. +/// Bytes of one output row: each column at [`column_bytes`]. pub fn row_bytes(schema: &Schema) -> u64 { + schema.fields.iter().map(column_bytes).sum::().max(1) +} + +/// Bytes one column of an output row is priced at: a string 16 (a label +/// value, or a series identity, which raw rows share one copy of per +/// series), any other value 8. Example: a PromQL sample row of a series +/// identity, `job`, its time and value is 16 + 16 + 8 + 8 = 48 bytes. +fn column_bytes(field: &Field) -> u64 { + match &field.dtype { + FieldDataType::Plain(DataType::Utf8) => 16, + _ => 8, + } +} + +/// Bytes a summary state's output row holds beside its state: its labels +/// and its window's time, each at [`column_bytes`] except the series +/// identity, which a state's row holds its own copy of, at +/// `SERIES_IDENTITY_BYTES`. Example: a stored per-series Sum carrying +/// `job` holds its window time, `job` and the series identity: 8 + 16 + 64 +/// = 88 bytes beside its 56-byte state, 144 in all, where the executor +/// holds 151 per state. +pub(crate) fn state_row_bytes_beside_state(schema: &Schema) -> u64 { schema .fields .iter() - .map(|f| match &f.dtype { - FieldDataType::Plain(DataType::Utf8) => 16, - _ => 8, + .map(|field| match &field.dtype { + FieldDataType::Sketch(..) | FieldDataType::ExactAggregate(..) => 0, + FieldDataType::Plain(DataType::Utf8) if field.name == PROMQL_SERIES_IDENTITY => { + SERIES_IDENTITY_BYTES + } + _ => column_bytes(field), }) - .sum::() - .max(1) + .sum() } +/// Bytes of the state one row of `edge` holds: its bytes per row without +/// what the row holds beside its state ([`state_row_bytes_beside_state`] of +/// the producer's `schema`). Example: 1,000 rows of stored per-series Sums +/// carrying `job`, 144,000 bytes, hold 144 − 88 = 56 bytes of state each. +pub(crate) fn state_bytes_per_row(edge: EdgeStatistics, schema: &Schema) -> u64 { + edge.bytes_per_row() + .saturating_sub(state_row_bytes_beside_state(schema)) +} + +/// Bytes of one series identity in a state's row: its serialized label +/// set, e.g. `{"instance":"host-17:9100","job":"api","region":"r1"}` +/// (about 55 bytes), and the column's 4-byte offset, rounded up. An +/// assumption: the data workload declares no label lengths. +const SERIES_IDENTITY_BYTES: u64 = 64; + /// Exact state that keeps every value or item of its group, so grows with /// the data; other exact accumulators are fixed-size. pub fn keeps_every_value(measure: &AggIntent) -> bool { @@ -42,14 +82,10 @@ pub fn keeps_every_value(measure: &AggIntent) -> bool { /// - a Count-Min / Count Sketch top-k heap full of keys of /// `ASSUMED_HEAP_KEY_BYTES`; each UnivMon layer's heap full of keys of /// `ASSUMED_VALUE_KEY_BYTES`; -/// - DDSketch and KMV: the rows the state folds, at least one (any number -/// when unknown), each a distinct item for KMV; a DDSketch's values span -/// `input_rows.value_magnitude_range` when declared, else -/// `DDSKETCH_ASSUMED_MAGNITUDE_RANGE`, and fill its negative store too -/// unless proven non-negative. Its bins are the bucket indices from -/// ⌊log_γ min_abs⌋ to ⌊log_γ max_abs⌋, at least one, for γ = (1 + α) / -/// (1 - α); this DDSketch has no bin limit to cap them. E.g. α = 0.01 -/// over [0.001, 10000] takes indices -346 to 460, 807 bins. +/// - KMV: the rows the state folds, at least one (any number when +/// unknown), each a distinct item; +/// - DDSketch: what it is expected to hold, not the most it can +/// ([`ddsketch_state_bytes`]). /// /// KLL is priced without the library's estimate: its fixed bytes /// (`kll_fixed_bytes`: the struct and its level array) plus the 8-byte @@ -155,18 +191,7 @@ pub fn sketch_state_bytes(params: &SketchParams, input_rows: StateInputRows) -> SketchParams::DDSketch { alpha } if !(*alpha > 0.0 && *alpha < 1.0) => { std::mem::size_of::() as u64 } - SketchParams::DDSketch { alpha } => { - let ValueMagnitudeRange { min_abs, max_abs } = input_rows - .value_magnitude_range - .unwrap_or(DDSKETCH_ASSUMED_MAGNITUDE_RANGE); - DdSketch::estimated_memory_bytes( - *alpha, - rows, - min_abs, - max_abs, - !input_rows.non_negative, - ) as u64 - } + SketchParams::DDSketch { alpha } => ddsketch_state_bytes(*alpha, input_rows), SketchParams::Hll { precision, .. } => { HyperLogLog::::estimated_memory_bytes(usize::from(*precision)) as u64 } @@ -254,14 +279,91 @@ const ASSUMED_HEAP_KEY_BYTES: usize = 3 * (1 + 4 + 16); /// A sample value's key owns no bytes, so this bounds it too. const ASSUMED_VALUE_KEY_BYTES: usize = 1 + 16; -/// The range of value magnitudes a DDSketch is assumed to see when its -/// column declares no `column_magnitudes`: nine decades, e.g. latencies from 1 ns -/// to 1 s, costed as magnitudes in `[1, 1e9]` (its bins depend on the ratio -/// alone). An assumption, not workload evidence. +/// The range of value magnitudes a DDSketch is assumed to see at most when +/// its column declares no `column_magnitudes`: nine decades, e.g. +/// latencies from 1 ns to 1 s, costed as magnitudes in `[1, 1e9]` (its +/// bins depend on the ratio alone). An assumption, not workload evidence. const DDSKETCH_ASSUMED_MAGNITUDE_RANGE: ValueMagnitudeRange = ValueMagnitudeRange { min_abs: 1.0, max_abs: 1e9, }; +/// The Euler–Mascheroni constant γ. +const EULER_GAMMA: f64 = 0.577_215_664_901_532_9; + +/// The ratio of the largest to the smallest of `rows` values a DDSketch +/// state is assumed to span when the workload declares nothing about its +/// values: `rows` samples of an exponential distribution, as latencies +/// roughly are, whose largest is about `H_n` times their mean and whose +/// smallest has a log about `ln(mean / n) − γ`, so a ratio of about +/// `n · H_n · e^γ`. Example: 10 values span a ratio of 52, 3,000 values +/// one of 4.6e4. +/// +/// The default when the data workload declares nothing about the values, +/// not workload evidence. At α = 0.01, values with a floor (lognormal +/// latencies, σ = 1) span less and are over-charged 1.1x; a narrow gauge +/// (100 ± 10), whose states hold one first block of buckets, 2.4x at 10 +/// values per state to 5.6x at 36,000; values with a heavier tail than an +/// exponential (Pareto) are under-charged. +fn exponential_values_magnitude_ratio(rows: u64) -> f64 { + let n = rows.max(1) as f64; + let harmonic = n.ln() + EULER_GAMMA + 1.0 / (2.0 * n); + n * harmonic * EULER_GAMMA.exp() +} + +/// The bytes a DDSketch of relative accuracy `alpha` is expected to hold +/// after folding `input_rows`: its struct, and per sign it holds (one when +/// its values are proven non-negative or it folds one row, else two) the +/// dense bucket store `asap_sketchlib` grows, one first block of buckets +/// (`DdSketch::estimated_memory_bytes` of one value) plus one 8-byte +/// counter per bucket its values span beyond the first. +/// +/// The span is that of the values the state folds, not of the column: the +/// magnitude ratio its rows are assumed to span +/// ([`exponential_values_magnitude_ratio`]), at most that of +/// `DDSKETCH_ASSUMED_MAGNITUDE_RANGE` (also the span when the rows are +/// unknown); a declared `column_magnitudes` range is not read yet. The library's worst case over every order of +/// inserts and merges (`estimated_memory_bytes`) is about twice the span +/// over the whole range, which is not what a state holds on average. +/// +/// Example: at α = 0.01, 10 exponential samples span a ratio of 52, 198 +/// buckets: 144 + 1,024 + 8 × 197 = 2,744 bytes, where `asap_sketchlib` +/// holds 2,910 on average; 3,000 samples span 538 buckets, 5,464 bytes +/// against 5,769 held. The library's bound for either is 18,752. +fn ddsketch_state_bytes(alpha: f64, input_rows: StateInputRows) -> u64 { + use asap_sketchlib::DdSketch; + let struct_bytes = std::mem::size_of::() as u64; + // One value allocates one store's first block. + let block_bytes = + DdSketch::estimated_memory_bytes(alpha, 1, 1.0, 1.0, false) as u64 - struct_bytes; + let sketch = DdSketch::new(alpha); + // Buckets a ratio of `ratio` between the largest and smallest + // magnitude spans, at least one. + let buckets_spanned = |ratio: f64| -> u64 { + let ratio = ratio.max(1.0); + match (sketch.bucket_index_for(ratio), sketch.bucket_index_for(1.0)) { + (Some(high), Some(low)) => u64::try_from(i64::from(high) - i64::from(low) + 1) + .unwrap_or(1) + .max(1), + _ => 1, + } + }; + let stores = match (input_rows.non_negative, input_rows.rows_per_state) { + (true, _) | (false, Some(0 | 1)) => 1, + (false, _) => 2, + }; + let rows_per_store = input_rows + .rows_per_state + .map(|rows| rows.div_ceil(stores).max(1)); + let column_ratio = + DDSKETCH_ASSUMED_MAGNITUDE_RANGE.max_abs / DDSKETCH_ASSUMED_MAGNITUDE_RANGE.min_abs; + let span = buckets_spanned(rows_per_store.map_or(column_ratio, |rows| { + exponential_values_magnitude_ratio(rows).min(column_ratio) + })); + let one_value = rows_per_store == Some(1); + let buckets_beyond_first = if one_value { 0 } else { span - 1 }; + struct_bytes + stores * (block_bytes + 8 * buckets_beyond_first) +} + #[cfg(test)] mod tests; diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index 131088675..03deb3999 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -28,6 +28,8 @@ fn exact_state_is_priced_at_the_executors_state_size() { enum Rows { /// One sample value a row. Values, + /// One latency-like sample value a row ([`latency_like`]). + LatencyLike, /// One label value a row, as Pass 1 feeds UnivMon a column. Labels, /// A label set and a weight a row. @@ -44,7 +46,7 @@ impl Rows { /// with a heap alone took about 22 s in a debug build. fn fed(&self) -> u32 { match self { - Rows::Values => 10_000, + Rows::Values | Rows::LatencyLike => 10_000, Rows::Labels | Rows::Keyed => 1_000, } } @@ -57,7 +59,7 @@ impl Rows { use asap_types::summary_type::{SummaryInputExpr, SummaryUpdate}; let item = Some(SummaryInputExpr::Column(ColumnRef::Named("item".into()))); match self { - Rows::Values => SummaryUpdate::column(ColumnRef::SampleValue), + Rows::Values | Rows::LatencyLike => SummaryUpdate::column(ColumnRef::SampleValue), Rows::Labels => SummaryUpdate { item, weight: SummaryInputExpr::Constant(1.0), @@ -95,13 +97,115 @@ fn library_ddsketch_bytes(alpha: f64, values: &[f64]) -> u64 { sketch.memory_bytes() as u64 } -/// A DDSketch is priced for what `asap_sketchlib` can hold after its -/// rows: at least what it holds for a few samples, for samples of either -/// sign, and for many samples over nine decades; a store less for values -/// declared non-negative. Many rows are priced as unknown ones. +/// A uniform draw in [0, 1) from a fixed hash of `x`. +fn uniform(mut x: u64) -> f64 { + x ^= x >> 33; + x = x.wrapping_mul(0xff51_afd7_ed55_8ccd); + x ^= x >> 33; + x = x.wrapping_mul(0xc4ce_b9fe_1a85_ec53); + x ^= x >> 33; + (x >> 11) as f64 / (1_u64 << 53) as f64 +} + +/// `n` latency-like values of series `series`: draws of an exponential +/// distribution of mean 10, from a fixed hash of `(series, i)`. +fn latency_like(series: u64, n: u32) -> Vec { + (0..u64::from(n)) + .map(|i| -(1.0 - uniform((series << 32) ^ i)).ln() * 10.0) + .collect() +} + +/// `n` values of series `series` with a floor, as measured latencies have: +/// draws of a lognormal distribution of median 10 ms and σ = 1 (half of +/// them within 5–20 ms), from a fixed hash of `(series, i)`. +fn lognormal(series: u64, n: u32) -> Vec { + (0..u64::from(n)) + .map(|i| { + // Box–Muller over two hashed uniforms. + let u1 = 1.0 - uniform((series << 32) ^ (2 * i)); + let u2 = uniform((series << 32) ^ (2 * i + 1)); + let normal = (-2.0 * u1.ln()).sqrt() * (std::f64::consts::TAU * u2).cos(); + 10.0 * normal.exp() + }) + .collect() +} + +/// `n` values of series `series` of a narrow gauge: uniform in 100 ± 10. +fn narrow_gauge(series: u64, n: u32) -> Vec { + (0..u64::from(n)) + .map(|i| 90.0 + 20.0 * uniform((series << 32) ^ i)) + .collect() +} + +/// The mean bytes `asap_sketchlib`'s DDSketch holds over 20 series of `n` +/// values each from `values`, of both signs when `signed`. +fn library_ddsketch_mean_bytes( + alpha: f64, + n: u32, + signed: bool, + values: fn(u64, u32) -> Vec, +) -> u64 { + let series = 20; + (0..series) + .map(|s| { + let values: Vec = values(s, n) + .into_iter() + .enumerate() + .map(|(i, v)| if signed && i % 2 == 0 { -v } else { v }) + .collect(); + library_ddsketch_bytes(alpha, &values) + }) + .sum::() + / series +} + +/// DDSketch states are charged as if their values spread like an +/// exponential's ([`exponential_values_magnitude_ratio`]); these pin how +/// far that assumption is from other value shapes, so a change to it shows +/// here. At α = 0.01, non-negative values, 10 to 36,000 per state: +/// lognormal latencies with a floor (σ = 1) are over-charged 1.08–1.13x; a +/// narrow gauge of 100 ± 10, whose states hold one first block of buckets +/// (1,168 bytes), 2.4x at 10 values to 5.6x at 36,000. #[test] -fn ddsketch_state_bytes_bound_the_library() { - let alpha = 0.05; +fn ddsketch_state_bytes_over_charge_narrower_values_within_their_bands() { + let alpha = 0.01; + let charged = |n: u32| { + sketch_state_bytes( + &SketchParams::DDSketch { alpha }, + StateInputRows { + rows_per_state: Some(u64::from(n)), + non_negative: true, + value_magnitude_range: None, + }, + ) as f64 + }; + for n in [10, 100, 600, 3_000, 36_000] { + let lognormal_held = library_ddsketch_mean_bytes(alpha, n, false, lognormal) as f64; + let ratio = charged(n) / lognormal_held; + assert!( + (1.0..=1.25).contains(&ratio), + "{n} lognormal values: charged {} for {lognormal_held} held, {ratio:.2}x", + charged(n) + ); + let gauge_held = library_ddsketch_mean_bytes(alpha, n, false, narrow_gauge) as f64; + let ratio = charged(n) / gauge_held; + assert!( + (2.0..=6.0).contains(&ratio), + "{n} gauge values: charged {} for {gauge_held} held, {ratio:.2}x", + charged(n) + ); + } +} + +/// A DDSketch state is charged what `asap_sketchlib` holds on average for +/// latency-like values, not the most it could hold over nine decades. At +/// α = 0.01 the executor's stored states of 1,000 latency series hold +/// 2.9 KB at 10 rows per state and 5.8 KB at 3,000, where the library's +/// worst case charged 18.8 KB for both, so stored quantile windows looked +/// 4x more expensive than they are. +#[test] +fn ddsketch_state_bytes_match_the_library_on_latency_like_values() { + let alpha = 0.01; let priced = |rows_per_state, non_negative| { sketch_state_bytes( &SketchParams::DDSketch { alpha }, @@ -112,37 +216,21 @@ fn ddsketch_state_bytes_bound_the_library() { }, ) }; - // Ten Pareto(2) samples (scale 1), one 1 s tumbling window of a 10 Hz series. - let pareto: Vec = (0..10) - .map(|i| (1.0 - (f64::from(i) + 0.5) / 10.0).powf(-0.5)) - .collect(); - let signed: Vec = pareto.iter().map(|v| -v).chain(pareto.clone()).collect(); - let decades: Vec = (0..10_000) - .map(|i| 10f64.powf(9.0 * f64::from((i * 7_919) % 10_000) / 10_000.0)) - .collect(); - let signed_decades: Vec = decades - .iter() - .enumerate() - .map(|(i, v)| if i % 2 == 0 { -v } else { *v }) - .collect(); - for (values, non_negative) in [ - (&pareto[..], true), - (&[-2.0][..], false), - (&[2.0][..], false), - (&[-2.0, 2.0][..], false), - (&signed[..], false), - (&decades[..], true), - (&signed_decades[..], false), - ] { - let rows = Some(values.len() as u64); - assert!( - library_ddsketch_bytes(alpha, values) <= priced(rows, non_negative), - "{} values, non-negative: {non_negative}", - values.len() - ); + for n in [1, 10, 100, 600, 3_000, 36_000] { + for signed in [false, true] { + let held = library_ddsketch_mean_bytes(alpha, n, signed, latency_like); + let charged = priced(Some(u64::from(n)), !signed); + assert!( + (1.0 / 1.5..=1.5).contains(&(charged as f64 / held as f64)), + "{n} values, signed {signed}: charged {charged}, held {held} on average" + ); + } } + // Unknown rows span the assumed nine decades, more than any number of + // latency-like values does. + assert!(priced(None, true) > priced(Some(1_000_000), true)); assert!(priced(Some(10_000), true) < priced(Some(10_000), false)); - assert_eq!(priced(Some(10_000), false), priced(None, false)); + assert!(priced(Some(10), true) < priced(Some(3_000), true)); } /// A DDSketch over a column declaring its value magnitudes is costed at @@ -231,14 +319,16 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() { /// Plan selection prices each state the executor builds per group at /// what the executor charges for it (#947). Each family is built /// through the executor's factory and fed `n` rows shaped as the -/// pricing assumes: signed values over [`DDSKETCH_ASSUMED_MAGNITUDE_RANGE`], -/// label values of [`ASSUMED_VALUE_KEY_BYTES`] keys for UnivMon, or +/// pricing assumes: signed values over [`DDSKETCH_ASSUMED_MAGNITUDE_RANGE`] +/// (signed latency-like values for DDSketch), label values of +/// [`ASSUMED_VALUE_KEY_BYTES`] keys for UnivMon, or /// keyed rows whose tuple keys are [`ASSUMED_HEAP_KEY_BYTES`] long. The /// heap families' shapes are dominated by their heaps. The charge is /// the sketch's bytes plus the accumulator's own fields, named per /// family. It equals the priced bytes where `asap_sketchlib`'s estimate /// is exact (Count-Min, Count Sketch, HLL, KMV at `n` distinct items) and -/// for KLL up to `k` rows. For DDSketch, the heap sketches and UnivMon +/// for KLL up to `k` rows. DDSketch is priced at what it is expected to +/// hold, within 1.5 times either way. For the heap sketches and UnivMon /// the price bounds it, by at most [`BYTES_AGREEMENT_FACTOR`] times once /// the state is full; /// UnivMon's price also bounds the bytes its sketch holds. @@ -269,6 +359,8 @@ fn state_bytes_bound_the_executors_charge() { /// At least, and at most [`BYTES_AGREEMENT_FACTOR`] times once the /// state is full. Bound, + /// Within 1.5 times either way. + Expected, } // (family, parameters, rows fed, how it is priced, the // accumulator's own fields, rows at which the state is full: four @@ -295,8 +387,8 @@ fn state_bytes_bound_the_executors_charge() { ( A::DDSketch, SketchParams::DDSketch { alpha: 0.01 }, - Rows::Values, - Priced::Bound, + Rows::LatencyLike, + Priced::Expected, 0, 10_000, ), @@ -394,6 +486,7 @@ fn state_bytes_bound_the_executors_charge() { let family = FieldDataType::Sketch(*algorithm); let mut updater = create_planner_accumulator(&family, Some(params), &rows.update()).unwrap(); + let latency = latency_like(0, n); for i in 0..n { match rows { // Distinct magnitudes over the assumed range, in a @@ -404,6 +497,11 @@ fn state_bytes_bound_the_executors_charge() { let sign = if i % 2 == 0 { 1.0 } else { -1.0 }; updater.update_single(sign * magnitude, 0).unwrap(); } + Rows::LatencyLike => { + let magnitude = latency[i as usize]; + let sign = if i % 2 == 0 { 1.0 } else { -1.0 }; + updater.update_single(sign * magnitude, 0).unwrap(); + } Rows::Labels => updater .update_value(&Value::Utf8(label(i).into()), 0) .unwrap(), @@ -431,6 +529,12 @@ fn state_bytes_bound_the_executors_charge() { charged <= priced + 8 * 2 * (*k as usize - 1), "{context}, {charged} charged" ), + // One value can be a zero, which no store holds. + Priced::Expected if n == 1 => assert!(charged <= priced, "{context}"), + Priced::Expected => assert!( + (1.0 / 1.5..=1.5).contains(&(priced as f64 / charged as f64)), + "{context}, {charged} charged" + ), Priced::Bound => { assert!(charged <= priced, "{context}, {charged} charged"); if n == *full { @@ -636,6 +740,7 @@ fn priced_bytes_agree_with_the_executor() { let mut updater = create_planner_accumulator(&family, params.as_ref(), &rows.update()).unwrap(); let mut seed = 0x5EED_u64; + let latency = latency_like(0, rows_fed); for i in 0..rows_fed { let key = (next_uniform(&mut seed) * f64::from(DISTINCT_KEYS)) as u32; match rows { @@ -646,6 +751,9 @@ fn priced_bytes_agree_with_the_executor() { .update_single(latency_ms, i64::from(i) * 1_000) .unwrap(); } + Rows::LatencyLike => updater + .update_single(latency[i as usize], i64::from(i) * 1_000) + .unwrap(), Rows::Labels => updater .update_value(&Value::Utf8(label(key).into()), 0) .unwrap(), @@ -708,3 +816,33 @@ fn priced_bytes_agree_with_the_executor() { } assert!(failures.is_empty(), "{}", failures.join("\n")); } + +/// A summary state's row holds its labels, its window's time and its +/// series identity beside the state; the state's own bytes, which a merge +/// is costed by, leave them out. A stored per-series Sum carrying `job`: +/// 144 bytes per row, 56 of them its state. +#[test] +fn state_bytes_per_row_leave_out_what_the_row_holds_beside_its_state() { + use asap_types::ir::schema::{Field, PROMQL_SERIES_IDENTITY}; + use asap_types::summary_type::{ExactKind, ExactParams}; + let schema = Schema::new(vec![ + Field::new("ts", FieldDataType::Plain(DataType::Int64), false), + Field::new( + "state", + FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + false, + ), + Field::new("job", FieldDataType::Plain(DataType::Utf8), false), + Field::new( + PROMQL_SERIES_IDENTITY, + FieldDataType::Plain(DataType::Utf8), + false, + ), + ]); + assert_eq!(state_row_bytes_beside_state(&schema), 88); + let edge = EdgeStatistics { + rows: 1_000, + bytes: 144_000, + }; + assert_eq!(state_bytes_per_row(edge, &schema), 56); +} diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 589a0edd2..f3b89d749 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -24,13 +24,15 @@ use crate::node::analytical::{ }; use crate::node::operations_per_unit::{ estimate_operations_per_group, hash_aggregate_operations_per_row, merge_operations_per_state, - update_operations_per_row, CHAIN_EMIT_OPERATIONS_PER_STATE, TIME_RANGE_OPERATIONS_PER_ROW, + merge_output_operations, update_operations_per_row, CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, + CHAIN_EMIT_OPERATIONS_PER_STATE, TIME_RANGE_OPERATIONS_PER_ROW, }; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, }; use crate::node::state_bytes::{ - keeps_every_value, row_bytes, sketch_state_bytes, summary_state_bytes, StateInputRows, + keeps_every_value, row_bytes, sketch_state_bytes, state_bytes_per_row, + state_row_bytes_beside_state, summary_state_bytes, StateInputRows, }; use crate::{aggregate_lookup, lookup, StateFamily}; use crate::{ @@ -212,6 +214,14 @@ pub struct ProducerOutput<'a> { /// `Filter`, `Project`), so a `TimeRange` over them selects windows; /// `None` for any other node. pub chain_tumbling_window_width_ms: Option, + /// What each summary state it outputs folds: the rows a `SummaryAgg` + /// or chain folds per state, a `SummaryMerge`'s input rows times the + /// states it merges per group, passed on by a `TimeRange` and the + /// operators that keep each row's window; `None` for any other node + /// or when unknown. Example: a 1h `TimeRange` over a chain of 10 s + /// windows of a 1 Hz series passes on 10 rows per state, and a merge + /// over it outputs states of 3,600 rows. + pub state_input_rows: Option, } impl ProducerOutput<'_> { @@ -258,6 +268,9 @@ pub struct NodeResourceUsageAndCost<'a> { /// `Filter`, `Project`), so a `TimeRange` over them selects windows; /// `None` for any other node. pub chain_tumbling_window_width_ms: Option, + /// What each summary state it outputs folds + /// ([`ProducerOutput::state_input_rows`]). + pub state_input_rows: Option, /// Work of one execution, in ms on one core. pub work_ms_per_execution: f64, /// CPU operations per second. @@ -298,6 +311,7 @@ impl<'a> NodeResourceUsageAndCost<'a> { unchanged_columns: self.unchanged_columns.clone(), tumbling_windows: self.tumbling_windows, chain_tumbling_window_width_ms: self.chain_tumbling_window_width_ms, + state_input_rows: self.state_input_rows, } } } @@ -370,6 +384,9 @@ pub fn node_resource_usage_and_cost<'a>( let mut raw_scan = None; // A query-time scan's raw rows retained. let mut raw_retained_rows = None; + // What each summary state it outputs folds, when it builds or merges + // states (else passed on below). + let mut built_state_input_rows: Option = None; let ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; // The outputs a node retains: its `retention_ms` over the time between // two outputs, at least one. A chain's outputs are its tumbling @@ -546,6 +563,12 @@ pub fn node_resource_usage_and_cost<'a>( (non_negative, range) }) }; + // The bytes of each summary state its first input holds. + let input_state_bytes = || { + producers.first().map_or(0, |producer| { + state_bytes_per_row(producer.output, producer.output_schema) + }) + }; let unary = |output| UnaryEdgeStatistics { input, output, @@ -662,13 +685,11 @@ pub fn node_resource_usage_and_cost<'a>( .. } => { let group_count = groups(reduction)?; - let keys = match reduction { - Reduction::Reduce(keys) => keys.keys().len() as u64, - Reduction::PerEntity => 1, - }; + let keys = grouping_key_count(reduction); let out = edge(group_count)?; let held = input.rows.div_ceil(group_count.max(1)); - let operations_per_row = hash_aggregate_operations_per_row(keys, measures, held); + let operations_per_row = hash_aggregate_operations_per_row(keys, measures, held) + + carried_label_operations_per_row(&node.output_schema, reduction); let estimate = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: keys, @@ -875,16 +896,18 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; + built_state_input_rows = Some(input_rows); let state_bytes = measured_state_bytes.unwrap_or(summary_state_bytes( family, node.parameters.as_ref(), input_rows, )?); - let update_ops = update_operations_per_row(family, node.parameters.as_ref()); + let update_ops = update_operations_per_row(family, node.parameters.as_ref()) + + carried_label_operations_per_row(&node.output_schema, reduction); let out = EdgeStatistics { rows: group_count, bytes: group_count - .checked_mul(state_bytes) + .checked_mul(state_bytes + state_row_bytes_beside_state(&node.output_schema)) .ok_or(AnalyticalCostError::Overflow)?, }; ( @@ -927,12 +950,14 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; + built_state_input_rows = Some(input_rows); let state_bytes = measured_state_bytes.unwrap_or(summary_state_bytes( family, node.parameters.as_ref(), input_rows, )?); - let update_ops = update_operations_per_row(family, node.parameters.as_ref()); + let update_ops = update_operations_per_row(family, node.parameters.as_ref()) + + carried_label_operations_per_row(&node.output_schema, reduction); // The states it completes in one execution: at ingestion time // one second's share of each group's window (a 10 s window: a // tenth of a state per group); at query time every retained @@ -954,7 +979,7 @@ pub fn node_resource_usage_and_cost<'a>( let out = EdgeStatistics { rows: group_count, bytes: group_count - .checked_mul(state_bytes) + .checked_mul(state_bytes + state_row_bytes_beside_state(&node.output_schema)) .ok_or(AnalyticalCostError::Overflow)?, }; ( @@ -992,21 +1017,47 @@ pub fn node_resource_usage_and_cost<'a>( ); } let states = merged_states(producers)?; - // Each input state's merge, by the states' family and bytes. + if let Some(keys) = group_by { + group_count = group_count.min(groups(&Reduction::by(keys.clone()))?); + } + // The merged state: each group's input states folded into one, + // sized for the rows they fold together when known (a KLL + // grows towards its `k`), else as one input state. + let states_per_group = states.div_ceil(group_count.max(1)); + let input_state_bytes = |producer: &ProducerOutput<'_>| { + state_bytes_per_row(producer.output, producer.output_schema) + }; + let merged_input_rows = producers + .first() + .and_then(|producer| producer.state_input_rows) + .map(|rows| StateInputRows { + rows_per_state: rows + .rows_per_state + .map(|per_state| per_state.saturating_mul(states_per_group)), + ..rows + }); + built_state_input_rows = merged_input_rows; + let merged_state_bytes = match (family, merged_input_rows) { + (StateFamily::Sketch(params), Some(rows)) => sketch_state_bytes(params, rows), + _ => producers.first().map_or(0, input_state_bytes), + }; + // Each input state's merge, by the states' family and bytes, + // and each output group's state and row. let merge_ops: f64 = producers .iter() .map(|producer| { producer.output.rows as f64 - * merge_operations_per_state(family, producer.output.bytes_per_row()) + * merge_operations_per_state( + family, + input_state_bytes(producer), + merged_state_bytes, + ) }) - .sum(); - if let Some(keys) = group_by { - group_count = group_count.min(groups(&Reduction::by(keys.clone()))?); - } + .sum::() + + merge_output_operations(group_count); let out = EdgeStatistics { rows: group_count, - bytes: input - .bytes_per_row() + bytes: (merged_state_bytes + state_row_bytes_beside_state(&node.output_schema)) .checked_mul(group_count) .ok_or(AnalyticalCostError::Overflow)?, }; @@ -1034,7 +1085,7 @@ pub fn node_resource_usage_and_cost<'a>( ( out, Ok(ResourceEstimate::new( - input.rows as f64 * estimate_operations_per_group(family), + input.rows as f64 * estimate_operations_per_group(family, input_state_bytes()), 0, 0, )), @@ -1044,7 +1095,7 @@ pub fn node_resource_usage_and_cost<'a>( Payload::ASAP(ASAPOp::FinalizeExactAccumulator { .. }) => ( edge(input.rows)?, Ok(ResourceEstimate::new( - input.rows as f64 * estimate_operations_per_group(family), + input.rows as f64 * estimate_operations_per_group(family, input_state_bytes()), 0, 0, )), @@ -1272,6 +1323,21 @@ pub fn node_resource_usage_and_cost<'a>( .then(|| chain_tumbling_window_width_ms(producers)) .flatten() }); + // A node that builds or merges states says what they fold; a time + // range and an operator keeping each row's window pass on their + // input's. + let state_input_rows = built_state_input_rows.or_else(|| { + let passes_states_on = + matches!(node.payload, Payload::NonASAP(NonASAPOp::TimeRange { .. })) + || node.payload.keeps_input_window(); + passes_states_on + .then(|| { + producers + .first() + .and_then(|producer| producer.state_input_rows) + }) + .flatten() + }); Ok(NodeResourceUsageAndCost { output: out, sources, @@ -1279,6 +1345,7 @@ pub fn node_resource_usage_and_cost<'a>( unchanged_columns, tumbling_windows, chain_tumbling_window_width_ms, + state_input_rows, work_ms_per_execution: node_ms, cpu_ops_per_second: estimate.cpu_ops() * per_second, scanned_bytes_per_second: estimate.scan_bytes() as f64 * per_second, @@ -1333,6 +1400,32 @@ fn selected_tumbling_windows( .max(1)) } +/// The grouping keys `reduction` hashes per row: its keys, or the entity +/// (one) when it keeps one row per entity. +fn grouping_key_count(reduction: &Reduction) -> u64 { + match reduction { + Reduction::Reduce(keys) => keys.keys().len() as u64, + Reduction::PerEntity => 1, + } +} + +/// The operations per input row a grouping by `reduction` pays for the +/// label columns it carries per group beside its keys, which each input +/// row copies: the string columns of its `output_schema` that are not +/// keys, at `CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL` each. Example: +/// the per-series sum under `sum by (job) (sum_over_time(latency[5m]))` +/// outputs the series identity (its key) and `job`, so it carries one +/// label: 37 operations per row. +fn carried_label_operations_per_row(output_schema: &Schema, reduction: &Reduction) -> f64 { + let label_columns = output_schema + .fields + .iter() + .filter(|field| field.dtype == FieldDataType::Plain(asap_types::ir::schema::DataType::Utf8)) + .count() as u64; + let carried = label_columns.saturating_sub(grouping_key_count(reduction)); + carried as f64 * CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL +} + /// The input states a `SummaryMerge` over `producers` reads: every row of /// each. fn merged_states(producers: &[ProducerOutput<'_>]) -> Result { diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 95e2c5e67..ac1cf5f39 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -60,6 +60,7 @@ fn a_merge_over_a_chain_s_windows_outputs_one_state_per_group() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: Some(60_000), + state_input_rows: None, }; let windows = node(Payload::NonASAP(NonASAPOp::TimeRange { range: Duration::from_secs(300), @@ -159,6 +160,7 @@ fn an_operator_keeping_its_input_window_over_a_chain_or_its_windows_selects_what unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: Some(60_000), + state_input_rows: None, }; for &step in path { let usage = @@ -209,6 +211,7 @@ fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { unchanged_columns: BTreeSet::new(), tumbling_windows: 5, chain_tumbling_window_width_ms: None, + state_input_rows: None, }; let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { children: vec![0], @@ -225,6 +228,7 @@ fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { assert_eq!( merge_usage.cpu_ops_per_second, 5_000.0 * crate::node::operations_per_unit::DDSKETCH_MERGE_OPERATIONS_PER_STATE + + crate::node::operations_per_unit::merge_output_operations(1_000) ); } @@ -254,6 +258,7 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { unchanged_columns: BTreeSet::new(), tumbling_windows: 5, chain_tumbling_window_width_ms: None, + state_input_rows: None, }; let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { children: vec![0], @@ -271,6 +276,7 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { assert_eq!( merge_usage.cpu_ops_per_second, 5_000.0 * crate::node::operations_per_unit::COUNTER_MERGE_OPERATIONS_PER_STATE + + crate::node::operations_per_unit::merge_output_operations(1_000) ); } @@ -306,6 +312,7 @@ fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + state_input_rows: None, }; let chain = PhysicalASAPDAGNode { retention_ms: Some(300_000), @@ -354,6 +361,7 @@ fn query_time_memory_is_held_only_while_the_node_computes() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + state_input_rows: None, }; let merge = PhysicalASAPDAGNode { lifetime: Lifetime::OneEvaluation(StorageMedium::Memory), @@ -484,6 +492,7 @@ fn an_ingestion_time_view_of_raw_rows_keeps_the_rows_its_retention_covers() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + state_input_rows: None, }; let row_schema = Schema::new(vec![ Field::plain("ts", DataType::Timestamp, false), @@ -546,6 +555,7 @@ fn a_view_without_retention_is_not_costed() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + state_input_rows: None, }; let shift = PhysicalASAPDAGNode { output_state: ExecutionDataState::INGESTION_ROWS, @@ -618,6 +628,7 @@ fn a_view_over_an_ingestion_time_chain_keeps_one_output_per_chain_window() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: Some(60_000), + state_input_rows: None, }; let usage = node_resource_usage_and_cost( &shift, @@ -672,6 +683,7 @@ fn ddsketch_state_bytes( unchanged_columns, tumbling_windows: 1, chain_tumbling_window_width_ms: None, + state_input_rows: None, }; let settings = CostInputs::default(); node_resource_usage_and_cost(&build, &[producer_output], (1, 1.0), None, data, &settings) @@ -878,3 +890,258 @@ fn a_ddsketch_over_a_counter_declaring_its_value_unknown_keeps_both_stores() { ddsketch_costed_bytes(true, None) ); } + +/// A schema of a window's time, `state` of `state_type` and the labels +/// `labels` (strings). +fn state_schema(state_type: FieldDataType, labels: &[&str]) -> Schema { + use asap_types::ir::schema::DataType; + let mut fields = vec![ + Field::new( + "window_end", + FieldDataType::Plain(DataType::Timestamp), + false, + ), + Field::new("state", state_type, false), + ]; + fields.extend( + labels + .iter() + .map(|label| Field::new(*label, FieldDataType::Plain(DataType::Utf8), false)), + ); + Schema::new(fields) +} + +/// A query-time merge of a 1h `TimeRange` over an ingestion-time chain of +/// KLL states in 10 s tumbling windows of 1,000 series sampled at 1 Hz +/// (`quantile_over_time(0.99, latency[1h])` at a 1% rank target): CPU +/// operations per second of the merge at KLL `k`. +fn kll_merge_cpu_ops(k: u32) -> f64 { + use asap_types::ir::properties::ExecutionDataState; + let settings = CostInputs::default(); + let data: DataWorkload = serde_json::from_value(serde_json::json!({ + "arrival": {"ingesting": {"rows_per_second": 1000.0}}, + "sources": {"latency": {"series_count": 1000, "metric_type": "gauge", + "column_signs": {"value": "non_negative"}, "sample_interval": 1000}} + })) + .unwrap(); + let scan_schema = Schema::default(); + let scan = node(Payload::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { + metric: "latency".into(), + }, + predicates: vec![], + schema: scan_schema, + })); + let scan_output = ProducerOutput { + payload: &scan.payload, + output_schema: &scan.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 48_000, + }, + sources: ["latency"].into(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + state_input_rows: None, + }; + let mut chain = node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 10_000, + })); + chain.output_state = ExecutionDataState::INGESTION_ROWS; + chain.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); + chain.retention_ms = Some(3_600_000); + chain.parameters = Some(SketchParams::Kll { k }); + chain.output_schema = state_schema( + FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), + &[asap_types::ir::schema::PROMQL_SERIES_IDENTITY], + ); + let chain_usage = node_resource_usage_and_cost( + &chain, + &[scan_output], + (1, 1.0), + Some(10_000), + &data, + &settings, + ) + .unwrap(); + let mut windows = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(3_600), + kind: TimeRangeKind::Range, + child: 0, + })); + windows.output_schema = chain.output_schema.clone(); + let windows_usage = node_resource_usage_and_cost( + &windows, + &[produced(&chain, &chain_usage)], + (1, 0.1), + None, + &data, + &settings, + ) + .unwrap(); + assert_eq!(windows_usage.output.rows, 360_000); + let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + merge.parameters = Some(SketchParams::Kll { k }); + merge.output_schema = chain.output_schema.clone(); + let merge_usage = node_resource_usage_and_cost( + &merge, + &[produced(&windows, &windows_usage)], + (1, 0.1), + None, + &data, + &settings, + ) + .unwrap(); + assert_eq!(merge_usage.output.rows, 1_000); + merge_usage.cpu_ops_per_second +} + +/// Stage 2 offers stored KLL windows of the same shape at k = 1,404 and +/// k = 547 for `quantile_over_time(0.99, latency[1h])` at a 1% rank +/// target. Merging each series' 360 windows of 10 rows measures 4.8 ms per +/// series at k = 1,404 and 1.7 ms at k = 547 in the executor, since each +/// window is merged into the group's state, which grows towards its `k`. A +/// merge charged per state alone cost both the same. The charge per series +/// is within 2x of each measurement, the merged state charged at the bytes +/// the planner gives it (17.0 KB at k = 547, where the library holds 8.9). +#[test] +fn a_kll_merge_costs_more_at_a_larger_k() { + let small = kll_merge_cpu_ops(547); + let large = kll_merge_cpu_ops(1_404); + assert!( + large > 1.5 * small, + "k = 1,404: {large} ops/s, k = 547: {small}; measured 2.8x" + ); + // Operations per second at one merge every 10 s, over 1,000 series, at + // the default 1e-6 ms per operation. + let ms_per_series = |ops_per_second: f64| ops_per_second * 10.0 / 1_000.0 * 1e-6; + for (k, ops_per_second, measured_ms) in [(547, small, 1.7), (1_404, large, 4.8)] { + let charged_ms = ms_per_series(ops_per_second); + assert!( + (0.5..=2.0).contains(&(charged_ms / measured_ms)), + "k = {k}: charged {charged_ms:.2} ms per series, measured {measured_ms}" + ); + } +} + +/// Finalizing 1,000 exact accumulators into their output rows measures +/// 2.5 µs per group (1.8–15 over 135 nodes); charged as one row's update +/// (40 ns) it made stored windows look cheaper than a query-time +/// accumulator grouped into 10 rows. +#[test] +fn a_finalize_charges_each_group_its_measured_time() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let merged = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + let merged_output = ProducerOutput { + payload: &merged.payload, + output_schema: &merged.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 56_000, + }, + sources: BTreeSet::new(), + family: StateFamily::Exact(&ExactKind::Sum), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + state_input_rows: None, + }; + let finalize = node(Payload::ASAP(ASAPOp::FinalizeExactAccumulator { child: 0 })); + let usage = node_resource_usage_and_cost( + &finalize, + &[merged_output], + (1, 1.0), + None, + &data, + &settings, + ) + .unwrap(); + assert!( + usage.work_ms_per_execution >= 1_000.0 * 2.0e-3, + "finalize of 1,000 groups charged {} ms, measured 2.5", + usage.work_ms_per_execution + ); +} + +/// A per-series exact aggregate that carries `job` beside the series +/// identity (under `sum by (job) (sum_over_time(latency[5m]))`) measures +/// 51–77 ns per row, one carrying nothing 18–38: the carried label costs +/// 37 ns per row more. +#[test] +fn a_per_series_aggregate_charges_the_labels_it_carries() { + use asap_types::ir::operator::AggIntent; + use asap_types::ir::schema::{DataType, PROMQL_SERIES_IDENTITY}; + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let rows = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let rows_output = ProducerOutput { + payload: &rows.payload, + output_schema: &rows.output_schema, + output: EdgeStatistics { + rows: 300_000, + bytes: 300_000 * 48, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + state_input_rows: None, + }; + let ms_per_row = |labels: &[&str]| { + let mut aggregate = node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::PerEntity, + measures: vec![AggIntent::Sum { col: None }], + output_names: vec![String::new()], + filters: vec![], + having: None, + child: 0, + })); + let mut fields = vec![ + Field::new("ts", FieldDataType::Plain(DataType::Timestamp), false), + Field::new("value", FieldDataType::Plain(DataType::Float64), false), + ]; + fields.extend( + labels + .iter() + .map(|label| Field::new(*label, FieldDataType::Plain(DataType::Utf8), false)), + ); + aggregate.output_schema = Schema::new(fields); + node_resource_usage_and_cost( + &aggregate, + std::slice::from_ref(&rows_output), + (1, 1.0), + None, + &data, + &settings, + ) + .unwrap() + .work_ms_per_execution + / 300_000.0 + }; + let carrying_job = ms_per_row(&["job", PROMQL_SERIES_IDENTITY]); + let carrying_nothing = ms_per_row(&[PROMQL_SERIES_IDENTITY]); + let carried_ns = (carrying_job - carrying_nothing) * 1e6; + assert!( + (30.0..=45.0).contains(&carried_ns), + "the carried job costs {carried_ns} ns per row, measured 37" + ); +} diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 91edcde02..d8e1a8036 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -11,7 +11,7 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use crate::node::analytical::AnalyticalCostError; use crate::node::operations_per_unit::merge_operations_per_state; use crate::node::operator_statistics::EdgeStatistics; -use crate::node::state_bytes::{sketch_state_bytes, StateInputRows}; +use crate::node::state_bytes::{sketch_state_bytes, state_bytes_per_row, StateInputRows}; use crate::node::usage::{cost_source, reads_one_batch_of}; use crate::plan::execution_demand::{computations, evaluation_rate, query_starts}; use crate::plan::read_back::read_back_ms; @@ -317,7 +317,9 @@ pub fn plan_cost( ) .map_or_else( || { - merge_operations_per_state(family, output[&id].bytes_per_row()) + // A partial copy merges into a state of the same window. + let state_bytes = state_bytes_per_row(output[&id], &nodes[&id].output_schema); + merge_operations_per_state(family, state_bytes, state_bytes) * speed.ms_per_cpu_op * formula_scale }, diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index c6c8da501..08462627b 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -267,6 +267,120 @@ fn sum_rate_at_1_hz_every_300_s_keeps_stored_rate_states() { assert_eq!(plan_kind(&plan), PlanKind::StoredStates); } +/// 10 Hz, every 10 s. +const TEN_HZ_EVERY_10_S: Setting = Setting { + sample_interval_ms: 100, + evaluation_interval_ms: 10_000, +}; +/// 1 Hz, every 60 s. +const ONE_HZ_EVERY_60_S: Setting = Setting { + sample_interval_ms: 1_000, + evaluation_interval_ms: 60_000, +}; + +/// 10 Hz, every 300 s. +const TEN_HZ_EVERY_300_S: Setting = Setting { + sample_interval_ms: 100, + evaluation_interval_ms: 300_000, +}; + +/// `quantile_over_time(0.99, latency[1h])`, 1% relative, at 10 Hz, raw data +/// kept. Measured cost per second (cost model 1's default prices on the +/// measured CPU and memory), exact plan against stored DDSketch windows: +/// 417.8 against 287.0 every 10 s, 71.3 against 44.0 every 60 s, 13.9 +/// against 11.0 every 300 s; the stored plan answers 4–80x faster. Each +/// stored state holds 4.3–5.4 KB (`DdSketch::memory_bytes`); charged the +/// 18.8 KB bound for values over nine decades, the stored plan was +/// predicted 2x more expensive than exact and never selected. +#[test] +fn quantile_at_10_hz_with_raw_data_keeps_stored_ddsketch_windows() { + for setting in [TEN_HZ_EVERY_10_S, TEN_HZ_EVERY_60_S, TEN_HZ_EVERY_300_S] { + let (plan, _) = selected(&workload(QUANTILE, setting), true); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates, "{setting:?}"); + assert!(has_sketch(&plan), "{setting:?}"); + } +} + +/// `plan` with every KLL it keeps or builds at `k`. +fn with_kll_k(plan: &PhysicalASAPDAG, k: u32) -> PhysicalASAPDAG { + use asap_types::summary_type::SketchParams; + let mut plan = plan.clone(); + for node in &mut plan.nodes { + if let Some(SketchParams::Kll { k: old }) = &mut node.parameters { + *old = k; + } + } + plan +} + +/// `quantile_over_time(0.99, latency[1h])`, 1% rank, at 1 Hz every 10 s and +/// every 60 s, raw data not kept. Stage 2 offers stored KLL windows at +/// k = 1,404 and at k = 547; merging 360 windows of 10 rows per series +/// measures 4.8 ms per series at k = 1,404 and 1.7 ms at k = 547 (the +/// merged sketch grows towards its `k`). Measured cost per second, k = +/// 1,404 against k = 547: 589 against 240 every 10 s, 31.4 against 19.4 +/// every 60 s. Charged a flat merge per state, both cost the same to the +/// last digit and the MILP picked either. It now selects the smaller `k`. +#[test] +fn quantile_rank_without_raw_data_charges_the_larger_kll_more() { + use asap_types::summary_type::SketchParams; + for setting in [ONE_HZ_EVERY_10_S, ONE_HZ_EVERY_60_S] { + let w = workload(QUANTILE_RANK, setting); + let (plan, _) = selected(&w, false); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates, "{setting:?}"); + let kll_ks: Vec = plan + .nodes + .iter() + .filter_map(|node| match node.parameters { + Some(SketchParams::Kll { k }) => Some(k), + _ => None, + }) + .collect(); + assert!( + !kll_ks.is_empty() && kll_ks.iter().all(|&k| k == 547), + "{setting:?}: selected KLL k {kll_ks:?}, the smallest offered is 547" + ); + let entries: Vec<_> = w.query_workload.entries().collect(); + let data = w.data_workload.clone().unwrap(); + let cost = |k| { + let (cost, _) = asap_cost::plan_cost( + &with_kll_k(&plan, k), + &entries, + &data, + &CostInputs::default(), + ) + .unwrap(); + cost.cost_per_second.unwrap() + }; + let (large, small) = (cost(1_404), cost(547)); + assert!( + large > 1.25 * small, + "{setting:?}: k = 1,404 predicted {large}/s, k = 547 {small}/s; measured 1.6-2.5x" + ); + } +} + +/// `sum by (job) (sum_over_time(latency[5m]))` at 1 Hz, raw data kept. +/// Measured cost per second, the query-time Sum accumulator grouped by +/// `job` against the selected plan: 1.44 against 1.73 (stored 10 s Sum +/// windows) every 10 s, 0.047 against 0.094 (the exact per-series +/// aggregate) every 300 s. The stored plan +/// pays a finalize of 2.5 µs per series (charged 40 ns), and the exact plan +/// carries each row's `job` label through its per-series aggregate (37 ns +/// per row, uncharged). +#[test] +fn sum_over_time_5m_at_1_hz_with_raw_data_builds_the_sum_at_query_time() { + let query = relative("sum by (job) (sum_over_time(latency[5m]))"); + for setting in [ONE_HZ_EVERY_10_S, ONE_HZ_EVERY_300_S] { + let (plan, _) = selected(&workload(query, setting), true); + assert_eq!( + plan_kind(&plan), + PlanKind::QueryTimeExactAccumulator, + "{setting:?}" + ); + } +} + /// A plan measured against the exact plan: the stored-state plan (Stage 3's /// selection without raw data), or a plan built at query time from raw data /// that Stage 3 selected before the measured unit costs, kept as a fixture diff --git a/crates/integration-tests/tests/plan_sharing.rs b/crates/integration-tests/tests/plan_sharing.rs index 1c1508c64..a7724a842 100644 --- a/crates/integration-tests/tests/plan_sharing.rs +++ b/crates/integration-tests/tests/plan_sharing.rs @@ -3,7 +3,8 @@ //! more than planning the first query alone. use crate::planning_common::{cost, cost_with, fuzz_workload, selected_plan}; use asap_cost::CostInputs; -use asap_types::ir::NonASAPOp; +use asap_devtools::planner_fuzz::{FuzzQuery, FuzzWorkload}; +use asap_types::ir::{ASAPOp, NonASAPOp}; use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload as Payload; use serde_json::json; @@ -100,7 +101,10 @@ fn duplicate_exact_topk_shares_work() { assert!(duplicate <= single * 1.001, "{duplicate} vs {single}"); } -/// SQL variants of the nested and slower-repeat cases: nested windows and slower queries reuse one sum. +/// SQL variants of the nested and slower-repeat cases: nested windows and +/// slower queries reuse one sum. The plan builds the sum once, and the +/// added reader costs far less than planned alone: it reads the shared sum +/// and pays only for its own answer each evaluation (its finalize). #[test] fn sql_sum_shares_nested_windows_and_slower_readers() { let mut w = fuzz_workload("identical_percentiles", false); @@ -112,23 +116,47 @@ fn sql_sum_shares_nested_windows_and_slower_readers() { ) .unwrap(); let single = cost(&w); - let mut nested = w.clone(); + // The nodes of `w`'s selected plan that build the sum: a summary state, + // stored or not, or an exact aggregate. + let sum_builders = |w: &FuzzWorkload| { + selected_plan(w, &CostInputs::default()) + .nodes + .iter() + .filter(|node| { + matches!( + node.payload, + Payload::ASAP( + ASAPOp::SummaryAgg { .. } | ASAPOp::SummaryTumblingWindowChain { .. } + ) | Payload::NonASAP(NonASAPOp::Aggregate { .. }) + ) + }) + .count() + }; + // A reader added to `w`: it builds no sum of its own, and costs less + // than a tenth of what it costs planned alone. + let assert_shares = |w: &FuzzWorkload, reader: &FuzzQuery| { + let mut with_reader = w.clone(); + with_reader.queries.push(reader.clone()); + let mut alone = w.clone(); + alone.queries = vec![reader.clone()]; + assert_eq!(sum_builders(&with_reader), 1, "{}", reader.entry.query.0); + let marginal = cost(&with_reader) - single; + let standalone = cost(&alone); + assert!( + marginal < 0.1 * standalone, + "{}: adds {marginal}, alone {standalone}", + reader.entry.query.0 + ); + }; let mut shorter = w.queries[0].clone(); shorter.entry.query.0 = shorter.entry.query.0.replace("'5'", "'1'"); - nested.queries.push(shorter); - let nested_cost = cost(&nested); - assert!(nested_cost <= single * 1.001, "{nested_cost} vs {single}"); + assert_shares(&w, &shorter); let mut slower = w.queries[0].clone(); slower.entry.demand = serde_json::from_value( json!({"fixed_interval_at": {"interval": 120000, "evaluation_phase": 0}}), ) .unwrap(); - w.queries.push(slower); - let repeated_cost = cost(&w); - assert!( - repeated_cost <= single * 1.001, - "{repeated_cost} vs {single}" - ); + assert_shares(&w, &slower); } /// Two identical exact percentile queries pay for their result once. diff --git a/crates/integration-tests/tests/promql_to_post_asap.rs b/crates/integration-tests/tests/promql_to_post_asap.rs index 433e81864..852ea9533 100644 --- a/crates/integration-tests/tests/promql_to_post_asap.rs +++ b/crates/integration-tests/tests/promql_to_post_asap.rs @@ -29,7 +29,12 @@ use asap_types::summary_type::{ /// a query at `accuracy` that uses an ASAP operator, or the plain exact one /// when none does: the summary-bound form the pins below inspect. fn realize(root: &Rc, accuracy: AccuracyTarget) -> Rc { - let candidates = logical_candidates(Rc::clone(root), accuracy); + realize_from(logical_candidates(Rc::clone(root), accuracy)) +} + +/// The first of `candidates` that uses an ASAP operator, or the first when +/// none does, checked against the IR contract. +fn realize_from(candidates: Vec>) -> Rc { let node = candidates .iter() .find(|candidate| candidate.contains_asap()) @@ -105,6 +110,23 @@ fn lower_and_realize(query: &str) -> Rc { realize(&pre, AccuracyTarget::exact()) } +/// `query` lowered at an exact target and realized +/// ([`realize_from`]) from the plan selected with accuracy guarantees +/// ignored and summaries' CPU costed free +/// ([`logical_candidates_ignoring_guarantees`]): for tests of a summary +/// plan's shape. Under the default unit costs a query-time exact +/// accumulator pays its finalize (2.5 µs per group) and can lose to the +/// plain exact plan on these tests' small data. +fn lower_and_realize_summaries(query: &str) -> Rc { + let pre = lower_promql(query, AccuracyTarget::exact()).expect("lowering failed"); + let root = realize_from(logical_candidates_ignoring_guarantees( + pre, + AccuracyTarget::exact(), + )); + assert!(root.contains_asap(), "{query}: a candidate reads a summary"); + root +} + #[test] fn instant_topk_and_unsupported_child_remain_local_residuals() { for query in ["topk(3, memory_bytes)", "topk(3, deriv(memory_bytes[5m]))"] { @@ -134,7 +156,7 @@ fn dtype<'a>(schema: &'a Schema, name: &str) -> &'a FieldDataType { #[test] fn promql_binary_arithmetic_retains_two_summary_leaves() { for op in ["+", "-", "*", "/", "%", "^", "atan2"] { - let root = lower_and_realize(&format!("rate(a[1m]) {op} rate(b[1m])")); + let root = lower_and_realize_summaries(&format!("rate(a[1m]) {op} rate(b[1m])")); let Some(NonASAPOp::BinaryOp { lhs, rhs, .. }) = root.non_asap() else { panic!("expected BinaryOp for {op}, got {:?}", root.operator); }; @@ -222,7 +244,7 @@ fn promql_binary_arithmetic_preserves_both_scalar_operand_orders() { /// the other arm still binds its exact accumulator. #[test] fn promql_binary_arithmetic_keeps_an_unsupported_arm_exact() { - let root = lower_and_realize("rate(a[1m]) + stddev_over_time(b[1m])"); + let root = lower_and_realize_summaries("rate(a[1m]) + stddev_over_time(b[1m])"); let Some(NonASAPOp::BinaryOp { lhs, rhs, .. }) = root.non_asap() else { panic!("expected BinaryOp, got {:?}", root.operator); }; @@ -235,7 +257,7 @@ fn promql_binary_arithmetic_keeps_an_unsupported_arm_exact() { #[test] fn promql_binary_arithmetic_preserves_nested_structure_and_vector_matching() { - let nested = lower_and_realize("(rate(a[1m]) + rate(b[1m])) / 2"); + let nested = lower_and_realize_summaries("(rate(a[1m]) + rate(b[1m])) / 2"); let Some(NonASAPOp::Project { child: lhs, .. }) = nested.non_asap() else { panic!("expected outer BinaryOp, got {:?}", nested.operator); }; @@ -243,7 +265,7 @@ fn promql_binary_arithmetic_preserves_nested_structure_and_vector_matching() { // A vector-matching modifier stays on the BinaryOp over the two // finalized rates. - let modified = lower_and_realize("rate(a[1m]) + on(job) rate(b[1m])"); + let modified = lower_and_realize_summaries("rate(a[1m]) + on(job) rate(b[1m])"); let Some(NonASAPOp::BinaryOp { operator, lhs, rhs, .. }) = modified.non_asap() diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index 016ca54d1..1de59718e 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -264,32 +264,33 @@ fn offset_windows_without_raw_data_select_installable_plans_costed_for_their_ret } } -/// Unit costs with a disk price like a cloud block volume's (3.1e-17 per -/// byte-second): what keeping raw data on disk costs per second is below -/// 1e-9, where the solver drops a constraint coefficient as zero. CPU is -/// priced at half AWS Fargate's vCPU-second (`cost_per_cpu_op` 5.622e-15), -/// so that disk decides between the plans below. -fn small_disk_costs() -> CostInputs { +/// Unit costs that charge disk alone, at a price like a cloud block +/// volume's (3.1e-17 per byte-second): what keeping raw data on disk costs +/// per second is below 1e-9, where the solver drops a constraint +/// coefficient as zero, and every other resource is free. +fn disk_only_costs() -> CostInputs { CostInputs { cost_model: CostModel::UsageOverTime(ResourceUnitCost { - cost_per_cpu_op: 5.622e-15, - cost_per_scanned_byte: 5.622e-16, - cost_per_memory_byte_second: 1.150183379650116e-15, + cost_per_cpu_op: 0.0, + cost_per_scanned_byte: 0.0, + cost_per_memory_byte_second: 0.0, + cost_per_provisioned_memory_byte_second: 0.0, + cost_per_provisioned_cpu_core_second: 0.0, cost_per_disk_byte_second: 3.1e-17, - ..ResourceUnitCost::illustrative() }), ..CostInputs::default() } } /// `sum_over_time(m[5m] offset 30m)` and `sum_over_time(m[5m])` every -/// minute and `count_over_time(m[2m])` every hour, raw data kept, disk -/// charged at 3.1e-17 per byte-second: raw data on disk costs about 2e-9 -/// per second. Its row carries the objective's scale, so the solver keeps -/// it, and the plan selected costs less than the one selected when disk -/// is free, costed at the disk price (that one is 50% dearer). With the +/// minute and `count_over_time(m[2m])` every hour, raw data kept, with disk +/// the only resource charged (3.1e-17 per byte-second): a plan that scans +/// raw data keeps it on disk for about 2e-9 per second, one that reads +/// only stored windows costs nothing. The raw-data row carries the +/// objective's scale, so the solver keeps that 2e-9 and selects a plan +/// keeping no raw data, whose objective is its `plan_cost`, 0. With the /// scale on the raw-data variable's objective coefficient instead, the -/// solver dropped the unscaled coefficients and selected that plan. +/// solver dropped the unscaled coefficients and could select either. #[test] fn raw_data_on_disk_costing_less_than_the_solver_tolerance_still_steers_selection() { let w = workload(&[ @@ -297,22 +298,23 @@ fn raw_data_on_disk_costing_less_than_the_solver_tolerance_still_steers_selectio ("sum_over_time(m[5m])", 60_000, ""), ("count_over_time(m[2m])", 3_600_000, ""), ]); - let settings = small_disk_costs(); - let (_, (objective, plan_cost)) = selected_plan_objective_and_plan_cost(&w, &settings, true); - assert_equal("small disk price", (objective, plan_cost)); - let mut free_disk = settings.clone(); - if let CostModel::UsageOverTime(unit_costs) = &mut free_disk.cost_model { - unit_costs.cost_per_disk_byte_second = 0.0; - } - let (ignoring_disk, _) = selected_plan_objective_and_plan_cost(&w, &free_disk, true); + let settings = disk_only_costs(); + let (plan, (objective, plan_cost)) = selected_plan_objective_and_plan_cost(&w, &settings, true); + assert_equal("disk only", (objective, plan_cost)); + assert_eq!( + plan_cost, 0.0, + "the selected plan keeps raw data: {plan:#?}" + ); + // A plan scanning raw data, the one selected under the default costs, + // pays for it on disk. + let (scanning, _) = selected_plan_objective_and_plan_cost(&w, &CostInputs::default(), true); let entries: Vec<_> = w.query_workload.entries().collect(); let data = w.data_workload.clone().unwrap(); - let (ignoring_disk_cost, _) = - asap_cost::plan_cost(&ignoring_disk, &entries, &data, &settings).unwrap(); - let ignoring_disk_cost = ignoring_disk_cost.cost_per_second.unwrap(); + let (scanning_cost, _) = asap_cost::plan_cost(&scanning, &entries, &data, &settings).unwrap(); + let scanning_cost = scanning_cost.cost_per_second.unwrap(); assert!( - plan_cost < ignoring_disk_cost * (1.0 - 1e-6), - "selected {plan_cost} is not cheaper than {ignoring_disk_cost}, the plan selected ignoring disk" + (1e-10..1e-8).contains(&scanning_cost), + "a plan scanning raw data costs {scanning_cost} on disk" ); } diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index 2af18c0d0..e94869807 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -640,10 +640,14 @@ fn reads_one_batch_of( } /// An input's logical output over its readable costed candidates' `usages`: the most -/// rows and bytes any outputs, the state family of the one with the most -/// bytes, the sources any reads and the columns every one leaves unchanged; -/// `payload` and `schema` the group's own. `None` without a costed -/// candidate. +/// rows and bytes any outputs, the state family and the rows each state +/// folds of the one with the most bytes, the sources any reads and the +/// columns every one leaves unchanged; `payload` and `schema` the group's +/// own. `None` without a costed candidate. Example: of two KLL candidates, +/// one at k = 547 whose states fold 10 rows and one at k = 1,404 whose +/// states fold 3,600, the larger gives both its family and its rows, so a +/// merge over the input never sizes one candidate's `k` for the other's +/// rows. fn logical_output<'a: 'b, 'b>( payload: &'a PhysicalASAPOperatorPayload, schema: &'a asap_types::ir::schema::Schema, @@ -657,6 +661,7 @@ fn logical_output<'a: 'b, 'b>( }; if usage.output.bytes > known.output.bytes { known.family = usage.family; + known.state_input_rows = usage.state_input_rows; } known.output.rows = known.output.rows.max(usage.output.rows); known.output.bytes = known.output.bytes.max(usage.output.bytes); diff --git a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md index 2d595c8cc..67d8f7c4d 100644 --- a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md +++ b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md @@ -790,9 +790,19 @@ from logical group count alone. Summary merge, subtract, delete, and readout are separate physical operators. Their CPU and memory use the concrete summary state size and number of input states. A merge costs its family's operations per input state (per byte for unmeasured -sketches), and outputs one state per -group: the input's groups, or for a rollup (`group_by`) the groups of its -keys, estimated like any `by (keys)` and at most the input's groups. A plan +sketches; for KLL also per byte of the input state and of the merged state, +which grows towards `k`), plus each output group's state and row, and outputs +one state per group: the input's groups, or for a rollup (`group_by`) the +groups of its keys, estimated like any `by (keys)` and at most the input's +groups. The merged state is sized for the rows its group's input states fold +together: 360 stored KLL windows of 10 rows each merge into a state sized for +3,600 rows. A readout (estimate or finalize) costs each group's output row, +plus reading its state (a KLL's per byte). A state's row on any edge holds +its labels, its window's time and its series identity beside the state; a +DDSketch state is sized for the values it is expected to hold, spread like +latencies, not for the most it could hold. A grouping that carries label +columns beside its keys (a per-series sum keeping `job` for a later +`sum by (job)`) costs per row and carried label. A plan using one of these operations is unavailable until the corresponding formula and required evidence are present. From d0190feb30bb1bd38fa2a12a685b1ebfc91ce2eb Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:56:53 +0000 Subject: [PATCH 02/15] test(cost): the declared range caps a DDSketch's span; Max merges and exact chain emits at their measured cost; a few-state KLL merge per group; a merge sized from all its inputs (failing) --- crates/cost/src/node/state_bytes/tests.rs | 56 +++++-- crates/cost/src/node/usage/tests.rs | 145 ++++++++++++++++-- .../tests/default_cost_plan_ranking.rs | 15 ++ 3 files changed, 192 insertions(+), 24 deletions(-) diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index 03deb3999..a075b277d 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -233,38 +233,64 @@ fn ddsketch_state_bytes_match_the_library_on_latency_like_values() { assert!(priced(Some(10), true) < priced(Some(3_000), true)); } -/// A DDSketch over a column declaring its value magnitudes is costed at -/// the bins they span, not nine decades. At α = 0.01, γ = 1.01 / 0.99 and -/// a value `v` falls in bin `floor(ln v / ln γ)`: magnitudes in -/// [0.001, 10000] take bins -346 to 460, 807 bins, where the default -/// [1, 1e9] takes 0 to 1036, 1,037. With three or more non-negative rows -/// the library's bound is `2 (bins + 126)` 8-byte slots plus the struct. +/// A DDSketch's span is the smallest of the exponential spread of its +/// rows, its column's declared `column_magnitudes` and the assumed nine +/// decades. At α = 0.01, non-negative values: +/// - a narrow gauge declared within [90, 110] spans a ratio of 1.22, 11 +/// buckets: 1,248 bytes at any number of rows, where `asap_sketchlib` +/// holds 1,168 (undeclared, 2,744 at 10 rows and 6,552 at 36,000); +/// - a range wider than the rows' spread (the latency generator's +/// [2.7e-8, 1104], 10.6 decades) leaves the spread's bytes unchanged; +/// - with the rows unknown, a declared [0.001, 10000] spans seven decades +/// instead of nine. #[test] -fn ddsketch_state_bytes_follow_the_declared_value_magnitude_range() { +fn ddsketch_state_bytes_are_capped_by_the_declared_value_magnitude_range() { let alpha = 0.01; - let costed = |value_magnitude_range| { + let costed = |rows_per_state, value_magnitude_range| { sketch_state_bytes( &SketchParams::DDSketch { alpha }, StateInputRows { - rows_per_state: Some(600), + rows_per_state, non_negative: true, value_magnitude_range, }, ) }; - let struct_bytes = std::mem::size_of::() as u64; - let bytes_for_bins = |bins: u64| struct_bytes + 8 * 2 * (bins + 126); + let gauge = ValueMagnitudeRange { + min_abs: 90.0, + max_abs: 110.0, + }; + for n in [10_u32, 600, 36_000] { + let rows = Some(u64::from(n)); + let charged = costed(rows, Some(gauge)); + assert_eq!(charged, 1_248, "{n} gauge rows"); + let held = library_ddsketch_mean_bytes(alpha, n, false, narrow_gauge); + assert!( + (1.0..=1.1).contains(&(charged as f64 / held as f64)), + "{n} gauge rows: {charged} charged, {held} held" + ); + } + let wide = ValueMagnitudeRange { + min_abs: 2.687e-8, + max_abs: 1_104.2, + }; + for n in [10, 600, 36_000] { + assert_eq!( + costed(Some(n), Some(wide)), + costed(Some(n), None), + "{n} rows" + ); + } let latency = ValueMagnitudeRange { min_abs: 0.001, max_abs: 10_000.0, }; - assert_eq!(costed(Some(latency)), bytes_for_bins(807)); - assert_eq!(costed(None), bytes_for_bins(1_037)); - // Values a narrower range holds fit the costed bytes. + assert!(costed(None, Some(latency)) < costed(None, None)); + // Values the declared range holds fit the bytes for unknown rows. let values: Vec = (0..600) .map(|i| 10f64.powf(-3.0 + 7.0 * f64::from(i) / 599.0)) .collect(); - assert!(library_ddsketch_bytes(alpha, &values) <= costed(Some(latency))); + assert!(library_ddsketch_bytes(alpha, &values) <= costed(None, Some(latency))); } /// A KLL state is charged what `asap_sketchlib` allocates for the diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index ac1cf5f39..504328cad 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -284,12 +284,11 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { /// window's range, as a `TimeRange` over the same rows does, besides /// updating the row's state. Example: a sum in 60 s windows over 36,000 /// raw rows, retaining five windows of one group, costs 36,000 × (40 -/// update + 12 range) operations plus five emitted states × 2,500. +/// update + 12 range) operations plus five emitted exact states × 1,500. #[test] fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { use crate::node::operations_per_unit::{ - CHAIN_EMIT_OPERATIONS_PER_STATE, EXACT_UPDATE_OPERATIONS_PER_ROW, - TIME_RANGE_OPERATIONS_PER_ROW, + EXACT_UPDATE_OPERATIONS_PER_ROW, TIME_RANGE_OPERATIONS_PER_ROW, }; let settings = CostInputs::default(); let data = DataWorkload::default(); @@ -330,7 +329,7 @@ fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { assert_eq!( usage.cpu_ops_per_second, 36_000.0 * (EXACT_UPDATE_OPERATIONS_PER_ROW + TIME_RANGE_OPERATIONS_PER_ROW) - + 5.0 * CHAIN_EMIT_OPERATIONS_PER_STATE + + 5.0 * 1_500.0 ); } @@ -916,6 +915,13 @@ fn state_schema(state_type: FieldDataType, labels: &[&str]) -> Schema { /// (`quantile_over_time(0.99, latency[1h])` at a 1% rank target): CPU /// operations per second of the merge at KLL `k`. fn kll_merge_cpu_ops(k: u32) -> f64 { + kll_merge_cpu_ops_over(k, 10_000, 3_600) +} + +/// [`kll_merge_cpu_ops`] over windows of `window_ms` and a `TimeRange` of +/// `range_s` seconds: at one execution every 10 s, the CPU operations per +/// second of a merge of `range_s` / `window_ms` states per series. +fn kll_merge_cpu_ops_over(k: u32, window_ms: u64, range_s: u64) -> f64 { use asap_types::ir::properties::ExecutionDataState; let settings = CostInputs::default(); let data: DataWorkload = serde_json::from_value(serde_json::json!({ @@ -952,11 +958,11 @@ fn kll_merge_cpu_ops(k: u32) -> f64 { input: SummaryUpdate::column(ColumnRef::SampleValue), reduction: Reduction::PerEntity, filter: None, - tumbling_window_width_ms: 10_000, + tumbling_window_width_ms: window_ms, })); chain.output_state = ExecutionDataState::INGESTION_ROWS; chain.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); - chain.retention_ms = Some(3_600_000); + chain.retention_ms = Some(range_s * 1_000); chain.parameters = Some(SketchParams::Kll { k }); chain.output_schema = state_schema( FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), @@ -966,13 +972,13 @@ fn kll_merge_cpu_ops(k: u32) -> f64 { &chain, &[scan_output], (1, 1.0), - Some(10_000), + Some(window_ms), &data, &settings, ) .unwrap(); let mut windows = node(Payload::NonASAP(NonASAPOp::TimeRange { - range: Duration::from_secs(3_600), + range: Duration::from_secs(range_s), kind: TimeRangeKind::Range, child: 0, })); @@ -986,7 +992,10 @@ fn kll_merge_cpu_ops(k: u32) -> f64 { &settings, ) .unwrap(); - assert_eq!(windows_usage.output.rows, 360_000); + assert_eq!( + windows_usage.output.rows, + 1_000 * range_s * 1_000 / window_ms + ); let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { children: vec![0], group_by: None, @@ -1145,3 +1154,121 @@ fn a_per_series_aggregate_charges_the_labels_it_carries() { "the carried job costs {carried_ns} ns per row, measured 37" ); } + +/// A merge of few KLL states per group pays more per group than its +/// states' merges: `quantile_over_time(0.99, latency[5m])` at a 1% rank +/// target over stored 60 s windows of 1,000 series at 1 Hz merges 5 +/// states of 60 rows per series, which the executor measures at 33 µs per +/// series (32 at 10,000 series), where the states' merges alone are +/// charged 14. +#[test] +fn a_kll_merge_of_few_states_charges_each_group_its_measured_time() { + // One merge every 10 s over 1,000 series, at 1e-6 ms per operation. + let us_per_series = kll_merge_cpu_ops_over(547, 60_000, 300) * 10.0 / 1_000.0 * 1e-3; + assert!( + (0.5..=2.0).contains(&(us_per_series / 33.0)), + "charged {us_per_series:.1} µs per series, measured 33" + ); +} + +/// Merging exact Max (or Min) states measures 113–153 ns per state over +/// 100 to 10,000 groups (a fit of 125 per state and 0.86 µs per group); +/// Sum and Count states measure about 230. +#[test] +fn a_max_merge_charges_its_measured_time_per_state() { + use crate::node::operations_per_unit::merge_operations_per_state; + for kind in [ExactKind::Max, ExactKind::Min] { + let max = merge_operations_per_state(StateFamily::Exact(&kind), 56, 56); + assert!((100.0..=150.0).contains(&max), "{kind:?}: {max} per state"); + } + let sum = merge_operations_per_state(StateFamily::Exact(&ExactKind::Sum), 56, 56); + assert!((200.0..=260.0).contains(&sum), "Sum: {sum} per state"); +} + +/// A merge over several inputs sizes each group's merged state for the +/// rows every input's states fold, not the first input's alone: over +/// 1,000 series, one input gives each series 360 KLL windows of 10 rows +/// and the other 60 windows of 100 rows, so its merged state folds +/// 3,600 + 6,000 = 9,600 rows (sized from the first input alone, 420 +/// states of 10 rows: 4,200), at k = 4,000 more bytes than 4,200 rows +/// take. +#[test] +fn a_merge_over_several_inputs_sizes_its_state_for_all_of_them() { + use crate::node::state_bytes::{sketch_state_bytes, StateInputRows}; + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let k = 4_000; + let windows = PhysicalASAPDAGNode { + output_schema: state_schema( + FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), + &[asap_types::ir::schema::PROMQL_SERIES_IDENTITY], + ), + ..node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(3_600), + kind: TimeRangeKind::Range, + child: 0, + })) + }; + let rows = StateInputRows { + rows_per_state: Some(10), + non_negative: true, + value_magnitude_range: None, + }; + let input = ProducerOutput { + payload: &windows.payload, + output_schema: &windows.output_schema, + output: EdgeStatistics { + rows: 360_000, + bytes: 360_000 * 944, + }, + sources: BTreeSet::new(), + family: StateFamily::Sketch(&SketchParams::Kll { k }), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 360, + chain_tumbling_window_width_ms: Some(10_000), + state_input_rows: Some(rows), + }; + let wider = ProducerOutput { + output: EdgeStatistics { + rows: 60_000, + bytes: 60_000 * 1_328, + }, + tumbling_windows: 60, + chain_tumbling_window_width_ms: Some(60_000), + state_input_rows: Some(StateInputRows { + rows_per_state: Some(100), + ..rows + }), + ..input.clone() + }; + let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0, 1], + group_by: None, + })); + merge.parameters = Some(SketchParams::Kll { k }); + merge.output_schema = windows.output_schema.clone(); + let usage = + node_resource_usage_and_cost(&merge, &[input, wider], (1, 0.1), None, &data, &settings) + .unwrap(); + assert_eq!(usage.output.rows, 1_000); + let merged = StateInputRows { + rows_per_state: Some(9_600), + ..rows + }; + assert_eq!( + usage.state_input_rows.and_then(|rows| rows.rows_per_state), + Some(9_600) + ); + let from_first_alone = StateInputRows { + rows_per_state: Some(4_200), + ..rows + }; + assert!( + sketch_state_bytes(&SketchParams::Kll { k }, merged) + > sketch_state_bytes(&SketchParams::Kll { k }, from_first_alone) + ); + assert!( + usage.output.bytes_per_row() > sketch_state_bytes(&SketchParams::Kll { k }, merged), + "merged state sized for 9,600 rows" + ); +} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 08462627b..30cb15612 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -682,3 +682,18 @@ fn compaction_latency_is_zero_for_ingestion_time_nodes_holding_no_states() { ); } } + +/// `max_over_time(latency[5m])` at 1 Hz, raw data kept, every 10 s and +/// every 60 s. Measured cost per second, stored Max windows against the +/// exact plan: 1.44 against 1.62–1.66 every 10 s, 0.240 against about +/// 0.27 every 60 s (regret 1.13–1.24 when exact was selected). The stored +/// plan was over-charged 29%: a Max state's merge measures 125 ns, not a +/// Sum's 230, and a chain emits an exact state in 1.3–2.1 µs, not 2.5. +#[test] +fn max_over_time_5m_at_1_hz_with_raw_data_keeps_stored_max_windows() { + let query = relative("max_over_time(latency[5m])"); + for setting in [ONE_HZ_EVERY_10_S, ONE_HZ_EVERY_60_S] { + let (plan, _) = selected(&workload(query, setting), true); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates, "{setting:?}"); + } +} From 6d5a127c6f67debde5a005db7386d1a2806e387d Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:02:13 +0000 Subject: [PATCH 03/15] fix(cost): cap a DDSketch's span by the declared range; Max merges, exact chain emits and few-state KLL merges at their measured cost; size a merge from all its inputs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DDSketch: the span is the smallest of the rows' exponential spread, the column's declared `column_magnitudes` and the assumed nine decades (`ValueMagnitudeRange::magnitude_ratio`). A gauge declared within [90, 110] is charged 1,248 bytes at any row count (holds 1,168). - An exact Min or Max state merges at 125 operations (measured 113–153 ns), a Sum or Count at 230. - A chain emits an exact state at 1,500 operations (measured 1.3–2.1 µs), a sketch state at 2,500 (`chain_emit_operations_per_state`). - A KLL merge pays 19,000 more per output group: the executor measures a merge of 5 states of 60 rows at 32–33 µs per series where the states' merges are charged 14 (`merge_output_operations` takes the family). - `StateInputRows::merged` sizes a merge's state from every input's rows and states per group, not the first input's. - Tests: #1313's known DDSketch gap in `priced_bytes_agree_with_the_executor` is removed (5,992 priced, 6,288 charged); a declared range never raises the bytes of rows that span less; values spread evenly in log over seven declared decades are under-charged 1.6x (pinned). --- crates/cost/src/node/operations_per_unit.rs | 83 +++++++++++++++++---- crates/cost/src/node/state_bytes/mod.rs | 56 ++++++++++++-- crates/cost/src/node/state_bytes/tests.rs | 18 +++-- crates/cost/src/node/usage/mod.rs | 29 ++++--- crates/cost/src/node/usage/tests.rs | 8 +- crates/types/src/workload/data.rs | 8 ++ 6 files changed, 157 insertions(+), 45 deletions(-) diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index a019daf10..aff101488 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -54,7 +54,7 @@ use asap_types::ir::operator::AggIntent; use asap_types::ir::schema::FieldDataType; -use asap_types::summary_type::SketchParams; +use asap_types::summary_type::{ExactKind, SketchParams}; use crate::StateFamily; @@ -123,10 +123,16 @@ pub(crate) const CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL: f64 = 37.0; /// 72 ns, 18 ns per counter. pub(crate) const SKETCH_UPDATE_OPERATIONS_PER_COUNTER: f64 = 18.0; -/// One exact accumulator state merged into its group's (sum, count, min, -/// max): 128–265 ns per state over 360,000 states. +/// One exact accumulator state merged into its group's (sum, count): +/// 128–265 ns per state over 360,000 states. pub(crate) const EXACT_MERGE_OPERATIONS_PER_STATE: f64 = 230.0; +/// One exact Min or Max state merged into its group's: 113–153 ns per +/// state over 100, 1,000 and 10,000 groups (a fit of 125 per state and +/// 0.86 µs per group, the group's part in `MERGE_OPERATIONS_PER_OUTPUT_GROUP`), +/// where a Sum or Count state measures about 230. +const EXTREMUM_MERGE_OPERATIONS_PER_STATE: f64 = 125.0; + /// One counter-increase accumulator state merged (rate, increase, irate): /// 346 ns per state over 360,000 states. pub(crate) const COUNTER_MERGE_OPERATIONS_PER_STATE: f64 = 350.0; @@ -207,15 +213,30 @@ pub(crate) const SKETCH_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 1_000.0; /// 3 µs per group. pub(crate) const OUTPUT_ROW_OPERATIONS_PER_GROUP: f64 = 2_500.0; -/// Per state a tumbling-window chain completes and emits at the end of each -/// window, beyond its rows' updates: fitting each window's time to its rows -/// and its 1,000 states at 1 Hz (10 s windows of 10,000 rows) and at -/// 10 Hz (60 s windows of 600,000 rows), 1.9 µs per exact Rate state, -/// 2.8 µs per KLL state and 3.4 µs per DDSketch state. Measured apart from +/// Per sketch state a tumbling-window chain completes and emits at the end +/// of each window, beyond its rows' updates: fitting each window's time to +/// its rows and its 1,000 states at 1 Hz (10 s windows of 10,000 rows) and +/// at 10 Hz (60 s windows of 600,000 rows), 2.8 µs per KLL state and +/// 3.4 µs per DDSketch state. Measured apart from /// `OUTPUT_ROW_OPERATIONS_PER_GROUP` and equal to it: both build one row per /// group with its labels and series identity, here around a state, there /// around a value. -pub(crate) const CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 2_500.0; +const SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 2_500.0; + +/// Per fixed-size exact state a tumbling-window chain emits (sum, count, +/// min, max, rate): 1.9 µs per Rate state, and at ingestion time +/// 1.3–2.1 µs per Max state over 100 to 10,000 series. Less than a sketch +/// state's (`SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE`): its state is a +/// fixed record, not a sketch to box. +const EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 1_500.0; + +/// Per output group of a KLL `SummaryMerge`, beyond +/// `MERGE_OPERATIONS_PER_OUTPUT_GROUP` and its states' merges: creating the +/// group's KLL and moving its merged state out. In the executor, a merge +/// of 5 KLL states of 60 rows per series (1,000 and 10,000 series) measures +/// 32–33 µs per series where its states' merges are charged 14; over 60 +/// states per series this adds 4% to the charge, measured 0.8x. +const KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 19_000.0; /// The operations one input row costs folded into a state of `summary_type` /// with `parameters`. @@ -298,6 +319,7 @@ pub(crate) fn merge_operations_per_state( StateFamily::Exact(kind) if kind.tracks_counter_increase() => { COUNTER_MERGE_OPERATIONS_PER_STATE } + StateFamily::Exact(ExactKind::Min | ExactKind::Max) => EXTREMUM_MERGE_OPERATIONS_PER_STATE, StateFamily::Exact(_) | StateFamily::None => EXACT_MERGE_OPERATIONS_PER_STATE, StateFamily::Sketch(SketchParams::DDSketch { .. }) => DDSKETCH_MERGE_OPERATIONS_PER_STATE, StateFamily::Sketch(SketchParams::Kll { .. }) => { @@ -320,11 +342,44 @@ pub(crate) fn merge_operations_per_state( } } -/// The operations a `SummaryMerge` with `output_groups` groups costs -/// beyond its input states' merges ([`merge_operations_per_state`]): each -/// group's state and output row. Example: 1,000 groups cost 950,000. -pub(crate) fn merge_output_operations(output_groups: u64) -> f64 { - output_groups as f64 * MERGE_OPERATIONS_PER_OUTPUT_GROUP +/// The operations a `SummaryMerge` of `family` with `output_groups` groups +/// costs beyond its input states' merges ([`merge_operations_per_state`]): +/// each group's state and output row. Example: 1,000 groups cost 950,000, +/// of KLL states 19,950,000. +pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u64) -> f64 { + let per_group = match family { + StateFamily::Sketch(SketchParams::Kll { .. }) => { + MERGE_OPERATIONS_PER_OUTPUT_GROUP + KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP + } + StateFamily::Sketch( + SketchParams::DDSketch { .. } + | SketchParams::Hydra(_) + | SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. }, + ) + | StateFamily::Exact(_) + | StateFamily::None => MERGE_OPERATIONS_PER_OUTPUT_GROUP, + }; + output_groups as f64 * per_group +} + +/// The operations per state a tumbling-window chain of `summary_type` +/// costs to complete and emit each window's state: 1,500 for an exact +/// accumulator, 2,500 for a sketch or any other summary. +pub(crate) fn chain_emit_operations_per_state(summary_type: &FieldDataType) -> f64 { + match summary_type { + FieldDataType::ExactAggregate(..) => EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE, + FieldDataType::Sketch(..) + | FieldDataType::Plain(..) + | FieldDataType::SummarySample(..) + | FieldDataType::Wavelet(..) + | FieldDataType::StatModel(..) => SKETCH_CHAIN_EMIT_OPERATIONS_PER_STATE, + } } /// The operations reading one group's answer from a state of `family` diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index a3d17efcf..54f15b94f 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -134,6 +134,41 @@ pub struct StateInputRows { } impl StateInputRows { + /// What one state folds when each group merges, from every input, that + /// input's states (`Some((rows each folds, states per group))`): their + /// rows summed, non-negative when all are, within the magnitude range + /// spanning all of theirs. `None` when any input's is unknown. Example: + /// 360 states of 10 rows and 60 of 100 merge into one of 9,600 rows. + pub(crate) fn merged(inputs: impl IntoIterator>) -> Option { + let mut merged: Option = None; + for (rows, states) in inputs.into_iter().collect::>>()? { + let input = Self { + rows_per_state: rows + .rows_per_state + .map(|per_state| per_state.saturating_mul(states)), + ..rows + }; + merged = Some(match merged { + None => input, + Some(sum) => Self { + rows_per_state: sum + .rows_per_state + .zip(input.rows_per_state) + .map(|(a, b)| a.saturating_add(b)), + non_negative: sum.non_negative && input.non_negative, + value_magnitude_range: sum + .value_magnitude_range + .zip(input.value_magnitude_range) + .map(|(a, b)| ValueMagnitudeRange { + min_abs: a.min_abs.min(b.min_abs), + max_abs: a.max_abs.max(b.max_abs), + }), + }, + }); + } + merged + } + /// Unknown rows of unknown sign: the most a state is assumed to hold. pub const UNKNOWN: Self = Self { rows_per_state: None, @@ -319,17 +354,20 @@ fn exponential_values_magnitude_ratio(rows: u64) -> f64 { /// counter per bucket its values span beyond the first. /// /// The span is that of the values the state folds, not of the column: the -/// magnitude ratio its rows are assumed to span -/// ([`exponential_values_magnitude_ratio`]), at most that of -/// `DDSKETCH_ASSUMED_MAGNITUDE_RANGE` (also the span when the rows are -/// unknown); a declared `column_magnitudes` range is not read yet. The library's worst case over every order of +/// smallest of the magnitude ratio its rows are assumed to span +/// ([`exponential_values_magnitude_ratio`]), the ratio of its column's +/// declared `column_magnitudes` (`input_rows.value_magnitude_range`) and +/// that of `DDSKETCH_ASSUMED_MAGNITUDE_RANGE`; without known rows, the +/// smaller of the last two. The library's worst case over every order of /// inserts and merges (`estimated_memory_bytes`) is about twice the span /// over the whole range, which is not what a state holds on average. /// /// Example: at α = 0.01, 10 exponential samples span a ratio of 52, 198 /// buckets: 144 + 1,024 + 8 × 197 = 2,744 bytes, where `asap_sketchlib` /// holds 2,910 on average; 3,000 samples span 538 buckets, 5,464 bytes -/// against 5,769 held. The library's bound for either is 18,752. +/// against 5,769 held. The library's bound for either is 18,752. A gauge +/// declared within [90, 110] spans a ratio of 1.22, 11 buckets: 1,248 +/// bytes for any number of rows. fn ddsketch_state_bytes(alpha: f64, input_rows: StateInputRows) -> u64 { use asap_sketchlib::DdSketch; let struct_bytes = std::mem::size_of::() as u64; @@ -355,8 +393,12 @@ fn ddsketch_state_bytes(alpha: f64, input_rows: StateInputRows) -> u64 { let rows_per_store = input_rows .rows_per_state .map(|rows| rows.div_ceil(stores).max(1)); - let column_ratio = - DDSKETCH_ASSUMED_MAGNITUDE_RANGE.max_abs / DDSKETCH_ASSUMED_MAGNITUDE_RANGE.min_abs; + // The most the column's values span: its declared range, at most the + // assumed one. + let column_ratio = input_rows + .value_magnitude_range + .map_or(f64::INFINITY, |range| range.magnitude_ratio()) + .min(DDSKETCH_ASSUMED_MAGNITUDE_RANGE.magnitude_ratio()); let span = buckets_spanned(rows_per_store.map_or(column_ratio, |rows| { exponential_values_magnitude_ratio(rows).min(column_ratio) })); diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index a075b277d..1045e38e8 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -286,11 +286,19 @@ fn ddsketch_state_bytes_are_capped_by_the_declared_value_magnitude_range() { max_abs: 10_000.0, }; assert!(costed(None, Some(latency)) < costed(None, None)); - // Values the declared range holds fit the bytes for unknown rows. + // Values spread evenly in log over the whole declared range are + // under-charged: the library grows its dense store in blocks, so 600 + // such values hold 12,432 bytes where their 807 buckets are charged + // 7,608 (a known limit of the expected size, within 2x). let values: Vec = (0..600) .map(|i| 10f64.powf(-3.0 + 7.0 * f64::from(i) / 599.0)) .collect(); - assert!(library_ddsketch_bytes(alpha, &values) <= costed(None, Some(latency))); + let held = library_ddsketch_bytes(alpha, &values); + let charged = costed(None, Some(latency)); + assert!( + (0.5..=1.0).contains(&(charged as f64 / held as f64)), + "{charged} charged, {held} held" + ); } /// A KLL state is charged what `asap_sketchlib` allocates for the @@ -673,13 +681,13 @@ fn priced_bytes_agree_with_the_executor() { Rows::Values, None, ), - // Priced for values over nine decades - // (`DDSKETCH_ASSUMED_VALUE_RANGE`); these span three. + // Priced for the spread its 10,000 rows are expected to span + // (`exponential_values_magnitude_ratio`); these span three decades. ( sketch(A::DDSketch), Some(SketchParams::DDSketch { alpha: 0.01 }), Rows::Values, - gap(1313, 2.982), + None, ), ( sketch(A::Hll), diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index f3b89d749..c82980a4c 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -23,9 +23,10 @@ use crate::node::analytical::{ estimate_operator, AnalyticalCostError, PhysicalOperator, ResourceEstimate, }; use crate::node::operations_per_unit::{ - estimate_operations_per_group, hash_aggregate_operations_per_row, merge_operations_per_state, - merge_output_operations, update_operations_per_row, CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, - CHAIN_EMIT_OPERATIONS_PER_STATE, TIME_RANGE_OPERATIONS_PER_ROW, + chain_emit_operations_per_state, estimate_operations_per_group, + hash_aggregate_operations_per_row, merge_operations_per_state, merge_output_operations, + update_operations_per_row, CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, + TIME_RANGE_OPERATIONS_PER_ROW, }; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, @@ -987,7 +988,7 @@ pub fn node_resource_usage_and_cost<'a>( Ok(ResourceEstimate::new( input.rows as f64 * update_ops + range_ops - + emitted_states * CHAIN_EMIT_OPERATIONS_PER_STATE, + + emitted_states * chain_emit_operations_per_state(family), out.bytes, 0, )), @@ -1023,23 +1024,19 @@ pub fn node_resource_usage_and_cost<'a>( // The merged state: each group's input states folded into one, // sized for the rows they fold together when known (a KLL // grows towards its `k`), else as one input state. - let states_per_group = states.div_ceil(group_count.max(1)); let input_state_bytes = |producer: &ProducerOutput<'_>| { state_bytes_per_row(producer.output, producer.output_schema) }; - let merged_input_rows = producers - .first() - .and_then(|producer| producer.state_input_rows) - .map(|rows| StateInputRows { - rows_per_state: rows - .rows_per_state - .map(|per_state| per_state.saturating_mul(states_per_group)), - ..rows - }); + // Each input's states per group, every one folding its rows. + let merged_input_rows = StateInputRows::merged(producers.iter().map(|producer| { + producer + .state_input_rows + .map(|rows| (rows, producer.output.rows.div_ceil(group_count.max(1)))) + })); built_state_input_rows = merged_input_rows; let merged_state_bytes = match (family, merged_input_rows) { (StateFamily::Sketch(params), Some(rows)) => sketch_state_bytes(params, rows), - _ => producers.first().map_or(0, input_state_bytes), + _ => producers.iter().map(input_state_bytes).max().unwrap_or(0), }; // Each input state's merge, by the states' family and bytes, // and each output group's state and row. @@ -1054,7 +1051,7 @@ pub fn node_resource_usage_and_cost<'a>( ) }) .sum::() - + merge_output_operations(group_count); + + merge_output_operations(family, group_count); let out = EdgeStatistics { rows: group_count, bytes: (merged_state_bytes + state_row_bytes_beside_state(&node.output_schema)) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 504328cad..093111a08 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -228,7 +228,7 @@ fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { assert_eq!( merge_usage.cpu_ops_per_second, 5_000.0 * crate::node::operations_per_unit::DDSKETCH_MERGE_OPERATIONS_PER_STATE - + crate::node::operations_per_unit::merge_output_operations(1_000) + + crate::node::operations_per_unit::merge_output_operations(merge_usage.family, 1_000) ); } @@ -276,7 +276,7 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { assert_eq!( merge_usage.cpu_ops_per_second, 5_000.0 * crate::node::operations_per_unit::COUNTER_MERGE_OPERATIONS_PER_STATE - + crate::node::operations_per_unit::merge_output_operations(1_000) + + crate::node::operations_per_unit::merge_output_operations(merge_usage.family, 1_000) ); } @@ -773,7 +773,9 @@ fn a_ddsketch_over_a_scan_is_costed_at_its_declared_value_magnitude_range() { state_bytes(&data(Some(latency)), BTreeSet::from(["ts"])), ddsketch_costed_bytes(false, None) ); - assert!(ddsketch_costed_bytes(false, Some(latency)) < ddsketch_costed_bytes(false, None)); + // 600 rows span less than the declared seven decades, so the declared + // range does not lower their bytes; it never raises them. + assert!(ddsketch_costed_bytes(false, Some(latency)) <= ddsketch_costed_bytes(false, None)); } /// A DDSketch over a SQL table's `latency` column that the table declares diff --git a/crates/types/src/workload/data.rs b/crates/types/src/workload/data.rs index 0274f3cab..6f37660b8 100644 --- a/crates/types/src/workload/data.rs +++ b/crates/types/src/workload/data.rs @@ -226,6 +226,14 @@ pub struct ValueMagnitudeRange { pub max_abs: f64, } +impl ValueMagnitudeRange { + /// The ratio of the largest to the smallest magnitude, at least 1: 1e7 + /// for `{"min_abs": 0.001, "max_abs": 10000.0}`. + pub fn magnitude_ratio(&self) -> f64 { + (self.max_abs / self.min_abs).max(1.0) + } +} + /// The distinct combinations of values a set of labels (or columns) of one /// source takes together, an upper bound. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] From 28345ce937f4319b76128e450eb0b8133e3ba4f8 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:33:36 +0000 Subject: [PATCH 04/15] test(cost): an ingestion-time chain assigns each row its window; a KLL estimate after updates at its measured time; the rate and increase cells at 10 Hz build at query time; log-uniform DDSketch values over a declared range (failing) - An ingestion-time chain costs a time range's work per row besides its update: a Rate chain measures 87 ns per row at 10 Hz where the same Rate built at query time measures 66-75 and its time range 11. - A KLL quantile read from a state built by updates measures 13.4 us per group (300 rows, k = 547), after a merge 5.9. - `sum by (job) (rate|increase(requests_total[5m]))` at 10 Hz every 300 s with raw data builds the accumulator at query time (measured 0.86 against 0.95 per second for the stored windows). - The DDSketch under-charge is pinned on the case the planner sees: rows known, random order, log-uniform over seven declared decades, charged 0.33-0.70 of what the library holds. --- crates/cost/src/node/state_bytes/tests.rs | 67 ++++++++-- crates/cost/src/node/usage/tests.rs | 119 ++++++++++++++++++ .../tests/default_cost_plan_ranking.rs | 23 ++++ 3 files changed, 196 insertions(+), 13 deletions(-) diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index 1045e38e8..90922a92e 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -137,6 +137,14 @@ fn narrow_gauge(series: u64, n: u32) -> Vec { .collect() } +/// `n` values of series `series` spread evenly in log over seven decades, +/// [0.001, 10000], in random order, from a fixed hash of `(series, i)`. +fn log_uniform_over_seven_decades(series: u64, n: u32) -> Vec { + (0..u64::from(n)) + .map(|i| 0.001 * 10f64.powf(7.0 * uniform((series << 32) ^ i))) + .collect() +} + /// The mean bytes `asap_sketchlib`'s DDSketch holds over 20 series of `n` /// values each from `values`, of both signs when `signed`. fn library_ddsketch_mean_bytes( @@ -286,19 +294,52 @@ fn ddsketch_state_bytes_are_capped_by_the_declared_value_magnitude_range() { max_abs: 10_000.0, }; assert!(costed(None, Some(latency)) < costed(None, None)); - // Values spread evenly in log over the whole declared range are - // under-charged: the library grows its dense store in blocks, so 600 - // such values hold 12,432 bytes where their 807 buckets are charged - // 7,608 (a known limit of the expected size, within 2x). - let values: Vec = (0..600) - .map(|i| 10f64.powf(-3.0 + 7.0 * f64::from(i) / 599.0)) - .collect(); - let held = library_ddsketch_bytes(alpha, &values); - let charged = costed(None, Some(latency)); - assert!( - (0.5..=1.0).contains(&(charged as f64 / held as f64)), - "{charged} charged, {held} held" - ); +} + +/// The planner knows a state's rows, and its expected span is the +/// exponential spread of those rows, which a declared range only caps. +/// Values spread evenly in log over the whole of a declared seven decades +/// ([0.001, 10000]), in random order, span more than that and are +/// under-charged, most at few rows per state: at α = 0.01 the library +/// holds 8.2–9.5 KB from 10 rows on (the mean of 20 series), charged +/// 2,744 bytes at 10 rows per state (0.33 of it), 3,648 at 60 (0.38), +/// 4,728 at 600 (0.50), 5,536 at 3,600 (0.59) and 6,552 at 36,000 (0.70). +/// A known limit: the declared range cannot raise the span, since it +/// bounds the column, not one state's rows. Charging such a state the +/// expected span of `n` log-uniform values over the declared range +/// (`(n − 1) / (n + 1)` of its decades) would charge these 0.78–0.81 of +/// what they hold, but the latency generator's states, declared over its +/// 10.6 decades, 1.3x (36,000 rows) to 3.6x (10 rows) of theirs, where +/// they are charged 0.88–1.07 now. +#[test] +fn ddsketch_state_bytes_under_charge_log_uniform_values_over_a_declared_range() { + let alpha = 0.01; + let declared = ValueMagnitudeRange { + min_abs: 0.001, + max_abs: 10_000.0, + }; + for (n, band) in [ + (10_u32, 0.30..=0.37), + (60, 0.35..=0.42), + (600, 0.46..=0.55), + (3_600, 0.54..=0.64), + (36_000, 0.64..=0.75), + ] { + let charged = sketch_state_bytes( + &SketchParams::DDSketch { alpha }, + StateInputRows { + rows_per_state: Some(u64::from(n)), + non_negative: true, + value_magnitude_range: Some(declared), + }, + ); + let held = library_ddsketch_mean_bytes(alpha, n, false, log_uniform_over_seven_decades); + let ratio = charged as f64 / held as f64; + assert!( + band.contains(&ratio), + "{n} rows: {charged} charged, {held} held ({ratio:.2})" + ); + } } /// A KLL state is charged what `asap_sketchlib` allocates for the diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 093111a08..e035c2aba 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -280,6 +280,63 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { ); } +/// An ingestion-time chain assigns each arriving row its window, the work +/// a `TimeRange` does per row, besides updating the row's state: built per +/// series carrying `job` over 1,000 series, a Rate chain measures 87 ns per +/// row at 10 Hz and 106–111 at 1 Hz, where the same Rate built at query +/// time measures 66–75 and 78–87, and its time range 11. Example: a sum in +/// 300 s windows of one group, over one second of 10,000 arriving rows, +/// costs 10,000 × (update + range) operations plus a 300th of an emitted +/// exact state. +#[test] +fn an_ingestion_time_chain_assigns_each_row_its_window_as_a_time_range_does() { + use crate::node::operations_per_unit::{ + EXACT_UPDATE_OPERATIONS_PER_ROW, TIME_RANGE_OPERATIONS_PER_ROW, + }; + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let scan = node(Payload::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { + metric: "latency".to_string(), + }, + predicates: vec![], + schema: Schema::default(), + })); + let rows = ProducerOutput { + payload: &scan.payload, + output_schema: &scan.output_schema, + output: EdgeStatistics { + rows: 10_000, + bytes: 10_000 * 16, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + state_input_rows: None, + }; + let chain = PhysicalASAPDAGNode { + retention_ms: Some(300_000), + output_state: ExecutionDataState::INGESTION_SUMMARY, + ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![]), + filter: None, + tumbling_window_width_ms: 300_000, + })) + }; + let usage = + node_resource_usage_and_cost(&chain, &[rows], (1, 1.0), None, &data, &settings).unwrap(); + assert_eq!( + usage.cpu_ops_per_second, + 10_000.0 * (EXACT_UPDATE_OPERATIONS_PER_ROW + TIME_RANGE_OPERATIONS_PER_ROW) + + 1_500.0 / 300.0 + ); +} + /// A query-time chain reads its scan's lookback and keeps each row in its /// window's range, as a `TimeRange` over the same rows does, besides /// updating the row's state. Example: a sum in 60 s windows over 36,000 @@ -1274,3 +1331,65 @@ fn a_merge_over_several_inputs_sizes_its_state_for_all_of_them() { "merged state sized for 9,600 rows" ); } + +/// The KLL estimate of `quantile_over_time(0.99, latency[5m])` at a 1% +/// rank target (k = 547) over 1,000 series at 1 Hz, per group, after +/// `producer` outputs states of 300 rows each, charged 4,844 bytes. +fn kll_estimate_us_per_group(producer: Payload) -> f64 { + use asap_types::summary_type::{SketchParams, SketchStatistic}; + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let kll = SketchParams::Kll { k: 547 }; + let states = node(producer); + let state_output = ProducerOutput { + payload: &states.payload, + output_schema: &states.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 1_000 * 4_844, + }, + sources: BTreeSet::new(), + family: StateFamily::Sketch(&kll), + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + state_input_rows: None, + }; + let estimate = node(Payload::ASAP(ASAPOp::SummaryEstimate { + summary_input: 0, + query: SketchStatistic::Quantile { q: 0.99 }, + })); + let usage = + node_resource_usage_and_cost(&estimate, &[state_output], (1, 1.0), None, &data, &settings) + .unwrap(); + usage.work_ms_per_execution * 1e3 / 1_000.0 +} + +/// A KLL quantile sorts the items its state holds. A merge leaves each +/// level sorted, so reading a merged state measures 5.9 µs per group +/// (300 rows per series at k = 547, 1,000 series); a state built by +/// updates keeps its newest items unsorted and measures 13.4 (13.4–13.9 +/// over 100 to 10,000 series), where charged as a merged state it was +/// 6.9. +#[test] +fn a_kll_estimate_over_a_state_built_by_updates_charges_its_measured_time() { + let merged = kll_estimate_us_per_group(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + assert!( + (0.67..=1.5).contains(&(merged / 5.9)), + "after a merge: charged {merged:.1} µs per group, measured 5.9" + ); + let built = kll_estimate_us_per_group(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + })); + assert!( + (0.67..=1.5).contains(&(built / 13.4)), + "after updates: charged {built:.1} µs per group, measured 13.4" + ); +} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 30cb15612..100110ced 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -697,3 +697,26 @@ fn max_over_time_5m_at_1_hz_with_raw_data_keeps_stored_max_windows() { assert_eq!(plan_kind(&plan), PlanKind::StoredStates, "{setting:?}"); } } + +/// `sum by (job) (rate(requests_total[5m]))` and the same `increase` at +/// 10 Hz every 300 s, raw data kept. Measured cost per second (cost model +/// 1's default prices on the measured usage), the Rate or Increase built at +/// query time from raw rows against stored 300 s windows: 0.86 against +/// 0.95 for both. Per row, the query-time plan's time range and update +/// measure 79 ns, the stored plan's ingestion-time chain 87: the chain +/// assigns each arriving row its window, which was not charged, so the +/// stored plan was predicted cheaper (regret 1.08–1.10 in every run). +#[test] +fn sum_rate_and_increase_over_5m_at_10_hz_every_300_s_with_raw_data_build_at_query_time() { + for query in [ + relative("sum by (job) (rate(requests_total[5m]))"), + relative("sum by (job) (increase(requests_total[5m]))"), + ] { + let (plan, _) = selected(&workload(query, TEN_HZ_EVERY_300_S), true); + assert_eq!( + plan_kind(&plan), + PlanKind::QueryTimeExactAccumulator, + "{query:?}" + ); + } +} From fede55396ff7f401d90a2b21dfdb1993048f85ee Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:47:58 +0000 Subject: [PATCH 05/15] fix(cost): an ingestion-time chain pays a time range per row; a KLL read after updates at its measured cost; time range refit to 11 - A tumbling-window chain keeps each row it reads in its window's range at either timing: at ingestion time it assigns each arriving row its window, charged `TIME_RANGE_OPERATIONS_PER_ROW` as at query time. A Rate chain measures 87 ns per row at 10 Hz where the query-time Rate and its time range measure 66-76 and 11. - `TIME_RANGE_OPERATIONS_PER_ROW` is refit to 11 over 54 time ranges at 1 Hz and 10 Hz (100 to 10,000 series). The counter update (45) and the carried label (37) keep their values: refit on the same points they give 83 per row together (82 charged). Their per-row time follows the sample rate, not the rows per series, so no per-group term is added; the docs say why. - A KLL estimate reads a state built by updates (no merge since) at 1.8 per byte (`KLL_ESTIMATE_OPERATIONS_PER_BYTE_OF_UPDATED_STATE`), a merged one at 0.9: fitted on 11 executor nodes at 100 to 10,000 series, 300 to 36,000 rows per state. - The merge's fallback to the largest input state and the compaction's missing per-group term are documented where they apply. --- crates/cost/src/node/operations_per_unit.rs | 90 +++++++++++++++------ crates/cost/src/node/usage/mod.rs | 39 ++++++--- crates/cost/src/node/usage/tests.rs | 2 +- crates/cost/src/plan/cost.rs | 3 + 4 files changed, 96 insertions(+), 38 deletions(-) diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index aff101488..c82e11926 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -36,12 +36,12 @@ //! only favours the exact plan further. //! //! Example: `quantile_over_time(0.99, latency[1h])` at 1 Hz reads 3.6 M -//! rows. The exact plan keeps them (`TIME_RANGE_OPERATIONS_PER_ROW`, 12), +//! rows. The exact plan keeps them (`TIME_RANGE_OPERATIONS_PER_ROW`, 11), //! groups them per series (`HASH_AGGREGATE_OPERATIONS_PER_KEY` + //! `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, 27) and sorts each series' 3,600 -//! values (11.8 levels × `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 104 ns per -//! row. A DDSketch built from the same rows takes 12 + -//! `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) = 222 ns per row, about twice +//! values (11.8 levels × `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 103 ns per +//! row. A DDSketch built from the same rows takes 11 + +//! `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) = 221 ns per row, about twice //! the exact plan, as measured (111 and 229). // Each rule lists every variant, as in `usage`: a wildcard arm would give a @@ -59,11 +59,15 @@ use asap_types::summary_type::{ExactKind, SketchParams}; use crate::StateFamily; /// A row kept by a time range over raw rows, or a state read by a time -/// range over a chain's tumbling windows: 11–12 ns per row over 3.6 M and -/// 36 M rows, 8–18 ns per state over 360,000 states. A query-time tumbling -/// window chain pays it too, per raw row it reads: it keeps each row of -/// its scan's lookback in its window's range, as a time range does. -pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: f64 = 12.0; +/// range over a chain's tumbling windows: 9–16 ns per row (median 11) over +/// 54 time ranges of 30,000 to 36 M rows at 1 Hz and 10 Hz over 100, 1,000 +/// and 10,000 series, 8–18 ns per state over 360,000 states. A tumbling +/// window chain pays it too, per raw row it reads: at query time it keeps +/// each row of its scan's lookback in its window's range, and at ingestion +/// time it assigns each arriving row its window, as a time range does. A +/// Rate chain at ingestion time measures 87 ns per row at 10 Hz, the same +/// Rate built at query time 66–76 and its time range 11. +pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: f64 = 11.0; /// Per input row, for each grouping key of a hash aggregate: hashing the /// key and finding its group. With `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, @@ -101,9 +105,19 @@ pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 40.0; /// One row folded into an exact accumulator of a counter's increase (rate, /// increase, irate), which also tracks resets and the first and last /// samples, beyond any label it carries: built per series carrying a -/// `job` label it takes 77 ns per row at query time and 87 at ingestion -/// time, of which the label is `CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL` -/// (37). +/// `job` label at query time it takes 66–109 ns per row (median 83) over +/// 24 nodes at 1 Hz and 10 Hz over 100, 1,000 and 10,000 series, of which +/// the label is `CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL` (37). The time +/// per row follows the sample rate, not the rows per series: 66–76 at +/// 10 Hz (3,000 and 36,000 rows per series) and 78–109 at 1 Hz (300 and +/// 3,600). The executor reads each group's state once per stored batch of +/// rows, and a batch holds a tenth as many rows of a series at 1 Hz. A +/// term per group cannot follow that (3,000 rows per series at 10 Hz cost +/// less per row than 3,600 at 1 Hz), and one per group and batch would +/// need the store's batch length, which the planner does not know; one +/// constant fits both rates, within 0.8–1.3x. At ingestion time a chain +/// also assigns each row its window (`TIME_RANGE_OPERATIONS_PER_ROW`): 87 +/// ns per row at 10 Hz, 106–111 at 1 Hz. pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 45.0; /// Per input row, for each label column a per-series state or group @@ -112,7 +126,9 @@ pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 45.0; /// exact `Aggregate` carrying `job` takes 51–77 ns per row where one /// carrying nothing takes 18–38 at the same settings, and an exact Count /// accumulator carrying it 79 ns where a Max carrying nothing takes 42: -/// 37 ns per row, the median difference. +/// 37 ns per row, the median difference. Refit at 1 Hz and 10 Hz over 100 +/// to 10,000 series, a per-series Count carrying `job` built at query time +/// measures 74 ns per row and a Max 40 (medians of 12 nodes each): 34. pub(crate) const CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL: f64 = 37.0; /// One counter update of any other sketch, per counter its row updates @@ -185,16 +201,29 @@ const MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 950.0; /// about 13 µs, 0.4–0.8 ns per byte. pub(crate) const SKETCH_MERGE_OPERATIONS_PER_BYTE: f64 = 1.0; -/// Per byte of the KLL state one group's answer is read from, before its -/// output row: the quantile sorts the items the state holds. The bytes are -/// those the planner charges for the state (`sketch_state_bytes`); 0.9 per -/// byte fits `asap_sketchlib`'s KLL at k = 200 to 1,404 over the same 24 -/// merged states as `KLL_MERGE_OPERATIONS_PER_STATE` (median of 3 runs over -/// 1,000 groups) within 2x at 21 of them. Example: a merged state of 360 -/// states of 10 rows is charged 15 µs at k = 547 (17.0 KB) and 30 µs at -/// k = 1,404 (33.5 KB), measured 23 and 42. +/// Per byte of the merged KLL state one group's answer is read from, +/// before its output row: the quantile sorts the items the state holds, +/// whose levels a merge leaves sorted. The bytes are those the planner +/// charges for the state (`sketch_state_bytes`); 0.9 per byte fits +/// `asap_sketchlib`'s KLL at k = 200 to 1,404 over the same 24 merged +/// states as `KLL_MERGE_OPERATIONS_PER_STATE` (median of 3 runs over 1,000 +/// groups) within 2x at 21 of them. Example: a merged state of 360 states +/// of 10 rows is charged 15 µs at k = 547 (17.0 KB) and 30 µs at k = 1,404 +/// (33.5 KB), measured 23 and 42. pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_BYTE: f64 = 0.9; +/// Per byte of a KLL state built by updates (by a `SummaryAgg`, or a +/// chain's window read without a merge) that one group's answer is read +/// from, before its output row: its newest items are unsorted, so the +/// quantile sorts them from scratch. Fitted on the executor at k = 547 +/// over 100, 1,000 and 10,000 series of states of 300 to 36,000 rows +/// (charged 4,844 and 17,000 bytes), the geometric mean of 11 nodes: +/// within 0.4–2.1x of each. Example: states of 300 rows measure 13.4–13.9 +/// µs per group, charged 1.8 × 4,844 + 2,500 = 11.2 (6.9 as a merged +/// state); of 3,000 rows 14.5–22.3, of 3,600 25.6–35.3 and of 36,000 +/// 58.6–65.7, each charged 33.1. +pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_BYTE_OF_UPDATED_STATE: f64 = 1.8; + /// One group's answer read from any other sketch state, before its output /// row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`): a DDSketch quantile estimate /// measures 3.7 µs per group with its row (median of 11 nodes of 1,000 @@ -384,12 +413,23 @@ pub(crate) fn chain_emit_operations_per_state(summary_type: &FieldDataType) -> f /// The operations reading one group's answer from a state of `family` /// holding `state_bytes` costs, its output row included (an exact -/// accumulator's: finalizing it, the output row alone). Example: a -/// DDSketch quantile costs 1,000 + 2,500 = 3,500 per group. -pub(crate) fn estimate_operations_per_group(family: StateFamily<'_>, state_bytes: u64) -> f64 { +/// accumulator's: finalizing it, the output row alone). A KLL state last +/// written by a merge (`state_from_merge`) has its levels sorted; one built +/// by updates sorts its newest items when read. Example: a DDSketch +/// quantile costs 1,000 + 2,500 = 3,500 per group; a KLL of 4,844 bytes +/// 6,860 after a merge and 11,219 after updates. +pub(crate) fn estimate_operations_per_group( + family: StateFamily<'_>, + state_bytes: u64, + state_from_merge: bool, +) -> f64 { let read = match family { StateFamily::Sketch(SketchParams::Kll { .. }) => { - KLL_ESTIMATE_OPERATIONS_PER_BYTE * state_bytes as f64 + let per_byte = match state_from_merge { + true => KLL_ESTIMATE_OPERATIONS_PER_BYTE, + false => KLL_ESTIMATE_OPERATIONS_PER_BYTE_OF_UPDATED_STATE, + }; + per_byte * state_bytes as f64 } StateFamily::Sketch( SketchParams::DDSketch { .. } diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index c82980a4c..f61497e74 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -969,14 +969,11 @@ pub fn node_resource_usage_and_cost<'a>( } false => group_count as f64 * retained_output_count as f64, }; - // At query time it reads the scan's lookback and keeps each - // row in its window's range, the work a `TimeRange` over the - // same rows does (12 per row); at ingestion time each row - // arrives once, in its own window. - let range_ops = match ingestion { - true => 0.0, - false => input.rows as f64 * TIME_RANGE_OPERATIONS_PER_ROW, - }; + // Each row it reads is kept in its window's range, the work a + // `TimeRange` over the same rows does (11 per row): at query + // time each row of the scan's lookback, at ingestion time each + // arriving row, assigned its window. + let range_ops = input.rows as f64 * TIME_RANGE_OPERATIONS_PER_ROW; let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -1022,8 +1019,13 @@ pub fn node_resource_usage_and_cost<'a>( group_count = group_count.min(groups(&Reduction::by(keys.clone()))?); } // The merged state: each group's input states folded into one, - // sized for the rows they fold together when known (a KLL - // grows towards its `k`), else as one input state. + // sized for the rows they fold together when every input says + // what its states fold (a KLL grows towards its `k`), else as + // the largest input state. That fallback under-charges a KLL + // merge, whose state grows past any one input's; it applies + // when an input passes on no `state_input_rows`, e.g. states + // read through an operator that does not keep each row's + // window. let input_state_bytes = |producer: &ProducerOutput<'_>| { state_bytes_per_row(producer.output, producer.output_schema) }; @@ -1065,6 +1067,11 @@ pub fn node_resource_usage_and_cost<'a>( ) } Payload::ASAP(ASAPOp::SummaryEstimate { query, .. }) => { + // A merge leaves a KLL's levels sorted; a state built by + // updates (a `SummaryAgg`, a chain's window) is sorted when read. + let state_from_merge = producers.first().is_some_and(|producer| { + matches!(producer.payload, Payload::ASAP(ASAPOp::SummaryMerge { .. })) + }); let per_group = match query { SketchStatistic::TopK { k } => *k as u64, SketchStatistic::FrequencyL2 @@ -1082,7 +1089,12 @@ pub fn node_resource_usage_and_cost<'a>( ( out, Ok(ResourceEstimate::new( - input.rows as f64 * estimate_operations_per_group(family, input_state_bytes()), + input.rows as f64 + * estimate_operations_per_group( + family, + input_state_bytes(), + state_from_merge, + ), 0, 0, )), @@ -1091,8 +1103,11 @@ pub fn node_resource_usage_and_cost<'a>( } Payload::ASAP(ASAPOp::FinalizeExactAccumulator { .. }) => ( edge(input.rows)?, + // An exact accumulator's read is its output row alone, however + // its state was written. Ok(ResourceEstimate::new( - input.rows as f64 * estimate_operations_per_group(family, input_state_bytes()), + input.rows as f64 + * estimate_operations_per_group(family, input_state_bytes(), true), 0, 0, )), diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index e035c2aba..104b27158 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -341,7 +341,7 @@ fn an_ingestion_time_chain_assigns_each_row_its_window_as_a_time_range_does() { /// window's range, as a `TimeRange` over the same rows does, besides /// updating the row's state. Example: a sum in 60 s windows over 36,000 /// raw rows, retaining five windows of one group, costs 36,000 × (40 -/// update + 12 range) operations plus five emitted exact states × 1,500. +/// update + 11 range) operations plus five emitted exact states × 1,500. #[test] fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { use crate::node::operations_per_unit::{ diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index d8e1a8036..b846903b3 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -304,6 +304,9 @@ pub fn plan_cost( // such as the raw rows a `Filter` or `TimeRange` keeps or an exact // `Aggregate`'s values: the executor merges ingest shards only of // summary state columns, and partial copies of rows are concatenated. + // No per-group term (`merge_output_operations`, 19,000 per KLL group): + // the executor merges a shard's states into the existing states in + // place, group by group, and creates no group's state or output row. let merge_ms = |id: PhysicalASAPNodeId| { let Some(&family) = family_of.get(&id) else { return 0.0; From ccff3baccade0aa8609f50de615e3b85a37f3c80 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:47:58 +0000 Subject: [PATCH 06/15] docs(cost): a declared magnitude range caps a DDSketch's span; the chain's window assignment, the KLL read after updates, Min/Max merges and exact chain emits in the design doc --- crates/types/src/workload/data.rs | 14 ++++++++------ .../asap-aware-mapping/analytical-resource-cost.md | 12 ++++++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/types/src/workload/data.rs b/crates/types/src/workload/data.rs index 6f37660b8..e6c884081 100644 --- a/crates/types/src/workload/data.rs +++ b/crates/types/src/workload/data.rs @@ -135,12 +135,14 @@ pub struct SourceFacts { /// 0.001, "max_abs": 10000.0}}` for a latency in seconds. Read per /// series name: a histogram or summary family's range does not apply to /// its `_sum`, `_count` or `_bucket` series. A DDSketch state built from - /// the column unchanged is costed at the bins those magnitudes span (the - /// bucket indices from ⌊log_γ min_abs⌋ to ⌊log_γ max_abs⌋, at least one) - /// instead of an assumed range of nine decades. The deployment must - /// ensure it: a range that is too narrow can make Stage 3 pick a finer - /// and much larger sketch than the default range would. Omitted from - /// JSON when empty. + /// the column unchanged is costed at the buckets its own rows are + /// expected to span, and the declared range only caps that span: a + /// gauge declared within [90, 110] is charged 11 buckets however many + /// rows a state folds, while a range wider than the rows' expected + /// spread changes nothing. It never raises the charge. The deployment + /// must ensure it: a range that is too narrow under-charges the states, + /// so Stage 3 may pick a sketch that holds more than it was charged. + /// Omitted from JSON when empty. #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub column_magnitudes: BTreeMap, /// How often each series of the metric (or group of rows of a SQL diff --git a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md index 67d8f7c4d..2e3fb7dfd 100644 --- a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md +++ b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md @@ -797,10 +797,18 @@ groups of its keys, estimated like any `by (keys)` and at most the input's groups. The merged state is sized for the rows its group's input states fold together: 360 stored KLL windows of 10 rows each merge into a state sized for 3,600 rows. A readout (estimate or finalize) costs each group's output row, -plus reading its state (a KLL's per byte). A state's row on any edge holds +plus reading its state (a KLL's per byte, twice that when no merge has +sorted its levels since its updates). A tumbling-window chain keeps each row +it reads in its window's range at either timing, at a time range's cost per +row: at query time over its scan's lookback, at ingestion time as it assigns +each arriving row its window. A state's row on any edge holds its labels, its window's time and its series identity beside the state; a DDSketch state is sized for the values it is expected to hold, spread like -latencies, not for the most it could hold. A grouping that carries label +latencies, not for the most it could hold; a column's declared +`column_magnitudes` caps that spread and never raises it. Merging exact Min or +Max states costs about half a Sum's or Count's per state, and a +tumbling-window chain emits a fixed-size exact state for less than a sketch +state. A grouping that carries label columns beside its keys (a per-series sum keeping `job` for a later `sum by (job)`) costs per row and carried label. A plan using one of these operations is unavailable until the corresponding formula From f7e5d2d4abc48b8e81d1492dfeb58fdd2cbb340c Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:18:43 +0000 Subject: [PATCH 07/15] test(cost): a grouping costs more per row at a lower sample rate; a KLL read after updates is nearly flat in its rows; the identical sum_over_time pair at 1 Hz keeps stored windows (failing) The per-row cost of a per-series hash aggregate, a query-time Rate build and an ingestion-time Rate chain follows the sample rate: each group's state is visited once per raw batch, 10 rows of a series at 1 Hz and 100 at 10 Hz. A KLL estimate over a state built by updates measures nearly the same at 300 and 3,000 rows per state. These replace the test of a time range charged per arriving row. --- crates/cost/src/node/usage/tests.rs | 299 ++++++++++++++---- .../tests/default_cost_plan_ranking.rs | 25 ++ 2 files changed, 266 insertions(+), 58 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 104b27158..b32fbaf83 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -280,60 +280,220 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { ); } -/// An ingestion-time chain assigns each arriving row its window, the work -/// a `TimeRange` does per row, besides updating the row's state: built per -/// series carrying `job` over 1,000 series, a Rate chain measures 87 ns per -/// row at 10 Hz and 106–111 at 1 Hz, where the same Rate built at query -/// time measures 66–75 and 78–87, and its time range 11. Example: a sum in -/// 300 s windows of one group, over one second of 10,000 arriving rows, -/// costs 10,000 × (update + range) operations plus a 300th of an emitted -/// exact state. -#[test] -fn an_ingestion_time_chain_assigns_each_row_its_window_as_a_time_range_does() { - use crate::node::operations_per_unit::{ - EXACT_UPDATE_OPERATIONS_PER_ROW, TIME_RANGE_OPERATIONS_PER_ROW, - }; - let settings = CostInputs::default(); - let data = DataWorkload::default(); - let scan = node(Payload::NonASAP(NonASAPOp::Scan { - source: Source::TimeSeries { - metric: "latency".to_string(), - }, - predicates: vec![], - schema: Schema::default(), - })); - let rows = ProducerOutput { - payload: &scan.payload, - output_schema: &scan.output_schema, +/// 1,000 series each of `latency` (a non-negative gauge) and +/// `requests_total` (a counter), sampled every `sample_interval_ms`. +fn thousand_series_sampled_every(sample_interval_ms: u64) -> DataWorkload { + serde_json::from_value(serde_json::json!({ + "arrival": {"ingesting": {"rows_per_second": 2_000_000.0 / sample_interval_ms as f64}}, + "sources": { + "latency": {"series_count": 1000, "metric_type": "gauge", + "column_signs": {"value": "non_negative"}, "sample_interval": sample_interval_ms}, + "requests_total": {"series_count": 1000, "metric_type": "counter", + "sample_interval": sample_interval_ms} + } + })) + .unwrap() +} + +/// `rows` raw rows of `metric`, as `producer` outputs them to a grouping. +fn raw_rows<'a>( + producer: &'a PhysicalASAPDAGNode, + metric: &'static str, + rows: u64, +) -> ProducerOutput<'a> { + ProducerOutput { + payload: &producer.payload, + output_schema: &producer.output_schema, output: EdgeStatistics { - rows: 10_000, - bytes: 10_000 * 16, + rows, + bytes: rows * 48, }, - sources: BTreeSet::new(), + sources: BTreeSet::from([metric]), family: StateFamily::None, unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, state_input_rows: None, - }; + } +} + +/// The output schema of a per-series grouping that carries `job` beside +/// the series identity (its key), with a `state` column of `state_type` +/// when it builds states. +fn per_series_carrying_job(state_type: Option) -> Schema { + use asap_types::ir::schema::{DataType, PROMQL_SERIES_IDENTITY}; + let mut fields = vec![Field::new( + "ts", + FieldDataType::Plain(DataType::Timestamp), + false, + )]; + fields.extend(state_type.map(|state_type| Field::new("state", state_type, false))); + fields.extend( + ["job", PROMQL_SERIES_IDENTITY] + .map(|label| Field::new(label, FieldDataType::Plain(DataType::Utf8), false)), + ); + Schema::new(fields) +} + +/// A per-series Rate chain carrying `job` in 300 s windows at ingestion +/// time over 1,000 series of `requests_total`: measured 125.6 ns per +/// arriving row at 1 Hz and 85.7 at 10 Hz (a stored-only plan's ingestion +/// CPU over its rows), 1.47x. Its per-row work is the same at both rates; +/// what differs is how often it visits each series' state and splits its +/// rows by window, once per 10 s raw batch, which holds 10 rows of a series +/// at 1 Hz and 100 at 10 Hz. Charged a time range per row instead, the two +/// rates differed by 5%, and the 1 Hz chain was 1.2x under-charged. +#[test] +fn an_ingestion_time_chain_costs_more_per_row_at_a_lower_sample_rate() { + let settings = CostInputs::default(); + let rate = FieldDataType::ExactAggregate(ExactKind::Rate, ExactParams::Rate); + let scan = node(metric_scan("requests_total")); let chain = PhysicalASAPDAGNode { retention_ms: Some(300_000), output_state: ExecutionDataState::INGESTION_SUMMARY, + output_schema: per_series_carrying_job(Some(rate.clone())), ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { child: 0, - summary_type: FieldDataType::ExactAggregate(ExactKind::Sum, ExactParams::Sum), + summary_type: rate.clone(), input: SummaryUpdate::column(ColumnRef::SampleValue), - reduction: Reduction::by(vec![]), + reduction: Reduction::PerEntity, filter: None, tumbling_window_width_ms: 300_000, })) }; - let usage = - node_resource_usage_and_cost(&chain, &[rows], (1, 1.0), None, &data, &settings).unwrap(); - assert_eq!( - usage.cpu_ops_per_second, - 10_000.0 * (EXACT_UPDATE_OPERATIONS_PER_ROW + TIME_RANGE_OPERATIONS_PER_ROW) - + 1_500.0 / 300.0 + // One second of arriving rows per execution, one execution per second. + let ns_per_row = |sample_interval_ms: u64| { + let rows_per_second = 1_000 * 1_000 / sample_interval_ms; + let usage = node_resource_usage_and_cost( + &chain, + &[raw_rows(&scan, "requests_total", rows_per_second)], + (1, 1.0), + None, + &thousand_series_sampled_every(sample_interval_ms), + &settings, + ) + .unwrap(); + usage.cpu_ops_per_second / rows_per_second as f64 + }; + let (one_hz, ten_hz) = (ns_per_row(1_000), ns_per_row(100)); + for (charged, measured) in [(one_hz, 125.6), (ten_hz, 85.7)] { + assert!( + (0.8..=1.25).contains(&(charged / measured)), + "charged {charged:.1} ns per row, measured {measured}" + ); + } + assert!( + (1.3..=1.7).contains(&(one_hz / ten_hz)), + "1 Hz charged {one_hz:.1} ns per row, 10 Hz {ten_hz:.1}; measured 1.47x" + ); +} + +/// A per-series `max_over_time(latency[5m])` hash aggregate at query time +/// over 1,000 series measures 40.5 ns per row at 1 Hz (300,000 rows) and +/// 20.9 at 10 Hz (3,000,000 rows), its output rows included. The executor +/// visits each series' group once per 10 s raw batch, 10 rows of it at +/// 1 Hz and 100 at 10 Hz. Charged per row alone, the 10 Hz aggregate was +/// 1.33x over-charged, and at 1 Hz an exact scan looked cheaper than the +/// stored plan it measured 1.24x more than. +#[test] +fn a_per_series_aggregate_costs_more_per_row_at_a_lower_sample_rate() { + use asap_types::ir::operator::AggIntent; + let settings = CostInputs::default(); + let range = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let aggregate = PhysicalASAPDAGNode { + output_schema: { + use asap_types::ir::schema::{DataType, PROMQL_SERIES_IDENTITY}; + Schema::new(vec![ + Field::new("value", FieldDataType::Plain(DataType::Float64), false), + Field::new( + PROMQL_SERIES_IDENTITY, + FieldDataType::Plain(DataType::Utf8), + false, + ), + ]) + }, + ..node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::PerEntity, + measures: vec![AggIntent::Max { col: None }], + output_names: vec![String::new()], + filters: vec![], + having: None, + child: 0, + })) + }; + let ns_per_row = |sample_interval_ms: u64| { + let rows = 300 * 1_000 * 1_000 / sample_interval_ms; + let usage = node_resource_usage_and_cost( + &aggregate, + &[raw_rows(&range, "latency", rows)], + (1, 1.0), + None, + &thousand_series_sampled_every(sample_interval_ms), + &settings, + ) + .unwrap(); + usage.work_ms_per_execution * 1e6 / rows as f64 + }; + for (sample_interval_ms, measured) in [(1_000, 40.5), (100, 20.9)] { + let charged = ns_per_row(sample_interval_ms); + assert!( + (0.85..=1.18).contains(&(charged / measured)), + "every {sample_interval_ms} ms: charged {charged:.1} ns per row, measured {measured}" + ); + } +} + +/// A per-series exact Rate carrying `job`, built at query time from 5 min +/// of 1,000 series of `requests_total`, measures 85.9 ns per row at 1 Hz +/// and 68.8 at 10 Hz (1.25x): its update is the same per row, and it visits +/// each series' state once per 10 s raw batch. Charged per row alone, both +/// were charged the same. +#[test] +fn a_query_time_rate_build_costs_more_per_row_at_a_lower_sample_rate() { + let settings = CostInputs::default(); + let rate = FieldDataType::ExactAggregate(ExactKind::Rate, ExactParams::Rate); + let range = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let build = PhysicalASAPDAGNode { + output_schema: per_series_carrying_job(Some(rate.clone())), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: rate.clone(), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + })) + }; + let ns_per_row = |sample_interval_ms: u64| { + let rows = 300 * 1_000 * 1_000 / sample_interval_ms; + let usage = node_resource_usage_and_cost( + &build, + &[raw_rows(&range, "requests_total", rows)], + (1, 1.0), + None, + &thousand_series_sampled_every(sample_interval_ms), + &settings, + ) + .unwrap(); + usage.work_ms_per_execution * 1e6 / rows as f64 + }; + let (one_hz, ten_hz) = (ns_per_row(1_000), ns_per_row(100)); + for (charged, measured) in [(one_hz, 85.9), (ten_hz, 68.8)] { + assert!( + (0.8..=1.25).contains(&(charged / measured)), + "charged {charged:.1} ns per row, measured {measured}" + ); + } + assert!( + (1.1..=1.4).contains(&(one_hz / ten_hz)), + "1 Hz charged {one_hz:.1} ns per row, 10 Hz {ten_hz:.1}; measured 1.25x" ); } @@ -1333,9 +1493,9 @@ fn a_merge_over_several_inputs_sizes_its_state_for_all_of_them() { } /// The KLL estimate of `quantile_over_time(0.99, latency[5m])` at a 1% -/// rank target (k = 547) over 1,000 series at 1 Hz, per group, after -/// `producer` outputs states of 300 rows each, charged 4,844 bytes. -fn kll_estimate_us_per_group(producer: Payload) -> f64 { +/// rank target (k = 547) over 1,000 series, per group, after `producer` +/// outputs states of `rows_per_state` rows each, charged `state_bytes`. +fn kll_estimate_us_per_group(producer: Payload, rows_per_state: u64, state_bytes: u64) -> f64 { use asap_types::summary_type::{SketchParams, SketchStatistic}; let settings = CostInputs::default(); let data = DataWorkload::default(); @@ -1346,14 +1506,18 @@ fn kll_estimate_us_per_group(producer: Payload) -> f64 { output_schema: &states.output_schema, output: EdgeStatistics { rows: 1_000, - bytes: 1_000 * 4_844, + bytes: 1_000 * state_bytes, }, sources: BTreeSet::new(), family: StateFamily::Sketch(&kll), unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, - state_input_rows: None, + state_input_rows: Some(StateInputRows { + rows_per_state: Some(rows_per_state), + non_negative: true, + value_magnitude_range: None, + }), }; let estimate = node(Payload::ASAP(ASAPOp::SummaryEstimate { summary_input: 0, @@ -1368,28 +1532,47 @@ fn kll_estimate_us_per_group(producer: Payload) -> f64 { /// A KLL quantile sorts the items its state holds. A merge leaves each /// level sorted, so reading a merged state measures 5.9 µs per group /// (300 rows per series at k = 547, 1,000 series); a state built by -/// updates keeps its newest items unsorted and measures 13.4 (13.4–13.9 -/// over 100 to 10,000 series), where charged as a merged state it was -/// 6.9. +/// updates keeps its newest items unsorted, at most `k` of them, and +/// measures nearly the same at 300 rows per state (13.1–14.3 µs over 100 +/// to 10,000 series) as at 3,000 (14.7–27.3, median 18.9). Charged per byte +/// of the state (4,844 bytes at 300 rows, 17,000 at 3,000), the 3,000-row +/// state was charged 33.1, 3x the 300-row one. #[test] fn a_kll_estimate_over_a_state_built_by_updates_charges_its_measured_time() { - let merged = kll_estimate_us_per_group(Payload::ASAP(ASAPOp::SummaryMerge { - children: vec![0], - group_by: None, - })); + let merged = kll_estimate_us_per_group( + Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + }), + 300, + 4_844, + ); assert!( (0.67..=1.5).contains(&(merged / 5.9)), "after a merge: charged {merged:.1} µs per group, measured 5.9" ); - let built = kll_estimate_us_per_group(Payload::ASAP(ASAPOp::SummaryAgg { - child: 0, - summary_type: FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), - input: SummaryUpdate::column(ColumnRef::SampleValue), - reduction: Reduction::PerEntity, - filter: None, - })); + let built = |rows_per_state, state_bytes| { + kll_estimate_us_per_group( + Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + }), + rows_per_state, + state_bytes, + ) + }; + let (few, many) = (built(300, 4_844), built(3_000, 17_000)); + for (rows, charged, measured) in [(300, few, 13.4), (3_000, many, 18.9)] { + assert!( + (0.67..=1.5).contains(&(charged / measured)), + "after updates of {rows} rows: charged {charged:.1} µs per group, measured {measured}" + ); + } assert!( - (0.67..=1.5).contains(&(built / 13.4)), - "after updates: charged {built:.1} µs per group, measured 13.4" + many < 2.0 * few, + "3,000 rows charged {many:.1} µs, 300 rows {few:.1}; measured 1.4x" ); } diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index 100110ced..feacc665d 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -720,3 +720,28 @@ fn sum_rate_and_increase_over_5m_at_10_hz_every_300_s_with_raw_data_build_at_que ); } } + +/// Two identical `sum_over_time(m[5m])` every 60 s over 1,000 series of a +/// gauge at 1 Hz, raw data kept, no accuracy target. Measured cost per +/// evaluation (cost model 1's default prices on the measured usage), the +/// exact scan against stored 10 s Sum windows: 0.323 against 0.261. The +/// exact plan's per-series hash aggregate measures 46 ns per row where it +/// was charged 35: at 1 Hz a 10 s raw batch holds 10 rows of a series, and +/// the executor visits each series' group once per batch. Charged per row +/// alone, the two plans were predicted within 0.4% (0.2527 against 0.2538) +/// and the exact scan was selected. +#[test] +fn identical_sum_over_time_pair_at_1_hz_keeps_stored_sum_windows() { + let entry = r#"{"query": "sum_over_time(m[5m])", "demand": {"fixed_interval_at": {"interval": 60000, "evaluation_phase": 0}}, + "requirements": {"max_latency_ms": null}, "predictability": {"predictable": {"known_at": null}}}"#; + let workload: PlanningWorkload = serde_json::from_str(&format!( + r#"{{"query_workload": {{"language": "promql", "repeating_queries": [{entry}, {entry}]}}, + "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 2000.0}}}}, + "sources": {{ + "m": {{"series_count": 1000, "metric_type": "gauge", "sample_interval": 1000}}, + "c": {{"series_count": 1000, "metric_type": "counter", "sample_interval": 1000}}}}}}}}"# + )) + .unwrap(); + let (plan, _) = selected(&workload, true); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates); +} From b9c2e0beb5864da5ae4b9185c8604082bffc52ee Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:19:00 +0000 Subject: [PATCH 08/15] fix(cost): a grouping pays once per group and raw batch, shared by hash aggregates, summary builds and chains; per-row constants refit at 1 Hz and 10 Hz; a KLL read after updates per unsorted item A new deployment input, ResourceSpeed::raw_batch_width_ms (10 s assumed by default), sets how many raw batches a grouping's input spans. Each group's state is visited once per batch (140 operations), and a chain also splits each group's rows of a batch by window (190) and routes each arriving row at ingestion time (8), replacing the time range charged per arriving row. The per-row constants are refit with the new term over 1 Hz and 10 Hz at 100 to 10,000 series; a hash aggregate's refit part is taken from its measure, so a grouping by several keys keeps its charge per key, and an exact distinct count, top-k or frequency statistic hashes each value as a key. A KLL estimate over a state built by updates costs 40 per unsorted item, at most k, instead of 1.8 per byte; a finalize charges its output row directly. A query-time chain sizes one window's states by the sources' rate, so Stage 3's baseline and plan_cost agree on the states a merge over it folds. --- crates/cost/src/model.rs | 34 ++- crates/cost/src/node/operations_per_unit.rs | 216 +++++++++++------- crates/cost/src/node/usage/mod.rs | 128 ++++++++--- crates/cost/src/node/usage/tests.rs | 56 ++++- .../tests/stage3_objective.rs | 11 +- .../tests/cost_inputs_json.rs | 1 + 6 files changed, 331 insertions(+), 115 deletions(-) diff --git a/crates/cost/src/model.rs b/crates/cost/src/model.rs index a68cb02dd..6dd32a18d 100644 --- a/crates/cost/src/model.rs +++ b/crates/cost/src/model.rs @@ -114,7 +114,8 @@ impl ResourceUnitCost { } } -/// How fast one core does work; the latency check reads it. +/// How fast one core does work, and the batches of raw rows its work is +/// split into; the latency check reads it. #[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields)] pub struct ResourceSpeed { @@ -126,16 +127,34 @@ pub struct ResourceSpeed { pub ms_per_cpu_op: f64, /// Milliseconds scanning one byte takes on one core. pub ms_per_scanned_byte: f64, + /// The time one batch of raw rows covers, as the executor receives and + /// stores them: a grouping visits each group's state once per batch + /// its rows arrive in, so a 5 min window of a series sampled at 1 Hz + /// in 10 s batches costs 30 visits per series, whether it holds 300 + /// rows or (at 10 Hz) 3,000. Default 10,000 (10 s), an assumed value: + /// the batch width the reference executor's measurements stored, not + /// one the deployment declared; omitted from JSON, it takes that + /// default. + #[serde(default = "default_raw_batch_width_ms")] + pub raw_batch_width_ms: u64, +} + +/// The assumed [`ResourceSpeed::raw_batch_width_ms`]: 10 s. +const DEFAULT_RAW_BATCH_WIDTH_MS: u64 = 10_000; + +fn default_raw_batch_width_ms() -> u64 { + DEFAULT_RAW_BATCH_WIDTH_MS } impl ResourceSpeed { /// 1 ns per operation and 0.1 ns per scanned byte, the times /// [`ResourceUnitCost::illustrative`] prices: one cost unit is one - /// millisecond of one core. + /// millisecond of one core. Raw rows in 10 s batches (assumed). pub(crate) fn illustrative() -> Self { Self { ms_per_cpu_op: 1e-6, ms_per_scanned_byte: 1e-7, + raw_batch_width_ms: DEFAULT_RAW_BATCH_WIDTH_MS, } } } @@ -239,8 +258,8 @@ impl Default for CostInputs { } impl CostInputs { - /// Bad input: a negative or non-finite speed, a horizon that is not - /// positive, a bad measured table ([`MeasuredOperationCosts`]: a + /// Bad input: a negative or non-finite speed, a raw batch width of 0, a + /// horizon that is not positive, a bad measured table ([`MeasuredOperationCosts`]: a /// negative or non-finite time or size, an entry naming two families, /// a duplicate entry, or `ms_per_cpu_op` 0), or a price the chosen cost model reads that is negative or /// not finite, or every such price 0 (cost model 3 reads none). @@ -259,6 +278,12 @@ impl CostInputs { return Err(AnalyticalCostError::InvalidCalibration(name, value)); } } + if self.resource_speed.raw_batch_width_ms == 0 { + return Err(AnalyticalCostError::InvalidCalibration( + "raw_batch_width_ms", + 0.0, + )); + } let horizon = self.non_repeating_query_horizon_seconds; if !horizon.is_finite() || horizon <= 0.0 { return Err(AnalyticalCostError::InvalidCalibration( @@ -381,6 +406,7 @@ mod tests { let slower = ResourceUnitCost::aws_fargate_us_east_1(ResourceSpeed { ms_per_cpu_op: 2e-6, ms_per_scanned_byte: 2e-7, + ..ResourceSpeed::illustrative() }); close(slower.cost_per_cpu_op, 2.2488e-14); close(slower.cost_per_scanned_byte, 2.2488e-15); diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index c82e11926..8fb32f363 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -1,8 +1,9 @@ //! The CPU operations the formulas charge per unit of work: per row a time //! range keeps, per row a hash aggregate groups, per row folded into a -//! summary state, per row and label column a grouping carries, per state -//! merged and per byte of a KLL state merged or read, per output group of a -//! merge, per group read out and per output row built. +//! summary state, per row and label column a grouping carries, per group +//! and raw batch a grouping visits, per state merged and per byte of a KLL +//! state merged or read, per output group of a merge, per group read out +//! and per output row built. //! //! One CPU operation is one nanosecond of the reference executor's work on //! one core: each count below is the executor's measured time for that @@ -38,11 +39,13 @@ //! Example: `quantile_over_time(0.99, latency[1h])` at 1 Hz reads 3.6 M //! rows. The exact plan keeps them (`TIME_RANGE_OPERATIONS_PER_ROW`, 11), //! groups them per series (`HASH_AGGREGATE_OPERATIONS_PER_KEY` + -//! `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, 27) and sorts each series' 3,600 -//! values (11.8 levels × `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 103 ns per -//! row. A DDSketch built from the same rows takes 11 + -//! `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) = 221 ns per row, about twice -//! the exact plan, as measured (111 and 229). +//! `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, 18, plus +//! `OPERATIONS_PER_GROUP_PER_RAW_BATCH` once per series and 10 s batch, 14 +//! per row) and sorts each series' 3,600 values (11.8 levels × +//! `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 108 ns per row. A DDSketch built +//! from the same rows takes 11 + `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (190) + +//! 14 = 215 ns per row, about twice the exact plan, as measured (111 and +//! 229). // Each rule lists every variant, as in `usage`: a wildcard arm would give a // new operator, summary type or sketch a default cost instead of failing @@ -61,23 +64,62 @@ use crate::StateFamily; /// A row kept by a time range over raw rows, or a state read by a time /// range over a chain's tumbling windows: 9–16 ns per row (median 11) over /// 54 time ranges of 30,000 to 36 M rows at 1 Hz and 10 Hz over 100, 1,000 -/// and 10,000 series, 8–18 ns per state over 360,000 states. A tumbling -/// window chain pays it too, per raw row it reads: at query time it keeps -/// each row of its scan's lookback in its window's range, and at ingestion -/// time it assigns each arriving row its window, as a time range does. A -/// Rate chain at ingestion time measures 87 ns per row at 10 Hz, the same -/// Rate built at query time 66–76 and its time range 11. +/// and 10,000 series, 8–18 ns per state over 360,000 states. A query-time +/// tumbling window chain pays it too, per raw row of its scan's lookback, +/// which it keeps in its window's range. pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: f64 = 11.0; +/// Per group of a grouping (a hash aggregate, a summary build or a +/// tumbling window chain), per batch of raw rows its rows arrive in +/// ([`ResourceSpeed::raw_batch_width_ms`](crate::ResourceSpeed)): finding +/// the group's state again for the next batch. It is why the same grouping +/// costs more per row at a lower sample rate: a 10 s batch holds 10 rows +/// of a series at 1 Hz and 100 at 10 Hz, so this term adds 14 ns per row +/// at 1 Hz and 1.4 at 10 Hz. Fitted together with the per-row constants +/// below, minimizing the absolute log error (robust to the few outlying +/// nodes) over 132 query-time `Aggregate` and `SummaryAgg` nodes +/// (per-series Max, Count, Sum and quantile aggregates; Max, Sum, Count, +/// Rate, Increase, DDSketch and KLL builds), 5 min and 1 h windows at +/// 1 Hz and 10 Hz over 100, 1,000 and 10,000 series, and 12 ingestion-time +/// chains: 144 per group and batch. A per-series hash aggregate then +/// measures 0.78–1.22x its charge at 1 Hz and 0.89–1.27x at 10 Hz +/// (0.88–1.31x and 0.72–0.95x without the term), an exact accumulator +/// build 0.90–1.27x and 0.89–1.14x (0.73–1.34x and 0.73–1.04x). +pub(crate) const OPERATIONS_PER_GROUP_PER_RAW_BATCH: f64 = 140.0; + +/// Per group of a tumbling window chain, per batch of raw rows, beyond +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: splitting the group's rows of the +/// batch by the window each falls in. A Rate chain at ingestion time +/// measures 124–126 ns per row at 1 Hz and 80–86 at 10 Hz over 1,000 and +/// 10,000 series, where the same Rate built at query time measures 86–110 +/// and 69–75: the chain's work per group and batch is about twice a plain +/// build's (fitted 186 with the constants above). With +/// `CHAIN_OPERATIONS_PER_ARRIVING_ROW`, Rate, Sum and KLL chains at +/// ingestion time then measure 0.82–1.04x their charge at 1 Hz and +/// 0.89–1.26x at 10 Hz over 100 to 10,000 series (1.0–1.4x and 0.9–1.3x +/// with neither term). +pub(crate) const CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH: f64 = 190.0; + +/// Per row an ingestion-time tumbling window chain receives: routing the +/// arriving row to its window's state, which a build at query time does +/// not do (fitted 7.9 with the constants above, the chain's per group and +/// batch term included). A query-time chain instead keeps each row of its +/// scan's lookback in its window's range (`TIME_RANGE_OPERATIONS_PER_ROW`). +pub(crate) const CHAIN_OPERATIONS_PER_ARRIVING_ROW: f64 = 8.0; + /// Per input row, for each grouping key of a hash aggregate: hashing the -/// key and finding its group. With `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, -/// a per-series `max_over_time` (one key, one measure) groups a row in -/// 27 ns. +/// key and finding its group. With `AGGREGATE_MEASURE_OPERATIONS_PER_ROW` +/// and `OPERATIONS_PER_GROUP_PER_RAW_BATCH`, a per-series `max_over_time` +/// (one key, one measure) groups a row in 18 + 14 ns at 1 Hz and +/// 18 + 1.4 at 10 Hz (fitted 17.5 for the two per-row constants). The +/// measurements fix only that sum, for one key and one measure; the part +/// the group and batch term took is charged to the measure, so a grouping +/// by several keys keeps its charge per key. pub(crate) const HASH_AGGREGATE_OPERATIONS_PER_KEY: f64 = 15.0; /// Per input row, for each plain measure of a hash aggregate (sum, count, /// min, max, ...): folding the row's value into its group's. -pub(crate) const AGGREGATE_MEASURE_OPERATIONS_PER_ROW: f64 = 12.0; +pub(crate) const AGGREGATE_MEASURE_OPERATIONS_PER_ROW: f64 = 3.0; /// Per input row, per level of the sort that an exact quantile runs over /// its group's values (`log2` of the rows per group levels): a per-series @@ -86,39 +128,38 @@ pub(crate) const AGGREGATE_MEASURE_OPERATIONS_PER_ROW: f64 = 12.0; /// 36,000 (15.1 levels, 4.5–6.4 ns per level). pub(crate) const SORT_OPERATIONS_PER_ROW_PER_LEVEL: f64 = 5.5; -/// One row folded into a DDSketch state: 217 ns per row at 1 Hz and 203 at -/// 10 Hz at query time, 211 at ingestion time. -pub(crate) const DDSKETCH_UPDATE_OPERATIONS_PER_ROW: f64 = 210.0; +/// One row folded into a DDSketch state, beyond +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: 193–278 ns per row at 1 Hz and +/// 172–191 at 10 Hz at query time over 100 to 10,000 series (fitted 189). +pub(crate) const DDSKETCH_UPDATE_OPERATIONS_PER_ROW: f64 = 190.0; -/// One row folded into a KLL state: 106 ns per row at 1 Hz and 84 at -/// 10 Hz at query time, 94 at ingestion time. -pub(crate) const KLL_UPDATE_OPERATIONS_PER_ROW: f64 = 95.0; +/// One row folded into a KLL state, beyond +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: 65–112 ns per row at 1 Hz and +/// 83–92 at 10 Hz at query time over 100 to 10,000 series (fitted 84). +/// A state of fewer rows than its `k` is cheaper (65–94 at 300 rows), as +/// it compacts nothing. +pub(crate) const KLL_UPDATE_OPERATIONS_PER_ROW: f64 = 84.0; /// One row folded into an exact accumulator that keeps one value (sum, /// count, min, max), beyond any label it carries -/// (`CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL`): over 1,000 series, an exact Max -/// built per series takes 35 ns per row at query time (median of 12 -/// runs, 300,000 to 36 M rows) and 44 at ingestion time, an exact Sum -/// grouped by `job` 30 at query time. -pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 40.0; +/// (`CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL`) and +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: an exact Max built per series at +/// query time takes 40–49 ns per row at 1 Hz and 31–35 at 10 Hz over 100 +/// to 10,000 series, an exact Sum grouped by `job` (10 groups) 29–33 at +/// both rates (fitted 30.2). +pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 30.0; /// One row folded into an exact accumulator of a counter's increase (rate, /// increase, irate), which also tracks resets and the first and last -/// samples, beyond any label it carries: built per series carrying a -/// `job` label at query time it takes 66–109 ns per row (median 83) over -/// 24 nodes at 1 Hz and 10 Hz over 100, 1,000 and 10,000 series, of which -/// the label is `CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL` (37). The time -/// per row follows the sample rate, not the rows per series: 66–76 at -/// 10 Hz (3,000 and 36,000 rows per series) and 78–109 at 1 Hz (300 and -/// 3,600). The executor reads each group's state once per stored batch of -/// rows, and a batch holds a tenth as many rows of a series at 1 Hz. A -/// term per group cannot follow that (3,000 rows per series at 10 Hz cost -/// less per row than 3,600 at 1 Hz), and one per group and batch would -/// need the store's batch length, which the planner does not know; one -/// constant fits both rates, within 0.8–1.3x. At ingestion time a chain -/// also assigns each row its window (`TIME_RANGE_OPERATIONS_PER_ROW`): 87 -/// ns per row at 10 Hz, 106–111 at 1 Hz. -pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 45.0; +/// samples, beyond any label it carries and +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: built per series carrying a `job` +/// label at query time it takes 66–110 ns per row over 22 nodes at 1 Hz +/// and 10 Hz over 100, 1,000 and 10,000 series, of which the label is +/// `CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL` (37). The time per row +/// follows the sample rate, not the rows per series: 66–85 at 10 Hz (3,000 +/// and 36,000 rows per series) and 78–110 at 1 Hz (300 and 3,600), as the +/// group and batch term charges it (fitted 35.8 with it). +pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 36.0; /// Per input row, for each label column a per-series state or group /// carries beside its key (the `job` of a per-series sum that a later @@ -212,17 +253,19 @@ pub(crate) const SKETCH_MERGE_OPERATIONS_PER_BYTE: f64 = 1.0; /// (33.5 KB), measured 23 and 42. pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_BYTE: f64 = 0.9; -/// Per byte of a KLL state built by updates (by a `SummaryAgg`, or a -/// chain's window read without a merge) that one group's answer is read -/// from, before its output row: its newest items are unsorted, so the -/// quantile sorts them from scratch. Fitted on the executor at k = 547 -/// over 100, 1,000 and 10,000 series of states of 300 to 36,000 rows -/// (charged 4,844 and 17,000 bytes), the geometric mean of 11 nodes: -/// within 0.4–2.1x of each. Example: states of 300 rows measure 13.4–13.9 -/// µs per group, charged 1.8 × 4,844 + 2,500 = 11.2 (6.9 as a merged -/// state); of 3,000 rows 14.5–22.3, of 3,600 25.6–35.3 and of 36,000 -/// 58.6–65.7, each charged 33.1. -pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_BYTE_OF_UPDATED_STATE: f64 = 1.8; +/// Per unsorted item of a KLL state built by updates (by a `SummaryAgg`, +/// or a chain's window read without a merge) that one group's answer is +/// read from, before its output row: the quantile sorts the newest items, +/// which updates leave unsorted, and there are at most `k` of them (the +/// state's rows while it holds fewer). The time is nearly flat in the +/// state's rows, unlike its bytes. Fitted on the executor at k = 547 over +/// 100, 1,000 and 10,000 series of states of 300 to 36,000 rows, +/// minimizing the squared log error over 13 nodes: within 0.55–1.43x of +/// each but the two of 36,000 rows (1.43x and 2.4x under). Example: states +/// of 300 rows measure 13.1–14.3 µs per group, charged 2,500 + 40 × 300 = +/// 14.5; of 3,000 rows 14.7–27.3 and of 3,600 13.3–32.7, charged 2,500 + +/// 40 × 547 = 24.4 (33.1 when charged per byte). +pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_UNSORTED_ITEM: f64 = 40.0; /// One group's answer read from any other sketch state, before its output /// row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`): a DDSketch quantile estimate @@ -237,9 +280,10 @@ pub(crate) const SKETCH_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 1_000.0; /// finalize of exact accumulators measures 2.5 µs /// per group (median over 135 nodes of 1,000 groups each, 1.8–15 µs). A /// per-series hash aggregate builds the same rows: e.g. a per-series max -/// over 300,000 rows into 1,000 groups measures 11.3 ms, 8.1 ms of it its -/// rows' work (`hash_aggregate_operations_per_row`, 27 per row), about -/// 3 µs per group. +/// over 300,000 rows of 1,000 series at 1 Hz measures 11.3 ms, of which +/// its rows (`hash_aggregate_operations_per_row`, 18 per row) and its +/// 30,000 group visits (`OPERATIONS_PER_GROUP_PER_RAW_BATCH`) are charged +/// 9.6, leaving about 1.7 µs per group. pub(crate) const OUTPUT_ROW_OPERATIONS_PER_GROUP: f64 = 2_500.0; /// Per sketch state a tumbling-window chain completes and emits at the end @@ -411,26 +455,38 @@ pub(crate) fn chain_emit_operations_per_state(summary_type: &FieldDataType) -> f } } +/// How the summary state an estimate reads was last written, which a +/// KLL's read depends on. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum StateLastWrittenBy { + /// A merge, which leaves a KLL's levels sorted. + Merge, + /// Updates of `rows_per_state` rows each (`None` when unknown), whose + /// newest items a KLL sorts when read. + Updates { rows_per_state: Option }, +} + /// The operations reading one group's answer from a state of `family` -/// holding `state_bytes` costs, its output row included (an exact -/// accumulator's: finalizing it, the output row alone). A KLL state last -/// written by a merge (`state_from_merge`) has its levels sorted; one built -/// by updates sorts its newest items when read. Example: a DDSketch -/// quantile costs 1,000 + 2,500 = 3,500 per group; a KLL of 4,844 bytes -/// 6,860 after a merge and 11,219 after updates. +/// holding `state_bytes`, last written by `written_by`, costs, its output +/// row included. A KLL state last written by a merge is read per byte; one +/// built by updates per unsorted item, at most its `k` (every one when its +/// rows are unknown). Example: a DDSketch quantile costs 1,000 + 2,500 = +/// 3,500 per group; a KLL at k = 547 of 4,844 bytes 6,860 after a merge +/// and, built from 300 rows, 2,500 + 40 × 300 = 14,500 after updates. pub(crate) fn estimate_operations_per_group( family: StateFamily<'_>, state_bytes: u64, - state_from_merge: bool, + written_by: StateLastWrittenBy, ) -> f64 { let read = match family { - StateFamily::Sketch(SketchParams::Kll { .. }) => { - let per_byte = match state_from_merge { - true => KLL_ESTIMATE_OPERATIONS_PER_BYTE, - false => KLL_ESTIMATE_OPERATIONS_PER_BYTE_OF_UPDATED_STATE, - }; - per_byte * state_bytes as f64 - } + StateFamily::Sketch(SketchParams::Kll { k }) => match written_by { + StateLastWrittenBy::Merge => KLL_ESTIMATE_OPERATIONS_PER_BYTE * state_bytes as f64, + StateLastWrittenBy::Updates { rows_per_state } => { + let k = u64::from(*k); + let unsorted_items = rows_per_state.map_or(k, |rows| rows.min(k)); + KLL_ESTIMATE_OPERATIONS_PER_UNSORTED_ITEM * unsorted_items as f64 + } + }, StateFamily::Sketch( SketchParams::DDSketch { .. } | SketchParams::Hydra(_) @@ -451,7 +507,7 @@ pub(crate) fn estimate_operations_per_group( /// `grouping_key_count` keys and `measures` over groups of `rows_per_group` /// rows: each key hashed and looked up, each measure folded (at least one /// operation for an aggregate without measures). Example: a per-series -/// `max_over_time` (one key, one measure) costs 15 + 12 = 27. +/// `max_over_time` (one key, one measure) costs 15 + 3 = 18. pub(crate) fn hash_aggregate_operations_per_row( grouping_key_count: u64, measures: &[AggIntent], @@ -469,6 +525,10 @@ pub(crate) fn hash_aggregate_operations_per_row( /// groups of `rows_per_group` rows, an estimate of its work: /// - an exact quantile keeps the row and sorts its group /// (`log2(rows_per_group)` levels); +/// - an exact distinct count, top-k or frequency statistic hashes the row's +/// value into its group's map of values, the work a grouping key does +/// (`HASH_AGGREGATE_OPERATIONS_PER_KEY`), besides folding it (not +/// measured); /// - a counter's rate or increase, or a regression over a series' samples /// (`deriv`, `predict_linear`), does about what the Rate accumulator does /// per row (`COUNTER_UPDATE_OPERATIONS_PER_ROW`): it reads each sample's @@ -483,6 +543,12 @@ fn aggregate_measure_operations_per_row(measure: &AggIntent, rows_per_group: u64 AGGREGATE_MEASURE_OPERATIONS_PER_ROW + (rows_per_group.max(2) as f64).log2() * SORT_OPERATIONS_PER_ROW_PER_LEVEL } + AggIntent::TopK { .. } + | AggIntent::Cardinality { .. } + | AggIntent::FrequencyL2 { .. } + | AggIntent::FrequencyEntropy { .. } => { + AGGREGATE_MEASURE_OPERATIONS_PER_ROW + HASH_AGGREGATE_OPERATIONS_PER_KEY + } AggIntent::Rate | AggIntent::IRate | AggIntent::Increase @@ -496,10 +562,6 @@ fn aggregate_measure_operations_per_row(measure: &AggIntent, rows_per_group: u64 | AggIntent::StdDev { .. } | AggIntent::Variance { .. } | AggIntent::PearsonCorr { .. } - | AggIntent::TopK { .. } - | AggIntent::Cardinality { .. } - | AggIntent::FrequencyL2 { .. } - | AggIntent::FrequencyEntropy { .. } | AggIntent::Changes | AggIntent::Delta | AggIntent::IDelta diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index f61497e74..66c978602 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -25,7 +25,9 @@ use crate::node::analytical::{ use crate::node::operations_per_unit::{ chain_emit_operations_per_state, estimate_operations_per_group, hash_aggregate_operations_per_row, merge_operations_per_state, merge_output_operations, - update_operations_per_row, CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, + update_operations_per_row, StateLastWrittenBy, CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, + CHAIN_OPERATIONS_PER_ARRIVING_ROW, CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH, + OPERATIONS_PER_GROUP_PER_RAW_BATCH, OUTPUT_ROW_OPERATIONS_PER_GROUP, TIME_RANGE_OPERATIONS_PER_ROW, }; use crate::node::operator_statistics::{ @@ -621,6 +623,18 @@ pub fn node_resource_usage_and_cost<'a>( (_, None) => rows_read(), }, }; + // The times a grouping into `group_count` groups visits a group's + // state: once per group per batch of raw rows its input arrives in + // (`group_raw_batch_visits`). + let visits = |group_count: u64| { + group_raw_batch_visits( + input.rows, + group_count, + rows_per_ms, + speed.raw_batch_width_ms, + ingestion, + ) + }; let (out, estimate, detail) = match &node.payload { Payload::NonASAP(operator) => match operator { NonASAPOp::Scan { source, .. } => { @@ -691,6 +705,7 @@ pub fn node_resource_usage_and_cost<'a>( let held = input.rows.div_ceil(group_count.max(1)); let operations_per_row = hash_aggregate_operations_per_row(keys, measures, held) + carried_label_operations_per_row(&node.output_schema, reduction); + let group_visits = visits(group_count); let estimate = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: keys, @@ -714,13 +729,20 @@ pub fn node_resource_usage_and_cost<'a>( })? .max(8), }, - ); + ) + .map(|estimate| { + ResourceEstimate::new( + estimate.cpu_ops() + group_visits * OPERATIONS_PER_GROUP_PER_RAW_BATCH, + estimate.peak_memory_bytes(), + estimate.scan_bytes(), + ) + }); ( out, estimate, format!( "hash aggregate {} rows into {group_count} groups: {operations_per_row:.0} \ - ops per row", + ops per row, {group_visits:.0} group visits", input.rows ), ) @@ -911,15 +933,18 @@ pub fn node_resource_usage_and_cost<'a>( .checked_mul(state_bytes + state_row_bytes_beside_state(&node.output_schema)) .ok_or(AnalyticalCostError::Overflow)?, }; + let group_visits = visits(group_count); ( out, Ok(ResourceEstimate::new( - input.rows as f64 * update_ops, + input.rows as f64 * update_ops + + group_visits * OPERATIONS_PER_GROUP_PER_RAW_BATCH, out.bytes, 0, )), format!( - "build {} into {group_count} states: {} rows x {update_ops} ops", + "build {} into {group_count} states: {} rows x {update_ops} ops, \ + {group_visits:.0} group visits", summary_type_name(family), input.rows ), @@ -935,7 +960,9 @@ pub fn node_resource_usage_and_cost<'a>( // One window's states: each input row updates the window its // time falls in. At ingestion time the rows of one window // arrive at the rate of one second's `input.rows`; at query - // time its input holds the rows of every retained window. + // time one window holds its width of the sources' rows (its + // input holds the rows of every retained window: without a + // declared rate, their share of one). let group_count = groups(reduction)?; let rows_per_tumbling_window = match ingestion { true => u64::try_from( @@ -943,7 +970,12 @@ pub fn node_resource_usage_and_cost<'a>( .div_ceil(u128::from(ONE_SECOND_MS)), ) .map_err(|_| AnalyticalCostError::Overflow)?, - false => input.rows.div_ceil(retained_output_count), + false => { + rows_per_ms.map_or(input.rows.div_ceil(retained_output_count), |rows_per_ms| { + ((rows_per_ms * *tumbling_window_width_ms as f64).round() as u64) + .clamp(1, input.rows.max(1)) + }) + } }; let (non_negative, value_magnitude_range) = folded_values(update); let input_rows = StateInputRows { @@ -969,11 +1001,17 @@ pub fn node_resource_usage_and_cost<'a>( } false => group_count as f64 * retained_output_count as f64, }; - // Each row it reads is kept in its window's range, the work a - // `TimeRange` over the same rows does (11 per row): at query - // time each row of the scan's lookback, at ingestion time each - // arriving row, assigned its window. - let range_ops = input.rows as f64 * TIME_RANGE_OPERATIONS_PER_ROW; + // At query time each row of the scan's lookback is kept in its + // window's range, the work a `TimeRange` over the same rows does + // (11 per row); at ingestion time each arriving row is routed to + // its window's state (8 per row). + let range_ops = input.rows as f64 + * match ingestion { + true => CHAIN_OPERATIONS_PER_ARRIVING_ROW, + false => TIME_RANGE_OPERATIONS_PER_ROW, + }; + // Each group's rows of each raw batch are split by window. + let group_visits = visits(group_count); let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -985,14 +1023,17 @@ pub fn node_resource_usage_and_cost<'a>( Ok(ResourceEstimate::new( input.rows as f64 * update_ops + range_ops + + group_visits + * (OPERATIONS_PER_GROUP_PER_RAW_BATCH + + CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH) + emitted_states * chain_emit_operations_per_state(family), out.bytes, 0, )), format!( "build {} in tumbling windows of {tumbling_window_width_ms} ms for \ - {group_count} groups: {} rows x {update_ops} ops, {emitted_states:.1} \ - states emitted", + {group_count} groups: {} rows x {update_ops} ops, {group_visits:.0} \ + group visits, {emitted_states:.1} states emitted", summary_type_name(family), input.rows ), @@ -1069,9 +1110,18 @@ pub fn node_resource_usage_and_cost<'a>( Payload::ASAP(ASAPOp::SummaryEstimate { query, .. }) => { // A merge leaves a KLL's levels sorted; a state built by // updates (a `SummaryAgg`, a chain's window) is sorted when read. - let state_from_merge = producers.first().is_some_and(|producer| { - matches!(producer.payload, Payload::ASAP(ASAPOp::SummaryMerge { .. })) - }); + let written_by = match producers.first() { + Some(producer) + if matches!(producer.payload, Payload::ASAP(ASAPOp::SummaryMerge { .. })) => + { + StateLastWrittenBy::Merge + } + producer => StateLastWrittenBy::Updates { + rows_per_state: producer + .and_then(|producer| producer.state_input_rows) + .and_then(|rows| rows.rows_per_state), + }, + }; let per_group = match query { SketchStatistic::TopK { k } => *k as u64, SketchStatistic::FrequencyL2 @@ -1090,11 +1140,7 @@ pub fn node_resource_usage_and_cost<'a>( out, Ok(ResourceEstimate::new( input.rows as f64 - * estimate_operations_per_group( - family, - input_state_bytes(), - state_from_merge, - ), + * estimate_operations_per_group(family, input_state_bytes(), written_by), 0, 0, )), @@ -1103,11 +1149,9 @@ pub fn node_resource_usage_and_cost<'a>( } Payload::ASAP(ASAPOp::FinalizeExactAccumulator { .. }) => ( edge(input.rows)?, - // An exact accumulator's read is its output row alone, however - // its state was written. + // An exact accumulator's read is its output row alone. Ok(ResourceEstimate::new( - input.rows as f64 - * estimate_operations_per_group(family, input_state_bytes(), true), + input.rows as f64 * OUTPUT_ROW_OPERATIONS_PER_GROUP, 0, 0, )), @@ -1412,6 +1456,38 @@ fn selected_tumbling_windows( .max(1)) } +/// The times a grouping into `group_count` groups visits a group's state +/// while reading `input_rows` rows: once per group per batch of raw rows +/// (`raw_batch_width_ms` each) its input arrives in, at most once per row. +/// The batches are the input's rows over a batch's rows at the sources' +/// `rows_per_ms`: at query time at least one; at ingestion time a share of +/// one per execution, the rows of one second over a batch's. Unknown rate: +/// one batch at query time, a second's share of one at ingestion time. +/// Example: a 5 min window of 1,000 series at 1 Hz (300,000 rows) in 10 s +/// batches of 10,000 rows spans 30 batches, 30,000 visits; at 10 Hz the +/// same 30,000 visits for 3,000,000 rows. +fn group_raw_batch_visits( + input_rows: u64, + group_count: u64, + rows_per_ms: Option, + raw_batch_width_ms: u64, + ingestion: bool, +) -> f64 { + let batch_width_ms = raw_batch_width_ms.max(1) as f64; + let batches = match rows_per_ms.filter(|rows_per_ms| *rows_per_ms > 0.0) { + Some(rows_per_ms) => input_rows as f64 / (rows_per_ms * batch_width_ms), + None => match ingestion { + true => ONE_SECOND_MS as f64 / batch_width_ms, + false => 1.0, + }, + }; + let batches = match ingestion { + true => batches, + false => batches.max(1.0), + }; + (group_count.max(1) as f64 * batches).min(input_rows as f64) +} + /// The grouping keys `reduction` hashes per row: its keys, or the entity /// (one) when it keeps one row per entity. fn grouping_key_count(reduction: &Reduction) -> u64 { diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index b32fbaf83..76c3feaa2 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -447,6 +447,49 @@ fn a_per_series_aggregate_costs_more_per_row_at_a_lower_sample_rate() { } } +/// A grouping visits each group's state once per raw batch of the +/// deployment's `raw_batch_width_ms`: the per-series aggregate over 5 min +/// of 1,000 series at 1 Hz visits each series 30 times in 10 s batches and +/// 300 times in 1 s batches (one row each), 270,000 visits × 140 +/// operations more. +#[test] +fn a_grouping_visits_each_group_once_per_raw_batch_of_the_declared_width() { + use crate::node::operations_per_unit::OPERATIONS_PER_GROUP_PER_RAW_BATCH; + use asap_types::ir::operator::AggIntent; + let range = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let aggregate = node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::PerEntity, + measures: vec![AggIntent::Max { col: None }], + output_names: vec![String::new()], + filters: vec![], + having: None, + child: 0, + })); + let cpu_ops = |raw_batch_width_ms: u64| { + let mut settings = CostInputs::default(); + settings.resource_speed.raw_batch_width_ms = raw_batch_width_ms; + node_resource_usage_and_cost( + &aggregate, + &[raw_rows(&range, "latency", 300_000)], + (1, 1.0), + None, + &thousand_series_sampled_every(1_000), + &settings, + ) + .unwrap() + .cpu_ops_per_second + }; + let extra = cpu_ops(1_000) - cpu_ops(10_000); + assert!( + (extra - 270_000.0 * OPERATIONS_PER_GROUP_PER_RAW_BATCH).abs() < 1.0, + "1 s batches cost {extra} operations more" + ); +} + /// A per-series exact Rate carrying `job`, built at query time from 5 min /// of 1,000 series of `requests_total`, measures 85.9 ns per row at 1 Hz /// and 68.8 at 10 Hz (1.25x): its update is the same per row, and it visits @@ -499,13 +542,16 @@ fn a_query_time_rate_build_costs_more_per_row_at_a_lower_sample_rate() { /// A query-time chain reads its scan's lookback and keeps each row in its /// window's range, as a `TimeRange` over the same rows does, besides -/// updating the row's state. Example: a sum in 60 s windows over 36,000 -/// raw rows, retaining five windows of one group, costs 36,000 × (40 -/// update + 11 range) operations plus five emitted exact states × 1,500. +/// updating the row's state and visiting its group once per raw batch. +/// Example: a sum in 60 s windows over 36,000 raw rows of unknown rate (one +/// batch), retaining five windows of one group, costs 36,000 × 41 +/// operations (30 to update, 11 to keep in range), one group visit × 330 +/// (140 plus the chain's 190) and five emitted exact states × 1,500. #[test] fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { use crate::node::operations_per_unit::{ - EXACT_UPDATE_OPERATIONS_PER_ROW, TIME_RANGE_OPERATIONS_PER_ROW, + CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH, EXACT_UPDATE_OPERATIONS_PER_ROW, + OPERATIONS_PER_GROUP_PER_RAW_BATCH, TIME_RANGE_OPERATIONS_PER_ROW, }; let settings = CostInputs::default(); let data = DataWorkload::default(); @@ -546,6 +592,8 @@ fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { assert_eq!( usage.cpu_ops_per_second, 36_000.0 * (EXACT_UPDATE_OPERATIONS_PER_ROW + TIME_RANGE_OPERATIONS_PER_ROW) + + OPERATIONS_PER_GROUP_PER_RAW_BATCH + + CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH + 5.0 * 1_500.0 ); } diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index 1de59718e..8c5b89938 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -597,8 +597,11 @@ fn the_objective_is_the_plan_cost_on_one_off_queries_over_years() { // The same queries over arriving rows only, with no raw data kept (a // deployment that stores nothing, as `mixed` arrival does): only // ingestion-time views over the arriving rows can answer them, the - // kind of plan the objective's scale once led the solver to. Reading - // raw data at query time is cheaper. + // kind of plan the objective's scale once led the solver to at 2,000x + // the cost of the best. With raw data kept the selection costs no more: + // here it is that views plan, since reading 5 y of raw rows at query + // time visits each series' state once per row (one sample per 15 s, at + // most one per 10 s raw batch). let capabilities = DeploymentCapabilities { raw_data_retained: false, ..Default::default() @@ -613,8 +616,8 @@ fn the_objective_is_the_plan_cost_on_one_off_queries_over_years() { let (_, (_, views_only_plan_cost)) = selected_plan_objective_and_plan_cost_with(&ingesting, views_only, &CostInputs::default()); assert!( - plan_cost < views_only_plan_cost, - "selected {plan_cost} is not below the ingestion-time views plan, {views_only_plan_cost}" + plan_cost <= views_only_plan_cost, + "selected {plan_cost} is above the ingestion-time views plan, {views_only_plan_cost}" ); } diff --git a/crates/stage3-plan-selection/tests/cost_inputs_json.rs b/crates/stage3-plan-selection/tests/cost_inputs_json.rs index cb890393d..899f9a2cd 100644 --- a/crates/stage3-plan-selection/tests/cost_inputs_json.rs +++ b/crates/stage3-plan-selection/tests/cost_inputs_json.rs @@ -36,6 +36,7 @@ fn settings_load_from_json_with_defaults_for_omitted_fields() { resource_speed: ResourceSpeed { ms_per_cpu_op: 2e-6, ms_per_scanned_byte: 1e-7, + raw_batch_width_ms: 10_000, }, ..CostInputs::default() } From e8fd332648e9888e337d56151ea312ee82949e4d Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:19:03 +0000 Subject: [PATCH 09/15] docs(cost): the per group and raw batch visit, the raw batch width input and the KLL read after updates in the design doc and library API; the rate and increase cells explained by the rate-dependent per-row cost --- .../tests/default_cost_plan_ranking.rs | 12 ++++++++---- .../analytical-resource-cost.md | 17 ++++++++++++----- docs/develop_docs/library-api.md | 2 +- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs index feacc665d..603072769 100644 --- a/crates/integration-tests/tests/default_cost_plan_ranking.rs +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -702,10 +702,14 @@ fn max_over_time_5m_at_1_hz_with_raw_data_keeps_stored_max_windows() { /// 10 Hz every 300 s, raw data kept. Measured cost per second (cost model /// 1's default prices on the measured usage), the Rate or Increase built at /// query time from raw rows against stored 300 s windows: 0.86 against -/// 0.95 for both. Per row, the query-time plan's time range and update -/// measure 79 ns, the stored plan's ingestion-time chain 87: the chain -/// assigns each arriving row its window, which was not charged, so the -/// stored plan was predicted cheaper (regret 1.08–1.10 in every run). +/// 0.95 for both. A grouping's cost per row depends on the sample rate: +/// the executor visits each group's state once per 10 s raw batch, which +/// holds 100 rows of a series at 10 Hz and 10 at 1 Hz, and a chain also +/// splits each group's rows of a batch by window. Charged one per-row +/// constant fitted across both rates, the query-time build was +/// over-charged at 10 Hz (69 ns per row measured, 82 charged) while the +/// stored chain was charged about right, so the stored plan was predicted +/// cheaper (regret 1.08–1.10 in every run). #[test] fn sum_rate_and_increase_over_5m_at_10_hz_every_300_s_with_raw_data_build_at_query_time() { for query in [ diff --git a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md index 2e3fb7dfd..49edb3163 100644 --- a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md +++ b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md @@ -797,11 +797,18 @@ groups of its keys, estimated like any `by (keys)` and at most the input's groups. The merged state is sized for the rows its group's input states fold together: 360 stored KLL windows of 10 rows each merge into a state sized for 3,600 rows. A readout (estimate or finalize) costs each group's output row, -plus reading its state (a KLL's per byte, twice that when no merge has -sorted its levels since its updates). A tumbling-window chain keeps each row -it reads in its window's range at either timing, at a time range's cost per -row: at query time over its scan's lookback, at ingestion time as it assigns -each arriving row its window. A state's row on any edge holds +plus reading its state (a KLL's per byte after a merge, which leaves its +levels sorted; after updates, per unsorted item, at most `k`). A grouping (a +hash aggregate, a summary build or a tumbling-window chain) also costs per +group per batch of raw rows its input arrives in, the deployment's raw +batch width (`ResourceSpeed::raw_batch_width_ms`, 10 s assumed when not +declared): the executor visits each group's state once per batch, so the +same grouping costs more per row at a lower sample rate (a 10 s batch holds +10 rows of a series at 1 Hz, 100 at 10 Hz). A chain also splits each group's +rows of a batch by window; at ingestion time it routes each arriving row to +its window's state, and at query time keeps each row of its scan's lookback +in its window's range at a time range's cost per row. A state's +row on any edge holds its labels, its window's time and its series identity beside the state; a DDSketch state is sized for the values it is expected to hold, spread like latencies, not for the most it could hold; a column's declared diff --git a/docs/develop_docs/library-api.md b/docs/develop_docs/library-api.md index 1932537c6..c2ad60dda 100644 --- a/docs/develop_docs/library-api.md +++ b/docs/develop_docs/library-api.md @@ -197,7 +197,7 @@ asap_stage3_plan_selection::select_plan( | `PlanningModels::new(evidence)` | Your `&dyn AccuracyEvidence` (measured guarantees; `NoMeasuredGuarantees` has none), with default cost inputs and deployment capabilities; `with_evidence` replaces it; `with_ignore_accuracy_guarantees(true)` gives every query no accuracy target | | `PlanningModels::builtin()` | No measured accuracy evidence. `with_cost_inputs(&settings)` sets the deployment's `CostInputs`; `with_deployment_capabilities(&capabilities)` sets its `DeploymentCapabilities` (field `deployment_capabilities`) | | `DeploymentCapabilities` (`asap_types::deployment_capabilities`, re-exported as `asap_planner::DeploymentCapabilities`) | What the executor supports: `supported_summaries` (`None`, the default, supports every summary the planner builds; otherwise `SupportedSummary { summary_type: SketchAlgorithm, readouts: Vec }` entries; Pass 1 prunes others with `PruneReason::UnsupportedByDeployment`), `raw_data_retained` (default `true`; when `false`, a candidate that scans raw data at query time is pruned by Stage 2, and stored data is refused with `asap_planner::SelectionError::InvalidDeploymentCapabilities`) and `query_time_results_retained` (default `false`; when `true`, Stage 2 offers query-time `AcrossEvaluations` for tumbling-window chains) | -| `CostInputs` (`asap_cost`) | `cost_model`, `resource_speed` (`ms_per_cpu_op`, `ms_per_scanned_byte`), `non_repeating_query_horizon_seconds` (default 3,600) and `measured_operation_costs` (`MeasuredOperationCosts`, default empty: `operators`, one `OperatorCost { operator, phase, sketch / exact / measure, ns_per_unit, ns_per_invocation, memory_bytes_per_state }` per measured operator kind, phase and family; `ns_per_evaluation` per query request; `provenance` (executor version, machine). A node with an entry is priced from it, a sketch between the two nearest measured sizes; every other node from the formulas times one calibration factor fitted from the measured nodes; each node's `NodeCost::cost_source` says which. The earlier JSON array of `MeasuredOperationCost { sketch, operation, cpu_ns_per_operation }` still loads) | +| `CostInputs` (`asap_cost`) | `cost_model`, `resource_speed` (`ms_per_cpu_op`, `ms_per_scanned_byte`, `raw_batch_width_ms`: the time one batch of raw rows covers, default 10,000, an assumed value), `non_repeating_query_horizon_seconds` (default 3,600) and `measured_operation_costs` (`MeasuredOperationCosts`, default empty: `operators`, one `OperatorCost { operator, phase, sketch / exact / measure, ns_per_unit, ns_per_invocation, memory_bytes_per_state }` per measured operator kind, phase and family; `ns_per_evaluation` per query request; `provenance` (executor version, machine). A node with an entry is priced from it, a sketch between the two nearest measured sizes; every other node from the formulas times one calibration factor fitted from the measured nodes; each node's `NodeCost::cost_source` says which. The earlier JSON array of `MeasuredOperationCost { sketch, operation, cpu_ns_per_operation }` still loads) | | `CostModel::UsageOverTime(ResourceUnitCost)` | Cost model 1 (default, `ResourceUnitCost::illustrative()`): unit cost × usage per second; the cheapest valid candidate wins | | `CostModel::ProvisionedCapacity(ResourceUnitCost)` | Cost model 2: unit cost × peak memory, peak CPU cores and disk; the cheapest valid candidate wins | | `CostModel::ClusterCapacity(ClusterLimits)` | Cost model 3: memory, disk and optional CPU cores are hard limits; `ClusterObjective::{LowestLatency, LeastMemory}` chooses among the candidates that fit | From fc4d67045f276fc23da8cbf650fb9b8b89163d04 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:05:16 +0000 Subject: [PATCH 10/15] test(cost): batches counted from the time the input spans; a one-state merge passes its state through; keys, measures, exact distinct counts and KMV builds and rollups at their measured costs; the years objective test at a pinned batch width (failing) - a_grouping_counts_raw_batches_from_the_time_its_input_spans and a_query_time_chain_sizes_its_windows_from_the_time_its_input_spans: a filter that keeps a tenth of each series' rows drops no raw batch. - a_merge_reading_one_state_per_group_passes_it_through: KLL and DDSketch merges of one state per group measure about 1 ms per 1,000 groups. - a_hash_aggregate_pays_each_further_key_and_measure_as_measured, an_exact_distinct_count_costs_more_per_row_with_more_distinct_values, a_kmv_build_costs_per_retained_hash_it_compares and a_kmv_rollup_merge_costs_per_hash_it_inserts: SQL aggregates, exact COUNT(DISTINCT) and KMV builds and rollups measured on the executor. - the_objective_is_the_plan_cost_on_one_off_queries_over_years pins raw_batch_width_ms at 30 min, plans with those cost inputs, and checks strictly that keeping raw data costs less than the views. --- crates/cost/src/node/usage/tests.rs | 395 ++++++++++++++++++ .../tests/stage3_objective.rs | 28 +- 2 files changed, 415 insertions(+), 8 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 76c3feaa2..a81e9682c 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -598,6 +598,401 @@ fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { ); } +/// A query-time scan of 5 min of `latency` (1,000 series at 1 Hz, 300,000 +/// rows) under a filter that keeps a tenth of each series' rows, e.g. +/// `latency > 100`: the 30,000 rows left still span 5 min, 30 raw batches +/// of 10 s, so a per-series grouping visits each of its 1,000 groups 30 +/// times, at most once per row: 30,000 visits. Counted from its input rows +/// over the sources' rate, the rows spanned 30 s, 3 batches, 3,000 visits. +/// A `Filter` passes its input's span on, as a `TimeRange` and the +/// operators that keep each row's window do. +#[test] +fn a_grouping_counts_raw_batches_from_the_time_its_input_spans() { + use asap_types::ir::operator::AggIntent; + use asap_types::ir::scalar::{Predicate, ScalarExpr, ScalarValue}; + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let scan = node(metric_scan("latency")); + let scan_usage = + node_resource_usage_and_cost(&scan, &[], (1, 1.0), Some(300_000), &data, &settings) + .unwrap(); + assert_eq!(scan_usage.output.rows, 300_000); + let filter = node(Payload::NonASAP(NonASAPOp::Filter { + pred: Predicate(ScalarExpr::Literal(ScalarValue::Boolean(true))), + child: 0, + })); + let filter_usage = node_resource_usage_and_cost( + &filter, + &[produced(&scan, &scan_usage)], + (1, 1.0), + None, + &data, + &settings, + ) + .unwrap(); + // The filter keeps a tenth of the rows; the cost model passes every + // row on, so the test sets what it keeps. + let mut kept = produced(&filter, &filter_usage); + kept.output.rows = 30_000; + kept.output.bytes /= 10; + let aggregate = node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::PerEntity, + measures: vec![AggIntent::Max { col: None }], + output_names: vec![String::new()], + filters: vec![], + having: None, + child: 0, + })); + let usage = node_resource_usage_and_cost(&aggregate, &[kept], (1, 1.0), None, &data, &settings) + .unwrap(); + assert!( + usage.cost.detail.contains(", 30000 group visits"), + "{}", + usage.cost.detail + ); +} + +/// A query-time chain of 60 s windows over the same filtered scan (30,000 +/// rows over 5 min): each window holds the rows of its 60 s, 6,000, so +/// each of its 1,000 per-series states folds 6 rows. Sized by the sources' +/// rate, a window held 60,000 rows (all 30,000 read), 30 per state. +#[test] +fn a_query_time_chain_sizes_its_windows_from_the_time_its_input_spans() { + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let scan = node(metric_scan("latency")); + let scan_usage = + node_resource_usage_and_cost(&scan, &[], (1, 1.0), Some(300_000), &data, &settings) + .unwrap(); + let mut kept = produced(&scan, &scan_usage); + kept.output.rows = 30_000; + kept.output.bytes /= 10; + let chain = PhysicalASAPDAGNode { + retention_ms: Some(300_000), + ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: FieldDataType::Sketch(asap_types::summary_type::SketchAlgorithm::Kll), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + })) + }; + let chain = PhysicalASAPDAGNode { + parameters: Some(SketchParams::Kll { k: 547 }), + ..chain + }; + let usage = + node_resource_usage_and_cost(&chain, &[kept], (1, 1.0), None, &data, &settings).unwrap(); + assert_eq!( + usage.state_input_rows.and_then(|rows| rows.rows_per_state), + Some(6) + ); +} + +/// A merge that reads one state per group passes each state through: the +/// executor moves a group's first state into its slot without merging it +/// (`merge_objects`), so it pays only each group's row. Merging the one +/// 10 s window a time range selects from a KLL chain (k = 547, 1,000 +/// series at 10 Hz, 100 rows per state) measures 1.0 ms, and from a +/// DDSketch chain 0.97 ms; charged as merges, they were 29.3 and 3.45 ms. +#[test] +fn a_merge_reading_one_state_per_group_passes_it_through() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let window = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(10), + kind: TimeRangeKind::Range, + child: 0, + })); + for (parameters, measured_ms) in [ + (SketchParams::Kll { k: 547 }, 1.0), + (SketchParams::DDSketch { alpha: 0.01 }, 0.97), + ] { + let state_bytes = crate::node::state_bytes::sketch_state_bytes( + ¶meters, + StateInputRows { + rows_per_state: Some(100), + non_negative: true, + value_magnitude_range: None, + }, + ); + let one_window = ProducerOutput { + payload: &window.payload, + output_schema: &window.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 1_000 * state_bytes, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + state_input_rows: Some(StateInputRows { + rows_per_state: Some(100), + non_negative: true, + value_magnitude_range: None, + }), + }; + let merge = PhysicalASAPDAGNode { + parameters: Some(parameters.clone()), + ..node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })) + }; + let usage = + node_resource_usage_and_cost(&merge, &[one_window], (1, 1.0), None, &data, &settings) + .unwrap(); + let charged_ms = usage.work_ms_per_execution; + assert!( + (0.8..=1.25).contains(&(measured_ms / charged_ms)), + "{parameters:?}: charged {charged_ms:.3} ms, measured {measured_ms}" + ); + } +} + +/// A SQL table `t` of 1,000 groups of rows (`instance`) sampled every +/// second, 10 `job`s and 4 `region`s, whose `user_id` takes +/// `user_ids` distinct values; 300,000 of its rows, as a time range +/// passes them to a grouping. +fn sql_rows_of_t(user_ids: u64) -> (DataWorkload, Schema) { + use asap_types::ir::schema::DataType; + let data = serde_json::from_value(serde_json::json!({ + "arrival": {"ingesting": {"rows_per_second": 1000.0}}, + "sources": {"t": {"series_count": 1000, "sample_interval": 1000, + "distinct_values_per_label": {"job": 10, "region": 4, "instance": 1000, + "user_id": user_ids}}} + })) + .unwrap(); + let schema = Schema::new(vec![ + Field::new("ts", FieldDataType::Plain(DataType::Timestamp), false), + Field::new("job", FieldDataType::Plain(DataType::Utf8), false), + Field::new("region", FieldDataType::Plain(DataType::Utf8), false), + Field::new("instance", FieldDataType::Plain(DataType::Utf8), false), + Field::new("user_id", FieldDataType::Plain(DataType::Float64), false), + Field::new("value", FieldDataType::Plain(DataType::Float64), false), + ]); + (data, schema) +} + +/// The CPU operations per input row a query-time hash aggregate of +/// `measures` grouped by `keys` charges over [`sql_rows_of_t`]'s 300,000 +/// rows. +fn sql_aggregate_operations_per_row( + keys: Vec, + measures: Vec, + user_ids: u64, +) -> f64 { + let settings = CostInputs::default(); + let (data, schema) = sql_rows_of_t(user_ids); + let range = PhysicalASAPDAGNode { + output_schema: schema, + ..node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })) + }; + let mut rows = raw_rows(&range, "t", 300_000); + rows.output_schema = &range.output_schema; + rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); + let names = measures.iter().map(|_| String::new()).collect(); + let aggregate = node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::by(keys), + measures, + output_names: names, + filters: vec![], + having: None, + child: 0, + })); + let usage = node_resource_usage_and_cost(&aggregate, &[rows], (1, 1.0), None, &data, &settings) + .unwrap(); + usage.cpu_ops_per_second / 300_000.0 +} + +/// A SQL hash aggregate pays per row for each grouping key beyond the +/// first and for each measure, as measured on the executor (300,000 rows +/// of 1,000 groups, 100 rows per group per raw batch, so the group visits +/// are negligible): `GROUP BY job, instance` costs 34.0 ns per row more +/// than `GROUP BY instance`, `GROUP BY job, region, instance` 50.4 more, +/// and `MAX, MIN, SUM, COUNT` 11.9 more than `MAX` alone. Charged 15 per +/// key and 3 per measure, the second and third keys were 0.44x and 0.60x +/// their measured cost. +#[test] +fn a_hash_aggregate_pays_each_further_key_and_measure_as_measured() { + use asap_types::ir::operator::AggIntent; + let max = || vec![AggIntent::Max { col: Some(5) }]; + let four = || { + vec![ + AggIntent::Max { col: Some(5) }, + AggIntent::Min { col: Some(5) }, + AggIntent::Sum { col: Some(5) }, + AggIntent::Count, + ] + }; + let one_key = sql_aggregate_operations_per_row(vec![3], max(), 1); + for (charged, measured, what) in [ + ( + sql_aggregate_operations_per_row(vec![1, 3], max(), 1) - one_key, + 34.0, + "a second key", + ), + ( + sql_aggregate_operations_per_row(vec![1, 2, 3], max(), 1) - one_key, + 50.4, + "two more keys", + ), + ( + sql_aggregate_operations_per_row(vec![3], four(), 1) - one_key, + 11.9, + "three more measures", + ), + ] { + assert!( + (0.8..=1.25).contains(&(charged / measured)), + "{what}: charged {charged:.1} ns per row more, measured {measured}" + ); + } +} + +/// An exact `COUNT(DISTINCT user_id)` over 300,000 rows interns each value +/// in a table of every distinct value read, then keeps each group's +/// (group, value) pairs: its time per row grows with the distinct values. +/// Measured on the executor with one group: 115 ns per row over 10,000 +/// distinct values and 589 over 259,000; charged 18 at both. +#[test] +fn an_exact_distinct_count_costs_more_per_row_with_more_distinct_values() { + use asap_types::ir::operator::AggIntent; + for (user_ids, measured) in [(10_000, 115.0), (259_000, 589.0)] { + let charged = sql_aggregate_operations_per_row( + vec![], + vec![AggIntent::Cardinality { cols: vec![4] }], + user_ids, + ); + assert!( + (0.75..=1.33).contains(&(charged / measured)), + "{user_ids} distinct: charged {charged:.1} ns per row, measured {measured}" + ); + } +} + +/// A KMV state compares each row's hash with the hashes it retains until +/// it holds `k` of them, then only hashes below its largest +/// (`asap_sketchlib`'s duplicate check). Over 300,000 rows of 100,000 +/// distinct `user_id`s, one state at k = 4,323 fills early and measures +/// 191 ns per row; four states by `region` at k = 26,656 (75,000 rows +/// each) stay below `k` for most rows and measure 7,410. Charged a fixed +/// 48 per row, the grouped build looked cheaper than an exact distinct +/// count it measures 25x more than. +#[test] +fn a_kmv_build_costs_per_retained_hash_it_compares() { + use asap_types::summary_type::SketchAlgorithm; + let settings = CostInputs::default(); + let (data, schema) = sql_rows_of_t(100_000); + let range = PhysicalASAPDAGNode { + output_schema: schema, + ..node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })) + }; + for (keys, k, measured) in [(vec![], 4_323, 191.0), (vec![2], 26_656, 7_410.0)] { + let mut rows = raw_rows(&range, "t", 300_000); + rows.output_schema = &range.output_schema; + rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); + let build = PhysicalASAPDAGNode { + parameters: Some(SketchParams::Kmv { k }), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), + reduction: Reduction::by(keys.clone()), + filter: None, + })) + }; + let usage = node_resource_usage_and_cost(&build, &[rows], (1, 1.0), None, &data, &settings) + .unwrap(); + let charged = usage.cpu_ops_per_second / 300_000.0; + assert!( + (0.75..=1.33).contains(&(charged / measured)), + "k = {k}, keys {keys:?}: charged {charged:.1} ns per row, measured {measured}" + ); + } +} + +/// A rollup merge of KMV states updates each group's state with every hash +/// each input state retains (`asap_sketchlib`'s merge), so it compares +/// those hashes with the ones the group's state already retains. Measured +/// on the executor: 1,000 states by (region, service) at k = 106,263, each +/// built from 12,000 rows of 1,000,000 distinct users, merged into 100 +/// groups by service take 352 s (the plan's 393 s less its 40 s build). +/// Charged per byte of state, they were 0.1 s. +#[test] +fn a_kmv_rollup_merge_costs_per_hash_it_inserts() { + use asap_types::ir::schema::DataType; + use asap_types::summary_type::SketchAlgorithm; + let settings = CostInputs::default(); + let data: DataWorkload = serde_json::from_value(serde_json::json!({ + "arrival": {"ingesting": {"rows_per_second": 2_000_000.0}}, + "sources": {"http": {"distinct_values_per_label": {"service": 100, "region": 10}}} + })) + .unwrap(); + let parameters = SketchParams::Kmv { k: 106_263 }; + let rows = StateInputRows { + rows_per_state: Some(12_000), + non_negative: false, + value_magnitude_range: None, + }; + let state_bytes = crate::node::state_bytes::sketch_state_bytes(¶meters, rows); + let build = PhysicalASAPDAGNode { + parameters: Some(parameters.clone()), + output_schema: Schema::new(vec![ + Field::new("region", FieldDataType::Plain(DataType::Utf8), false), + Field::new("service", FieldDataType::Plain(DataType::Utf8), false), + Field::new("state", FieldDataType::Sketch(SketchAlgorithm::Kmv), false), + ]), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), + reduction: Reduction::by(vec![0, 1]), + filter: None, + })) + }; + let states = ProducerOutput { + payload: &build.payload, + output_schema: &build.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 1_000 * (state_bytes + 32), + }, + sources: BTreeSet::from(["http"]), + family: StateFamily::Sketch(¶meters), + unchanged_columns: ["region", "service"].into(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + state_input_rows: Some(rows), + }; + let rollup = PhysicalASAPDAGNode { + parameters: Some(parameters.clone()), + ..node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: Some(vec![1]), + })) + }; + let usage = + node_resource_usage_and_cost(&rollup, &[states], (1, 1.0), None, &data, &settings).unwrap(); + assert_eq!(usage.output.rows, 100); + let charged_s = usage.work_ms_per_execution / 1_000.0; + assert!( + (0.75..=1.33).contains(&(352.0 / charged_s)), + "charged {charged_s:.1} s, measured 352" + ); +} + /// A query-time node holds its state only /// while it computes: bytes × its work per execution × its executions /// per second, with no query latency. A KLL state of 2 MB whose own diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index 8c5b89938..93ab5ee30 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -567,6 +567,15 @@ fn the_objective_is_the_plan_cost_on_anchored_and_plain_queries_on_one_grid() { /// per second instead, $\pi_v h_v$ of a view kept for years was 6.6e12/s, /// its $W^{\mathrm{mat}}_v$ as negative, and the objective's scale left the /// solver at a plan costing 4.68e7/s instead of 2.17e4/s. +/// +/// The data store here keeps raw rows in batches of 30 min +/// (`raw_batch_width_ms`), which the second check depends on: a per-series +/// grouping visits each series once per batch, and a 15 s series has at +/// most one row in a batch narrower than 15 s, so at the 10 s default every +/// row is a visit, the raw scan costs more than the views and the two +/// plans are the same. With 30 min batches, a series has 120 rows in a +/// batch, and the plan selected with raw data kept costs 4.7e5/s against +/// 7.1e5/s for the views. #[test] fn the_objective_is_the_plan_cost_on_one_off_queries_over_years() { let query = |query: &str| { @@ -590,33 +599,36 @@ fn the_objective_is_the_plan_cost_on_one_off_queries_over_years() { }, })) .unwrap(); - let models = PlanningModels::builtin().with_ignore_accuracy_guarantees(true); + let mut settings = CostInputs::default(); + settings.resource_speed.raw_batch_width_ms = 1_800_000; + let models = PlanningModels::builtin() + .with_ignore_accuracy_guarantees(true) + .with_cost_inputs(&settings); let (objective, plan_cost) = - selected_plan_objective_and_plan_cost_with(&w, models, &CostInputs::default()).1; + selected_plan_objective_and_plan_cost_with(&w, models, &settings).1; assert_equal("one-off queries over years", (objective, plan_cost)); // The same queries over arriving rows only, with no raw data kept (a // deployment that stores nothing, as `mixed` arrival does): only // ingestion-time views over the arriving rows can answer them, the // kind of plan the objective's scale once led the solver to at 2,000x - // the cost of the best. With raw data kept the selection costs no more: - // here it is that views plan, since reading 5 y of raw rows at query - // time visits each series' state once per row (one sample per 15 s, at - // most one per 10 s raw batch). + // the cost of the best. With raw data kept in 30 min batches, reading + // them at query time is cheaper, so the selection costs less. let capabilities = DeploymentCapabilities { raw_data_retained: false, ..Default::default() }; let views_only = PlanningModels::builtin() .with_ignore_accuracy_guarantees(true) + .with_cost_inputs(&settings) .with_deployment_capabilities(&capabilities); let mut ingesting = serde_json::to_value(&w).unwrap(); ingesting["data_workload"]["arrival"] = serde_json::json!({"ingesting": {"rows_per_second": 1_000_000.0 / 15.0}}); let ingesting: PlanningWorkload = serde_json::from_value(ingesting).unwrap(); let (_, (_, views_only_plan_cost)) = - selected_plan_objective_and_plan_cost_with(&ingesting, views_only, &CostInputs::default()); + selected_plan_objective_and_plan_cost_with(&ingesting, views_only, &settings); assert!( - plan_cost <= views_only_plan_cost, + plan_cost < views_only_plan_cost, "selected {plan_cost} is above the ingestion-time views plan, {views_only_plan_cost}" ); } From b62c370492d85729dbd705ede723a361d930272a Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:05:28 +0000 Subject: [PATCH 11/15] fix(cost): raw batches from the time the input spans; one-state merges as pass-through; keys, measures, exact distinct counts and KMV at their measured costs; per-row, visit and chain constants refit on a grid - A grouping counts the raw batches its input arrives in from the time its raw rows span (ProducerOutput::raw_rows_span_ms: a scan's lookback, narrowed by a time range, kept by the operators that keep each row's window), and a query-time chain sizes its windows the same way, so a filter between the scan and the grouping drops no batch. - A SummaryMerge that reads one state per group passes each state through and pays only each group's row. - A hash aggregate pays 16 per row for its first key, 28 per further key and 4 per measure (SQL aggregates over 1 to 3 keys, 1 to 4 measures, 100 to 10,000 groups). - An exact distinct count or frequency statistic pays per square root of the distinct values its column takes and per value and pair it inserts (45 executor aggregates up to 3 M distinct values). - A KMV update and merge pay per retained hash they compare a hash with (asap_sketchlib's duplicate check); 22 builds and one rollup. - OPERATIONS_PER_GROUP_PER_RAW_BATCH 170, EXACT 29, COUNTER 33, DDSKETCH 210, KLL 84, CHAIN_OPERATIONS_PER_ARRIVING_ROW 14, refit by squared log error over 245 workloads (1 to 10 Hz, 100 to 10,000 series, 5 min to 1 h, every 60 and 300 s); the loss and the terms chosen by leave-one-setting-out error. The chain's own per group and batch term is deleted: it did not lower that error. - raw_batch_width_ms is the data store's batch width. - The two SQL distinct rollup tests now expect the exact counts, the plan measured cheaper; the Rate chain test takes this run's measurements. --- crates/cost/src/model.rs | 21 +- crates/cost/src/node/operations_per_unit.rs | 397 +++++++++++++----- crates/cost/src/node/usage/mod.rs | 247 +++++++++-- crates/cost/src/node/usage/tests.rs | 73 ++-- crates/cost/src/plan/cost.rs | 2 +- .../tests/production_rollup_workloads.rs | 40 +- .../tests/rollup_controlled.rs | 55 +-- crates/stage3-plan-selection/src/cost.rs | 17 +- 8 files changed, 633 insertions(+), 219 deletions(-) diff --git a/crates/cost/src/model.rs b/crates/cost/src/model.rs index 6dd32a18d..40e16c35c 100644 --- a/crates/cost/src/model.rs +++ b/crates/cost/src/model.rs @@ -127,14 +127,19 @@ pub struct ResourceSpeed { pub ms_per_cpu_op: f64, /// Milliseconds scanning one byte takes on one core. pub ms_per_scanned_byte: f64, - /// The time one batch of raw rows covers, as the executor receives and - /// stores them: a grouping visits each group's state once per batch - /// its rows arrive in, so a 5 min window of a series sampled at 1 Hz - /// in 10 s batches costs 30 visits per series, whether it holds 300 - /// rows or (at 10 Hz) 3,000. Default 10,000 (10 s), an assumed value: - /// the batch width the reference executor's measurements stored, not - /// one the deployment declared; omitted from JSON, it takes that - /// default. + /// The time one batch of raw rows covers in the deployment's data + /// store, which returns raw rows to a query in those batches, each + /// series' rows of a batch together. The executor has no batch width of + /// its own: it groups the batches the store returns and looks up a + /// series' group once per batch, so a grouping visits each group's + /// state once per batch its rows are in. A 5 min window of a series + /// sampled at 1 Hz in 10 s batches costs 30 visits per series, whether + /// it holds 300 rows or (at 10 Hz) 3,000. Default 10,000 (10 s), an + /// assumed value: the batch width of the store the reference + /// measurements read from, not one the deployment declared; omitted + /// from JSON, it takes that default. Ingestion-time work is charged + /// one second's share of a batch per execution, which follows the + /// arriving rows, not the store's batches. #[serde(default = "default_raw_batch_width_ms")] pub raw_batch_width_ms: u64, } diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index 8fb32f363..1cf192c4f 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -38,13 +38,13 @@ //! //! Example: `quantile_over_time(0.99, latency[1h])` at 1 Hz reads 3.6 M //! rows. The exact plan keeps them (`TIME_RANGE_OPERATIONS_PER_ROW`, 11), -//! groups them per series (`HASH_AGGREGATE_OPERATIONS_PER_KEY` + -//! `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, 18, plus -//! `OPERATIONS_PER_GROUP_PER_RAW_BATCH` once per series and 10 s batch, 14 +//! groups them per series (`HASH_AGGREGATE_OPERATIONS_PER_ROW` + +//! `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, 20, plus +//! `OPERATIONS_PER_GROUP_PER_RAW_BATCH` once per series and 10 s batch, 17 //! per row) and sorts each series' 3,600 values (11.8 levels × -//! `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 108 ns per row. A DDSketch built -//! from the same rows takes 11 + `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (190) + -//! 14 = 215 ns per row, about twice the exact plan, as measured (111 and +//! `SORT_OPERATIONS_PER_ROW_PER_LEVEL`, 65): 113 ns per row. A DDSketch built +//! from the same rows takes 11 + `DDSKETCH_UPDATE_OPERATIONS_PER_ROW` (210) + +//! 17 = 238 ns per row, about twice the exact plan, as measured (111 and //! 229). // Each rule lists every variant, as in `usage`: a wildcard arm would give a @@ -57,7 +57,7 @@ use asap_types::ir::operator::AggIntent; use asap_types::ir::schema::FieldDataType; -use asap_types::summary_type::{ExactKind, SketchParams}; +use asap_types::summary_type::{ExactKind, SketchAlgorithm, SketchParams}; use crate::StateFamily; @@ -74,52 +74,68 @@ pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: f64 = 11.0; /// ([`ResourceSpeed::raw_batch_width_ms`](crate::ResourceSpeed)): finding /// the group's state again for the next batch. It is why the same grouping /// costs more per row at a lower sample rate: a 10 s batch holds 10 rows -/// of a series at 1 Hz and 100 at 10 Hz, so this term adds 14 ns per row -/// at 1 Hz and 1.4 at 10 Hz. Fitted together with the per-row constants -/// below, minimizing the absolute log error (robust to the few outlying -/// nodes) over 132 query-time `Aggregate` and `SummaryAgg` nodes -/// (per-series Max, Count, Sum and quantile aggregates; Max, Sum, Count, -/// Rate, Increase, DDSketch and KLL builds), 5 min and 1 h windows at -/// 1 Hz and 10 Hz over 100, 1,000 and 10,000 series, and 12 ingestion-time -/// chains: 144 per group and batch. A per-series hash aggregate then -/// measures 0.78–1.22x its charge at 1 Hz and 0.89–1.27x at 10 Hz -/// (0.88–1.31x and 0.72–0.95x without the term), an exact accumulator -/// build 0.90–1.27x and 0.89–1.14x (0.73–1.34x and 0.73–1.04x). -pub(crate) const OPERATIONS_PER_GROUP_PER_RAW_BATCH: f64 = 140.0; - -/// Per group of a tumbling window chain, per batch of raw rows, beyond -/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: splitting the group's rows of the -/// batch by the window each falls in. A Rate chain at ingestion time -/// measures 124–126 ns per row at 1 Hz and 80–86 at 10 Hz over 1,000 and -/// 10,000 series, where the same Rate built at query time measures 86–110 -/// and 69–75: the chain's work per group and batch is about twice a plain -/// build's (fitted 186 with the constants above). With -/// `CHAIN_OPERATIONS_PER_ARRIVING_ROW`, Rate, Sum and KLL chains at -/// ingestion time then measure 0.82–1.04x their charge at 1 Hz and -/// 0.89–1.26x at 10 Hz over 100 to 10,000 series (1.0–1.4x and 0.9–1.3x -/// with neither term). -pub(crate) const CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH: f64 = 190.0; +/// of a series at 1 Hz and 100 at 10 Hz, so this term adds 17 ns per row +/// at 1 Hz and 1.7 at 10 Hz. +/// +/// Fitted together with the per-row constants below +/// (`HASH_AGGREGATE_OPERATIONS_PER_ROW` with one measure, +/// `EXACT_UPDATE_OPERATIONS_PER_ROW`, `COUNTER_UPDATE_OPERATIONS_PER_ROW`, +/// `DDSKETCH_UPDATE_OPERATIONS_PER_ROW`, `KLL_UPDATE_OPERATIONS_PER_ROW` +/// and `CHAIN_OPERATIONS_PER_ARRIVING_ROW`) over a grid of 245 workloads: +/// 1, 2, 5 and 10 Hz; 100, 1,000 and 10,000 series; 5 min, 30 min and 1 h +/// windows (at most 36 M rows); every 300 s, and every 60 s at 1,000 +/// series over 5 min. The points are 572 query-time per-series hash +/// aggregates and summary builds (Max, Count, Sum and quantile aggregates; +/// Max, Sum, Count, Rate, Increase, DDSketch and KLL builds) and 254 +/// stored plans' ingestion CPU. The loss and the terms were chosen by +/// leave-one-setting-out error (each rate, series count, window and +/// interval left out in turn, its points predicted by a fit on the rest): +/// squared log error 0.147, absolute 0.150. A chain's term per group and +/// batch did not lower it (0.147 with it, fitted 15), so a chain pays only +/// this one. Fitted 171, then rounded with the others: measured ÷ charged +/// is 0.76–1.50 for per-series hash aggregates at 1 Hz and 0.87–1.26 at +/// 10 Hz, 0.66–1.48 and 0.93–1.30 for exact builds, 0.67–1.42 and +/// 0.81–1.16 for ingestion chains (RMS log error 0.143 over all points, +/// 0.155 with the constants fitted on 1 and 10 Hz alone). The error is +/// largest at 1 Hz and at 10,000 series, which nothing here follows. +pub(crate) const OPERATIONS_PER_GROUP_PER_RAW_BATCH: f64 = 170.0; /// Per row an ingestion-time tumbling window chain receives: routing the /// arriving row to its window's state, which a build at query time does -/// not do (fitted 7.9 with the constants above, the chain's per group and -/// batch term included). A query-time chain instead keeps each row of its -/// scan's lookback in its window's range (`TIME_RANGE_OPERATIONS_PER_ROW`). -pub(crate) const CHAIN_OPERATIONS_PER_ARRIVING_ROW: f64 = 8.0; - -/// Per input row, for each grouping key of a hash aggregate: hashing the -/// key and finding its group. With `AGGREGATE_MEASURE_OPERATIONS_PER_ROW` -/// and `OPERATIONS_PER_GROUP_PER_RAW_BATCH`, a per-series `max_over_time` -/// (one key, one measure) groups a row in 18 + 14 ns at 1 Hz and -/// 18 + 1.4 at 10 Hz (fitted 17.5 for the two per-row constants). The -/// measurements fix only that sum, for one key and one measure; the part -/// the group and batch term took is charged to the measure, so a grouping -/// by several keys keeps its charge per key. -pub(crate) const HASH_AGGREGATE_OPERATIONS_PER_KEY: f64 = 15.0; +/// not do (fitted 13.8 with `OPERATIONS_PER_GROUP_PER_RAW_BATCH`). A +/// query-time chain instead keeps each row of its scan's lookback in its +/// window's range (`TIME_RANGE_OPERATIONS_PER_ROW`). +pub(crate) const CHAIN_OPERATIONS_PER_ARRIVING_ROW: f64 = 14.0; + +/// Per input row of a hash aggregate grouped by one key or more: interning +/// the row's first key value in the key column's table of distinct values +/// and reading its group at that value's index (the executor's +/// `GroupTable` with one group column). With +/// `AGGREGATE_MEASURE_OPERATIONS_PER_ROW` and +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`, a per-series `max_over_time` (one +/// key, one measure) groups a row in 16 + 4 + 17 ns at 1 Hz and 16 + 4 + +/// 1.7 at 10 Hz (fitted 20.4 for one key and one measure). A SQL +/// aggregate grouped by one key measures about 4 ns per row less (12.8 + +/// 3.8 when fitted on its own). +pub(crate) const HASH_AGGREGATE_OPERATIONS_PER_ROW: f64 = 16.0; + +/// Per input row, for each grouping key beyond the first: interning that +/// key's value too and finding the group by the tuple of values in a hash +/// map, which a grouping by one key does not need. Fitted on SQL hash +/// aggregates over 1 to 3 keys and 1 to 4 measures, 100 to 10,000 groups +/// of 30 to 3,000 rows, 10 and 100 rows per group per raw batch (65 +/// aggregates, minimizing the absolute log error, chosen by +/// leave-one-setting-out error 0.156 against 0.167 for the squared): 28 per +/// further key and 3.8 per measure, beside 12.8 for the first key. A +/// second key measures 29–34 ns per row more than one key, a third 16–20 +/// more than two. +pub(crate) const HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY: f64 = 28.0; /// Per input row, for each plain measure of a hash aggregate (sum, count, -/// min, max, ...): folding the row's value into its group's. -pub(crate) const AGGREGATE_MEASURE_OPERATIONS_PER_ROW: f64 = 3.0; +/// min, max, ...): folding the row's value into its group's. Three more +/// measures (`MIN`, `SUM`, `COUNT` beside `MAX`) measure 10–12 ns per row +/// more (see `HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY`). +pub(crate) const AGGREGATE_MEASURE_OPERATIONS_PER_ROW: f64 = 4.0; /// Per input row, per level of the sort that an exact quantile runs over /// its group's values (`log2` of the rows per group levels): a per-series @@ -129,25 +145,26 @@ pub(crate) const AGGREGATE_MEASURE_OPERATIONS_PER_ROW: f64 = 3.0; pub(crate) const SORT_OPERATIONS_PER_ROW_PER_LEVEL: f64 = 5.5; /// One row folded into a DDSketch state, beyond -/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: 193–278 ns per row at 1 Hz and -/// 172–191 at 10 Hz at query time over 100 to 10,000 series (fitted 189). -pub(crate) const DDSKETCH_UPDATE_OPERATIONS_PER_ROW: f64 = 190.0; +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH` (fitted 210.8 on the grid of +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`): a query-time build measures +/// 0.83–1.45x its charge at 1 Hz and 0.81–0.96x at 10 Hz. +pub(crate) const DDSKETCH_UPDATE_OPERATIONS_PER_ROW: f64 = 210.0; /// One row folded into a KLL state, beyond -/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: 65–112 ns per row at 1 Hz and -/// 83–92 at 10 Hz at query time over 100 to 10,000 series (fitted 84). -/// A state of fewer rows than its `k` is cheaper (65–94 at 300 rows), as -/// it compacts nothing. +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH` (fitted 83.5 on the grid of +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`): a query-time build measures +/// 0.68–1.55x its charge at 1 Hz and 0.97–1.23x at 10 Hz. A state of fewer +/// rows than its `k` is cheaper, as it compacts nothing. pub(crate) const KLL_UPDATE_OPERATIONS_PER_ROW: f64 = 84.0; /// One row folded into an exact accumulator that keeps one value (sum, /// count, min, max), beyond any label it carries /// (`CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL`) and -/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`: an exact Max built per series at -/// query time takes 40–49 ns per row at 1 Hz and 31–35 at 10 Hz over 100 -/// to 10,000 series, an exact Sum grouped by `job` (10 groups) 29–33 at -/// both rates (fitted 30.2). -pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 30.0; +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH` (fitted 28.7 on the grid of +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`): an exact Max, Sum or Count built +/// at query time measures 0.66–1.34x its charge at 1 Hz and 0.97–1.30x at +/// 10 Hz. +pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 29.0; /// One row folded into an exact accumulator of a counter's increase (rate, /// increase, irate), which also tracks resets and the first and last @@ -156,10 +173,12 @@ pub(crate) const EXACT_UPDATE_OPERATIONS_PER_ROW: f64 = 30.0; /// label at query time it takes 66–110 ns per row over 22 nodes at 1 Hz /// and 10 Hz over 100, 1,000 and 10,000 series, of which the label is /// `CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL` (37). The time per row -/// follows the sample rate, not the rows per series: 66–85 at 10 Hz (3,000 -/// and 36,000 rows per series) and 78–110 at 1 Hz (300 and 3,600), as the -/// group and batch term charges it (fitted 35.8 with it). -pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 36.0; +/// follows the sample rate, not the rows per series, as the group and +/// batch term charges it (fitted 33.4 with it on the grid of +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH`): a Rate or Increase built at +/// query time measures 0.87–1.48x its charge at 1 Hz and 0.93–1.11x at +/// 10 Hz. +pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 33.0; /// Per input row, for each label column a per-series state or group /// carries beside its key (the `job` of a per-series sum that a later @@ -172,6 +191,78 @@ pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 36.0; /// measures 74 ns per row and a Max 40 (medians of 12 nodes each): 34. pub(crate) const CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL: f64 = 37.0; +/// One row folded into a KMV state, beyond the retained hashes it compares +/// the row's hash with (`KMV_OPERATIONS_PER_COMPARED_HASH`): hashing the +/// value and the state's checks. Fitted with that term on the executor +/// over 22 builds: 300,000 rows of 100 to 10,000,000 distinct values, one +/// state at k = 4,323 and 4 to 1,000 states at k = 26,656 (minimizing the +/// squared log error, chosen by leave-one-out error over the distinct +/// counts, 0.108 against 0.202 for the absolute): 65 per row and 0.50 per +/// compared hash, within 0.75–1.27x of each build. +const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 65.0; + +/// Per retained hash a KMV update compares the row's hash with +/// ([`kmv_compared_hashes_per_row`]): `asap_sketchlib` checks a new hash +/// for a duplicate by walking every hash it retains, unless the state is +/// full and the hash is above its largest. Example: 4 states by `region` at +/// k = 26,656 over 75,000 rows each of 100,000 distinct values compare +/// 13,820 hashes per row, 6,975 ns charged, 7,410 measured; one state at +/// k = 4,323 over all 300,000 rows fills early and compares 314, 222 ns +/// charged, 191 measured. +const KMV_OPERATIONS_PER_COMPARED_HASH: f64 = 0.5; + +/// Per input row of an exact distinct count or frequency statistic, beyond +/// its lookups and inserts below: reading the row's value. Fitted on the +/// executor's exact `COUNT(DISTINCT user_id)` over 300,000 and 3,000,000 +/// rows of 100 to 100,000,000 distinct values, in 1 to 1,000 groups (45 +/// aggregates; squared log error, the form chosen among 26 by the error +/// leaving each distinct count and row count out in turn, 0.219; measured +/// ÷ charged 0.74–1.83): 61 per row, less the measure's own +/// `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`. +const EXACT_DISTINCT_OPERATIONS_PER_ROW: f64 = 57.0; + +/// Per input row of an exact distinct count, per square root of the +/// distinct values its value table holds (at most +/// `EXACT_DISTINCT_LOOKUP_MOST_VALUES`): looking the value up, a cache +/// miss more often as the table grows (fitted 0.493). Example: 300,000 rows +/// of 10,000 distinct values measure 115 ns per row, charged 57 + 4 + 49 + +/// 7 = 117; of 259,000, 589, charged 57 + 4 + 249 + 150 = 460. +const EXACT_DISTINCT_LOOKUP_OPERATIONS_PER_SQRT_VALUE: f64 = 0.49; + +/// The most distinct values measured: past 3,000,000 the lookup is +/// charged as at 3,000,000 (943 ns per row at 2,955,000 values against +/// 918 at 2,592,000, as each lookup misses the cache either way). +const EXACT_DISTINCT_LOOKUP_MOST_VALUES: f64 = 3_000_000.0; + +/// Per entry an exact distinct count inserts, shared over its input rows: +/// each distinct value into its value table and each (group, value) pair +/// into its set of pairs (fitted 103). +const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_ENTRY: f64 = 103.0; + +/// The operations one input row costs an exact distinct count (or +/// frequency statistic) over `rows` rows in `group_count` groups, of a +/// column taking `distinct_values` values, beyond the measure's own: the +/// executor interns each value in one table of every distinct value read +/// and keeps each group's (group, value) pairs in a set, so its time per +/// row grows with the distinct values. The pairs are each group's +/// expected distinct values among its rows, drawn from `distinct_values`. +/// Example: 300,000 rows of 10,000 values in one group cost 57 + 0.49 × +/// 100 + 103 × 20,000 ÷ 300,000 = 113. +pub(crate) fn exact_distinct_value_operations_per_row( + distinct_values: u64, + group_count: u64, + rows: u64, +) -> f64 { + let values = distinct_values.max(1) as f64; + let rows = rows.max(1) as f64; + let groups = group_count.max(1) as f64; + let pairs = groups * -values * (-(rows / groups) / values).exp_m1(); + EXACT_DISTINCT_OPERATIONS_PER_ROW + + EXACT_DISTINCT_LOOKUP_OPERATIONS_PER_SQRT_VALUE + * values.min(EXACT_DISTINCT_LOOKUP_MOST_VALUES).sqrt() + + EXACT_DISTINCT_OPERATIONS_PER_INSERTED_ENTRY * (values.min(rows) + pairs) / rows +} + /// One counter update of any other sketch, per counter its row updates /// ([`counters_updated_per_row`]), above /// `EXACT_UPDATE_OPERATIONS_PER_ROW`, the executor's cost of handing a row @@ -279,11 +370,9 @@ pub(crate) const SKETCH_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 1_000.0; /// top-k estimate emits `k` rows per group and is charged one): a /// finalize of exact accumulators measures 2.5 µs /// per group (median over 135 nodes of 1,000 groups each, 1.8–15 µs). A -/// per-series hash aggregate builds the same rows: e.g. a per-series max -/// over 300,000 rows of 1,000 series at 1 Hz measures 11.3 ms, of which -/// its rows (`hash_aggregate_operations_per_row`, 18 per row) and its -/// 30,000 group visits (`OPERATIONS_PER_GROUP_PER_RAW_BATCH`) are charged -/// 9.6, leaving about 1.7 µs per group. +/// per-series hash aggregate builds the same rows; its per-row and visit +/// constants are fitted with this charge in place +/// (`OPERATIONS_PER_GROUP_PER_RAW_BATCH`). pub(crate) const OUTPUT_ROW_OPERATIONS_PER_GROUP: f64 = 2_500.0; /// Per sketch state a tumbling-window chain completes and emits at the end @@ -312,10 +401,15 @@ const EXACT_CHAIN_EMIT_OPERATIONS_PER_STATE: f64 = 1_500.0; const KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 19_000.0; /// The operations one input row costs folded into a state of `summary_type` -/// with `parameters`. +/// with `parameters`, which folds `rows_per_state` rows (`None`: unknown) +/// of a column taking `distinct_values` values (`None`: every row's +/// differs). Only a KMV's depends on them +/// ([`kmv_compared_hashes_per_row`]). pub(crate) fn update_operations_per_row( summary_type: &FieldDataType, parameters: Option<&SketchParams>, + rows_per_state: Option, + distinct_values: Option, ) -> f64 { match (summary_type, parameters) { (FieldDataType::ExactAggregate(kind, _), _) if kind.tracks_counter_increase() => { @@ -324,6 +418,11 @@ pub(crate) fn update_operations_per_row( (FieldDataType::ExactAggregate(..), _) => EXACT_UPDATE_OPERATIONS_PER_ROW, (_, Some(SketchParams::DDSketch { .. })) => DDSKETCH_UPDATE_OPERATIONS_PER_ROW, (_, Some(SketchParams::Kll { .. })) => KLL_UPDATE_OPERATIONS_PER_ROW, + (_, Some(SketchParams::Kmv { k })) => { + KMV_UPDATE_OPERATIONS_PER_ROW + + KMV_OPERATIONS_PER_COMPARED_HASH + * kmv_compared_hashes_per_row(*k, rows_per_state, distinct_values) + } ( _, Some( @@ -333,8 +432,7 @@ pub(crate) fn update_operations_per_row( | SketchParams::CmsWithHeap { .. } | SketchParams::CountSketchWithHeap { .. } | SketchParams::UnivMon { .. } - | SketchParams::Hll { .. } - | SketchParams::Kmv { .. }), + | SketchParams::Hll { .. }), ), ) => { EXACT_UPDATE_OPERATIONS_PER_ROW @@ -345,6 +443,46 @@ pub(crate) fn update_operations_per_row( } } +/// The retained hashes one KMV update at `k` compares the row's hash with, +/// averaged over a state's `rows_per_state` rows (`None`: `k` rows), each a +/// draw from `distinct_values` equally likely values (`None`: every row's +/// differs). While the state retains fewer than `k` hashes, every update +/// walks them: a value seen before is found halfway, a new one walks all. +/// Once full, only a hash below the largest retained is walked: a retained +/// value seen before (`k / 2` of them on average) or a new value that +/// enters (all `k`), which after `j` distinct values is `k / j` of the new +/// ones. Averaged over 64 equal slices of the rows; within 1% of a +/// simulation of the same draws. Example: k = 4,323 over 300,000 rows of +/// 1,000 values never fills and compares 500 per row; of 100,000 values, +/// it fills after about 4,400 rows and compares 314. +pub(crate) fn kmv_compared_hashes_per_row( + k: u32, + rows_per_state: Option, + distinct_values: Option, +) -> f64 { + const SLICES: u32 = 64; + let k = f64::from(k); + let rows = rows_per_state.map_or(k, |rows| rows as f64); + let values = distinct_values.map_or(f64::INFINITY, |values| values.max(1) as f64); + let compared = (0..SLICES) + .map(|slice| { + let row = (f64::from(slice) + 0.5) * rows / f64::from(SLICES); + // Distinct values among the rows before it, and the share of + // its own value among them. + let seen = match values.is_finite() { + true => -values * (-row / values).exp_m1(), + false => row, + }; + let seen_before = seen / values; + match seen < k { + true => seen_before * seen / 2.0 + (1.0 - seen_before) * seen, + false => k * k / (2.0 * values) + (1.0 - seen_before) * k * k / seen, + } + }) + .sum::(); + compared / f64::from(SLICES) +} + /// The counters one input row updates in a sketch with `parameters`: one /// per row of depth for Count-Min and Count Sketch, plus one heap update for /// their top-k variants; for UnivMon, the same per layer it updates @@ -381,12 +519,23 @@ fn univmon_expected_layers(layers: u8) -> f64 { /// The operations one input state of `family` holding `state_bytes` costs /// merged into its group's state, which holds `merged_state_bytes` once -/// merged. Example: a KLL state of 872 bytes merged into one of 33,512 -/// costs 360 + 0.1 × 872 + 0.43 × 33,512 = 14,857. +/// merged; each input state folds `rows_per_input_state` rows (`None`: +/// unknown) and its group merges `input_states_per_group` of them. A KMV +/// merge updates the group's state with each hash the input retains (at +/// most `k`, its rows when fewer), as `asap_sketchlib` merges, so each hash +/// costs a row's update over all the hashes the group's merge inserts +/// ([`kmv_compared_hashes_per_row`], each a different value). Example: a +/// KLL state of 872 bytes merged into one of 33,512 costs 360 + 0.1 × 872 + +/// 0.43 × 33,512 = 14,857. A rollup of 1,000 KMV states at k = 106,263 of +/// 12,000 rows each into 100 groups (10 states per group) inserts 119,300 +/// hashes per group, 58,278 compared per hash: 348 s, measured 352 on the +/// executor. pub(crate) fn merge_operations_per_state( family: StateFamily<'_>, state_bytes: u64, merged_state_bytes: u64, + rows_per_input_state: Option, + input_states_per_group: u64, ) -> f64 { match family { StateFamily::Exact(kind) if kind.tracks_counter_increase() => { @@ -400,6 +549,18 @@ pub(crate) fn merge_operations_per_state( + KLL_MERGE_OPERATIONS_PER_INPUT_BYTE * state_bytes as f64 + KLL_MERGE_OPERATIONS_PER_MERGED_BYTE * merged_state_bytes as f64 } + StateFamily::Sketch(SketchParams::Kmv { k }) => { + let retained_hashes = + rows_per_input_state.map_or(u64::from(*k), |rows| rows.min(u64::from(*k))); + let hashes_per_group = retained_hashes.saturating_mul(input_states_per_group.max(1)); + retained_hashes as f64 + * update_operations_per_row( + &FieldDataType::Sketch(SketchAlgorithm::Kmv), + Some(&SketchParams::Kmv { k: *k }), + Some(hashes_per_group), + None, + ) + } StateFamily::Sketch( SketchParams::Hydra(_) | SketchParams::Cms { .. } @@ -407,8 +568,7 @@ pub(crate) fn merge_operations_per_state( | SketchParams::CmsWithHeap { .. } | SketchParams::CountSketchWithHeap { .. } | SketchParams::UnivMon { .. } - | SketchParams::Hll { .. } - | SketchParams::Kmv { .. }, + | SketchParams::Hll { .. }, ) => { EXACT_MERGE_OPERATIONS_PER_STATE + SKETCH_MERGE_OPERATIONS_PER_BYTE * state_bytes as f64 } @@ -441,6 +601,18 @@ pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u6 output_groups as f64 * per_group } +/// The operations a `SummaryMerge` that reads one state per group costs, +/// of any family: each state passes through to its group's slot unmerged +/// (the executor keeps a group's first state as it is), so the merge pays +/// each group's row (`MERGE_OPERATIONS_PER_OUTPUT_GROUP`) and no state's +/// merge. Merging the one window a time range selects from a chain of +/// 1,000 states per window measures 0.94–1.0 ms for KLL states (k = 547), +/// 0.97 for DDSketch states and 0.87–1.31 for exact accumulators, where a +/// KLL merge was charged 29 ms. Example: 1,000 groups cost 950,000. +pub(crate) fn pass_through_merge_operations(output_groups: u64) -> f64 { + output_groups as f64 * MERGE_OPERATIONS_PER_OUTPUT_GROUP +} + /// The operations per state a tumbling-window chain of `summary_type` /// costs to complete and emit each window's state: 1,500 for an exact /// accumulator, 2,500 for a sketch or any other summary. @@ -504,31 +676,55 @@ pub(crate) fn estimate_operations_per_group( } /// The operations one input row costs a hash aggregate with -/// `grouping_key_count` keys and `measures` over groups of `rows_per_group` -/// rows: each key hashed and looked up, each measure folded (at least one -/// operation for an aggregate without measures). Example: a per-series -/// `max_over_time` (one key, one measure) costs 15 + 3 = 18. +/// `grouping_key_count` keys and `measures` reading `rows` rows into +/// `group_count` groups, whose distinct counts and frequency statistics +/// read columns taking `distinct_values` values: the first key interned +/// and its group read, each further key interned and the tuple's group +/// found, each measure folded (at least one operation for an aggregate +/// without measures). Example: a per-series `max_over_time` (one key, one +/// measure) costs 16 + 4 = 20; `GROUP BY job, instance` with one measure +/// 28 more. pub(crate) fn hash_aggregate_operations_per_row( grouping_key_count: u64, measures: &[AggIntent], - rows_per_group: u64, + rows: u64, + group_count: u64, + distinct_values: u64, ) -> f64 { - grouping_key_count as f64 * HASH_AGGREGATE_OPERATIONS_PER_KEY - + measures - .iter() - .map(|measure| aggregate_measure_operations_per_row(measure, rows_per_group)) - .sum::() - .max(1.0) + let rows_per_group = rows.div_ceil(group_count.max(1)); + let keys = match grouping_key_count { + 0 => 0.0, + keys => { + HASH_AGGREGATE_OPERATIONS_PER_ROW + + (keys - 1) as f64 * HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY + } + }; + keys + measures + .iter() + .map(|measure| { + aggregate_measure_operations_per_row( + measure, + rows, + group_count, + rows_per_group, + distinct_values, + ) + }) + .sum::() + .max(1.0) } /// The operations one input row costs a hash aggregate's `measure` over -/// groups of `rows_per_group` rows, an estimate of its work: +/// `rows` rows in `group_count` groups of `rows_per_group`, an estimate of +/// its work: /// - an exact quantile keeps the row and sorts its group /// (`log2(rows_per_group)` levels); -/// - an exact distinct count, top-k or frequency statistic hashes the row's -/// value into its group's map of values, the work a grouping key does -/// (`HASH_AGGREGATE_OPERATIONS_PER_KEY`), besides folding it (not -/// measured); +/// - an exact distinct count or frequency statistic interns the row's value +/// in a table of every distinct value read and keeps its group's +/// (group, value) pairs ([`exact_distinct_value_operations_per_row`] of +/// the `distinct_values` the column takes); a top-k hashes the row's +/// value into its group's map, the work a further grouping key does +/// (`HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY`, not measured); /// - a counter's rate or increase, or a regression over a series' samples /// (`deriv`, `predict_linear`), does about what the Rate accumulator does /// per row (`COUNTER_UPDATE_OPERATIONS_PER_ROW`): it reads each sample's @@ -537,17 +733,26 @@ pub(crate) fn hash_aggregate_operations_per_row( /// including `changes`, `delta`, `idelta` and `resets`, which compare /// neighbouring samples without tracking a counter's increase (not /// measured). -fn aggregate_measure_operations_per_row(measure: &AggIntent, rows_per_group: u64) -> f64 { +fn aggregate_measure_operations_per_row( + measure: &AggIntent, + rows: u64, + group_count: u64, + rows_per_group: u64, + distinct_values: u64, +) -> f64 { match measure { AggIntent::Quantile { .. } => { AGGREGATE_MEASURE_OPERATIONS_PER_ROW + (rows_per_group.max(2) as f64).log2() * SORT_OPERATIONS_PER_ROW_PER_LEVEL } - AggIntent::TopK { .. } - | AggIntent::Cardinality { .. } + AggIntent::Cardinality { .. } | AggIntent::FrequencyL2 { .. } | AggIntent::FrequencyEntropy { .. } => { - AGGREGATE_MEASURE_OPERATIONS_PER_ROW + HASH_AGGREGATE_OPERATIONS_PER_KEY + AGGREGATE_MEASURE_OPERATIONS_PER_ROW + + exact_distinct_value_operations_per_row(distinct_values, group_count, rows) + } + AggIntent::TopK { .. } => { + AGGREGATE_MEASURE_OPERATIONS_PER_ROW + HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY } AggIntent::Rate | AggIntent::IRate diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 66c978602..7221f064d 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -25,8 +25,8 @@ use crate::node::analytical::{ use crate::node::operations_per_unit::{ chain_emit_operations_per_state, estimate_operations_per_group, hash_aggregate_operations_per_row, merge_operations_per_state, merge_output_operations, - update_operations_per_row, StateLastWrittenBy, CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, - CHAIN_OPERATIONS_PER_ARRIVING_ROW, CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH, + pass_through_merge_operations, update_operations_per_row, StateLastWrittenBy, + CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL, CHAIN_OPERATIONS_PER_ARRIVING_ROW, OPERATIONS_PER_GROUP_PER_RAW_BATCH, OUTPUT_ROW_OPERATIONS_PER_GROUP, TIME_RANGE_OPERATIONS_PER_ROW, }; @@ -42,7 +42,7 @@ use crate::{ CalibrationFit, CostInputs, NodeCostSource, OperatorKind, COST_SOURCE, MEASURED_COST_SOURCE, MIXED_COST_SOURCE, }; -use asap_types::ir::operator::{GroupKeys, Reduction, Source}; +use asap_types::ir::operator::{AggIntent, GroupKeys, Reduction, Source}; use asap_types::ir::properties::ExecutionTiming; use asap_types::ir::scalar::resolve_column_ref; use asap_types::ir::schema::{FieldDataType, Schema}; @@ -225,6 +225,16 @@ pub struct ProducerOutput<'a> { /// windows of a 1 Hz series passes on 10 rows per state, and a merge /// over it outputs states of 3,600 rows. pub state_input_rows: Option, + /// The time, in ms, that the raw rows it outputs span: a scan's + /// lookback (one second of arriving rows at ingestion time, the + /// retained rows of raw data materialized at ingestion), narrowed by a + /// `TimeRange`, passed on by the operators that keep each row's window + /// (a `Filter` keeps the span of the rows it drops); `None` for any + /// other node, whose rows are not raw rows. A grouping counts the raw + /// batches its input arrives in from it. Example: `latency > 100` over + /// a 5 min scan spans 300,000 ms, 30 batches of 10 s, however few rows + /// it keeps. + pub raw_rows_span_ms: Option, } impl ProducerOutput<'_> { @@ -274,6 +284,9 @@ pub struct NodeResourceUsageAndCost<'a> { /// What each summary state it outputs folds /// ([`ProducerOutput::state_input_rows`]). pub state_input_rows: Option, + /// The time its raw output rows span + /// ([`ProducerOutput::raw_rows_span_ms`]). + pub raw_rows_span_ms: Option, /// Work of one execution, in ms on one core. pub work_ms_per_execution: f64, /// CPU operations per second. @@ -315,6 +328,7 @@ impl<'a> NodeResourceUsageAndCost<'a> { tumbling_windows: self.tumbling_windows, chain_tumbling_window_width_ms: self.chain_tumbling_window_width_ms, state_input_rows: self.state_input_rows, + raw_rows_span_ms: self.raw_rows_span_ms, } } } @@ -390,6 +404,9 @@ pub fn node_resource_usage_and_cost<'a>( // What each summary state it outputs folds, when it builds or merges // states (else passed on below). let mut built_state_input_rows: Option = None; + // The time its raw output rows span, when it reads or narrows raw rows + // (else passed on below). + let mut read_raw_rows_span_ms: Option = None; let ingestion = node.output_state.timing == ExecutionTiming::IngestionTime; // The outputs a node retains: its `retention_ms` over the time between // two outputs, at least one. A chain's outputs are its tumbling @@ -566,6 +583,49 @@ pub fn node_resource_usage_and_cost<'a>( (non_negative, range) }) }; + // The distinct values a source column passed on unchanged takes per + // evaluation, as every source declares them + // (`distinct_values_per_label`, summed over the sources); `None` for a + // computed column or when one does not. + let declared_distinct_values = |column: Option<&str>| -> Option { + let column = column?; + if sources.is_empty() { + return None; + } + sources.iter().try_fold(0_u64, |total, source| { + Some( + total.saturating_add( + *data + .sources + .get(*source)? + .distinct_values_per_label + .get(column)?, + ), + ) + }) + }; + // The distinct items one output row's state holds at most per + // evaluation (`max_distinct_per_evaluation`, summed over the sources). + let distinct_items_per_group = || -> Option { + if sources.is_empty() { + return None; + } + sources.iter().try_fold(0_u64, |total, source| { + Some(total.saturating_add(data.sources.get(*source)?.max_distinct_per_evaluation?)) + }) + }; + // The values the column a summary built here folds takes in one state: + // the fewer of its declared distinct values and the sources' bound per + // output row; `None` when neither is declared. + let folded_distinct_values = |update: &SummaryUpdate| -> Option { + let column = producers + .first() + .and_then(|producer| producer.unchanged_column(&update.weight)); + [declared_distinct_values(column), distinct_items_per_group()] + .into_iter() + .flatten() + .min() + }; // The bytes of each summary state its first input holds. let input_state_bytes = || { producers.first().map_or(0, |producer| { @@ -623,6 +683,10 @@ pub fn node_resource_usage_and_cost<'a>( (_, None) => rows_read(), }, }; + // The time its input's raw rows span, when its input is raw rows. + let input_raw_rows_span_ms = producers + .first() + .and_then(|producer| producer.raw_rows_span_ms); // The times a grouping into `group_count` groups visits a group's // state: once per group per batch of raw rows its input arrives in // (`group_raw_batch_visits`). @@ -630,7 +694,7 @@ pub fn node_resource_usage_and_cost<'a>( group_raw_batch_visits( input.rows, group_count, - rows_per_ms, + input_raw_rows_span_ms, speed.raw_batch_width_ms, ingestion, ) @@ -692,6 +756,18 @@ pub fn node_resource_usage_and_cost<'a>( true => edge(raw_retained_rows.ok_or(retained_without_a_bound(source))?)?, false => out, }; + // The time those rows span: one second at ingestion time, + // the retention of raw data materialized there, at query + // time the rows read at the sources' rate (its lookback, + // less when fewer rows are held). + read_raw_rows_span_ms = match (ingestion, raw_rows_materialized_at_ingestion) { + (_, true) => node.retention_ms, + (true, false) => Some(ONE_SECOND_MS), + (false, false) => rows_per_ms + .filter(|rows_per_ms| *rows_per_ms > 0.0) + .map(|rows_per_ms| (out.rows as f64 / rows_per_ms).round() as u64) + .or(lookback), + }; (out, estimate, format!("scan {} samples", out.rows)) } NonASAPOp::Aggregate { @@ -703,8 +779,53 @@ pub fn node_resource_usage_and_cost<'a>( let keys = grouping_key_count(reduction); let out = edge(group_count)?; let held = input.rows.div_ceil(group_count.max(1)); - let operations_per_row = hash_aggregate_operations_per_row(keys, measures, held) - + carried_label_operations_per_row(&node.output_schema, reduction); + // The values its distinct counts and frequency statistics + // read: the most any one's columns take together, as + // declared for source columns passed on unchanged, else + // every row's differs; at most each group's bound on + // distinct items, and the rows read. + let declared_together = |cols: &[usize]| -> Option { + let unchanged = &producers.first()?.unchanged_columns; + cols.iter().try_fold(1_u64, |product, &col| { + let name = input_schema?.fields.get(col)?.name.as_str(); + let values = + declared_distinct_values(unchanged.contains(name).then_some(name))?; + Some(product.saturating_mul(values)) + }) + }; + let distinct_values = measures + .iter() + .filter_map(|measure| { + if let AggIntent::Cardinality { cols } = measure { + Some(cols.clone()) + } else if let AggIntent::FrequencyL2 { col } + | AggIntent::FrequencyEntropy { col } = measure + { + Some(col.iter().copied().collect()) + } else { + None + } + }) + .map(|cols| { + (!cols.is_empty()) + .then(|| declared_together(&cols)) + .flatten() + .unwrap_or(input.rows) + }) + .max() + .into_iter() + .chain(distinct_items_per_group().map(|per| per.saturating_mul(group_count))) + .chain([input.rows]) + .min() + .unwrap_or(input.rows); + let operations_per_row = + hash_aggregate_operations_per_row( + keys, + measures, + input.rows, + group_count, + distinct_values, + ) + carried_label_operations_per_row(&node.output_schema, reduction); let group_visits = visits(group_count); let estimate = estimate_operator( PhysicalOperator::HashAggregate { @@ -798,6 +919,7 @@ pub fn node_resource_usage_and_cost<'a>( // second of them. let out = match raw_rows_materialized_at_ingestion { true => { + read_raw_rows_span_ms = node.retention_ms; raw_retained_rows = raw_rows_back(node.retention_ms); let source = sources.first().copied().unwrap_or("(none)"); edge(raw_retained_rows.ok_or(retained_without_a_bound(source))?)? @@ -863,6 +985,11 @@ pub fn node_resource_usage_and_cost<'a>( rows: out.rows, bytes: out.bytes.min(input.bytes), }; + // The rows it keeps span its range, at most its input's. + let range_ms = u64::try_from(range.as_millis()).unwrap_or(u64::MAX); + read_raw_rows_span_ms = Some( + input_raw_rows_span_ms.map_or(range_ms, |input_ms| input_ms.min(range_ms)), + ); let estimate = estimate_operator( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { edges: unary(out) }, @@ -925,8 +1052,12 @@ pub fn node_resource_usage_and_cost<'a>( node.parameters.as_ref(), input_rows, )?); - let update_ops = update_operations_per_row(family, node.parameters.as_ref()) - + carried_label_operations_per_row(&node.output_schema, reduction); + let update_ops = update_operations_per_row( + family, + node.parameters.as_ref(), + input_rows.rows_per_state, + folded_distinct_values(update), + ) + carried_label_operations_per_row(&node.output_schema, reduction); let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -960,9 +1091,9 @@ pub fn node_resource_usage_and_cost<'a>( // One window's states: each input row updates the window its // time falls in. At ingestion time the rows of one window // arrive at the rate of one second's `input.rows`; at query - // time one window holds its width of the sources' rows (its - // input holds the rows of every retained window: without a - // declared rate, their share of one). + // time one window holds its width's share of the time its + // input's raw rows span (without a span, its share of the + // retained windows). let group_count = groups(reduction)?; let rows_per_tumbling_window = match ingestion { true => u64::try_from( @@ -970,12 +1101,13 @@ pub fn node_resource_usage_and_cost<'a>( .div_ceil(u128::from(ONE_SECOND_MS)), ) .map_err(|_| AnalyticalCostError::Overflow)?, - false => { - rows_per_ms.map_or(input.rows.div_ceil(retained_output_count), |rows_per_ms| { - ((rows_per_ms * *tumbling_window_width_ms as f64).round() as u64) + false => input_raw_rows_span_ms + .filter(|span_ms| *span_ms > 0) + .map_or(input.rows.div_ceil(retained_output_count), |span_ms| { + ((input.rows as f64 * *tumbling_window_width_ms as f64 / span_ms as f64) + .round() as u64) .clamp(1, input.rows.max(1)) - }) - } + }), }; let (non_negative, value_magnitude_range) = folded_values(update); let input_rows = StateInputRows { @@ -989,8 +1121,12 @@ pub fn node_resource_usage_and_cost<'a>( node.parameters.as_ref(), input_rows, )?); - let update_ops = update_operations_per_row(family, node.parameters.as_ref()) - + carried_label_operations_per_row(&node.output_schema, reduction); + let update_ops = update_operations_per_row( + family, + node.parameters.as_ref(), + input_rows.rows_per_state, + folded_distinct_values(update), + ) + carried_label_operations_per_row(&node.output_schema, reduction); // The states it completes in one execution: at ingestion time // one second's share of each group's window (a 10 s window: a // tenth of a state per group); at query time every retained @@ -1004,13 +1140,13 @@ pub fn node_resource_usage_and_cost<'a>( // At query time each row of the scan's lookback is kept in its // window's range, the work a `TimeRange` over the same rows does // (11 per row); at ingestion time each arriving row is routed to - // its window's state (8 per row). + // its window's state (14 per row). let range_ops = input.rows as f64 * match ingestion { true => CHAIN_OPERATIONS_PER_ARRIVING_ROW, false => TIME_RANGE_OPERATIONS_PER_ROW, }; - // Each group's rows of each raw batch are split by window. + // Each group's state found once per raw batch. let group_visits = visits(group_count); let out = EdgeStatistics { rows: group_count, @@ -1023,9 +1159,7 @@ pub fn node_resource_usage_and_cost<'a>( Ok(ResourceEstimate::new( input.rows as f64 * update_ops + range_ops - + group_visits - * (OPERATIONS_PER_GROUP_PER_RAW_BATCH - + CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH) + + group_visits * OPERATIONS_PER_GROUP_PER_RAW_BATCH + emitted_states * chain_emit_operations_per_state(family), out.bytes, 0, @@ -1082,19 +1216,29 @@ pub fn node_resource_usage_and_cost<'a>( _ => producers.iter().map(input_state_bytes).max().unwrap_or(0), }; // Each input state's merge, by the states' family and bytes, - // and each output group's state and row. - let merge_ops: f64 = producers - .iter() - .map(|producer| { - producer.output.rows as f64 - * merge_operations_per_state( - family, - input_state_bytes(producer), - merged_state_bytes, - ) - }) - .sum::() - + merge_output_operations(family, group_count); + // and each output group's state and row; with one state per + // group, each state passes through to its group unmerged. + let merge_ops: f64 = match states <= group_count { + true => pass_through_merge_operations(group_count), + false => { + producers + .iter() + .map(|producer| { + producer.output.rows as f64 + * merge_operations_per_state( + family, + input_state_bytes(producer), + merged_state_bytes, + producer + .state_input_rows + .and_then(|rows| rows.rows_per_state), + states.div_ceil(group_count.max(1)), + ) + }) + .sum::() + + merge_output_operations(family, group_count) + } + }; let out = EdgeStatistics { rows: group_count, bytes: (merged_state_bytes + state_row_bytes_beside_state(&node.output_schema)) @@ -1394,6 +1538,14 @@ pub fn node_resource_usage_and_cost<'a>( }) .flatten() }); + // A scan and a time range over raw rows say what their rows span; an + // operator keeping each row's window passes on its input's. + let raw_rows_span_ms = read_raw_rows_span_ms.or_else(|| { + node.payload + .keeps_input_window() + .then_some(input_raw_rows_span_ms) + .flatten() + }); Ok(NodeResourceUsageAndCost { output: out, sources, @@ -1402,6 +1554,7 @@ pub fn node_resource_usage_and_cost<'a>( tumbling_windows, chain_tumbling_window_width_ms, state_input_rows, + raw_rows_span_ms, work_ms_per_execution: node_ms, cpu_ops_per_second: estimate.cpu_ops() * per_second, scanned_bytes_per_second: estimate.scan_bytes() as f64 * per_second, @@ -1459,23 +1612,25 @@ fn selected_tumbling_windows( /// The times a grouping into `group_count` groups visits a group's state /// while reading `input_rows` rows: once per group per batch of raw rows /// (`raw_batch_width_ms` each) its input arrives in, at most once per row. -/// The batches are the input's rows over a batch's rows at the sources' -/// `rows_per_ms`: at query time at least one; at ingestion time a share of -/// one per execution, the rows of one second over a batch's. Unknown rate: -/// one batch at query time, a second's share of one at ingestion time. -/// Example: a 5 min window of 1,000 series at 1 Hz (300,000 rows) in 10 s -/// batches of 10,000 rows spans 30 batches, 30,000 visits; at 10 Hz the -/// same 30,000 visits for 3,000,000 rows. +/// The batches are the time its input's raw rows span +/// (`input_raw_rows_span_ms`) over a batch's: at query time at least one; +/// at ingestion time a share of one per execution, one second over a +/// batch's width. Input that is not raw rows: one batch at query time, a +/// second's share of one at ingestion time. Example: a 5 min window of +/// 1,000 series at 1 Hz (300,000 rows) in 10 s batches spans 30 batches, +/// 30,000 visits; at 10 Hz the same 30,000 visits for 3,000,000 rows, and +/// under a filter keeping a tenth of each series' rows the same 30,000 +/// for 30,000 rows. fn group_raw_batch_visits( input_rows: u64, group_count: u64, - rows_per_ms: Option, + input_raw_rows_span_ms: Option, raw_batch_width_ms: u64, ingestion: bool, ) -> f64 { let batch_width_ms = raw_batch_width_ms.max(1) as f64; - let batches = match rows_per_ms.filter(|rows_per_ms| *rows_per_ms > 0.0) { - Some(rows_per_ms) => input_rows as f64 / (rows_per_ms * batch_width_ms), + let batches = match input_raw_rows_span_ms { + Some(span_ms) => span_ms as f64 / batch_width_ms, None => match ingestion { true => ONE_SECOND_MS as f64 / batch_width_ms, false => 1.0, diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index a81e9682c..e8bf9c73e 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -60,6 +60,7 @@ fn a_merge_over_a_chain_s_windows_outputs_one_state_per_group() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: Some(60_000), + raw_rows_span_ms: None, state_input_rows: None, }; let windows = node(Payload::NonASAP(NonASAPOp::TimeRange { @@ -160,6 +161,7 @@ fn an_operator_keeping_its_input_window_over_a_chain_or_its_windows_selects_what unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: Some(60_000), + raw_rows_span_ms: None, state_input_rows: None, }; for &step in path { @@ -211,6 +213,7 @@ fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { unchanged_columns: BTreeSet::new(), tumbling_windows: 5, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { @@ -258,6 +261,7 @@ fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { unchanged_columns: BTreeSet::new(), tumbling_windows: 5, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { @@ -295,11 +299,13 @@ fn thousand_series_sampled_every(sample_interval_ms: u64) -> DataWorkload { .unwrap() } -/// `rows` raw rows of `metric`, as `producer` outputs them to a grouping. +/// `rows` raw rows of `metric` spanning `span_ms`, as `producer` outputs +/// them to a grouping. fn raw_rows<'a>( producer: &'a PhysicalASAPDAGNode, metric: &'static str, rows: u64, + span_ms: u64, ) -> ProducerOutput<'a> { ProducerOutput { payload: &producer.payload, @@ -313,6 +319,7 @@ fn raw_rows<'a>( unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: Some(span_ms), state_input_rows: None, } } @@ -336,13 +343,14 @@ fn per_series_carrying_job(state_type: Option) -> Schema { } /// A per-series Rate chain carrying `job` in 300 s windows at ingestion -/// time over 1,000 series of `requests_total`: measured 125.6 ns per -/// arriving row at 1 Hz and 85.7 at 10 Hz (a stored-only plan's ingestion -/// CPU over its rows), 1.47x. Its per-row work is the same at both rates; -/// what differs is how often it visits each series' state and splits its -/// rows by window, once per 10 s raw batch, which holds 10 rows of a series -/// at 1 Hz and 100 at 10 Hz. Charged a time range per row instead, the two -/// rates differed by 5%, and the 1 Hz chain was 1.2x under-charged. +/// time over 1,000 series of `requests_total`: measured 106.3 ns per +/// arriving row at 1 Hz and 87.6 at 10 Hz (a stored-only plan's ingestion +/// CPU over its rows), 1.21x; an earlier run measured 125.6 and 85.7, as 1 Hz +/// ingestion varies between runs. Its per-row work is the same at both +/// rates; what differs is how often it visits each series' state, once per +/// 10 s raw batch, which holds 10 rows of a series at 1 Hz and 100 at +/// 10 Hz. Charged a time range per row instead, the two rates differed by +/// 5%. #[test] fn an_ingestion_time_chain_costs_more_per_row_at_a_lower_sample_rate() { let settings = CostInputs::default(); @@ -366,7 +374,7 @@ fn an_ingestion_time_chain_costs_more_per_row_at_a_lower_sample_rate() { let rows_per_second = 1_000 * 1_000 / sample_interval_ms; let usage = node_resource_usage_and_cost( &chain, - &[raw_rows(&scan, "requests_total", rows_per_second)], + &[raw_rows(&scan, "requests_total", rows_per_second, 1_000)], (1, 1.0), None, &thousand_series_sampled_every(sample_interval_ms), @@ -376,15 +384,15 @@ fn an_ingestion_time_chain_costs_more_per_row_at_a_lower_sample_rate() { usage.cpu_ops_per_second / rows_per_second as f64 }; let (one_hz, ten_hz) = (ns_per_row(1_000), ns_per_row(100)); - for (charged, measured) in [(one_hz, 125.6), (ten_hz, 85.7)] { + for (charged, measured) in [(one_hz, 106.3), (ten_hz, 87.6)] { assert!( (0.8..=1.25).contains(&(charged / measured)), "charged {charged:.1} ns per row, measured {measured}" ); } assert!( - (1.3..=1.7).contains(&(one_hz / ten_hz)), - "1 Hz charged {one_hz:.1} ns per row, 10 Hz {ten_hz:.1}; measured 1.47x" + (1.1..=1.4).contains(&(one_hz / ten_hz)), + "1 Hz charged {one_hz:.1} ns per row, 10 Hz {ten_hz:.1}; measured 1.21x" ); } @@ -429,7 +437,7 @@ fn a_per_series_aggregate_costs_more_per_row_at_a_lower_sample_rate() { let rows = 300 * 1_000 * 1_000 / sample_interval_ms; let usage = node_resource_usage_and_cost( &aggregate, - &[raw_rows(&range, "latency", rows)], + &[raw_rows(&range, "latency", rows, 300_000)], (1, 1.0), None, &thousand_series_sampled_every(sample_interval_ms), @@ -450,7 +458,7 @@ fn a_per_series_aggregate_costs_more_per_row_at_a_lower_sample_rate() { /// A grouping visits each group's state once per raw batch of the /// deployment's `raw_batch_width_ms`: the per-series aggregate over 5 min /// of 1,000 series at 1 Hz visits each series 30 times in 10 s batches and -/// 300 times in 1 s batches (one row each), 270,000 visits × 140 +/// 300 times in 1 s batches (one row each), 270,000 visits × 170 /// operations more. #[test] fn a_grouping_visits_each_group_once_per_raw_batch_of_the_declared_width() { @@ -474,7 +482,7 @@ fn a_grouping_visits_each_group_once_per_raw_batch_of_the_declared_width() { settings.resource_speed.raw_batch_width_ms = raw_batch_width_ms; node_resource_usage_and_cost( &aggregate, - &[raw_rows(&range, "latency", 300_000)], + &[raw_rows(&range, "latency", 300_000, 300_000)], (1, 1.0), None, &thousand_series_sampled_every(1_000), @@ -518,7 +526,7 @@ fn a_query_time_rate_build_costs_more_per_row_at_a_lower_sample_rate() { let rows = 300 * 1_000 * 1_000 / sample_interval_ms; let usage = node_resource_usage_and_cost( &build, - &[raw_rows(&range, "requests_total", rows)], + &[raw_rows(&range, "requests_total", rows, 300_000)], (1, 1.0), None, &thousand_series_sampled_every(sample_interval_ms), @@ -544,14 +552,14 @@ fn a_query_time_rate_build_costs_more_per_row_at_a_lower_sample_rate() { /// window's range, as a `TimeRange` over the same rows does, besides /// updating the row's state and visiting its group once per raw batch. /// Example: a sum in 60 s windows over 36,000 raw rows of unknown rate (one -/// batch), retaining five windows of one group, costs 36,000 × 41 -/// operations (30 to update, 11 to keep in range), one group visit × 330 -/// (140 plus the chain's 190) and five emitted exact states × 1,500. +/// batch), retaining five windows of one group, costs 36,000 × 40 +/// operations (29 to update, 11 to keep in range), one group visit × 170 +/// and five emitted exact states × 1,500. #[test] fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { use crate::node::operations_per_unit::{ - CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH, EXACT_UPDATE_OPERATIONS_PER_ROW, - OPERATIONS_PER_GROUP_PER_RAW_BATCH, TIME_RANGE_OPERATIONS_PER_ROW, + EXACT_UPDATE_OPERATIONS_PER_ROW, OPERATIONS_PER_GROUP_PER_RAW_BATCH, + TIME_RANGE_OPERATIONS_PER_ROW, }; let settings = CostInputs::default(); let data = DataWorkload::default(); @@ -574,6 +582,7 @@ fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let chain = PhysicalASAPDAGNode { @@ -593,7 +602,6 @@ fn a_query_time_chain_pays_the_time_range_over_the_rows_it_reads() { usage.cpu_ops_per_second, 36_000.0 * (EXACT_UPDATE_OPERATIONS_PER_ROW + TIME_RANGE_OPERATIONS_PER_ROW) + OPERATIONS_PER_GROUP_PER_RAW_BATCH - + CHAIN_WINDOW_OPERATIONS_PER_GROUP_PER_RAW_BATCH + 5.0 * 1_500.0 ); } @@ -729,6 +737,7 @@ fn a_merge_reading_one_state_per_group_passes_it_through() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: Some(StateInputRows { rows_per_state: Some(100), non_negative: true, @@ -795,7 +804,7 @@ fn sql_aggregate_operations_per_row( child: 0, })) }; - let mut rows = raw_rows(&range, "t", 300_000); + let mut rows = raw_rows(&range, "t", 300_000, 300_000); rows.output_schema = &range.output_schema; rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); let names = measures.iter().map(|_| String::new()).collect(); @@ -900,7 +909,7 @@ fn a_kmv_build_costs_per_retained_hash_it_compares() { })) }; for (keys, k, measured) in [(vec![], 4_323, 191.0), (vec![2], 26_656, 7_410.0)] { - let mut rows = raw_rows(&range, "t", 300_000); + let mut rows = raw_rows(&range, "t", 300_000, 300_000); rows.output_schema = &range.output_schema; rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); let build = PhysicalASAPDAGNode { @@ -974,6 +983,7 @@ fn a_kmv_rollup_merge_costs_per_hash_it_inserts() { unchanged_columns: ["region", "service"].into(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: Some(rows), }; let rollup = PhysicalASAPDAGNode { @@ -1020,6 +1030,7 @@ fn query_time_memory_is_held_only_while_the_node_computes() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let merge = PhysicalASAPDAGNode { @@ -1151,6 +1162,7 @@ fn an_ingestion_time_view_of_raw_rows_keeps_the_rows_its_retention_covers() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let row_schema = Schema::new(vec![ @@ -1214,6 +1226,7 @@ fn a_view_without_retention_is_not_costed() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let shift = PhysicalASAPDAGNode { @@ -1287,6 +1300,7 @@ fn a_view_over_an_ingestion_time_chain_keeps_one_output_per_chain_window() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: Some(60_000), + raw_rows_span_ms: None, state_input_rows: None, }; let usage = node_resource_usage_and_cost( @@ -1342,6 +1356,7 @@ fn ddsketch_state_bytes( unchanged_columns, tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let settings = CostInputs::default(); @@ -1612,6 +1627,7 @@ fn kll_merge_cpu_ops_over(k: u32, window_ms: u64, range_s: u64) -> f64 { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let mut chain = node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { @@ -1729,6 +1745,7 @@ fn a_finalize_charges_each_group_its_measured_time() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let finalize = node(Payload::ASAP(ASAPOp::FinalizeExactAccumulator { child: 0 })); @@ -1775,6 +1792,7 @@ fn a_per_series_aggregate_charges_the_labels_it_carries() { unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: None, }; let ms_per_row = |labels: &[&str]| { @@ -1840,10 +1858,10 @@ fn a_kll_merge_of_few_states_charges_each_group_its_measured_time() { fn a_max_merge_charges_its_measured_time_per_state() { use crate::node::operations_per_unit::merge_operations_per_state; for kind in [ExactKind::Max, ExactKind::Min] { - let max = merge_operations_per_state(StateFamily::Exact(&kind), 56, 56); + let max = merge_operations_per_state(StateFamily::Exact(&kind), 56, 56, None, 1); assert!((100.0..=150.0).contains(&max), "{kind:?}: {max} per state"); } - let sum = merge_operations_per_state(StateFamily::Exact(&ExactKind::Sum), 56, 56); + let sum = merge_operations_per_state(StateFamily::Exact(&ExactKind::Sum), 56, 56, None, 1); assert!((200.0..=260.0).contains(&sum), "Sum: {sum} per state"); } @@ -1888,6 +1906,7 @@ fn a_merge_over_several_inputs_sizes_its_state_for_all_of_them() { unchanged_columns: BTreeSet::new(), tumbling_windows: 360, chain_tumbling_window_width_ms: Some(10_000), + raw_rows_span_ms: None, state_input_rows: Some(rows), }; let wider = ProducerOutput { @@ -1897,6 +1916,7 @@ fn a_merge_over_several_inputs_sizes_its_state_for_all_of_them() { }, tumbling_windows: 60, chain_tumbling_window_width_ms: Some(60_000), + raw_rows_span_ms: None, state_input_rows: Some(StateInputRows { rows_per_state: Some(100), ..rows @@ -1956,6 +1976,7 @@ fn kll_estimate_us_per_group(producer: Payload, rows_per_state: u64, state_bytes unchanged_columns: BTreeSet::new(), tumbling_windows: 1, chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, state_input_rows: Some(StateInputRows { rows_per_state: Some(rows_per_state), non_negative: true, diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index b846903b3..d8ce14979 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -322,7 +322,7 @@ pub fn plan_cost( || { // A partial copy merges into a state of the same window. let state_bytes = state_bytes_per_row(output[&id], &nodes[&id].output_schema); - merge_operations_per_state(family, state_bytes, state_bytes) + merge_operations_per_state(family, state_bytes, state_bytes, None, 1) * speed.ms_per_cpu_op * formula_scale }, diff --git a/crates/integration-tests/tests/production_rollup_workloads.rs b/crates/integration-tests/tests/production_rollup_workloads.rs index 3e3d10125..5c19d5da5 100644 --- a/crates/integration-tests/tests/production_rollup_workloads.rs +++ b/crates/integration-tests/tests/production_rollup_workloads.rs @@ -12,6 +12,7 @@ use asap_planner::{run_planning, PlanSearchSettings, PlanningModels, PlanningRun use asap_types::accuracy::{AccuracyMetric, AccuracyScope, AccuracyTarget}; use asap_types::ir::schema::{DataType, Field, Schema}; use asap_types::ir::{ASAPOp, OperatorNode, QueryRoot}; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; use asap_types::workload::{ DataWorkload, Predictability, QueryRecurrence, QueryRequirements, QueryWorkloadEntry, RepeatedDemand, RepetitionInterval, TimestampMs, @@ -151,14 +152,49 @@ async fn production_three_sql_groupings_share_kll() { check(&sql(&queries, accuracy).await, 3, 2, "Kll"); } +/// `COUNT(DISTINCT user_id)` by (region, service) and by service: the two +/// exact distinct counts cost less than one KMV build by (region, service) +/// with a rollup to service, so the planner counts exactly. Both plans as +/// the planner sizes them, executed over 12,000,000 rows of this shape +/// (1,000 (region, service) groups, 1,000,000 distinct users), measure +/// 14.7 s per evaluation for the exact counts and 393 s for the KMV plan at +/// k = 106,263: its build compares each row with the hashes each state +/// retains (40.0 s, charged 36.5), and its rollup inserts each state's +/// hashes the same way (the rest). Charged a fixed 48 per row, the KMV +/// plan was selected. #[tokio::test] -async fn production_sql_distinct_region_service_rollup() { +async fn production_sql_distinct_region_service_counts_exactly() { let accuracy = AccuracyTarget::max_error(0.05) .failure_probability(0.05) .accuracy_metric(AccuracyMetric::Relative) .accuracy_scope(AccuracyScope::PerValue); let queries: Vec<_> = ["region, service", "service"].iter().map(|keys| format!("SELECT {keys}, COUNT(DISTINCT user_id) AS value FROM http WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY {keys}")).collect(); - check(&sql(&queries, accuracy).await, 2, 1, "Kmv"); + let run = sql(&queries, accuracy).await; + let (plan, selection) = run + .selection + .as_ref() + .expect("validated production plan within 30s"); + assert!( + selection + .accepted_accuracy_per_query + .iter() + .all(|accepted| accepted.sketches.is_empty()), + "{:?}", + selection.accepted_accuracy_per_query + ); + let builds = plan + .nodes + .iter() + .filter(|node| { + matches!( + node.payload, + PhysicalASAPOperatorPayload::ASAP( + ASAPOp::SummaryAgg { .. } | ASAPOp::SummaryTumblingWindowChain { .. } + ) + ) + }) + .count(); + assert_eq!(builds, 0, "the selected plan builds no summary"); } // Exercise multi-input operators through the production route under more than diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index aa4696abb..f86e4e8d8 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -10,7 +10,7 @@ use asap_types::{ }, workload::PlanningWorkload, }; -use std::{collections::HashSet, rc::Rc}; +use std::rc::Rc; fn fixture() -> (PlanningWorkload, Vec) { let workload: PlanningWorkload = serde_json::from_str(include_str!( @@ -41,14 +41,19 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { .collect() } -// The production entrypoint must find a sketch rollup cheaper than -// computing both distinct counts exactly from raw data at query time. +/// The viewer's distinct-count rollup workload, `COUNT(DISTINCT user_id)` +/// by service and by (service, endpoint) over 5 min every 60 s: the +/// production entrypoint counts both exactly, the measured-better plan. A +/// KMV build compares each row with the hashes its state retains, and a +/// rollup inserts each state's hashes the same way: over 12,000,000 rows +/// of this data's shape (1,000,000 distinct users, 1,000 groups of the +/// finer grouping), the two exact counts measure 14.7 s per evaluation and +/// one KMV build with a rollup 393 s +/// (`production_sql_distinct_region_service_counts_exactly`). Charged a +/// fixed 48 per row and per byte merged, the KMV rollup was selected. #[test] -fn production_distinct_rollup_finds_shared_kmv() { +fn production_distinct_rollup_counts_exactly() { let (workload, roots) = fixture(); - let (exact_plan, _) = - asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) - .unwrap(); let entries: Vec<_> = workload.query_workload.entries().collect(); let data = workload.data_workload.as_ref().unwrap(); let mut settings = CostInputs::default(); @@ -69,33 +74,19 @@ fn production_distinct_rollup_finds_shared_kmv() { .expect("validated plan within budget"); let accepted = &selection.accepted_accuracy_per_query; assert_eq!(accepted.len(), 2); - assert!(accepted - .iter() - .all(|accuracy| accuracy.sketches.iter().any(|sketch| sketch == "Kmv"))); - let cost_per_second = - asap_integration_tests::planning::plan_cost_per_second(plan, &entries, data, &settings); - let exact_cost_per_second = asap_integration_tests::planning::plan_cost_per_second( - &exact_plan, - &entries, - data, - &settings, - ); assert!( - cost_per_second < exact_cost_per_second, - "KMV rollup {cost_per_second} vs exact {exact_cost_per_second}" + accepted.iter().all(|accuracy| accuracy.sketches.is_empty()), + "{accepted:?}" ); let roots = plan.query_roots().unwrap(); - let builds: HashSet<_> = nodes(&roots) + let builds = nodes(&roots) .iter() - .filter(|node| matches!(node.asap(), Some(ASAPOp::SummaryAgg { .. }))) - .map(Rc::as_ptr) - .collect(); - assert_eq!(builds.len(), 1, "both queries reuse a single state build"); - let rollup = nodes(&roots) - .iter() - .any(|n| matches!(n.asap(), Some(ASAPOp::SummaryMerge { .. }))); - assert!( - rollup, - "production search must select the cheaper legal rollup" - ); + .filter(|node| { + matches!( + node.asap(), + Some(ASAPOp::SummaryAgg { .. } | ASAPOp::SummaryTumblingWindowChain { .. }) + ) + }) + .count(); + assert_eq!(builds, 0, "the selected plan builds no summary"); } diff --git a/crates/stage3-plan-selection/src/cost.rs b/crates/stage3-plan-selection/src/cost.rs index e94869807..a87cd798c 100644 --- a/crates/stage3-plan-selection/src/cost.rs +++ b/crates/stage3-plan-selection/src/cost.rs @@ -640,14 +640,14 @@ fn reads_one_batch_of( } /// An input's logical output over its readable costed candidates' `usages`: the most -/// rows and bytes any outputs, the state family and the rows each state -/// folds of the one with the most bytes, the sources any reads and the -/// columns every one leaves unchanged; `payload` and `schema` the group's -/// own. `None` without a costed candidate. Example: of two KLL candidates, -/// one at k = 547 whose states fold 10 rows and one at k = 1,404 whose -/// states fold 3,600, the larger gives both its family and its rows, so a -/// merge over the input never sizes one candidate's `k` for the other's -/// rows. +/// rows and bytes any outputs and the longest time their raw rows span, the +/// state family and the rows each state folds of the one with the most +/// bytes, the sources any reads and the columns every one leaves unchanged; +/// `payload` and `schema` the group's own. `None` without a costed +/// candidate. Example: of two KLL candidates, one at k = 547 whose states +/// fold 10 rows and one at k = 1,404 whose states fold 3,600, the larger +/// gives both its family and its rows, so a merge over the input never +/// sizes one candidate's `k` for the other's rows. fn logical_output<'a: 'b, 'b>( payload: &'a PhysicalASAPOperatorPayload, schema: &'a asap_types::ir::schema::Schema, @@ -663,6 +663,7 @@ fn logical_output<'a: 'b, 'b>( known.family = usage.family; known.state_input_rows = usage.state_input_rows; } + known.raw_rows_span_ms = known.raw_rows_span_ms.max(usage.raw_rows_span_ms); known.output.rows = known.output.rows.max(usage.output.rows); known.output.bytes = known.output.bytes.max(usage.output.bytes); known.sources.extend(usage.sources.iter().copied()); From 6ca12f8c6b7f956b30e45a5b2a1bba9c7d66b63b Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:05:28 +0000 Subject: [PATCH 12/15] docs(cost): raw batches from the time the input spans, the data store's batch width, one-state merges, keys, exact distinct counts and KMV in the design doc and library API --- .../analytical-resource-cost.md | 31 +++++++++++++------ docs/develop_docs/library-api.md | 2 +- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md index 49edb3163..0942604fb 100644 --- a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md +++ b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md @@ -792,7 +792,8 @@ Their CPU and memory use the concrete summary state size and number of input states. A merge costs its family's operations per input state (per byte for unmeasured sketches; for KLL also per byte of the input state and of the merged state, which grows towards `k`), plus each output group's state and row, and outputs -one state per group: the input's groups, or for a rollup (`group_by`) the +one state per group. A merge that reads one state per group passes each +state through to its group unmerged and costs only each group's row: the input's groups, or for a rollup (`group_by`) the groups of its keys, estimated like any `by (keys)` and at most the input's groups. The merged state is sized for the rows its group's input states fold together: 360 stored KLL windows of 10 rows each merge into a state sized for @@ -800,14 +801,26 @@ together: 360 stored KLL windows of 10 rows each merge into a state sized for plus reading its state (a KLL's per byte after a merge, which leaves its levels sorted; after updates, per unsorted item, at most `k`). A grouping (a hash aggregate, a summary build or a tumbling-window chain) also costs per -group per batch of raw rows its input arrives in, the deployment's raw -batch width (`ResourceSpeed::raw_batch_width_ms`, 10 s assumed when not -declared): the executor visits each group's state once per batch, so the -same grouping costs more per row at a lower sample rate (a 10 s batch holds -10 rows of a series at 1 Hz, 100 at 10 Hz). A chain also splits each group's -rows of a batch by window; at ingestion time it routes each arriving row to -its window's state, and at query time keeps each row of its scan's lookback -in its window's range at a time range's cost per row. A state's +group per batch of raw rows its input arrives in, the batch width of the +deployment's data store (`ResourceSpeed::raw_batch_width_ms`, 10 s assumed +when not declared): the executor visits each group's state once per batch, +so the same grouping costs more per row at a lower sample rate (a 10 s batch +holds 10 rows of a series at 1 Hz, 100 at 10 Hz). The batches are counted +from the time its input's raw rows span (a scan's lookback, narrowed by a +time range and kept by a filter), not from its rows, so a filter that drops +rows between the scan and the grouping drops no batch; a query-time chain +sizes its windows the same way. At ingestion time a chain routes each +arriving row to its window's state, and at query time keeps each row of its +scan's lookback in its window's range at a time range's cost per row. A hash +aggregate pays per row for its first key, more for each further key (the +executor finds a group by the tuple of key values in a hash map), and per +measure. An exact distinct count interns each value in a table of every +distinct value read, so its cost per row grows with the distinct values its +column takes (declared in `distinct_values_per_label`, else every row's +differs). A KMV build compares each row's hash with the hashes its state +retains until the state holds `k` of them, so its cost per row depends on +`k`, its rows per state and the distinct values; a KMV merge inserts each +hash an input state retains the same way. A state's row on any edge holds its labels, its window's time and its series identity beside the state; a DDSketch state is sized for the values it is expected to hold, spread like diff --git a/docs/develop_docs/library-api.md b/docs/develop_docs/library-api.md index c2ad60dda..6e18189ca 100644 --- a/docs/develop_docs/library-api.md +++ b/docs/develop_docs/library-api.md @@ -197,7 +197,7 @@ asap_stage3_plan_selection::select_plan( | `PlanningModels::new(evidence)` | Your `&dyn AccuracyEvidence` (measured guarantees; `NoMeasuredGuarantees` has none), with default cost inputs and deployment capabilities; `with_evidence` replaces it; `with_ignore_accuracy_guarantees(true)` gives every query no accuracy target | | `PlanningModels::builtin()` | No measured accuracy evidence. `with_cost_inputs(&settings)` sets the deployment's `CostInputs`; `with_deployment_capabilities(&capabilities)` sets its `DeploymentCapabilities` (field `deployment_capabilities`) | | `DeploymentCapabilities` (`asap_types::deployment_capabilities`, re-exported as `asap_planner::DeploymentCapabilities`) | What the executor supports: `supported_summaries` (`None`, the default, supports every summary the planner builds; otherwise `SupportedSummary { summary_type: SketchAlgorithm, readouts: Vec }` entries; Pass 1 prunes others with `PruneReason::UnsupportedByDeployment`), `raw_data_retained` (default `true`; when `false`, a candidate that scans raw data at query time is pruned by Stage 2, and stored data is refused with `asap_planner::SelectionError::InvalidDeploymentCapabilities`) and `query_time_results_retained` (default `false`; when `true`, Stage 2 offers query-time `AcrossEvaluations` for tumbling-window chains) | -| `CostInputs` (`asap_cost`) | `cost_model`, `resource_speed` (`ms_per_cpu_op`, `ms_per_scanned_byte`, `raw_batch_width_ms`: the time one batch of raw rows covers, default 10,000, an assumed value), `non_repeating_query_horizon_seconds` (default 3,600) and `measured_operation_costs` (`MeasuredOperationCosts`, default empty: `operators`, one `OperatorCost { operator, phase, sketch / exact / measure, ns_per_unit, ns_per_invocation, memory_bytes_per_state }` per measured operator kind, phase and family; `ns_per_evaluation` per query request; `provenance` (executor version, machine). A node with an entry is priced from it, a sketch between the two nearest measured sizes; every other node from the formulas times one calibration factor fitted from the measured nodes; each node's `NodeCost::cost_source` says which. The earlier JSON array of `MeasuredOperationCost { sketch, operation, cpu_ns_per_operation }` still loads) | +| `CostInputs` (`asap_cost`) | `cost_model`, `resource_speed` (`ms_per_cpu_op`, `ms_per_scanned_byte`, `raw_batch_width_ms`: the time one batch of raw rows covers in the data store, default 10,000, an assumed value), `non_repeating_query_horizon_seconds` (default 3,600) and `measured_operation_costs` (`MeasuredOperationCosts`, default empty: `operators`, one `OperatorCost { operator, phase, sketch / exact / measure, ns_per_unit, ns_per_invocation, memory_bytes_per_state }` per measured operator kind, phase and family; `ns_per_evaluation` per query request; `provenance` (executor version, machine). A node with an entry is priced from it, a sketch between the two nearest measured sizes; every other node from the formulas times one calibration factor fitted from the measured nodes; each node's `NodeCost::cost_source` says which. The earlier JSON array of `MeasuredOperationCost { sketch, operation, cpu_ns_per_operation }` still loads) | | `CostModel::UsageOverTime(ResourceUnitCost)` | Cost model 1 (default, `ResourceUnitCost::illustrative()`): unit cost × usage per second; the cheapest valid candidate wins | | `CostModel::ProvisionedCapacity(ResourceUnitCost)` | Cost model 2: unit cost × peak memory, peak CPU cores and disk; the cheapest valid candidate wins | | `CostModel::ClusterCapacity(ClusterLimits)` | Cost model 3: memory, disk and optional CPU cores are hard limits; `ClusterObjective::{LowestLatency, LeastMemory}` chooses among the candidates that fit | From dcaabcef7fc2c22a859311e6e22670883f71854c Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:51:13 +0000 Subject: [PATCH 13/15] test(cost): a rollup merges its states when its group estimate ties its input's; a grouped exact distinct count pays its key and visits once; a sort, limit, dedup and concat pass on their raw rows' span (failing) --- crates/cost/src/node/usage/tests.rs | 191 +++++++++++++++++++++++++++- 1 file changed, 189 insertions(+), 2 deletions(-) diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index e8bf9c73e..7ec108d6d 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -660,6 +660,87 @@ fn a_grouping_counts_raw_batches_from_the_time_its_input_spans() { ); } +/// A sort, a limit and a dedup pass raw rows on as they are, so they pass +/// on the time those rows span, as a `Filter` does; a concat of raw rows +/// spans the longest of its inputs'. Example: a 5 min scan of 1,000 series +/// at 1 Hz sorted spans 300,000 ms, 30 batches of 10 s, so a per-series +/// grouping over it visits each group 30 times: 30,000 visits, where +/// without a span it counted one batch, 1,000. +#[test] +fn a_sort_limit_dedup_and_concat_pass_on_the_time_their_raw_rows_span() { + use asap_types::ir::operator::GroupKeys; + use asap_types::ir::scalar::{ScalarExpr, ScalarValue, SortKey}; + let settings = CostInputs::default(); + let data = thousand_series_sampled_every(1_000); + let scan = node(metric_scan("latency")); + let scan_usage = + node_resource_usage_and_cost(&scan, &[], (1, 1.0), Some(300_000), &data, &settings) + .unwrap(); + let short_scan_usage = + node_resource_usage_and_cost(&scan, &[], (1, 1.0), Some(60_000), &data, &settings).unwrap(); + let span_of = |payload: Payload, producers: Vec>| { + node_resource_usage_and_cost(&node(payload), &producers, (1, 1.0), None, &data, &settings) + .unwrap() + .raw_rows_span_ms + }; + let raw = || produced(&scan, &scan_usage); + for payload in [ + Payload::NonASAP(NonASAPOp::Sort { + keys: vec![SortKey { + expr: ScalarExpr::Literal(ScalarValue::Float64(1.0)), + ascending: true, + nulls_first: false, + }], + partition_by: GroupKeys::default(), + child: 0, + }), + Payload::NonASAP(NonASAPOp::Limit { + n: Some(10), + offset: 0, + partition_by: GroupKeys::default(), + child: 0, + }), + Payload::NonASAP(NonASAPOp::Dedup { + cols: vec![], + child: 0, + }), + ] { + assert_eq!( + span_of(payload.clone(), vec![raw()]), + Some(300_000), + "{payload:?}" + ); + } + let concat = || { + Payload::NonASAP(NonASAPOp::Concat { + children: vec![0, 1], + }) + }; + assert_eq!( + span_of(concat(), vec![raw(), produced(&scan, &short_scan_usage)]), + Some(300_000) + ); + // A concat with an input that is not raw rows outputs no raw rows. + let aggregate = node(Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::PerEntity, + measures: vec![asap_types::ir::operator::AggIntent::Max { col: None }], + output_names: vec![String::new()], + filters: vec![], + having: None, + child: 0, + })); + let aggregate_usage = + node_resource_usage_and_cost(&aggregate, &[raw()], (1, 1.0), None, &data, &settings) + .unwrap(); + assert_eq!( + span_of( + concat(), + vec![raw(), produced(&aggregate, &aggregate_usage)] + ), + None + ); +} + /// A query-time chain of 60 s windows over the same filtered scan (30,000 /// rows over 5 min): each window holds the rows of its 60 s, 6,000, so /// each of its 1,000 per-series states folds 6 rows. Sized by the sources' @@ -870,11 +951,12 @@ fn a_hash_aggregate_pays_each_further_key_and_measure_as_measured() { /// in a table of every distinct value read, then keeps each group's /// (group, value) pairs: its time per row grows with the distinct values. /// Measured on the executor with one group: 115 ns per row over 10,000 -/// distinct values and 589 over 259,000; charged 18 at both. +/// distinct users and 589 over 1,000,000 (259,000 of them among the rows); +/// charged 18 at both. #[test] fn an_exact_distinct_count_costs_more_per_row_with_more_distinct_values() { use asap_types::ir::operator::AggIntent; - for (user_ids, measured) in [(10_000, 115.0), (259_000, 589.0)] { + for (user_ids, measured) in [(10_000, 115.0), (1_000_000, 589.0)] { let charged = sql_aggregate_operations_per_row( vec![], vec![AggIntent::Cardinality { cols: vec![4] }], @@ -887,6 +969,28 @@ fn an_exact_distinct_count_costs_more_per_row_with_more_distinct_values() { } } +/// A grouped exact `COUNT(DISTINCT user_id)` pays its grouping key and its +/// group visits once: its own terms were fitted with them fixed at their +/// constants. Measured on the executor over 300,000 rows by `job` (10 +/// groups): 60.2 ns per row of 100 distinct users and 81.3 of 1,000. With +/// the key and the visits also inside the count's fitted constant, they +/// were charged 83 and 97, 1.38x and 1.19x. +#[test] +fn a_grouped_exact_distinct_count_pays_its_key_and_visits_once() { + use asap_types::ir::operator::AggIntent; + for (user_ids, measured) in [(100, 60.2), (1_000, 81.3)] { + let charged = sql_aggregate_operations_per_row( + vec![1], + vec![AggIntent::Cardinality { cols: vec![4] }], + user_ids, + ); + assert!( + (0.8..=1.25).contains(&(charged / measured)), + "by job of {user_ids} users: charged {charged:.1} ns per row, measured {measured}" + ); + } +} + /// A KMV state compares each row's hash with the hashes it retains until /// it holds `k` of them, then only hashes below its largest /// (`asap_sketchlib`'s duplicate check). Over 300,000 rows of 100,000 @@ -1003,6 +1107,89 @@ fn a_kmv_rollup_merge_costs_per_hash_it_inserts() { ); } +/// A rollup merges every state its keys put in one group, even when the +/// estimates give it as many groups as states: with no declared label +/// values, KMV states by (region, service) and their rollup by `service` +/// both estimate 1,000 groups, yet each service's states merge into one. +/// Only a merge that keeps its input's groups (no `group_by`, or the +/// input's own keys) passes one state per group through. +#[test] +fn a_rollup_merges_its_states_when_its_group_estimate_ties_its_input() { + use asap_types::ir::schema::DataType; + use asap_types::summary_type::SketchAlgorithm; + let settings = CostInputs::default(); + let data: DataWorkload = serde_json::from_value(serde_json::json!({ + "arrival": {"ingesting": {"rows_per_second": 2_000_000.0}}, + "sources": {"http": {}} + })) + .unwrap(); + let parameters = SketchParams::Kmv { k: 26_656 }; + let rows = StateInputRows { + rows_per_state: Some(12_000), + non_negative: false, + value_magnitude_range: None, + }; + let state_bytes = crate::node::state_bytes::sketch_state_bytes(¶meters, rows); + let build = PhysicalASAPDAGNode { + parameters: Some(parameters.clone()), + output_schema: Schema::new(vec![ + Field::new("region", FieldDataType::Plain(DataType::Utf8), false), + Field::new("service", FieldDataType::Plain(DataType::Utf8), false), + Field::new("state", FieldDataType::Sketch(SketchAlgorithm::Kmv), false), + ]), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), + reduction: Reduction::by(vec![0, 1]), + filter: None, + })) + }; + let states = ProducerOutput { + payload: &build.payload, + output_schema: &build.output_schema, + output: EdgeStatistics { + rows: 1_000, + bytes: 1_000 * (state_bytes + 32), + }, + sources: BTreeSet::from(["http"]), + family: StateFamily::Sketch(¶meters), + unchanged_columns: ["region", "service"].into(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, + state_input_rows: Some(rows), + }; + let merge_ms = |group_by: Option>| { + let merge = PhysicalASAPDAGNode { + parameters: Some(parameters.clone()), + ..node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by, + })) + }; + let usage = node_resource_usage_and_cost( + &merge, + std::slice::from_ref(&states), + (1, 1.0), + None, + &data, + &settings, + ) + .unwrap(); + assert_eq!(usage.output.rows, 1_000, "{}", usage.cost.detail); + usage.work_ms_per_execution + }; + let kept_groups_ms = merge_ms(None); + assert_eq!(merge_ms(Some(vec![0, 1])), kept_groups_ms); + assert_eq!(merge_ms(Some(vec![1, 0])), kept_groups_ms); + let rollup_ms = merge_ms(Some(vec![1])); + assert!( + rollup_ms > 100.0 * kept_groups_ms, + "rollup charged {rollup_ms:.3} ms, a pass-through {kept_groups_ms:.3} ms" + ); +} + /// A query-time node holds its state only /// while it computes: bytes × its work per execution × its executions /// per second, with no query latency. A KLL state of 2 MB whose own From b138585be2fd750fa89bcfc729cbb107d018e117 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:55:01 +0000 Subject: [PATCH 14/15] fix(cost): a rollup merges its states when its group estimate ties its input's; exact distinct terms refit with the key and visits fixed; one merge row 1,100; raw rows' span through sort, limit, dedup and concat - A SummaryMerge passes one state per group through only when it keeps its input's groups (no group_by, or the input's own keys); a rollup whose estimate ties its input's is charged its merges. - The exact distinct count's own terms are refit with the grouping key, the measure, the group visits and the output row fixed at their constants (41 per row, 0.53 per square root of the distinct values, 72 per inserted entry), so a grouped count pays them once. - MERGE_OPERATIONS_PER_OUTPUT_GROUP 950 -> 1,100: lowers the error over every merge on the fitting grid and on the held-out settings; the 10,000-group range is stated in the docs. - A Sort, Limit and Dedup pass the raw rows' span on; a Concat of raw rows spans the longest of its inputs'. - Doc fixes: the validation wording, the KMV fit (28 builds, 63.8 + 0.51), and the distinct rollup test's justification at its own shape. --- crates/cost/src/node/operations_per_unit.rs | 100 +++++++++++------- crates/cost/src/node/usage/mod.rs | 60 +++++++++-- .../tests/rollup_controlled.rs | 15 +-- 3 files changed, 124 insertions(+), 51 deletions(-) diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index 1cf192c4f..191eca2ce 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -88,9 +88,12 @@ pub(crate) const TIME_RANGE_OPERATIONS_PER_ROW: f64 = 11.0; /// aggregates and summary builds (Max, Count, Sum and quantile aggregates; /// Max, Sum, Count, Rate, Increase, DDSketch and KLL builds) and 254 /// stored plans' ingestion CPU. The loss and the terms were chosen by -/// leave-one-setting-out error (each rate, series count, window and -/// interval left out in turn, its points predicted by a fit on the rest): -/// squared log error 0.147, absolute 0.150. A chain's term per group and +/// leave-one-setting-out error (each of the 35 settings, one series count, +/// rate, window and interval together, left out in turn, its points +/// predicted by a fit on the rest): squared log error 0.147, absolute +/// 0.150. Leaving out each rate, each series count or each window instead +/// gives 0.148, 0.172 and 0.148, the squared loss again at or below the +/// absolute and the terms the same. A chain's term per group and /// batch did not lower it (0.147 with it, fitted 15), so a chain pays only /// this one. Fitted 171, then rounded with the others: measured ÷ charged /// is 0.76–1.50 for per-series hash aggregates at 1 Hz and 0.87–1.26 at @@ -194,11 +197,11 @@ pub(crate) const CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL: f64 = 37.0; /// One row folded into a KMV state, beyond the retained hashes it compares /// the row's hash with (`KMV_OPERATIONS_PER_COMPARED_HASH`): hashing the /// value and the state's checks. Fitted with that term on the executor -/// over 22 builds: 300,000 rows of 100 to 10,000,000 distinct values, one +/// over 28 builds: 300,000 rows of 100 to 10,000,000 distinct values, one /// state at k = 4,323 and 4 to 1,000 states at k = 26,656 (minimizing the /// squared log error, chosen by leave-one-out error over the distinct -/// counts, 0.108 against 0.202 for the absolute): 65 per row and 0.50 per -/// compared hash, within 0.75–1.27x of each build. +/// counts, 0.107 against 0.200 for the absolute): 63.8 per row and 0.51 +/// per compared hash, charged 65 and 0.50. const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 65.0; /// Per retained hash a KMV update compares the row's hash with @@ -212,22 +215,34 @@ const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 65.0; const KMV_OPERATIONS_PER_COMPARED_HASH: f64 = 0.5; /// Per input row of an exact distinct count or frequency statistic, beyond -/// its lookups and inserts below: reading the row's value. Fitted on the -/// executor's exact `COUNT(DISTINCT user_id)` over 300,000 and 3,000,000 -/// rows of 100 to 100,000,000 distinct values, in 1 to 1,000 groups (45 -/// aggregates; squared log error, the form chosen among 26 by the error -/// leaving each distinct count and row count out in turn, 0.219; measured -/// ÷ charged 0.74–1.83): 61 per row, less the measure's own -/// `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`. -const EXACT_DISTINCT_OPERATIONS_PER_ROW: f64 = 57.0; +/// its lookups and inserts below, the measure's own +/// `AGGREGATE_MEASURE_OPERATIONS_PER_ROW`, its grouping keys +/// (`HASH_AGGREGATE_OPERATIONS_PER_ROW`, +/// `HASH_AGGREGATE_OPERATIONS_PER_FURTHER_KEY`) and its group visits +/// (`OPERATIONS_PER_GROUP_PER_RAW_BATCH`): reading the row's value. +/// Measured on the executor's exact `COUNT(DISTINCT user_id)` over 300,000 +/// and 3,000,000 rows of 100 to 100,000,000 distinct values, in 1 to 1,000 +/// groups (45 aggregates). Of 26 forms, the one with the lowest error +/// leaving each distinct count and row count out in turn is a constant, +/// a term per square root of the distinct values and one per inserted +/// entry (0.215 by squared log error, fitted 61 + 0.4845 × √(distinct +/// values) + 103.7 per entry with the key and visits inside the +/// constant). Refit with the key, the measure, the visits and each +/// group's output row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`) fixed at their +/// constants, so a grouped count pays them once: 41.2 per row, 0.529 per +/// square root and 71.7 per entry (leave-one-out error 0.233; measured ÷ +/// charged 0.74–2.40, 0.74–1.29 for grouped counts over at most 1,000 +/// distinct values, which were 0.59–1.05). +const EXACT_DISTINCT_OPERATIONS_PER_ROW: f64 = 41.0; /// Per input row of an exact distinct count, per square root of the /// distinct values its value table holds (at most /// `EXACT_DISTINCT_LOOKUP_MOST_VALUES`): looking the value up, a cache -/// miss more often as the table grows (fitted 0.493). Example: 300,000 rows -/// of 10,000 distinct values measure 115 ns per row, charged 57 + 4 + 49 + -/// 7 = 117; of 259,000, 589, charged 57 + 4 + 249 + 150 = 460. -const EXACT_DISTINCT_LOOKUP_OPERATIONS_PER_SQRT_VALUE: f64 = 0.49; +/// miss more often as the table grows (fitted 0.529). Example: 300,000 +/// rows of 10,000 distinct values in one group measure 115 ns per row, +/// charged 4 + 41 + 53 + 5 = 103; of 1,000,000 (charged as 300,000, the +/// rows read), 589, charged 4 + 41 + 290 + 118 = 453. +const EXACT_DISTINCT_LOOKUP_OPERATIONS_PER_SQRT_VALUE: f64 = 0.53; /// The most distinct values measured: past 3,000,000 the lookup is /// charged as at 3,000,000 (943 ns per row at 2,955,000 values against @@ -236,18 +251,18 @@ const EXACT_DISTINCT_LOOKUP_MOST_VALUES: f64 = 3_000_000.0; /// Per entry an exact distinct count inserts, shared over its input rows: /// each distinct value into its value table and each (group, value) pair -/// into its set of pairs (fitted 103). -const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_ENTRY: f64 = 103.0; +/// into its set of pairs (fitted 71.7). +const EXACT_DISTINCT_OPERATIONS_PER_INSERTED_ENTRY: f64 = 72.0; /// The operations one input row costs an exact distinct count (or /// frequency statistic) over `rows` rows in `group_count` groups, of a -/// column taking `distinct_values` values, beyond the measure's own: the -/// executor interns each value in one table of every distinct value read -/// and keeps each group's (group, value) pairs in a set, so its time per -/// row grows with the distinct values. The pairs are each group's -/// expected distinct values among its rows, drawn from `distinct_values`. -/// Example: 300,000 rows of 10,000 values in one group cost 57 + 0.49 × -/// 100 + 103 × 20,000 ÷ 300,000 = 113. +/// column taking `distinct_values` values, beyond the measure's own, its +/// grouping keys and its group visits: the executor interns each value in +/// one table of every distinct value read and keeps each group's (group, +/// value) pairs in a set, so its time per row grows with the distinct +/// values. The pairs are each group's expected distinct values among its +/// rows, drawn from `distinct_values`. Example: 300,000 rows of 10,000 +/// values in one group cost 41 + 0.53 × 100 + 72 × 20,000 ÷ 300,000 = 99. pub(crate) fn exact_distinct_value_operations_per_row( distinct_values: u64, group_count: u64, @@ -319,12 +334,21 @@ pub(crate) const KLL_MERGE_OPERATIONS_PER_MERGED_BYTE: f64 = 0.43; /// creating the group's state and its output row. Merging exact /// accumulators over 1,000 groups costs 1.17 µs per group with one state /// per group and 215 ns per state plus about 0.95 µs per group with 5 to -/// 360 (median of 20–26 runs per point). Less than a finalize's or an -/// estimate's output row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`, 2,500): a -/// merge's row takes its group's key columns and merged state as they are, -/// where a finalize or an estimate also reads each state into a new value -/// column. -const MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 950.0; +/// 360 (median of 20–26 runs per point). The time per group grows with the +/// groups: one-state merges on the fitting grid of +/// `OPERATIONS_PER_GROUP_PER_RAW_BATCH` measure a median 0.98–1.15 µs per +/// group at 100 and 1,000 groups (1.34 and 1.15 µs for exact +/// accumulators) and 1.5–1.8 µs at 10,000 (up to 2.5 µs for KLL states). +/// 1,100 minimizes neither alone but lowers the RMS log error of measured +/// ÷ charged over every merge on that grid from 0.281 at 950 to 0.248 +/// (one state per group 0.375 → 0.273, several 0.214 → 0.233), and on the +/// held-out settings from 0.225 to 0.217. At 10,000 groups one-state +/// merges still measure a median 1.38–1.62x their charge (up to 2.29x for +/// KLL), at 1,000 groups 0.96x. Less than a finalize's or an estimate's +/// output row (`OUTPUT_ROW_OPERATIONS_PER_GROUP`, 2,500): a merge's row +/// takes its group's key columns and merged state as they are, where a +/// finalize or an estimate also reads each state into a new value column. +const MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 1_100.0; /// Per byte of any other sketch state merged, above /// `EXACT_MERGE_OPERATIONS_PER_STATE`: merging adds every counter of the @@ -577,8 +601,8 @@ pub(crate) fn merge_operations_per_state( /// The operations a `SummaryMerge` of `family` with `output_groups` groups /// costs beyond its input states' merges ([`merge_operations_per_state`]): -/// each group's state and output row. Example: 1,000 groups cost 950,000, -/// of KLL states 19,950,000. +/// each group's state and output row. Example: 1,000 groups cost +/// 1,100,000, of KLL states 20,100,000. pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u64) -> f64 { let per_group = match family { StateFamily::Sketch(SketchParams::Kll { .. }) => { @@ -608,7 +632,11 @@ pub(crate) fn merge_output_operations(family: StateFamily<'_>, output_groups: u6 /// merge. Merging the one window a time range selects from a chain of /// 1,000 states per window measures 0.94–1.0 ms for KLL states (k = 547), /// 0.97 for DDSketch states and 0.87–1.31 for exact accumulators, where a -/// KLL merge was charged 29 ms. Example: 1,000 groups cost 950,000. +/// KLL merge was charged 29 ms. The time per group grows with the +/// groups: at 10,000 groups such merges measure 1.5–1.8 µs per group, a +/// median 1.38–1.62x their charge (up to 2.29x for KLL states), at 100 and +/// 1,000 groups 0.96–1.20x (`MERGE_OPERATIONS_PER_OUTPUT_GROUP`). Example: +/// 1,000 groups cost 1,100,000. pub(crate) fn pass_through_merge_operations(output_groups: u64) -> f64 { output_groups as f64 * MERGE_OPERATIONS_PER_OUTPUT_GROUP } diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 7221f064d..4b803f379 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -45,7 +45,7 @@ use crate::{ use asap_types::ir::operator::{AggIntent, GroupKeys, Reduction, Source}; use asap_types::ir::properties::ExecutionTiming; use asap_types::ir::scalar::resolve_column_ref; -use asap_types::ir::schema::{FieldDataType, Schema}; +use asap_types::ir::schema::{ColumnId, FieldDataType, Schema}; use asap_types::ir::{ASAPOp, NonASAPOp, Operator, TimeRangeKind}; use asap_types::plan::physical_asap_dag::{ PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload, @@ -229,8 +229,9 @@ pub struct ProducerOutput<'a> { /// lookback (one second of arriving rows at ingestion time, the /// retained rows of raw data materialized at ingestion), narrowed by a /// `TimeRange`, passed on by the operators that keep each row's window - /// (a `Filter` keeps the span of the rows it drops); `None` for any - /// other node, whose rows are not raw rows. A grouping counts the raw + /// and by a `Sort`, `Limit` or `Dedup` (a `Filter` or a `Limit` keeps + /// the span of the rows it drops), the longest of a `Concat`'s raw + /// inputs'; `None` for any other node, whose rows are not raw rows. A grouping counts the raw /// batches its input arrives in from it. Example: `latency > 100` over /// a 5 min scan spans 300,000 ms, 30 batches of 10 s, however few rows /// it keeps. @@ -1216,9 +1217,18 @@ pub fn node_resource_usage_and_cost<'a>( _ => producers.iter().map(input_state_bytes).max().unwrap_or(0), }; // Each input state's merge, by the states' family and bytes, - // and each output group's state and row; with one state per - // group, each state passes through to its group unmerged. - let merge_ops: f64 = match states <= group_count { + // and each output group's state and row. A merge that keeps + // its input's groups (no `group_by`, or the input's own keys) + // and reads one state per group passes each state through + // unmerged. A rollup merges even when its estimate ties its + // input's: states by (region, service) rolled up by `service` + // merge several per service. + let keeps_input_groups = group_by.as_ref().is_none_or(|keys| { + producers + .iter() + .all(|producer| keys_are_input_keys(keys, producer.output_schema)) + }); + let merge_ops: f64 = match keeps_input_groups && states <= group_count { true => pass_through_merge_operations(group_count), false => { producers @@ -1539,10 +1549,27 @@ pub fn node_resource_usage_and_cost<'a>( .flatten() }); // A scan and a time range over raw rows say what their rows span; an - // operator keeping each row's window passes on its input's. + // operator keeping each row's window passes on its input's, and so do + // a sort, a limit and a dedup, which output raw rows as they are (a + // limit and a dedup drop some, as a filter does). A concat of raw rows + // spans the longest of its inputs'. + let passes_raw_rows_on = node.payload.keeps_input_window() + || matches!( + node.payload, + Payload::NonASAP( + NonASAPOp::Sort { .. } | NonASAPOp::Limit { .. } | NonASAPOp::Dedup { .. } + ) + ); let raw_rows_span_ms = read_raw_rows_span_ms.or_else(|| { - node.payload - .keeps_input_window() + if matches!(node.payload, Payload::NonASAP(NonASAPOp::Concat { .. })) { + return producers + .iter() + .map(|producer| producer.raw_rows_span_ms) + .collect::>>()? + .into_iter() + .max(); + } + passes_raw_rows_on .then_some(input_raw_rows_span_ms) .flatten() }); @@ -1671,6 +1698,21 @@ fn carried_label_operations_per_row(output_schema: &Schema, reduction: &Reductio /// The input states a `SummaryMerge` over `producers` reads: every row of /// each. +/// Whether a rollup's `keys` are its input's own keys: every plain +/// column of `input` but its time index, in any order. Example: over +/// states keyed by (window_end, region, service), `[2, 1]` is, `[2]` is +/// not. +fn keys_are_input_keys(keys: &[ColumnId], input: &Schema) -> bool { + let input_keys: BTreeSet = input + .fields + .iter() + .enumerate() + .filter(|(column, field)| field.is_plain() && input.time_index != Some(*column)) + .map(|(column, _)| column) + .collect(); + keys.iter().copied().collect::>() == input_keys +} + fn merged_states(producers: &[ProducerOutput<'_>]) -> Result { producers.iter().try_fold(0_u64, |total, producer| { total diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index f86e4e8d8..4458f05d1 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -45,12 +45,15 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { /// by service and by (service, endpoint) over 5 min every 60 s: the /// production entrypoint counts both exactly, the measured-better plan. A /// KMV build compares each row with the hashes its state retains, and a -/// rollup inserts each state's hashes the same way: over 12,000,000 rows -/// of this data's shape (1,000,000 distinct users, 1,000 groups of the -/// finer grouping), the two exact counts measure 14.7 s per evaluation and -/// one KMV build with a rollup 393 s -/// (`production_sql_distinct_region_service_counts_exactly`). Charged a -/// fixed 48 per row and per byte merged, the KMV rollup was selected. +/// rollup inserts each state's hashes the same way. The planner sees +/// 600,000,000 rows per evaluation and 10,000 groups for each grouping, +/// and sizes the KMV at k = 26,656. Measured on the executor at that +/// shape (k = 26,656, 1,000,000 distinct users, grouped), a KMV build +/// takes 7.8 µs per row and an exact count 0.59 µs, 13x apart. The +/// planner charges the KMV plan 3.9x the exact counts (0.00154 against +/// 0.00039 per second; 2.8x while the rollup by `service` was charged as +/// passing one state per group through). Charged a fixed 48 per row and +/// per byte merged, the KMV rollup was selected. #[test] fn production_distinct_rollup_counts_exactly() { let (workload, roots) = fixture(); From e089d2fbb89ccb3a66346bd9302851bc8fe1e56e Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:56:27 +0000 Subject: [PATCH 15/15] docs(cost): a rollup merges its states when its estimate ties its input's; the raw rows' span through sort, limit, dedup and concat; the merge paragraph rewrapped --- .../analytical-resource-cost.md | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md index 0942604fb..753bfd4c7 100644 --- a/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md +++ b/docs/design_docs/proposals/asap-aware-mapping/analytical-resource-cost.md @@ -789,13 +789,16 @@ from logical group count alone. Summary merge, subtract, delete, and readout are separate physical operators. Their CPU and memory use the concrete summary state size and number of input -states. A merge costs its family's operations per input state (per byte for unmeasured -sketches; for KLL also per byte of the input state and of the merged state, -which grows towards `k`), plus each output group's state and row, and outputs -one state per group. A merge that reads one state per group passes each -state through to its group unmerged and costs only each group's row: the input's groups, or for a rollup (`group_by`) the -groups of its keys, estimated like any `by (keys)` and at most the input's -groups. The merged state is sized for the rows its group's input states fold +states. A merge costs its family's operations per input state (per byte for +unmeasured sketches; for KLL also per byte of the input state and of the +merged state, which grows towards `k`), plus each output group's state and +row, and outputs one state per group: the input's groups, or for a rollup +(`group_by`) the groups of its keys, estimated like any `by (keys)` and at +most the input's groups. A merge that keeps its input's groups (no +`group_by`, or the input's own keys) and reads one state per group passes +each state through to its group unmerged and costs only each group's row. A +rollup merges its states even when its group estimate ties its input's. The +merged state is sized for the rows its group's input states fold together: 360 stored KLL windows of 10 rows each merge into a state sized for 3,600 rows. A readout (estimate or finalize) costs each group's output row, plus reading its state (a KLL's per byte after a merge, which leaves its @@ -807,7 +810,8 @@ when not declared): the executor visits each group's state once per batch, so the same grouping costs more per row at a lower sample rate (a 10 s batch holds 10 rows of a series at 1 Hz, 100 at 10 Hz). The batches are counted from the time its input's raw rows span (a scan's lookback, narrowed by a -time range and kept by a filter), not from its rows, so a filter that drops +time range and kept by a filter, a sort, a limit or a dedup; a concat's +longest), not from its rows, so a filter that drops rows between the scan and the grouping drops no batch; a query-time chain sizes its windows the same way. At ingestion time a chain routes each arriving row to its window's state, and at query time keeps each row of its @@ -820,8 +824,7 @@ column takes (declared in `distinct_values_per_label`, else every row's differs). A KMV build compares each row's hash with the hashes its state retains until the state holds `k` of them, so its cost per row depends on `k`, its rows per state and the distinct values; a KMV merge inserts each -hash an input state retains the same way. A state's -row on any edge holds +hash an input state retains the same way. A state's row on any edge holds its labels, its window's time and its series identity beside the state; a DDSketch state is sized for the values it is expected to hold, spread like latencies, not for the most it could hold; a column's declared