Skip to content
Draft
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
67 changes: 60 additions & 7 deletions crates/control-plane/src/collector_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ use asap_planner::{
DeploymentCapabilities, ExecutionTiming, InstallablePlan, InstallablePlanError,
PhysicalASAPDAG, PhysicalASAPNodeId, SupportedSummary, INSTALLABLE_PLAN_WIRE_VERSION,
};
use asap_types::deployment_capabilities::CandidateDescription;
use asap_types::ir::operator::arguments::{Reduction, Source};
use asap_types::ir::operator::kind::OperatorKind;
use asap_types::ir::scalar::{ColumnRef, CompareOpKind, ScalarValue};
use asap_types::ir::schema::FieldDataType;
use asap_types::ir::{ASAPOp, NonASAPOp, Operator, ScalarExpr};
use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode;
use asap_types::stages::PhysicalProperties;
use asap_types::summary_type::{SketchAlgorithm, SummaryInputExpr, SummaryUpdate};
use common::CollectorViewsFile;
use serde::Deserialize;

/// The `capabilities_version` this control plane reads.
const CAPABILITIES_VERSION: u32 = 1;
/// The `capabilities_version` this control plane reads. Version 2 reads
/// `executor` with the planner's semantics in full, exact accumulators
/// included; version 1 meant sketches only, with the same JSON.
const CAPABILITIES_VERSION: u32 = 2;

/// What `asap_sketches --print-capabilities` prints: what the collector can
/// run, in the planner's vocabulary, each capability stated once.
Expand All @@ -56,7 +60,7 @@ pub struct CollectorExecutor {
pub supported_summaries: Vec<CollectorSummary>,
/// Every other field, with the planner's serde (unknown fields
/// refused); its own `supported_summaries` is `None`. The rule reads
/// none of them yet.
/// the exclusions they expand into for exact accumulator builds.
pub rest: DeploymentCapabilities,
}

Expand Down Expand Up @@ -502,8 +506,10 @@ impl<'a> Index<'a> {
}

/// Rule (a): `node`'s operator is listed, with its arguments within that
/// entry's limits and `predicate_ops`, and any summary it builds or reads
/// within `executor.supported_summaries`. `Err` names what is not.
/// entry's limits and `predicate_ops`, any sketch it builds or reads
/// within `executor.supported_summaries`, and any exact accumulator it
/// builds not excluded by `executor` ([`within_exact`]). `Err` names what
/// is not.
fn within_capabilities(
node: &PhysicalASAPDAGNode,
index: &Index,
Expand Down Expand Up @@ -608,8 +614,12 @@ fn within_summaries(
node: &PhysicalASAPDAGNode,
capabilities: &CollectorCapabilities,
) -> Result<(), String> {
let FieldDataType::Sketch(algorithm) = summary_type else {
return Err(format!("summary {summary_type:?}"));
let algorithm = match summary_type {
FieldDataType::Sketch(algorithm) => algorithm,
FieldDataType::ExactAggregate(..) => {
return within_exact(summary_type, input, node, capabilities)
}
_ => return Err(format!("summary {summary_type:?}")),
};
let entry = summary_entry(*algorithm, capabilities)?;
let input_kind = match (&input.item, &input.weight) {
Expand Down Expand Up @@ -640,6 +650,49 @@ fn within_summaries(
Ok(())
}

/// An exact accumulator build (`Sum`, `Count`, `Min`, ...) within the
/// capabilities. The planner's `DeploymentCapabilities` lists no exact
/// family: every one is supported unless an exclusion its fields expand
/// into rules it out (`counter_semantics` rules out the counter states,
/// `state_not_mergeable_across_windows` a family's chains, and
/// `excluded_candidates` anything). So the build is within them when none
/// of those exclusions matches the node, and it reads each sample's value,
/// the one input the collector folds into an exact state.
fn within_exact(
summary_type: &FieldDataType,
input: &SummaryUpdate,
node: &PhysicalASAPDAGNode,
capabilities: &CollectorCapabilities,
) -> Result<(), String> {
let sample_value = SummaryUpdate {
item: None,
weight: SummaryInputExpr::Column(ColumnRef::SampleValue),
};
if *input != sample_value {
return Err(format!("input {input:?} of {summary_type:?}"));
}
let candidate = CandidateDescription {
operator: &node.payload,
parameters: node.parameters.as_ref(),
physical_properties: PhysicalProperties::new(node.output_state.timing, node.lifetime)
.map_err(|e| e.to_string())?,
read_summary_type: None,
};
match capabilities
.executor
.rest
.exclusions()
.into_iter()
.find(|excluded| excluded.exclusion.matches(&candidate))
{
Some(excluded) => Err(format!(
"summary {summary_type:?}, excluded by {}",
excluded.source
)),
None => Ok(()),
}
}

/// A predicate the collector evaluates: a conjunction of comparisons of a
/// column with a `Utf8` literal (a label matcher), each with an op of
/// `ops`.
Expand Down
71 changes: 58 additions & 13 deletions crates/control-plane/tests/collector_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,71 @@ fn a_leaf_sketch_chain_is_the_collector_view() {
}
}

/// With an exact view over the same metric (not in the capabilities), the
/// quantile's chain is still the one collector view, and `data` is still
/// read raw by the backend.
/// `sum_over_time` is an exact `Sum` chain. The planner's capabilities
/// list no exact family: each is supported unless an exclusion rules it
/// out, and the collector's exclude only the counter states. So the chain
/// is the collector view, and the backend no longer reads `data` raw.
#[test]
fn a_metric_another_view_reads_stays_raw() {
fn a_leaf_exact_chain_is_the_collector_view() {
let plan = plan(&["sum_over_time(data[1m])"], false);
let (file, _) = collector_sub_dag(&plan, &parse(&capabilities())).unwrap();
let chain = chain(&plan);
let summary_type = serde_json::to_value(
&plan
.dag
.nodes
.iter()
.find(|node| node.id == chain)
.unwrap()
.payload,
)
.unwrap()["ASAP"]["SummaryTumblingWindowChain"]["summary_type"]
.clone();
assert_eq!(summary_type, json!({"ExactAggregate": ["Sum", "Sum"]}));
assert_eq!(file.outputs, [chain]);
assert_eq!(file.backend_reads_raw, [("data".to_string(), false)].into());
}

/// `sum`, `count`, `avg`, `min` and `max` over time, and a quantile, over
/// one metric: every chain (`avg` reads the `Sum` and `Count` ones) is a
/// collector view, and `data` is no longer read raw.
#[test]
fn exact_and_sketch_views_of_one_metric_all_go_to_the_collector() {
let plan = plan(
&[
"quantile_over_time(0.99, data[5m])",
"sum_over_time(data[5m])",
"quantile_over_time(0.99, data[1m])",
"sum_over_time(data[1m])",
"count_over_time(data[1m])",
"avg_over_time(data[1m])",
"min_over_time(data[1m])",
"max_over_time(data[1m])",
],
false,
);
let (file, reasons) = collector_sub_dag(&plan, &parse(&capabilities())).unwrap();
let chains = nodes_of(&plan, "SummaryTumblingWindowChain");
assert_eq!(chains.len(), 2);
assert_eq!(file.outputs.len(), 1);
let exact = chains.iter().find(|c| !file.outputs.contains(c)).unwrap();
let mut chains = nodes_of(&plan, "SummaryTumblingWindowChain");
chains.sort();
assert_eq!(chains.len(), 5, "DDSketch, Sum, Count, Min, Max");
let mut outputs = file.outputs.clone();
outputs.sort();
assert_eq!(outputs, chains, "{reasons:?}");
assert_eq!(file.backend_reads_raw, [("data".to_string(), false)].into());
}

/// An exact family an exclusion of the capabilities rules out stays in
/// the backend, with the exclusion as the reason, and its metric is read
/// raw.
#[test]
fn an_excluded_exact_family_stays_in_the_backend() {
let plan = plan(&["sum_over_time(data[1m])"], false);
let mut no_sum = capabilities();
no_sum["executor"]["excluded_candidates"] = json!([{"summary_family": {"exact": "Sum"}}]);
let (file, reasons) = collector_sub_dag(&plan, &parse(&no_sum)).unwrap();
assert!(file.outputs.is_empty());
assert!(matches!(
reason(&reasons, *exact),
Some(NotOnCollector::NotInCapabilities(what)) if what.contains("ExactAggregate")
reason(&reasons, chain(&plan)),
Some(NotOnCollector::NotInCapabilities(what))
if what.contains("ExactAggregate") && what.contains("excluded_candidates[0]")
));
assert_eq!(file.backend_reads_raw, [("data".to_string(), true)].into());
}
Expand Down Expand Up @@ -305,7 +350,7 @@ fn the_file_reads_back_and_its_views_keep_their_keys() {
#[test]
fn unknown_capabilities_are_refused() {
let mut newer = capabilities();
newer["capabilities_version"] = json!(2);
newer["capabilities_version"] = json!(3);
assert!(CollectorCapabilities::from_json(&newer.to_string()).is_err());
let mut unknown = capabilities();
unknown["operators"][0]["reductions_typo"] = json!([]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"capabilities_version": 1,
{"capabilities_version": 2,
"executor": {
"supported_summaries": [
{"summary": {"summary_type": "DDSketch", "readouts": []}, "input": "value", "parameters": {"alpha": {"gt": 0, "lt": 1}}},
Expand Down
Loading