diff --git a/crates/cost/src/measured.rs b/crates/cost/src/measured.rs index c587526a6..3d2c368cd 100644 --- a/crates/cost/src/measured.rs +++ b/crates/cost/src/measured.rs @@ -22,6 +22,12 @@ //! covering every operator kind the plans use makes the choice the //! measured one (plan selection's module doc, "Measured operation costs"). //! +//! Without a table, the formulas are not unit-free guesses: they carry the +//! reference executor's measured time per unit of work +//! (`node::operations_per_unit`), the built-in fallback. A table measured +//! on the deployment's own machine (by a calibration run of its executor) +//! overrides them node by node. +//! //! A measured time enters [`ResourceUsage`] as CPU operations: `t` ns is //! `t · 10⁻⁶ / ms_per_cpu_op` operations of [`ResourceSpeed`], so latency //! (operations × `ms_per_cpu_op`) is the measured time, and every cost model diff --git a/crates/cost/src/model.rs b/crates/cost/src/model.rs index 355867a13..a68cb02dd 100644 --- a/crates/cost/src/model.rs +++ b/crates/cost/src/model.rs @@ -44,7 +44,8 @@ pub struct ResourceUnitCost { impl ResourceUnitCost { /// The prices plan selection uses until a deployment is calibrated: - /// 1 ns of CPU per operation, 0.1 ns per scanned byte, 1 GB retained for a + /// 1 ns of CPU per operation (one operation is one ns of the reference + /// executor's work, [`ResourceSpeed::ms_per_cpu_op`]), 0.1 ns per scanned byte, 1 GB retained for a /// second costs 1/8 vCPU (125 CPU-ms), held or provisioned alike; one /// provisioned core-second is 1,000 CPU-ms. Disk is not priced (0). pub fn illustrative() -> Self { @@ -117,7 +118,11 @@ impl ResourceUnitCost { #[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields)] pub struct ResourceSpeed { - /// Milliseconds one CPU operation takes on one core. + /// Milliseconds one CPU operation takes on one core. One CPU operation + /// is one nanosecond of the reference executor's work + /// (`node::operations_per_unit`), so the default is 1e-6; for another + /// machine set this to 1e-6 × its time ÷ the reference time (a core + /// half as fast: 2e-6). pub ms_per_cpu_op: f64, /// Milliseconds scanning one byte takes on one core. pub ms_per_scanned_byte: f64, diff --git a/crates/cost/src/node/analytical.rs b/crates/cost/src/node/analytical.rs index e476dea25..3be752eac 100644 --- a/crates/cost/src/node/analytical.rs +++ b/crates/cost/src/node/analytical.rs @@ -4,6 +4,7 @@ //! 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::resources::ModeledCpu; use asap_types::summary_type::{SketchAlgorithm, SketchParams}; use serde::{Deserialize, Serialize}; @@ -96,7 +97,11 @@ pub(crate) enum PhysicalOperator { }, HashAggregate { grouping_key_count: u64, - accumulator_count: u64, + /// CPU operations one input row costs, its grouping keys and + /// measures together + /// (`operations_per_unit::hash_aggregate_operations_per_row`): + /// 27 for a per-series `max_over_time` (one key, one measure). + operations_per_row: u64, }, InMemoryComparisonSort { ordering_key_count: u64, @@ -443,7 +448,7 @@ pub(crate) fn estimate_operator( ), (PhysicalOperator::TimeShift, _) => ResourceEstimate::new(0.0, 0, 0), (PhysicalOperator::TimeRange, _) => ResourceEstimate::new( - output.rows as f64, + output.rows as f64 * TIME_RANGE_OPERATIONS_PER_ROW, per_row_width(output.rows, output.bytes)?, 0, ), @@ -454,8 +459,7 @@ pub(crate) fn estimate_operator( ), ( PhysicalOperator::HashAggregate { - grouping_key_count, - accumulator_count, + operations_per_row, .. }, OperatorStatistics::HashAggregate { group_count, @@ -464,12 +468,7 @@ pub(crate) fn estimate_operator( .. }, ) => ResourceEstimate::new( - checked_cpu_product( - left.rows, - grouping_key_count - .checked_add(accumulator_count) - .ok_or(AnalyticalCostError::Overflow)?, - )?, + checked_cpu_product(left.rows, operations_per_row)?, checked_bytes(&[ *group_count, key_bytes @@ -842,7 +841,7 @@ pub(crate) fn validate_operator_semantics( ( PhysicalOperator::HashAggregate { grouping_key_count, - accumulator_count, + operations_per_row, }, OperatorStatistics::HashAggregate { group_count, @@ -856,7 +855,7 @@ pub(crate) fn validate_operator_semantics( "PromQL cross-series aggregation needs an explicit physical operator", ); } - if accumulator_count == 0 || *accumulator_bytes_per_group == 0 { + if operations_per_row == 0 || *accumulator_bytes_per_group == 0 { return inconsistent("HashAggregate accumulator work and width must be positive"); } if grouping_key_count == 0 { @@ -1578,8 +1577,8 @@ mod tests { } /// A time shift only moves when its input is evaluated: it costs - /// nothing. A time range is priced by the rows it keeps, and keeps no - /// more than it reads. + /// nothing. A time range is priced by the rows it keeps (12 measured + /// operations each), and keeps no more than it reads. #[test] fn time_shift_is_free_and_time_range_is_priced_by_the_rows_it_keeps() { let shift = estimate_operator( @@ -1598,7 +1597,7 @@ mod tests { }, ) .unwrap(); - assert_eq!(range.cpu_ops(), 200.0); + assert_eq!(range.cpu_ops(), 200.0 * TIME_RANGE_OPERATIONS_PER_ROW); assert!(estimate_operator( PhysicalOperator::TimeRange, OperatorStatistics::PassThrough { @@ -1630,7 +1629,7 @@ mod tests { fn aggregate_operator() -> PhysicalOperator { PhysicalOperator::HashAggregate { grouping_key_count: 1, - accumulator_count: 1, + operations_per_row: 1, } } @@ -1792,7 +1791,7 @@ mod tests { estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 0, - accumulator_count: 1, + operations_per_row: 1, }, statistics, ), @@ -2126,7 +2125,7 @@ mod tests { let ungrouped = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 0, - accumulator_count: 1, + operations_per_row: 1, }, OperatorStatistics::HashAggregate { edges: unary_edges(empty, EdgeStatistics { rows: 1, bytes: 8 }), @@ -2142,7 +2141,7 @@ mod tests { let grouped = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: 1, - accumulator_count: 1, + operations_per_row: 1, }, OperatorStatistics::HashAggregate { edges: unary_edges(empty, empty), diff --git a/crates/cost/src/node/mod.rs b/crates/cost/src/node/mod.rs index 1ab5a2605..97ad25baf 100644 --- a/crates/cost/src/node/mod.rs +++ b/crates/cost/src/node/mod.rs @@ -1,9 +1,11 @@ //! One node of a physical plan DAG: its resource usage and cost //! (`usage`), from the analytical per-operator resource model -//! ([`analytical`], `operator_statistics`) and the bytes a state holds +//! ([`analytical`], `operator_statistics`), the measured CPU operations per +//! unit of work (`operations_per_unit`) and the bytes a state holds //! ([`state_bytes`]). pub mod analytical; +pub(crate) mod operations_per_unit; pub(crate) mod operator_statistics; pub mod state_bytes; pub(crate) mod usage; diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs new file mode 100644 index 000000000..fbde9c04c --- /dev/null +++ b/crates/cost/src/node/operations_per_unit.rs @@ -0,0 +1,350 @@ +//! 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. +//! +//! 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 +//! unit, in ns, and [`ResourceSpeed::ms_per_cpu_op`](crate::ResourceSpeed) +//! (1e-6 by default) turns operations into milliseconds on the deployment's +//! machine. What plan selection needs is their ratios: a sketch built at +//! query time is cheaper than the exact plan only when its update is +//! cheaper than keeping and sorting the same rows. These are the built-in +//! fallback; a table measured on the deployment +//! (`CostInputs::measured_operation_costs`) replaces them node by node. +//! +//! Operators not measured here still charge an abstract 1 operation per +//! unit, unscaled: the source scan (1 per sample plus 0.1 per byte), a +//! filter or projection (1 per row per predicate or expression), sorts, +//! joins, deduplication, set operations and the other non-summary +//! operators (`analytical`), and every node `usage` prices by its input +//! rows alone. +//! +//! Measured on one core of an idle server with the executor at commit +//! 59365ce80, in a release build: each query-time node of a plan run as the +//! only root over warm in-memory data, its own time the sub-plan's time +//! minus its inputs', median of 5 runs. Data: 1,000 series of a +//! non-negative gauge (`ts`, `value`, the series identity) and of a counter +//! (with a `job` label), 1 h windows at 1 Hz (3,600 rows per series, +//! tumbling windows of 10 s) and at 10 Hz (36,000 rows per series, windows +//! of 60 s). Each constant's own measurement is in its doc. On a host +//! with most cores busy, the sketch constants measure about twice as high +//! (DDSketch update 325–403 ns per row, merge 3.4–3.9 µs per state) while +//! the row-streaming ones (time range, hash aggregate, exact accumulators) +//! barely move: memory and cache contention slows sketch kernels most, which +//! 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), +//! 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 +//! 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 +// the build (under `-D warnings`). +#![warn( + clippy::wildcard_enum_match_arm, + clippy::match_wildcard_for_single_variants +)] + +use asap_types::ir::operator::AggIntent; +use asap_types::ir::schema::FieldDataType; +use asap_types::summary_type::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; + +/// 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. +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; + +/// 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 +/// exact quantile costs 72 ns per row more than a per-series max at 3,600 +/// rows per series (11.8 levels, 6.1 ns per level) and 69–97 ns more at +/// 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 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 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. +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; + +/// 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 +/// to its group's state. Not measured on the executor: `asap_sketchlib`'s +/// own Count-Min Sketch of depth 3 with a top-k heap updates in about +/// 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. +pub(crate) const EXACT_MERGE_OPERATIONS_PER_STATE: f64 = 230.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; + +/// One DDSketch state merged: 1.8 µs per state of 10 rows, 2.5 µs per +/// 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; + +/// Per byte of any other sketch state merged, above +/// `EXACT_MERGE_OPERATIONS_PER_STATE`: merging adds every counter of the +/// input state. Not measured on the executor: `asap_sketchlib` merges a +/// Count-Min Sketch of 16 KB in about 6 µs and a HyperLogLog of 16 KB in +/// 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; + +/// 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 +/// 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; + +/// 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. +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` +/// with `parameters`. +pub(crate) fn update_operations_per_row( + summary_type: &FieldDataType, + parameters: Option<&SketchParams>, +) -> f64 { + match (summary_type, parameters) { + (FieldDataType::ExactAggregate(kind, _), _) if kind.tracks_counter_increase() => { + COUNTER_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( + parameters @ (SketchParams::Hydra(_) + | SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. }), + ), + ) => { + EXACT_UPDATE_OPERATIONS_PER_ROW + + SKETCH_UPDATE_OPERATIONS_PER_COUNTER * counters_updated_per_row(parameters) + } + // A summary without sketch parameters (a sample): one counter. + (_, None) => EXACT_UPDATE_OPERATIONS_PER_ROW + SKETCH_UPDATE_OPERATIONS_PER_COUNTER, + } +} + +/// 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 +/// (`univmon_expected_layers`); one for every other sketch. Example: a +/// Count-Min Sketch of depth 3 with a heap updates 4. +pub(crate) fn counters_updated_per_row(parameters: &SketchParams) -> f64 { + match parameters { + SketchParams::Hydra(hydra) => counters_updated_per_row(&hydra.per_subpopulation()), + SketchParams::Cms { depth, .. } | SketchParams::CountSketch { depth, .. } => { + f64::from(*depth) + } + SketchParams::CmsWithHeap { depth, .. } + | SketchParams::CountSketchWithHeap { depth, .. } => f64::from(*depth) + 1.0, + SketchParams::UnivMon { + sketch_rows, + layers, + .. + } => univmon_expected_layers(*layers) * (f64::from(*sketch_rows) + 1.0), + SketchParams::Kll { .. } + | SketchParams::DDSketch { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. } => 1.0, + } +} + +/// Layers a UnivMon row updates in expectation. `asap_sketchlib` updates +/// layers `0..=b`, with bottom layer `b` the index of the first zero among +/// hash bits `1..layers`, capped at `layers - 1`; so it reaches layer `j` +/// with probability `2^-j`, and the expectation is +/// `sum_{j < layers} 2^-j = 2 - 2^(1 - layers)`. +fn univmon_expected_layers(layers: u8) -> f64 { + 2.0 - 2f64.powi(1 - i32::from(layers.max(1))) +} + +/// 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 { + 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::Hydra(_) + | SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. }, + ) => { + EXACT_MERGE_OPERATIONS_PER_STATE + SKETCH_MERGE_OPERATIONS_PER_BYTE * state_bytes as f64 + } + } +} + +/// 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, + StateFamily::Sketch( + SketchParams::DDSketch { .. } + | SketchParams::Hydra(_) + | SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. }, + ) => SKETCH_ESTIMATE_OPERATIONS_PER_GROUP, + StateFamily::Exact(_) | StateFamily::None => FINALIZE_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 + 12 = 27. +pub(crate) fn hash_aggregate_operations_per_row( + grouping_key_count: u64, + measures: &[AggIntent], + rows_per_group: 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) +} + +/// The operations one input row costs a hash aggregate's `measure` over +/// 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); +/// - 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 +/// time and value in order and keeps running terms; +/// - any other folds the value (`AGGREGATE_MEASURE_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 { + match measure { + AggIntent::Quantile { .. } => { + AGGREGATE_MEASURE_OPERATIONS_PER_ROW + + (rows_per_group.max(2) as f64).log2() * SORT_OPERATIONS_PER_ROW_PER_LEVEL + } + AggIntent::Rate + | AggIntent::IRate + | AggIntent::Increase + | AggIntent::Deriv + | AggIntent::PredictLinear { .. } => COUNTER_UPDATE_OPERATIONS_PER_ROW, + AggIntent::Count + | AggIntent::Sum { .. } + | AggIntent::Min { .. } + | AggIntent::Max { .. } + | AggIntent::Avg { .. } + | AggIntent::StdDev { .. } + | AggIntent::Variance { .. } + | AggIntent::PearsonCorr { .. } + | AggIntent::TopK { .. } + | AggIntent::Cardinality { .. } + | AggIntent::FrequencyL2 { .. } + | AggIntent::FrequencyEntropy { .. } + | AggIntent::Changes + | AggIntent::Delta + | AggIntent::IDelta + | AggIntent::Resets + | AggIntent::DoubleExpSmoothing { .. } + | AggIntent::HistogramCount + | AggIntent::HistogramSum + | AggIntent::HistogramAvg + | AggIntent::HistogramStdDev + | AggIntent::HistogramStdVar + | AggIntent::HistogramFraction { .. } + | AggIntent::HistogramQuantile { .. } + | AggIntent::Math(_) + | AggIntent::Absent { .. } + | AggIntent::AbsentOverTime + | AggIntent::PresentOverTime + | AggIntent::TimeFn(_) + | AggIntent::Group + | AggIntent::CountValues { .. } + | AggIntent::LastOverTime + | AggIntent::FirstOverTime + | AggIntent::MadOverTime + | AggIntent::TsOfMinOverTime + | AggIntent::TsOfMaxOverTime + | AggIntent::TsOfFirstOverTime + | AggIntent::TsOfLastOverTime + | AggIntent::Extension { .. } => AGGREGATE_MEASURE_OPERATIONS_PER_ROW, + } +} diff --git a/crates/cost/src/node/operator_statistics.rs b/crates/cost/src/node/operator_statistics.rs index f382dec0f..d24c8a50e 100644 --- a/crates/cost/src/node/operator_statistics.rs +++ b/crates/cost/src/node/operator_statistics.rs @@ -18,6 +18,12 @@ impl EdgeStatistics { pub(crate) fn is_consistent(self) -> bool { matches!((self.rows, self.bytes), (0, 0) | (1.., 1..)) } + + /// The bytes of one row, rounded up: one state's bytes on an edge of + /// states (1,000 states of 16,000 bytes: 16), 0 on an empty edge. + pub(crate) fn bytes_per_row(self) -> u64 { + self.bytes.div_ceil(self.rows.max(1)) + } } /// Input and output evidence for a unary physical operator. diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index fac0cc11e..8d67db565 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -34,7 +34,7 @@ pub fn keeps_every_value(measure: &AggIntent) -> bool { ) } -/// Update operations per input row and bytes per state, from the sketch's +/// Bytes per state, from the summary's family, its /// parameters and `input_rows` (the rows one state folds). The bytes are /// `asap_sketchlib`'s `estimated_memory_bytes` for the type the executor /// builds (Count-Min and Count Sketch over `f64` counters), given what the @@ -59,24 +59,19 @@ pub fn keeps_every_value(measure: &AggIntent) -> bool { /// An exact accumulator's state is the executor's per-group state, /// [`EXACT_ACCUMULATOR_STATE_BYTES`] for every kind. /// -/// Update operations per input row: one counter 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 (`univmon_expected_layers`); -/// 1 for every other sketch. -/// /// `parameters` is the build's θ (`PhysicalASAPDAGNode::parameters`). Err /// [`AnalyticalCostError::UnsupportedCandidate`] for a sketch without θ: a /// planner bug, since every sketch build carries its θ. -pub fn summary_shape( +pub fn summary_state_bytes( family: &FieldDataType, parameters: Option<&SketchParams>, input_rows: StateInputRows, -) -> Result<(f64, u64), AnalyticalCostError> { +) -> Result { Ok(match (family, parameters) { - (FieldDataType::Sketch(..), Some(parameters)) => sketch_shape(parameters, input_rows), + (FieldDataType::Sketch(..), Some(parameters)) => sketch_state_bytes(parameters, input_rows), (FieldDataType::Sketch(..), None) => return Err(AnalyticalCostError::UnsupportedCandidate), - (FieldDataType::ExactAggregate(..), _) => (1.0, EXACT_ACCUMULATOR_STATE_BYTES), - _ => (1.0, 8), + (FieldDataType::ExactAggregate(..), _) => EXACT_ACCUMULATOR_STATE_BYTES, + _ => 8, }) } @@ -111,11 +106,10 @@ impl StateInputRows { }; } -/// [`summary_shape`] of one sketch: its update operations per row and its -/// bytes per state. With [`StateInputRows::UNKNOWN`], the bytes are also the +/// [`summary_state_bytes`] of one sketch. With [`StateInputRows::UNKNOWN`], the bytes are also the /// size the measured table interpolates between /// (`crate::measured`). -pub fn sketch_shape(params: &SketchParams, input_rows: StateInputRows) -> (f64, u64) { +pub fn sketch_state_bytes(params: &SketchParams, input_rows: StateInputRows) -> u64 { use asap_sketchlib::{ Classic, CountMinF64, CountMinSketchWithHeap, CountSketch, CountSketchWithHeap, DataInput, DdSketch, HyperLogLog, Kmv, RegularPath, UnivMon, Vector2D, @@ -125,89 +119,71 @@ pub fn sketch_shape(params: &SketchParams, input_rows: StateInputRows) -> (f64, usize::try_from(rows.max(1)).unwrap_or(usize::MAX) }); match params { - SketchParams::Hydra(hydra) => sketch_shape(&hydra.per_subpopulation(), input_rows), - SketchParams::Cms { width, depth } => ( - f64::from(*depth), - CountMinF64::estimated_memory_bytes(*depth as usize, *width as usize) as u64, - ), - SketchParams::CountSketch { width, depth } => ( - f64::from(*depth), + SketchParams::Hydra(hydra) => sketch_state_bytes(&hydra.per_subpopulation(), input_rows), + SketchParams::Cms { width, depth } => { + CountMinF64::estimated_memory_bytes(*depth as usize, *width as usize) as u64 + } + SketchParams::CountSketch { width, depth } => { CountSketch::, RegularPath>::estimated_memory_bytes( *depth as usize, *width as usize, - ) as u64, - ), + ) as u64 + } SketchParams::CmsWithHeap { width, depth, heap_size, - } => ( - f64::from(*depth) + 1.0, - CountMinSketchWithHeap::, RegularPath>::estimated_memory_bytes( - *depth as usize, - *width as usize, - *heap_size as usize, - &heap_key, - ) as u64, - ), + } => CountMinSketchWithHeap::, RegularPath>::estimated_memory_bytes( + *depth as usize, + *width as usize, + *heap_size as usize, + &heap_key, + ) as u64, SketchParams::CountSketchWithHeap { width, depth, heap_size, - } => ( - f64::from(*depth) + 1.0, - CountSketchWithHeap::, RegularPath>::estimated_memory_bytes( - *depth as usize, - *width as usize, - *heap_size as usize, - &heap_key, - ) as u64, - ), - SketchParams::Kll { k, .. } => ( - 1.0, - kll_fixed_bytes() + 8 * kll_buffer_items(*k, input_rows), - ), + } => CountSketchWithHeap::, RegularPath>::estimated_memory_bytes( + *depth as usize, + *width as usize, + *heap_size as usize, + &heap_key, + ) as u64, + SketchParams::Kll { k, .. } => kll_fixed_bytes() + 8 * kll_buffer_items(*k, input_rows), // The executor builds no DDSketch outside α in (0, 1), where // sketchlib's estimate panics: priced as an empty one. SketchParams::DDSketch { alpha } if !(*alpha > 0.0 && *alpha < 1.0) => { - (1.0, std::mem::size_of::() as u64) + 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); - ( - 1.0, - DdSketch::estimated_memory_bytes( - *alpha, - rows, - min_abs, - max_abs, - !input_rows.non_negative, - ) as u64, - ) + DdSketch::estimated_memory_bytes( + *alpha, + rows, + min_abs, + max_abs, + !input_rows.non_negative, + ) as u64 + } + SketchParams::Hll { precision, .. } => { + HyperLogLog::::estimated_memory_bytes(usize::from(*precision)) as u64 } - SketchParams::Hll { precision, .. } => ( - 1.0, - HyperLogLog::::estimated_memory_bytes(usize::from(*precision)) as u64, - ), - SketchParams::Kmv { k, .. } => (1.0, Kmv::estimated_memory_bytes(*k as usize, rows) as u64), + SketchParams::Kmv { k, .. } => Kmv::estimated_memory_bytes(*k as usize, rows) as u64, SketchParams::UnivMon { heap_size, sketch_rows, sketch_cols, layers, .. - } => ( - univmon_expected_layers(*layers) * (f64::from(*sketch_rows) + 1.0), - UnivMon::estimated_memory_bytes( - *heap_size as usize, - *sketch_rows as usize, - *sketch_cols as usize, - usize::from(*layers), - &DataInput::Bytes(&[0; ASSUMED_VALUE_KEY_BYTES]), - ) as u64, - ), + } => UnivMon::estimated_memory_bytes( + *heap_size as usize, + *sketch_rows as usize, + *sketch_cols as usize, + usize::from(*layers), + &DataInput::Bytes(&[0; ASSUMED_VALUE_KEY_BYTES]), + ) as u64, } } @@ -278,15 +254,6 @@ 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; -/// Layers a UnivMon row updates in expectation. `asap_sketchlib` updates -/// layers `0..=b`, with bottom layer `b` the index of the first zero among -/// hash bits `1..layers`, capped at `layers - 1`; so it reaches layer `j` -/// with probability `2^-j`, and the expectation is -/// `sum_{j < layers} 2^-j = 2 - 2^(1 - layers)`. -fn univmon_expected_layers(layers: u8) -> f64 { - 2.0 - 2f64.powi(1 - i32::from(layers.max(1))) -} - /// 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 diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index f9fd83bce..131088675 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -13,12 +13,12 @@ fn exact_state_is_priced_at_the_executors_state_size() { (ExactKind::Rate, ExactParams::Rate), ] { assert_eq!( - summary_shape( + summary_state_bytes( &FieldDataType::ExactAggregate(kind, params), None, StateInputRows::UNKNOWN ), - Ok((1.0, 56)) + Ok(56) ); } assert_eq!(asap_types::summary_type::EXACT_ACCUMULATOR_STATE_BYTES, 56); @@ -103,7 +103,7 @@ fn library_ddsketch_bytes(alpha: f64, values: &[f64]) -> u64 { fn ddsketch_state_bytes_bound_the_library() { let alpha = 0.05; let priced = |rows_per_state, non_negative| { - sketch_shape( + sketch_state_bytes( &SketchParams::DDSketch { alpha }, StateInputRows { rows_per_state, @@ -111,7 +111,6 @@ fn ddsketch_state_bytes_bound_the_library() { value_magnitude_range: None, }, ) - .1 }; // Ten Pareto(2) samples (scale 1), one 1 s tumbling window of a 10 Hz series. let pareto: Vec = (0..10) @@ -156,7 +155,7 @@ fn ddsketch_state_bytes_bound_the_library() { fn ddsketch_state_bytes_follow_the_declared_value_magnitude_range() { let alpha = 0.01; let costed = |value_magnitude_range| { - sketch_shape( + sketch_state_bytes( &SketchParams::DDSketch { alpha }, StateInputRows { rows_per_state: Some(600), @@ -164,7 +163,6 @@ fn ddsketch_state_bytes_follow_the_declared_value_magnitude_range() { value_magnitude_range, }, ) - .1 }; let struct_bytes = std::mem::size_of::() as u64; let bytes_for_bins = |bins: u64| struct_bytes + 8 * 2 * (bins + 126); @@ -199,7 +197,7 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() { sketch.memory_bytes() as u64 }; let model_bytes = |rows_per_state| { - sketch_shape( + sketch_state_bytes( &SketchParams::Kll { k }, StateInputRows { rows_per_state, @@ -207,7 +205,6 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() { value_magnitude_range: None, }, ) - .1 }; // An empty sketch holds its fixed bytes; one sample adds 16 items. assert_eq!(library_bytes(0), kll_fixed_bytes()); @@ -416,15 +413,14 @@ fn state_bytes_bound_the_executors_charge() { let state = updater.into_accumulator(); let charged = state.approx_memory_bytes(); let priced = fields - + sketch_shape( + + sketch_state_bytes( params, StateInputRows { rows_per_state: Some(u64::from(n)), non_negative: false, value_magnitude_range: None, }, - ) - .1 as usize; + ) as usize; let context = format!("{algorithm:?} after {n} rows: {priced} priced"); match priced_as { Priced::Exact => assert_eq!(charged, priced, "{context}"), @@ -499,7 +495,7 @@ fn next_uniform(state: &mut u64) -> f64 { /// non-negative), or 1,000 rows of 1,000 distinct label values or label /// sets. Its charge /// (`approx_memory_bytes`; for HydraCms its shared grid over its 10 -/// groups) is compared with `summary_shape` for the same rows. Priced ÷ +/// groups) is compared with `summary_state_bytes` for the same rows. Priced ÷ /// charged lies within [`BYTES_AGREEMENT_FACTOR`], or the family's row /// carries its [`KnownBytesGap`]. /// @@ -657,9 +653,7 @@ fn priced_bytes_agree_with_the_executor() { } } let charged = updater.into_accumulator().approx_memory_bytes() as u64; - let priced = summary_shape(&family, params.as_ref(), input_rows(rows_fed)) - .unwrap() - .1; + let priced = summary_state_bytes(&family, params.as_ref(), input_rows(rows_fed)).unwrap(); measured.push((family, priced, charged, known_gap)); } // HydraCms: one grid for every group, priced as one Count-Min per @@ -682,7 +676,7 @@ fn priced_bytes_agree_with_the_executor() { } measured.push(( hydra_family, - sketch_shape(&hydra, input_rows(hydra_rows_fed / HYDRA_GROUPS)).1, + sketch_state_bytes(&hydra, input_rows(hydra_rows_fed / HYDRA_GROUPS)), (grid.approx_memory_bytes() / HYDRA_GROUPS as usize) as u64, gap(947, 0.104), )); diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index efc2bd383..589a0edd2 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -22,11 +22,15 @@ use std::collections::{BTreeMap, BTreeSet}; 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, + update_operations_per_row, 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_shape, summary_shape, StateInputRows, + keeps_every_value, row_bytes, sketch_state_bytes, summary_state_bytes, StateInputRows, }; use crate::{aggregate_lookup, lookup, StateFamily}; use crate::{ @@ -115,11 +119,16 @@ fn state_family<'a>( | FieldDataType::Wavelet(..) | FieldDataType::StatModel(..) => StateFamily::None, }, - Payload::ASAP( - ASAPOp::SummaryMerge { .. } - | ASAPOp::SummaryEstimate { .. } - | ASAPOp::FinalizeExactAccumulator { .. }, - ) => producer_family.unwrap_or(StateFamily::None), + // A merge over a time range of a chain's windows reads states whose + // producer (the time range) has no family: they are its own θ's, or + // the exact accumulator its output column holds. + Payload::ASAP(ASAPOp::SummaryMerge { .. }) => match producer_family { + None | Some(StateFamily::None) => merged_state_family(node), + Some(family) => family, + }, + Payload::ASAP(ASAPOp::SummaryEstimate { .. } | ASAPOp::FinalizeExactAccumulator { .. }) => { + producer_family.unwrap_or(StateFamily::None) + } Payload::NonASAP( NonASAPOp::Scan { .. } | NonASAPOp::Values { .. } @@ -153,6 +162,28 @@ fn state_family<'a>( }) } +/// The family of the states merge `node` outputs: its θ for a sketch, else +/// the exact accumulator of its output's state column, else none. +fn merged_state_family( + node: &asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode, +) -> StateFamily<'_> { + if let Some(parameters) = &node.parameters { + return StateFamily::Sketch(parameters); + } + node.output_schema + .fields + .iter() + .find_map(|field| match &field.dtype { + FieldDataType::ExactAggregate(kind, _) => Some(StateFamily::Exact(kind)), + FieldDataType::Plain(_) + | FieldDataType::Sketch(..) + | FieldDataType::SummarySample(..) + | FieldDataType::Wavelet(..) + | FieldDataType::StatModel(..) => None, + }) + .unwrap_or(StateFamily::None) +} + /// One producer of a node being costed ([`node_resource_usage_and_cost`]): its payload and /// schema, its output statistics, the sources it reads and its state /// family. @@ -331,7 +362,7 @@ pub fn node_resource_usage_and_cost<'a>( let speed = settings.resource_speed; let unit_costs = settings.cost_model.per_node_unit_costs(); let sketch_size = - |params: &SketchParams| sketch_shape(params, StateInputRows::UNKNOWN).1 as f64; + |params: &SketchParams| sketch_state_bytes(params, StateInputRows::UNKNOWN) as f64; let mut fit = CalibrationFit::default(); // Query-time memory held while the node computes. let mut held_bytes = None; @@ -637,10 +668,11 @@ pub fn node_resource_usage_and_cost<'a>( }; 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 estimate = estimate_operator( PhysicalOperator::HashAggregate { grouping_key_count: keys, - accumulator_count: measures.len().max(1) as u64, + operations_per_row: operations_per_row.ceil() as u64, }, OperatorStatistics::HashAggregate { edges: unary(out), @@ -665,7 +697,8 @@ pub fn node_resource_usage_and_cost<'a>( out, estimate, format!( - "hash aggregate {} rows into {group_count} groups", + "hash aggregate {} rows into {group_count} groups: {operations_per_row:.0} \ + ops per row", input.rows ), ) @@ -842,8 +875,12 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; - let (depth, state_bytes) = summary_shape(family, node.parameters.as_ref(), input_rows)?; - let state_bytes = measured_state_bytes.unwrap_or(state_bytes); + 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 out = EdgeStatistics { rows: group_count, bytes: group_count @@ -853,12 +890,12 @@ pub fn node_resource_usage_and_cost<'a>( ( out, Ok(ResourceEstimate::new( - input.rows as f64 * depth, + input.rows as f64 * update_ops, out.bytes, 0, )), format!( - "build {} into {group_count} states: {} rows x depth {depth}", + "build {} into {group_count} states: {} rows x {update_ops} ops", summary_type_name(family), input.rows ), @@ -890,8 +927,30 @@ pub fn node_resource_usage_and_cost<'a>( non_negative, value_magnitude_range, }; - let (depth, state_bytes) = summary_shape(family, node.parameters.as_ref(), input_rows)?; - let state_bytes = measured_state_bytes.unwrap_or(state_bytes); + 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()); + // 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 + // window's. + let emitted_states = match ingestion { + true => { + group_count as f64 * ONE_SECOND_MS as f64 / *tumbling_window_width_ms as f64 + } + 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, + }; let out = EdgeStatistics { rows: group_count, bytes: group_count @@ -901,20 +960,24 @@ pub fn node_resource_usage_and_cost<'a>( ( out, Ok(ResourceEstimate::new( - input.rows as f64 * (depth + 1.0), + input.rows as f64 * update_ops + + range_ops + + emitted_states * CHAIN_EMIT_OPERATIONS_PER_STATE, out.bytes, 0, )), format!( "build {} in tumbling windows of {tumbling_window_width_ms} ms for \ - {group_count} groups: {} rows x (1 + depth {depth})", + {group_count} groups: {} rows x {update_ops} ops, {emitted_states:.1} \ + states emitted", summary_type_name(family), input.rows ), ) } Payload::ASAP(ASAPOp::SummaryMerge { group_by, .. }) => { - // One operation per input state read; one state per group + // Each input state read is merged into its group's (by the + // states' family, `merge_ops` below); one state per group // out: an input's groups are its states of one window (all // its states unless it selects a chain's windows). A // rollup's groups are those of its keys, at most its input @@ -929,20 +992,27 @@ pub fn node_resource_usage_and_cost<'a>( ); } let states = merged_states(producers)?; + // Each input state's merge, by the states' family and bytes. + let merge_ops: f64 = producers + .iter() + .map(|producer| { + producer.output.rows as f64 + * merge_operations_per_state(family, producer.output.bytes_per_row()) + }) + .sum(); if let Some(keys) = group_by { group_count = group_count.min(groups(&Reduction::by(keys.clone()))?); } let out = EdgeStatistics { rows: group_count, bytes: input - .bytes - .div_ceil(input.rows.max(1)) + .bytes_per_row() .checked_mul(group_count) .ok_or(AnalyticalCostError::Overflow)?, }; ( out, - Ok(ResourceEstimate::new(states as f64, out.bytes, 0)), + Ok(ResourceEstimate::new(merge_ops, out.bytes, 0)), format!("merge {states} states into {group_count}"), ) } @@ -963,13 +1033,21 @@ pub fn node_resource_usage_and_cost<'a>( )?; ( out, - Ok(ResourceEstimate::new(out.rows as f64, 0, 0)), + Ok(ResourceEstimate::new( + input.rows as f64 * estimate_operations_per_group(family), + 0, + 0, + )), format!("estimate {} groups x {per_group}", input.rows), ) } Payload::ASAP(ASAPOp::FinalizeExactAccumulator { .. }) => ( edge(input.rows)?, - Ok(ResourceEstimate::new(input.rows as f64, 0, 0)), + Ok(ResourceEstimate::new( + input.rows as f64 * estimate_operations_per_group(family), + 0, + 0, + )), format!("finalize {} accumulators", input.rows), ), Payload::ASAP( diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index efd664bf0..95e2c5e67 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -4,6 +4,7 @@ use std::time::Duration; use asap_types::ir::properties::ExecutionDataState; use asap_types::ir::scalar::ColumnRef; +use asap_types::ir::schema::Field; use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode; use asap_types::plan::time_window::{InputTimeAccess, TimeIndexedPlan}; use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; @@ -183,6 +184,149 @@ fn an_operator_keeping_its_input_window_over_a_chain_or_its_windows_selects_what } } +/// A merge over a time range of a DDSketch chain's windows reads DDSketch +/// states, though the time range itself has no family: the merge's θ names +/// it, so each of the 5,000 states read costs a DDSketch merge (2,500 +/// operations), not an exact accumulator's (230). +#[test] +fn a_merge_over_a_chain_s_windows_is_costed_by_the_chain_s_family() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let windows = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let windows_output = ProducerOutput { + payload: &windows.payload, + output_schema: &windows.output_schema, + output: EdgeStatistics { + rows: 5_000, + bytes: 5_000 * 4_096, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 5, + chain_tumbling_window_width_ms: None, + }; + let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + merge.parameters = Some(SketchParams::DDSketch { alpha: 0.01 }); + let merge_usage = + node_resource_usage_and_cost(&merge, &[windows_output], (1, 1.0), None, &data, &settings) + .unwrap(); + assert!(matches!( + merge_usage.family, + StateFamily::Sketch(SketchParams::DDSketch { .. }) + )); + assert_eq!( + merge_usage.cpu_ops_per_second, + 5_000.0 * crate::node::operations_per_unit::DDSKETCH_MERGE_OPERATIONS_PER_STATE + ); +} + +/// A merge over a time range of a Rate chain's windows reads Rate +/// accumulator states, though the time range has no family and the merge +/// no θ: its output's state column names the family, so each of the 5,000 +/// states read costs a counter-increase merge (350 operations), not a +/// plain exact accumulator's (230). +#[test] +fn a_merge_over_a_rate_chain_s_windows_is_costed_by_the_rate_accumulator() { + let settings = CostInputs::default(); + let data = DataWorkload::default(); + let windows = node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })); + let windows_output = ProducerOutput { + payload: &windows.payload, + output_schema: &windows.output_schema, + output: EdgeStatistics { + rows: 5_000, + bytes: 5_000 * asap_types::summary_type::EXACT_ACCUMULATOR_STATE_BYTES, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 5, + chain_tumbling_window_width_ms: None, + }; + let mut merge = node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: None, + })); + merge.output_schema = Schema::new(vec![Field::new( + "state", + FieldDataType::ExactAggregate(ExactKind::Rate, ExactParams::Rate), + false, + )]); + let merge_usage = + node_resource_usage_and_cost(&merge, &[windows_output], (1, 1.0), None, &data, &settings) + .unwrap(); + assert_eq!(merge_usage.family, StateFamily::Exact(&ExactKind::Rate)); + assert_eq!( + merge_usage.cpu_ops_per_second, + 5_000.0 * crate::node::operations_per_unit::COUNTER_MERGE_OPERATIONS_PER_STATE + ); +} + +/// 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 + 12 range) operations plus five emitted states × 2,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, + }; + 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: 36_000, + bytes: 36_000 * 16, + }, + sources: BTreeSet::new(), + family: StateFamily::None, + unchanged_columns: BTreeSet::new(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + }; + let chain = PhysicalASAPDAGNode { + retention_ms: Some(300_000), + ..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: 60_000, + })) + }; + let usage = + node_resource_usage_and_cost(&chain, &[rows], (1, 1.0), None, &data, &settings).unwrap(); + 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 + ); +} + /// 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 @@ -536,14 +680,14 @@ fn ddsketch_state_bytes( .bytes } -/// The bytes `sketch_shape` gives a DDSketch state at α = 0.01 over 600 +/// The bytes `sketch_state_bytes` gives a DDSketch state at α = 0.01 over 600 /// rows of the given sign and magnitude range. fn ddsketch_costed_bytes( non_negative: bool, value_magnitude_range: Option, ) -> u64 { - use crate::node::state_bytes::{sketch_shape, StateInputRows}; - sketch_shape( + use crate::node::state_bytes::{sketch_state_bytes, StateInputRows}; + sketch_state_bytes( &SketchParams::DDSketch { alpha: 0.01 }, StateInputRows { rows_per_state: Some(600), @@ -551,7 +695,6 @@ fn ddsketch_costed_bytes( value_magnitude_range, }, ) - .1 } /// A scan of PromQL metric `metric`. diff --git a/crates/cost/src/plan/cost.rs b/crates/cost/src/plan/cost.rs index 41253cf88..91edcde02 100644 --- a/crates/cost/src/plan/cost.rs +++ b/crates/cost/src/plan/cost.rs @@ -9,8 +9,9 @@ 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_shape, StateInputRows}; +use crate::node::state_bytes::{sketch_state_bytes, 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; @@ -57,7 +58,8 @@ pub struct PlanCost { pub struct QueryLatency { /// Milliseconds to merge the ingest workers' partial copies of the /// newest window of the largest ingestion-time state the query reads; - /// 0 when ingestion fits on one core or the query reads none. + /// 0 when ingestion fits on one core or the query reads no state (only + /// raw rows or an exact `Aggregate`'s values kept at ingestion time). pub compaction_ms: f64, /// Milliseconds of the query-time work the query reaches, each node /// once. @@ -190,7 +192,7 @@ pub fn plan_cost( // finalizations over it. let mut family_of: HashMap> = HashMap::new(); let sketch_size = - |params: &SketchParams| sketch_shape(params, StateInputRows::UNKNOWN).1 as f64; + |params: &SketchParams| sketch_state_bytes(params, StateInputRows::UNKNOWN) as f64; for node in &dag.nodes { let node_costed = &costed[&node.id]; usage.cpu_ops_per_second += node_costed.cpu_ops_per_second; @@ -294,29 +296,35 @@ pub fn plan_cost( measured.detail += &format!("; {copies} partial copies of its newest window, {bytes} bytes"); } - // Milliseconds of one merge of each state with a measured query-time - // merge entry (measured ns · 10⁻⁶), for the newest window's compaction. - let merge_ms: HashMap = family_of - .iter() - .filter_map(|(id, family)| { - lookup( - table, - OperatorKind::SummaryMerge, - ExecutionTiming::QueryTime, - *family, - sketch_size, - ) - .map(|unit| (*id, (unit.ns_per_unit + unit.ns_per_invocation) * 1e-6)) - }) - .collect(); - let mut latency = query_latencies( - dag, - nodes, - &output, - &work_ms, - &merge_ms, - speed.ms_per_cpu_op * formula_scale, - ); + // Milliseconds of one merge of a node's state when the newest window's + // partial copies are compacted: its measured query-time merge entry + // (ns · 10⁻⁶) when the table has one, else the merge formula of its + // family and bytes per state, as a `SummaryMerge` over it is costed (a + // DDSketch state: 2,500 operations). 0 for a node holding no states, + // 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. + let merge_ms = |id: PhysicalASAPNodeId| { + let Some(&family) = family_of.get(&id) else { + return 0.0; + }; + lookup( + table, + OperatorKind::SummaryMerge, + ExecutionTiming::QueryTime, + family, + sketch_size, + ) + .map_or_else( + || { + merge_operations_per_state(family, output[&id].bytes_per_row()) + * speed.ms_per_cpu_op + * formula_scale + }, + |unit| (unit.ns_per_unit + unit.ns_per_invocation) * 1e-6, + ) + }; + let mut latency = query_latencies(dag, nodes, &output, &work_ms, merge_ms); for (latency, evaluation_ms) in latency.iter_mut().zip(evaluation_ms_per_query) { latency.query_ms += evaluation_ms; } @@ -472,15 +480,14 @@ fn peak_cpu_cores( /// state's newest window merges, for each of its `I` states per window, /// the `k` ingest workers' partial copies, `(k − 1)·I` merges, where `k` /// is the cores its ingestion work takes, at least one. Each merge takes -/// the state's measured merge time (`merge_ms`) when the measured table has -/// one, and one operation otherwise. +/// `merge_ms` of the state: measured, or its family's merge formula, 0 for +/// a node holding no states (its partial copies are concatenated). fn query_latencies( dag: &PhysicalASAPDAG, nodes: &HashMap, output: &HashMap, work_ms: &HashMap, - merge_ms: &HashMap, - formula_merge_ms: f64, + merge_ms: impl Fn(PhysicalASAPNodeId) -> f64, ) -> Vec { let inputs = |id: PhysicalASAPNodeId| { dag.edges @@ -493,8 +500,7 @@ fn query_latencies( let compaction_ms = |state: PhysicalASAPNodeId| { let workers = ingest_workers(dag, work_ms, state) as f64; let states = output[&state].rows as f64; - let per_merge_ms = merge_ms.get(&state).copied().unwrap_or(formula_merge_ms); - (workers - 1.0) * states * per_merge_ms + (workers - 1.0) * states * merge_ms(state) }; query_starts(dag) .into_iter() diff --git a/crates/integration-tests/src/lib.rs b/crates/integration-tests/src/lib.rs index 813deaa4e..7d2ff54e4 100644 --- a/crates/integration-tests/src/lib.rs +++ b/crates/integration-tests/src/lib.rs @@ -245,14 +245,154 @@ pub mod planning { /// For tests of a rewrite's structure or execution over a summary whose /// output has no guarantee that meets `accuracy` (e.g. a value filter /// over an estimate), not of accuracy acceptance. + /// + /// Summaries' CPU is costed free ([`summary_cpu_free`]), so the plan reads + /// the summary candidate wherever Stage 1 offers one, whether or not the + /// default unit costs make it cheaper than the exact plan on these + /// tests' small data. pub fn logical_candidates_ignoring_guarantees( root: Rc, accuracy: AccuracyTarget, ) -> Vec> { - let models = asap_planner::PlanningModels::builtin().with_ignore_accuracy_guarantees(true); + logical_candidates_ignoring_guarantees_on( + root, + accuracy, + &asap_types::deployment_capabilities::DeploymentCapabilities::default(), + ) + } + + /// [`logical_candidates_ignoring_guarantees`] on a deployment with + /// `capabilities`. For a test of one sketch's execution: with every + /// summary's CPU free, Stage 3 picks among sketches serving the same + /// readout by memory alone, e.g. a DDSketch over a KLL for a global + /// quantile, so a KLL test declares an executor that builds only KLL. + pub fn logical_candidates_ignoring_guarantees_on( + root: Rc, + accuracy: AccuracyTarget, + capabilities: &asap_types::deployment_capabilities::DeploymentCapabilities, + ) -> Vec> { + let settings = summary_cpu_free(); + let models = asap_planner::PlanningModels::builtin() + .with_ignore_accuracy_guarantees(true) + .with_cost_inputs(&settings) + .with_deployment_capabilities(capabilities); selected_operator_roots(&run_all_stages_with(root, accuracy, models)) } + /// Default cost inputs with a measured table that prices the CPU of + /// every summary operator (build, chain, merge, estimate, finalize) of + /// every sketch family, every exact accumulator and no family at 0 ns; + /// memory is still charged. For tests of a summary plan's shape, + /// execution or reuse, which need Stage 3 to select the summary. + pub fn summary_cpu_free() -> asap_cost::CostInputs { + use asap_cost::{OperatorCost, OperatorKind}; + use asap_types::summary_type::{ExactKind, HydraParams, SketchParams}; + // One instance of each sketch family: the table matches an entry by + // family. A new family fails to compile in the exhaustive match + // below; add an instance of it to this array too. + let sketches = [ + SketchParams::Kll { k: 200 }, + SketchParams::DDSketch { alpha: 0.01 }, + SketchParams::Cms { + width: 1024, + depth: 3, + }, + SketchParams::CmsWithHeap { + width: 1024, + depth: 3, + heap_size: 32, + }, + SketchParams::CountSketch { + width: 1024, + depth: 3, + }, + SketchParams::CountSketchWithHeap { + width: 1024, + depth: 3, + heap_size: 32, + }, + SketchParams::Hll { precision: 12 }, + SketchParams::Kmv { k: 1024 }, + SketchParams::UnivMon { + heap_size: 32, + sketch_rows: 3, + sketch_cols: 1024, + layers: 8, + }, + SketchParams::Hydra(HydraParams::HydraCms { + width: 1024, + depth: 3, + shared_rows: 3, + shared_columns: 1024, + }), + ]; + for sketch in &sketches { + match sketch { + SketchParams::Kll { .. } + | SketchParams::DDSketch { .. } + | SketchParams::Cms { .. } + | SketchParams::CmsWithHeap { .. } + | SketchParams::CountSketch { .. } + | SketchParams::CountSketchWithHeap { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. } + | SketchParams::UnivMon { .. } + | SketchParams::Hydra(_) => {} + } + } + // Every exact accumulator; as above, a new kind fails to compile in + // the match below, and needs adding to this array too. + let exact = [ + ExactKind::Sum, + ExactKind::Count, + ExactKind::Min, + ExactKind::Max, + ExactKind::Increase, + ExactKind::Rate, + ExactKind::IRate, + ]; + for kind in &exact { + match kind { + ExactKind::Sum + | ExactKind::Count + | ExactKind::Min + | ExactKind::Max + | ExactKind::Increase + | ExactKind::Rate + | ExactKind::IRate => {} + } + } + // Every family, and no family (`sketch` and `exact` both `None`). + let families = sketches + .into_iter() + .map(|sketch| (Some(sketch), None)) + .chain(exact.into_iter().map(|kind| (None, Some(kind)))) + .chain([(None, None)]); + let mut settings = asap_cost::CostInputs::default(); + settings.measured_operation_costs.operators = families + .flat_map(|(sketch, exact)| { + [ + OperatorKind::SummaryAgg, + OperatorKind::SummaryTumblingWindowChain, + OperatorKind::SummaryMerge, + OperatorKind::SummaryEstimate, + OperatorKind::FinalizeExactAccumulator, + ] + .map(|operator| OperatorCost { + operator, + phase: None, + sketch: sketch.clone(), + exact, + measure: None, + ns_per_unit: 0.0, + ns_per_invocation: 0.0, + memory_bytes_per_state: None, + }) + }) + .collect(); + settings + } + /// Whether `node` builds a summary. fn is_build(node: &Rc) -> bool { matches!( diff --git a/crates/integration-tests/tests/default_cost_plan_ranking.rs b/crates/integration-tests/tests/default_cost_plan_ranking.rs new file mode 100644 index 000000000..c6c8da501 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking.rs @@ -0,0 +1,570 @@ +//! Stage 3 under the default `CostInputs` (no measured table) ranks the +//! exact plan against the alternatives as the executor measures them. +//! +//! The expectations are the executor's measured total CPU (query plus +//! ingestion) per second of wall time, for each plan kind of each workload +//! below: measured on one core of an idle server, in a release build, +//! serially, with the executor at commit 59365ce80 over the synthetic data the +//! workloads declare (`latency` a non-negative gauge, `requests_total` a +//! counter, 10 jobs), every accuracy target 1% relative at δ = 0.01. +//! +//! Example: `quantile_over_time(0.99, latency[1h])` over 1,000 series +//! sampled at 1 Hz, every 10 s, raw data kept. The exact plan (scan, keep +//! every value per series, sort) measures 39.0 CPU-ms per second; a +//! DDSketch built from the raw rows at query time measures 77.8, twice as +//! much, because inserting a row into a DDSketch costs more than keeping it +//! and sorting it. The planner must not predict the sketch cheaper. + +use asap_cost::CostInputs; +use asap_devtools::planning_stages::{lower_promql_roots, run_planning_stages_over}; +use asap_planner::{DeploymentCapabilities, PlanningModels}; +use asap_types::ir::properties::ExecutionTiming; +use asap_types::ir::schema::FieldDataType; +use asap_types::ir::ASAPOp; +use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPOperatorPayload}; +use asap_types::workload::PlanningWorkload; + +/// Data rate and evaluation interval of one measured setting, over 1,000 +/// series of each source. +#[derive(Debug, Clone, Copy)] +struct Setting { + /// Milliseconds between two samples of one series. + sample_interval_ms: u32, + /// Milliseconds between two evaluations of the query. + evaluation_interval_ms: u32, +} + +/// 1 Hz, every 10 s. +const ONE_HZ_EVERY_10_S: Setting = Setting { + sample_interval_ms: 1_000, + evaluation_interval_ms: 10_000, +}; +/// 10 Hz, every 60 s. +const TEN_HZ_EVERY_60_S: Setting = Setting { + sample_interval_ms: 100, + evaluation_interval_ms: 60_000, +}; +/// 1 Hz, every 300 s. +const ONE_HZ_EVERY_300_S: Setting = Setting { + sample_interval_ms: 1_000, + evaluation_interval_ms: 300_000, +}; + +/// A measured PromQL query and the metric of its 1% accuracy target. +#[derive(Debug, Clone, Copy)] +struct Query { + promql: &'static str, + /// `relative` or `rank`. + accuracy_metric: &'static str, +} + +/// `query` every `setting.evaluation_interval_ms` with its 1% target at +/// δ = 0.01, over 1,000 series each of `latency` and `requests_total`. +fn workload(query: Query, setting: Setting) -> PlanningWorkload { + let Setting { + sample_interval_ms, + evaluation_interval_ms, + } = setting; + let Query { + promql, + accuracy_metric, + } = query; + let requirements = format!( + r#""accuracy": {{"max_error": 0.01, "failure_probability": 0.01, "accuracy_scope": "per_value", "accuracy_metric": "{accuracy_metric}"}}"# + ); + let rows_per_second = 2.0 * 1_000.0 * 1_000.0 / f64::from(sample_interval_ms); + serde_json::from_str(&format!( + r#"{{"query_workload": {{"language": "promql", "repeating_queries": [ + {{"query": {promql:?}, "demand": {{"fixed_interval_at": {{"interval": {evaluation_interval_ms}, "evaluation_phase": 0}}}}, + "requirements": {{{requirements}}}, "predictability": {{"predictable": {{"known_at": null}}}}}}]}}, + "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": {rows_per_second}}}}}, + "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() +} + +/// The plan Stage 3 selects for `workload` under the default cost inputs, +/// with raw data kept or not, and its predicted CPU core-seconds per second. +fn selected(workload: &PlanningWorkload, raw_data_retained: bool) -> (PhysicalASAPDAG, f64) { + let settings = CostInputs::default(); + let capabilities = DeploymentCapabilities { + raw_data_retained, + ..Default::default() + }; + let models = PlanningModels::builtin() + .with_cost_inputs(&settings) + .with_deployment_capabilities(&capabilities); + let entries: Vec<_> = workload.query_workload.entries().collect(); + let data = workload.data_workload.clone().unwrap(); + let roots = lower_promql_roots(workload).unwrap(); + let run = run_planning_stages_over(roots, &entries, &data, models).unwrap(); + let (plan, _) = run.run.selection.as_ref().expect("a plan is selected"); + let cpu = predicted_cpu(plan, workload); + (plan.clone(), cpu) +} + +/// The exact plan the executor measured as the baseline: the query-time +/// lowering of `query` (scan raw data, compute exactly), predicted CPU +/// core-seconds per second. +fn exact_query_time_cpu(query: Query, setting: Setting) -> f64 { + let w = workload(query, setting); + let roots = lower_promql_roots(&w).unwrap(); + let (plan, _) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) + .unwrap(); + predicted_cpu(&plan, &w) +} + +/// `plan`'s predicted CPU core-seconds per second under the default cost +/// inputs: query and ingestion time. +fn predicted_cpu(plan: &PhysicalASAPDAG, workload: &PlanningWorkload) -> f64 { + let entries: Vec<_> = workload.query_workload.entries().collect(); + let data = workload.data_workload.clone().unwrap(); + let (cost, _) = asap_cost::plan_cost(plan, &entries, &data, &CostInputs::default()).unwrap(); + cost.per_node + .values() + .map(|node| node.cpu_core_seconds_per_second) + .sum() +} + +/// What a plan computes its answer from, as the measurements name it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum PlanKind { + /// Raw data scanned at query time and computed exactly (no summary). + ExactScan, + /// An exact accumulator built from raw data at query time. + QueryTimeExactAccumulator, + /// A sketch built from raw data at query time. + QueryTimeSketch, + /// States maintained at ingestion time in tumbling windows and merged + /// at query time. + StoredStates, +} + +fn plan_kind(plan: &PhysicalASAPDAG) -> PlanKind { + let mut kind = PlanKind::ExactScan; + for node in &plan.nodes { + match (&node.payload, node.output_state.timing) { + ( + PhysicalASAPOperatorPayload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }), + ExecutionTiming::IngestionTime, + ) => return PlanKind::StoredStates, + ( + PhysicalASAPOperatorPayload::ASAP(ASAPOp::SummaryAgg { summary_type, .. }), + ExecutionTiming::QueryTime, + ) if kind != PlanKind::QueryTimeSketch => { + kind = match summary_type { + FieldDataType::Sketch(..) => PlanKind::QueryTimeSketch, + _ => PlanKind::QueryTimeExactAccumulator, + } + } + _ => {} + } + } + kind +} + +/// Whether `plan` keeps or builds a sketch anywhere. +fn has_sketch(plan: &PhysicalASAPDAG) -> bool { + plan.nodes.iter().any(|node| { + matches!( + &node.payload, + PhysicalASAPOperatorPayload::ASAP( + ASAPOp::SummaryAgg { + summary_type: FieldDataType::Sketch(..), + .. + } | ASAPOp::SummaryTumblingWindowChain { + summary_type: FieldDataType::Sketch(..), + .. + } + ) + ) + }) +} + +const fn relative(promql: &'static str) -> Query { + Query { + promql, + accuracy_metric: "relative", + } +} + +const QUANTILE: Query = relative("quantile_over_time(0.99, latency[1h])"); +/// The same quantile with a 1% rank target: a KLL instead of a DDSketch. +const QUANTILE_RANK: Query = Query { + promql: "quantile_over_time(0.99, latency[1h])", + accuracy_metric: "rank", +}; +const MAX: Query = relative("max_over_time(latency[1h])"); +const SUM_OVER_TIME: Query = relative("sum by (job) (sum_over_time(latency[1h]))"); +const COUNT_OVER_TIME: Query = relative("sum by (job) (count_over_time(latency[1h]))"); +const SUM_RATE: Query = relative("sum by (job) (rate(requests_total[1h]))"); + +/// With raw data kept at 1 Hz every 10 s, a DDSketch built at query time +/// measures 77.8 CPU-ms/s against the exact plan's 39.0, and stored DDSketch +/// windows 69.7: the exact plan is selected. +#[test] +fn quantile_at_1_hz_every_10_s_with_raw_data_selects_the_exact_plan() { + let (plan, _) = selected(&workload(QUANTILE, ONE_HZ_EVERY_10_S), true); + assert!( + !has_sketch(&plan), + "selected a sketch plan ({:?}), measured 2x slower than exact", + plan_kind(&plan) + ); +} + +/// With raw data kept at 1 Hz every 10 s, a Max accumulator built at query +/// time measures 19.1 CPU-ms/s, slower than the exact plan's 14.6; stored +/// Max windows measure 5.6. Either of the two measured no slower than +/// exact may be selected, not the query-time build. +#[test] +fn max_at_1_hz_every_10_s_with_raw_data_does_not_build_at_query_time() { + let (plan, _) = selected(&workload(MAX, ONE_HZ_EVERY_10_S), true); + let kind = plan_kind(&plan); + assert!( + matches!(kind, PlanKind::ExactScan | PlanKind::StoredStates), + "selected {kind:?}, measured slower than exact" + ); +} + +/// With no raw data at 10 Hz every 60 s, stored DDSketch windows measure +/// 6.1 CPU-ms/s against the exact plan's 76.9 (with raw data): the +/// quantile still keeps a stored sketch. +#[test] +fn quantile_at_10_hz_every_60_s_without_raw_data_keeps_a_stored_sketch() { + let (plan, _) = selected(&workload(QUANTILE, TEN_HZ_EVERY_60_S), false); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates); + assert!(has_sketch(&plan)); +} + +/// Stored exact accumulators measure 3.3–4.0x cheaper than exact at 1 Hz +/// every 10 s and 37–41x at 10 Hz every 60 s, for `sum(rate)` and +/// `sum_over_time`: both keep them, with raw data kept. +#[test] +fn sum_rate_and_sum_over_time_keep_stored_exact_accumulators() { + for setting in [ONE_HZ_EVERY_10_S, TEN_HZ_EVERY_60_S] { + for query in [SUM_RATE, SUM_OVER_TIME] { + let (plan, _) = selected(&workload(query, setting), true); + assert_eq!( + plan_kind(&plan), + PlanKind::StoredStates, + "{query:?} at {setting:?}" + ); + assert!(!has_sketch(&plan), "{query:?} at {setting:?}"); + } + } +} + +/// `sum by (job) (rate(requests_total[1h]))` at 1 Hz every 300 s, raw data +/// kept: the query-time Rate over raw data measures 1.12 CPU-ms/s, stored +/// 300 s Rate windows 0.137, 8x less work and 45x lower latency. The +/// stored windows are selected. +#[test] +fn sum_rate_at_1_hz_every_300_s_keeps_stored_rate_states() { + let (plan, _) = selected(&workload(SUM_RATE, ONE_HZ_EVERY_300_S), true); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates); +} + +/// 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 +/// (`default_cost_plan_ranking_fixtures/.json`). +#[derive(Debug, Clone, Copy)] +enum Alternative { + Stored, + QueryTimeFixture(&'static str), +} + +/// Measured total CPU, in CPU-ms per second of wall time, of the exact +/// plan and of an alternative, for each query and setting. Rate's exact +/// plan is the query-time Rate accumulator over raw data, which the +/// measurement used because the executor cannot run rate's query-time +/// lowering. +const MEASURED_CPU_MS_PER_S: [(Query, Setting, Alternative, f64, f64); 15] = [ + ( + QUANTILE, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 39.047, + 69.718, + ), + ( + QUANTILE, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 76.912, + 6.077, + ), + (MAX, ONE_HZ_EVERY_10_S, Alternative::Stored, 14.557, 5.572), + (MAX, TEN_HZ_EVERY_60_S, Alternative::Stored, 17.763, 0.620), + ( + SUM_OVER_TIME, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 30.101, + 8.758, + ), + ( + SUM_OVER_TIME, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 39.236, + 1.055, + ), + ( + COUNT_OVER_TIME, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 28.740, + 8.746, + ), + ( + COUNT_OVER_TIME, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 37.644, + 1.028, + ), + ( + SUM_RATE, + ONE_HZ_EVERY_10_S, + Alternative::Stored, + 35.126, + 13.079, + ), + ( + SUM_RATE, + TEN_HZ_EVERY_60_S, + Alternative::Stored, + 48.367, + 1.227, + ), + ( + SUM_RATE, + ONE_HZ_EVERY_300_S, + Alternative::Stored, + 1.118, + 0.137, + ), + ( + QUANTILE, + ONE_HZ_EVERY_10_S, + Alternative::QueryTimeFixture("quantile_1_hz_every_10_s_query_time_ddsketch"), + 39.047, + 77.786, + ), + ( + QUANTILE, + TEN_HZ_EVERY_60_S, + Alternative::QueryTimeFixture("quantile_10_hz_every_60_s_query_time_ddsketch"), + 76.912, + 140.022, + ), + ( + QUANTILE_RANK, + ONE_HZ_EVERY_10_S, + Alternative::QueryTimeFixture("quantile_rank_1_hz_every_10_s_query_time_kll"), + 39.118, + 43.483, + ), + ( + MAX, + ONE_HZ_EVERY_10_S, + Alternative::QueryTimeFixture("max_1_hz_every_10_s_query_time_max"), + 14.557, + 19.130, + ), +]; + +/// The plan kept as fixture `name` (`default_cost_plan_ranking_fixtures/.json`). +fn fixture_plan(name: &str) -> PhysicalASAPDAG { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("tests/default_cost_plan_ranking_fixtures") + .join(format!("{name}.json")); + serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap() +} + +/// The exact plan's predicted CPU for `query`: its query-time lowering, or +/// for a rate the query-time Rate accumulator over raw data the +/// measurement ran (fixture `sum_rate_query_time_rate`, Stage 3's exact +/// plan over data at rest before the measured unit costs). +fn exact_cpu(query: Query, setting: Setting) -> f64 { + if !query.promql.contains("rate(") { + return exact_query_time_cpu(query, setting); + } + let plan = fixture_plan("sum_rate_query_time_rate"); + assert_eq!(plan_kind(&plan), PlanKind::QueryTimeExactAccumulator); + predicted_cpu(&plan, &workload(query, setting)) +} + +/// The alternative's predicted CPU for `query` at `setting`. +fn alternative_cpu(query: Query, setting: Setting, alternative: Alternative) -> f64 { + let w = workload(query, setting); + match alternative { + Alternative::Stored => { + let (plan, cpu) = selected(&w, false); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates, "{query:?}"); + cpu + } + Alternative::QueryTimeFixture(name) => { + let plan = fixture_plan(name); + assert!( + matches!( + plan_kind(&plan), + PlanKind::QueryTimeSketch | PlanKind::QueryTimeExactAccumulator + ), + "{name}" + ); + predicted_cpu(&plan, &w) + } + } +} + +/// For every measured query, setting and alternative, the predicted CPU +/// orders the exact plan and the alternative as the measurement does. +#[test] +fn predicted_cpu_orders_exact_and_alternative_plans_as_measured() { + let mut wrong = Vec::new(); + for (query, setting, alternative, measured_exact, measured_alternative) in MEASURED_CPU_MS_PER_S + { + let exact = exact_cpu(query, setting); + let predicted = alternative_cpu(query, setting, alternative); + if (exact > predicted) != (measured_exact > measured_alternative) { + wrong.push(format!( + "{query:?} at {setting:?}, {alternative:?}: predicted exact {:.3} vs {:.3} \ + CPU-ms/s, measured {measured_exact} vs {measured_alternative}", + exact * 1e3, + predicted * 1e3 + )); + } + } + assert!(wrong.is_empty(), "misranked:\n{}", wrong.join("\n")); +} + +/// A query reading stored windows waits for the newest window's +/// compaction: each ingest worker beyond the first builds its own partial +/// copy of that window, merged at query time, one merge per state per extra +/// worker. Example: the stored DDSketch windows of the quantile over +/// 100,000 series sampled every 10 ms (10 M rows per second, at 210 ns per +/// row more than one core of ingestion work) merge at least one extra copy +/// of each of the window's 100,000 states, and a DDSketch merge measures +/// 1.8–2.5 µs: at least 180 ms. One operation (1 ns) per merge would +/// predict 0.2 ms. +#[test] +fn compaction_latency_charges_each_merge_its_family_s_merge_time() { + let (plan, _) = selected(&workload(QUANTILE, TEN_HZ_EVERY_60_S), false); + assert_eq!(plan_kind(&plan), PlanKind::StoredStates); + assert!(has_sketch(&plan)); + let mut heavy = serde_json::to_value(workload(QUANTILE, TEN_HZ_EVERY_60_S)).unwrap(); + let data = &mut heavy["data_workload"]; + data["arrival"]["ingesting"]["rows_per_second"] = serde_json::json!(10_000_000.0); + data["sources"]["latency"]["series_count"] = serde_json::json!(100_000); + data["sources"]["latency"]["sample_interval"] = serde_json::json!(10); + let heavy: PlanningWorkload = serde_json::from_value(heavy).unwrap(); + let entries: Vec<_> = heavy.query_workload.entries().collect(); + let (cost, _) = asap_cost::plan_cost( + &plan, + &entries, + heavy.data_workload.as_ref().unwrap(), + &CostInputs::default(), + ) + .unwrap(); + let compaction_ms = cost.usage.latency_per_query[0].compaction_ms; + assert!( + compaction_ms >= 100_000.0 * 1.8e-3, + "predicted compaction {compaction_ms} ms, measured merges take at least 180 ms" + ); +} + +/// Partial copies of an ingestion-time node that holds no states (raw rows +/// a time range keeps, or the values of an exact `Aggregate`) are +/// concatenated, not merged: the executor merges ingest shards only of +/// summary state columns. So reading one adds no compaction latency. +/// Example: `max_over_time(latency[5m])` over 100,000 series sampled every +/// 1 ms (100 M rows per second, more than one core of ingestion work) with +/// its 5m time range, and then also its per-series max, kept at ingestion +/// time: an exact accumulator's merge (230 ns) per row of the 5m window +/// read would predict seconds of compaction for no merge work. +#[test] +fn compaction_latency_is_zero_for_ingestion_time_nodes_holding_no_states() { + use asap_types::ir::properties::ExecutionDataState; + use asap_types::ir::NonASAPOp; + use asap_types::stages::{Lifetime, StorageMedium}; + let query = Query { + promql: "max_over_time(latency[5m])", + accuracy_metric: "relative", + }; + let mut heavy = serde_json::to_value(workload(query, TEN_HZ_EVERY_60_S)).unwrap(); + let data = &mut heavy["data_workload"]; + data["arrival"]["ingesting"]["rows_per_second"] = serde_json::json!(100_000_000.0); + data["sources"]["latency"]["series_count"] = serde_json::json!(100_000); + data["sources"]["latency"]["sample_interval"] = serde_json::json!(1); + let heavy: PlanningWorkload = serde_json::from_value(heavy).unwrap(); + let roots = lower_promql_roots(&heavy).unwrap(); + let (exact, _) = + asap_types::test_support::forwarders::physical_asap_dag_at_query_time_with_node_ids(&roots) + .unwrap(); + let entries: Vec<_> = heavy.query_workload.entries().collect(); + let data = heavy.data_workload.as_ref().unwrap(); + // Scan, then the 5m `TimeRange`, then the per-series max `Aggregate`. + let is_time_range = |payload: &PhysicalASAPOperatorPayload| { + matches!( + payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::TimeRange { .. }) + ) + }; + let is_aggregate = |payload: &PhysicalASAPOperatorPayload| { + matches!( + payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Aggregate { .. }) + ) + }; + let kept_at_ingestion = |up_to_aggregate: bool| { + let mut plan = exact.clone(); + let ingested: Vec<_> = plan + .nodes + .iter() + .filter(|node| !is_aggregate(&node.payload) || up_to_aggregate) + .map(|node| node.id) + .collect(); + for node in &mut plan.nodes { + if !ingested.contains(&node.id) { + continue; + } + node.output_state = ExecutionDataState::INGESTION_ROWS; + if is_time_range(&node.payload) || is_aggregate(&node.payload) { + node.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); + node.retention_ms = Some(300_000); + } + } + for edge in &mut plan.edges { + if ingested.contains(&edge.producer) { + *edge.data_state_mut() = ExecutionDataState::INGESTION_ROWS; + } + } + plan + }; + for (kept, plan) in [ + ("time range", kept_at_ingestion(false)), + ("time range and max", kept_at_ingestion(true)), + ] { + let (cost, _) = + asap_cost::plan_cost(&plan, &entries, data, &CostInputs::default()).unwrap(); + let ingest_ms_per_second: f64 = cost + .per_node + .values() + .filter(|node| node.timing == ExecutionTiming::IngestionTime) + .map(|node| node.cpu_core_seconds_per_second * 1_000.0) + .sum(); + assert!( + ingest_ms_per_second > 1_000.0, + "{kept}: ingestion takes {ingest_ms_per_second} ms per second, one core" + ); + assert_eq!( + cost.usage.latency_per_query[0].compaction_ms, 0.0, + "{kept} kept at ingestion time" + ); + } +} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/max_1_hz_every_10_s_query_time_max.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/max_1_hz_every_10_s_query_time_max.json new file mode 100644 index 000000000..235458873 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/max_1_hz_every_10_s_query_time_max.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"ExactAggregate":["Max","Max"]},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Max","Max"]},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"FinalizeExactAccumulator":{"child":2}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Max","Max"]},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[3],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_10_hz_every_60_s_query_time_ddsketch.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_10_hz_every_60_s_query_time_ddsketch.json new file mode 100644 index 000000000..77a14e899 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_10_hz_every_60_s_query_time_ddsketch.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"Sketch":"DDSketch"},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":{"OneEvaluation":"Memory"},"parameters":{"DDSketch":{"alpha":0.01}},"output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"SummaryEstimate":{"summary_input":2,"query":{"Quantile":{"q":0.99}}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":4,"payload":{"NonASAP":{"Project":{"cols":[{"alias":"ts","expr":{"Column":0}},{"alias":"value","expr":{"Column":1}},{"alias":"$promql_series_identity","expr":{"Column":2}}],"child":3}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[4],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_1_hz_every_10_s_query_time_ddsketch.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_1_hz_every_10_s_query_time_ddsketch.json new file mode 100644 index 000000000..0a3062ca8 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_1_hz_every_10_s_query_time_ddsketch.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"Sketch":"DDSketch"},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","parameters":{"DDSketch":{"alpha":0.01}},"output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"SummaryEstimate":{"summary_input":2,"query":{"Quantile":{"q":0.99}}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":4,"payload":{"NonASAP":{"Project":{"cols":[{"alias":"ts","expr":{"Column":0}},{"alias":"value","expr":{"Column":1}},{"alias":"$promql_series_identity","expr":{"Column":2}}],"child":3}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"DDSketch"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[4],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_rank_1_hz_every_10_s_query_time_kll.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_rank_1_hz_every_10_s_query_time_kll.json new file mode 100644 index 000000000..755f5c035 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/quantile_rank_1_hz_every_10_s_query_time_kll.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"latency"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"Sketch":"Kll"},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","parameters":{"Kll":{"k":547}},"output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"Kll"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"SummaryEstimate":{"summary_input":2,"query":{"Quantile":{"q":0.99}}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}},{"id":4,"payload":{"NonASAP":{"Project":{"cols":[{"alias":"ts","expr":{"Column":0}},{"alias":"value","expr":{"Column":1}},{"alias":"$promql_series_identity","expr":{"Column":2}}],"child":3}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"Sketch":"Kll"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"quantile","dtype":{"Plain":"float64"},"nullable":false},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[2,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[4],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/sum_rate_query_time_rate.json b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/sum_rate_query_time_rate.json new file mode 100644 index 000000000..67e59c809 --- /dev/null +++ b/crates/integration-tests/tests/default_cost_plan_ranking_fixtures/sum_rate_query_time_rate.json @@ -0,0 +1 @@ +{"nodes":[{"id":0,"payload":{"NonASAP":{"Scan":{"source":{"TimeSeries":{"metric":"requests_total"}},"predicates":[],"schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"retention_ms":3600000},{"id":1,"payload":{"NonASAP":{"TimeRange":{"range":{"secs":3600,"nanos":0},"kind":"range","child":0}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}},{"id":2,"payload":{"ASAP":{"SummaryAgg":{"child":1,"summary_type":{"ExactAggregate":["Rate","Rate"]},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":"PerEntity","filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Rate","Rate"]},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}},{"id":3,"payload":{"ASAP":{"FinalizeExactAccumulator":{"child":2}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true}},{"id":4,"payload":{"ASAP":{"SummaryAgg":{"child":3,"summary_type":{"ExactAggregate":["Sum","Sum"]},"input":{"item":null,"weight":{"Column":"SampleValue"}},"reduction":{"Reduce":[2]},"filter":null}}},"output_state":{"timing":"query_time","primitive":"SummaryState"},"lifetime":"None","output_schema":{"fields":[{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"state","dtype":{"ExactAggregate":["Sum","Sum"]},"nullable":false}],"time_index":null,"unique_keys":[[0]],"closed":true}},{"id":5,"payload":{"ASAP":{"FinalizeExactAccumulator":{"child":4}}},"output_state":{"timing":"query_time","primitive":"Raw"},"lifetime":"None","output_schema":{"fields":[{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"sum","dtype":{"Plain":"float64"},"nullable":false}],"time_index":null,"unique_keys":[[0]],"closed":true}}],"edges":[{"producer":0,"consumer":1,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":1,"consumer":2,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":2,"consumer":3,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"state","dtype":{"ExactAggregate":["Rate","Rate"]},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":3,"consumer":4,"role":"Input","intermediate_schema":{"fields":[{"name":"ts","dtype":{"Plain":"timestamp"},"nullable":false},{"name":"value","dtype":{"Plain":"float64"},"nullable":false},{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"$promql_series_identity","dtype":{"Plain":"utf8"},"nullable":false}],"time_index":0,"unique_keys":[[3,0]],"closed":true},"data_state":{"timing":"query_time","primitive":"Raw"},"grouping":"NotApplicable","window":"NotApplicable"},{"producer":4,"consumer":5,"role":"Input","intermediate_schema":{"fields":[{"name":"job","dtype":{"Plain":"utf8"},"nullable":true},{"name":"state","dtype":{"ExactAggregate":["Sum","Sum"]},"nullable":false}],"time_index":null,"unique_keys":[[0]],"closed":true},"data_state":{"timing":"query_time","primitive":"SummaryState"},"grouping":"NotApplicable","window":"NotApplicable"}],"roots":[5],"scalar_roots":[]} diff --git a/crates/integration-tests/tests/filtered_aggregates.rs b/crates/integration-tests/tests/filtered_aggregates.rs index eaa96aa4f..c8f6cfadd 100644 --- a/crates/integration-tests/tests/filtered_aggregates.rs +++ b/crates/integration-tests/tests/filtered_aggregates.rs @@ -12,8 +12,11 @@ use asap_executor::{ values::{Batch, Value}, }; use asap_frontend_sql::{lower_sql, SqlCatalog}; -use asap_integration_tests::planning::{logical_candidates_ignoring_guarantees, selected_dag}; +use asap_integration_tests::planning::{ + logical_candidates_ignoring_guarantees, logical_candidates_ignoring_guarantees_on, selected_dag, +}; use asap_types::accuracy::AccuracyTarget; +use asap_types::deployment_capabilities::DeploymentCapabilities; use asap_types::ir::schema::FieldDataType; use asap_types::ir::schema::{DataType, Field, Schema}; use asap_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, Predicate}; @@ -267,11 +270,20 @@ async fn hand_filtered_kll_compiles_and_executes() { } /// The KLL candidates of `sql`, unchanged, every guarantee accepted (as -/// [`hand_filtered`]). +/// [`hand_filtered`]), on an executor that builds only KLL. async fn kll_candidates(sql: &str) -> Vec> { let target = AccuracyTarget::max_error(0.01).failure_probability(0.01); + let kll_only = DeploymentCapabilities { + supported_summaries: Some( + asap_executor::supported_summary_families() + .into_iter() + .filter(|family| family.summary_type == SketchAlgorithm::Kll) + .collect(), + ), + ..DeploymentCapabilities::default() + }; let roots: Vec<_> = - logical_candidates_ignoring_guarantees(lower(sql).await, target) + logical_candidates_ignoring_guarantees_on(lower(sql).await, target, &kll_only) .into_iter() .filter(|root| { OperatorNode::reachable(root).iter().any(|n| { diff --git a/crates/integration-tests/tests/main.rs b/crates/integration-tests/tests/main.rs index bebcbab67..9ee627c25 100644 --- a/crates/integration-tests/tests/main.rs +++ b/crates/integration-tests/tests/main.rs @@ -9,6 +9,7 @@ mod planning_common; mod aggregate; mod binary_op; mod crate_graph; +mod default_cost_plan_ranking; mod exact_reduce_execution; mod filtered_aggregates; mod finer_aggregate_reuse; diff --git a/crates/integration-tests/tests/plan_sharing.rs b/crates/integration-tests/tests/plan_sharing.rs index f113ca7e5..1c1508c64 100644 --- a/crates/integration-tests/tests/plan_sharing.rs +++ b/crates/integration-tests/tests/plan_sharing.rs @@ -1,7 +1,10 @@ //! Workloads whose queries share computation, planned through the production //! stages: adding a query that can read an existing result must not cost //! more than planning the first query alone. -use crate::planning_common::{cost, fuzz_workload}; +use crate::planning_common::{cost, cost_with, fuzz_workload, selected_plan}; +use asap_cost::CostInputs; +use asap_types::ir::NonASAPOp; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload as Payload; use serde_json::json; /// Deriving the coarse sum costs only a sum over the 150 fine groups. @@ -49,12 +52,37 @@ fn slower_repeat_reuses_faster_sum() { ); } -/// The filtered query avoids its independent scan and aggregation. +/// The filtered query reads the by-city quantile's summary states instead +/// of its own scan and aggregation: with summaries' CPU costed free (so +/// the sketch plans are selected, as under the earlier unit costs), it +/// adds at most 0.1% to the by-city quantile's cost. +/// +/// Under the default unit costs both quantiles are exact plans, and no +/// rule reads the filtered quantile from the by-city exact values +/// (#1358): the filtered query pays its own scan and sort. The last +/// assertion pins that; when the rule exists it fails, and should then +/// expect the reuse. #[test] fn grouped_filtered_quantile_plans() { + let settings = asap_integration_tests::planning::summary_cpu_free(); + let w = fuzz_workload("percentile_filtered_and_by_city", true); + let mut grouped = w.clone(); + grouped.queries = vec![w.queries[2].clone()]; + let grouped_cost = cost_with(&grouped, &settings); + let mut filtered_and_grouped = w.clone(); + filtered_and_grouped.queries.remove(0); + let together = cost_with(&filtered_and_grouped, &settings); assert!( - cost(&fuzz_workload("percentile_filtered_and_by_city", true)) < 0.25, - "filtered query must reuse the grouped result" + together <= grouped_cost * 1.001, + "filtered query must reuse the grouped result: {together} vs {grouped_cost}" + ); + let defaults = CostInputs::default(); + let grouped_default = cost_with(&grouped, &defaults); + let together_default = cost_with(&filtered_and_grouped, &defaults); + assert!( + together_default > grouped_default * 1.001, + "under the default costs the filtered query now reuses the grouped exact result \ + ({together_default} vs {grouped_default}): update this test" ); } @@ -117,11 +145,33 @@ fn identical_exact_percentiles_share_work() { ); } -/// Different ranks share the source window but still compute their own values. +/// Different ranks share the source window but still compute their own +/// values: the plan scans the table and selects the window once, and both +/// percentiles read that one node. #[test] fn different_exact_percentiles_share_the_input_window() { let mut related = fuzz_workload("identical_percentiles", false); related.queries[1].entry.query.0 = related.queries[1].entry.query.0.replace("0.99", "0.5"); - let related_cost = cost(&related); - assert!(related_cost <= 0.0883, "{related_cost}"); + let plan = selected_plan(&related, &CostInputs::default()); + let scans: Vec<_> = plan + .nodes + .iter() + .filter(|node| matches!(node.payload, Payload::NonASAP(NonASAPOp::Scan { .. }))) + .collect(); + assert_eq!(scans.len(), 1, "one scan of the table: {plan:#?}"); + let windows: Vec<_> = plan + .nodes + .iter() + .filter(|node| matches!(node.payload, Payload::NonASAP(NonASAPOp::TimeRange { .. }))) + .collect(); + assert!(windows.len() <= 1, "one input window: {plan:#?}"); + // The node both percentiles read: the window, or the scan if the + // window is a filter over it. + let shared = windows.first().unwrap_or(&scans[0]).id; + let readers = plan + .edges + .iter() + .filter(|edge| edge.producer == shared) + .count(); + assert_eq!(readers, 2, "both percentiles read node {shared}: {plan:#?}"); } diff --git a/crates/integration-tests/tests/planning_common/mod.rs b/crates/integration-tests/tests/planning_common/mod.rs index 809bcbc80..fe0ae4df0 100644 --- a/crates/integration-tests/tests/planning_common/mod.rs +++ b/crates/integration-tests/tests/planning_common/mod.rs @@ -7,6 +7,7 @@ use asap_devtools::planning_stages::{ lower_promql_roots, lower_sql_roots, run_search_over, run_stages_until, sql_catalog, RunUntil, }; use asap_planner::{PlanSearchSettings, PlanningModels}; +use asap_types::plan::physical_asap_dag::PhysicalASAPDAG; use asap_types::stages::LogicalASAPMemoCandidates; use asap_types::workload::PlanningWorkload; use serde_json::{json, Value}; @@ -48,13 +49,28 @@ pub fn with_queries(mut w: FuzzWorkload, queries: &[String]) -> FuzzWorkload { /// The selected plan's cost per second for `w`. pub fn cost(w: &FuzzWorkload) -> f64 { + cost_with(w, &CostInputs::default()) +} + +/// [`cost`] under `settings`. +pub fn cost_with(w: &FuzzWorkload, settings: &CostInputs) -> f64 { + let plan = selected_plan(w, settings); + let planning = w.planning_workload(); + let entries: Vec<_> = planning.query_workload.entries().collect(); + let value = + asap_integration_tests::planning::plan_cost_per_second(&plan, &entries, &w.data, settings); + println!("cost={value}"); + value +} + +/// The plan the production stages select for `w` under `settings`. +pub fn selected_plan(w: &FuzzWorkload, settings: &CostInputs) -> PhysicalASAPDAG { let roots = Planner::new(FuzzSettings::default()).lower(w).unwrap(); let planning = w.planning_workload(); let entries: Vec<_> = planning.query_workload.entries().collect(); - let settings = CostInputs::default(); let deployment = w.deployment(); let models = PlanningModels::builtin() - .with_cost_inputs(&settings) + .with_cost_inputs(settings) .with_deployment_capabilities(&deployment); let run = run_search_over( roots, @@ -65,10 +81,7 @@ pub fn cost(w: &FuzzWorkload) -> f64 { ) .unwrap(); let (plan, _) = run.run.selection.as_ref().unwrap(); - let value = - asap_integration_tests::planning::plan_cost_per_second(plan, &entries, &w.data, &settings); - println!("cost={value}"); - value + plan.clone() } /// Pass 2's output for PromQL `queries`, each with its accuracy target, diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index 7f05e3911..aa4696abb 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -41,10 +41,14 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { .collect() } -// The production entrypoint must find a sketch rollup under the viewer budget. +// The production entrypoint must find a sketch rollup cheaper than +// computing both distinct counts exactly from raw data at query time. #[test] fn production_distinct_rollup_finds_shared_kmv() { 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(); @@ -68,9 +72,17 @@ fn production_distinct_rollup_finds_shared_kmv() { 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!( - asap_integration_tests::planning::plan_cost_per_second(plan, &entries, data, &settings) - < 2.0e-6 + cost_per_second < exact_cost_per_second, + "KMV rollup {cost_per_second} vs exact {exact_cost_per_second}" ); let roots = plan.query_roots().unwrap(); let builds: HashSet<_> = nodes(&roots) diff --git a/crates/integration-tests/tests/stage3_objective.rs b/crates/integration-tests/tests/stage3_objective.rs index 51100e33f..016ca54d1 100644 --- a/crates/integration-tests/tests/stage3_objective.rs +++ b/crates/integration-tests/tests/stage3_objective.rs @@ -8,7 +8,9 @@ use asap_devtools::planning_stages::{lower_promql_roots, run_planning_stages_ove use asap_planner::{DeploymentCapabilities, PlanningModels}; use asap_types::ir::properties::ExecutionTiming; use asap_types::ir::NonASAPOp; -use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPOperatorPayload}; +use asap_types::plan::physical_asap_dag::{ + PhysicalASAPDAG, PhysicalASAPNodeId, PhysicalASAPOperatorPayload, +}; use asap_types::stages::Lifetime; use asap_types::workload::PlanningWorkload; @@ -89,6 +91,77 @@ fn assert_equal(name: &str, (objective, plan_cost): (f64, f64)) { ); } +/// The ids of the plan's `Scan` nodes, each with how many of the plan's +/// roots (queries) read it, directly or through other nodes: a `Scan` read +/// by 2 of 2 queries is one scan they share. +fn scans_and_their_reading_query_counts( + plan: &PhysicalASAPDAG, +) -> Vec<(PhysicalASAPNodeId, usize)> { + let reads = |root: PhysicalASAPNodeId, target: PhysicalASAPNodeId| { + let mut pending = vec![root]; + let mut seen = vec![root]; + while let Some(node) = pending.pop() { + if node == target { + return true; + } + for edge in plan.edges.iter().filter(|edge| edge.consumer == node) { + if !seen.contains(&edge.producer) { + seen.push(edge.producer); + pending.push(edge.producer); + } + } + } + false + }; + plan.nodes + .iter() + .filter(|node| { + matches!( + node.payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { .. }) + ) + }) + .map(|node| { + let readers = plan + .roots + .iter() + .filter(|&&root| reads(root, node.id)) + .count(); + (node.id, readers) + }) + .collect() +} + +/// Asserts that `plan` has one `Scan`, read by all `query_count` queries. +fn assert_one_scan_read_by_every_query(plan: &PhysicalASAPDAG, query_count: usize) { + assert_eq!( + scans_and_their_reading_query_counts(plan) + .iter() + .map(|&(_, readers)| readers) + .collect::>(), + vec![query_count], + "expected one Scan read by all {query_count} queries" + ); +} + +/// `plan_cost` per second of the plan Stage 3 selects for each of the +/// `queries` planned alone. +fn costs_of_queries_planned_alone( + queries: &[serde_json::Value], + workload_of: impl Fn(&[serde_json::Value]) -> PlanningWorkload, + models: impl Fn() -> PlanningModels<'static>, +) -> Vec { + queries + .iter() + .map(|query| { + let alone = workload_of(std::slice::from_ref(query)); + selected_plan_objective_and_plan_cost_with(&alone, models(), &CostInputs::default()) + .1 + .1 + }) + .collect() +} + /// Unit costs that charge what the default ones leave at 0: 50 µs of /// runtime overhead per query request and 1e-9 per byte-second on disk. fn overhead_and_disk_costs() -> CostInputs { @@ -193,12 +266,14 @@ 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. +/// 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 { CostInputs { cost_model: CostModel::UsageOverTime(ResourceUnitCost { - cost_per_cpu_op: 1.1244e-14, - cost_per_scanned_byte: 1.1244e-15, + cost_per_cpu_op: 5.622e-15, + cost_per_scanned_byte: 5.622e-16, cost_per_memory_byte_second: 1.150183379650116e-15, cost_per_disk_byte_second: 3.1e-17, ..ResourceUnitCost::illustrative() @@ -212,9 +287,9 @@ fn small_disk_costs() -> CostInputs { /// 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. With the scale on the raw-data -/// variable's objective coefficient instead, the solver dropped the -/// unscaled coefficients and selected that plan, 57% dearer. +/// is free, costed at the disk price (that one is 50% dearer). With the +/// scale on the raw-data variable's objective coefficient instead, the +/// solver dropped the unscaled coefficients and selected that plan. #[test] fn raw_data_on_disk_costing_less_than_the_solver_tolerance_still_steers_selection() { let w = workload(&[ @@ -244,29 +319,43 @@ fn raw_data_on_disk_costing_less_than_the_solver_tolerance_still_steers_selectio /// Two windowed quantiles over one metric (1 m and 5 m, every 10 s; 10,000 /// series every 15 s), accuracy not enforced: the selected plan shares one /// query-time scan, whose execution reads the 5 m window once and retains -/// it ($h_v$, constraint (R)), at most 0.144/s. +/// it ($h_v$, constraint (R)). It costs less than the two queries planned +/// alone, which each read the metric once. #[test] fn the_objective_is_the_plan_cost_on_a_shared_scan() { - let w: PlanningWorkload = serde_json::from_value(serde_json::json!({ - "query_workload": { - "language": "promql", - "query_batch": null, - "repeating_queries": [ - every_10s("quantile_over_time(0.5, data[1m])"), - every_10s("quantile_over_time(0.9, data[5m])"), - ], - }, - "data_workload": { - "arrival": {"ingesting": {"rows_per_second": 666.67}}, - "sources": {"data": {"series_count": 10000, "metric_type": "gauge", "sample_interval": 15000}}, - }, - })) - .unwrap(); - let models = PlanningModels::builtin().with_ignore_accuracy_guarantees(true); - let (_, (objective, plan_cost)) = - selected_plan_objective_and_plan_cost_with(&w, models, &CostInputs::default()); + let queries = [ + every_10s("quantile_over_time(0.5, data[1m])"), + every_10s("quantile_over_time(0.9, data[5m])"), + ]; + let workload_of = |queries: &[serde_json::Value]| -> PlanningWorkload { + serde_json::from_value(serde_json::json!({ + "query_workload": { + "language": "promql", + "query_batch": null, + "repeating_queries": queries, + }, + "data_workload": { + "arrival": {"ingesting": {"rows_per_second": 666.67}}, + "sources": {"data": {"series_count": 10000, "metric_type": "gauge", "sample_interval": 15000}}, + }, + })) + .unwrap() + }; + let models = || PlanningModels::builtin().with_ignore_accuracy_guarantees(true); + let (plan, (objective, plan_cost)) = selected_plan_objective_and_plan_cost_with( + &workload_of(&queries), + models(), + &CostInputs::default(), + ); assert_equal("shared scan", (objective, plan_cost)); - assert!(plan_cost <= 0.144, "{plan_cost}"); + assert_one_scan_read_by_every_query(&plan, 2); + let alone: f64 = costs_of_queries_planned_alone(&queries, workload_of, models) + .iter() + .sum(); + assert!( + plan_cost < alone, + "shared {plan_cost} is not below the two queries planned alone, {alone}" + ); } /// The o11y benchmark's q21: `sum(rate(..[5m] offset 1h))`, a 1 h range @@ -503,26 +592,63 @@ fn the_objective_is_the_plan_cost_on_one_off_queries_over_years() { let (objective, plan_cost) = selected_plan_objective_and_plan_cost_with(&w, models, &CostInputs::default()).1; assert_equal("one-off queries over years", (objective, plan_cost)); - assert!(objective < 30_000.0, "objective {objective}"); + // 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. + let capabilities = DeploymentCapabilities { + raw_data_retained: false, + ..Default::default() + }; + let views_only = PlanningModels::builtin() + .with_ignore_accuracy_guarantees(true) + .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()); + assert!( + plan_cost < views_only_plan_cost, + "selected {plan_cost} is not below the ingestion-time views plan, {views_only_plan_cost}" + ); } /// `sum_over_time(m[5m] @ end())` and `sum_over_time(m[5m])`, both instant /// every 60 s: an instant query's `@ end()` is its evaluation time, so the /// anchored input runs in the evaluation context and shares `m[5m]` with -/// the plain query, in the MILP and in `plan_cost` alike (0.0360/s; in a -/// request context of its own it cost 0.0620/s). +/// the plain query, in the MILP and in `plan_cost` alike. The two cost less +/// than planned alone, which is what any plan that does not share `m[5m]` +/// (such as one in a request context of its own) costs. #[test] fn an_instant_at_end_input_shares_the_evaluation_context() { - let w = over_m(vec![ + let queries = [ requested("sum_over_time(m[5m] @ end())", 60_000, None), requested("sum_over_time(m[5m])", 60_000, None), - ]); - let (objective, plan_cost) = objective_and_plan_cost(&w, &CostInputs::default(), true); + ]; + let (plan, (objective, plan_cost)) = selected_plan_objective_and_plan_cost_with( + &over_m(queries.to_vec()), + PlanningModels::builtin(), + &CostInputs::default(), + ); assert_equal( "instant @ end() beside a plain query", (objective, plan_cost), ); - assert!(objective < 0.04, "objective {objective}"); + assert_one_scan_read_by_every_query(&plan, 2); + let alone: f64 = costs_of_queries_planned_alone( + &queries, + |queries| over_m(queries.to_vec()), + PlanningModels::builtin, + ) + .iter() + .sum(); + assert!( + plan_cost < alone, + "shared {plan_cost} is not below the two queries planned alone, {alone}" + ); } /// `sum_over_time(m[1m])` every 20 s at phase 10 s and diff --git a/crates/planner/src/tutorials/_06_extending.md b/crates/planner/src/tutorials/_06_extending.md index 618c958c9..419571e84 100644 --- a/crates/planner/src/tutorials/_06_extending.md +++ b/crates/planner/src/tutorials/_06_extending.md @@ -11,8 +11,9 @@ recipes in [extend ASAP-aware mapping](https://github.com/ProjectASAP/ASAPPlanne `parameter_list`) and its capability row (`SketchAlgorithm::capability`, read by `SummaryTypeCapability`); - offering it in Pass 1 (`ALGORITHM_PREFERENCE`, `summary_candidates`); - - its error model (`theoretical_guarantee`) and its cost - (`sketch_shape`); + - its error model (`theoretical_guarantee`), its state bytes + (`sketch_state_bytes`, `crates/cost/src/node/state_bytes/mod.rs`) and + its per-row and per-merge CPU (`crates/cost/src/node/operations_per_unit.rs`); - its executor kernel (`summary_kernels`, `EVALUATED_READOUTS`). - Adding a reuse rule: a memo rule under Pass 2's `rules` that adds groups (`Memo::find_or_add_group`) or candidates (`Memo::add_candidate`), called diff --git a/crates/planner/tests/metricsql_workloads.rs b/crates/planner/tests/metricsql_workloads.rs index 2d2dd1707..c0197efbe 100644 --- a/crates/planner/tests/metricsql_workloads.rs +++ b/crates/planner/tests/metricsql_workloads.rs @@ -34,17 +34,28 @@ const QUERIES: [&str; 4] = [ r#"sum(latency{job="j1"})"#, ]; +/// Data arriving at 2,000 rows per second: the plans the Planner selects +/// for these queries in production. +const INGESTING: &str = r#"{"ingesting": {"rows_per_second": 2000.0}}"#; + +/// Data at rest: what [`query_answers`] runs over, since it answers each +/// root at query time from the raw samples alone. With data arriving, +/// `increase` selects windows maintained at ingestion time, which this +/// harness never fills. +const AT_REST: &str = r#""at_rest""#; + /// `query` written in `language` ("promql" or "metricsql"), every 10 s /// within relative error 0.01, over 1,000 `requests_total` counter series -/// and 1,000 `latency` gauge series, each sampled every second. -fn workload(language: &str, query: &str) -> PlanningWorkload { +/// and 1,000 `latency` gauge series, each sampled every second, with the +/// data `arrival` ([`INGESTING`] or [`AT_REST`]). +fn workload(language: &str, query: &str, arrival: &str) -> PlanningWorkload { serde_json::from_str(&format!( r#"{{"query_workload": {{"language": "{language}", "repeating_queries": [{{ "query": {query:?}, "demand": {{"fixed_interval_at": {{"interval": 10000, "evaluation_phase": 0}}}}, "requirements": {{"accuracy": {{"max_error": 0.01}}}}, "predictability": {{"predictable": {{"known_at": null}}}}}}]}}, - "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 2000.0}}}}, + "data_workload": {{"arrival": {arrival}, "sources": {{ "requests_total": {{"series_count": 1000, "metric_type": "counter", "sample_interval": 1000}}, @@ -54,10 +65,10 @@ fn workload(language: &str, query: &str) -> PlanningWorkload { .unwrap() } -/// The built-in plan of `query` written in MetricsQL, or the error the -/// Planner returned. -async fn plan_metricsql(query: &str) -> Result { - let workload = workload("metricsql", query); +/// The built-in plan of `query` written in MetricsQL, with the data +/// `arrival`, or the error the Planner returned. +async fn plan_metricsql(query: &str, arrival: &str) -> Result { + let workload = workload("metricsql", query, arrival); plan_installable(UserInput::new( &workload, FrontendInput::Metricsql, @@ -68,9 +79,9 @@ async fn plan_metricsql(query: &str) -> Result { .map_err(|e| e.to_string()) } -/// The built-in plan of `query` written in PromQL. -async fn plan_promql(query: &str) -> InstallablePlan { - let workload = workload("promql", query); +/// The built-in plan of `query` written in PromQL, with the data `arrival`. +async fn plan_promql(query: &str, arrival: &str) -> InstallablePlan { + let workload = workload("promql", query, arrival); plan_installable(UserInput::new( &workload, FrontendInput::Promql, @@ -118,7 +129,7 @@ fn query_answers(plan: &InstallablePlan, samples: &[RawDataSample]) -> Vec Vec InstallablePlan { let entries: Vec = queries @@ -572,7 +592,7 @@ async fn plan_counter_queries( .collect(); let workload: PlanningWorkload = serde_json::from_str(&format!( r#"{{"query_workload": {{"language": "promql", "repeating_queries": [{}]}}, - "data_workload": {{"arrival": {{"ingesting": {{"rows_per_second": 1000.0}}}}, + "data_workload": {{"arrival": {arrival}, "sources": {{"requests_total": {{"series_count": 1000, "metric_type": "counter", "sample_interval": 1000}}}}}}}}"#, entries.join(",") @@ -712,7 +732,8 @@ async fn counter_query_mismatches( let mut mismatches = Vec::new(); for raw_data_retained in [true, false] { for (query, expected) in counter_queries() { - let plan = plan_counter_queries(&[query], 10_000, raw_data_retained).await; + let plan = + plan_counter_queries(&[query], 10_000, COUNTERS_INGESTING, raw_data_retained).await; let answered = answer(&plan, &samples).remove(0); let matches = answered.len() == expected.len() && answered.iter().zip(&expected).all(|(answer, expected)| { @@ -751,36 +772,37 @@ async fn counter_rates_and_increases_answer_at_query_time_from_raw_samples() { } /// `rate(requests_total[5m] offset 1m)` and `rate(requests_total[5m])`, -/// each every minute, share a tumbling-window chain of 1-minute windows -/// that the plan builds at query time. Installed, the query run builds it -/// from the raw samples, each update at its sample's own time: over -/// [`counter_samples`] (four samples per window) both queries answer the -/// hand-worked rates `(i + 1) * 20 / 300` per series. `(T - 6m, T - 1m]` -/// holds steps 9..=28, 20 samples as `(T - 5m, T]` does. -#[tokio::test] -async fn counter_rates_over_a_shared_query_time_chain_answer_as_installed() { +/// each every minute, planned together with data `arrival`: the plan with +/// its shared tumbling-window chain (1-minute windows) built at `timing`. +async fn plan_shared_rate_chain(arrival: &str, timing: ExecutionTiming) -> InstallablePlan { let plan = plan_counter_queries( &[ "rate(requests_total[5m] offset 1m)", "rate(requests_total[5m])", ], 60_000, + arrival, true, ) .await; assert!( plan.dag.nodes.iter().any(|node| matches!( node.payload, - asap_types::ir::Operator::ASAP( - asap_types::ir::ASAPOp::SummaryTumblingWindowChain { .. } - ) - ) && node.output_state.timing - == asap_types::ir::properties::ExecutionTiming::QueryTime), - "the plan builds a chain at query time: {:#?}", + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { .. }) + ) && node.output_state.timing == timing), + "the plan builds a chain at {timing:?}: {:#?}", plan.dag.nodes ); + plan +} + +/// The answers of [`plan_shared_rate_chain`]'s plan as installed, over +/// [`counter_samples`] (four samples per window): both queries answer the +/// hand-worked rates `(i + 1) * 20 / 300` per series. `(T - 6m, T - 1m]` +/// holds steps 9..=28, 20 samples as `(T - 5m, T]` does. +fn assert_shared_rate_chain_answers(plan: &InstallablePlan) { let rates: Vec = (1..=4).map(|i| i as f64 * 20.0 / 300.0).collect(); - for answered in installed_answers(&plan, &counter_samples()) { + for answered in installed_answers(plan, &counter_samples()) { let values: Vec = answered.iter().map(|(_, value)| *value).collect(); assert_eq!(values.len(), rates.len(), "{answered:?}"); for (value, rate) in values.iter().zip(&rates) { @@ -789,6 +811,26 @@ async fn counter_rates_over_a_shared_query_time_chain_answer_as_installed() { } } +/// With the counter samples at rest nothing arrives, so the two rates' +/// shared tumbling-window chain is built at query time. Installed, the +/// query run builds it from the raw samples, each update at its sample's +/// own time, and both queries answer the hand-worked rates. +#[tokio::test] +async fn counter_rates_over_a_shared_query_time_chain_answer_as_installed() { + let plan = plan_shared_rate_chain(COUNTERS_AT_REST, ExecutionTiming::QueryTime).await; + assert_shared_rate_chain_answers(&plan); +} + +/// With the counter samples arriving, the two rates' shared +/// tumbling-window chain is cheaper stored: the plan builds it at +/// ingestion time into a materialized view, and both queries, reading the +/// view, answer the same hand-worked rates. +#[tokio::test] +async fn counter_rates_over_a_shared_ingestion_time_chain_answer_as_installed() { + let plan = plan_shared_rate_chain(COUNTERS_INGESTING, ExecutionTiming::IngestionTime).await; + assert_shared_rate_chain_answers(&plan); +} + // ── Global aggregates over an aggregate's output ───────────────────────────── /// A PromQL aggregate with no grouping keys over another aggregate's output @@ -842,3 +884,46 @@ async fn a_global_promql_count_over_an_empty_aggregate_answers_no_sample() { assert_eq!(values(&plan, &m_samples()), with_samples, "{query}"); } } + +/// `sum by (job) (rate(requests_total[5m]))` every minute over 1,000 +/// counter series sampled every 100 ms, raw data kept, with the arrival +/// left unknown (the default): the plan installs and answers. Four series +/// (jobs `x` and `y`) each grow by 1 every 100 ms, so each series' rate is +/// 10 per second and each job's sum 20. +#[tokio::test] +async fn sum_rate_at_10_hz_with_unknown_arrival_installs_and_answers() { + let workload: PlanningWorkload = serde_json::from_str( + r#"{"query_workload": {"language": "promql", "repeating_queries": [ + {"query": "sum by (job) (rate(requests_total[5m]))", + "demand": {"fixed_interval_at": {"interval": 60000, "evaluation_phase": 0}}, + "requirements": {}, "predictability": {"predictable": {"known_at": null}}}]}, + "data_workload": {"arrival": "unknown", "sources": {"requests_total": { + "series_count": 1000, "metric_type": "counter", "sample_interval": 100}}}}"#, + ) + .unwrap(); + let plan = plan_installable(UserInput::new( + &workload, + FrontendInput::Promql, + PlanningModels::builtin(), + )) + .await + .unwrap() + .0; + let series = [("a", "x"), ("b", "x"), ("c", "y"), ("d", "y")]; + let mut samples = Vec::new(); + for (instance, job) in series { + for step in 0..=3_600i64 { + samples.push(sample( + "requests_total", + &[("instance", instance), ("job", job)], + EVALUATION_MS - 360_000 + step * 100, + step as f64, + )); + } + } + let answered = query_answers(&plan, &samples); + assert_eq!(answered[0].len(), 2, "{answered:?}"); + for (labels, rate) in &answered[0] { + assert!((rate - 20.0).abs() < 0.01, "{labels:?}: {rate}"); + } +} diff --git a/crates/stage2-physical-optimization/src/candidate.rs b/crates/stage2-physical-optimization/src/candidate.rs index effe6d523..f8e6aef59 100644 --- a/crates/stage2-physical-optimization/src/candidate.rs +++ b/crates/stage2-physical-optimization/src/candidate.rs @@ -14,7 +14,7 @@ use asap_types::stages::{ CandidateForMemoGroupId, Lifetime, Memo, MemoGroupId, PhysicalProperties, PruneReason, StorageMedium, ValueSign, }; -use asap_types::summary_type::{ExactKind, SketchAlgorithm}; +use asap_types::summary_type::SketchAlgorithm; use asap_types::workload::{DataArrival, DataWorkload}; /// Level 3's values, $\tau$. @@ -153,7 +153,7 @@ fn counter_state_rows_ordered( ) -> bool { let counter_state = matches!( summary_type, - FieldDataType::ExactAggregate(ExactKind::Rate | ExactKind::Increase | ExactKind::IRate, _) + FieldDataType::ExactAggregate(kind, _) if kind.tracks_counter_increase() ); let out_of_order_bound_known = matches!( data.arrival, diff --git a/crates/types/src/summary_type/parameters.rs b/crates/types/src/summary_type/parameters.rs index 339fa9b83..21cce4b0b 100644 --- a/crates/types/src/summary_type/parameters.rs +++ b/crates/types/src/summary_type/parameters.rs @@ -52,6 +52,19 @@ impl ExactKind { ExactKind::Increase => AggIntent::Increase, } } + + /// Whether this accumulator tracks a counter's increase (`Rate`, + /// `Increase`, `IRate`): besides its value it keeps the window's first + /// and last samples and the resets between them, so it needs its rows + /// in time order and does more work per row than a `Sum`. Example: + /// `rate(requests_total[5m])`'s accumulator does; `sum_over_time`'s + /// does not. + pub fn tracks_counter_increase(&self) -> bool { + match self { + Self::Rate | Self::Increase | Self::IRate => true, + Self::Sum | Self::Min | Self::Max | Self::Count => false, + } + } } /// Parameters for an [`ExactKind`] accumulator. All exact accumulators have 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 ed5f8a9e7..2d595c8cc 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 @@ -765,7 +765,11 @@ scan_bytes = source_read_bytes for the build Merge, subtract, and delete add their own invocation counts; they are never folded into the simple formula implicitly. -Concrete accuracy-sized parameters determine state and work: +Concrete accuracy-sized parameters determine state and work. The table counts +each kernel's counter updates and reads; `asap-cost` charges a row, a merged +state and a read group the executor's measured time for that unit +(`node::operations_per_unit`, one operation per nanosecond at the default speed), +so a DDSketch or KLL row costs what the executor measured, not one operation: | Summary | Update operations per row | Read operations | State bytes per physical instance | |---|---:|---:|---:| @@ -785,7 +789,8 @@ 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 one operation per input state, and outputs one state per +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 using one of these operations is unavailable until the corresponding formula diff --git a/docs/develop_docs/extend-asap-aware-mapping.md b/docs/develop_docs/extend-asap-aware-mapping.md index af00cceaa..d0ff341ed 100644 --- a/docs/develop_docs/extend-asap-aware-mapping.md +++ b/docs/develop_docs/extend-asap-aware-mapping.md @@ -35,8 +35,11 @@ A new sketch that Pass 1 can offer for an intent. rejected. 5. **Cost.** Give it a state size (the library's `estimated_memory_bytes` for the type the executor builds; KLL keeps the planner's own formula) - and per-operation CPU in `sketch_shape` - (`crates/cost/src/node/state_bytes.rs`), and accept its parameters in + in `sketch_state_bytes` (`crates/cost/src/node/state_bytes/mod.rs`), + its per-row and per-merge CPU in + `crates/cost/src/node/operations_per_unit.rs` + (`update_operations_per_row`, `counters_updated_per_row`, + `merge_operations_per_state`), and accept its parameters in `crates/cost/src/measured.rs` if deployments measure it. 6. **Executor.** Wrap the `asap_sketchlib` implementation in `crates/executor/src/summary_kernels/` and register it in