: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(devex): stop local ClickHouse Kafka consumers spinning while idle (#96233)
Julian Bez275ad37dc453
diff --git a/docker-compose.dev.yml b/docker-compose.dev.ymlindex 13edcfea83ad..e932d902516f 100644--- a/docker-compose.dev.yml+++ b/docker-compose.dev.yml@@ -585,41 +585,23 @@ services:exit 1fidone- # The combined analytics ingestion consumer checks topic existence at- # startup for every output it produces to (see analytics/outputs/registry.ts).- # Auto-create only fires on producer-first writes, so pre-create them here.- TOPICS="document_embeddings_input \- document_embedding_results \- error_tracking_ingestion_notifications \- clickhouse_events_json \- clickhouse_ai_events_json \- clickhouse_heatmap_events \- clickhouse_flag_evaluations \- clickhouse_ingestion_warnings \- events_plugin_ingestion_ai \- events_plugin_ingestion_dlq \- events_plugin_ingestion_overflow \- events_plugin_ingestion_async \- ingestion-clientwarnings-main-1 \- heatmaps_ingestion \- clickhouse_groups \- clickhouse_person \- clickhouse_person_distinct_id \- clickhouse_app_metrics2 \- log_entries \- clickhouse_property_values \- clickhouse_tophog \- clickhouse_billing_usage_records"+ # The topic list is docker/kafka/topics.txt, mounted below. It is a+ # shared file rather than a literal here so a test can check it against the+ # Kafka tables in schema.py; that header explains what needs pre-creating.+ TOPICS=$$(grep -vE '^[[:space:]]*#|^[[:space:]]*$$' /topics.txt)+ EXISTING=$$(rpk topic list --brokers kafka:9092 | awk '{print $$1}')for topic in $$TOPICS; doif rpk topic create "$$topic" --brokers kafka:9092 -p 1 -r 1 2>&1; thenecho "Topic $$topic created"- elif rpk topic list --brokers kafka:9092 | grep -q "$$topic"; then+ elif echo "$$EXISTING" | grep -qx "$$topic"; thenecho "Topic $$topic already exists"elseecho "Failed to create topic $$topic"exit 1fidone+ volumes:+ - ./docker/kafka/topics.txt:/topics.txt:rodepends_on:kafka:condition: service_healthydiff --git a/docker-compose.hobby.yml b/docker-compose.hobby.ymlindex 453a6ea0c98d..6f635b7d40a2 100644--- a/docker-compose.hobby.yml+++ b/docker-compose.hobby.yml@@ -611,33 +611,16 @@ services:fidoneecho "Kafka broker is accepting requests, creating topics..."- # Create topics needed by rust services and the combined analytics ingestion- # consumer, which checks topic existence at startup for every output it produces- # to (see analytics/outputs/registry.ts). Auto-create only fires on producer-first- # writes, so pre-create them here.+ # The topic list is docker/kafka/topics.txt, mounted below, shared with+ # docker-compose.dev.yml so the two stacks cannot drift apart. That header+ # explains what needs pre-creating.# Note: $$ escapes $ for docker-compose variable substitution- TOPICS="clickhouse_events_json \- clickhouse_ai_events_json \- clickhouse_heatmap_events \- clickhouse_flag_evaluations \- clickhouse_ingestion_warnings \- events_plugin_ingestion_ai \- events_plugin_ingestion_dlq \- events_plugin_ingestion_overflow \- events_plugin_ingestion_async \- ingestion-clientwarnings-main-1 \- heatmaps_ingestion \- clickhouse_groups \- clickhouse_person \- clickhouse_person_distinct_id \- clickhouse_app_metrics2 \- log_entries \- clickhouse_tophog"+ TOPICS=$$(grep -vE '^[[:space:]]*#|^[[:space:]]*$$' /topics.txt)for topic in $$TOPICS; doif rpk topic create "$$topic" --brokers kafka:9092 -p 1 -r 1 2>&1; thenecho "Topic $$topic created successfully"else- if rpk topic list --brokers kafka:9092 | grep -q "$$topic"; then+ if rpk topic list --brokers kafka:9092 | awk '{print $$1}' | grep -qx "$$topic"; thenecho "Topic $$topic already exists, continuing"elseecho "Failed to create topic $$topic"@@ -648,6 +631,8 @@ services:echo "Final topic list:"rpk topic list --brokers kafka:9092echo "Topics ready"+ volumes:+ - ./posthog/docker/kafka/topics.txt:/topics.txt:rodepends_on:kafka:condition: service_healthydiff --git a/docker/clickhouse/config.xml b/docker/clickhouse/config.xmlindex 156ecce2332e..ebbc370af5a4 100644--- a/docker/clickhouse/config.xml+++ b/docker/clickhouse/config.xml@@ -10,8 +10,11 @@<level>warning</level><log>/var/log/clickhouse-server/clickhouse-server.log</log><errorlog>/var/log/clickhouse-server/clickhouse-server.err.log</errorlog>- <size>1000M</size>- <count>10</count>+ <!-- Only the local stacks mount this file, where 10 GB per stream is far past+ anything a developer reads. A noisy consumer loop can produce hundreds of MB+ per hour, so keep the total bounded. -->+ <size>100M</size>+ <count>3</count></logger><http_port>8123</http_port>diff --git a/docker/kafka/topics.txt b/docker/kafka/topics.txtnew file mode 100644index 000000000000..cec348ea44c8--- /dev/null+++ b/docker/kafka/topics.txt@@ -0,0 +1,56 @@+# Kafka topics the local stacks pre-create (docker-compose.dev.yml and docker-compose.hobby.yml).+#+# Redpanda runs with auto_create_topics_enabled, but that only fires on a producer-first+# write. Two kinds of consumer start before any producer and need the topic to exist:+#+# * Consumer-only services error at startup on a missing topic.+# * A ClickHouse Kafka table whose topic is absent never gets a partition assignment. It+# holds a thread and repeats the request for as long as the server runs, which writes a+# "Can't get assignment" warning every time and floods clickhouse-server.log.+#+# posthog/clickhouse/test/test_schema.py checks that every topic a Kafka table in schema.py+# consumes appears below. Topics that only a migration creates have no such guard, so add+# them here by hand.+clickhouse_ai_events_json+clickhouse_app_metrics+clickhouse_app_metrics2+clickhouse_billing_usage_records+clickhouse_document_embeddings+clickhouse_error_tracking_fingerprint_issue_state+clickhouse_error_tracking_issue_fingerprint+clickhouse_events_json+clickhouse_flag_evaluations+clickhouse_groups+clickhouse_heatmap_events+clickhouse_hog_invocation_results+clickhouse_ingestion_events_duplicates+clickhouse_ingestion_warnings+clickhouse_logs+clickhouse_message_assets+clickhouse_metrics+clickhouse_performance_events+clickhouse_person+clickhouse_person_distinct_id+clickhouse_person_override+clickhouse_person_unique_id+clickhouse_precalculated_person_properties+clickhouse_prefiltered_events+clickhouse_property_values+clickhouse_session_replay_events+clickhouse_session_replay_features+clickhouse_tophog+clickhouse_traces+cohort_membership_changed+distinct_id_usage_events_json+document_embedding_results+document_embeddings_input+error_tracking_ingestion_notifications+events_dead_letter_queue+events_plugin_ingestion_ai+events_plugin_ingestion_async+events_plugin_ingestion_dlq+events_plugin_ingestion_overflow+heatmaps_ingestion+ingestion-clientwarnings-main-1+log_entries+plugin_log_entriesdiff --git a/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl b/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hclindex 221e9d5953df..3bd51d83ea2d 100644--- a/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl@@ -695,7 +695,7 @@ database "posthog" {topic_list = "clickhouse_property_values"group_name = "clickhouse_property_values"format = "JSONEachRow"- num_consumers = 8+ num_consumers = 1thread_per_consumer = true}}diff --git a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hclindex 09ace9abf004..4cbeb7b6afc5 100644--- a/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/ingestion_events.hcl@@ -184,7 +184,7 @@ database "posthog" {topic_list = "clickhouse_logs"group_name = "clickhouse-logs-avro-new"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hclindex a865844433cb..ebb5c102eb2d 100644--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl@@ -65,7 +65,7 @@ database "posthog" {topic_list = "clickhouse_metrics"group_name = "clickhouse-metrics-avro-new"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000@@ -148,7 +148,7 @@ database "posthog" {topic_list = "clickhouse_metrics"group_name = "clickhouse-metrics-avro2"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000@@ -225,7 +225,7 @@ database "posthog" {topic_list = "clickhouse_traces"group_name = "clickhouse-traces-avro"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hclindex 5347a436db1b..6ad379d3cae1 100644--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl@@ -3533,7 +3533,7 @@ database "posthog" {topic_list = "clickhouse_logs"group_name = "clickhouse-logs-avro-new"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000@@ -3665,7 +3665,7 @@ database "posthog" {topic_list = "clickhouse_metrics"group_name = "clickhouse-metrics-avro-new"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000@@ -3748,7 +3748,7 @@ database "posthog" {topic_list = "clickhouse_metrics"group_name = "clickhouse-metrics-avro2"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000@@ -4206,7 +4206,7 @@ database "posthog" {topic_list = "clickhouse_property_values"group_name = "clickhouse_property_values"format = "JSONEachRow"- num_consumers = 8+ num_consumers = 1thread_per_consumer = true}}@@ -4663,7 +4663,7 @@ database "posthog" {topic_list = "clickhouse_traces"group_name = "clickhouse-traces-avro"format = "Avro"- num_consumers = 8+ num_consumers = 1skip_broken_messages = 100poll_timeout_ms = 3000poll_max_batch_size = 1000diff --git a/posthog/clickhouse/hcl/roles/auxiliary/local/tables.hcl b/posthog/clickhouse/hcl/roles/auxiliary/local/tables.hclindex 2e5944367cdb..7125ea6b82bf 100644--- a/posthog/clickhouse/hcl/roles/auxiliary/local/tables.hcl+++ b/posthog/clickhouse/hcl/roles/auxiliary/local/tables.hcl@@ -93,4 +93,18 @@ database "posthog" {type = "UInt64"}}++ # Local stacks create every topic with one partition, so only one consumer of this group can+ # get an assignment. The rest retry forever, which holds threads and floods the server log.+ patch_table "kafka_property_values" {+ engine "kafka" {+ collection = "warpstream_ingestion"+ topic_list = "clickhouse_property_values"+ group_name = "clickhouse_property_values"+ format = "JSONEachRow"+ num_consumers = 1+ thread_per_consumer = true+ }+ }+}diff --git a/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hcl b/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hclindex 0f6435ee5d87..687750ba4b99 100644--- a/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hcl+++ b/posthog/clickhouse/hcl/roles/ingestion_events/local/tables.hcl@@ -249,4 +249,26 @@ SQL}}++ # Local stacks create every topic with one partition, so only one consumer of this group can+ # get an assignment. The rest retry forever, which holds threads and floods the server log.+ # The table itself is declared in roles/coshared/logs_avro_ingest, which dev and the local+ # stacks share, so the count is lowered here instead of there.+ patch_table "kafka_logs_avro" {+ engine "kafka" {+ collection = "warpstream_logs"+ topic_list = "clickhouse_logs"+ group_name = "clickhouse-logs-avro-new"+ format = "Avro"+ num_consumers = 1+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ settings = {+ input_format_avro_allow_missing_fields = "1"+ }+ }+}diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hclindex 2734786dffd8..08936f451cfd 100644--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl@@ -621,4 +621,49 @@ FROM posthog.kafka_trace_spans_avroSQL}++ # Local stacks create every topic with one partition, so only one consumer of this group can+ # get an assignment. The rest retry forever, which holds threads and floods the server log.+ patch_table "kafka_metrics_avro" {+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro-new"+ format = "Avro"+ num_consumers = 1+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }++ patch_table "kafka_metrics_avro2" {+ engine "kafka" {+ collection = "warpstream_metrics"+ topic_list = "clickhouse_metrics"+ group_name = "clickhouse-metrics-avro2"+ format = "Avro"+ num_consumers = 1+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }++ patch_table "kafka_trace_spans_avro" {+ engine "kafka" {+ collection = "warpstream_traces"+ topic_list = "clickhouse_traces"+ group_name = "clickhouse-traces-avro"+ format = "Avro"+ num_consumers = 1+ skip_broken_messages = 100+ poll_timeout_ms = 3000+ poll_max_batch_size = 1000+ thread_per_consumer = true+ }+ }+}diff --git a/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql b/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sqlindex 7450d6bf5c30..875b258e5a3b 100644--- a/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql@@ -133,7 +133,7 @@ CREATE TABLE posthog.kafka_property_values (property_key String,property_value String,property_count UInt64-) ENGINE = Kafka(warpstream_ingestion) SETTINGS kafka_format = 'JSONEachRow', kafka_group_name = 'clickhouse_property_values', kafka_num_consumers = 8, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_property_values';+) ENGINE = Kafka(warpstream_ingestion) SETTINGS kafka_format = 'JSONEachRow', kafka_group_name = 'clickhouse_property_values', kafka_num_consumers = 1, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_property_values';CREATE TABLE posthog.message_assets_data (team_id Int64,function_kind LowCardinality(String),diff --git a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sqlindex 53fb017ec4f2..6830c1c75747 100644--- a/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/ingestion_events.sql@@ -54,7 +54,7 @@ CREATE TABLE posthog.kafka_logs_avro (retention_days Nullable(Int32),pattern Nullable(String),pattern_version Nullable(Int32)-) ENGINE = Kafka(warpstream_logs) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-logs-avro-new', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_logs';+) ENGINE = Kafka(warpstream_logs) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-logs-avro-new', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_logs';CREATE TABLE posthog.query_log_archive (hostname LowCardinality(String),user LowCardinality(String),diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sqlindex 9b1986e3eef5..76caa19076bf 100644--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql@@ -22,7 +22,7 @@ CREATE TABLE posthog.kafka_metrics_avro (instrumentation_scope Nullable(String),attributes Map(String, String),series_fingerprint Nullable(Int64)-) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro-new', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';+) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro-new', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_metrics_avro2 (uuid String,trace_id String,@@ -46,7 +46,7 @@ CREATE TABLE posthog.kafka_metrics_avro2 (series_fingerprint Nullable(Int64),has_labels Nullable(UInt8),retention_days Nullable(Int32)-) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';+) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_trace_spans_avro (uuid String,trace_id String,@@ -69,7 +69,7 @@ CREATE TABLE posthog.kafka_trace_spans_avro (links Array(String),dropped_links_count Int32,status_code Int32-) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';+) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';CREATE TABLE posthog.log_attributes (team_id Int32 CODEC(DoubleDelta, ZSTD(1)),time_bucket DateTime64(0) CODEC(DoubleDelta, ZSTD(1)),diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sqlindex 73e7a8579fc8..4764f39509bf 100644--- a/posthog/clickhouse/hcl/sql/local-single/all.sql+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql@@ -575,7 +575,7 @@ CREATE TABLE posthog.kafka_logs_avro (retention_days Nullable(Int32),pattern Nullable(String),pattern_version Nullable(Int32)-) ENGINE = Kafka(warpstream_logs) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-logs-avro-new', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_logs';+) ENGINE = Kafka(warpstream_logs) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-logs-avro-new', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_logs';CREATE TABLE posthog.kafka_message_assets (team_id Int64,function_kind LowCardinality(String),@@ -615,7 +615,7 @@ CREATE TABLE posthog.kafka_metrics_avro (instrumentation_scope Nullable(String),attributes Map(String, String),series_fingerprint Nullable(Int64)-) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro-new', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';+) ENGINE = Kafka(warpstream_metrics) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro-new', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_metrics_avro2 (uuid String,trace_id String,@@ -639,7 +639,7 @@ CREATE TABLE posthog.kafka_metrics_avro2 (series_fingerprint Nullable(Int64),has_labels Nullable(UInt8),retention_days Nullable(Int32)-) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';+) ENGINE = Kafka(warpstream_metrics) SETTINGS input_format_avro_allow_missing_fields = 1, kafka_format = 'Avro', kafka_group_name = 'clickhouse-metrics-avro2', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_metrics';CREATE TABLE posthog.kafka_performance_events (uuid UUID,session_id String,@@ -775,7 +775,7 @@ CREATE TABLE posthog.kafka_property_values (property_key String,property_value String,property_count UInt64-) ENGINE = Kafka(warpstream_ingestion) SETTINGS kafka_format = 'JSONEachRow', kafka_group_name = 'clickhouse_property_values', kafka_num_consumers = 8, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_property_values';+) ENGINE = Kafka(warpstream_ingestion) SETTINGS kafka_format = 'JSONEachRow', kafka_group_name = 'clickhouse_property_values', kafka_num_consumers = 1, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_property_values';CREATE TABLE posthog.kafka_session_replay_events (session_id String,team_id Int64,@@ -919,7 +919,7 @@ CREATE TABLE posthog.kafka_trace_spans_avro (links Array(String),dropped_links_count Int32,status_code Int32-) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 8, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';+) ENGINE = Kafka(warpstream_traces) SETTINGS kafka_format = 'Avro', kafka_group_name = 'clickhouse-traces-avro', kafka_num_consumers = 1, kafka_poll_max_batch_size = 1000, kafka_poll_timeout_ms = 3000, kafka_skip_broken_messages = 100, kafka_thread_per_consumer = 1, kafka_topic_list = 'clickhouse_traces';CREATE TABLE posthog.kafka_usage_report_events_preagg (uuid UUID,event String,diff --git a/posthog/clickhouse/kafka_engine.py b/posthog/clickhouse/kafka_engine.pyindex 9687c0d5ac54..6c8fd27f5419 100644--- a/posthog/clickhouse/kafka_engine.py+++ b/posthog/clickhouse/kafka_engine.py@@ -3,6 +3,8 @@ from typing import Literalfrom django.conf import settings+from posthog.run_mode import run_mode+# Consumer group names for Kafka tables.# US deployment uses named groups after the cluster reshard, other deployments use legacy group names.# Once we make all envs match, we can remove the _US check@@ -98,6 +100,16 @@ KAFKA_COLUMNS_WITH_PARTITION = """KAFKA_TIMESTAMP_MS_COLUMN = "_timestamp_ms DateTime64"+# Every stack outside deployed cloud runs a single-node Kafka whose topics have one partition,+# so a consumer group can place only one consumer. The other consumers never receive an+# assignment, and each one holds a thread and repeats the request, which writes a+# "Can't get assignment" warning every time. Cloud topics have many partitions, so they keep the+# tuned count. Resolve the mode per call, because a module-level constant would freeze the value+# at import and defeat a test that patches the mode.+def kafka_num_consumers(cloud_count: int) -> int:+ return cloud_count if run_mode().is_deployed_cloud else 1++def kafka_engine(topic: str,kafka_host: str | None = None,diff --git a/posthog/clickhouse/logs/logs34.py b/posthog/clickhouse/logs/logs34.pyindex 9851e3d43c6f..048836fcd689 100644--- a/posthog/clickhouse/logs/logs34.py+++ b/posthog/clickhouse/logs/logs34.py@@ -1,6 +1,6 @@from django.conf import settings-from posthog.clickhouse.kafka_engine import kafka_engine+from posthog.clickhouse.kafka_engine import kafka_engine, kafka_num_consumersfrom posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationSchemefrom .log_attributes3 import TABLE_NAME as LOG_ATTRIBUTES3_TABLE_NAME@@ -286,7 +286,7 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{KAFKA_TAENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="Avro", named_collection=KAFKA_NAMED_COLLECTION)}SETTINGSkafka_skip_broken_messages = 100,- kafka_num_consumers = 8,+ kafka_num_consumers = {kafka_num_consumers(8)},kafka_poll_timeout_ms = 3000,kafka_poll_max_batch_size = 1000,kafka_thread_per_consumer = 1,diff --git a/posthog/clickhouse/metrics/kafka_metrics.py b/posthog/clickhouse/metrics/kafka_metrics.pyindex c2f72682396a..729a59d13110 100644--- a/posthog/clickhouse/metrics/kafka_metrics.py+++ b/posthog/clickhouse/metrics/kafka_metrics.py@@ -11,7 +11,7 @@ HCL definitions — the multinode convergence gate diffs the live schema againstfrom django.conf import settings-from posthog.clickhouse.kafka_engine import kafka_engine+from posthog.clickhouse.kafka_engine import kafka_engine, kafka_num_consumersfrom posthog.clickhouse.table_engines import AggregatingMergeTree, ReplicationSchemefrom .metric_events import SAMPLES_TABLE_NAME, SERIES_TABLE_NAME@@ -53,7 +53,7 @@ ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="AvroSETTINGSkafka_skip_broken_messages = 100,kafka_thread_per_consumer = 1,- kafka_num_consumers = 8,+ kafka_num_consumers = {kafka_num_consumers(8)},kafka_poll_timeout_ms = 3000,kafka_poll_max_batch_size = 1000"""diff --git a/posthog/clickhouse/metrics/metrics2.py b/posthog/clickhouse/metrics/metrics2.pyindex 433cebad6521..6bef000f7ec0 100644--- a/posthog/clickhouse/metrics/metrics2.py+++ b/posthog/clickhouse/metrics/metrics2.py@@ -1,6 +1,6 @@from django.conf import settings-from posthog.clickhouse.kafka_engine import kafka_engine+from posthog.clickhouse.kafka_engine import kafka_engine, kafka_num_consumersfrom posthog.clickhouse.table_engines import (AggregatingMergeTree,Distributed,@@ -60,7 +60,7 @@ ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="AvroSETTINGSkafka_skip_broken_messages = 100,kafka_thread_per_consumer = 1,- kafka_num_consumers = 8,+ kafka_num_consumers = {kafka_num_consumers(8)},kafka_poll_timeout_ms = 3000,kafka_poll_max_batch_size = 1000,input_format_avro_allow_missing_fields = 1diff --git a/posthog/clickhouse/property_values.py b/posthog/clickhouse/property_values.pyindex 8bfc72f45ed7..ecb5fdcecefd 100644--- a/posthog/clickhouse/property_values.py+++ b/posthog/clickhouse/property_values.py@@ -1,6 +1,6 @@from django.conf import settings-from posthog.clickhouse.kafka_engine import CONSUMER_GROUP_PROPERTY_VALUES, kafka_engine+from posthog.clickhouse.kafka_engine import CONSUMER_GROUP_PROPERTY_VALUES, kafka_engine, kafka_num_consumersfrom posthog.clickhouse.table_engines import AggregatingMergeTree, Distributed, ReplicationSchemefrom posthog.kafka_client.topics import KAFKA_CLICKHOUSE_PROPERTY_VALUES@@ -21,7 +21,7 @@ CREATE TABLE IF NOT EXISTS {table_name}`property_value` String,`property_count` UInt64) ENGINE = {engine}-SETTINGS kafka_num_consumers = 8, kafka_thread_per_consumer = 1+SETTINGS kafka_num_consumers = {num_consumers}, kafka_thread_per_consumer = 1"""PROPERTY_VALUES_TABLE_BASE_SQL = """@@ -58,6 +58,7 @@ SETTINGSdef KAFKA_PROPERTY_VALUES_TABLE_SQL_FN() -> str:return KAFKA_PROPERTY_VALUES_TABLE_SQL.format(table_name=KAFKA_TABLE_NAME,+ num_consumers=kafka_num_consumers(8),engine=kafka_engine(topic=KAFKA_CLICKHOUSE_PROPERTY_VALUES,group=CONSUMER_GROUP_PROPERTY_VALUES,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex afd7f9a44610..9be4af3c5f77 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -731,7 +731,7 @@`property_value` String,`property_count` UInt64) ENGINE = Kafka(warpstream_ingestion, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')- SETTINGS kafka_num_consumers = 8, kafka_thread_per_consumer = 1+ SETTINGS kafka_num_consumers = 1, kafka_thread_per_consumer = 1'''# ---@@ -3035,7 +3035,7 @@`property_value` String,`property_count` UInt64) ENGINE = Kafka(warpstream_ingestion, kafka_topic_list = 'clickhouse_property_values_test', kafka_group_name = 'clickhouse_property_values', kafka_format = 'JSONEachRow')- SETTINGS kafka_num_consumers = 8, kafka_thread_per_consumer = 1+ SETTINGS kafka_num_consumers = 1, kafka_thread_per_consumer = 1'''# ---diff --git a/posthog/clickhouse/test/test_schema.py b/posthog/clickhouse/test/test_schema.pyindex 67ad72906160..afcfaf43e033 100644--- a/posthog/clickhouse/test/test_schema.py+++ b/posthog/clickhouse/test/test_schema.py@@ -1,6 +1,7 @@import reimport uuidfrom collections.abc import Iterator+from pathlib import Pathimport pytest@@ -24,6 +25,8 @@ from posthog.models.flag_evaluations.sql import (FLAG_EVALUATIONS_MV_SQL,FLAG_EVALUATIONS_TABLE_SQL,)+from posthog.settings.data_stores import SUFFIX+from posthog.settings.kafka import KAFKA_PREFIX@pytest.mark.parametrize("query", CREATE_TABLE_QUERIES, ids=get_table_name)@@ -128,3 +131,31 @@ def mock_uuid4(mocker):mock_uuid4 = mocker.patch("uuid.uuid4")mock_uuid4.return_value = uuid.UUID("77f1df52-4b43-11e9-910f-b8ca3a9b9f3e")yield mock_uuid4+++def _kafka_topics_in_schema() -> set[str]:+ # Topic names are built as KAFKA_PREFIX + name + SUFFIX, and the test settings set a+ # suffix the dev stack does not use. Compare the bare names the bootstrap file holds.+ topics: set[str] = set()+ for query in CREATE_KAFKA_TABLE_QUERIES:+ sql = build_query(query)+ topics.update(re.findall(r"kafka_topic_list\s*=\s*'([^']+)'", sql))+ topics.update(re.findall(r"Kafka\('[^']*',\s*'([^']+)'", sql))+ return {t.removeprefix(KAFKA_PREFIX).removesuffix(SUFFIX) for t in topics}+++def test_dev_stack_pre_creates_every_kafka_table_topic():+ bootstrap = Path(__file__).parents[3] / "docker" / "kafka" / "topics.txt"+ listed = {+ stripped+ for line in bootstrap.read_text().splitlines()+ if (stripped := line.strip()) and not stripped.startswith("#")+ }++ missing = sorted(_kafka_topics_in_schema() - listed)++ assert not missing, (+ f"{bootstrap.name} does not list {missing}. A ClickHouse Kafka table whose topic is "+ "absent never gets a partition assignment, so it holds a thread and repeats the "+ "request for as long as a local stack runs. Add each topic to that file."+ )diff --git a/posthog/clickhouse/traces/spans.py b/posthog/clickhouse/traces/spans.pyindex 874ddaa5871f..dd6a123abfbd 100644--- a/posthog/clickhouse/traces/spans.py+++ b/posthog/clickhouse/traces/spans.py@@ -1,6 +1,6 @@from django.conf import settings-from posthog.clickhouse.kafka_engine import kafka_engine+from posthog.clickhouse.kafka_engine import kafka_engine, kafka_num_consumersfrom posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplicationSchemefrom .trace_attributes import (@@ -315,7 +315,7 @@ ENGINE = {kafka_engine(topic=KAFKA_TOPIC, group=KAFKA_GROUP, serialization="AvroSETTINGSkafka_skip_broken_messages = 100,kafka_thread_per_consumer = 1,- kafka_num_consumers = 8,+ kafka_num_consumers = {kafka_num_consumers(8)},kafka_poll_timeout_ms = 3000,kafka_poll_max_batch_size = 1000"""