File size: 14,964 Bytes
f3b0a91 44084af f3b0a91 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | """TaskWorld: workflow x tier x seed → services, oracle rules, pristine (defective) rules, AM configs, requirements."""
from __future__ import annotations
import copy
import hashlib
import os
import random
from dataclasses import dataclass, field
import yaml
from . import bugs, distractors, naming, routing
from .alerts import BURN, FLEET_KINDS, FLEET_NAMES, RECORD_PREFIX, AlertSpec, oracle_rule, record_rules
WF_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "workflows", "workflows.yaml")
TIER_SERVICES = {"easy": 1, "medium": 2, "hard": 3}
TIER_DISTRACTOR_FILES = {"easy": 1, "medium": 3, "hard": 8}
WEIGHTS = {"new_alert": 3, "repair": 3, "recording": 2, "route": 2, "inhibit": 2}
CATEGORY = {"new_alert": "req_alerts", "repair": "req_repairs", "recording": "req_recording",
"route": "req_routing", "inhibit": "req_inhibit"}
EXPERTISE = {"easy": ["novice"], "medium": ["novice", "practitioner"], "hard": ["practitioner", "expert"]}
def load_workflows(path: str | None = None) -> dict:
with open(path or os.path.abspath(WF_PATH)) as fh:
return {w["id"]: w for w in yaml.safe_load(fh)["workflows"]}
def task_seed(master: int, workflow: str, tier: str, seed: int) -> int:
return int(hashlib.sha256(f"{master}:{workflow}:{tier}:{seed}".encode()).hexdigest()[:16], 16)
@dataclass
class Svc:
name: str
team: str
target: str
family: str
slo_le: str = "0.5"
@dataclass
class Req:
id: str
kind: str
alert: AlertSpec | None = None
defect: str | None = None
record: dict | None = None
route: dict | None = None
inhibit: dict | None = None
pm: str | None = None # postmortem file name (repairs)
@property
def category(self) -> str:
return CATEGORY[self.kind]
@property
def weight(self) -> int:
return WEIGHTS[self.kind]
@dataclass
class World:
task_id: str
workflow: str
tier: str
seed: int
tseed: int
axes: dict
company: str
domain: str
platform_team: str
services: list[Svc]
decoys: list[str]
alerts: list[AlertSpec] = field(default_factory=list)
reqs: list[Req] = field(default_factory=list)
oracle_files: dict = field(default_factory=dict) # stem -> [group]
pristine_files: dict = field(default_factory=dict)
am_oracle: dict = field(default_factory=dict)
am_pristine: dict = field(default_factory=dict)
postmortems: dict = field(default_factory=dict)
old_team: str | None = None
extra_teams: list[str] = field(default_factory=list)
sibling: Svc | None = None
def _alert(rid: str, kind: str, svc: Svc | None, family: str, world: World, rng: random.Random,
team: str | None = None) -> AlertSpec:
if kind in FLEET_KINDS:
name = rng.choice(FLEET_NAMES[kind])
params = {"error_ratio": {"thr": rng.choice(["0.02", "0.05", "0.1"]), "min_rps": "1"},
"mem_high": {"thr": rng.choice(["0.85", "0.9"])}, "crashloop": {"thr": "3"},
"latency_p99": {"thr": rng.choice(["0.5", "1"])}}[kind]
labels = {"severity": "warning", "team": world.platform_team}
return AlertSpec(rid, kind, name, None, family, labels, f"https://runbooks.{world.domain}/alerts/{name}", params)
cam = naming.camel(svc.name)
name = {"burn_page": f"{cam}ErrorBudgetBurnFast", "burn_ticket": f"{cam}ErrorBudgetBurnSlow",
"target_down": f"{cam}Down"}[kind]
labels = {"severity": {"burn_page": "page", "burn_ticket": "ticket", "target_down": "page"}[kind],
"team": team or svc.team}
if kind in BURN:
labels["slo"] = f"{svc.name}-{'latency' if svc.family == 'latency' else 'availability'}"
params = {"target": svc.target, "slo_le": svc.slo_le} if kind in BURN else {}
return AlertSpec(rid, kind, name, svc.name, svc.family, labels,
f"https://runbooks.{world.domain}/alerts/{name}", params)
def build_world(workflow: str, tier: str, seed: int, master_seed: int = 20260928, task_id: str = "",
wfs: dict | None = None) -> World:
wf = (wfs or load_workflows())[workflow]
mix = wf["mix"][tier]
tseed = task_seed(master_seed, workflow, tier, seed)
rng = random.Random(tseed)
company, domain = rng.choice(naming.COMPANIES)
axes = dict(rng.choice(wf["axis_pool"]))
axes.update(tone=rng.choice(["terse", "neutral", "urgent-pager"]), expertise=rng.choice(EXPERTISE[tier]),
rules_format=rng.choice(["plain", "prometheusrule"]) if tier != "easy" else "plain", tier=tier)
n = TIER_SERVICES[tier]
names = naming.pick_services(rng, n + 1 + 3)
svc_names, sib_name, decoys = names[:n], names[n], names[n + 1:]
teams = rng.sample(naming.TEAMS, n + 3)
fams = ["http", "grpc"]
rng.shuffle(fams)
f0 = "latency" if wf["rec_family"] == "latency" else fams[0]
f1 = fams[1] if wf["rec_family"] == "other" else fams[0]
services = [Svc(svc_names[i], teams[i], rng.choice(naming.SLO_TARGETS), f0 if i == 0 else f1,
rng.choice(["0.25", "0.5"]) if f0 == "latency" else "0.5") for i in range(n)]
w = World(task_id, workflow, tier, seed, tseed, axes, company, domain, rng.choice(naming.PLATFORM_TEAMS),
services, decoys, extra_teams=teams[n:n + 2])
if workflow == "team-reorg-migration":
w.old_team = teams[n + 2]
for i, s in enumerate(services[:2]):
s.team = f"{w.old_team}-{['core', 'edge'][i]}"
if tier == "easy":
w.sibling = Svc(sib_name, teams[n], rng.choice(naming.SLO_TARGETS), f1)
_build_alerts_and_reqs(w, wf, mix, rng)
return w
def _build_alerts_and_reqs(w: World, wf: dict, mix: dict, rng: random.Random) -> None:
svcs = w.services
new_specs = [(k.split(":")[0], int(k.split(":")[1])) for k in mix["new"]]
rec_specs = [(r.split(":")[0], r.split(":")[1].split(",")) for r in mix["rec"]]
new_windows = {win for d, ws in rec_specs if d == "new" for win in ws}
counter = {"A": 0, "P": 0, "C": 0, "R": 0, "I": 0}
def rid(prefix: str) -> str:
counter[prefix] += 1
return f"{prefix}{counter[prefix]}"
# new alerts
for kind, i in new_specs:
a = _alert(rid("A"), kind, svcs[i], svcs[i].family, w, rng)
w.alerts.append(a)
w.reqs.append(Req(a.req_id, "new_alert", alert=a))
new_names = {a.name for a in w.alerts}
# existing per-service alerts (healthy unless chosen as repair hosts)
existing: list[AlertSpec] = []
for idx, s in enumerate(svcs):
for kind in ("burn_page", "burn_ticket", "target_down"):
if (kind, idx) in new_specs:
continue
if kind in BURN and s.family == svcs[0].family and set(BURN[kind][:2]) & new_windows:
continue # its records are part of a Recording requirement
if kind in BURN and w.workflow == "slo-onboarding" and idx == 0:
continue # the new service has no SLO yet
existing.append(_alert("", kind, s, s.family, w, rng))
team_of = {s.name: s.team for s in svcs}
fleet = [_alert("", k, None, "latency" if k == "latency_p99" else rng.choice(["http", "grpc"]), w, rng)
for k in FLEET_KINDS]
existing += fleet
# repairs: reorg relabels first, then the defect pool
repairs: list[tuple[AlertSpec, str]] = []
used: set[str] = set()
if w.workflow == "team-reorg-migration":
for a in existing:
if a.service in team_of and team_of[a.service].startswith(f"{w.old_team}-") and len(repairs) < mix["repair"]:
repairs.append((a, "B12t"))
used.add(a.name)
pool = list(wf["defect_pool"])
rng.shuffle(pool)
while len(repairs) < mix["repair"] and pool:
bug = pool.pop(0)
hosts = [a for a in existing if a.kind in bugs.HOSTS[bug] and a.name not in used
and not (bug == "B02" and a.for_min < 2)]
if not hosts:
continue
a = rng.choice(hosts)
repairs.append((a, bug))
used.add(a.name)
if not pool and len(repairs) < mix["repair"]:
pool = [b for b in wf["defect_pool"] if b not in ("B12",)]
rng.shuffle(pool)
for a, bug in repairs:
a.req_id = rid("P")
if bugs.accepted_for(a, bug):
a.params["for_range"] = bugs.accepted_for(a, bug)
w.reqs.append(Req(a.req_id, "repair", alert=a, defect=bug))
# H11: no healthy per-service burn siblings on hard
if w.tier == "hard":
existing = [a for a in existing if a.name in used or a.kind not in BURN]
w.alerts += existing
if w.sibling:
w.alerts += [_alert("", k, w.sibling, w.sibling.family, w, rng) for k in ("burn_page", "burn_ticket", "target_down")]
# recording requirements (family of service 0)
for defect, windows in rec_specs:
r = Req(rid("C"), "recording", defect=defect,
record={"family": svcs[0].family, "windows": windows, "slo_le": svcs[0].slo_le,
"service": svcs[0].name})
w.reqs.append(r)
req_windows = {win for _, ws in rec_specs for win in ws}
for a in w.alerts:
if a.kind in BURN and a.req_id and a.family == svcs[0].family:
a.params["reads_required"] = bool(set(BURN[a.kind][:2]) & req_windows)
# routes
for spec_ in mix["route"]:
d, i = spec_.split(":")
s = svcs[int(i)]
w.reqs.append(Req(rid("R"), "route", defect=d, route={"team": s.team, "service": s.name}))
# inhibits: source = <Svc>Down (created healthy if not present)
for spec_ in mix["inhibit"]:
d, i = spec_.split(":")
s = svcs[int(i)]
down = next((a for a in w.alerts if a.kind == "target_down" and a.service == s.name), None)
if down is None:
down = _alert("", "target_down", s, s.family, w, rng)
w.alerts.append(down)
targets = sorted({a.name for a in w.alerts if a.service == s.name and a.kind in BURN}
| {a.name for a in w.alerts if a.kind == "error_ratio"})
w.reqs.append(Req(rid("I"), "inhibit", defect=d, inhibit={"source": down.name, "targets": targets,
"service": s.name, "team": s.team}))
_build_files(w, rng)
_build_am(w, rng)
def _records_needed(w: World) -> dict[str, dict[str, set[str]]]:
"""family -> {window -> slo_le set}: windows read by any burn alert, plus required windows."""
need: dict[str, dict[str, set[str]]] = {}
for a in w.alerts:
if a.kind in BURN:
for win in BURN[a.kind][:2]:
need.setdefault(a.family, {}).setdefault(win, set()).add(a.params.get("slo_le", "0.5"))
for r in w.reqs:
if r.kind == "recording":
for win in r.record["windows"]:
need.setdefault(r.record["family"], {}).setdefault(win, set()).add(r.record["slo_le"])
return need
def _file_for(a: AlertSpec) -> str:
if a.kind in FLEET_KINDS:
return "fleet"
if a.kind == "target_down":
return "service-health"
return f"slo-{a.service}"
def _build_files(w: World, rng: random.Random) -> None:
order = ["5m", "30m", "1h", "6h"]
files: dict[str, list] = {}
rec_groups = []
for fam, wins in sorted(_records_needed(w).items()):
le = sorted({x for v in wins.values() for x in v})[0]
rec_groups.append({"name": f"sli-{fam}", "rules": record_rules(fam, [x for x in order if x in wins], le)})
files["slo-recording"] = rec_groups
for a in w.alerts:
stem = _file_for(a)
groups = files.setdefault(stem, [{"name": stem, "rules": []}])
groups[0]["rules"].append(oracle_rule(a))
for stem, rules in distractors.pick(rng, TIER_DISTRACTOR_FILES[w.tier], w.domain, w.platform_team).items():
files[stem] = [{"name": stem, "rules": rules}]
w.oracle_files = files
def materialize(w: World, fixed: set[str]) -> tuple[dict, dict]:
"""Rule files and AM config with every requirement NOT in `fixed` left in its pristine (defective)
state. materialize(w, set()) is the pristine repo; materialize(w, all ids) is the oracle."""
files = copy.deepcopy(w.oracle_files)
for r in w.reqs:
if r.id in fixed:
continue
if r.kind == "new_alert":
_drop_rule(files, r.alert.name)
elif r.kind == "repair":
_replace_rule(files, r.alert.name, lambda rule, a=r.alert, b=r.defect:
bugs.mutate(a, rule, b, w.old_team))
elif r.kind == "recording":
grp = next(g for g in files["slo-recording"] if g["name"] == f"sli-{r.record['family']}")
names = {RECORD_PREFIX + win for win in r.record["windows"]}
if r.defect == "new":
grp["rules"] = [x for x in grp["rules"] if x["record"] not in names]
for rule in grp["rules"]:
if rule["record"] not in names:
continue
if r.defect == "RB1":
rule["expr"] = rule["expr"].replace("sum by (service) ", "sum ")
elif r.defect == "RB2":
rule["expr"] = rule["expr"].replace('code=~"5.."', 'code=~"5xx"').replace(
'grpc_code=~"Unavailable', 'grpc_code=~"unavailable')
for stem in list(files):
files[stem] = [g for g in files[stem] if g["rules"]]
if not files[stem]:
del files[stem]
am = copy.deepcopy(w.am_oracle)
for r in w.reqs:
if r.id in fixed:
continue
if r.kind == "route":
am = routing.apply_route_defect(am, r.defect, r.route["team"], r.route["service"],
random.Random(w.tseed + int(r.id[1:])))
elif r.kind == "inhibit":
am = routing.apply_inhibit_defect(am, r.defect, routing.inhibit_rule(r.inhibit["source"], r.inhibit["targets"]))
return files, am
def _drop_rule(files: dict, name: str) -> None:
for groups in files.values():
for g in groups:
g["rules"] = [r for r in g["rules"] if r.get("alert") != name]
def _replace_rule(files: dict, name: str, fn) -> None:
for groups in files.values():
for g in groups:
g["rules"] = [fn(r) if r.get("alert") == name else r for r in g["rules"]]
def _build_am(w: World, rng: random.Random) -> None:
in_scope = [s.team for s in w.services]
routed = sorted(set(in_scope + w.extra_teams + [w.platform_team] + ([w.old_team] if w.old_team else [])
+ ([w.sibling.team] if w.sibling else [])))
cfg = routing.base_config(routed, routed, w.domain)
for r in w.reqs:
if r.kind == "inhibit":
cfg["inhibit_rules"].append(routing.inhibit_rule(r.inhibit["source"], r.inhibit["targets"]))
w.am_oracle = cfg
w.pristine_files, w.am_pristine = materialize(w, set())
|