|
| 1 | +//! Complete a backend-local planning snapshot with deterministic unit-cost |
| 2 | +//! quotes for the PromQL compliance harness. Candidate identities and demand |
| 3 | +//! still come from the production planner and physical compiler. |
| 4 | +
|
| 5 | +use control_plane::physical::{ |
| 6 | + compiler::{ |
| 7 | + BackendLocalPlanningInput, PhysicalPlanCompiler, BACKEND_REVISION, PLANNER_REVISION, |
| 8 | + }, |
| 9 | + workload_cost::{ |
| 10 | + enumerate_exact_and_materialized_candidates, manifest, WorkloadCostEvidence, WorkloadQuote, |
| 11 | + }, |
| 12 | +}; |
| 13 | +use std::{collections::BTreeMap, path::Path}; |
| 14 | + |
| 15 | +fn quote_snapshot( |
| 16 | + mut snapshot: BackendLocalPlanningInput, |
| 17 | +) -> Result<BackendLocalPlanningInput, String> { |
| 18 | + if snapshot.workload_cost_evidence.is_some() { |
| 19 | + return Err("planning snapshot already contains workload_cost_evidence".into()); |
| 20 | + } |
| 21 | + let observed_at_unix_ms = snapshot.environment.observed_at_unix_ms; |
| 22 | + let valid_for_ms = snapshot.environment.max_evidence_age_ms; |
| 23 | + let (request, environment) = snapshot |
| 24 | + .clone() |
| 25 | + .into_physical_compilation_request() |
| 26 | + .map_err(|error| error.to_string())?; |
| 27 | + let quotes = enumerate_exact_and_materialized_candidates(request) |
| 28 | + .map_err(|error| error.to_string())? |
| 29 | + .into_iter() |
| 30 | + .filter_map(|candidate| { |
| 31 | + let plan = PhysicalPlanCompiler |
| 32 | + .compile_promql(candidate.clone(), environment.clone()) |
| 33 | + .ok()?; |
| 34 | + // This execution suite must exercise maintained candidates when legal. |
| 35 | + let unit_cost = if plan.precompute_plan.materializations.is_empty() { |
| 36 | + 1e12 |
| 37 | + } else { |
| 38 | + 1.0 |
| 39 | + }; |
| 40 | + let manifest = manifest(&plan, &candidate.queries).ok()?; |
| 41 | + Some(WorkloadQuote { |
| 42 | + unit_costs: manifest |
| 43 | + .components |
| 44 | + .keys() |
| 45 | + .map(|key| (key.clone(), unit_cost)) |
| 46 | + .collect::<BTreeMap<_, _>>(), |
| 47 | + manifest, |
| 48 | + executable: true, |
| 49 | + }) |
| 50 | + }) |
| 51 | + .collect(); |
| 52 | + snapshot.workload_cost_evidence = Some(WorkloadCostEvidence { |
| 53 | + backend_revision: BACKEND_REVISION.into(), |
| 54 | + planner_revision: PLANNER_REVISION.into(), |
| 55 | + data_snapshot_id: "promql-compliance".into(), |
| 56 | + model_version: "promql-compliance-unit-costs".into(), |
| 57 | + observed_at_unix_ms, |
| 58 | + valid_for_ms, |
| 59 | + quotes, |
| 60 | + }); |
| 61 | + Ok(snapshot) |
| 62 | +} |
| 63 | + |
| 64 | +fn main() -> Result<(), String> { |
| 65 | + let mut args = std::env::args_os().skip(1); |
| 66 | + let input = args |
| 67 | + .next() |
| 68 | + .ok_or("usage: control_plane_quote_snapshot INPUT OUTPUT")?; |
| 69 | + let output = args |
| 70 | + .next() |
| 71 | + .ok_or("usage: control_plane_quote_snapshot INPUT OUTPUT")?; |
| 72 | + if args.next().is_some() { |
| 73 | + return Err("usage: control_plane_quote_snapshot INPUT OUTPUT".into()); |
| 74 | + } |
| 75 | + let snapshot = std::fs::read(&input) |
| 76 | + .map_err(|error| format!("read {}: {error}", Path::new(&input).display()))?; |
| 77 | + let snapshot = |
| 78 | + serde_json::from_slice(&snapshot).map_err(|error| format!("decode snapshot: {error}"))?; |
| 79 | + let quoted = quote_snapshot(snapshot)?; |
| 80 | + std::fs::write( |
| 81 | + &output, |
| 82 | + serde_json::to_vec_pretty("ed).map_err(|error| error.to_string())?, |
| 83 | + ) |
| 84 | + .map_err(|error| format!("write {}: {error}", Path::new(&output).display()))?; |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn adds_complete_candidate_quotes_to_a_snapshot() { |
| 94 | + let fixture = |
| 95 | + include_str!("../../../docs/examples/asapquery-compatibility-demo-snapshot.json"); |
| 96 | + let mut snapshot: BackendLocalPlanningInput = serde_json::from_str(fixture).unwrap(); |
| 97 | + snapshot.workload_cost_evidence = None; |
| 98 | + let quoted = quote_snapshot(snapshot).unwrap(); |
| 99 | + assert!(!quoted |
| 100 | + .workload_cost_evidence |
| 101 | + .as_ref() |
| 102 | + .unwrap() |
| 103 | + .quotes |
| 104 | + .is_empty()); |
| 105 | + let plan = quoted.compile_promql().unwrap(); |
| 106 | + assert!( |
| 107 | + !plan.precompute_plan.materializations.is_empty(), |
| 108 | + "deterministic compliance quotes should select a maintained candidate" |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments