Skip to content
Merged
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
2 changes: 1 addition & 1 deletion docs/rqe_optimizer_TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ implemented, what is next, and what is intentionally deferred.
cardinality per label set. The arrival rate is derived from them, and
`validate_facts` reports bad facts up front.
- Eligibility enforces capability, metric, spatial filter, grouping, alignment, and the RQE's
measured `accuracy_metric` constraint.
measured accuracy, by each family's own metric, against the RAQE's SLA.
- Candidate generation uses legal windows and subset gcds of query intervals,
keeping only multiples of the scrape interval. A deployment may serve
multiple compatible RQEs.
Expand Down
11 changes: 5 additions & 6 deletions docs/rqe_sketch_deployment_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ Let `R = {r_1, ..., r_n}` be the RAQE workload. Each RAQE `r_i` provides:
| `T_i` | Query slide: how often the RAQE runs. This is the current `interval` field. |
| `metric_i` | Metric the RAQE reads. |
| `G_i` | Group-by label set (`grouping_labels`). |
| `accuracy_metric_i` | Accuracy measurement to check. |
| `tol_i` | Accuracy threshold. |
| `direction_i` | Whether lower or higher values are better. |
| `tol_i` | Accuracy threshold, checked against each candidate family's own metric. |

For each metric, the caller provides `MetricFacts`:

Expand Down Expand Up @@ -203,8 +201,8 @@ An RAQE `r_i` is eligible for a candidate `D` when:

1. `cap_i = D.capability`, `metric_i = D.metric` and `G_i = D.G`.
2. `D.x % D.y = 0`, `S_i % D.x = 0`, and `T_i % D.y = 0`.
3. The accuracy of `D` for `r_i` in `accuracy_metric_i` clears `tol_i` in
`direction_i`.
3. The accuracy of `D` for `r_i`, in the accuracy metric of `D`'s family
(`family_properties`, which also gives the direction), clears `tol_i`.

Exact accumulators take that accuracy from the cost table, at both measured
merge counts bracketing the query's `S_i / D.x` windows (count 1 is the
Expand All @@ -221,7 +219,8 @@ lookback: series per group times scrapes per lookback.
accuracy.
- Between checkpoints, the worse neighbour. Below the first checkpoint, no
accuracy. Past the last, the plateau if the point saturated, else none.
- The curve's `error_metric` must be `accuracy_metric_i`.
- The curve's `error_metric` must be the family's metric; `SaturationCurves::load`
refuses a curve for another metric as a stale study.
- The window size doesn't matter: a merged answer reads the curve at the
lookback's item count, like a single sketch. KLL and top-k merge lossily;
their merge penalty is #158.
Expand Down
46 changes: 11 additions & 35 deletions rqe-optimizer/examples/small_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,10 @@ use rqe_optimizer::milp::{minimize, Objective};
use rqe_optimizer::pareto::{pareto_front, ParetoFront};
use rqe_optimizer::saturation::{DataShape, SaturationCurves};
use rqe_optimizer::{
validate_facts, AccuracyDirection, AtomicCostTable, Capability, Deployment, LabelSet,
MetricFacts, Raqe, WorkloadFacts,
validate_facts, AtomicCostTable, Capability, Deployment, LabelSet, MetricFacts, Raqe,
WorkloadFacts,
};

/// Accuracy metric keys the real comparators actually report (checked
/// against `aqpbm-core/src/accuracy/{aggregate,quantile,cardinality}.rs`).
/// These three are lower-is-better errors; each RAQE below pairs its metric with the matching
/// [`AccuracyDirection`] so the comparison can't be applied the wrong way
/// round.
///
/// Request rate is served by an exact counter accumulator, so its error
/// should be zero; the bound is there to reject anything that isn't.
const RATE_ERR: &str = "relative_error";
const RANK_ERR: &str = "mean_rank_err";
const CARDINALITY_ERR: &str = "relative_error";
/// Top-k's metric is the odd one out: a *score*, not an error, so its
/// tolerance below is a floor and its direction is `HigherIsBetter`.
const TOPK_PRECISION: &str = "precision_at_k";

const COST_TABLE_FILE: &str = "rqe_atomic_costs.json";

const REQUESTS: &str = "http_requests_total";
Expand Down Expand Up @@ -150,6 +135,13 @@ fn data_shape(distinct_keys: f64) -> DataShape {
}
}

/// Each RAQE's `accuracy_sla` is checked against the metric its candidate
/// family reports (`rqe_optimizer::family_properties`): rank error for KLL,
/// relative value error for DDSketch, relative error for the exact counter
/// and HLL, precision@k for top-k.
///
/// Request rate is served by an exact counter accumulator, so its error
/// should be zero; the bound is there to reject anything that isn't.
fn raqes() -> Vec<Raqe> {
let se = label_set(&["service", "endpoint"]);
let s = label_set(&["service"]);
Expand All @@ -164,9 +156,7 @@ fn raqes() -> Vec<Raqe> {
metric: requests(),
spatial_filter: String::new(),
grouping_labels: se.clone(),
accuracy_metric: RATE_ERR.to_string(),
accuracy_sla: 0.1,
accuracy_direction: AccuracyDirection::LowerIsBetter,
latency_sla_ms: None,
},
Raqe {
Expand All @@ -177,9 +167,7 @@ fn raqes() -> Vec<Raqe> {
metric: requests(),
spatial_filter: String::new(),
grouping_labels: se.clone(),
accuracy_metric: RATE_ERR.to_string(),
accuracy_sla: 0.1,
accuracy_direction: AccuracyDirection::LowerIsBetter,
latency_sla_ms: None,
},
Raqe {
Expand All @@ -190,9 +178,7 @@ fn raqes() -> Vec<Raqe> {
metric: requests(),
spatial_filter: String::new(),
grouping_labels: se.clone(),
accuracy_metric: RATE_ERR.to_string(),
accuracy_sla: 0.1,
accuracy_direction: AccuracyDirection::LowerIsBetter,
latency_sla_ms: None,
},
Raqe {
Expand All @@ -203,9 +189,7 @@ fn raqes() -> Vec<Raqe> {
metric: duration(),
spatial_filter: String::new(),
grouping_labels: se.clone(),
accuracy_metric: RANK_ERR.to_string(),
accuracy_sla: 0.05,
accuracy_direction: AccuracyDirection::LowerIsBetter,
latency_sla_ms: None,
},
Raqe {
Expand All @@ -216,9 +200,7 @@ fn raqes() -> Vec<Raqe> {
metric: duration(),
spatial_filter: String::new(),
grouping_labels: se.clone(),
accuracy_metric: RANK_ERR.to_string(),
accuracy_sla: 0.05,
accuracy_direction: AccuracyDirection::LowerIsBetter,
latency_sla_ms: None,
},
Raqe {
Expand All @@ -229,9 +211,7 @@ fn raqes() -> Vec<Raqe> {
metric: duration(),
spatial_filter: String::new(),
grouping_labels: se.clone(),
accuracy_metric: RANK_ERR.to_string(),
accuracy_sla: 0.05,
accuracy_direction: AccuracyDirection::LowerIsBetter,
latency_sla_ms: None,
},
Raqe {
Expand All @@ -242,13 +222,11 @@ fn raqes() -> Vec<Raqe> {
metric: requests(),
spatial_filter: String::new(),
grouping_labels: s.clone(),
accuracy_metric: CARDINALITY_ERR.to_string(),
accuracy_sla: 0.1,
accuracy_direction: AccuracyDirection::LowerIsBetter,
latency_sla_ms: None,
},
// The higher-is-better case: 0.9 is a *floor* on precision@k, not a
// ceiling on error. Served by cms-heap, whose measured precision is
// The higher-is-better case: top-k's metric is precision@k, so 0.9 is
// a *floor*, not a ceiling on error. Served by cms-heap, whose measured precision is
// 1.0 at both configs under the zipf workload (it was 0.0 at both
// under uniform -- top-k is meaningless without real skew).
Raqe {
Expand All @@ -259,9 +237,7 @@ fn raqes() -> Vec<Raqe> {
metric: requests(),
spatial_filter: String::new(),
grouping_labels: se.clone(),
accuracy_metric: TOPK_PRECISION.to_string(),
accuracy_sla: 0.9,
accuracy_direction: AccuracyDirection::HigherIsBetter,
latency_sla_ms: None,
},
]
Expand Down
48 changes: 25 additions & 23 deletions rqe-optimizer/src/autosketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub fn window_adapter(raqe: &Raqe) -> (Millis, Millis) {
(raqe.lookback_ms, gcd(raqe.lookback_ms, raqe.interval_ms))
}

/// Search every RAQE independently. `accuracy` is the benchmark oracle: the
/// measured metric named by `raqe.accuracy_metric` for a configuration, or
/// `None` when it was not measured. Returns the IDs of unservable RAQEs.
/// Search every RAQE independently. `accuracy` is the benchmark oracle: a
/// configuration's accuracy in its family's metric ([`crate::accuracy_key`]),
/// or `None` when it was not measured. Returns the IDs of unservable RAQEs.
/// `allow_undeployable_families` as in
/// [`crate::Capability::candidate_families`].
pub fn plan(
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn search(
continue;
}
probes.push(index);
let feasible = raqe.meets_sla(accuracy(raqe, &costs[index]));
let feasible = raqe.meets_sla(&costs[index].sketch, accuracy(raqe, &costs[index]));
evaluated.insert(index, feasible);
if feasible && best.is_none_or(|b| cheaper(index, b)) {
best = Some(index);
Expand Down Expand Up @@ -340,11 +340,9 @@ mod tests {
use crate::analytical_cost_model::{score, PhaseCost};
use crate::candidates::is_eligible;
use crate::test_support::{facts, METRIC};
use crate::{table_accuracy_at, AccuracyDirection, Capability, LabelSet};
use crate::{table_accuracy_at, Capability, LabelSet};
use std::collections::BTreeSet;

const ERR: &str = "err";

fn cms(rows: u64, cols: u64) -> AtomicCostEntry {
AtomicCostEntry {
sketch: "cms-heap-topk-fastpath-vector2d".into(),
Expand All @@ -356,8 +354,11 @@ mod tests {
insert_cpu_secs: 1e-8 * rows as f64,
merge_cpu_secs: 1e-6,
query_cpu_secs: 1e-6,
// Error falls with width and depth, so feasibility is monotone.
query_accuracy: BTreeMap::from([(ERR.into(), 1.0 / (rows * cols) as f64)]),
// Precision rises with width and depth, so feasibility is monotone.
query_accuracy: BTreeMap::from([(
"precision_at_k".into(),
1.0 - 1.0 / (rows * cols) as f64,
)]),
merge_accuracy: BTreeMap::new(),
measured_at: None,
}
Expand All @@ -369,7 +370,8 @@ mod tests {
.collect()
}

fn raqe(id: &str, lookback: Millis, interval: Millis, tolerance: f64) -> Raqe {
/// A top-k RAQE whose precision floor is `1 - error_budget`.
fn raqe(id: &str, lookback: Millis, interval: Millis, error_budget: f64) -> Raqe {
Raqe {
id: id.into(),
capability: Capability::TopKByValue,
Expand All @@ -378,20 +380,18 @@ mod tests {
metric: METRIC.into(),
spatial_filter: String::new(),
grouping_labels: LabelSet::new(),
accuracy_metric: ERR.into(),
accuracy_sla: tolerance,
accuracy_direction: AccuracyDirection::LowerIsBetter,
accuracy_sla: 1.0 - error_budget,
latency_sla_ms: None,
}
}

fn table_accuracy(raqe: &Raqe, config: &AtomicCostEntry) -> Option<f64> {
config.query_accuracy.get(&raqe.accuracy_metric).copied()
fn table_accuracy(_: &Raqe, config: &AtomicCostEntry) -> Option<f64> {
Some(table_accuracy_at(config, 1))
}

fn cheapest_feasible(raqe: &Raqe, costs: &[AtomicCostEntry]) -> usize {
(0..costs.len())
.filter(|&i| raqe.meets_sla(table_accuracy_at(raqe, &costs[i], 1)))
.filter(|&i| raqe.meets_sla(&costs[i].sketch, table_accuracy(raqe, &costs[i])))
.min_by(|&a, &b| compare_resources(&costs[a], &costs[b]))
.unwrap()
}
Expand Down Expand Up @@ -497,16 +497,17 @@ mod tests {
insert_cpu_secs: 1e-8,
merge_cpu_secs: 1e-6,
query_cpu_secs: 1e-6,
query_accuracy: BTreeMap::from([(ERR.into(), 1.0 / k as f64)]),
query_accuracy: BTreeMap::from([("mean_rank_err".into(), 1.0 / k as f64)]),
merge_accuracy: BTreeMap::new(),
measured_at: None,
}
}

fn quantile_raqe(tolerance: f64) -> Raqe {
fn quantile_raqe(max_rank_err: f64) -> Raqe {
Raqe {
capability: Capability::Quantile,
..raqe("q", 3_600_000, 60_000, tolerance)
accuracy_sla: max_rank_err,
..raqe("q", 3_600_000, 60_000, 0.0)
}
}

Expand Down Expand Up @@ -549,14 +550,15 @@ mod tests {
// Precision rises with k; the target is a floor of 0.95.
let costs: Vec<_> = (1..=10)
.map(|k| {
let mut c = kll(k);
c.query_accuracy = BTreeMap::from([(ERR.into(), 0.9 + 0.01 * k as f64)]);
let mut c = cms(1, k);
c.query_accuracy =
BTreeMap::from([("precision_at_k".into(), 0.9 + 0.01 * k as f64)]);
c
})
.collect();
let r = Raqe {
accuracy_direction: AccuracyDirection::HigherIsBetter,
..quantile_raqe(0.95)
accuracy_sla: 0.95,
..raqe("r", 3_600_000, 60_000, 0.0)
};
let found = search(&r, &costs, 11, false, table_accuracy);
assert_eq!(found.selected, Some(4)); // k = 5: 0.95
Expand Down
42 changes: 24 additions & 18 deletions rqe-optimizer/src/candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,14 @@ pub fn is_eligible(r: &Raqe, d: &Deployment, facts: &WorkloadFacts, accuracy: &A
&& properties.needs_delta_set_key_tracker == d.key_tracker.is_some()
&& (!properties.one_fixed_size_sketch_for_all_groups
|| measured_at_group_count(&d.config, facts[&d.metric].cardinality[&d.grouping_labels]))
&& r.meets_sla(accuracy(r, d))
&& r.meets_sla(&d.config.sketch, accuracy(r, d))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::{facts, METRIC};
use crate::{table_accuracy, AccuracyDirection, LabelSet};
use crate::test_support::{facts, perfect_accuracy, METRIC};
use crate::{table_accuracy, LabelSet};
use std::collections::BTreeMap;
fn raqe(id: &str, lookback: Millis, interval: Millis) -> Raqe {
Raqe {
Expand All @@ -383,9 +383,7 @@ mod tests {
metric: METRIC.into(),
spatial_filter: String::new(),
grouping_labels: LabelSet::new(),
accuracy_metric: "err".into(),
accuracy_sla: 1.0,
accuracy_direction: AccuracyDirection::LowerIsBetter,
accuracy_sla: 0.5,
latency_sla_ms: None,
}
}
Expand All @@ -397,17 +395,21 @@ mod tests {
insert_cpu_secs: 1.0,
merge_cpu_secs: 1.0,
query_cpu_secs: 1.0,
query_accuracy: BTreeMap::from([("err".into(), 0.1)]),
query_accuracy: perfect_accuracy(),
merge_accuracy: BTreeMap::new(),
measured_at: None,
}
}
#[test]
fn eligibility_checks_accuracy_at_the_queries_merge_count() {
let mut config = cost();
let err = |v: f64| BTreeMap::from([("err".into(), v)]);
// Error is not monotone in the count: 16 is worse than 4 and 64.
config.merge_accuracy = BTreeMap::from([(4, err(0.2)), (16, err(2.0)), (64, err(0.3))]);
let precision = |v: f64| BTreeMap::from([("precision_at_k".into(), v)]);
// Precision is not monotone in the count: 16 is worse than 4 and 64.
config.merge_accuracy = BTreeMap::from([
(4, precision(0.8)),
(16, precision(0.1)),
(64, precision(0.7)),
]);
let at_window = |window_ms| Deployment {
capability: Capability::TopKByValue,
metric: METRIC.into(),
Expand Down Expand Up @@ -540,15 +542,15 @@ mod tests {

#[test]
fn undeployable_families_are_candidates_only_when_allowed() {
let r = Raqe {
capability: Capability::Quantile,
..raqe("r", 60_000, 60_000)
};
let r = raqe("r", 60_000, 60_000);
let named = |sketch: &str| AtomicCostEntry {
sketch: sketch.into(),
..cost()
};
let costs = [named("kll-percall"), named("dd")];
let costs = [
named("cms-heap-topk-fastpath-vector2d"),
named("countsketch-heap-topk-fastpath-vector2d"),
];
let candidates = build_all_candidates(
std::slice::from_ref(&r),
&costs,
Expand All @@ -557,9 +559,13 @@ mod tests {
&table_accuracy,
);
assert!(!candidates.is_empty());
assert!(candidates.iter().all(|d| d.config.sketch == "kll-percall"));
assert!(candidates
.iter()
.all(|d| d.config.sketch == "cms-heap-topk-fastpath-vector2d"));
let all = build_all_candidates_unpruned(&[r], &costs, &facts(1, 1), true);
assert!(all.iter().any(|d| d.config.sketch == "dd"));
assert!(all
.iter()
.any(|d| d.config.sketch == "countsketch-heap-topk-fastpath-vector2d"));
}

#[test]
Expand Down Expand Up @@ -742,7 +748,7 @@ mod tests {
sketch: "hydra-kll".into(),
mem_bytes_per_instance: 1000.0,
query_accuracy: BTreeMap::from([
("err".into(), 0.1),
("mean_rank_err".into(), 0.1),
("subpopulations".into(), subpopulations),
]),
..cost()
Expand Down
Loading
Loading