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
22 changes: 15 additions & 7 deletions asap-precompute-rs/src/bin/asap_views_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,21 @@ fn run(views: &str, mode: &str, series: usize, rate: u64, seconds: u64) -> (Dura
let mut rng = Rng(0x9E37_79B9_7F4A_7C15);
let views = planner_views::parse(views).expect("views load");
// The `sketches` mode builds q01's view: one DDSketch view, 5 s windows.
let [view] = views.views.as_slice() else {
panic!("one view");
};
assert_eq!(
(view.sketch, view.tumbling_window_width_ms),
(asap_sketchlib::SketchAlgorithm::DDSketch, 5_000)
);
// The `executor` mode builds any views, e.g. the exact fixture's.
if mode == "sketches" {
let [view] = views.views.as_slice() else {
panic!("one view");
};
assert_eq!(
(view.summary, view.tumbling_window_width_ms),
(
asap_types::summary_type::SummaryFamily::Sketch(
asap_sketchlib::SketchAlgorithm::DDSketch
),
5_000
)
);
}
let mut executor = (mode == "executor").then(|| ViewExecutor::new(&views).expect("compiles"));
let sketches = (mode == "sketches").then(|| {
let config = PrecomputeConfig {
Expand Down
109 changes: 106 additions & 3 deletions asap-precompute-rs/src/otap/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
//! except that each `supported_summaries` entry is the planner's
//! `SupportedSummary` wrapped as `{"summary", "input", "parameters"}`. The
//! sketches, their input kind and their parameter ranges come from the
//! processor's dispatch table ([`SKETCH_TYPES`]).
//! processor's dispatch table ([`SKETCH_TYPES`]). It is read with the
//! planner's semantics in full: `supported_summaries` lists sketches only,
//! and every exact accumulator family is supported unless an exclusion
//! the fields expand into (`DeploymentCapabilities::exclusions`) rules it
//! out. The collector builds `Sum`, `Count`, `Min` and `Max` per series
//! and window over the sample value; `counter_semantics` (no reset
//! handling, no extrapolation) rules out the order-sensitive `Counter`
//! and `IRate` states ([`CollectorCapabilities::exclusion_of`]).
//! - `predicate_ops`: the label comparisons the runtime's matchers
//! ([`MatchOp`], `PrecomputeConfig::matchers`) evaluate, for every
//! operator that takes a predicate. The processor's own config sets no
Expand All @@ -22,18 +29,24 @@
use std::time::Duration;

use asap_types::deployment_capabilities::{
CounterSemanticsSupport, DeploymentCapabilities, SupportedSummary,
CandidateDescription, CounterSemanticsSupport, DeploymentCapabilities, ExclusionSource,
SupportedSummary,
};
use asap_types::ir::operator::{OperatorKind, Reduction, Source};
use asap_types::ir::scalar::CompareOpKind;
use asap_types::plan::physical_asap_dag::PhysicalASAPDAGNode;
use asap_types::stages::PhysicalProperties;
use serde::ser::{Error as _, SerializeMap};
use serde::{Serialize, Serializer};

use super::config::{PluginConfig, SketchInput, SketchParameter, SKETCH_TYPES};
use crate::matchers::MatchOp;

/// The version of this format; a reader refuses one it does not know.
pub const CAPABILITIES_VERSION: u32 = 1;
/// Version 2: `executor` is read with the planner's semantics in full, so
/// the exact accumulator families it does not exclude are built (version 1
/// built sketches only, with the same JSON).
pub const CAPABILITIES_VERSION: u32 = 2;

/// What `asap-collector --print-capabilities` prints.
#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -123,6 +136,29 @@ fn serialize_parameters<S: Serializer>(
map.end()
}

impl CollectorCapabilities {
/// The capability of `executor` that rules out `node` in the planner's
/// reading: the source of the first of its exclusions
/// (`DeploymentCapabilities::exclusions`) that matches the node's
/// operator, θ, timing and lifetime. `None` when none does, e.g. for
/// an ingestion-time exact `Sum` chain.
pub fn exclusion_of(&self, node: &PhysicalASAPDAGNode) -> Option<ExclusionSource> {
let candidate = CandidateDescription {
operator: &node.payload,
parameters: node.parameters.as_ref(),
physical_properties: PhysicalProperties::new(node.output_state.timing, node.lifetime)
.ok()?,
read_summary_type: None,
};
self.executor
.deployment_capabilities()
.exclusions()
.into_iter()
.find(|excluded| excluded.exclusion.matches(&candidate))
.map(|excluded| excluded.source)
}
}

/// One operator the collector runs, with the limits of that operator only.
#[derive(Debug, Serialize)]
pub struct OperatorCapability {
Expand Down Expand Up @@ -391,6 +427,73 @@ mod tests {
assert_eq!(loaded.window_close_delay_ms, 6000);
}

/// The printed `executor`, read as the planner reads it, supports the
/// exact `Sum`, `Count`, `Min` and `Max` families (no exclusion rules
/// any of them out) and excludes the order-sensitive `Counter` and
/// `IRate` ones.
#[test]
fn exact_accumulators_are_in_the_printed_capabilities() {
use asap_types::summary_type::{ExactKind, SummaryFamily};
let json: serde_json::Value = serde_json::from_str(&capabilities_json()).unwrap();
assert_eq!(json["capabilities_version"], 2);
let mut executor = json["executor"].clone();
executor["supported_summaries"] = serde_json::Value::Null;
let parsed: DeploymentCapabilities = serde_json::from_value(executor).unwrap();
let excluded: Vec<SummaryFamily> = parsed
.exclusions()
.iter()
.filter_map(|excluded| excluded.exclusion.family_excluded_outright())
.collect();
for kind in [
ExactKind::Sum,
ExactKind::Count,
ExactKind::Min,
ExactKind::Max,
] {
assert!(!excluded.contains(&SummaryFamily::Exact(kind)), "{kind:?}");
}
for kind in [ExactKind::Counter, ExactKind::IRate] {
assert!(excluded.contains(&SummaryFamily::Exact(kind)), "{kind:?}");
}
}

/// The exact fixture's chains (ingestion-time `Sum`, `Count`, `Min` and
/// `Max` per series) match no exclusion; the same chain of a `Counter`
/// matches `counter_semantics`'.
#[test]
fn exact_chains_match_no_exclusion_but_a_counters_does() {
use asap_types::plan::physical_asap_dag::PhysicalASAPDAG;
#[derive(serde::Deserialize)]
struct File {
dag: PhysicalASAPDAG,
}
let file: File = serde_json::from_str(include_str!(
"../../tests/fixtures/collector-views/exact/collector-views.json"
))
.unwrap();
let capabilities = collector_capabilities();
let chains: Vec<&PhysicalASAPDAGNode> = file
.dag
.nodes
.iter()
.filter(|node| {
OperatorKind::of(&node.payload) == OperatorKind::SummaryTumblingWindowChain
})
.collect();
assert_eq!(chains.len(), 4);
for chain in &chains {
assert_eq!(capabilities.exclusion_of(chain), None, "node {}", chain.id);
}
let mut counter = serde_json::to_value(chains[0]).unwrap();
counter["payload"]["ASAP"]["SummaryTumblingWindowChain"]["summary_type"] =
serde_json::json!({"ExactAggregate": ["Counter", "Counter"]});
let counter: PhysicalASAPDAGNode = serde_json::from_value(counter).unwrap();
assert_eq!(
capabilities.exclusion_of(&counter),
Some(ExclusionSource::CounterResetsNotHandled)
);
}

/// `executor`, with each entry replaced by its `summary`, is the
/// planner's `DeploymentCapabilities` (its serde denies unknown fields).
#[test]
Expand Down
48 changes: 43 additions & 5 deletions asap-precompute-rs/src/otap/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1839,18 +1839,19 @@ struct DecodeMetric {

impl DecodeMetric {
/// The sketch type of the metric's states: a collector view's, from θ
/// in its view state key; a hand-configured sketch's, from the
/// Resource's `sketch.type`.
/// in its view state key (a key without θ is an exact accumulator's);
/// a hand-configured sketch's, from the Resource's `sketch.type`.
fn sketch_type(&self, schema: &DecodeSchema) -> Result<SketchType, CodecError> {
let Some(view) = &self.view else {
return parse_sketch_type(schema.sketch_type.as_deref());
};
let key: ViewStateKey = serde_json::from_str(&view.view_state_key).map_err(|e| {
CodecError::Decode(format!("metric {}: {META_VIEW_STATE_KEY}: {e}", self.name))
})?;
key.logical_properties
.parameters()
.and_then(super::planner_views::sketch_type_spec_of)
let Some(parameters) = key.logical_properties.parameters() else {
return Ok(SketchType::Exact);
};
super::planner_views::sketch_type_spec_of(parameters)
.map(|spec| spec.sketch_type)
.ok_or_else(|| {
CodecError::Decode(format!(
Expand Down Expand Up @@ -2562,6 +2563,43 @@ mod tests {
assert_eq!(env.payload, state.payload);
}

/// An exact view's state (the exact fixture's `Sum`, whose key has no
/// θ) round-trips as `Exact` with its view-layout bytes and count.
#[test]
fn exact_view_state_round_trips_through_otap_arrow() {
let views = super::super::planner_views::parse(include_str!(
"../../tests/fixtures/collector-views/exact/collector-views.json"
))
.unwrap();
let sum = views
.views
.iter()
.find(|v| {
v.summary
== asap_types::summary_type::SummaryFamily::Exact(
asap_types::summary_type::ExactKind::Sum,
)
})
.unwrap();
let mut state = q01_state(1_767_225_600_000);
state.sketch_type = SketchType::Exact;
state.view = Some(Arc::clone(&sum.identity));
state.metric_name = sum.output_metric_name.clone();
// Sum 7.5, no compensation, seen.
state.payload = [7.5f64.to_le_bytes(), 0f64.to_le_bytes(), 1u64.to_le_bytes()].concat();
state.count = 3;
let pdata = encode_envelopes_to_pdata(std::slice::from_ref(&state)).expect("encode");
let decoded = decode_pdata_to_observations(pdata).expect("decode");
let [obs] = decoded.observations.as_slice() else {
panic!("one state");
};
let env = obs.value.envelope.as_ref().expect("a state");
assert_eq!(env.view, state.view);
assert_eq!(env.sketch_type, SketchType::Exact, "a key without θ");
assert_eq!(env.payload, state.payload);
assert_eq!(env.count, 3);
}

/// Through OTAP Arrow and the OTAP → OTLP conversion, the state is an
/// OTLP Summary point with the raw sample's structure, and the view
/// identity is `Metric.metadata`; decoding the OTLP request again gives
Expand Down
Loading
Loading