Skip to content

: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.

feat(data-warehouse): let a new non-resumable import leave a stopping worker (#109063)

Tom Owers87086ac7d3f9
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
index 15145613ffad..002db4c94ed6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
@@ -1,5 +1,6 @@
import json
from contextlib import asynccontextmanager
+from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING, Any, NoReturn
from django.conf import settings
@@ -78,6 +79,7 @@ def build_non_retryable_errors_redis_key(team_id: int, source_id: str, run_id: s
NON_RETRYABLE_ERROR_RETRY_LIMIT = 3
+NON_RESUMABLE_HANDOFF_MAX_ATTEMPT_AGE = timedelta(minutes=10)
UNREADABLE_JOB_INPUTS_MESSAGE = (
@@ -788,6 +790,14 @@ async def update_row_tracking_after_batch(
await decrement_rows(team_id, schema_id, row_count)
+def _is_young_first_attempt() -> bool:
+ if not activity.in_activity():
+ return False
+
+ info = activity.info()
+ return info.attempt == 1 and datetime.now(UTC) - info.started_time < NON_RESUMABLE_HANDOFF_MAX_ATTEMPT_AGE
+
+
def should_check_shutdown(
schema: "ExternalDataSchema",
resource: SourceResponse,
@@ -797,12 +807,13 @@ def should_check_shutdown(
# Only raise if we're not running in descending order, otherwise we'll often not
# complete the job before the incremental value can be updated. Or if the source is
# resumable
- # TODO: raise when we're within `x` time of the worker being forced to shutdown
+ # Let a new attempt leave a shutting-down worker before it holds the worker for hours.
+ # Limit handoffs to attempt one so full refresh keeps two attempts for retries.
# Raising during a full reset will reset our progress back to 0 rows
incremental_sync_raise_during_shutdown = (
schema.should_use_incremental_field and resource.sort_mode != "desc" and not reset_pipeline
)
- return incremental_sync_raise_during_shutdown or source_is_resumable
+ return incremental_sync_raise_during_shutdown or source_is_resumable or _is_young_first_attempt()
async def finalize_desc_sort_incremental_value(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
index 128824970958..a86202c6cedf 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
@@ -28,6 +28,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.common.e
report_heartbeat_timeout,
reset_rows_synced_if_needed,
resolve_primary_keys,
+ should_check_shutdown,
trim_source_job_inputs,
validate_incremental_sync,
)
@@ -39,6 +40,44 @@ from products.warehouse_sources.backend.temporal.data_imports.util import NonRet
_EXTRACT_MODULE = "products.warehouse_sources.backend.temporal.data_imports.pipelines.common.extract"
+class TestShouldCheckShutdown:
+ @parameterized.expand(
+ [
+ ("young_first_attempt", False, "asc", True, False, True, 1, 2, True),
+ ("old_first_attempt", False, "asc", True, False, True, 1, 20, False),
+ ("young_second_attempt", False, "asc", True, False, True, 2, 1, False),
+ ("outside_activity", False, "asc", True, False, False, 1, 2, False),
+ ("resumable_source", False, "asc", True, True, True, 2, 20, True),
+ ("ascending_incremental", True, "asc", False, False, True, 2, 20, True),
+ ("descending_incremental", True, "desc", False, False, True, 2, 20, False),
+ ]
+ )
+ def test_should_check_shutdown(
+ self,
+ _name: str,
+ is_incremental: bool,
+ sort_mode: str,
+ reset_pipeline: bool,
+ source_is_resumable: bool,
+ in_activity: bool,
+ attempt: int,
+ age_minutes: int,
+ expected: bool,
+ ) -> None:
+ schema = MagicMock(should_use_incremental_field=is_incremental)
+ resource = MagicMock(sort_mode=sort_mode)
+ info = MagicMock(attempt=attempt, started_time=datetime.now(UTC) - timedelta(minutes=age_minutes))
+
+ with (
+ patch(f"{_EXTRACT_MODULE}.activity.in_activity", return_value=in_activity),
+ patch(f"{_EXTRACT_MODULE}.activity.info", return_value=info) as activity_info,
+ ):
+ assert should_check_shutdown(schema, resource, reset_pipeline, source_is_resumable) is expected
+
+ if not in_activity:
+ activity_info.assert_not_called()
+
+
class TestResolvePrimaryKeys:
@parameterized.expand(
[