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): keep a repartition that converges one budget per sync (#105077)

Tom Owersaee23920c4ec
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 ca8994c05945..7e7442dadaef 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
@@ -445,7 +445,7 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin
# attempt that ran out of budget. The checkpoint is the only signal available here, because a
# killed attempt records no outcome of its own. Otherwise a large table that converges one
# worker death per sync is abandoned at the cap, and `_give_up` discards its progress too.
- if _last_attempt_advanced_rewrite(schema, pending):
+ if _last_run_advanced_rewrite(schema, pending):
logger.warning(
f"repartition: attempts are spent but the rewrite advanced to "
f"{_rewrite_rows_written(schema)} rows, resetting the count and resuming "
@@ -807,18 +807,26 @@ def _rewrite_rows_written(schema: ExternalDataSchema) -> int:
return int((schema.repartition_rewrite or {}).get("rows_written") or 0)
-def _last_attempt_advanced_rewrite(schema: ExternalDataSchema, pending: dict[str, Any] | None) -> bool:
- """Whether the last attempt left the rewrite checkpoint further along than it found it.
+def _last_run_advanced_rewrite(schema: ExternalDataSchema, pending: dict[str, Any] | None) -> bool:
+ """Whether the last sync run left the rewrite checkpoint further along than it found it.
- Every attempt stamps `attempt_rows` with the checkpoint it is about to run from (see
- `_charge_attempt`), so a checkpoint reading past that stamp is the only trace an attempt killed
- outright can leave of the rows it committed. Strictly past: a checkpoint standing exactly where
- the attempt began is the stalled rewrite the cap exists to stop. A marker written before that
- stamp existed carries no `attempt_rows` and claims no progress.
+ A checkpoint reading past the stamp the run began on is the only trace an attempt killed
+ outright can leave of the rows it committed. The stamp has to be the run's, because the cap
+ counts sync runs and not the Temporal retries inside one (see `_charge_attempt`): `attempt_rows`
+ is re-stamped by every retry, so measuring against it asks only whether the run's *last* retry
+ moved anything. A run whose first retry streams for an hour and whose second dies on arrival
+ reads as stalled on that stamp, so a rewrite converging a budget per sync is abandoned
+ mid-flight. `run_rows` is written once per charged run and answers for the whole of it.
+
+ Strictly past: a checkpoint standing exactly where the run began is the stalled rewrite the cap
+ exists to stop. A marker written before `run_rows` existed falls back to `attempt_rows`, and one
+ carrying neither claims no progress.
"""
if pending is None:
return False
- started_from = pending.get("attempt_rows")
+ started_from = pending.get("run_rows")
+ if started_from is None:
+ started_from = pending.get("attempt_rows")
return started_from is not None and _rewrite_rows_written(schema) > int(started_from)
@@ -935,15 +943,18 @@ def _charge_attempt(
Every attempt also stamps `attempt_rows`, the rewrite checkpoint it is about to run from, charged
or not: an attempt killed outright records nothing itself, so that stamp is the only thing a
- retry can judge the attempt it retries against (see `_retrying_a_killed_attempt`).
+ retry can judge the attempt it retries against (see `_retrying_a_killed_attempt`). `run_rows` is
+ the same reading kept once per charged run, so the give-up check weighs the run the cap actually
+ counted rather than whichever retry happened to end it (see `_last_run_advanced_rewrite`).
"""
if pending is None:
return None
prior = int(pending.get("attempts", 0))
already_charged = pending.get("charged_job_id") == job_id
- marker = {**pending, "attempt_rows": _rewrite_rows_written(schema)}
+ rows_at_start = _rewrite_rows_written(schema)
+ marker = {**pending, "attempt_rows": rows_at_start}
if not already_charged:
- marker |= {"attempts": prior + 1, "charged_job_id": job_id}
+ marker |= {"attempts": prior + 1, "charged_job_id": job_id, "run_rows": rows_at_start}
try:
schema.set_repartition_pending(marker)
except Exception:
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 eddca14666c4..630d4fca4ffe 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
@@ -568,6 +568,8 @@ class TestKilledAttemptRetry:
mock_repartition.assert_awaited_once()
# Recorded for the next retry, which can only judge this attempt against where it began.
assert schema.repartition_pending["attempt_rows"] == checkpoint_rows
+ # The run's own stamp belongs to the charge, so a retry must never move it on.
+ assert schema.repartition_pending.get("run_rows") is None
else:
mock_repartition.assert_not_awaited()
skipped = [
@@ -585,9 +587,10 @@ class TestKilledAttemptRetry:
@parameterized.expand(
[
- ("checkpoint_advanced", 488925, 301744, True),
- ("checkpoint_stood_still", 488925, 488925, False),
- ("no_recorded_start", 488925, None, False),
+ ("checkpoint_advanced", 488925, 301744, None, True),
+ ("checkpoint_stood_still", 488925, 488925, None, False),
+ ("no_recorded_start", 488925, None, None, False),
+ ("run_advanced_though_its_last_retry_did_not", 488925, 488925, 301744, True),
]
)
@patch(f"{MODULE}.capture_exception")
@@ -604,6 +607,7 @@ class TestKilledAttemptRetry:
_name: str,
checkpoint_rows: int,
started_from: int | None,
+ run_started_from: int | None,
expect_resume: bool,
mock_schema_model: MagicMock,
_mock_job_model: MagicMock,
@@ -615,17 +619,22 @@ class TestKilledAttemptRetry:
mock_capture_event: MagicMock,
_mock_capture_exception: MagicMock,
) -> None:
- # A hard-killed attempt records no outcome; only its checkpoint can prove progress.
+ # A hard-killed attempt records no outcome; only its checkpoint can prove progress. The cap
+ # counts sync runs, so a run that advanced the rewrite and then spent its last retry dying
+ # on arrival still converged and must keep the table.
+ pending = {
+ **PENDING_TARGET,
+ "trigger_reason": "coarsening",
+ "attempts": MAX_REPARTITION_ATTEMPTS,
+ "charged_job_id": str(uuid.uuid4()),
+ "attempt_rows": started_from,
+ }
+ if run_started_from is not None:
+ pending["run_rows"] = run_started_from
schema = _schema(
name="public.deals",
s3_folder_name="deals",
- pending={
- **PENDING_TARGET,
- "trigger_reason": "coarsening",
- "attempts": MAX_REPARTITION_ATTEMPTS,
- "charged_job_id": str(uuid.uuid4()),
- "attempt_rows": started_from,
- },
+ pending=pending,
rewrite={"rows_written": checkpoint_rows},
)
mock_schema_model.objects.select_related.return_value.get.return_value = schema