Skip to content
Merged
4 changes: 3 additions & 1 deletion rqe-optimizer/results/e2e-rate10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ differences for the end-to-end run:

- `inputs/tables/`: the tables, `export_autosketch_eval_table.py --synthetic
--samples-per-sec 10` (classic, all, multigroup at shared = 1). Each one's
`workloads[0].e2e` holds the queries and metrics to generate.
`workloads[0].e2e` holds the queries and metrics to generate. Each query
but `rate` carries `expr`, its RQE expression tree (leaves name the plans'
RQE ids), added after planning: the RQEs and plans are unchanged.
- `plans/<table>.plans.json.gz`: each method's chosen plans, in the format of
`../autosketch-vs-asap-hydra/README.md`, "Plans" (`--runs 1`, kept, then
`scripts/extract_autosketch_plans.py`).
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

112 changes: 90 additions & 22 deletions scripts/export_autosketch_eval_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ def rqe_entry(q, curves, curves_big, sat, sat_big, merge):
EXACT_SKETCH = {"exact-sum": "exact-sum", "exact-increase": "exact-increase"}
WINDOW_SECS = {"1m": 60, "5m": 300, "10m": 600, "15m": 900, "1h": 3600, "6h": 21600,
"24h": 86400}
SERIES_LABELS = ["job", "label_0"] # a classic metric's labels
# Capabilities an expression-tree leaf can read (no rate); a topk leaf is
# named topk_by_value.
LEAF_CAPABILITIES = {"sum", "quantile", "cardinality", "topk"}
DASHBOARD_QUANTILES = ["0.5", "0.9", "0.99"]
DASHBOARD_FIXED_WINDOWS = ["15m", "1h"] # D5
# Shared replicas: each replica draws windows, quantiles and an interval.
Expand Down Expand Up @@ -445,6 +449,22 @@ def point_id(point):
return base if metrics == 1 else f"{base}/metrics={metrics}"


def rqe_leaf(rqe, metric, capability, lookback_s, grouping, phi=None, k=None, value=None,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR merges #225 into the #226 branch and then changes rqe_leaf (adds topk_by), so its diff repeats all of #225. Merge order needs to be 225 and 226 into main, then retarget this PR to main; otherwise reviewers see 225's changes twice and the branches conflict.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed: this is a stacked PR. Plan: merge #225 and #226 into main, then retarget this PR to main (or merge main into it), and the diff shrinks to its own change. Latest #225 and #226 are merged in (a360ded, c313c1f).

topk_by=None):
"""An expression-tree leaf reading RQE `rqe`, whose output is grouped by
`grouping` (a top-k ranks within each `topk_by` group, else over all);
None for a capability no leaf reads (rate)."""
if capability not in LEAF_CAPABILITIES:
return None
if capability == "topk":
capability = "topk_by_value"
leaf = {"id": rqe, "metric": metric, "capability": capability,
"lookback_ms": round(lookback_s * 1000), "grouping": list(grouping)}
leaf.update((key, v) for key, v in [("phi", phi), ("k", k), ("value", value),
("topk_by", topk_by)] if v is not None)
return {"rqe": leaf}


def synthetic_queries(windows=SYNTHETIC_WINDOWS, interval=SYNTHETIC_INTERVAL,
templates="dashboard", quantiles=None, dataset=None,
range_with_interval=False, seen=None, metric=None, e2e=None):
Expand All @@ -465,9 +485,10 @@ def synthetic_queries(windows=SYNTHETIC_WINDOWS, interval=SYNTHETIC_INTERVAL,
schema_queries() to the `classic` set.

With `e2e` (a list), the `classic` templates append their PromQL to it,
one {promql, interval_secs, rqes} per query (e2e_block()), and the schema
templates their query fields (schema_queries()). Values are read from the
gauge `data`, increments from the counter `data_total`.
one {promql, interval_secs, rqes, expr} per query (e2e_block()), and the
schema templates their query fields (schema_queries()). `expr` is the
query's RQE expression tree (rqe_leaf()); a rate query has none. Values are
read from the gauge `data`, increments from the counter `data_total`.
"""
rate = SYNTHETIC_SERIES * SYNTHETIC_SAMPLES_PER_SEC # samples/s over all series
scrape = 1 / SYNTHETIC_SAMPLES_PER_SEC # an instant query's lookback, in s
Expand All @@ -479,14 +500,18 @@ def synthetic_queries(windows=SYNTHETIC_WINDOWS, interval=SYNTHETIC_INTERVAL,
def label(rng):
return f"{rng}@{interval}s" if range_with_interval else rng

def add(query_id, stream, capability, rng, s, t, groups, keys=None, promql=None):
"""Adds the RQE (once) and its query `promql`; returns the RQE's id."""
def add(query_id, stream, capability, rng, s, t, groups, keys=None, promql=None,
grouping=SERIES_LABELS, **leaf):
"""Adds the RQE (once) and its query `promql`, grouped by `grouping`,
with `leaf`'s rqe_leaf() fields; returns the RQE's id."""
if metric:
query_id, stream = f"{query_id}_{metric}", f"{metric}/{stream}"
kind = "values" if capability == "quantile" else "keys"
rqe = f"{dataset}/{query_id}/{kind}/{rng}"
if promql and e2e is not None:
e2e.append({"promql": promql, "interval_secs": t, "rqes": [rqe]})
query = {"promql": promql, "interval_secs": t, "rqes": [rqe]}
expr = rqe_leaf(rqe, gauge, capability, s, grouping, **leaf)
e2e.append({**query, "expr": expr} if expr else query)
if (query_id, rng) in seen:
return rqe
seen.add((query_id, rng))
Expand Down Expand Up @@ -525,34 +550,38 @@ def add(query_id, stream, capability, rng, s, t, groups, keys=None, promql=None)
return schema_queries(interval, dataset, range_with_interval, seen, metric, e2e)
assert templates in ("classic", "all"), templates
quantiles = quantiles or SYNTHETIC_QUANTILES
add("sum_by_job", "job/value", "sum", "1s", scrape, 1, J, promql=f"sum by (job) ({gauge})")
add("sum_by_job", "job/value", "sum", "1s", scrape, 1, J, promql=f"sum by (job) ({gauge})",
grouping=["job"])
for q in quantiles:
add(f"quantile_by_job_p{q}", "job/dist", "quantile", "1s", scrape, 1, J,
promql=f"quantile by (job) ({q}, {gauge})")
promql=f"quantile by (job) ({q}, {gauge})", grouping=["job"], phi=float(q))
for rng, s in ranges:
lbl = label(rng)
# Template 2's PromQL omits `sum by (label_0)`: label_0 names one
# series (child_of job, unique values), so it only drops `job`, and
# the ranking and values are the same. ASAPQuery's planner supports
# this two-level shape, not the three-level one (#744, #772).
add("topk32_sum_by_label0_sum_over_time", "label_0/value", "topk", lbl, s, interval,
1, C, promql=f"topk(32, sum_over_time({gauge}[{rng}]))")
1, C, promql=f"topk(32, sum_over_time({gauge}[{rng}]))", k=32)
add("sum_over_time", "series/value", "sum", lbl, s, interval, C,
promql=f"sum_over_time({gauge}[{rng}])")
for q in quantiles:
add(f"quantile_over_time_p{q}", "series/dist", "quantile", lbl, s, interval, C,
promql=f"quantile_over_time({q}, {gauge}[{rng}])")
promql=f"quantile_over_time({q}, {gauge}[{rng}])", phi=float(q))
add("rate", "series/increment", "rate", lbl, s, interval, C,
promql=f"rate({counter}[{rng}])")
add("sum_by_job_sum_over_time", "job/value", "sum", lbl, s, interval, J,
promql=f"sum by (job) (sum_over_time({gauge}[{rng}]))")
promql=f"sum by (job) (sum_over_time({gauge}[{rng}]))", grouping=["job"])
# Template 10, the same RQEs as template 5's: one query reads both.
rqes = [add(f"quantile_over_time_p{q}", "series/dist", "quantile", lbl, s, interval, C)
for q in ["0.9", "0.5"]]
if e2e is not None:
lhs, rhs = (rqe_leaf(rqe, gauge, "quantile", s, SERIES_LABELS, phi=phi)
for rqe, phi in zip(rqes, [0.9, 0.5]))
e2e.append({"promql": f"quantile_over_time(0.9, {gauge}[{rng}]) / "
f"quantile_over_time(0.5, {gauge}[{rng}])",
"interval_secs": interval, "rqes": rqes})
"interval_secs": interval, "rqes": rqes,
"expr": {"op": "/", "lhs": lhs, "rhs": rhs}})
if templates == "all":
out += schema_queries(interval, dataset, range_with_interval, seen, metric, e2e)
return out
Expand Down Expand Up @@ -590,8 +619,8 @@ def schema_queries(interval, dataset, range_with_interval, seen, metric=None, e2
With `e2e` (a list), each RQE appends its query's fields and canonical
ASAPQuery SQL (there is no PromQL): {sql, table, value,
capability, grouping, quantile (quantiles only), window_secs,
interval_secs, rqes}. `table` is the metric with `/` as `_`
(`data_1_http`).
interval_secs, rqes, expr}. `table` is the metric with `/` as `_`
(`data_1_http`); `expr` is one rqe_leaf() on it.

A query several templates ask (14, 15 and 17 share groupings) is only
its strictest template's RQE, unless KEEP_DUPLICATE_QUERIES."""
Expand Down Expand Up @@ -641,8 +670,11 @@ def schema_queries(interval, dataset, range_with_interval, seen, metric=None, e2
query = schema_query(full.replace("/", "_"), value, capability, what,
grouping)
query["sql"] = asapquery_sql(query, s)
rqe = f"{dataset}/{query_id}/{kind}/{rng}"
e2e.append({**query, "window_secs": s, "interval_secs": interval,
"rqes": [f"{dataset}/{query_id}/{kind}/{rng}"]})
"rqes": [rqe],
"expr": rqe_leaf(rqe, query["table"], capability, s, grouping,
phi=query.get("quantile"), value=value)})
out.append({
"dataset": dataset, "query_id": query_id, "kind": kind,
"range": rng, "range_s": s, "step_s": interval,
Expand Down Expand Up @@ -761,7 +793,8 @@ def e2e_block(qs, queries):
labels in SCHEMAS' format: `label_0` has SYNTHETIC_SERIES /
SYNTHETIC_JOBS values under each `job` (`child_of`), so there are
SYNTHETIC_SERIES series. Schema queries are schema_queries()' fields, one
per distinct query (with every RQE it reads, sorted); each schema metric
per distinct query (with every RQE it reads, sorted; one reading several
has no `expr`, as no single leaf names them); each schema metric
is a `records` table (`http`, or `data_1_http` under metric `data_1`)
with SCHEMAS' labels, `rate` as `records_per_sec`, and its `values`."""
unique = {}
Expand All @@ -771,9 +804,11 @@ def e2e_block(qs, queries):
continue
# Schema queries identical but for their RQEs (templates 14, 15, 17
# share groupings) are one query reading them all.
key = json.dumps({k: v for k, v in q.items() if k != "rqes"}, sort_keys=True)
key = json.dumps({k: v for k, v in q.items() if k not in ("rqes", "expr")},
sort_keys=True)
if key in unique:
unique[key] = {**unique[key], "rqes": sorted(unique[key]["rqes"] + q["rqes"])}
merged = {k: v for k, v in unique[key].items() if k != "expr"}
unique[key] = {**merged, "rqes": sorted(unique[key]["rqes"] + q["rqes"])}
else:
unique[key] = q
labels = [{"name": "job", "cardinality": SYNTHETIC_JOBS},
Expand Down Expand Up @@ -811,6 +846,40 @@ def e2e_block(qs, queries):
r"^avg by \(([\w, ]+)\) \(quantile_over_time\([\d.]+, data\[\w+\]\)\)$")


def random_expr(rqe, rqe_id, series_labels):
"""The RQE expression tree of a manifest query whose one RQE is `rqe`
(the table's `rqe_id` on `data`), built from the RQE's fields as
asap-tools' rqe_query() renders them, or None for another capability. A leaf's
grouping is its output labels; G is the RQE's grouping_labels:
- quantile, `avg by (G) (quantile_over_time(phi, data[W]))`: avg by G of
a per-series quantile leaf;
- sum, `sum by (G) (sum_over_time(data[W]))`: a leaf grouped by G;
- topk_by_value, `topk [by (G)] (k, sum_over_time(data[W]))`: a
per-series top-k leaf ranked within each G (`topk_by`, left out for a
global top-k)."""
capability, grouping = rqe["capability"], rqe["grouping_labels"]

def leaf(capability, grouping, **extra):
return rqe_leaf(rqe_id, "data", capability, rqe["lookback_ms"] / 1000, grouping, **extra)

if capability == "quantile":
return {"agg": "avg", "by": grouping,
"of": leaf("quantile", series_labels, phi=rqe["phi"])}
if capability == "topk_by_value":
return leaf("topk", series_labels, k=rqe["k"], topk_by=grouping or None)
if capability == "sum":
return leaf("sum", grouping)
return None


def e2e_query(query, rqe, t, rqe_id, series_labels):
"""The e2e entry of manifest `query` (its RQE `rqe`) reading the table's
`rqe_id`, possibly shared with other queries, with its tree when it has one."""
entry = {"promql": query["promql"], "interval_secs": t, "rqes": [rqe_id]}
expr = random_expr(rqe, rqe_id, series_labels)
return {**entry, "expr": expr} if expr else entry


def random_manifest_table(manifest, revision, inputs):
"""The eval table of `manifest`'s RANDOM_SUPPORT queries: one RQE each
on the schema metric `data`, its labels the manifest's. Every series is
Expand All @@ -819,7 +888,7 @@ def random_manifest_table(manifest, revision, inputs):
grouping, on one stream, so a fine sum can serve a coarse one; a
quantile is `avg by` of per-series quantiles, so its RQE is per series.
Queries of other capabilities are left out (logged). The e2e block holds
the PromQL."""
the PromQL and the query's RQE expression tree (random_expr())."""
labels = manifest["labels"]
names = [label["name"] for label in labels]
series = labels_cardinality(labels, names)
Expand Down Expand Up @@ -872,8 +941,7 @@ def random_manifest_table(manifest, revision, inputs):
if key in shared:
if (query["promql"], t) not in e2e_keys:
e2e_keys.add((query["promql"], t))
e2e.append({"promql": query["promql"], "interval_secs": t,
"rqes": [shared[key]]})
e2e.append(e2e_query(query, rqe, t, shared[key], names))
continue
groups = labels_cardinality(labels, grouping)
s = ms / 1000
Expand Down Expand Up @@ -916,7 +984,7 @@ def random_manifest_table(manifest, revision, inputs):
rqes.append(synthetic_entry(q))
shared[key] = rqes[-1]["id"]
e2e_keys.add((query["promql"], t))
e2e.append({"promql": query["promql"], "interval_secs": t, "rqes": [shared[key]]})
e2e.append(e2e_query(query, rqe, t, shared[key], names))
check_unique_ids(rqes)
metrics = {"data": {
"type": "gauge", "labels": labels, "samples_per_sec_per_series": per_sec,
Expand Down
Loading