Skip to content
Open
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
16 changes: 16 additions & 0 deletions crates/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ pub mod planning {
selected_operator_roots(&run_all_stages(root, accuracy))
}

/// [`logical_candidates`] with summaries' CPU costed free
/// ([`summary_cpu_free`]) and accuracy enforced: the plan reads a summary
/// candidate wherever Stage 1 offers one that meets `accuracy`, whether
/// or not the default unit costs make it cheaper than the exact plan.
/// For tests of a summary plan's shape. Example: `quantile(0.99,
/// rate(c[5m]))` at 1,000 series selects the exact sort under the
/// default costs and the KLL over the finalized Rate here.
pub fn logical_candidates_with_summary_cpu_free(
root: Rc<OperatorNode>,
accuracy: AccuracyTarget,
) -> Vec<Rc<OperatorNode>> {
let settings = summary_cpu_free();
let models = asap_planner::PlanningModels::builtin().with_cost_inputs(&settings);
selected_operator_roots(&run_all_stages_with(root, accuracy, models))
}

/// [`logical_candidates`] with accuracy not enforced
/// (`ignore_accuracy_guarantees`): no readout gets a required accuracy.
/// For tests of a rewrite's structure or execution over a summary whose
Expand Down
83 changes: 70 additions & 13 deletions crates/integration-tests/tests/joint_search_equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

use asap_cost::{ClusterLimits, ClusterObjective, CostInputs, CostModel};
use asap_devtools::planning_stages::{lower_promql_roots, lower_sql_roots};
use asap_planner::{run_planning, PlanSearchSettings, PlanningModels, PlanningRun};
use asap_planner::{
run_planning, CandidateExclusion, ErrorSelectingPlan, PlanSearchSettings, PlanningModels,
PlanningRun, SelectionError,
};
use asap_types::accuracy::AccuracyTarget;
use asap_types::deployment_capabilities::DeploymentCapabilities;
use asap_types::ir::QueryRoot;
use asap_types::ir::operator::AggIntent;
use asap_types::ir::{NonASAPOp, QueryRoot};
use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload;
use asap_types::workload::{
DataArrival, DataWorkload, DurationMs, PlanningWorkload, Predictability, PrometheusMetricType,
Query, QueryLanguage, QueryRequirements, QueryWorkload, QueryWorkloadEntry, RepeatedDemand,
Expand Down Expand Up @@ -321,9 +326,38 @@ fn repeated_sql_queries_preserve_separate_output_references() {
assert_valid(&run, 3);
}

// Request work on literal outputs still consumes CPU and latency budgets even
// when there is no operator node to which a fee could be attributed.
// Request work on a literal output is charged though no operator node
// owns it. `1` every 1 ms is 1,000 requests per second; at 2 ms
// (2,000,000 ns) per request and the default 1 ns per CPU operation that
// is 2,000,000 operations each, 2e9 per second. The default unit cost
// prices one operation at 1e-6 (one cost unit is one core-millisecond),
// so the plan costs 2,000 per second, all of it request overhead.
#[test]
fn rootless_request_overhead_is_charged() {
let workload = workload_of(&[("1", 1)], AccuracyTarget::exact());
let roots = lower_promql_roots(&workload).unwrap();
let mut settings = CostInputs::default();
settings.measured_operation_costs.ns_per_evaluation = 2_000_000.0;
let entries: Vec<_> = workload.query_workload.entries().collect();
let result = run(
&roots,
&entries,
workload.data_workload.as_ref().unwrap(),
PlanningModels::builtin().with_cost_inputs(&settings),
false,
);
let (plan, selection) = result.selection.as_ref().unwrap();
assert!(plan.nodes.is_empty(), "a literal needs no operator node");
let cost = selection.selected_plan_cost_per_second;
assert!((cost - 2_000.0).abs() < 1e-6, "{cost}");
}

// The same request overhead counts against a latency bound and a cluster's
// CPU: 2 ms per request is over `max_latency_ms` 1, and 2 cores of work
// are over 1 core. Stage 3 checks neither yet: latency (L) and resource
// budgets are outside the selection MILP, and it refuses cost model 3.
#[test]
#[ignore = "Stage 3 checks no latency bound and no cluster capacity (#1316)"]
fn rootless_request_overhead_respects_capacity_and_latency() {
let mut workload = workload_of(&[("1", 1)], AccuracyTarget::exact());
let roots = lower_promql_roots(&workload).unwrap();
Expand All @@ -350,30 +384,53 @@ fn rootless_request_overhead_respects_capacity_and_latency() {
PlanningModels::builtin().with_cost_inputs(&settings),
false,
);
assert!(result.selection.is_err());
// Refused because the request overhead does not fit, not because
// the cost model is unsupported.
assert!(matches!(
&result.selection,
Err(SelectionError::Selection(error))
if !matches!(error, ErrorSelectingPlan::UnsupportedCostModel(_))
));
}
}

// Unsupported exact operators cannot become accepted merely because no summary
// candidate exists; failure must leave the exported physical plan absent.
// `absent_over_time` is lowered as a per-entity aggregate without an exact
// per-entity kernel.
// An exact operator the executor does not run is planned like any other,
// from raw rows, until the deployment declares it unsupported; then no
// plan is returned. `absent_over_time(m[5m])` exactly has one plan: a
// per-series `Aggregate[AbsentOverTime]` over the 5 m of raw rows (no
// summary answers it). Excluding that measure leaves no candidate.
#[test]
fn unsupported_exact_operation_never_returns_an_unvalidated_plan() {
fn unsupported_exact_operation_declared_by_the_deployment_returns_no_plan() {
let workload = workload_of(
&[("absent_over_time(m[5m])", 60_000)],
AccuracyTarget::exact(),
);
let settings = CostInputs::default();
let planned = run_workload(&workload, &settings, &DeploymentCapabilities::default());
let (plan, _) = planned.selection.as_ref().unwrap();
assert!(plan.nodes.iter().any(|node| matches!(
&node.payload,
PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Aggregate { measures, .. })
if measures.as_slice() == [AggIntent::AbsentOverTime]
)));

let mut deployment = DeploymentCapabilities::default();
deployment.excluded_candidates.push(CandidateExclusion {
aggregate_measure: Some("absent_over_time".to_string()),
..Default::default()
});
let roots = lower_promql_roots(&workload).unwrap();
let entries: Vec<_> = workload.query_workload.entries().collect();
let result = run(
let refused = run(
&roots,
&entries,
workload.data_workload.as_ref().unwrap(),
PlanningModels::builtin(),
PlanningModels::builtin()
.with_cost_inputs(&settings)
.with_deployment_capabilities(&deployment),
false,
);
assert!(result.selection.is_err());
assert!(refused.selection.is_err());
}

// Deterministic small mixed workloads exercise exact temporal operators, counter
Expand Down

This file was deleted.

121 changes: 3 additions & 118 deletions crates/integration-tests/tests/materialized_views.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
//! What a runtime derives from an installable plan's DAG alone: its
//! materialized views and its raw-data retention.
//! How an installable plan file is read back: a file of an older wire
//! version is refused for its version.

use std::collections::HashSet;

use asap_planner::{
InstallablePlan, InstallablePlanError, MaterializedViewRefresh, RefreshWindow, TimeWindow,
INSTALLABLE_PLAN_WIRE_VERSION,
};
use asap_types::plan::physical_asap_dag::PhysicalASAPDAG;

use asap_types::ir::{NonASAPOp, Operator};
use asap_planner::{InstallablePlan, InstallablePlanError, INSTALLABLE_PLAN_WIRE_VERSION};

use serde_json::Value;

Expand Down Expand Up @@ -44,110 +36,3 @@ fn a_plan_file_of_an_older_wire_version_is_refused_by_version() {
let read = InstallablePlan::from_json(&serde_json::to_string(&current).unwrap()).unwrap();
assert_eq!(read, current);
}

/// For installable plans captured before views were derived from the DAG (each the plan
/// file's DAG and its `stored_outputs` field), `materialized_views()` of
/// that DAG, with each view's `retention_ms` on its node, equals
/// `stored_outputs` field for field after the renames: `built_at` →
/// `refresh`, `inputs.stored_outputs` → `inputs.materialized_views`, and
/// each raw-sample window `(time_range_node_id, width_ms, end_offset_ms,
/// scan_node_id)` → `(source of the Scan, end_offset_ms, width_ms)`. The
/// cases cover views refreshed at ingestion and on first read, offset
/// windows, and views read by other views.
#[test]
fn materialized_views_equal_the_stored_outputs_main_listed() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/materialized_view_fixtures/stored_outputs_before_view_coverage.json");
let cases: Vec<Value> = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
assert!(!cases.is_empty());
let mut refreshes = HashSet::new();
let mut read_by_views = 0;
for case in &cases {
let name = case["name"].as_str().unwrap();
let mut dag: PhysicalASAPDAG = serde_json::from_value(case["dag"].clone()).unwrap();
let stored = case["stored_outputs"].as_array().unwrap();
for output in stored {
let id = output["node_id"].as_u64().unwrap() as usize;
let node = dag.nodes.iter_mut().find(|node| node.id == id).unwrap();
node.retention_ms = Some(output["retention_ms"].as_u64().unwrap());
}
let views = dag.materialized_views().unwrap();
assert_eq!(views.len(), stored.len(), "{name}");
let source_of = |scan: u64| match &dag
.nodes
.iter()
.find(|node| node.id == scan as usize)
.unwrap()
.payload
{
Operator::NonASAP(NonASAPOp::Scan { source, .. }) => source.clone(),
other => panic!("{name}: node {scan} is not a Scan: {other:?}"),
};
for (view, output) in views.iter().zip(stored) {
let refresh = match output["built_at"].as_str().unwrap() {
"ingestion_run" => MaterializedViewRefresh::AtIngestion,
"query_run" => MaterializedViewRefresh::OnFirstRead,
other => panic!("{name}: {other}"),
};
refreshes.insert(format!("{refresh:?}"));
assert_eq!(
view.node_id as u64,
output["node_id"].as_u64().unwrap(),
"{name}"
);
assert_eq!(view.refresh(), refresh, "{name}");
assert_eq!(
serde_json::to_value(view.coverage()).unwrap(),
output["coverage"],
"{name}"
);
assert_eq!(
view.retention_ms(),
output["retention_ms"].as_u64().unwrap(),
"{name}"
);
let views_read: Vec<u64> = output["inputs"]["stored_outputs"]
.as_array()
.unwrap()
.iter()
.map(|id| id.as_u64().unwrap())
.collect();
read_by_views += usize::from(!views_read.is_empty());
assert_eq!(
view.inputs
.materialized_views
.iter()
.map(|&id| id as u64)
.collect::<Vec<_>>(),
views_read,
"{name}"
);
// The raw-data windows it scans: each reads a source, and their
// union is its refresh window when it reads no view.
let windows: Vec<TimeWindow> = output["inputs"]["raw_sample_windows"]
.as_array()
.unwrap()
.iter()
.map(|window| {
source_of(window["scan_node_id"].as_u64().unwrap());
TimeWindow {
ends_before_evaluation_ms: window["end_offset_ms"].as_u64().unwrap(),
length_ms: window["width_ms"].as_u64().unwrap(),
}
})
.collect();
if views_read.is_empty() && !windows.is_empty() {
assert_eq!(
dag.refresh_window(view.node_id),
windows
.into_iter()
.reduce(TimeWindow::union)
.and_then(RefreshWindow::of_window),
"{name}"
);
}
}
}
assert_eq!(refreshes.len(), 2, "both refresh modes are covered");
assert!(read_by_views > 0, "a view read by another view is covered");
}
57 changes: 47 additions & 10 deletions crates/integration-tests/tests/plan_sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,55 @@ fn exact_coarse_sum_reuses_fine_sum() {
);
}

/// The 1m sum reads the newest tumbling window of the existing 2m chain.
/// The 1m sum reads the newest tumbling window of the 2m sum's chain: with
/// no raw data kept, the plan builds one tumbling-window chain and both
/// queries read it.
///
/// With raw data kept, each query is answered at query time from the raw
/// rows (0.086/s together, 0.059/s for the 2m sum alone): the shared chain
/// costs 2.16/s under the measured unit costs, so it is not selected.
///
/// The module's cost relation (at most 0.1% more than the 2m sum alone)
/// does not hold even with the chain: the 1m sum reads a different range
/// of the chain's 60 s windows (its newest one, against the 2m sum's
/// newest two), so it pays its own merge, finalize and sum over the 10,000
/// series every evaluation: 0.18/s + 0.42/s + 0.0007/s = 0.60/s, which is
/// the whole difference (2.16/s against 1.56/s alone). The reuse is the
/// shared chain, which this test checks.
#[test]
fn nested_exact_sum_reuses_newest_tumbling_windows() {
let related = fuzz_workload("nested_exact_sum", false);
let mut base = related.clone();
base.queries.truncate(1);
let base_cost = cost(&base);
let related_cost = cost(&related);
assert!(
related_cost <= base_cost * 1.001,
"{related_cost} vs {base_cost}"
);
let mut related = fuzz_workload("nested_exact_sum", false);
related.raw_data_retained = false;
let plan = selected_plan(&related, &CostInputs::default());
let chains: Vec<_> = plan
.nodes
.iter()
.filter(|node| {
matches!(
node.payload,
Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { .. })
)
})
.map(|node| node.id)
.collect();
assert_eq!(chains.len(), 1, "one chain: {plan:#?}");
for &root in &plan.roots {
assert!(reaches(&plan, root, chains[0]), "root {root}: {plan:#?}");
}
}

/// Whether `from` is `to` or reads it through the plan's edges.
fn reaches(
plan: &asap_types::plan::physical_asap_dag::PhysicalASAPDAG,
from: usize,
to: usize,
) -> bool {
from == to
|| plan
.edges
.iter()
.filter(|edge| edge.consumer == from)
.any(|edge| reaches(plan, edge.producer, to))
}

/// The slower copy reads the same maintained sum.
Expand Down
9 changes: 8 additions & 1 deletion crates/integration-tests/tests/promql_to_post_asap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use asap_integration_tests::fixtures::lower_promql;
use asap_integration_tests::physical_export::physical_asap_dag;
use asap_integration_tests::planning::{
logical_candidates, logical_candidates_ignoring_guarantees,
logical_candidates_with_summary_cpu_free,
};
use asap_types::ir::operator::arguments::Reduction;

Expand Down Expand Up @@ -62,12 +63,18 @@ fn uses_sketch(node: &OperatorNode, algorithm: &SketchAlgorithm) -> bool {

/// The logical candidates of `root` at `accuracy` that build a sketch of
/// `algorithm`, each with the `Project`s Stage 1 restores names with skipped.
/// Summaries' CPU is costed free, so Stage 3 selects the sketch plan these
/// shape tests inspect: under the default unit costs an exact plan is
/// cheaper on their 1,000 series.
fn candidates_using(
root: &Rc<OperatorNode>,
accuracy: AccuracyTarget,
algorithm: SketchAlgorithm,
) -> Vec<Rc<OperatorNode>> {
using(logical_candidates(Rc::clone(root), accuracy), algorithm)
using(
logical_candidates_with_summary_cpu_free(Rc::clone(root), accuracy),
algorithm,
)
}

/// [`candidates_using`] with every output accepted whatever its guarantee:
Expand Down
10 changes: 7 additions & 3 deletions crates/planner/tests/summary_sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,18 @@ fn unique_deployments(output: &PlanOutput) -> usize {
seen.len()
}

/// Two identical ungrouped queries: their identical producers (rate, then
/// sum) are shared.
/// Two identical ungrouped queries: their identical producers are shared,
/// so the plan deploys one state for both. That state is the per-series
/// rate's exact accumulator. The sum over the 1,000 finalized rates is a
/// plain query-time `Aggregate`, not a second state: an exact Sum state
/// over the same 1,000 values does the same additions and also builds and
/// finalizes a state, so it never costs less.
#[tokio::test]
async fn identical_ungrouped_queries_share_their_producers() {
let query = ("sum(rate(x[5m]))", 0.01);
let output = plan_promql(&[query, query]).await;
assert!(same_states(&states(&output)));
assert_eq!(unique_deployments(&output), 2);
assert_eq!(unique_deployments(&output), 1);
}

/// The SQL frontend reaches the same sharing for two copies of one filtered
Expand Down
Loading