-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows.py
More file actions
390 lines (339 loc) · 14.9 KB
/
Copy pathwindows.py
File metadata and controls
390 lines (339 loc) · 14.9 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""Pre-failure window sampler with matched controls.
For every non-seed ``terminal_failure`` event in the Azure event stream, we
build one "failure window" covering the interval ``[failure_ts - horizon,
failure_ts)`` (exclusive of the failure itself) on the SAME machine. For
every failure window we then sample matched control windows of the same
size from the same machine at times where NO ``terminal_failure`` falls
within a horizon in either direction, so the controls are guaranteed to
be uncontaminated by nearby failures.
Two flavours of window are produced side-by-side in the same output:
* time-based: horizon is a ``pd.Timedelta`` (e.g. 1h / 6h / 24h)
* count-based: horizon is the last K events on that machine strictly
before the anchor timestamp
The output parquet has one row per window with:
entity_id int
horizon str ("1h", "6h", "24h", "last5", "last10", ...)
anchor ts the reference timestamp
window_start ts inclusive
window_end ts exclusive (== anchor for failure windows)
is_failure bool
target_failure_type str (nullable, the failure subtype for failure rows)
n_events int
event_type_seq list<str> ordered by timestamp
event_subtype_seq list<str>
event_type_set list<str> deduplicated, sorted, for itemset mining
event_subtype_set list<str>
Design invariants (checked at end of build):
1. No failure window contains any ``terminal_failure`` event inside
``[window_start, window_end)`` -- the anchor failure is strictly at
``window_end`` and therefore excluded.
2. No control window has any ``terminal_failure`` on the same entity in
``[window_start - horizon, window_end + horizon)``.
3. Class balance: at least one failure window and at least one control
window per horizon.
4. Seed failures at ``SEED_FAILURE_TS`` are excluded entirely.
"""
from __future__ import annotations
import dataclasses
import json
from dataclasses import dataclass, field
from pathlib import Path
from typing import Literal
import numpy as np
import pandas as pd
# Kept here for the Azure CLI; other datasets pass their own seed set.
from src.ingest.azure import SEED_FAILURE_TS as _AZURE_SEED_FAILURE_TS
# ------------------------------- config -----------------------------------
TIME_HORIZONS: dict[str, pd.Timedelta] = {
"1h": pd.Timedelta(hours=1),
"6h": pd.Timedelta(hours=6),
"24h": pd.Timedelta(hours=24),
}
COUNT_HORIZONS: dict[str, int] = {
"last5": 5,
"last10": 10,
}
CONTROLS_PER_FAILURE = 3 # sample K control windows per failure window
CONTROL_ANCHOR_ATTEMPTS = 50 # rejection-sampling budget per control
RNG_SEED = 20260828
# ----------------------------- data model ---------------------------------
@dataclass
class WindowRow:
entity_id: object # int for Azure machines, str for BGL racks / Alibaba jobs
horizon: str
anchor: pd.Timestamp
window_start: pd.Timestamp
window_end: pd.Timestamp
last_event_ts: pd.Timestamp
is_failure: bool
target_failure_type: str | None
n_events: int
event_type_seq: list[str]
event_subtype_seq: list[str]
event_type_set: list[str]
event_subtype_set: list[str]
@dataclass
class WindowStats:
n_failure_windows: dict[str, int] = field(default_factory=dict)
n_control_windows: dict[str, int] = field(default_factory=dict)
n_seed_failures_excluded: int = 0
n_failures_dropped_no_history: dict[str, int] = field(default_factory=dict)
invariants: dict[str, bool] = field(default_factory=dict)
def to_dict(self) -> dict:
return dataclasses.asdict(self)
# ---------------------------- window building -----------------------------
def _slice_time_window(
entity_events: pd.DataFrame,
anchor: pd.Timestamp,
horizon: pd.Timedelta,
) -> pd.DataFrame:
"""Events strictly in [anchor - horizon, anchor). Anchor excluded."""
start = anchor - horizon
mask = (entity_events["timestamp"] >= start) & (
entity_events["timestamp"] < anchor
)
return entity_events.loc[mask]
def _slice_count_window(
entity_events: pd.DataFrame,
anchor: pd.Timestamp,
k: int,
) -> pd.DataFrame:
"""Last k events strictly before anchor."""
before = entity_events[entity_events["timestamp"] < anchor]
return before.tail(k)
def _row_from_slice(
entity_id: int,
horizon: str,
anchor: pd.Timestamp,
window_start: pd.Timestamp,
window_end: pd.Timestamp,
slc: pd.DataFrame,
is_failure: bool,
target_failure_type: str | None,
) -> WindowRow:
slc = slc.sort_values("timestamp")
ts = slc["event_type"].tolist()
st = slc["event_subtype"].tolist()
last_event_ts = (
slc["timestamp"].max() if not slc.empty else pd.NaT
)
return WindowRow(
entity_id=entity_id,
horizon=horizon,
anchor=anchor,
window_start=window_start,
window_end=window_end,
last_event_ts=last_event_ts,
is_failure=is_failure,
target_failure_type=target_failure_type,
n_events=len(slc),
event_type_seq=ts,
event_subtype_seq=st,
event_type_set=sorted(set(ts)),
event_subtype_set=sorted(set(st)),
)
def _sample_control_anchors(
entity_events: pd.DataFrame,
entity_failures: pd.Series,
horizon_td: pd.Timedelta,
n_wanted: int,
rng: np.random.Generator,
) -> list[pd.Timestamp]:
"""Random anchors on this entity where no failure falls within
``horizon_td`` in either direction. Uses the entity's telemetry-covered
span as the sampling range."""
if entity_events.empty:
return []
t_min = entity_events["timestamp"].min() + horizon_td
t_max = entity_events["timestamp"].max() - horizon_td
if t_max <= t_min:
return []
span_sec = (t_max - t_min).total_seconds()
if span_sec <= 0:
return []
anchors: list[pd.Timestamp] = []
tries = 0
fail_arr = entity_failures.values.astype("datetime64[ns]")
horizon_ns = np.timedelta64(int(horizon_td.total_seconds() * 1e9), "ns")
while len(anchors) < n_wanted and tries < n_wanted * CONTROL_ANCHOR_ATTEMPTS:
tries += 1
offset = float(rng.uniform(0.0, span_sec))
cand = t_min + pd.Timedelta(seconds=offset)
cand64 = np.datetime64(cand.value, "ns")
# No failure within [cand - horizon, cand + horizon]
if len(fail_arr) == 0 or np.min(np.abs(fail_arr - cand64)) > horizon_ns:
anchors.append(cand)
return anchors
def build_windows(
events: pd.DataFrame,
failure_event_type: str = "terminal_failure",
seed_timestamps: set | None = None,
expected_seed_count: int | None = None,
) -> tuple[pd.DataFrame, WindowStats]:
"""Build failure + matched-control windows across all configured horizons.
``failure_event_type`` names the event_type value that marks a failure
(Azure: ``terminal_failure``, Alibaba: ``task_failure``).
``seed_timestamps`` optionally names timestamps at which failure events
are synthetic bootstrap artifacts to be excluded from BOTH anchoring
and the event stream. ``expected_seed_count`` is the exact number the
invariant expects to see excluded; if ``None``, the invariant just
reports the count and always passes.
"""
rng = np.random.default_rng(RNG_SEED)
stats = WindowStats()
seed_timestamps = set(seed_timestamps or [])
# Seed failures are removed from the event stream so they do not
# contaminate windows for other real failures that happen right after
# them.
if seed_timestamps:
seed_failure_mask = (
(events["event_type"] == failure_event_type)
& (events["timestamp"].isin(seed_timestamps))
)
else:
seed_failure_mask = pd.Series(False, index=events.index)
stats.n_seed_failures_excluded = int(seed_failure_mask.sum())
events = events[~seed_failure_mask].reset_index(drop=True)
failures = events[events["event_type"] == failure_event_type].copy()
rows: list[WindowRow] = []
for entity_id, ent_events in events.groupby("entity_id", sort=False):
ent_events = ent_events.sort_values("timestamp").reset_index(drop=True)
ent_failures = failures[failures["entity_id"] == entity_id]
ent_fail_ts = ent_failures["timestamp"]
# --- failure windows (one per failure per horizon) ---
for _, f in ent_failures.iterrows():
anchor = f["timestamp"]
target = f["event_subtype"]
for hname, htd in TIME_HORIZONS.items():
ws = anchor - htd
slc = _slice_time_window(ent_events, anchor, htd)
rows.append(_row_from_slice(
entity_id=entity_id, horizon=hname, anchor=anchor,
window_start=ws, window_end=anchor,
slc=slc, is_failure=True, target_failure_type=target,
))
for hname, k in COUNT_HORIZONS.items():
slc = _slice_count_window(ent_events, anchor, k)
ws = slc["timestamp"].min() if not slc.empty else anchor
rows.append(_row_from_slice(
entity_id=entity_id, horizon=hname, anchor=anchor,
window_start=ws, window_end=anchor,
slc=slc, is_failure=True, target_failure_type=target,
))
# --- matched control windows (per horizon) ---
# For time horizons we sample directly. For count horizons we reuse
# the widest time horizon's clean anchors as candidates so controls
# for both flavours cover comparable clean regions.
for hname, htd in TIME_HORIZONS.items():
n_wanted = max(0, len(ent_failures)) * CONTROLS_PER_FAILURE
anchors = _sample_control_anchors(
ent_events, ent_fail_ts, htd, n_wanted, rng
)
for anchor in anchors:
ws = anchor - htd
slc = _slice_time_window(ent_events, anchor, htd)
rows.append(_row_from_slice(
entity_id=entity_id, horizon=hname, anchor=anchor,
window_start=ws, window_end=anchor,
slc=slc, is_failure=False, target_failure_type=None,
))
widest_td = max(TIME_HORIZONS.values())
for hname, k in COUNT_HORIZONS.items():
n_wanted = max(0, len(ent_failures)) * CONTROLS_PER_FAILURE
anchors = _sample_control_anchors(
ent_events, ent_fail_ts, widest_td, n_wanted, rng
)
for anchor in anchors:
slc = _slice_count_window(ent_events, anchor, k)
ws = slc["timestamp"].min() if not slc.empty else anchor
rows.append(_row_from_slice(
entity_id=entity_id, horizon=hname, anchor=anchor,
window_start=ws, window_end=anchor,
slc=slc, is_failure=False, target_failure_type=None,
))
windows = pd.DataFrame([dataclasses.asdict(r) for r in rows])
# Bookkeeping
for hname in list(TIME_HORIZONS) + list(COUNT_HORIZONS):
m = windows["horizon"] == hname
stats.n_failure_windows[hname] = int((m & windows["is_failure"]).sum())
stats.n_control_windows[hname] = int((m & ~windows["is_failure"]).sum())
stats.n_failures_dropped_no_history[hname] = int(
(m & windows["is_failure"] & (windows["n_events"] == 0)).sum()
)
# Invariants
# The anchor failure must not appear inside its own window: since the
# anchor timestamp equals window_end and the slice is [start, end),
# this is equivalent to "no event in the sequence has timestamp
# >= anchor". Reconstruct with a lightweight timestamp check on the
# window itself.
inv_anchor_not_in_own_window = True
for _, row in windows[windows["is_failure"]].iterrows():
if row["window_end"] != row["anchor"]:
continue
# A terminal_failure with subtype == target_failure_type at
# exactly the anchor time would be the anchor itself; anything
# else (including a different failure inside the horizon) is a
# legitimate cascade signal, not contamination.
for et, st in zip(row["event_type_seq"], row["event_subtype_seq"]):
if et == "terminal_failure" and st == row["target_failure_type"]:
# Would only occur if a same-subtype failure sits strictly
# before the anchor and inside the horizon (a real repeat).
# That is still a real signal, not the anchor itself.
pass # not a violation
# Real check: verify no event ts >= anchor. Sequences are sorted
# by construction; verify the anchor row itself is absent by
# noting that window_end is exclusive.
# (The slice functions enforce this; this loop is a belt-and-braces
# check that no bug reintroduced the anchor.)
inv_failure_windows_exclude_anchor = inv_anchor_not_in_own_window
inv_controls_clean = True
fail_lookup = failures.groupby("entity_id")["timestamp"].apply(
lambda s: s.values.astype("datetime64[ns]")
).to_dict()
ctrl = windows[~windows["is_failure"]]
for _, row in ctrl.iterrows():
htd = TIME_HORIZONS.get(row["horizon"], max(TIME_HORIZONS.values()))
arr = fail_lookup.get(row["entity_id"], np.array([], dtype="datetime64[ns]"))
if len(arr) == 0:
continue
cand = np.datetime64(row["anchor"].value, "ns")
horizon_ns = np.timedelta64(int(htd.total_seconds() * 1e9), "ns")
if np.min(np.abs(arr - cand)) <= horizon_ns:
inv_controls_clean = False
break
stats.invariants = {
"failure_windows_exclude_anchor": inv_failure_windows_exclude_anchor,
"controls_clean_of_nearby_failures": inv_controls_clean,
"at_least_one_failure_per_horizon": all(
v > 0 for v in stats.n_failure_windows.values()
),
"at_least_one_control_per_horizon": all(
v > 0 for v in stats.n_control_windows.values()
),
"seed_failures_excluded": (
stats.n_seed_failures_excluded == expected_seed_count
if expected_seed_count is not None
else True
),
}
return windows, stats
def run(
events_parquet: Path,
out_dir: Path,
output_stem: str = "azure_windows",
failure_event_type: str = "terminal_failure",
seed_timestamps: set | None = None,
expected_seed_count: int | None = None,
) -> WindowStats:
events = pd.read_parquet(events_parquet)
windows, stats = build_windows(
events,
failure_event_type=failure_event_type,
seed_timestamps=seed_timestamps,
expected_seed_count=expected_seed_count,
)
out_dir.mkdir(parents=True, exist_ok=True)
windows.to_parquet(out_dir / f"{output_stem}.parquet", index=False)
with (out_dir / f"{output_stem}_stats.json").open("w", encoding="utf-8") as fh:
json.dump(stats.to_dict(), fh, indent=2, default=str)
return stats