: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): stand down on a transient db error fetching the repartition schema or job (#102809)
Tom Owers9d9950fa49a2
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 51b95f112cec..894bc1a89248 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@@ -296,6 +296,16 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterinschema_id=inputs.schema_id,)return+ except Exception as e:+ # retry_on_db_connection_drop already retried once; a second failure here (e.g. the worker+ # briefly exhausting its file descriptors under load) is the same transient-infra shape the+ # rewrite itself stands down on below, just hit before a run has even started. No claim has+ # been staked and no attempt charged yet, so standing down costs nothing: the table is simply+ # picked up again on the next sync.+ if not _is_transient_infra_error(e):+ raise+ logger.warning("repartition: database unavailable while fetching schema, standing down", exc_info=True)+ return# A table with a pending corruption revive must heal first — the extract activity resets it and# rebuilds from source. Repartitioning it here would interleave with that heal and re-hollow the@@ -367,6 +377,14 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterinjob_id=inputs.job_id,)return+ except Exception as e:+ # See the matching comment on the schema fetch above: a second connection failure after+ # retry_on_db_connection_drop's own retry is transient infra, not a repartition bug, and+ # nothing has been claimed or charged yet.+ if not _is_transient_infra_error(e):+ raise+ logger.warning("repartition: database unavailable while fetching job, standing down", exc_info=True)+ return# Attach the same source/schema identity the import activity does, so an exception captured# anywhere below (budget exhaustion, an unexpected rewrite failure) can be attributed to adiff --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 4d4a8e4c213f..16ff5131cbd3 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@@ -12,6 +12,8 @@ from parameterized import parameterizedfrom posthog.exceptions_capture import ambient_exception_properties+from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob+from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchemafrom products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.errors import (TransientObjectStoreError,)@@ -832,6 +834,79 @@ class TestTransientObjectStoreFailure:)+class TestEarlyFetchTransientInfraError:+ """A DB error fetching the schema or job, before any claim is staked or attempt charged, must+ stand down like the rewrite's own transient-infra handling instead of escaping to error+ tracking. `retry_on_db_connection_drop`'s built-in retry only clears a stale pooled+ connection; it does nothing for e.g. the worker running out of file descriptors, so the same+ error resurfaces on the retry and previously propagated all the way to error tracking."""++ @patch(f"{MODULE}.capture_exception")+ @patch(f"{MODULE}.ExternalDataJob")+ @patch(f"{MODULE}.ExternalDataSchema")+ def test_transient_db_error_fetching_schema_stands_down(+ self,+ mock_schema_model: MagicMock,+ _mock_job_model: MagicMock,+ mock_capture_exception: MagicMock,+ ) -> None:+ mock_schema_model.DoesNotExist = ExternalDataSchema.DoesNotExist+ mock_schema_model.objects.select_related.return_value.get.side_effect = OperationalError(+ "[Errno 24] Too many open files"+ )++ _maybe_repartition_table(+ RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),+ MagicMock(),+ )++ mock_capture_exception.assert_not_called()++ @patch(f"{MODULE}.capture_exception")+ @patch(f"{MODULE}.ExternalDataJob")+ @patch(f"{MODULE}.ExternalDataSchema")+ def test_non_transient_error_fetching_schema_still_raises(+ self,+ mock_schema_model: MagicMock,+ _mock_job_model: MagicMock,+ mock_capture_exception: MagicMock,+ ) -> None:+ # Guards against overbroadening the stand-down: a real bug fetching the schema must still+ # surface, not be silently swallowed alongside the transient case above.+ mock_schema_model.DoesNotExist = ExternalDataSchema.DoesNotExist+ mock_schema_model.objects.select_related.return_value.get.side_effect = ValueError("boom")++ with pytest.raises(ValueError):+ _maybe_repartition_table(+ RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),+ MagicMock(),+ )+ mock_capture_exception.assert_not_called()++ @patch(f"{MODULE}.capture_exception")+ @patch(f"{MODULE}.is_auto_repartition_enabled", return_value=True)+ @patch(f"{MODULE}.ExternalDataJob")+ @patch(f"{MODULE}.ExternalDataSchema")+ def test_transient_db_error_fetching_job_stands_down(+ self,+ mock_schema_model: MagicMock,+ mock_job_model: MagicMock,+ _mock_enabled: MagicMock,+ mock_capture_exception: MagicMock,+ ) -> None:+ schema = _schema(name="public.usages", s3_folder_name="usages")+ mock_schema_model.objects.select_related.return_value.get.return_value = schema+ mock_job_model.DoesNotExist = ExternalDataJob.DoesNotExist+ mock_job_model.objects.get.side_effect = OperationalError("[Errno 24] Too many open files")++ _maybe_repartition_table(+ RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),+ MagicMock(),+ )++ mock_capture_exception.assert_not_called()++class TestFeatureFlagGate:@parameterized.expand([