: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(ducklake): scope register-data-imports workflow id per generation (#75241)
Eric Duong757897f0ead1
diff --git a/posthog/temporal/ducklake/ducklake_register_data_imports_workflow.py b/posthog/temporal/ducklake/ducklake_register_data_imports_workflow.pyindex 0cd6b106cc05..37bc0f1fcbab 100644--- a/posthog/temporal/ducklake/ducklake_register_data_imports_workflow.py+++ b/posthog/temporal/ducklake/ducklake_register_data_imports_workflow.py@@ -2,6 +2,7 @@ import reimport jsonimport uuidimport typing+import hashlibimport datetime as dtimport contextlibimport dataclasses@@ -207,6 +208,43 @@ def _is_valid_queryable_folder(queryable_folder: str) -> bool:return bool(queryable_folder) and "/" not in queryable_folder and queryable_folder not in {".", ".."}+# `<table>__query_<epoch_seconds>[_<8 hex>]`, produced by prepare_s3_files_for_querying.+_GENERATION_SUFFIX_PATTERN = re.compile(r"__query_(\d+(?:_[0-9a-f]{8})?)$")+++def _generation_token(prepared_queryable_folder: str) -> str:+ match = _GENERATION_SUFFIX_PATTERN.search(prepared_queryable_folder)+ if match:+ return match.group(1)+ # Folders predating the timestamped naming carry no generation, so derive a stable+ # token from the whole name to keep the workflow id unique per generation.+ return hashlib.sha256(prepared_queryable_folder.encode()).hexdigest()[:12]+++def build_register_data_imports_workflow_id(+ *,+ team_id: int,+ schema_id: str,+ job_id: str,+ prepared_queryable_folder: str,+) -> str:+ """Workflow id for one registration attempt, scoped to a single prepared generation.++ The generation belongs in the id because one job can publish several prepared+ generations: the v3 load consumer re-runs post-load on a redelivered final batch, and+ each run mints a new timestamped queryable folder. A job-scoped id makes every+ generation after the first collide with the in-flight run, and the caller treats+ WorkflowAlreadyStartedError as "already handled" and drops the trigger. The in-flight+ run is pinned to the older generation and correctly refuses to publish it, so the+ newest generation reaches DuckLake only on the next sync. Including the generation+ gives each one its own run, and a same-generation duplicate still coalesces, which is+ what that error should mean.+ """+ return (+ f"ducklake-register-data-imports-{team_id}-{schema_id}-{job_id}-{_generation_token(prepared_queryable_folder)}"+ )++def _resolve_data_imports_landing_uri(*,team_id: int,diff --git a/posthog/temporal/tests/ducklake/test_ducklake_register_data_imports_workflow.py b/posthog/temporal/tests/ducklake/test_ducklake_register_data_imports_workflow.pyindex 02a887776618..54264e782dc9 100644--- a/posthog/temporal/tests/ducklake/test_ducklake_register_data_imports_workflow.py+++ b/posthog/temporal/tests/ducklake/test_ducklake_register_data_imports_workflow.py@@ -3,6 +3,7 @@ import contextlibimport pytestfrom unittest.mock import MagicMock+from parameterized import parameterizedfrom temporalio.exceptions import ApplicationErrorfrom posthog.ducklake import cp_teams@@ -14,6 +15,7 @@ from posthog.temporal.ducklake.ducklake_register_data_imports_workflow import (DuckLakeRegisterDataImportsGateInputs,DuckLakeRegisterDataImportsInputs,DuckLakeRegisterDataImportsMetadata,+ build_register_data_imports_workflow_id,copy_and_register_ducklake_data_imports_activity,ducklake_register_data_imports_gate_activity,prepare_ducklake_data_imports_registration_activity,@@ -258,3 +260,42 @@ def _activity_inputs() -> DuckLakeRegisterDataImportsActivityInputs:ducklake_table_name="postgres_customers",),)+++def _workflow_id(prepared_queryable_folder: str) -> str:+ return build_register_data_imports_workflow_id(+ team_id=473662,+ schema_id="019ef5df-e4c7-0000-b543-8ef7f13b5f15",+ job_id="019fb012-26e7-0000-2959-704b254131bd",+ prepared_queryable_folder=prepared_queryable_folder,+ )+++ [+ (+ "timestamped",+ "customer_balance_transaction__query_1785365519_02076d94",+ "customer_balance_transaction__query_1785365530_d3277966",+ ),+ (+ "untimestamped",+ "customer_balance_transaction__query",+ "customer_balance_transaction__query_legacy",+ ),+ ]+)+def test_workflow_id_differs_per_prepared_generation(_name, earlier_folder, later_folder):+ earlier = _workflow_id(earlier_folder)+ later = _workflow_id(later_folder)++ assert earlier != later+++def test_workflow_id_is_stable_for_one_prepared_generation():+ folder = "customer_balance_transaction__query_1785365530_d3277966"++ first = _workflow_id(folder)+ second = _workflow_id(folder)++ assert first == seconddiff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.pyindex d29a0042a5f4..42ddeabcf45c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py@@ -470,6 +470,7 @@ def _trigger_ducklake_register_data_imports(export_signal: ExportSignalMessage,from posthog.temporal.ducklake.ducklake_register_data_imports_workflow import (DuckLakeRegisterDataImportsInputs,DuckLakeRegisterDataImportsWorkflow,+ build_register_data_imports_workflow_id,)# Connect and start inside one event loop: sync_connect() builds the client in@@ -485,9 +486,11 @@ def _trigger_ducklake_register_data_imports(export_signal: ExportSignalMessage,schema_id=uuid.UUID(export_signal.schema_id),prepared_queryable_folder=prepared_queryable_folder,),- id=(- f"ducklake-register-data-imports-{export_signal.team_id}-"- f"{export_signal.schema_id}-{export_signal.job_id}"+ id=build_register_data_imports_workflow_id(+ team_id=export_signal.team_id,+ schema_id=export_signal.schema_id,+ job_id=export_signal.job_id,+ prepared_queryable_folder=prepared_queryable_folder,),task_queue=settings.DUCKLAKE_TASK_QUEUE,)@@ -500,8 +503,8 @@ def _trigger_ducklake_register_data_imports(export_signal: ExportSignalMessage,external_data_job_id=export_signal.job_id,)except WorkflowAlreadyStartedError:- # Same job already has a registration in flight (e.g. a V2 child, or a duplicate- # final-batch redelivery) — registration is already handled, nothing to do.+ # The id is scoped to this prepared generation, so a collision means this exact+ # generation is already being registered and dropping the duplicate is correct.logger.info("ducklake_registration_workflow_already_started",team_id=export_signal.team_id,