: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(clickhouse): keep LOGS workload pinned in the async query worker (#70409)
Julian Bez892b23e905f8
diff --git a/posthog/clickhouse/client/execute.py b/posthog/clickhouse/client/execute.pyindex 8727fb6b3954..1bb2b5e7bd8c 100644--- a/posthog/clickhouse/client/execute.py+++ b/posthog/clickhouse/client/execute.py@@ -337,13 +337,17 @@ def sync_execute(if workload == Workload.DEFAULT and (is_api_key_auth or tags.kind == "celery"):workload = Workload.OFFLINE- # Make sure we always have process_query_task on the online cluster+ # Make sure we always have process_query_task on the online cluster.+ # Workload.LOGS is exempt: it pins queries to the dedicated logs cluster, which is the+ # only place the logs tables exist, so overriding it would send the query to a cluster+ # that cannot answer it.tags_id: str = tags.id or ""if tags_id == "posthog.tasks.tasks.process_query_task":- workload = Workload.ONLINE+ if workload != Workload.LOGS:+ workload = Workload.ONLINEch_user = ClickHouseUser.API if is_api_key_auth else ClickHouseUser.APP- if tags.workload == Workload.ENDPOINTS:+ if tags.workload == Workload.ENDPOINTS and workload != Workload.LOGS:workload = Workload.ENDPOINTSif workload == Workload.DEFAULT:diff --git a/posthog/clickhouse/client/test/test_execute.py b/posthog/clickhouse/client/test/test_execute.pynew file mode 100644index 000000000000..f203de52525b--- /dev/null+++ b/posthog/clickhouse/client/test/test_execute.py@@ -0,0 +1,50 @@+import pytest+from unittest.mock import MagicMock, patch++from posthog.clickhouse.client.connection import ClickHouseUser, Workload+from posthog.clickhouse.client.execute import sync_execute+from posthog.clickhouse.query_tagging import tags_context+++def client_from_pool():+ with patch("posthog.clickhouse.client.execute.get_client_from_pool") as mock:+ client = MagicMock()+ client.execute.return_value = [(1,)]+ mock.return_value.__enter__.return_value = client+ yield mock+++ "workload,expected_workload",+ [+ (Workload.DEFAULT, Workload.ONLINE),+ (Workload.OFFLINE, Workload.ONLINE),+ (Workload.LOGS, Workload.LOGS),+ ],+)+def test_process_query_task_workload_routing(client_from_pool, workload, expected_workload):+ # The async query worker forces queries onto the online cluster, but must not override+ # cluster-pinned workloads: LOGS-workload tables only exist on the logs cluster.+ with tags_context(kind="celery", id="posthog.tasks.tasks.process_query_task"):+ sync_execute("SELECT 1", workload=workload, flush=False)++ called_workload, _, _, called_ch_user = client_from_pool.call_args[0]+ assert called_workload == expected_workload+ assert called_ch_user == ClickHouseUser.APP+++ "workload,expected_workload",+ [+ (Workload.DEFAULT, Workload.ENDPOINTS),+ (Workload.LOGS, Workload.LOGS),+ ],+)+def test_endpoints_tag_workload_routing(client_from_pool, workload, expected_workload):+ # The ENDPOINTS tag reroutes queries to the endpoints cluster, but must not override+ # the LOGS cluster pin either.+ with tags_context(kind="request", id="api/endpoint", workload=Workload.ENDPOINTS):+ sync_execute("SELECT 1", workload=workload, flush=False)++ assert client_from_pool.call_args[0][0] == expected_workload