: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): keep cdp producer db blips retryable (#73391)
Tom Owers423daca21aa7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/cdp_producer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/cdp_producer.pyindex 018c72c9e250..5ce64dc20111 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/cdp_producer.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/cdp_producer.py@@ -4,6 +4,7 @@ import asyncioimport hashlibfrom django.conf import settings+from django.db.utils import OperationalError as DjangoOperationalErrorimport orjsonimport pyarrow as pa@@ -23,6 +24,7 @@ from products.cdp.backend.models.hog_functions import HogFunctionfrom products.data_warehouse.backend.facade.api import aget_s3_client, ensure_bucket_existsfrom products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchemafrom products.warehouse_sources.backend.temporal.data_imports.pipelines.helpers import build_table_name+from products.warehouse_sources.backend.temporal.data_imports.util import PostHogInternalDatabaseErrorfrom products.workflows.backend.models.hog_flow.hog_flow import HogFlow@@ -119,28 +121,38 @@ class CDPProducer:self.logger.debug(f"Checking if table {dot_notated_table_name} is used in any hog functions or workflows")self.logger.debug(f"Using table_name = {dot_notated_table_name}, source = data-warehouse-table")- has_matching_hog_function = (- HogFunction.objects.filter(- team_id=self.team_id,- enabled=True,- filters__source="data-warehouse-table",- filters__data_warehouse__contains=[{"table_name": dot_notated_table_name}],+ try:+ has_matching_hog_function = (+ HogFunction.objects.filter(+ team_id=self.team_id,+ enabled=True,+ filters__source="data-warehouse-table",+ filters__data_warehouse__contains=[{"table_name": dot_notated_table_name}],+ )+ .exclude(deleted=True)+ .exists())- .exclude(deleted=True)- .exists()- )- if has_matching_hog_function:- return True-- # Also gate on active workflows (HogFlows) triggered by this table - without this the- # producer never emits to Kafka for a team whose only consumer is a warehouse-triggered workflow.- return HogFlow.objects.filter(- team_id=self.team_id,- status=HogFlow.State.ACTIVE,- trigger__type="data-warehouse-table",- trigger__table_name=dot_notated_table_name,- ).exists()+ if has_matching_hog_function:+ return True++ # Also gate on active workflows (HogFlows) triggered by this table - without this the+ # producer never emits to Kafka for a team whose only consumer is a warehouse-triggered workflow.+ return HogFlow.objects.filter(+ team_id=self.team_id,+ status=HogFlow.State.ACTIVE,+ trigger__type="data-warehouse-table",+ trigger__table_name=dot_notated_table_name,+ ).exists()+ except DjangoOperationalError as e:+ # This queries PostHog's own database, not the source being synced. A transient+ # failure reaching it (e.g. a DNS blip resolving our host) stringifies with the+ # same wording a customer's misconfigured source host would, which the source's+ # `get_non_retryable_errors` would misclassify as non-retryable and permanently+ # stop a healthy sync. Re-raise clear of those substrings so it stays retryable.+ raise PostHogInternalDatabaseError(+ "Failed to check hog function/workflow triggers in PostHog's database"+ ) from eself._should_produce_cache = await _check()return self._should_produce_cachediff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_cdp_producer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_cdp_producer.pyindex d6de2938001c..822c95c04570 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_cdp_producer.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_cdp_producer.py@@ -7,6 +7,8 @@ import pytestfrom unittest import mockfrom unittest.mock import MagicMock, patch+from django.db.utils import OperationalError as DjangoOperationalError+import pyarrow as paimport pyarrow.parquet as pqfrom asgiref.sync import sync_to_async@@ -16,6 +18,8 @@ from products.warehouse_sources.backend.models.external_data_schema import Exterfrom products.warehouse_sources.backend.models.external_data_source import ExternalDataSourcefrom products.warehouse_sources.backend.models.table import DataWarehouseTablefrom products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.cdp_producer import CDPProducer+from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.source import PostgresSource+from products.warehouse_sources.backend.temporal.data_imports.util import PostHogInternalDatabaseErrorfrom products.warehouse_sources.backend.types import ExternalDataSourceTypefrom products.workflows.backend.models.hog_flow.hog_flow import HogFlow@@ -286,6 +290,39 @@ async def test_should_produce_table_with_both_hog_function_and_flow(team):assert await producer.should_produce_table() is True[email protected]_db(transaction=True)+async def test_should_produce_table_posthog_database_connection_failure_stays_retryable(team):+ # `should_produce_table` queries PostHog's own database (HogFunction/HogFlow), not the+ # source being synced. A transient connection failure there (e.g. a DNS blip resolving our+ # host) surfaces the same "Name or service not known" wording a customer's misconfigured+ # source host would, so it must be re-raised as PostHogInternalDatabaseError to avoid being+ # misclassified as non-retryable by the source's `get_non_retryable_errors`.+ source = await sync_to_async(ExternalDataSource.objects.create)(+ team=team, source_type=ExternalDataSourceType.POSTGRES+ )+ table = await sync_to_async(DataWarehouseTable.objects.create)(+ team=team, name="postgres_table_1", external_data_source=source+ )+ schema = await sync_to_async(ExternalDataSchema.objects.create)(+ team=team, name="table_1", source=source, table=table+ )++ producer = CDPProducer(team_id=team.id, schema_id=str(schema.id), job_id="", logger=mock.AsyncMock())++ with patch(+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.cdp_producer.HogFunction.objects"+ ) as mock_hog_function_objects:+ mock_hog_function_objects.filter.side_effect = DjangoOperationalError("[Errno -2] Name or service not known")+ with pytest.raises(PostHogInternalDatabaseError) as exc_info:+ await producer.should_produce_table()++ non_retryable = PostgresSource().get_non_retryable_errors()+ error_msg = str(exc_info.value)+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())+ assert not is_non_retryable, f"A PostHog-side DB connection failure must stay retryable: {error_msg}"++@pytest.mark.django_db(transaction=True)@pytest.mark.asyncio@patch("products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.cdp_producer.aget_s3_client")diff --git a/products/warehouse_sources/backend/temporal/data_imports/util.py b/products/warehouse_sources/backend/temporal/data_imports/util.pyindex bbdb64839842..fc39a3a16c6a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/util.py+++ b/products/warehouse_sources/backend/temporal/data_imports/util.py@@ -26,6 +26,18 @@ class NonRetryableException(Exception):return self.__cause__+class PostHogInternalDatabaseError(Exception):+ """Raised when shared pipeline code fails to reach PostHog's own database.++ A transient connectivity blip reaching our database (e.g. a DNS hiccup resolving our+ host) stringifies with the same wording (e.g. "Name or service not known") a customer's+ misconfigured source host would produce. Sources' `get_non_retryable_errors` match on+ that wording to stop syncs against a permanently broken customer host, so this error's+ message intentionally avoids those substrings to keep it retryable instead of being+ misclassified as a permanent failure of the source being synced.+ """++# 10 mins buffer to avoid deleting files Clickhouse may be readingS3_DELETE_TIME_BUFFER = 600