Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-warehouse): count a progressing over-budget repartition as progress (#83064)

Tom Owers2371cee9d827
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
index 0bedcd37cd5b..3e3b212f6142 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
@@ -518,11 +518,17 @@ def _handle_budget_exceeded(
"""Record a rewrite that ran out of one activity's budget, distinguishing progress from a stall.
Checkpoint/resume lets a table too large to rewrite in one activity converge across runs (see
- `RepartitionBudgetExceededError`), so an attempt that advanced the checkpoint is forward progress,
- not a failure: counting it against the finite `MAX_REPARTITION_ATTEMPTS` would abandon a table that
- simply needs more than three budgets mid-convergence and leave it un-repartitioned. Only an attempt
- that wrote no new rows since the last checkpoint is stuck; that one falls through to
- `_handle_failure` so a rewrite which genuinely can't advance in one budget still gives up.
+ `RepartitionBudgetExceededError`), so an attempt that appended new rows is forward progress, not a
+ failure: counting it against the finite `MAX_REPARTITION_ATTEMPTS` would abandon a table that simply
+ needs more than three budgets mid-convergence and leave it un-repartitioned. Only an attempt that
+ appended nothing this run is stuck; that one falls through to `_handle_failure` so a rewrite which
+ genuinely can't advance in one budget still gives up.
+
+ Progress is judged from the rows this attempt wrote (`error.rows_written`), not the checkpoint's
+ cumulative temp size against a stored high-water mark: the rewrite restarts from row 0 whenever its
+ checkpoint is discarded (the source's Delta version moved between runs), so the cumulative size is
+ not monotonic across attempts, and a genuinely progressing fresh rebuild that read back below an
+ earlier, longer attempt's mark was charged a spurious failure.
Returns the metric outcome: "superseded" when a newer attempt owns the claim, "progressing" when
the rewrite advanced, otherwise whatever `_handle_failure` returns.
@@ -534,22 +540,20 @@ def _handle_budget_exceeded(
return "superseded"
pending = schema.repartition_pending or pending or {}
- rows_written = int((schema.repartition_rewrite or {}).get("rows_written", 0))
- high_water = int(pending.get("rewrite_high_water", 0))
- if rows_written > high_water:
- # Forward progress this attempt: keep the checkpoint, record the new high-water mark, and reset
- # the failure counter — a rewrite still advancing is not the doomed one the cap exists to stop.
- # The next run resumes from the checkpoint rather than re-streaming from row 0.
- schema.set_repartition_pending({**pending, "attempts": 0, "rewrite_high_water": rows_written})
+ if error.rows_written > 0:
+ # Forward progress this attempt: keep the checkpoint and reset the failure counter — a rewrite
+ # still advancing is not the doomed one the cap exists to stop. The next run resumes from the
+ # checkpoint (or rebuilds fresh if it was invalidated) rather than giving up.
+ schema.set_repartition_pending({**pending, "attempts": 0})
logger.info(
- f"repartition: over budget but rewrite advanced to {rows_written} rows, resuming next run",
- rows_written=rows_written,
+ f"repartition: over budget but rewrite advanced {error.rows_written} rows this attempt, resuming next run",
+ rows_written=error.rows_written,
)
_capture_stood_down(schema, inputs, trigger_reason, "rewrite_progressing", logger)
return "progressing"
- # No new rows since the last checkpoint: the rewrite can't make headway inside one budget, so count
- # it as a real failed attempt and let `MAX_REPARTITION_ATTEMPTS` eventually give up on it.
+ # Nothing appended this attempt: the rewrite can't make headway inside one budget, so count it as a
+ # real failed attempt and let `MAX_REPARTITION_ATTEMPTS` eventually give up on it.
return _handle_failure(inputs, schema, pending, trigger_reason, error, claim_token, logger)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
index b64e026cf044..c4c2a1a0a5b0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
@@ -217,16 +217,16 @@ class TestBudgetExhaustion:
_mock_capture_event: MagicMock,
_mock_capture_exception: MagicMock,
) -> None:
- # A rewrite that wrote no new rows since the last checkpoint (rows_written == high_water) is
- # stuck, so budget exhaustion counts as a real failed attempt.
+ # A rewrite that appended no rows this run (e.g. a resume that spent its whole budget skipping
+ # to the checkpoint) is stuck, so budget exhaustion counts as a real failed attempt.
schema = _schema(
name="public.usages",
s3_folder_name="usages",
- pending={**PENDING_TARGET, "attempts": prior_attempts, "rewrite_high_water": 12},
+ pending={**PENDING_TARGET, "attempts": prior_attempts},
rewrite={"rows_written": 12},
)
mock_schema_model.objects.select_related.return_value.get.return_value = schema
- mock_repartition.side_effect = RepartitionBudgetExceededError("out of budget")
+ mock_repartition.side_effect = RepartitionBudgetExceededError("out of budget", rows_written=0)
_maybe_repartition_table(
RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),
@@ -246,7 +246,7 @@ class TestBudgetExhaustion:
schema.stamp_last_repartition_at.assert_not_called()
schema.clear_repartition_rewrite.assert_not_called()
- @parameterized.expand([("first_attempt", 0, 0), ("last_attempt_before_give_up", 2, 100)])
+ @parameterized.expand([("first_attempt", 0), ("last_attempt_before_give_up", 2)])
@patch(f"{MODULE}.capture_exception")
@patch(f"{MODULE}.capture_repartition_event")
@patch(f"{MODULE}.HeartbeaterSync")
@@ -259,7 +259,6 @@ class TestBudgetExhaustion:
self,
_name: str,
prior_attempts: int,
- prior_high_water: int,
mock_schema_model: MagicMock,
_mock_job_model: MagicMock,
_mock_enabled: MagicMock,
@@ -269,18 +268,20 @@ class TestBudgetExhaustion:
mock_capture_event: MagicMock,
_mock_capture_exception: MagicMock,
) -> None:
- # The checkpoint advanced (rows_written > high_water), so a table too large for one budget is
- # converging across runs. It must never give up — even sitting at the last attempt before the
- # cap — and must reset the failure counter and record the new high-water mark so the next run
- # resumes rather than re-streaming from row 0.
+ # This attempt appended new rows, so a table too large for one budget is converging across runs.
+ # It must never give up — even at the last attempt before the cap — and must reset the failure
+ # counter. The checkpoint's cumulative temp size (`repartition_rewrite.rows_written`) sits far
+ # below a longer earlier attempt's high-water mark here: the case a fresh rebuild hits after its
+ # checkpoint is discarded. Judging progress by that cumulative size mislabeled it a stall and
+ # emitted a spurious `warehouse_repartition_failed`; the rows written this run are the signal.
schema = _schema(
name="public.usages",
s3_folder_name="usages",
- pending={**PENDING_TARGET, "attempts": prior_attempts, "rewrite_high_water": prior_high_water},
- rewrite={"rows_written": prior_high_water + 500},
+ pending={**PENDING_TARGET, "attempts": prior_attempts, "rewrite_high_water": 5_000_000},
+ rewrite={"rows_written": 230_000},
)
mock_schema_model.objects.select_related.return_value.get.return_value = schema
- mock_repartition.side_effect = RepartitionBudgetExceededError("out of budget")
+ mock_repartition.side_effect = RepartitionBudgetExceededError("out of budget", rows_written=230_000)
_maybe_repartition_table(
RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),
@@ -292,7 +293,6 @@ class TestBudgetExhaustion:
schema.stamp_last_repartition_at.assert_not_called()
updated = schema.set_repartition_pending.call_args.args[0]
assert updated["attempts"] == 0
- assert updated["rewrite_high_water"] == prior_high_water + 500
emitted = [c.args[0] for c in mock_capture_event.call_args_list]
assert "warehouse_repartition_failed" not in emitted
assert "warehouse_repartition_skipped" in emitted