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(ai-research): release image-fetch retry batches in concurrent groups (#116929)

Robbie0ab3a8969819
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md
index d3ede94ca8d3..c415d7f720cb 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md
@@ -631,6 +631,8 @@ ai_research_session_replay_image_fetch_retry_1h
**16.9** The delay consumer's `max.poll.interval.ms` must exceed the longest possible batch wait plus the time needed to publish the batch.
+**16.10** The consumer publishes a batch in release groups of at most 50 records. It starts all publishes in a group together and waits for the whole group before it starts the next group. After a group with a failed publish, it publishes no more records from the batch. One publish at a time limits a tier to one record for each produce round trip. The group size limits the number of records that one failure publishes twice.
+
## 17. Fetcher to scrubber message format
**17.1** The fetcher publishes one Kafka record for each successful image fetch.
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.test.ts
index 0acc4f6fe122..17bfa95b70ab 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.test.ts
@@ -29,7 +29,7 @@ interface Harness {
heartbeat: jest.Mock<void, []>
}
-function build(overrides: { isStopping?: () => boolean; delayMs?: number } = {}): Harness {
+function build(overrides: { isStopping?: () => boolean; delayMs?: number; releaseGroupSize?: number } = {}): Harness {
const produce = jest.fn(() => Promise.resolve())
const storeOffsets = jest.fn()
const heartbeat = jest.fn()
@@ -40,6 +40,7 @@ function build(overrides: { isStopping?: () => boolean; delayMs?: number } = {})
heartbeatIntervalMs: 10_000,
isStopping: overrides.isStopping,
storeOffsets,
+ releaseGroupSize: overrides.releaseGroupSize,
})
return { consumer, produce, storeOffsets, heartbeat }
}
@@ -51,8 +52,14 @@ describe('RetryDelayConsumer', () => {
jest.useRealTimers()
})
- it.each([0, -1, Number.NaN])('refuses an invalid delay of %p', (delayMs) => {
- expect(() => build({ delayMs })).toThrow('positive delay')
+ it.each([
+ [{ delayMs: 0 }, 'positive delay'],
+ [{ delayMs: -1 }, 'positive delay'],
+ [{ delayMs: Number.NaN }, 'positive delay'],
+ [{ releaseGroupSize: 0 }, 'positive whole release group size'],
+ [{ releaseGroupSize: 1.5 }, 'positive whole release group size'],
+ ])('refuses invalid options %p', (overrides, error) => {
+ expect(() => build(overrides)).toThrow(error)
})
it('does nothing for an empty batch', async () => {
@@ -130,8 +137,32 @@ describe('RetryDelayConsumer', () => {
expect(harness.storeOffsets).not.toHaveBeenCalled()
})
- it('stops publishing the batch after the first frontier failure', async () => {
- const harness = build()
+ it('publishes each release group concurrently and starts the next group after it settles', async () => {
+ const harness = build({ releaseGroupSize: 2 })
+ const acknowledgements: (() => void)[] = []
+ harness.produce.mockImplementation(() => new Promise<void>((resolve) => acknowledgements.push(resolve)))
+ const messages = [
+ message(NOW_MS - DELAY_MS, { offset: 1 }),
+ message(NOW_MS - DELAY_MS, { offset: 2 }),
+ message(NOW_MS - DELAY_MS, { offset: 3 }),
+ ]
+
+ const handled = harness.consumer.handleBatch(messages)
+ await jest.advanceTimersByTimeAsync(0)
+ expect(harness.produce).toHaveBeenCalledTimes(2)
+
+ acknowledgements.splice(0).forEach((acknowledge) => acknowledge())
+ await jest.advanceTimersByTimeAsync(0)
+ expect(harness.produce).toHaveBeenCalledTimes(3)
+ expect(harness.storeOffsets).not.toHaveBeenCalled()
+
+ acknowledgements.splice(0).forEach((acknowledge) => acknowledge())
+ await handled
+ expect(harness.storeOffsets).toHaveBeenCalledWith(messages)
+ })
+
+ it('stops publishing the batch after the release group with the first frontier failure', async () => {
+ const harness = build({ releaseGroupSize: 2 })
harness.produce.mockResolvedValueOnce().mockRejectedValueOnce(new Error('broker unavailable'))
const messages = [
message(NOW_MS - DELAY_MS, { offset: 1 }),
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.ts
index c08defd6408a..08463b7d4012 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/retry-delay-consumer.ts
@@ -18,9 +18,16 @@ export interface RetryDelayConsumerOptions {
/** True once the pod is shutting down. A wait of an hour would otherwise hold the rolling deploy until Kubernetes killed it. */
isStopping?: () => boolean
storeOffsets: (messages: Message[]) => void
+ releaseGroupSize?: number
}
const DEFAULT_HEARTBEAT_INTERVAL_MS = 10_000
+/**
+ * Each release waits for one broker acknowledgement, so releasing one record at a time limits a tier to one record per
+ * produce round trip. A failed group throws and the consumer reads the whole batch again, so the group size also limits
+ * how many records one failure publishes twice.
+ */
+const DEFAULT_RELEASE_GROUP_SIZE = 50
/**
* The consumer of one delay topic.
@@ -43,6 +50,12 @@ export class RetryDelayConsumer {
if (!Number.isFinite(options.delayMs) || options.delayMs <= 0) {
throw new Error(`the retry delay consumer needs a positive delay, got ${options.delayMs}`)
}
+ const releaseGroupSize = options.releaseGroupSize ?? DEFAULT_RELEASE_GROUP_SIZE
+ if (!Number.isInteger(releaseGroupSize) || releaseGroupSize <= 0) {
+ throw new Error(
+ `the retry delay consumer needs a positive whole release group size, got ${releaseGroupSize}`
+ )
+ }
}
public async handleBatch(messages: Message[]): Promise<void> {
@@ -69,8 +82,10 @@ export class RetryDelayConsumer {
return
}
}
- for (const message of messages) {
- await this.release(message)
+ const releaseGroupSize = this.options.releaseGroupSize ?? DEFAULT_RELEASE_GROUP_SIZE
+ for (let start = 0; start < messages.length; start += releaseGroupSize) {
+ const group = messages.slice(start, start + releaseGroupSize)
+ await Promise.all(group.map((message) => this.release(message)))
}
this.options.storeOffsets(messages)
}