: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): retry transient redis blips before failing closed on v3 pipeline lock (#73384)
Tom Owersab983107d8c7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/sync_lock.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/sync_lock.pyindex 9073ccb29b1b..9d0033e51ad8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/sync_lock.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/sync_lock.py@@ -8,6 +8,7 @@ from django.conf import settingsimport redisimport structlog+from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitterfrom posthog.exceptions_capture import capture_exceptionfrom posthog.redis import get_client@@ -42,8 +43,22 @@ def _lock_meta_key(team_id: int, schema_id: str) -> str:return f"{LOCK_META_KEY_PREFIX}:{team_id}:{schema_id}"+@retry(+ retry=retry_if_exception_type((redis.exceptions.ConnectionError, redis.exceptions.TimeoutError)),+ stop=stop_after_attempt(3),+ wait=wait_exponential_jitter(initial=0.1, max=1),+ reraise=True,+)+def _connect_and_ping(redis_client: redis.Redis) -> None:+ redis_client.ping()++@contextmanagerdef _get_redis_client() -> Generator[redis.Redis | None]:+ # The acquire/release activities run with a single Temporal attempt (see+ # external_data_job.py), so a bare DNS/connection blip here has no outer retry+ # and would skip the whole scheduled sync run. Absorb a few quick retries before+ # falling back to the fail-closed/fail-silent behavior callers rely on.redis_client = Nonetry:if not settings.DATA_WAREHOUSE_REDIS_HOST or not settings.DATA_WAREHOUSE_REDIS_PORT:@@ -52,7 +67,7 @@ def _get_redis_client() -> Generator[redis.Redis | None]:)redis_client = get_client(f"redis://{settings.DATA_WAREHOUSE_REDIS_HOST}:{settings.DATA_WAREHOUSE_REDIS_PORT}/")- redis_client.ping()+ _connect_and_ping(redis_client)except Exception as e:logger.exception("redis_unavailable_for_v3_pipeline_lock", error=str(e))capture_exception(e)diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/test_sync_lock.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/test_sync_lock.pyindex 847080c94933..38a60da0aa14 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/test_sync_lock.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/test_sync_lock.py@@ -1,8 +1,11 @@import pytestfrom unittest.mock import MagicMock, patch+import redis+from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.sync_lock import (LOCK_TTL_SECONDS,+ _get_redis_client,_lock_key,acquire_v3_pipeline_lock,get_v3_pipeline_lock_holder,@@ -122,3 +125,28 @@ class TestReleaseV3PipelineLock:mock_ctx.return_value.__exit__ = MagicMock(return_value=False)assert release_v3_pipeline_lock(1, "s-1", "tok-1") is False+++class TestGetRedisClient:+ """The acquire/release activities run with a single Temporal attempt, so a bare+ DNS/connection blip previously skipped the whole scheduled sync run with no retry."""++ @patch("products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.sync_lock.get_client")+ def test_recovers_from_transient_connection_error(self, mock_get_client: MagicMock) -> None:+ mock_redis = MagicMock()+ mock_redis.ping.side_effect = [redis.exceptions.ConnectionError("Temporary failure in name resolution"), None]+ mock_get_client.return_value = mock_redis++ with _get_redis_client() as client:+ assert client is mock_redis+ assert mock_redis.ping.call_count == 2++ @patch("products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.sync_lock.get_client")+ def test_fails_closed_after_exhausting_retries(self, mock_get_client: MagicMock) -> None:+ mock_redis = MagicMock()+ mock_redis.ping.side_effect = redis.exceptions.ConnectionError("Temporary failure in name resolution")+ mock_get_client.return_value = mock_redis++ with _get_redis_client() as client:+ assert client is None+ assert mock_redis.ping.call_count == 3