diff --git a/crates/cost/src/node/cost_terms.rs b/crates/cost/src/node/cost_terms.rs index dbdef16b7..47fce9463 100644 --- a/crates/cost/src/node/cost_terms.rs +++ b/crates/cost/src/node/cost_terms.rs @@ -44,7 +44,9 @@ pub enum CostConstant { CounterUpdateOperationsPerRow, CarriedLabelOperationsPerRowPerLabel, KmvUpdateOperationsPerRow, - KmvOperationsPerComparedHash, + KmvUpdateOperationsPerComparedHash, + KmvMergeOperationsPerHash, + KmvMergeOperationsPerComparedHash, ExactDistinctOperationsPerRow, ExactDistinctOperationsPerValueDoubling, ExactDistinctOperationsPerRowAtFullValueMiss, @@ -106,8 +108,12 @@ impl CostConstant { built_in::CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL } CostConstant::KmvUpdateOperationsPerRow => built_in::KMV_UPDATE_OPERATIONS_PER_ROW, - CostConstant::KmvOperationsPerComparedHash => { - built_in::KMV_OPERATIONS_PER_COMPARED_HASH + CostConstant::KmvUpdateOperationsPerComparedHash => { + built_in::KMV_UPDATE_OPERATIONS_PER_COMPARED_HASH + } + CostConstant::KmvMergeOperationsPerHash => built_in::KMV_MERGE_OPERATIONS_PER_HASH, + CostConstant::KmvMergeOperationsPerComparedHash => { + built_in::KMV_MERGE_OPERATIONS_PER_COMPARED_HASH } CostConstant::ExactDistinctOperationsPerRow => { built_in::EXACT_DISTINCT_OPERATIONS_PER_ROW diff --git a/crates/cost/src/node/cost_terms/tests.rs b/crates/cost/src/node/cost_terms/tests.rs index 00cbf015b..6a063f0e2 100644 --- a/crates/cost/src/node/cost_terms/tests.rs +++ b/crates/cost/src/node/cost_terms/tests.rs @@ -50,7 +50,7 @@ fn every_per_unit_constant_has_a_cost_constant() { .iter() .map(|constant| format!("{constant:?}")) .collect(); - assert_eq!(defined.len(), 37, "{defined:?}"); + assert_eq!(defined.len(), 39, "{defined:?}"); assert_eq!( defined.difference(&variants).collect::>(), Vec::<&String>::new(), @@ -103,7 +103,7 @@ fn cost_constant_json_names_are_the_full_snake_case_list() { } /// Every constant's JSON name, in declaration order. -const JSON_NAMES: [&str; 37] = [ +const JSON_NAMES: [&str; 39] = [ "time_range_operations_per_row", "operations_per_group_per_raw_batch", "chain_operations_per_arriving_row", @@ -117,7 +117,9 @@ const JSON_NAMES: [&str; 37] = [ "counter_update_operations_per_row", "carried_label_operations_per_row_per_label", "kmv_update_operations_per_row", - "kmv_operations_per_compared_hash", + "kmv_update_operations_per_compared_hash", + "kmv_merge_operations_per_hash", + "kmv_merge_operations_per_compared_hash", "exact_distinct_operations_per_row", "exact_distinct_operations_per_value_doubling", "exact_distinct_operations_per_row_at_full_value_miss", @@ -190,16 +192,17 @@ fn defined_constants() -> Vec<(String, f64)> { /// The charge is the formula's own floating-point value, its terms added /// in the order the formula adds them, so it does not move when the -/// terms are kept per constant: 0.1 compared hashes (0.5 each) and 0.3 +/// terms are kept per constant: 0.1 compared hashes (0.62 each) and 0.3 /// merged bytes (0.43 each) per row, times 7 rows, charge -/// (0.1 × 0.5 + 0.3 × 0.43) × 7 = 1.253, where adding the per-constant -/// products 0.7 × 0.5 + 2.1 × 0.43 gives 1.2530000000000001. +/// (0.1 × 0.62 + 0.3 × 0.43) × 7 = 1.337, where adding the per-constant +/// products (0.1 × 7) × 0.62 + (0.3 × 7) × 0.43 gives 1.3370000000000002 +/// (0.1 × 7 is 0.7000000000000001). #[test] fn the_charge_adds_terms_in_the_formula_s_order() { - let terms = NodeCostTerms::of(CostConstant::KmvOperationsPerComparedHash, 0.1) + let terms = NodeCostTerms::of(CostConstant::KmvUpdateOperationsPerComparedHash, 0.1) .plus(CostConstant::KllMergeOperationsPerMergedByte, 0.3) .times(7.0); - assert_eq!(terms.operations(), (0.1 * 0.5 + 0.3 * 0.43) * 7.0); + assert_eq!(terms.operations(), (0.1 * 0.62 + 0.3 * 0.43) * 7.0); let by_constant = terms.operations_with(CostConstant::operations_per_unit); assert_ne!(by_constant, terms.operations()); assert!((by_constant - terms.operations()).abs() <= 1e-12 * terms.operations()); @@ -211,7 +214,7 @@ fn the_charge_adds_terms_in_the_formula_s_order() { #[test] fn attributing_to_constants_keeps_the_charge() { let per_row = NodeCostTerms::of(CostConstant::HashAggregateOperationsPerRow, 1.0) - .plus(CostConstant::KmvOperationsPerComparedHash, 20.8); + .plus(CostConstant::KmvMergeOperationsPerHash, 4.0); let operator = NodeCostTerms::without_constant(27_000.0) .plus(CostConstant::OutputRowOperationsPerGroup, 10.0); let terms = operator diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index 7344882c1..b2f271da9 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -57,9 +57,10 @@ use asap_types::ir::operator::AggIntent; use asap_types::ir::schema::FieldDataType; -use asap_types::summary_type::{ExactKind, SketchAlgorithm, SketchParams}; +use asap_types::summary_type::{ExactKind, SketchParams}; use crate::node::cost_terms::{CostConstant, NodeCostTerms}; +use crate::node::state_bytes::StateInputRows; use crate::StateFamily; /// A row kept by a time range over raw rows, or a state read by a time @@ -196,24 +197,76 @@ pub(crate) const COUNTER_UPDATE_OPERATIONS_PER_ROW: f64 = 33.0; pub(crate) const CARRIED_LABEL_OPERATIONS_PER_ROW_PER_LABEL: f64 = 37.0; /// One row folded into a KMV state, beyond the retained hashes it compares -/// the row's hash with (`KMV_OPERATIONS_PER_COMPARED_HASH`): hashing the -/// value and the state's checks. Fitted with that term on the executor -/// over 28 builds: 300,000 rows of 100 to 10,000,000 distinct values, one -/// state at k = 4,323 and 4 to 1,000 states at k = 26,656 (minimizing the -/// squared log error, chosen by leave-one-out error over the distinct -/// counts, 0.107 against 0.200 for the absolute): 63.8 per row and 0.51 -/// per compared hash, charged 65 and 0.50. -pub(crate) const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 65.0; +/// the row's hash with (`KMV_UPDATE_OPERATIONS_PER_COMPARED_HASH`): reading +/// the row's value, finding its group's state, hashing the value and +/// checking it against the state's largest retained hash. +/// +/// Measured on the executor's KMV `SummaryAgg` by (service, endpoint), +/// each build's time less its input's, median of 3 runs on one pinned +/// core, 10 rows per group per batch: 10 to 10,000 groups, 1,200 to +/// 60,000 rows per group, 10,000 to 1,000,000 distinct values, at k = +/// 1,081, 4,323, 26,656 and 106,263 (132 builds). The two settings a busy +/// host disturbed were measured three and four times and their median +/// kept. Of the forms compared by the error leaving one setting (groups, +/// rows per group, distinct values) out, a constant per row and one per +/// compared hash scores 0.058 (RMS of the log error; largest 0.25), and +/// 0.057 leaving each k out. Adding a term per inserted hash (the heap's +/// push), or per inserted hash times log2 k, leaves it at 0.058; a term +/// for the states' retained hashes outgrowing the cache brings it to 0.049 +/// but fits a machine-specific cache size, so it is left out. Fitted 108.8 +/// per row and 0.620 per compared hash, charged 110 and 0.62: measured ÷ +/// charged is 0.81–1.27 over the 132 builds, against 1.17–1.63 at the +/// earlier 65 and 0.50. Example: 400 states at k = 4,323 over 60,000 rows +/// each of 1,000,000 distinct values compare 976 hashes per row, 715 ns +/// charged, 712 measured (553 charged before). +pub(crate) const KMV_UPDATE_OPERATIONS_PER_ROW: f64 = 110.0; /// Per retained hash a KMV update compares the row's hash with /// ([`kmv_compared_hashes_per_row`]): `asap_sketchlib` checks a new hash /// for a duplicate by walking every hash it retains, unless the state is -/// full and the hash is above its largest. Example: 4 states by `region` at -/// k = 26,656 over 75,000 rows each of 100,000 distinct values compare -/// 13,820 hashes per row, 6,975 ns charged, 7,410 measured; one state at -/// k = 4,323 over all 300,000 rows fills early and compares 314, 222 ns -/// charged, 191 measured. -pub(crate) const KMV_OPERATIONS_PER_COMPARED_HASH: f64 = 0.5; +/// full and the hash is above its largest, so an update costs O(k) while +/// the state fills and for every hash that enters it. That walk is the +/// library's, not the algorithm's: a set membership check would take O(1) +/// per update; once the library has one, this constant and +/// `KMV_UPDATE_OPERATIONS_PER_ROW` are measured again. Fitted with that +/// constant (see its doc). Example: 4 states by `region` at k = 26,656 over +/// 75,000 rows each of 100,000 distinct values compare 13,820 hashes per +/// row, 8,678 ns charged, 8,357 measured; one state at k = 4,323 over all +/// 300,000 rows fills early and compares 314, 305 ns charged, 329 +/// measured. +pub(crate) const KMV_UPDATE_OPERATIONS_PER_COMPARED_HASH: f64 = 0.62; + +/// Per hash a KMV merge reads from an input state, beyond the retained +/// hashes it compares that hash with +/// (`KMV_MERGE_OPERATIONS_PER_COMPARED_HASH`): `asap_sketchlib` merges by +/// updating the group's state with each hash the input retains, without +/// reading a row or hashing a value. +/// +/// Measured on the executor's `SummaryMerge` over stored KMV states, median +/// of 3 runs on one pinned core: states of 1,200, 12,000 and 60,000 rows +/// of 1,000,000 distinct values, merged 5 per group (a 5-minute range of +/// 1-minute windows) and 10, 50 and 200 per group (a rollup), at k = +/// 1,081, 4,323, 26,656 and 106,263 (45 merges). Of the forms compared by +/// the error leaving one setting (k, rows per state, states per group) +/// out, a constant per hash and one per compared hash, with the states +/// sharing hashes (see [`merge_terms_per_state`]), scores 0.050 (RMS +/// of the log error; largest 0.10); taking every hash as distinct scores +/// 0.104, and also passing each group's first state through unmerged +/// 0.120. Adding a constant per state, or per hash times log2 k, brings it +/// to 0.046 but fits a negative constant per hash, so neither is kept. Fitted 2.61 per hash and 0.428 per compared hash, charged 2.6 +/// and 0.43: measured ÷ charged is 0.90–1.08 over the 45 merges, against +/// 0.21–1.10 at the earlier 65 and 0.5. On 24 held-out rollups of states +/// merged from 5 windows (10 and 100 per group, 6,000 to 300,000 rows each, +/// some of 20,000 users) it is 0.84–1.14 (RMS log error 0.091, against +/// 0.436 at 65 and 0.5). +pub(crate) const KMV_MERGE_OPERATIONS_PER_HASH: f64 = 2.6; + +/// Per retained hash a KMV merge compares an inserted hash with, as a +/// build's update does (`KMV_UPDATE_OPERATIONS_PER_COMPARED_HASH`); the +/// library's linear walk makes a merge of `k`-hash states O(k²), where a +/// sorted merge would be O(k). Fitted with `KMV_MERGE_OPERATIONS_PER_HASH` +/// (see its doc). +pub(crate) const KMV_MERGE_OPERATIONS_PER_COMPARED_HASH: f64 = 0.43; /// Per input row of an exact distinct count or frequency statistic, beyond /// its lookups and inserts below, the measure's own @@ -308,6 +361,13 @@ fn cache_miss_fraction(entries: f64, entries_in_cache: f64) -> f64 { (1.0 - entries_in_cache / entries.max(1.0)).max(0.0) } +/// The expected distinct values among `draws` uniform draws from a column +/// of `values` values: `values · (1 − e^(−draws / values))`. Example: +/// 12,000 rows of 1,000,000 users hold 11,929 distinct users. +fn expected_distinct_values(draws: f64, values: f64) -> f64 { + -values * (-draws / values).exp_m1() +} + /// The operations one input row costs an exact distinct count (or /// frequency statistic) over `rows` rows in `group_count` groups, of a /// column taking `distinct_values` values, beyond the measure's own, its @@ -329,7 +389,7 @@ pub(crate) fn exact_distinct_value_terms_per_row( let values = distinct_values.max(1) as f64; let rows = rows.max(1) as f64; let groups = group_count.max(1) as f64; - let pairs = groups * -values * (-(rows / groups) / values).exp_m1(); + let pairs = groups * expected_distinct_values(rows / groups, values); let table_values = values.clamp(EXACT_DISTINCT_FEWEST_VALUES, EXACT_DISTINCT_MOST_VALUES); NodeCostTerms::of(CostConstant::ExactDistinctOperationsPerRow, 1.0) .plus( @@ -526,7 +586,7 @@ pub(crate) fn update_terms_per_row( } (_, Some(SketchParams::Kmv { k })) => { NodeCostTerms::of(CostConstant::KmvUpdateOperationsPerRow, 1.0).plus( - CostConstant::KmvOperationsPerComparedHash, + CostConstant::KmvUpdateOperationsPerComparedHash, kmv_compared_hashes_per_row(*k, rows_per_state, distinct_values), ) } @@ -577,7 +637,7 @@ pub(crate) fn kmv_compared_hashes_per_row( // Distinct values among the rows before it, and the share of // its own value among them. let seen = match values.is_finite() { - true => -values * (-row / values).exp_m1(), + true => expected_distinct_values(row, values), false => row, }; let seen_before = seen / values; @@ -626,22 +686,26 @@ fn univmon_expected_layers(layers: u8) -> f64 { /// The operations one input state of `family` holding `state_bytes` costs /// merged into its group's state, which holds `merged_state_bytes` once -/// merged; each input state folds `rows_per_input_state` rows (`None`: -/// unknown) and its group merges `input_states_per_group` of them. A KMV -/// merge updates the group's state with each hash the input retains (at -/// most `k`, its rows when fewer), as `asap_sketchlib` merges, so each hash -/// costs a row's update over all the hashes the group's merge inserts -/// ([`kmv_compared_hashes_per_row`], each a different value). Example: a -/// KLL state of 872 bytes merged into one of 33,512 costs 360 + 0.1 × 872 + -/// 0.43 × 33,512 = 14,857. A rollup of 1,000 KMV states at k = 106,263 of -/// 12,000 rows each into 100 groups (10 states per group) inserts 119,300 -/// hashes per group, 58,278 compared per hash: 348 s, measured 352 on the -/// executor. +/// merged; each input state folds `input_rows` (`None`: unknown) and its +/// group merges `input_states_per_group` of them. A KMV merge updates the +/// group's state with each hash the input retains (at most `k`, the +/// distinct values it folded when fewer), as `asap_sketchlib` merges, so +/// each hash costs `KMV_MERGE_OPERATIONS_PER_HASH` plus the retained hashes +/// it is compared with over all the hashes the group's merge inserts +/// ([`kmv_compared_hashes_per_row`]). Each state retains the smallest +/// hashes of the values it folded, so the states draw theirs from the same +/// smallest hashes of the column's values and share some: of `V` values, a +/// state retaining `h` hashes of the `D` it folded draws them from the +/// `V h / D` smallest. Example: a KLL state of 872 bytes merged into one of +/// 33,512 costs 360 + 0.1 × 872 + 0.43 × 33,512 = 14,857. Five 1-minute +/// KMV states at k = 4,323 of 12,000 rows each of 1,000,000 users retain +/// 4,323 hashes each, drawn from the 362,000 smallest: 3.40 ms per state, +/// measured 3.12 on the executor. pub(crate) fn merge_terms_per_state( family: StateFamily<'_>, state_bytes: u64, merged_state_bytes: u64, - rows_per_input_state: Option, + input_rows: Option, input_states_per_group: u64, ) -> NodeCostTerms { match family { @@ -669,16 +733,34 @@ pub(crate) fn merge_terms_per_state( ) } StateFamily::Sketch(SketchParams::Kmv { k }) => { + let values = input_rows.and_then(|rows| rows.distinct_values); + // The distinct values each input state folded, of which it + // retains the `k` smallest hashes: all `values` when its rows + // are unknown (a state never folds more). + let folded_values = input_rows + .and_then(|rows| rows.rows_per_state) + .map(|rows| { + let rows = rows as f64; + values.map_or(rows, |values| { + let values = values.max(1) as f64; + expected_distinct_values(rows, values) + }) + }) + .or(values.map(|values| values.max(1) as f64)); let retained_hashes = - rows_per_input_state.map_or(u64::from(*k), |rows| rows.min(u64::from(*k))); - let hashes_per_group = retained_hashes.saturating_mul(input_states_per_group.max(1)); - update_terms_per_row( - &FieldDataType::Sketch(SketchAlgorithm::Kmv), - Some(&SketchParams::Kmv { k: *k }), - Some(hashes_per_group), - None, - ) - .times(retained_hashes as f64) + folded_values.map_or(f64::from(*k), |folded| folded.min(f64::from(*k))); + let hashes_per_group = retained_hashes * input_states_per_group.max(1) as f64; + // The smallest hashes of the column's values the states draw + // theirs from (`None`: every hash differs). + let shared_hashes = values + .zip(folded_values) + .map(|(values, folded)| (values as f64 * retained_hashes / folded.max(1.0)) as u64); + NodeCostTerms::of(CostConstant::KmvMergeOperationsPerHash, 1.0) + .plus( + CostConstant::KmvMergeOperationsPerComparedHash, + kmv_compared_hashes_per_row(*k, Some(hashes_per_group as u64), shared_hashes), + ) + .times(retained_hashes) } StateFamily::Sketch( SketchParams::Hydra(_) diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index 120ae8a8a..4efa09ff4 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -113,7 +113,9 @@ pub fn summary_state_bytes( /// What one summary state's bytes depend on besides its parameters (only /// a KLL's, a DDSketch's and a KMV's do): the input rows it folds, the -/// sign of their values and, for a DDSketch, their magnitudes. +/// sign of their values and, for a DDSketch, their magnitudes; and the +/// distinct values of the column it folds, which a KMV build's and merge's +/// CPU depend on. /// The rows are whatever its producer emits (raw data samples, rates, sums), /// not a summary sample. #[derive(Debug, Clone, Copy, PartialEq)] @@ -131,13 +133,22 @@ pub struct StateInputRows { /// `DDSKETCH_ASSUMED_MAGNITUDE_RANGE`. E.g. `Some(ValueMagnitudeRange { /// min_abs: 0.001, max_abs: 10000.0 })` for a latency in seconds. pub value_magnitude_range: Option, + /// The distinct values the folded column takes in one state, each + /// folded row a draw from them: the fewer of the column's declared + /// distinct values and the sources' bound per group; `None` when + /// neither is declared, which takes every row's value as new. E.g. + /// `Some(1_000_000)` for `user_id` over 1,000,000 users: KMV states of + /// 12,000 rows each then fold about 11,929 distinct users, and states + /// merged together share some of their retained hashes. + pub distinct_values: Option, } impl StateInputRows { /// What one state folds when each group merges, from every input, that /// input's states (`Some((rows each folds, states per group))`): their /// rows summed, non-negative when all are, within the magnitude range - /// spanning all of theirs. `None` when any input's is unknown. Example: + /// spanning all of theirs, of the most distinct values any declares. + /// `None` when any input's is unknown. Example: /// 360 states of 10 rows and 60 of 100 merge into one of 9,600 rows. pub(crate) fn merged(inputs: impl IntoIterator>) -> Option { let mut merged: Option = None; @@ -163,6 +174,10 @@ impl StateInputRows { min_abs: a.min_abs.min(b.min_abs), max_abs: a.max_abs.max(b.max_abs), }), + distinct_values: sum + .distinct_values + .zip(input.distinct_values) + .map(|(a, b)| a.max(b)), }, }); } @@ -174,6 +189,7 @@ impl StateInputRows { rows_per_state: None, non_negative: false, value_magnitude_range: None, + distinct_values: None, }; } diff --git a/crates/cost/src/node/state_bytes/tests.rs b/crates/cost/src/node/state_bytes/tests.rs index 8d5a0bf6a..1fad2f50e 100644 --- a/crates/cost/src/node/state_bytes/tests.rs +++ b/crates/cost/src/node/state_bytes/tests.rs @@ -24,6 +24,33 @@ fn exact_state_is_priced_at_the_executors_state_size() { assert_eq!(asap_types::summary_type::EXACT_ACCUMULATOR_STATE_BYTES, 56); } +/// Merging states keeps the larger column's distinct values, since they +/// describe the column the states were built from, not one state: states +/// over 1,000 and 1,000,000 users merge as 1,000,000; with either unknown, +/// unknown. Rows add up per state. +#[test] +fn merged_input_rows_keep_the_larger_distinct_values() { + let rows = |rows_per_state, distinct_values| StateInputRows { + rows_per_state: Some(rows_per_state), + non_negative: true, + value_magnitude_range: None, + distinct_values, + }; + let merged = StateInputRows::merged([ + Some((rows(12_000, Some(1_000)), 2)), + Some((rows(6_000, Some(1_000_000)), 1)), + ]) + .unwrap(); + assert_eq!(merged.rows_per_state, Some(30_000)); + assert_eq!(merged.distinct_values, Some(1_000_000)); + let merged = StateInputRows::merged([ + Some((rows(12_000, Some(1_000)), 1)), + Some((rows(6_000, None), 1)), + ]) + .unwrap(); + assert_eq!(merged.distinct_values, None); +} + /// The rows a family is fed. enum Rows { /// One sample value a row. @@ -184,6 +211,7 @@ fn ddsketch_state_bytes_over_charge_narrower_values_within_their_bands() { rows_per_state: Some(u64::from(n)), non_negative: true, value_magnitude_range: None, + distinct_values: None, }, ) as f64 }; @@ -221,6 +249,7 @@ fn ddsketch_state_bytes_match_the_library_on_latency_like_values() { rows_per_state, non_negative, value_magnitude_range: None, + distinct_values: None, }, ) }; @@ -252,6 +281,7 @@ fn the_assumed_ddsketch_range_is_named_only_when_the_bytes_use_it() { rows_per_state: Some(600), non_negative: true, value_magnitude_range, + distinct_values: None, }; let declared = ValueMagnitudeRange { min_abs: 0.001, @@ -304,6 +334,7 @@ fn ddsketch_state_bytes_are_capped_by_the_declared_value_magnitude_range() { rows_per_state, non_negative: true, value_magnitude_range, + distinct_values: None, }, ) }; @@ -374,6 +405,7 @@ fn ddsketch_state_bytes_under_charge_log_uniform_values_over_a_declared_range() rows_per_state: Some(u64::from(n)), non_negative: true, value_magnitude_range: Some(declared), + distinct_values: None, }, ); let held = library_ddsketch_mean_bytes(alpha, n, false, log_uniform_over_seven_decades); @@ -409,6 +441,7 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() { rows_per_state, non_negative: true, value_magnitude_range: None, + distinct_values: None, }, ) }; @@ -635,6 +668,7 @@ fn state_bytes_bound_the_executors_charge() { rows_per_state: Some(u64::from(n)), non_negative: false, value_magnitude_range: None, + distinct_values: None, }, ) as usize; let context = format!("{algorithm:?} after {n} rows: {priced} priced"); @@ -735,6 +769,7 @@ fn priced_bytes_agree_with_the_executor() { rows_per_state: Some(u64::from(rows_per_state)), non_negative: true, value_magnitude_range: None, + distinct_values: None, }; let gap = |issue, priced_over_charged| { Some(KnownBytesGap { diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 093fc44ec..04a1ce866 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -1098,6 +1098,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( rows_per_state: (!ingestion).then(|| input.rows.div_ceil(group_count.max(1))), non_negative, value_magnitude_range, + distinct_values: folded_distinct_values(update), }; built_state_input_rows = Some(input_rows); let state_bytes = measured_state_bytes.unwrap_or(summary_state_bytes( @@ -1110,7 +1111,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( family, node.parameters.as_ref(), input_rows.rows_per_state, - folded_distinct_values(update), + input_rows.distinct_values, ) .plus_terms(&carried_label_terms_per_row(&node.output_schema, reduction)); let update_ops = update_terms.operations(); @@ -1168,6 +1169,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( rows_per_state: Some(rows_per_tumbling_window.div_ceil(group_count.max(1))), non_negative, value_magnitude_range, + distinct_values: folded_distinct_values(update), }; built_state_input_rows = Some(input_rows); let state_bytes = measured_state_bytes.unwrap_or(summary_state_bytes( @@ -1180,7 +1182,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( family, node.parameters.as_ref(), input_rows.rows_per_state, - folded_distinct_values(update), + input_rows.distinct_values, ) .plus_terms(&carried_label_terms_per_row(&node.output_schema, reduction)); let update_ops = update_terms.operations(); @@ -1294,9 +1296,7 @@ pub(crate) fn node_usage_and_cost_terms<'a>( family, input_state_bytes(producer), merged_state_bytes, - producer - .state_input_rows - .and_then(|rows| rows.rows_per_state), + producer.state_input_rows, states.div_ceil(group_count.max(1)), ) .times(producer.output.rows as f64), diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 611aaf6be..199c47adb 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -806,6 +806,7 @@ fn a_merge_reading_one_state_per_group_passes_it_through() { rows_per_state: Some(100), non_negative: true, value_magnitude_range: None, + distinct_values: None, }, ); let one_window = ProducerOutput { @@ -825,6 +826,7 @@ fn a_merge_reading_one_state_per_group_passes_it_through() { rows_per_state: Some(100), non_negative: true, value_magnitude_range: None, + distinct_values: None, }), }; let merge = PhysicalASAPDAGNode { @@ -1130,10 +1132,10 @@ fn an_exact_distinct_count_reads_at_most_its_column_s_declared_distinct_values() /// it holds `k` of them, then only hashes below its largest /// (`asap_sketchlib`'s duplicate check). Over 300,000 rows of 100,000 /// distinct `user_id`s, one state at k = 4,323 fills early and measures -/// 191 ns per row; four states by `region` at k = 26,656 (75,000 rows -/// each) stay below `k` for most rows and measure 7,410. Charged a fixed -/// 48 per row, the grouped build looked cheaper than an exact distinct -/// count it measures 25x more than. +/// 329 ns per row; four states by `region` at k = 26,656 (75,000 rows +/// each) stay below `k` for most rows and measure 8,357 (median of 3 +/// runs of 3). Charged a fixed 48 per row, the grouped build looked +/// cheaper than an exact distinct count it measures 25x more than. #[test] fn a_kmv_build_costs_per_retained_hash_it_compares() { use asap_types::summary_type::SketchAlgorithm; @@ -1147,7 +1149,7 @@ fn a_kmv_build_costs_per_retained_hash_it_compares() { child: 0, })) }; - for (keys, k, measured) in [(vec![], 4_323, 191.0), (vec![2], 26_656, 7_410.0)] { + for (keys, k, measured) in [(vec![], 4_323, 329.0), (vec![2], 26_656, 8_357.0)] { let mut rows = raw_rows(&range, "t", 300_000, 300_000); rows.output_schema = &range.output_schema; rows.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); @@ -1171,13 +1173,143 @@ fn a_kmv_build_costs_per_retained_hash_it_compares() { } } +/// A KMV build's time per row, measured on the executor (median of 3 runs +/// on one pinned core) over `user_id`s drawn from 1,000,000 values, in 10 +/// states by `job` and 1,000 by `instance`, from 1,200 to 60,000 rows per +/// state at k = 1,081 to 106,263. Charged 65 per row and 0.5 per compared +/// hash, these builds were charged 0.62–0.84 of their time: 400 states at +/// k = 4,323 over 60,000 rows each were charged 553 ns per row and +/// measure 712. +#[test] +fn a_kmv_build_charges_its_measured_time_per_row() { + use asap_types::summary_type::SketchAlgorithm; + let settings = CostInputs::default(); + let (data, schema) = sql_rows_of_t(1_000_000); + let range = PhysicalASAPDAGNode { + output_schema: schema, + ..node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + })) + }; + // (grouping keys, input rows, k, measured ns per row) + for (keys, rows, k, measured) in [ + (vec![1], 600_000, 1_081, 176.4), + (vec![1], 600_000, 4_323, 733.6), + (vec![1], 120_000, 26_656, 3_611.3), + (vec![3], 1_200_000, 4_323, 513.0), + (vec![3], 12_000_000, 4_323, 1_645.4), + (vec![3], 12_000_000, 106_263, 3_911.4), + ] { + let mut input = raw_rows(&range, "t", rows, 300_000); + input.output_schema = &range.output_schema; + input.unchanged_columns = ["job", "region", "instance", "user_id", "value"].into(); + let build = PhysicalASAPDAGNode { + parameters: Some(SketchParams::Kmv { k }), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + input: SummaryUpdate::column(ColumnRef::Named("user_id".into())), + reduction: Reduction::by(keys.clone()), + filter: None, + })) + }; + let usage = + node_resource_usage_and_cost(&build, &[input], (1, 1.0), None, &data, &settings) + .unwrap(); + let charged = usage.cpu_ops_per_second / rows as f64; + assert!( + (0.8..=1.25).contains(&(charged / measured)), + "k = {k}, keys {keys:?}, {rows} rows: charged {charged:.1} ns per row, \ + measured {measured}" + ); + } +} + +/// A KMV merge's time per input state, measured on the executor's +/// `SummaryMerge` over stored KMV states (median of 3 runs on one pinned +/// core): states of `user_id`s drawn from 1,000,000 values (or 20,000), +/// merged 5 per group (a 5-minute range of 1-minute windows) and 10 to 200 +/// per group (a rollup), at k = 1,081 to 106,263. Charged 65 per hash and +/// 0.5 per compared hash, every hash taken as distinct, these merges were +/// charged 0.87x to 4.9x their time: 200 states per group of 1,200 rows +/// each at k = 1,081 were charged 88,151 ns per state and measure 18,109. +/// States of 20,000 users share many hashes, and each shared one walks +/// until it finds itself. +#[test] +fn a_kmv_merge_charges_its_measured_time_per_state() { + use crate::node::operations_per_unit::merge_terms_per_state; + // (k, input states per group, rows each state folds, distinct users, + // measured ns per state) + for (k, states, rows, users, measured) in [ + (4_323, 5, 12_000, 1_000_000, 3_115_412.0), + (1_081, 200, 1_200, 1_000_000, 18_109.0), + (26_656, 50, 12_000, 1_000_000, 22_587_600.0), + (106_263, 10, 12_000, 1_000_000, 299_052_000.0), + (4_323, 200, 60_000, 1_000_000, 361_479.0), + (4_323, 10, 60_000, 20_000, 3_330_600.0), + ] { + let parameters = SketchParams::Kmv { k }; + let input_rows = StateInputRows { + rows_per_state: Some(rows), + distinct_values: Some(users), + ..StateInputRows::UNKNOWN + }; + let charged = merge_terms_per_state( + StateFamily::Sketch(¶meters), + 0, + 0, + Some(input_rows), + states, + ) + .operations(); + assert!( + (0.8..=1.25).contains(&(charged / measured)), + "k = {k}, {states} states per group of {rows} rows of {users} users: charged \ + {charged:.0} ns per state, measured {measured}" + ); + } +} + +/// A KMV state whose rows are unknown (built at ingestion) retains at most +/// the distinct values its column takes: merging states of 500 users at +/// k = 4,323 is charged as states that folded all 500, not 4,323 hashes +/// each. +#[test] +fn a_kmv_merge_of_states_with_unknown_rows_retains_at_most_the_column_s_values() { + use crate::node::operations_per_unit::merge_terms_per_state; + let parameters = SketchParams::Kmv { k: 4_323 }; + let charge = |rows_per_state, distinct_values| { + merge_terms_per_state( + StateFamily::Sketch(¶meters), + 0, + 0, + Some(StateInputRows { + rows_per_state, + distinct_values, + ..StateInputRows::UNKNOWN + }), + 10, + ) + .operations() + }; + let unknown_rows = charge(None, Some(500)); + let all_values_folded = charge(Some(1_000_000), Some(500)); + assert!( + (unknown_rows / all_values_folded - 1.0).abs() < 1e-6, + "unknown rows {unknown_rows:.0}, every value folded {all_values_folded:.0}" + ); + assert!(unknown_rows < charge(None, None) / 8.0); +} + /// A rollup merge of KMV states updates each group's state with every hash /// each input state retains (`asap_sketchlib`'s merge), so it compares /// those hashes with the ones the group's state already retains. Measured /// on the executor: 1,000 states by (region, service) at k = 106,263, each /// built from 12,000 rows of 1,000,000 distinct users, merged into 100 -/// groups by service take 352 s (the plan's 393 s less its 40 s build). -/// Charged per byte of state, they were 0.1 s. +/// groups by service take 299 s (299 ms per state, median of 3 runs on +/// one pinned core). Charged per byte of state, they were 0.1 s. #[test] fn a_kmv_rollup_merge_costs_per_hash_it_inserts() { use asap_types::ir::schema::DataType; @@ -1193,6 +1325,7 @@ fn a_kmv_rollup_merge_costs_per_hash_it_inserts() { rows_per_state: Some(12_000), non_negative: false, value_magnitude_range: None, + distinct_values: Some(1_000_000), }; let state_bytes = crate::node::state_bytes::sketch_state_bytes(¶meters, rows); let build = PhysicalASAPDAGNode { @@ -1237,8 +1370,8 @@ fn a_kmv_rollup_merge_costs_per_hash_it_inserts() { assert_eq!(usage.output.rows, 100); let charged_s = usage.work_ms_per_execution / 1_000.0; assert!( - (0.75..=1.33).contains(&(352.0 / charged_s)), - "charged {charged_s:.1} s, measured 352" + (0.8..=1.25).contains(&(299.0 / charged_s)), + "charged {charged_s:.1} s, measured 299" ); } @@ -1263,6 +1396,7 @@ fn a_rollup_merges_its_states_when_its_group_estimate_ties_its_input() { rows_per_state: Some(12_000), non_negative: false, value_magnitude_range: None, + distinct_values: None, }; let state_bytes = crate::node::state_bytes::sketch_state_bytes(¶meters, rows); let build = PhysicalASAPDAGNode { @@ -1712,6 +1846,7 @@ fn ddsketch_costed_bytes( rows_per_state: Some(600), non_negative, value_magnitude_range, + distinct_values: None, }, ) } @@ -2356,6 +2491,7 @@ fn a_merge_over_several_inputs_sizes_its_state_for_all_of_them() { rows_per_state: Some(10), non_negative: true, value_magnitude_range: None, + distinct_values: None, }; let input = ProducerOutput { payload: &windows.payload, @@ -2444,6 +2580,7 @@ fn kll_estimate_us_per_group(producer: Payload, rows_per_state: u64, state_bytes rows_per_state: Some(rows_per_state), non_negative: true, value_magnitude_range: None, + distinct_values: None, }), }; let estimate = node(Payload::ASAP(ASAPOp::SummaryEstimate { @@ -2883,6 +3020,7 @@ fn terms_reproduce_the_formula_cpu_of_every_costed_node() { rows_per_state: Some(60), non_negative: true, value_magnitude_range: None, + distinct_values: None, }; // A KLL state's bytes grow with its `k`; an exact state is 56 bytes. let state_bytes = parameters.as_ref().map_or(56, |parameters| { @@ -3001,6 +3139,7 @@ fn terms_reproduce_the_formula_cpu_of_every_costed_node() { rows_per_state: Some(12_000), non_negative: false, value_magnitude_range: None, + distinct_values: None, }; let kmv_build = PhysicalASAPDAGNode { parameters: Some(kmv.clone()), @@ -3115,6 +3254,7 @@ fn terms_reproduce_the_formula_cpu_of_every_costed_node() { rows_per_state: Some(rows_per_state), non_negative: true, value_magnitude_range: None, + distinct_values: None, }), }], None, diff --git a/crates/integration-tests/tests/rollup_controlled.rs b/crates/integration-tests/tests/rollup_controlled.rs index a056a3f70..8646fe1b2 100644 --- a/crates/integration-tests/tests/rollup_controlled.rs +++ b/crates/integration-tests/tests/rollup_controlled.rs @@ -9,7 +9,7 @@ use asap_types::{ ASAPOp, OperatorNode, QueryRoot, }, summary_type::SketchAlgorithm, - workload::PlanningWorkload, + workload::{DurationMs, PlanningWorkload}, }; use std::rc::Rc; @@ -43,25 +43,24 @@ fn nodes(roots: &[QueryRoot]) -> Vec> { } /// The viewer's distinct-count rollup workload, `COUNT(DISTINCT user_id)` -/// by service and by (service, endpoint) over 5 min every 60 s, at 2,000,000 -/// rows per second: 600,000,000 rows per evaluation, 10,000 groups for each -/// grouping and 1,000,000 users. The production entrypoint answers both -/// queries from a KMV (k = 4,323), the plan that measures faster at the -/// largest shape measured. +/// by service and by (service, endpoint) over 5 min every 60 s, with each +/// of its 10,000 series sampled every `sample_interval_ms`: at 5 ms, +/// 2,000,000 rows per second and 600,000,000 rows per evaluation, 10,000 +/// groups for each grouping and 1,000,000 users. /// -/// The better plan depends on the rows per state. Measured on the executor, -/// both plans over the same data: -/// - 60,000,000 rows (60,000 rows per state, scaled ×10 to one evaluation): -/// exact 609.6 s, KMV 483.6 s. KMV is 1.26x faster. -/// - 12,000,000 rows (12,000 rows per state): exact 12.79 s, KMV 17.84 s. -/// Exact is 1.39x faster. -/// -/// So the crossover lies between 12,000 and 60,000 rows per state. The -/// planned shape (600,000,000 rows over 10,000 groups) has 60,000 rows per -/// state, as the 60,000,000-row run does, so the test expects KMV. -#[test] -fn production_distinct_rollup_builds_kmv_at_60k_rows_per_state() { - let (workload, roots) = fixture(); +/// Returns the sketches each query's accepted plan reads and how many KMV +/// builds (at query time, or in tumbling windows as rows arrive) the +/// selected plan holds. +fn plan_rollup(sample_interval_ms: u64) -> (Vec>, usize) { + let (mut workload, roots) = fixture(); + let data = workload.data_workload.as_mut().unwrap(); + let http = data.sources.get_mut("http").unwrap(); + http.sample_interval = Some(DurationMs(sample_interval_ms)); + let rows_per_second = http.series_count.unwrap() as f64 * 1_000.0 / sample_interval_ms as f64; + data.arrival = serde_json::from_value(serde_json::json!({ + "ingesting": {"rows_per_second": rows_per_second} + })) + .unwrap(); let entries: Vec<_> = workload.query_workload.entries().collect(); let data = workload.data_workload.as_ref().unwrap(); let mut settings = CostInputs::default(); @@ -80,24 +79,68 @@ fn production_distinct_rollup_builds_kmv_at_60k_rows_per_state() { .selection .as_ref() .expect("validated plan within budget"); - let accepted = &selection.accepted_accuracy_per_query; - assert_eq!(accepted.len(), 2); - assert!( - accepted.iter().all(|accuracy| accuracy.sketches == ["Kmv"]), - "both queries read a KMV: {accepted:?}" - ); + let sketches = selection + .accepted_accuracy_per_query + .iter() + .map(|accuracy| accuracy.sketches.clone()) + .collect(); let roots = plan.query_roots().unwrap(); let kmv_builds = nodes(&roots) .iter() .filter(|node| { matches!( node.asap(), - Some(ASAPOp::SummaryAgg { - summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), - .. - }) + Some( + ASAPOp::SummaryAgg { + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + .. + } | ASAPOp::SummaryTumblingWindowChain { + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kmv), + .. + } + ) ) }) .count(); + (sketches, kmv_builds) +} + +/// The better plan for the rollup depends on the rows each KMV state folds. +/// Measured on the executor, both plans over the same data of 1,000 groups +/// by (service, endpoint), scaled to one evaluation: +/// - 60,000,000 rows (60,000 rows per state, ×10): exact 609.6 s, KMV +/// (k = 4,323) 483.6 s. KMV is 1.26x faster. +/// - 12,000,000 rows (12,000 rows per state, ×10): exact 127.9 s, KMV +/// 178.4 s. Exact is 1.39x faster. +/// +/// At 2,000,000 rows per second (a sample every 5 ms) the planned shape +/// (600,000,000 rows over 10,000 groups) has 60,000 rows per state, as the +/// 60,000,000-row run does, so the planner reads a KMV. It builds it in +/// 1-minute tumbling windows as rows arrive and merges the 5 the range +/// covers: each minute's 120,000,000 new rows are folded once, where a +/// query-time build folds all 600,000,000 at each evaluation. +#[test] +fn production_distinct_rollup_builds_kmv_at_60k_rows_per_state() { + let (sketches, kmv_builds) = plan_rollup(5); + assert_eq!(sketches.len(), 2); + assert!( + sketches.iter().all(|sketches| sketches == &["Kmv"]), + "both queries read a KMV: {sketches:?}" + ); assert!(kmv_builds > 0, "the selected plan builds a KMV"); } + +/// At 400,000 rows per second (a sample every 25 ms) the planned shape +/// (120,000,000 rows over 10,000 groups) has 12,000 rows per state, as the +/// 12,000,000-row run above does, where exact measured 1.39x faster than +/// KMV: the planner counts both queries exactly. +#[test] +fn production_distinct_rollup_counts_exactly_at_12k_rows_per_state() { + let (sketches, kmv_builds) = plan_rollup(25); + assert_eq!(sketches.len(), 2); + assert!( + sketches.iter().all(|sketches| sketches.is_empty()), + "both queries count exactly: {sketches:?}" + ); + assert_eq!(kmv_builds, 0, "the selected plan builds no KMV"); +}