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): anchor repartition rewrite deadline to activity start (#88302)

Tom Owers3b0a89e6b25f
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 2a1a6d4cdf26..a7139b464927 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
@@ -103,9 +103,18 @@ _TRANSIENT_ERROR_SNIPPETS = (
REWRITE_DEADLINE_MARGIN = dt.timedelta(minutes=5)
-def _rewrite_deadline() -> float | None:
+def _rewrite_deadline(activity_started: float) -> float | None:
"""Monotonic time by which the rewrite must stop to leave room to record its own failure.
+ `activity_started` is the `time.monotonic()` reading taken when the activity began. The deadline
+ is anchored to it, not to now: the budget is measured from `start_to_close_timeout`, which Temporal
+ counts from the same start, so the pre-rewrite work (job fetch, flag evaluation, the pre-extraction
+ Delta-log measurement, temp validation) has to be charged against it too. Anchoring to now instead
+ hands the rewrite a deadline later than the activity's own timeout by however long that pre-work
+ took, so on the heavily-fragmented tables this path exists to rescue — where reading the log alone
+ runs into minutes — Temporal kills the rewrite mid-stream before it can record an outcome, and the
+ hard-killed attempt counts toward the give-up cap.
+
None when there is no budget to derive one from: outside an activity context (direct calls from
tests), when the activity declares no `start_to_close_timeout`, or when that timeout is shorter
than the margin. In each case the rewrite runs unbounded, which is the behavior that predates
@@ -120,7 +129,7 @@ def _rewrite_deadline() -> float | None:
budget = (info.start_to_close_timeout - REWRITE_DEADLINE_MARGIN).total_seconds()
if budget <= 0:
return None
- return time.monotonic() + budget
+ return activity_started + budget
def _is_transient_infra_error(error: Exception) -> bool:
@@ -242,6 +251,10 @@ def maybe_repartition_table_activity(inputs: RepartitionActivityInputs) -> None:
def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: FilteringBoundLogger) -> None:
+ # Anchor the rewrite deadline to when the activity began, so the pre-rewrite work below (job fetch,
+ # flag evaluation, pre-extraction Delta-log measurement) is charged against the activity's timeout
+ # rather than handed to the rewrite on top of it. See `_rewrite_deadline`.
+ activity_started = time.monotonic()
try:
schema = retry_on_db_connection_drop(
lambda: ExternalDataSchema.objects.select_related("source").get(id=inputs.schema_id)
@@ -412,7 +425,7 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin
target=target,
logger=logger,
claim_token=claim_token,
- deadline=_rewrite_deadline(),
+ deadline=_rewrite_deadline(activity_started),
)
except RepartitionBudgetExceededError as e:
# The rewrite didn't fit in one activity's budget. Checkpoint/resume lets a large table
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 8d5be47ca48a..f6d0fb898548 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
@@ -192,11 +192,23 @@ class TestRewriteDeadline:
else MagicMock(return_value=info)
)
+ with patch(f"{MODULE}.activity.info", info_fn):
+ assert _rewrite_deadline(1000.0) == expected
+
+ def test_deadline_is_anchored_to_the_activity_start_not_now(self) -> None:
+ # The deadline must be measured from when the activity began, because Temporal counts the
+ # start_to_close_timeout from the same point. Time already spent before the rewrite starts
+ # (log measurement on a fragmented table can run into minutes) has to shrink the rewrite's
+ # window, not be handed to it on top of the full budget — otherwise the rewrite outlives the
+ # timeout and Temporal kills it before it records an outcome.
+ info = MagicMock()
+ info.start_to_close_timeout = dt.timedelta(hours=6)
with (
- patch(f"{MODULE}.activity.info", info_fn),
- patch(f"{MODULE}.time", MagicMock(monotonic=MagicMock(return_value=1000.0))),
+ patch(f"{MODULE}.activity.info", MagicMock(return_value=info)),
+ # A later wall reading must not move the deadline: it depends only on the passed anchor.
+ patch(f"{MODULE}.time", MagicMock(monotonic=MagicMock(return_value=9_999_999.0))),
):
- assert _rewrite_deadline() == expected
+ assert _rewrite_deadline(1000.0) == 22300.0
class TestBudgetExhaustion: