: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(managed-warehouse): retry trino shadow builds after worker node failures (#119096)
posthog[bot]9559a6b89e0f
diff --git a/posthog/temporal/data_modeling/activities/utils.py b/posthog/temporal/data_modeling/activities/utils.pyindex fbf6c0aeac98..b8adbf1687ca 100644--- a/posthog/temporal/data_modeling/activities/utils.py+++ b/posthog/temporal/data_modeling/activities/utils.py@@ -113,6 +113,8 @@ EXTERNALLY_ABORTED_MARKERS = (# Trino killed the query because the whole cluster ran out of memory. A query over its own# memory limit fails with an EXCEEDED_*_MEMORY_LIMIT name and still counts."name=CLUSTER_OUT_OF_MEMORY,",+ # a Trino worker node crashed or stopped answering, so the query never got to finish+ "name=TOO_MANY_REQUESTS_FAILED,",)STREAM_FAULT_ERROR_PREFIXES = (diff --git a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.pyindex 4d1de8e44999..34eb26ee951c 100644--- a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py@@ -875,6 +875,7 @@ class TestNodeSuspension:"This model has been suspended after 5 consecutive failed materializations. ""Error: InvalidMessageFormat: Arrow stream contains bytes that are not an IPC message: 'garbage'",'TrinoQueryError(type=INSUFFICIENT_RESOURCES, name=CLUSTER_OUT_OF_MEMORY, message="Query killed because the cluster is out of memory. Please try again in a few minutes.", query_id=20261011_000000_00000_abcde)',+ 'TrinoQueryError(type=INTERNAL_ERROR, name=TOO_MANY_REQUESTS_FAILED, message="Encountered too many errors talking to a worker node. The node may have crashed or be under too much load.", query_id=20261017_000000_00000_abcde)',],)async def test_externally_aborted_failures_do_not_suspend(self, ateam, anode, asaved_query, adag, aborted_error):diff --git a/products/managed_warehouse/backend/tests/test_trino_execution.py b/products/managed_warehouse/backend/tests/test_trino_execution.pyindex 1243ab4dee08..f63580ebf321 100644--- a/products/managed_warehouse/backend/tests/test_trino_execution.py+++ b/products/managed_warehouse/backend/tests/test_trino_execution.py@@ -66,9 +66,9 @@ async def test_success_preserves_result() -> None:@pytest.mark.asyncio@pytest.mark.parametrize("error_name,expected_attempts",- [("CLUSTER_OUT_OF_MEMORY", 2), ("EXCEEDED_LOCAL_MEMORY_LIMIT", 1)],+ [("CLUSTER_OUT_OF_MEMORY", 2), ("TOO_MANY_REQUESTS_FAILED", 2), ("EXCEEDED_LOCAL_MEMORY_LIMIT", 1)],)-async def test_retries_only_cluster_out_of_memory(error_name: str, expected_attempts: int) -> None:+async def test_retries_only_transient_trino_errors(error_name: str, expected_attempts: int) -> None:attempts = 0processed: list[int] = []diff --git a/products/managed_warehouse/backend/trino_execution.py b/products/managed_warehouse/backend/trino_execution.pyindex 29748ed21be9..337dff3c73d9 100644--- a/products/managed_warehouse/backend/trino_execution.py+++ b/products/managed_warehouse/backend/trino_execution.py@@ -18,10 +18,12 @@ if TYPE_CHECKING:_Result = TypeVar("_Result")TRINO_QUERY_SECONDS = 6 * 60 * 60-# Trino's low-memory killer stops a query when the whole cluster runs out of memory, and asks the-# client to try again in a few minutes. A query over its own memory limit fails with another name.-CLUSTER_OUT_OF_MEMORY = "CLUSTER_OUT_OF_MEMORY"-CLUSTER_OUT_OF_MEMORY_RETRY_DELAYS_SECONDS = (2 * 60, 5 * 60)+# Trino stops these queries for a cluster fault, not for the query itself, and asks the client to try+# again in a few minutes. The low-memory killer raises CLUSTER_OUT_OF_MEMORY when the whole cluster runs+# out of memory. TOO_MANY_REQUESTS_FAILED means a worker node crashed or stopped answering. A query over+# its own memory limit fails with another name.+TRANSIENT_TRINO_ERROR_NAMES = frozenset({"CLUSTER_OUT_OF_MEMORY", "TOO_MANY_REQUESTS_FAILED"})+TRANSIENT_TRINO_ERROR_RETRY_DELAYS_SECONDS = (2 * 60, 5 * 60)_executor = ThreadPoolExecutor(thread_name_prefix="trino-model")logger = structlog.get_logger(__name__)@@ -94,18 +96,18 @@ async def run_trino_model(control.finished.set()-def is_cluster_out_of_memory(error: BaseException) -> bool:- return getattr(error, "error_name", None) == CLUSTER_OUT_OF_MEMORY+def is_transient_trino_error(error: BaseException) -> bool:+ return getattr(error, "error_name", None) in TRANSIENT_TRINO_ERROR_NAMESasync def run_trino_model_with_retries(execute: Callable[[TrinoQueryControl], _Result],*,query_seconds: float = TRINO_QUERY_SECONDS,- retry_delays: tuple[float, ...] = CLUSTER_OUT_OF_MEMORY_RETRY_DELAYS_SECONDS,+ retry_delays: tuple[float, ...] = TRANSIENT_TRINO_ERROR_RETRY_DELAYS_SECONDS,on_processed_bytes: Callable[[int], None] | None = None,) -> _Result:- """Run ``execute`` again after a cluster out-of-memory kill. All attempts share one deadline.+ """Run ``execute`` again after a transient Trino cluster fault. All attempts share one deadline.``on_processed_bytes`` receives the bytes each statement processed, in every attempt."""@@ -116,8 +118,12 @@ async def run_trino_model_with_retries(execute, query_seconds=deadline - monotonic(), on_processed_bytes=on_processed_bytes)except Exception as error:- if not is_cluster_out_of_memory(error) or monotonic() + delay >= deadline:+ if not is_transient_trino_error(error) or monotonic() + delay >= deadline:raise- logger.warning("Trino cluster is out of memory, retrying the model", retry_in_seconds=delay)+ logger.warning(+ "Trino cluster failed the query, retrying the model",+ error_name=getattr(error, "error_name", None),+ retry_in_seconds=delay,+ )await asyncio.sleep(delay)return await run_trino_model(execute, query_seconds=deadline - monotonic(), on_processed_bytes=on_processed_bytes)