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): stop a repartition retry abandoning the table (#95956)

Tom Owers07778f74486a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
index 2159e326743e..23a67dc42e2d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
@@ -830,6 +830,36 @@ class TestRepartitionActivity:
pending = schema.repartition_pending
assert pending is not None and pending["attempts"] == 1
+ def test_a_retry_inside_the_run_that_spent_the_cap_still_rewrites(self, team):
+ # The attempt is charged before the rewrite, so on the run that charges the last one the
+ # persisted count already reads the cap when that run's own Temporal retry re-reads it. Giving
+ # up there abandoned the table terminally on a heartbeat blip, before the retry ran the
+ # rewrite at all, although the cap counts the sync runs that failed and not the retries
+ # within one.
+ schema = _make_schema(team, {})
+ schema.set_repartition_pending(
+ {
+ "partition_mode": "md5",
+ "partition_count": 4,
+ "partition_keys": ["id"],
+ "trigger_reason": "t",
+ "attempts": MAX_REPARTITION_ATTEMPTS - 1,
+ }
+ )
+ inputs = self._inputs(team, schema)
+
+ def killed_mid_rewrite(**kwargs):
+ raise KeyboardInterrupt("worker killed")
+
+ with contextlib.suppress(BaseException):
+ self._run(inputs, AsyncMock(side_effect=killed_mid_rewrite))
+
+ rewrite = AsyncMock(return_value={"outcome": "completed", "row_count": 6, "partition_mode_after": "md5"})
+ capture = self._run(inputs, rewrite)
+
+ rewrite.assert_awaited_once()
+ assert "warehouse_repartition_failed" not in [c.args[0] for c in capture.call_args_list]
+
def test_an_overlapping_attempt_charge_survives_another_attempts_refund(self, team):
# Overlapping attempts must not erase each other's charge, or the cap never counts up and the
# retry loop this change exists to stop comes back.
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 6de632430ed5..efeab860e4c7 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
@@ -404,7 +404,7 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin
# Never while a swap is staged: an interrupted swap may already have deleted live, leaving temp
# the only intact copy, and `_give_up` clears the marker that points at it. A ready swap has to be
# completed however many attempts it took to get here.
- if swap is None and _exhausted_attempts(pending):
+ if swap is None and _exhausted_attempts(pending, inputs.job_id):
_give_up(inputs, schema, pending, trigger_reason, logger)
return
@@ -700,8 +700,21 @@ def _handle_budget_exceeded(
return _handle_failure(inputs, schema, pending, trigger_reason, error, claim_token, logger, charged_attempts)
-def _exhausted_attempts(pending: dict[str, Any] | None) -> bool:
- return pending is not None and int(pending.get("attempts", 0)) >= MAX_REPARTITION_ATTEMPTS
+def _exhausted_attempts(pending: dict[str, Any] | None, job_id: str) -> bool:
+ """Whether earlier sync runs already spent the whole retry cap on this rewrite.
+
+ Discounts a charge this run made itself. An attempt is charged before the rewrite (see
+ `_charge_attempt`), and Temporal retries the activity inside one sync, so on the run that charges
+ the last attempt the persisted count already reads the cap when that run's own retry re-reads it.
+ Giving up there abandons the table before the retry runs the rewrite at all, because the cap
+ counts the sync runs that failed and not the retries within one.
+ """
+ if pending is None:
+ return False
+ attempts = int(pending.get("attempts", 0))
+ if pending.get("charged_job_id") == job_id:
+ attempts -= 1
+ return attempts >= MAX_REPARTITION_ATTEMPTS
def _give_up(