: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): re-read repartition markers before giving up (#108215)
Tom Owerse02358142734
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.pyindex 393eaa36f3d8..71e3924c56cb 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@@ -419,23 +419,35 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin# 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, inputs.job_id):- # A killed attempt that still moved the checkpoint on is forward progress, not evidence the- # rewrite is doomed, which is the distinction `_handle_budget_exceeded` already draws for an- # 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_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 "- f"schema_id={schema.id}",- schema_id=str(schema.id),- rewrite_rows=_rewrite_rows_written(schema),- )- pending = _clear_attempts_after_progress(schema, pending, logger)- else:- _give_up(inputs, schema, pending, trigger_reason, logger)- return+ # Re-read the markers before acting on them. They were read before the job fetch and the+ # Delta-log work above, and the attempts that got us here are by definition ones that died+ # without recording an outcome — a heartbeat timeout among them leaves a predecessor still+ # running across that window, which is what the fresh claim `_give_up` stakes is for. Each+ # marker write persists the whole in-memory `sync_type_config`, so deciding on the stale copy+ # would erase a swap the predecessor staged in between: the only record of which scheme the+ # data on disk carries, and the marker that holds this schema's imports until it resolves.+ schema.refresh_from_db(fields=["sync_type_config"])+ swap = schema.repartition_swap+ pending = schema.repartition_pending or pending+ if swap is None and _exhausted_attempts(pending, inputs.job_id):+ # A killed attempt that still moved the checkpoint on is forward progress, not evidence+ # the rewrite is doomed, which is the distinction `_handle_budget_exceeded` already draws+ # for an 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_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 "+ f"schema_id={schema.id}",+ schema_id=str(schema.id),+ rewrite_rows=_rewrite_rows_written(schema),+ )+ pending = _clear_attempts_after_progress(schema, pending, logger)+ else:+ _give_up(inputs, schema, pending, trigger_reason, logger)+ return# Never while a swap is staged: that recovery runs no rewrite, so the checkpoint says nothing# about it, and temp is the only intact copy until it completes.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.pyindex 6bc4bbe1309b..7efef2b53396 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@@ -665,6 +665,67 @@ class TestKilledAttemptRetry:schema.clear_repartition_pending.assert_called_once()schema.stamp_last_repartition_at.assert_called_once()+ @patch(f"{MODULE}.capture_exception")+ @patch(f"{MODULE}.capture_repartition_event")+ @patch(f"{MODULE}.HeartbeaterSync")+ @patch(f"{MODULE}.repartition_table_in_place", new_callable=AsyncMock)+ @patch(f"{MODULE}.DeltaTableRef")+ @patch(f"{MODULE}.is_auto_coarsen_enabled", return_value=True)+ @patch(f"{MODULE}.is_auto_repartition_enabled", return_value=True)+ @patch(f"{MODULE}.ExternalDataJob")+ @patch(f"{MODULE}.ExternalDataSchema")+ def test_the_cap_does_not_discard_a_swap_staged_while_it_was_deciding(+ self,+ mock_schema_model: MagicMock,+ _mock_job_model: MagicMock,+ _mock_enabled: MagicMock,+ _mock_coarsen_enabled: MagicMock,+ _mock_helper_cls: MagicMock,+ mock_repartition: AsyncMock,+ _mock_heartbeater: MagicMock,+ mock_capture_event: MagicMock,+ _mock_capture_exception: MagicMock,+ ) -> None:+ # Every attempt that spends the cap died without recording an outcome, and a heartbeat-timed-out+ # one keeps running meanwhile: it can stage a swap between the markers being read and the cap+ # being weighed. Giving up there writes back the whole stale config, so the swap marker — which+ # says what scheme the data on disk now carries, and holds this schema's imports until it+ # resolves — would be erased and the next merge would run against settings that no longer+ # describe the table.+ 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": 488925,+ "run_rows": 488925,+ },+ rewrite={"rows_written": 488925},+ )+ staged_swap = {+ "state": "ready",+ "temp_uri": "s3://bucket/deals__repartitioned",+ "live_uri": "s3://bucket/deals",+ }+ schema.refresh_from_db.side_effect = lambda **_: setattr(schema, "repartition_swap", staged_swap)+ mock_schema_model.objects.select_related.return_value.get.return_value = schema+ mock_repartition.return_value = {"outcome": "completed"}++ _maybe_repartition_table(+ RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),+ MagicMock(),+ )++ emitted = [c.args[0] for c in mock_capture_event.call_args_list]+ assert "warehouse_repartition_failed" not in emitted+ schema.clear_repartition_swap.assert_not_called()+ schema.clear_repartition_pending.assert_not_called()+ # The swap is driven to completion instead of being abandoned at the cap.+ mock_repartition.assert_awaited_once()+class TestTransientObjectStoreFailure:@patch(f"{MODULE}.capture_exception")