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.

fix(managed-warehouse): extend trino shadow execution deadline (#111874)

Eric Duong312c7cf07871
diff --git a/posthog/temporal/data_modeling/workflows/materialize_view.py b/posthog/temporal/data_modeling/workflows/materialize_view.py
index 680bbdcb8ddc..8f88418002b5 100644
--- a/posthog/temporal/data_modeling/workflows/materialize_view.py
+++ b/posthog/temporal/data_modeling/workflows/materialize_view.py
@@ -260,7 +260,7 @@ class MaterializeViewWorkflow(PostHogWorkflow):
dangerously_execute_raw_sql=inputs.dangerously_execute_raw_sql,
use_trino=use_trino,
),
- start_to_close_timeout=dt.timedelta(minutes=20),
+ start_to_close_timeout=dt.timedelta(minutes=35 if use_trino else 20),
heartbeat_timeout=dt.timedelta(minutes=2) if use_trino else None,
cancellation_type=(
temporalio.workflow.ActivityCancellationType.WAIT_CANCELLATION_COMPLETED
diff --git a/posthog/temporal/tests/data_modeling/test_materialize_view_workflow.py b/posthog/temporal/tests/data_modeling/test_materialize_view_workflow.py
index f2037fcdc257..3f4d9e869bfe 100644
--- a/posthog/temporal/tests/data_modeling/test_materialize_view_workflow.py
+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_workflow.py
@@ -422,6 +422,9 @@ class TestQualityGateBranching:
assert execute_activity.await_args_list[1].args[1].engine == DataModelingJobEngine.MANAGED_WAREHOUSE
collect.assert_awaited_once()
assert start_shadow.call_args.args[1].use_trino is trino_patch
+ assert start_shadow.call_args.kwargs["start_to_close_timeout"] == dt.timedelta(
+ minutes=35 if trino_patch else 20
+ )
def _cancelled_child_error() -> ChildWorkflowError:
diff --git a/products/managed_warehouse/backend/README.md b/products/managed_warehouse/backend/README.md
index d497dd4d2e60..e050a829d6d0 100644
--- a/products/managed_warehouse/backend/README.md
+++ b/products/managed_warehouse/backend/README.md
@@ -73,6 +73,8 @@ The manual trigger is deliberately separate from provisioning. A future provisio
Data modeling shadow materialization starts only when its feature flag is enabled, the managed warehouse is provisioned, the organization has a ready Trino target, and the saved query has a compiled translation for its current definition. Missing, failed, stale, or empty translations keep the shadow path disabled.
+Trino shadow materialization has a 30-minute execution deadline. Its Temporal activity allows 35 minutes so cancellation and cleanup can finish before the activity times out, and requires a heartbeat every two minutes. Deadline failures retain an explicit timeout message on the modeling job. Alias reconciliation keeps its separate five-minute execution deadline.
+
## Environment variables
The workflow obtains its DuckLake configuration from the following environment variables:
diff --git a/products/managed_warehouse/backend/tests/test_trino_execution.py b/products/managed_warehouse/backend/tests/test_trino_execution.py
index 980917d7bbc8..9feaae3d45c5 100644
--- a/products/managed_warehouse/backend/tests/test_trino_execution.py
+++ b/products/managed_warehouse/backend/tests/test_trino_execution.py
@@ -32,7 +32,7 @@ async def test_cancellation_reaches_cursor_and_waits_for_cleanup(deadline_expire
return 1
with patch.object(execution.asyncio, "timeout", return_value=timeout):
- task = asyncio.create_task(execution.run_trino_model(execute))
+ task = asyncio.create_task(execution.run_trino_model(execute, query_seconds=60))
try:
await asyncio.wait_for(entered.wait(), timeout=10)
if deadline_expired:
@@ -42,7 +42,10 @@ async def test_cancellation_reaches_cursor_and_waits_for_cleanup(deadline_expire
await asyncio.wait_for(canceled.wait(), timeout=10)
assert not task.done()
cleanup.set()
- with pytest.raises(TimeoutError if deadline_expired else asyncio.CancelledError):
+ with pytest.raises(
+ TimeoutError if deadline_expired else asyncio.CancelledError,
+ match="Trino model execution exceeded its 1-minute deadline" if deadline_expired else None,
+ ):
await task
cursor.cancel.assert_called()
finally:
@@ -55,4 +58,4 @@ async def test_cancellation_reaches_cursor_and_waits_for_cleanup(deadline_expire
@pytest.mark.asyncio
async def test_success_preserves_result() -> None:
- assert await execution.run_trino_model(lambda control: 42) == 42
+ assert await execution.run_trino_model(lambda control: 42, query_seconds=60) == 42
diff --git a/products/managed_warehouse/backend/trino_execution.py b/products/managed_warehouse/backend/trino_execution.py
index 5ba8235e387c..9cdb628b9cd9 100644
--- a/products/managed_warehouse/backend/trino_execution.py
+++ b/products/managed_warehouse/backend/trino_execution.py
@@ -15,7 +15,7 @@ if TYPE_CHECKING:
_Result = TypeVar("_Result")
-TRINO_QUERY_SECONDS = 15 * 60
+TRINO_QUERY_SECONDS = 30 * 60
_executor = ThreadPoolExecutor(thread_name_prefix="trino-model")
@@ -54,10 +54,11 @@ async def run_trino_model(
) -> _Result:
control = TrinoQueryControl(query_seconds)
task = asyncio.create_task(database_sync_to_async_pool(execute, executor=_executor)(control))
+ timeout = asyncio.timeout(query_seconds)
try:
- async with asyncio.timeout(query_seconds):
+ async with timeout:
return await asyncio.shield(task)
- except (asyncio.CancelledError, TimeoutError):
+ except (asyncio.CancelledError, TimeoutError) as error:
control.cancel()
while not task.done():
try:
@@ -66,6 +67,8 @@ async def run_trino_model(
continue
except Exception:
break
+ if isinstance(error, TimeoutError) and timeout.expired():
+ raise TimeoutError(f"Trino model execution exceeded its {query_seconds / 60:g}-minute deadline") from error
raise
finally:
control.finished.set()