Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions crates/cost/src/node/state_bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use asap_types::ir::operator::AggIntent;
use asap_types::ir::schema::{DataType, FieldDataType, Schema};
use asap_types::summary_type::KLL_MAX_K;
use asap_types::summary_type::{SketchParams, EXACT_ACCUMULATOR_STATE_BYTES};
use asap_types::workload::ValueMagnitudeRange;

/// Plain values are 8 bytes, strings 16; summary columns are sized apart.
pub fn row_bytes(schema: &Schema) -> u64 {
Expand Down Expand Up @@ -43,8 +44,12 @@ pub fn keeps_every_value(measure: &AggIntent) -> bool {
/// `ASSUMED_VALUE_KEY_BYTES`;
/// - DDSketch and KMV: the rows the state folds, at least one (any number
/// when unknown), each a distinct item for KMV; a DDSketch's values span
/// `DDSKETCH_ASSUMED_VALUE_RANGE` and fill its negative store too
/// unless proven non-negative.
/// `input_rows.value_magnitude_range` when declared, else
/// `DDSKETCH_ASSUMED_MAGNITUDE_RANGE`, and fill its negative store too
/// unless proven non-negative. Its bins are the bucket indices from
/// ⌊log_γ min_abs⌋ to ⌊log_γ max_abs⌋, at least one, for γ = (1 + α) /
/// (1 - α); this DDSketch has no bin limit to cap them. E.g. α = 0.01
/// over [0.001, 10000] takes indices -346 to 460, 807 bins.
///
/// KLL is priced without the library's estimate: its fixed bytes
/// (`kll_fixed_bytes`: the struct and its level array) plus the 8-byte
Expand Down Expand Up @@ -76,8 +81,8 @@ pub fn summary_shape(
}

/// 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 and the
/// sign of their values.
/// 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.
/// The rows are whatever its producer emits (raw data samples, rates, sums),
/// not a summary sample.
#[derive(Debug, Clone, Copy, PartialEq)]
Expand All @@ -86,16 +91,23 @@ pub struct StateInputRows {
/// `None` when unknown, which sizes it for every value it can be assumed
/// to see.
pub rows_per_state: Option<u64>,
/// Whether every value folded is proven non-negative (raw data samples of
/// sources declared non-negative), so a DDSketch fills no negative store.
/// Whether every value folded is proven non-negative (a source column
/// passed on unchanged, which every source declares non-negative), so a
/// DDSketch fills no negative store.
pub non_negative: bool,
/// Bounds on `|value|` of the non-zero values folded into the state;
/// `None` when not declared, which costs a DDSketch over the assumed
/// `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<ValueMagnitudeRange>,
}

impl StateInputRows {
/// Unknown rows of unknown sign: the most a state is assumed to hold.
pub const UNKNOWN: Self = Self {
rows_per_state: None,
non_negative: false,
value_magnitude_range: None,
};
}

Expand Down Expand Up @@ -160,16 +172,21 @@ pub fn sketch_shape(params: &SketchParams, input_rows: StateInputRows) -> (f64,
SketchParams::DDSketch { alpha } if !(*alpha > 0.0 && *alpha < 1.0) => {
(1.0, std::mem::size_of::<DdSketch>() as u64)
}
SketchParams::DDSketch { alpha } => (
1.0,
DdSketch::estimated_memory_bytes(
*alpha,
rows,
SketchParams::DDSketch { alpha } => {
let ValueMagnitudeRange { min_abs, max_abs } = input_rows
.value_magnitude_range
.unwrap_or(DDSKETCH_ASSUMED_MAGNITUDE_RANGE);
(
1.0,
DDSKETCH_ASSUMED_VALUE_RANGE,
!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, .. } => (
1.0,
HyperLogLog::<Classic>::estimated_memory_bytes(usize::from(*precision)) as u64,
Expand Down Expand Up @@ -270,12 +287,14 @@ fn univmon_expected_layers(layers: u8) -> f64 {
2.0 - 2f64.powi(1 - i32::from(layers.max(1)))
}

/// Ratio of the largest to the smallest value magnitude a DDSketch is
/// assumed to see: nine decades, e.g. latencies from 1 ns to 1 s, priced as
/// magnitudes in `[1, 1e9]` (its bins depend on the ratio alone). An
/// assumption, not workload evidence: the data workload declares no value
/// range.
const DDSKETCH_ASSUMED_VALUE_RANGE: f64 = 1e9;
/// The range of value magnitudes a DDSketch is assumed to see when its
/// column declares no `column_magnitudes`: nine decades, e.g. latencies from 1 ns
/// to 1 s, costed as magnitudes in `[1, 1e9]` (its bins depend on the ratio
/// alone). An assumption, not workload evidence.
const DDSKETCH_ASSUMED_MAGNITUDE_RANGE: ValueMagnitudeRange = ValueMagnitudeRange {
min_abs: 1.0,
max_abs: 1e9,
};

#[cfg(test)]
mod tests;
43 changes: 41 additions & 2 deletions crates/cost/src/node/state_bytes/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn ddsketch_state_bytes_bound_the_library() {
StateInputRows {
rows_per_state,
non_negative,
value_magnitude_range: None,
},
)
.1
Expand Down Expand Up @@ -145,6 +146,41 @@ fn ddsketch_state_bytes_bound_the_library() {
assert_eq!(priced(Some(10_000), false), priced(None, false));
}

/// A DDSketch over a column declaring its value magnitudes is costed at
/// the bins they span, not nine decades. At α = 0.01, γ = 1.01 / 0.99 and
/// a value `v` falls in bin `floor(ln v / ln γ)`: magnitudes in
/// [0.001, 10000] take bins -346 to 460, 807 bins, where the default
/// [1, 1e9] takes 0 to 1036, 1,037. With three or more non-negative rows
/// the library's bound is `2 (bins + 126)` 8-byte slots plus the struct.
#[test]
fn ddsketch_state_bytes_follow_the_declared_value_magnitude_range() {
let alpha = 0.01;
let costed = |value_magnitude_range| {
sketch_shape(
&SketchParams::DDSketch { alpha },
StateInputRows {
rows_per_state: Some(600),
non_negative: true,
value_magnitude_range,
},
)
.1
};
let struct_bytes = std::mem::size_of::<asap_sketchlib::DdSketch>() as u64;
let bytes_for_bins = |bins: u64| struct_bytes + 8 * 2 * (bins + 126);
let latency = ValueMagnitudeRange {
min_abs: 0.001,
max_abs: 10_000.0,
};
assert_eq!(costed(Some(latency)), bytes_for_bins(807));
assert_eq!(costed(None), bytes_for_bins(1_037));
// Values a narrower range holds fit the costed bytes.
let values: Vec<f64> = (0..600)
.map(|i| 10f64.powf(-3.0 + 7.0 * f64::from(i) / 599.0))
.collect();
assert!(library_ddsketch_bytes(alpha, &values) <= costed(Some(latency)));
}

/// A KLL state is charged what `asap_sketchlib` allocates for the
/// samples it sees, not its full capacity: its fixed struct and level
/// array, plus the item array the library starts empty and doubles from
Expand All @@ -168,6 +204,7 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() {
StateInputRows {
rows_per_state,
non_negative: true,
value_magnitude_range: None,
},
)
.1
Expand Down Expand Up @@ -197,7 +234,7 @@ fn kll_state_bytes_grow_with_the_samples_it_sees() {
/// Plan selection prices each state the executor builds per group at
/// what the executor charges for it (#947). Each family is built
/// through the executor's factory and fed `n` rows shaped as the
/// pricing assumes: signed values over [`DDSKETCH_ASSUMED_VALUE_RANGE`],
/// pricing assumes: signed values over [`DDSKETCH_ASSUMED_MAGNITUDE_RANGE`],
/// label values of [`ASSUMED_VALUE_KEY_BYTES`] keys for UnivMon, or
/// keyed rows whose tuple keys are [`ASSUMED_HEAP_KEY_BYTES`] long. The
/// heap families' shapes are dominated by their heaps. The charge is
Expand Down Expand Up @@ -366,7 +403,7 @@ fn state_bytes_bound_the_executors_charge() {
// scrambled order, alternating in sign.
Rows::Values => {
let at = f64::from(i * 7_919 % n) / f64::from(n);
let magnitude = DDSKETCH_ASSUMED_VALUE_RANGE.powf(at);
let magnitude = DDSKETCH_ASSUMED_MAGNITUDE_RANGE.max_abs.powf(at);
let sign = if i % 2 == 0 { 1.0 } else { -1.0 };
updater.update_single(sign * magnitude, 0).unwrap();
}
Expand All @@ -384,6 +421,7 @@ fn state_bytes_bound_the_executors_charge() {
StateInputRows {
rows_per_state: Some(u64::from(n)),
non_negative: false,
value_magnitude_range: None,
},
)
.1 as usize;
Expand Down Expand Up @@ -478,6 +516,7 @@ fn priced_bytes_agree_with_the_executor() {
let input_rows = |rows_per_state: u32| StateInputRows {
rows_per_state: Some(u64::from(rows_per_state)),
non_negative: true,
value_magnitude_range: None,
};
let gap = |issue, priced_over_charged| {
Some(KnownBytesGap {
Expand Down
61 changes: 44 additions & 17 deletions crates/cost/src/node/usage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ use crate::{
};
use asap_types::ir::operator::{GroupKeys, Reduction, Source};
use asap_types::ir::properties::ExecutionTiming;
use asap_types::ir::scalar::resolve_column_ref;
use asap_types::ir::schema::{FieldDataType, Schema};
use asap_types::ir::{ASAPOp, NonASAPOp, Operator, TimeRangeKind};
use asap_types::plan::physical_asap_dag::{
PhysicalASAPDAGNode, PhysicalASAPNodeId, PhysicalASAPOperatorPayload as Payload,
};
use asap_types::stages::{Lifetime, StorageMedium};
use asap_types::summary_type::{SketchParams, SketchStatistic};
use asap_types::summary_type::{SketchParams, SketchStatistic, SummaryInputExpr, SummaryUpdate};
use asap_types::workload::bounds::{
declared_non_negative, declared_series, group_count_bound, source_name, source_rows_per_ms,
unchanged_source_columns,
declared_non_negative, declared_series, declared_value_magnitude_range, group_count_bound,
source_name, source_rows_per_ms, unchanged_source_columns,
};
use asap_types::workload::{DataWorkload, ONE_SECOND_MS};

Expand Down Expand Up @@ -182,6 +183,22 @@ pub struct ProducerOutput<'a> {
pub chain_tumbling_window_width_ms: Option<u64>,
}

impl ProducerOutput<'_> {
/// The source column `weight` reads, when this producer passes that
/// column on unchanged from the sources: the column whose values a
/// summary built over this producer folds. E.g. `value` for a DDSketch
/// over a scan of `latency` or over a filter on that scan; `None` for a
/// computed weight such as a rate.
pub(crate) fn unchanged_column(&self, weight: &SummaryInputExpr) -> Option<&str> {
let SummaryInputExpr::Column(column) = weight else {
return None;
};
let index = resolve_column_ref(column, self.output_schema).ok()?;
let name = self.output_schema.fields.get(index)?.name.as_str();
self.unchanged_columns.contains(name).then_some(name)
}
}

/// What one node uses and outputs ([`node_resource_usage_and_cost`]): its
/// output statistics, sources and state family for its consumers; its work
/// for one computation (one execution) in ms on one core; its usage per
Expand Down Expand Up @@ -481,18 +498,22 @@ pub fn node_resource_usage_and_cost<'a>(
.ok_or(AnalyticalCostError::Overflow)?,
})
};
// Whether a summary built here sees raw data samples (its producer a
// scan or a time range) of sources all declared non-negative.
let raw_data_samples_non_negative = || {
let raw = producers.first().is_some_and(|producer| {
matches!(
producer.payload,
Payload::NonASAP(NonASAPOp::Scan { .. } | NonASAPOp::TimeRange { .. })
)
});
raw && sources
.iter()
.all(|source| declared_non_negative(data, source))
// What the sources declare about the values a summary built here folds:
// whether every one is non-negative, and their magnitude range, both
// from the one source column it folds
// (`ProducerOutput::unchanged_column`).
let folded_values = |update: &SummaryUpdate| {
producers
.first()
.and_then(|producer| producer.unchanged_column(&update.weight))
.map_or((false, None), |column| {
let non_negative = !sources.is_empty()
&& sources
.iter()
.all(|source| declared_non_negative(data, source, column));
let range = declared_value_magnitude_range(data, sources.iter().copied(), column);
(non_negative, range)
})
};
let unary = |output| UnaryEdgeStatistics {
input,
Expand Down Expand Up @@ -807,16 +828,19 @@ pub fn node_resource_usage_and_cost<'a>(
},
Payload::ASAP(ASAPOp::SummaryAgg {
summary_type: family,
input: update,
reduction,
..
}) => {
let group_count = groups(reduction)?;
// At query time, each state sees its group's input rows; at
// ingestion time, the rows of a window one second of
// arriving rows does not bound.
let (non_negative, value_magnitude_range) = folded_values(update);
let input_rows = StateInputRows {
rows_per_state: (!ingestion).then(|| input.rows.div_ceil(group_count.max(1))),
non_negative: raw_data_samples_non_negative(),
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);
Expand All @@ -842,6 +866,7 @@ pub fn node_resource_usage_and_cost<'a>(
}
Payload::ASAP(ASAPOp::SummaryTumblingWindowChain {
summary_type: family,
input: update,
reduction,
tumbling_window_width_ms,
..
Expand All @@ -859,9 +884,11 @@ pub fn node_resource_usage_and_cost<'a>(
.map_err(|_| AnalyticalCostError::Overflow)?,
false => input.rows.div_ceil(retained_output_count),
};
let (non_negative, value_magnitude_range) = folded_values(update);
let input_rows = StateInputRows {
rows_per_state: Some(rows_per_tumbling_window.div_ceil(group_count.max(1))),
non_negative: raw_data_samples_non_negative(),
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);
Expand Down
Loading
Loading