: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
test(data-warehouse): speed up non-retryable short-circuit e2e test (#64692)
Frank Hamand72e93c29d42f
diff --git a/posthog/temporal/data_imports/external_data_job.py b/posthog/temporal/data_imports/external_data_job.pyindex 9f317f806d89..0c2a01f3cda6 100644--- a/posthog/temporal/data_imports/external_data_job.py+++ b/posthog/temporal/data_imports/external_data_job.py@@ -73,6 +73,12 @@ from products.warehouse_sources.backend.models.external_data_source import ExterLOGGER = get_logger(__name__)+# Cap retries at 3 in local dev so failing syncs don't loop for tens of minutes while developers+# iterate; prod cadence is unchanged. Defined at module level so tests can patch them to keep the+# expensive retry-exhaustion paths fast.+MAX_RESUMABLE_SOURCE_RETRIES = 3 if settings.DEBUG else 15+MAX_INCREMENTAL_SOURCE_RETRIES = 3 if settings.DEBUG else 9+Any_Source_Errors: dict[str, str | None] = {"Could not establish session to SSH gateway": None,# Raised by `SSHTunnel.get_tunnel` when `is_auth_valid()` fails — the SSH tunnel private key@@ -399,11 +405,8 @@ class ExternalDataJobWorkflow(PostHogWorkflow):source = SourceRegistry.get_source(ExternalDataSourceType(source_type))is_resumable_source = isinstance(source, ResumableSource)- # Cap retries at 3 in local dev so failing syncs don't loop for- # tens of minutes while developers iterate. Prod cadence is- # unchanged.- max_resumable_attempts = 3 if settings.DEBUG else 15- max_incremental_attempts = 3 if settings.DEBUG else 9+ max_resumable_attempts = MAX_RESUMABLE_SOURCE_RETRIES+ max_incremental_attempts = MAX_INCREMENTAL_SOURCE_RETRIESif is_resumable_source:timeout_params = {diff --git a/posthog/temporal/data_imports/pipelines/common/extract.py b/posthog/temporal/data_imports/pipelines/common/extract.pyindex 00c33cd079fe..c20a68f6345e 100644--- a/posthog/temporal/data_imports/pipelines/common/extract.py+++ b/posthog/temporal/data_imports/pipelines/common/extract.py@@ -53,6 +53,9 @@ def build_non_retryable_errors_redis_key(team_id: int, source_id: str, run_id: sreturn f"posthog:data_warehouse:non_retryable_errors:{team_id}:{source_id}:{run_id}"+NON_RETRYABLE_ERROR_RETRY_LIMIT = 3++async def trim_source_job_inputs(source: "ExternalDataSource") -> None:if not source.job_inputs:return@@ -169,9 +172,11 @@ async def handle_non_retryable_error()attempts = await redis_client.incr(retry_key)- if attempts <= 3:+ if attempts <= NON_RETRYABLE_ERROR_RETRY_LIMIT:await redis_client.expire(retry_key, 86400) # Expire after 24 hours- await logger.adebug(f"Non-retryable error attempt {attempts}/3, retrying. error={error_msg}")+ await logger.adebug(+ f"Non-retryable error attempt {attempts}/{NON_RETRYABLE_ERROR_RETRY_LIMIT}, retrying. error={error_msg}"+ )raise errorawait logger.adebug(f"Non-retryable error after {attempts} runs, giving up. error={error_msg}")diff --git a/posthog/temporal/tests/data_imports/test_end_to_end.py b/posthog/temporal/tests/data_imports/test_end_to_end.pyindex afb0f4ed6330..ed2c350f108c 100644--- a/posthog/temporal/tests/data_imports/test_end_to_end.py+++ b/posthog/temporal/tests/data_imports/test_end_to_end.py@@ -3361,50 +3361,71 @@ async def test_v3_delta_commit_metadata_and_idempotency_fallback(team, stripe_cu@pytest.mark.django_db(transaction=True)@pytest.mark.asyncio-async def test_non_retryable_error_short_circuiting(team, stripe_customer, mock_stripe_client):- with mock.patch("posthog.temporal.data_imports.sources.stripe.stripe.get_rows") as mock_get_rows:- mock_get_rows.side_effect = Exception("Some error that doesn't retry")[email protected]("pipeline_mode", ["non_dlt"], indirect=True)+async def test_non_retryable_error_short_circuiting(team, stripe_customer, mock_stripe_client, pipeline_mode):+ # The retry/short-circuit behaviour lives in the workflow + activity layer, upstream of the+ # v3/non_dlt split, so running a single pipeline mode is enough — running both just doubles the+ # cost. Each attempt re-executes the whole import activity, so we also shrink the retry budgets+ # to keep the test fast: cap resumable retries at 3 and make the non-retryable path give up after+ # 2 attempts. The contrast (3 retryable attempts vs 2 non-retryable attempts) is what proves the+ # short-circuit; the prod caps (15 / 3) are just larger values of the same mechanism.+ resumable_retry_cap = 3+ non_retryable_attempts = 2- with pytest.raises(Exception):- await _run(- team=team,- schema_name=STRIPE_CUSTOMER_RESOURCE_NAME,- table_name="stripe_customer",- source_type="Stripe",- job_inputs={- "auth_method": {"selection": "api_key", "stripe_secret_key": "test-key"},- "stripe_account_id": "acct_id",- },- mock_data_response=stripe_customer["data"],- ignore_assertions=True,- )-- # Resumable source syncs retry up to 15 times- assert mock_get_rows.call_count == 15-- source_cls = SourceRegistry.get_source(ExternalDataSourceType.STRIPE)- non_retryable_errors = source_cls.get_non_retryable_errors()- non_retryable_error = next(iter(non_retryable_errors.keys()))-- with mock.patch("posthog.temporal.data_imports.sources.stripe.stripe.get_rows") as mock_get_rows:- mock_get_rows.side_effect = Exception(non_retryable_error)+ with (+ mock.patch(+ "posthog.temporal.data_imports.external_data_job.MAX_RESUMABLE_SOURCE_RETRIES",+ resumable_retry_cap,+ ),+ mock.patch(+ "posthog.temporal.data_imports.pipelines.common.extract.NON_RETRYABLE_ERROR_RETRY_LIMIT",+ non_retryable_attempts - 1,+ ),+ ):+ with mock.patch("posthog.temporal.data_imports.sources.stripe.stripe.get_rows") as mock_get_rows:+ mock_get_rows.side_effect = Exception("Some error that doesn't retry")++ with pytest.raises(Exception):+ await _run(+ team=team,+ schema_name=STRIPE_CUSTOMER_RESOURCE_NAME,+ table_name="stripe_customer",+ source_type="Stripe",+ job_inputs={+ "auth_method": {"selection": "api_key", "stripe_secret_key": "test-key"},+ "stripe_account_id": "acct_id",+ },+ mock_data_response=stripe_customer["data"],+ ignore_assertions=True,+ )- with pytest.raises(Exception):- await _run(- team=team,- schema_name=STRIPE_CUSTOMER_RESOURCE_NAME,- table_name="stripe_customer",- source_type="Stripe",- job_inputs={- "auth_method": {"selection": "api_key", "stripe_secret_key": "test-key"},- "stripe_account_id": "acct_id",- },- mock_data_response=stripe_customer["data"],- ignore_assertions=True,- )+ # Resumable source syncs retry up to the configured cap+ assert mock_get_rows.call_count == resumable_retry_cap++ source_cls = SourceRegistry.get_source(ExternalDataSourceType.STRIPE)+ non_retryable_errors = source_cls.get_non_retryable_errors()+ non_retryable_error = next(iter(non_retryable_errors.keys()))++ with mock.patch("posthog.temporal.data_imports.sources.stripe.stripe.get_rows") as mock_get_rows:+ mock_get_rows.side_effect = Exception(non_retryable_error)++ with pytest.raises(Exception):+ await _run(+ team=team,+ schema_name=STRIPE_CUSTOMER_RESOURCE_NAME,+ table_name="stripe_customer",+ source_type="Stripe",+ job_inputs={+ "auth_method": {"selection": "api_key", "stripe_secret_key": "test-key"},+ "stripe_account_id": "acct_id",+ },+ mock_data_response=stripe_customer["data"],+ ignore_assertions=True,+ )- # Non-retryable errors are retried up to 3 times before giving up (4 total attempts)- assert mock_get_rows.call_count == 4+ # Non-retryable errors short-circuit before reaching the resumable cap+ assert mock_get_rows.call_count == non_retryable_attempts+ assert non_retryable_attempts < resumable_retry_cap@pytest.mark.django_db(transaction=True)