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
1 change: 1 addition & 0 deletions crates/devtools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn lower_promql_with_sample_interval(
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload::sampled_every(
asap_frontend_promql::promql_metric_names(query)?,
Expand Down
1 change: 1 addition & 0 deletions crates/devtools/src/planner_fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl FuzzWorkload {
query_batch: None,
repeating_queries: Some(self.queries.iter().map(|q| q.entry.clone()).collect()),
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(self.data.clone()),
}
Expand Down
1 change: 1 addition & 0 deletions crates/executor/tests/current_series_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ fn population_fixture_compiles_with_dynamic_identity() {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload::sampled_every(
["m".to_string()],
Expand Down
1 change: 1 addition & 0 deletions crates/executor/tests/deployment_computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn lower_with(query: &str, accuracy: AccuracyTarget) -> Rc<planner_types::ir::Op
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload::sampled_every(
asap_frontend_promql::promql_metric_names(query).unwrap_or_default(),
Expand Down
1 change: 1 addition & 0 deletions crates/executor/tests/planspace_series_identity_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn lower(query: &str, accuracy: &AccuracyTarget) -> Rc<OperatorNode> {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload::sampled_every(
asap_frontend_promql::promql_metric_names(query).unwrap_or_default(),
Expand Down
1 change: 1 addition & 0 deletions crates/frontend-metricsql/tests/lowering_semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn workload(language: QueryLanguage, query: &str) -> PlanningWorkload {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload {
arrival: DataArrival::Unknown,
Expand Down
1 change: 1 addition & 0 deletions crates/frontend-promql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ mod tests {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload {
arrival: DataArrival::Unknown,
Expand Down
2 changes: 2 additions & 0 deletions crates/frontend-promql/tests/promql_lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ fn batch_lowers_each_entry_and_reads_per_query_accuracy() {
]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
// Range selectors only: no sample interval is needed.
data_workload: None,
Expand All @@ -1166,6 +1167,7 @@ fn batch_rejects_non_promql_language() {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: None,
};
Expand Down
1 change: 1 addition & 0 deletions crates/frontend-promql/tests/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn workload(query: &str, accuracy: AccuracyTarget) -> PlanningWorkload {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload::sampled_every(
asap_frontend_promql::promql_metric_names(query).unwrap_or_default(),
Expand Down
1 change: 1 addition & 0 deletions crates/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub mod fixtures {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload::sampled_every(
asap_frontend_promql::promql_metric_names(query).unwrap_or_default(),
Expand Down
2 changes: 2 additions & 0 deletions crates/integration-tests/tests/joint_search_equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn workload_of(queries: &[(&str, u32)], accuracy: AccuracyTarget) -> PlanningWor
.collect(),
),
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload {
arrival: DataArrival::Ingesting {
Expand Down Expand Up @@ -281,6 +282,7 @@ fn example2() -> (PlanningWorkload, asap_frontend_sql::SqlCatalog) {
.collect(),
),
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
// SQL needs no sample interval; `flows` holds 10,000,000 groups
// (one per source IP).
Expand Down
2 changes: 2 additions & 0 deletions crates/integration-tests/tests/pass1_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn promql(query: &str) -> QueryRoot {
}]),
repeating_queries: None,
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(DataWorkload::sampled_every(
asap_frontend_promql::promql_metric_names(query).unwrap_or_default(),
Expand Down Expand Up @@ -398,6 +399,7 @@ async fn avg_as_sum_and_count(
&without_candidates(physical, own),
&[single_query_entry(accuracy())],
&[],
0,
&declared_data(&exact),
&asap_cost::CostInputs::default(),
asap_types::deployment_capabilities::FinalResultsCache::NotCached,
Expand Down
12 changes: 10 additions & 2 deletions crates/planner/src/installable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,20 @@ impl From<InstallableDagError> for InstallablePlanError {
/// installation compiles the selected plan, and one the executor cannot run
/// returns `PlanError::Installable` with
/// [`InstallablePlanError::MaterializedViewNotExecutable`] or
/// [`InstallablePlanError::QueryRunNotExecutable`].
/// [`InstallablePlanError::QueryRunNotExecutable`]. The workload's optional
/// queries (`QueryWorkload::optional_query_count`, the last ones) are not
/// answered: the plan, its query texts and its report cover the others.
pub async fn plan_installable(
input: UserInput<'_>,
) -> Result<(InstallablePlan, SelectedPlanReport), PlanError> {
let capabilities = executor_summaries_when_unrestricted(input.models.deployment_capabilities);
let entries: Vec<_> = input.workload.query_workload.entries().collect();
// The plan answers the queries but the optional ones, which come last.
let query_workload = &input.workload.query_workload;
let answered = query_workload
.entries()
.count()
.saturating_sub(query_workload.optional_query_count);
let entries: Vec<_> = query_workload.entries().take(answered).collect();
let models = input.models.with_deployment_capabilities(&capabilities);
let input = UserInput {
workload: input.workload,
Expand Down
63 changes: 43 additions & 20 deletions crates/planner/src/intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use serde::{Deserialize, Serialize};

use crate::installable::{executor_summaries_when_unrestricted, installable_with_report};
use crate::{
e2e_plan, plan_installable, FrontendInput, InstallablePlan, PlanError, PlanningModels,
SelectedPlanReport, SqlCatalog, UserInput,
e2e_plan, plan_installable, CostModel, FrontendInput, InstallablePlan, PlanError,
PlanningModels, SelectedPlanReport, SqlCatalog, UserInput,
};

/// The most values one binding is planned with: a larger IN-list is
Expand All @@ -56,8 +56,10 @@ pub enum IntentPlanningMode {
IntentOnDemandHoles,
/// The queries planned together, and a state covering every value a
/// hole may take (its lightcone) may be kept when cheaper: planned
/// with one lightcone query per IN-list reader and without, the
/// cheaper plan kept.
/// once with one optional lightcone query per IN-list reader, whose
/// states the plan may read but which it does not answer (cost model
/// 1); under cost model 3, planned with the lightcone queries and
/// without, the better plan kept.
#[default]
IntentFull,
}
Expand All @@ -80,7 +82,8 @@ pub struct IntentPlan {
/// The selected plan, as `plan_installable` returns it.
pub plan: InstallablePlan,
/// Plan queries the planner added (lightcone queries), which a round
/// never issues.
/// never issues: the plan's roots past the intent's queries. Empty
/// under cost model 1, whose plan answers no lightcone query.
pub lightcone_query_indices: Vec<usize>,
/// One per clock of each intent (its queries on one interval), in
/// program order, an intent's clocks in the order of their first query.
Expand Down Expand Up @@ -517,6 +520,7 @@ pub async fn lower_intents(
query_batch: None,
repeating_queries: Some(entries),
group_latency_bounds,
optional_query_count: 0,
},
data_workload: Some(data.clone()),
},
Expand Down Expand Up @@ -730,24 +734,31 @@ pub async fn plan_intents(
let planned = plan_joint(&lowered).await?;
(lowered, planned)
}
// With the lightcone queries and without, the better plan: a
// lightcone query is a root with its own (small) request cost, so
// the plan with them is kept only when their states pay off.
// Under cost model 1, one plan with the lightcone queries optional
// (`QueryWorkload::optional_query_count`): the plan may read their
// states but answers none of them, so it is no dearer than the plan
// without them or with all of them answered. Under cost model 3
// (no prices), the plans with the lightcone queries and without,
// the better kept. A plan with them that fails falls back to the
// plan without them.
IntentPlanningMode::IntentFull => {
let without = lower_intents(
intents,
catalog,
data,
&IntentPlanningOptions {
mode: IntentPlanningMode::IntentOnDemandHoles,
..options.clone()
},
)
.await?;
let planned_without = plan_joint(&without).await?;
let without = without_lightcones(&lowered);
if lowered.lightcone_query_indices.is_empty() {
(without, planned_without)
let planned = plan_joint(&without).await?;
(without, planned)
} else if matches!(models.cost_inputs.cost_model, CostModel::UsageOverTime(_)) {
let mut once = lowered.clone();
once.workload.query_workload.optional_query_count =
lowered.lightcone_query_indices.len();
match plan_joint(&once).await {
Ok(planned) => (without, planned),
Err(_) => {
let planned = plan_joint(&without).await?;
(without, planned)
}
}
} else {
let planned_without = plan_joint(&without).await?;
match plan_joint(&lowered).await {
Ok(planned_with) if better(&planned_with.1, &planned_without.1, &lowered) => {
(lowered, planned_with)
Expand Down Expand Up @@ -783,6 +794,17 @@ pub async fn plan_intents(
))
}

/// `lowered` without its lightcone queries: the workload and rounds
/// `IntentOnDemandHoles` lowers to.
fn without_lightcones(lowered: &LoweredIntents) -> LoweredIntents {
let mut without = lowered.clone();
if let Some(entries) = without.workload.query_workload.repeating_queries.as_mut() {
entries.truncate(entries.len() - lowered.lightcone_query_indices.len());
}
without.lightcone_query_indices.clear();
without
}

/// Whether `with` (the plan with lightcone queries) is better than
/// `without`: a lower cost per second, or under cost model 3 (no prices) a
/// lower mean latency of the intent's queries (lightcone queries left out).
Expand Down Expand Up @@ -836,6 +858,7 @@ async fn plan_query_at_a_time(
query_batch: None,
repeating_queries: Some(vec![entry.clone()]),
group_latency_bounds: Vec::new(),
optional_query_count: 0,
},
data_workload: Some(data.clone()),
};
Expand Down
3 changes: 2 additions & 1 deletion crates/planner/src/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ pub fn optimize(
input: OptimizationInput<'_>,
) -> Result<PlanOutput, OptimizeError> {
input.validate()?;
let expected_len = input.workload.len();
// An optional query (`QueryWorkload::optional_query_count`) has no root.
let expected_len = input.workload.len() - input.workload.query_workload().optional_query_count;

let output = pass.optimize(input)?;
check_contract(&output, pass.name(), expected_len)?;
Expand Down
35 changes: 32 additions & 3 deletions crates/planner/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,32 @@ pub fn run_planning_until(
models: PlanningModels<'_>,
settings: PlanSearchSettings,
until: RunUntil,
) -> Result<PlanningRun, PerComputationCandidateError> {
run_stages(
roots,
entries,
group_latency_bounds,
0,
data,
models,
settings,
until,
)
}

/// [`run_planning_until`] with the last `optional_query_count` queries
/// optional (`QueryWorkload::optional_query_count`): Stage 3 answers only
/// the others.
#[allow(clippy::too_many_arguments)]
fn run_stages(
roots: &[QueryRoot],
entries: &[QueryWorkloadEntry],
group_latency_bounds: &[GroupLatencyBound],
optional_query_count: usize,
data: &DataWorkload,
models: PlanningModels<'_>,
settings: PlanSearchSettings,
until: RunUntil,
) -> Result<PlanningRun, PerComputationCandidateError> {
let started = Instant::now();
for root in roots {
Expand Down Expand Up @@ -265,6 +291,7 @@ pub fn run_planning_until(
physical,
entries,
group_latency_bounds,
optional_query_count,
data,
models.cost_inputs,
models.deployment_capabilities.final_results_cache(),
Expand Down Expand Up @@ -322,16 +349,18 @@ impl OptimizationPass for Stage1ToStage3Pass {
.map(|(index, expr)| (*index, QueryRoot::Scalar(expr.clone()))),
);
roots.sort_by_key(|(index, _)| *index);
let roots = roots.into_iter().map(|(_, root)| root).collect();
let roots: Vec<QueryRoot> = roots.into_iter().map(|(_, root)| root).collect();
let entries: Vec<_> = workload.query_workload().entries().collect();
let data = workload.data_workload().cloned().unwrap_or_default();
let run = run_planning(
roots,
let run = run_stages(
&roots,
&entries,
&workload.query_workload().group_latency_bounds,
workload.query_workload().optional_query_count,
&data,
input.models,
PlanSearchSettings::default(),
RunUntil::Stage3,
)?;
let (plan, selection) = run.selection?;
let mut output = PlanOutput::new(plan);
Expand Down
Loading