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.

chore(ingestion): fix flaky clickhouse kafka warmup in ingestion e2e (#62694)

José Sequeirac542f6892a0e
diff --git a/nodejs/src/ingestion/ingestion-e2e.test.ts b/nodejs/src/ingestion/ingestion-e2e.test.ts
index 7a9cc5fa47f8..e59d49618785 100644
--- a/nodejs/src/ingestion/ingestion-e2e.test.ts
+++ b/nodejs/src/ingestion/ingestion-e2e.test.ts
@@ -1,7 +1,7 @@
import { DateTime } from 'luxon'
import { waitForExpect } from '~/tests/helpers/expectations'
-import { resetKafka } from '~/tests/helpers/kafka'
+import { TEST_KAFKA_TOPICS, ensureKafkaTopics } from '~/tests/helpers/kafka'
import { Clickhouse } from '../../tests/helpers/clickhouse'
import {
@@ -70,7 +70,7 @@ describe.each([{ PERSONS_PREFETCH_ENABLED: false }, { PERSONS_PREFETCH_ENABLED:
beforeAll(async () => {
console.log('Creating Clickhouse client')
clickhouse = Clickhouse.create()
- await resetKafka()
+ await ensureKafkaTopics(TEST_KAFKA_TOPICS)
await resetTestDatabase()
await clickhouse.resetTestDatabase()
await waitForClickHouseKafkaConsumer(clickhouse)
diff --git a/nodejs/tests/helpers/clickhouse.ts b/nodejs/tests/helpers/clickhouse.ts
index 8acdf30e5b8c..ee0fa8d6813c 100644
--- a/nodejs/tests/helpers/clickhouse.ts
+++ b/nodejs/tests/helpers/clickhouse.ts
@@ -22,6 +22,18 @@ import { fetch } from '~/utils/request'
import { logger } from '../../src/utils/logger'
import { delay, escapeClickHouseString } from '../../src/utils/utils'
+// Extracts the topic(s) a ClickHouse Kafka engine table consumes from its `engine_full` string.
+// Handles both the keyword form (`kafka_topic_list = 'a,b'`) and the positional form
+// (`Kafka('broker', 'topic', 'group', 'format')`, where the topic is the second quoted argument).
+function extractKafkaTopicList(engineFull: string): string[] {
+ const keyword = engineFull.match(/kafka_topic_list\s*=\s*'([^']+)'/)
+ const raw = keyword ? keyword[1] : ([...engineFull.matchAll(/'([^']*)'/g)].map((m) => m[1])[1] ?? '')
+ return raw
+ .split(',')
+ .map((topic) => topic.trim())
+ .filter(Boolean)
+}
+
export class Clickhouse {
private client: ClickHouseClient
@@ -89,6 +101,17 @@ export class Clickhouse {
await Promise.allSettled([this.truncate('sharded_ingestion_warnings'), this.truncate('sharded_app_metrics')])
}
+ // Topics that this database's Kafka engine tables consume from. Tests pre-create every
+ // subscribed topic so no consumer is left retrying "Can't get assignment" against a missing
+ // topic — those retries saturate ClickHouse's background scheduler and intermittently starve
+ // the consumers the tests depend on (e.g. the ingestion_warnings warmup probe).
+ async getKafkaEngineTopics(): Promise<string[]> {
+ const rows = await this.query<{ engine_full: string }>(
+ `SELECT engine_full FROM system.tables WHERE database = currentDatabase() AND engine = 'Kafka'`
+ )
+ return [...new Set(rows.flatMap((row) => extractKafkaTopicList(row.engine_full)))]
+ }
+
async waitForHealthy(delayMs = 100, maxDelayCount = 300): Promise<void> {
const timer = performance.now()
diff --git a/nodejs/tests/helpers/ingestion-e2e.ts b/nodejs/tests/helpers/ingestion-e2e.ts
index 92ed641b16f4..d2a82e9777cc 100644
--- a/nodejs/tests/helpers/ingestion-e2e.ts
+++ b/nodejs/tests/helpers/ingestion-e2e.ts
@@ -29,6 +29,7 @@ import { PersonRepository } from '../../src/worker/ingestion/persons/repositorie
import { PostgresPersonRepository } from '../../src/worker/ingestion/persons/repositories/postgres-person-repository'
import { Clickhouse } from './clickhouse'
import { waitForExpect } from './expectations'
+import { ensureKafkaTopics } from './kafka'
import { createUserTeamAndOrganization } from './sql'
export const DEFAULT_TEAM: Team = {
@@ -162,12 +163,18 @@ export const waitForKafkaMessages = async (kafkaProducer: KafkaProducerWrapper)
}
/**
- * After `resetKafka()` recreates topics, ClickHouse's Kafka engine consumers need to
- * reconnect. With the default auto.offset.reset=latest, any messages produced before
- * reconnection are permanently missed. We repeatedly produce probe messages until
- * ClickHouse consumes one, which guarantees at least one lands after reconnection.
+ * Waits until ClickHouse's Kafka engine is consuming, by producing probe messages until one
+ * lands in the ingestion_warnings table.
+ *
+ * Before probing we create every topic ClickHouse's Kafka engine tables subscribe to. Otherwise
+ * any table whose topic is missing keeps retrying "Can't get assignment", which saturates
+ * ClickHouse's background scheduler and intermittently starves the consumers we depend on — the
+ * root cause of this suite's flakiness. We repeatedly produce probe messages because, with the
+ * default auto.offset.reset=latest, messages produced before assignment are missed.
*/
export async function waitForClickHouseKafkaConsumer(clickhouse: Clickhouse): Promise<void> {
+ await ensureKafkaTopics(await clickhouse.getKafkaEngineTopics())
+
const producer = await KafkaProducerWrapper.create(undefined)
const probeTeamId = -1
diff --git a/nodejs/tests/helpers/kafka.ts b/nodejs/tests/helpers/kafka.ts
index 25a056921705..416dfb0e682a 100644
--- a/nodejs/tests/helpers/kafka.ts
+++ b/nodejs/tests/helpers/kafka.ts
@@ -65,39 +65,44 @@ async function createTopicsWithClient(client: ReturnType<typeof AdminClient.crea
}
}
+// Topics that the ClickHouse Kafka engine tables and the ingestion stack expect to
+// exist. Created once (idempotently via ensureKafkaTopics) rather than deleted and
+// recreated per test, so ClickHouse's Kafka consumers keep their partition assignments.
+export const TEST_KAFKA_TOPICS = [
+ KAFKA_CLICKHOUSE_AI_EVENTS_JSON,
+ KAFKA_EVENTS_JSON,
+ KAFKA_EVENTS_PLUGIN_INGESTION,
+ KAFKA_BUFFER,
+ KAFKA_GROUPS,
+ KAFKA_SESSION_RECORDING_SNAPSHOT_ITEM_EVENTS,
+ KAFKA_PERFORMANCE_EVENTS,
+ KAFKA_PERSON,
+ KAFKA_PERSON_UNIQUE_ID,
+ KAFKA_PERSON_DISTINCT_ID,
+ KAFKA_PERSON_DISTINCT_ID_OVERRIDES,
+ KAFKA_PLUGIN_LOG_ENTRIES,
+ KAFKA_EVENTS_DEAD_LETTER_QUEUE,
+ KAFKA_EVENTS_PLUGIN_INGESTION_DLQ,
+ KAFKA_EVENTS_PLUGIN_INGESTION_OVERFLOW,
+ KAFKA_EVENTS_PLUGIN_INGESTION_ASYNC,
+ KAFKA_INGESTION_WARNINGS,
+ KAFKA_CLICKHOUSE_HEATMAP_EVENTS,
+ KAFKA_APP_METRICS_2,
+ KAFKA_PERSON,
+ KAFKA_CLICKHOUSE_SESSION_RECORDING_EVENTS,
+ KAFKA_CLICKHOUSE_SESSION_REPLAY_EVENTS,
+ KAFKA_LOG_ENTRIES,
+ KAFKA_EVENTS_RECENT_JSON,
+ KAFKA_ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES,
+ KAFKA_CDP_CLICKHOUSE_BEHAVIORAL_COHORTS_MATCHES,
+ KAFKA_CDP_CLICKHOUSE_PREFILTERED_EVENTS,
+ KAFKA_COHORT_MEMBERSHIP_CHANGED,
+ KAFKA_CLICKHOUSE_TOPHOG,
+]
+
export async function resetKafka(extraServerConfig?: Partial<PluginsServerConfig>): Promise<void> {
const kafkaConfig = buildKafkaConfig(extraServerConfig)
- await createTopics(kafkaConfig, [
- KAFKA_CLICKHOUSE_AI_EVENTS_JSON,
- KAFKA_EVENTS_JSON,
- KAFKA_EVENTS_PLUGIN_INGESTION,
- KAFKA_BUFFER,
- KAFKA_GROUPS,
- KAFKA_SESSION_RECORDING_SNAPSHOT_ITEM_EVENTS,
- KAFKA_PERFORMANCE_EVENTS,
- KAFKA_PERSON,
- KAFKA_PERSON_UNIQUE_ID,
- KAFKA_PERSON_DISTINCT_ID,
- KAFKA_PERSON_DISTINCT_ID_OVERRIDES,
- KAFKA_PLUGIN_LOG_ENTRIES,
- KAFKA_EVENTS_DEAD_LETTER_QUEUE,
- KAFKA_EVENTS_PLUGIN_INGESTION_DLQ,
- KAFKA_EVENTS_PLUGIN_INGESTION_OVERFLOW,
- KAFKA_EVENTS_PLUGIN_INGESTION_ASYNC,
- KAFKA_INGESTION_WARNINGS,
- KAFKA_CLICKHOUSE_HEATMAP_EVENTS,
- KAFKA_APP_METRICS_2,
- KAFKA_PERSON,
- KAFKA_CLICKHOUSE_SESSION_RECORDING_EVENTS,
- KAFKA_CLICKHOUSE_SESSION_REPLAY_EVENTS,
- KAFKA_LOG_ENTRIES,
- KAFKA_EVENTS_RECENT_JSON,
- KAFKA_ERROR_TRACKING_ISSUE_FINGERPRINT_OVERRIDES,
- KAFKA_CDP_CLICKHOUSE_BEHAVIORAL_COHORTS_MATCHES,
- KAFKA_CDP_CLICKHOUSE_PREFILTERED_EVENTS,
- KAFKA_COHORT_MEMBERSHIP_CHANGED,
- KAFKA_CLICKHOUSE_TOPHOG,
- ])
+ await createTopics(kafkaConfig, TEST_KAFKA_TOPICS)
}
export async function createTopics(kafkaConfig: any, topics: string[]): Promise<void> {