: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(nodejs): answer queued rebalance events when the consumer stops (#90348)
José Sequeira22c2fbca8daf
diff --git a/nodejs/src/common/kafka/consumer/consumer-v2.rebalance.integration.serial.test.ts b/nodejs/src/common/kafka/consumer/consumer-v2.rebalance.integration.serial.test.tsindex 0acc262c8370..fa35a15a76f2 100644--- a/nodejs/src/common/kafka/consumer/consumer-v2.rebalance.integration.serial.test.ts+++ b/nodejs/src/common/kafka/consumer/consumer-v2.rebalance.integration.serial.test.ts@@ -1,4 +1,6 @@-// Serial: tight Kafka broker rebalance timing assertions are invalid under concurrent broker load.+// Serial: every test drives real consumer groups through session and rebalance timeouts, so the+// file must not share the broker with the parallel lane's load. Within the file the tests run+// concurrently: each owns its topic, group, and consumer budgets, and the cost is protocol waits.import {AdminClient,HighLevelProducer,@@ -9,7 +11,6 @@ import {} from 'node-rdkafka'import { randomUUID } from 'node:crypto'-import { defaultConfig } from '../../config/config'import { delay } from '../../utils/utils'import { KafkaConsumerV2 } from './consumer-v2'@@ -181,6 +182,11 @@ async function fetchCommittedOffset(groupId: string, topic: string, partition: n}}+// A test that hits Jest's timeout never reaches its `finally`, and a still-connected consumer+// keeps the worker process alive past --forceExit. Track every consumer so afterAll can close+// whatever a failed test left behind.+const liveConsumers = new Set<KafkaConsumerV2>()+function makeConsumer(groupId: string,topic: string,@@ -189,10 +195,19 @@ function makeConsumer(autoOffsetStore?: booleanonPartitionsRevoked?: (assignments: { topic: string; partition: number }[]) => Promise<void>rdKafkaOverrides?: Record<string, string | number | boolean>+ maxBackgroundTasks?: number+ rebalanceTimeoutMs?: number} = {}): KafkaConsumerV2 {const consumer = new KafkaConsumerV2(- { groupId, topic, batchTimeoutMs: 50, autoOffsetStore: opts.autoOffsetStore },+ {+ groupId,+ topic,+ batchTimeoutMs: 50,+ autoOffsetStore: opts.autoOffsetStore,+ maxBackgroundTasks: opts.maxBackgroundTasks,+ rebalanceTimeoutMs: opts.rebalanceTimeoutMs,+ },{...KAFKA_CONFIG,// Short but realistic session timeout keeps group transitions in these tests fast.@@ -211,6 +226,7 @@ function makeConsumer(void consumer.connect(eachBatch, opts.onPartitionsRevoked).catch((err: unknown) => {throw new Error(`Consumer failed to connect: ${String(err)}`)})+ liveConsumers.add(consumer)return consumer}@@ -231,6 +247,7 @@ describe('KafkaConsumerV2 rebalance semantics (integration)', () => {})afterAll(async () => {+ await Promise.allSettled([...liveConsumers].map((consumer) => consumer.disconnect()))await new Promise<void>((resolve) => producer.disconnect(() => resolve()))})@@ -238,258 +255,152 @@ describe('KafkaConsumerV2 rebalance semantics (integration)', () => {// commits stored offsets on the auto-commit timer and on unassign. No test code in this// group stores offsets.describe('autoOffsetStore on: offsets stored automatically as batches settle', () => {- it('settled work commits exactly; cooperative rebalance moves one partition with zero redelivery', async () => {- const topic = `v2_int_reb_exact_${randomUUID()}`- const groupId = `v2-int-reb-exact-${randomUUID()}`- await createTopic(topic, 2)-- const ledger: LedgerEntry[] = []- const record = (consumerId: string) => (messages: Message[]) => {- for (const m of messages) {- ledger.push({- consumerId,- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })+ it.concurrent(+ 'settled work commits exactly; cooperative rebalance moves one partition with zero redelivery',+ async () => {+ const topic = `v2_int_reb_exact_${randomUUID()}`+ const groupId = `v2-int-reb-exact-${randomUUID()}`+ await createTopic(topic, 2)++ const ledger: LedgerEntry[] = []+ const record = (consumerId: string) => (messages: Message[]) => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ }+ return Promise.resolve()}- return Promise.resolve()- }- const consumerA = makeConsumer(groupId, topic, record('A'), { autoOffsetStore: true })- let consumerB: KafkaConsumerV2 | undefined+ const consumerA = makeConsumer(groupId, topic, record('A'), { autoOffsetStore: true })+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)++ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 5 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 5 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ // Fresh topic: the broker must have assigned offsets 0..4 on each partition.+ for (const partition of [0, 1]) {+ expect(+ wave1+ .filter((r) => r.partition === partition)+ .map((r) => r.offset)+ .sort((x, y) => x - y)+ ).toEqual([0, 1, 2, 3, 4])+ }+ await waitFor(() => ledger.length >= wave1.length, 8_000)++ // With every batch settled, the stored offsets are committed at exactly+ // highest-delivered + 1 per partition.+ const expectedCommit = new Map<number, number>()+ for (const partition of [0, 1]) {+ const highest = Math.max(...wave1.filter((r) => r.partition === partition).map((r) => r.offset))+ expectedCommit.set(partition, highest + 1)+ }+ for (const [partition, offset] of expectedCommit) {+ await waitForAsync(+ async () => (await fetchCommittedOffset(groupId, topic, partition)) === offset,+ 10_000+ )+ }++ // B joins: cooperative-sticky moves exactly one partition, and A never drops to+ // zero assignments while it happens (the incremental-rebalance property).+ consumerB = makeConsumer(groupId, topic, record('B'), { autoOffsetStore: true })+ const b = consumerB+ let minAssignedA = 2+ await waitFor(() => {+ minAssignedA = Math.min(minAssignedA, consumerA.assignments().length)+ return consumerA.assignments().length === 1 && b.assignments().length === 1+ }, 15_000)+ expect(minAssignedA).toBeGreaterThanOrEqual(1)++ // The new owner resumes from the exact committed offsets: a second wave lands+ // contiguously after the first, and nothing from the first wave is redelivered.+ const wave2 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${5 + i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${5 + i}`, partition: 1 })),+ ])+ for (const partition of [0, 1]) {+ expect(Math.min(...wave2.filter((r) => r.partition === partition).map((r) => r.offset))).toBe(+ expectedCommit.get(partition)+ )+ }- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)+ const produced = [...wave1, ...wave2]+ await waitFor(() => countByPartitionOffset(ledger).size >= produced.length, 15_000)- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 5 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 5 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- // Fresh topic: the broker must have assigned offsets 0..4 on each partition.- for (const partition of [0, 1]) {- expect(- wave1- .filter((r) => r.partition === partition)- .map((r) => r.offset)- .sort((x, y) => x - y)- ).toEqual([0, 1, 2, 3, 4])- }- await waitFor(() => ledger.length >= wave1.length, 8_000)-- // With every batch settled, the stored offsets are committed at exactly- // highest-delivered + 1 per partition.- const expectedCommit = new Map<number, number>()- for (const partition of [0, 1]) {- const highest = Math.max(...wave1.filter((r) => r.partition === partition).map((r) => r.offset))- expectedCommit.set(partition, highest + 1)- }- for (const [partition, offset] of expectedCommit) {- await waitForAsync(- async () => (await fetchCommittedOffset(groupId, topic, partition)) === offset,- 10_000+ // Exact per-message validation: every produced record consumed at exactly the+ // (partition, offset) its delivery report announced, exactly once, correct content.+ expect(ledger.length).toBe(produced.length)+ const consumedAt = new Map(+ ledger.map((e) => [`${e.partition}:${e.offset}`, { key: e.key, value: e.value }]))- }-- // B joins: cooperative-sticky moves exactly one partition, and A never drops to- // zero assignments while it happens (the incremental-rebalance property).- consumerB = makeConsumer(groupId, topic, record('B'), { autoOffsetStore: true })- const b = consumerB- let minAssignedA = 2- await waitFor(() => {- minAssignedA = Math.min(minAssignedA, consumerA.assignments().length)- return consumerA.assignments().length === 1 && b.assignments().length === 1- }, 15_000)- expect(minAssignedA).toBeGreaterThanOrEqual(1)-- // The new owner resumes from the exact committed offsets: a second wave lands- // contiguously after the first, and nothing from the first wave is redelivered.- const wave2 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${5 + i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${5 + i}`, partition: 1 })),- ])- for (const partition of [0, 1]) {- expect(Math.min(...wave2.filter((r) => r.partition === partition).map((r) => r.offset))).toBe(- expectedCommit.get(partition)+ const producedAt = new Map(+ produced.map((r) => [`${r.partition}:${r.offset}`, { key: r.key, value: r.value }]))- }-- const produced = [...wave1, ...wave2]- await waitFor(() => countByPartitionOffset(ledger).size >= produced.length, 15_000)+ expect(consumedAt).toEqual(producedAt)- // Exact per-message validation: every produced record consumed at exactly the- // (partition, offset) its delivery report announced, exactly once, correct content.- expect(ledger.length).toBe(produced.length)- const consumedAt = new Map(- ledger.map((e) => [`${e.partition}:${e.offset}`, { key: e.key, value: e.value }])- )- const producedAt = new Map(- produced.map((r) => [`${r.partition}:${r.offset}`, { key: r.key, value: r.value }])- )- expect(consumedAt).toEqual(producedAt)-- // Both members did real work after the split.- expect(- new Set(ledger.filter((e) => wave2.some((r) => r.value === e.value)).map((e) => e.consumerId)).size- ).toBe(2)- } finally {- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)- }- })-- it('a batch in flight at revoke that settles inside the drain budget commits its offsets: no redelivery on either partition', async () => {- const topic = `v2_int_reb_gen_${randomUUID()}`- const groupId = `v2-int-reb-gen-${randomUUID()}`- await createTopic(topic, 2)-- // The generation guard only engages when the loop processes the REVOKE while a task- // is still in flight. With the default of 1 background slot, backpressure blocks the- // loop on that very task (no polling → no rebalance callback → the task settles and- // stores before the generation ever bumps). Give the loop free slots so it can take- // the revoke while the gated task is pending — the scenario the guard exists for.- const savedMaxBackgroundTasks = defaultConfig.CONSUMER_MAX_BACKGROUND_TASKS- defaultConfig.CONSUMER_MAX_BACKGROUND_TASKS = 4-- const ledger: LedgerEntry[] = []- // Gate for consumer A's background tasks: once armed, A's batches don't settle- // until the test releases them — keeping their offset stores in flight across- // the revoke so the generation guard is what decides their fate.- let gateArmed = false- let releaseGate: () => void = () => {}- const gate = new Promise<void>((resolve) => {- releaseGate = resolve- })-- const recordA = (messages: Message[]): { backgroundTask?: Promise<unknown> } => {- for (const m of messages) {- ledger.push({- consumerId: 'A',- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })- }- return gateArmed ? { backgroundTask: gate } : {}- }- const recordB = (messages: Message[]): void => {- for (const m of messages) {- ledger.push({- consumerId: 'B',- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })+ // Both members did real work after the split.+ expect(+ new Set(ledger.filter((e) => wave2.some((r) => r.value === e.value)).map((e) => e.consumerId))+ .size+ ).toBe(2)+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}}+ )- const consumerA = makeConsumer(groupId, topic, (msgs) => Promise.resolve(recordA(msgs)), {- autoOffsetStore: true,- })- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)-- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- await waitFor(() => ledger.length >= wave1.length, 8_000)-- for (const partition of [0, 1]) {- const highest = Math.max(...wave1.filter((r) => r.partition === partition).map((r) => r.offset))- await waitForAsync(- async () => (await fetchCommittedOffset(groupId, topic, partition)) === highest + 1,- 10_000- )- }-- // One gated message per partition: consumed by A, but its settle chain (and- // therefore its offset store) hangs on the gate.- gateArmed = true- const gated = await produceTracked(producer, topic, [- { key: 'gated-p0', partition: 0 },- { key: 'gated-p1', partition: 1 },- ])- await waitFor(() => gated.every((g) => ledger.some((e) => e.value === g.value)), 8_000)-- // B joins while the gated batch is still in flight. The revoke drains first — the- // drain blocks on our gate. Hold it long enough for the revoke to be firmly in- // progress, then release well inside the drain budget- // (CONSUMER_REBALANCE_TIMEOUT_MS, 20s by default).- consumerB = makeConsumer(groupId, topic, (msgs) => Promise.resolve(recordB(msgs)), {- autoOffsetStore: true,+ it.concurrent(+ 'a batch in flight at revoke that settles inside the drain budget commits its offsets: no redelivery on either partition',+ async () => {+ const topic = `v2_int_reb_gen_${randomUUID()}`+ const groupId = `v2-int-reb-gen-${randomUUID()}`+ await createTopic(topic, 2)++ // The generation guard only engages when the loop processes the REVOKE while a task+ // is still in flight. With the default of 1 background slot, backpressure blocks the+ // loop on that very task (no polling → no rebalance callback → the task settles and+ // stores before the generation ever bumps). Give the loop free slots so it can take+ // the revoke while the gated task is pending — the scenario the guard exists for.+ const ledger: LedgerEntry[] = []+ // Gate for consumer A's background tasks: once armed, A's batches don't settle+ // until the test releases them — keeping their offset stores in flight across+ // the revoke so the generation guard is what decides their fate.+ let gateArmed = false+ let releaseGate: () => void = () => {}+ const gate = new Promise<void>((resolve) => {+ releaseGate = resolve})- const b = consumerB- await delay(8_000)- releaseGate()-- await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 15_000)- const movedPartition = b.assignments()[0].partition- const retainedPartition = movedPartition === 0 ? 1 : 0-- // The gated batch settled inside the drain budget, so its offsets were stored while- // both partitions were still assigned and committed as the moved one was given up.- // Neither gated message is redelivered: A saw each exactly once and B re-reads- // nothing. This is the duplicate-send guarantee — replaying here would fire a- // destination's outbound request a second time.- const gatedMoved = gated.find((g) => g.partition === movedPartition)!- const gatedRetained = gated.find((g) => g.partition === retainedPartition)!- for (const partition of [movedPartition, retainedPartition]) {- const gatedForPartition = gated.find((g) => g.partition === partition)!- await waitForAsync(- async () =>- (await fetchCommittedOffset(groupId, topic, partition)) === gatedForPartition.offset + 1,- 15_000- )- }- expect(ledger.filter((e) => e.value === gatedMoved.value).map((e) => e.consumerId)).toEqual(['A'])- expect(ledger.filter((e) => e.value === gatedRetained.value).map((e) => e.consumerId)).toEqual(['A'])-- // Nothing else was re-read either: every (partition, offset) appears exactly once.- expect([...countByPartitionOffset(ledger).values()].filter((n) => n > 1)).toEqual([])- } finally {- defaultConfig.CONSUMER_MAX_BACKGROUND_TASKS = savedMaxBackgroundTasks- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)- }- })- it('a batch outliving the drain budget is fenced: its offsets never commit and the moved partition redelivers it', async () => {- const topic = `v2_int_reb_fence_${randomUUID()}`- const groupId = `v2-int-reb-fence-${randomUUID()}`- await createTopic(topic, 2)-- // Free background slots so the loop can take the revoke while the gated task is- // pending, and a drain budget short enough that the gate outlives it — the task then- // settles after the fence, which is what must block its offset store.- const savedMaxBackgroundTasks = defaultConfig.CONSUMER_MAX_BACKGROUND_TASKS- const savedRebalanceTimeout = defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MS- defaultConfig.CONSUMER_MAX_BACKGROUND_TASKS = 4- defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MS = 2_000-- const ledger: LedgerEntry[] = []- let gateArmed = false- let releaseGate: () => void = () => {}- const gate = new Promise<void>((resolve) => {- releaseGate = resolve- })-- const record =- (consumerId: string, gated: boolean) =>- (messages: Message[]): { backgroundTask?: Promise<unknown> } => {+ const recordA = (messages: Message[]): { backgroundTask?: Promise<unknown> } => {for (const m of messages) {ledger.push({- consumerId,+ consumerId: 'A',+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ }+ return gateArmed ? { backgroundTask: gate } : {}+ }+ const recordB = (messages: Message[]): void => {+ for (const m of messages) {+ ledger.push({+ consumerId: 'B',partition: m.partition,offset: m.offset,key: m.key?.toString() ?? '',@@ -497,73 +408,194 @@ describe('KafkaConsumerV2 rebalance semantics (integration)', () => {seenAt: Date.now(),})}- return gated && gateArmed ? { backgroundTask: gate } : {}}- const consumerA = makeConsumer(groupId, topic, (msgs) => Promise.resolve(record('A', true)(msgs)), {- autoOffsetStore: true,- })- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)-- const wave1 = await produceTracked(producer, topic, [- { key: 'a0', partition: 0 },- { key: 'b0', partition: 1 },- ])- await waitFor(() => ledger.length >= wave1.length, 8_000)- const preGateCommit = new Map<number, number>()- for (const produced of wave1) {- preGateCommit.set(produced.partition, produced.offset + 1)- await waitForAsync(- async () =>- (await fetchCommittedOffset(groupId, topic, produced.partition)) === produced.offset + 1,- 10_000- )+ const consumerA = makeConsumer(groupId, topic, (msgs) => Promise.resolve(recordA(msgs)), {+ autoOffsetStore: true,+ maxBackgroundTasks: 4,+ })+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)++ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ await waitFor(() => ledger.length >= wave1.length, 8_000)++ for (const partition of [0, 1]) {+ const highest = Math.max(...wave1.filter((r) => r.partition === partition).map((r) => r.offset))+ await waitForAsync(+ async () => (await fetchCommittedOffset(groupId, topic, partition)) === highest + 1,+ 10_000+ )+ }++ // One gated message per partition: consumed by A, but its settle chain (and+ // therefore its offset store) hangs on the gate.+ gateArmed = true+ const gated = await produceTracked(producer, topic, [+ { key: 'gated-p0', partition: 0 },+ { key: 'gated-p1', partition: 1 },+ ])+ await waitFor(() => gated.every((g) => ledger.some((e) => e.value === g.value)), 8_000)++ // B joins while the gated batch is still in flight. The revoke drains first — the+ // drain blocks on our gate. Hold it long enough for the revoke to be firmly in+ // progress, then release well inside the drain budget+ // (CONSUMER_REBALANCE_TIMEOUT_MS, 20s by default).+ consumerB = makeConsumer(groupId, topic, (msgs) => Promise.resolve(recordB(msgs)), {+ autoOffsetStore: true,+ maxBackgroundTasks: 4,+ })+ const b = consumerB+ await delay(8_000)+ releaseGate()++ await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 15_000)+ const movedPartition = b.assignments()[0].partition+ const retainedPartition = movedPartition === 0 ? 1 : 0++ // The gated batch settled inside the drain budget, so its offsets were stored while+ // both partitions were still assigned and committed as the moved one was given up.+ // Neither gated message is redelivered: A saw each exactly once and B re-reads+ // nothing. This is the duplicate-send guarantee — replaying here would fire a+ // destination's outbound request a second time.+ const gatedMoved = gated.find((g) => g.partition === movedPartition)!+ const gatedRetained = gated.find((g) => g.partition === retainedPartition)!+ for (const partition of [movedPartition, retainedPartition]) {+ const gatedForPartition = gated.find((g) => g.partition === partition)!+ await waitForAsync(+ async () =>+ (await fetchCommittedOffset(groupId, topic, partition)) ===+ gatedForPartition.offset + 1,+ 15_000+ )+ }+ expect(ledger.filter((e) => e.value === gatedMoved.value).map((e) => e.consumerId)).toEqual(['A'])+ expect(ledger.filter((e) => e.value === gatedRetained.value).map((e) => e.consumerId)).toEqual([+ 'A',+ ])++ // Nothing else was re-read either: every (partition, offset) appears exactly once.+ expect([...countByPartitionOffset(ledger).values()].filter((n) => n > 1)).toEqual([])+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}+ }+ )++ it.concurrent(+ 'a batch outliving the drain budget is fenced: its offsets never commit and the moved partition redelivers it',+ async () => {+ const topic = `v2_int_reb_fence_${randomUUID()}`+ const groupId = `v2-int-reb-fence-${randomUUID()}`+ await createTopic(topic, 2)++ // Free background slots so the loop can take the revoke while the gated task is+ // pending, and a drain budget short enough that the gate outlives it — the task then+ // settles after the fence, which is what must block its offset store.+ const drainBudgetMs = 2_000++ const ledger: LedgerEntry[] = []+ let gateArmed = false+ let releaseGate: () => void = () => {}+ const gate = new Promise<void>((resolve) => {+ releaseGate = resolve+ })- gateArmed = true- const gated = await produceTracked(producer, topic, [- { key: 'gated-p0', partition: 0 },- { key: 'gated-p1', partition: 1 },- ])- await waitFor(() => gated.every((g) => ledger.some((e) => e.value === g.value)), 8_000)+ const record =+ (consumerId: string, gated: boolean) =>+ (messages: Message[]): { backgroundTask?: Promise<unknown> } => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ }+ return gated && gateArmed ? { backgroundTask: gate } : {}+ }- // B joins. The drain gives up after 2s with the gate still closed, so the fence- // engages before the task settles.- consumerB = makeConsumer(groupId, topic, (msgs) => Promise.resolve(record('B', false)(msgs)), {+ const consumerA = makeConsumer(groupId, topic, (msgs) => Promise.resolve(record('A', true)(msgs)), {autoOffsetStore: true,+ maxBackgroundTasks: 4,+ rebalanceTimeoutMs: drainBudgetMs,})- const b = consumerB- await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 20_000)- releaseGate()-- const movedPartition = b.assignments()[0].partition- const retainedPartition = movedPartition === 0 ? 1 : 0- const gatedMoved = gated.find((g) => g.partition === movedPartition)!-- // Fenced: the gated batch's store was skipped, so the moved partition redelivers- // its message to B from the pre-gate commit. At-least-once is preserved — an- // unfinished batch must never be committed as done.- await waitFor(() => ledger.filter((e) => e.value === gatedMoved.value).length >= 2, 20_000)- expect(- ledger- .filter((e) => e.value === gatedMoved.value)- .map((e) => e.consumerId)- .sort()- ).toEqual(['A', 'B'])- expect(await fetchCommittedOffset(groupId, topic, retainedPartition)).toBe(- preGateCommit.get(retainedPartition)- )- } finally {- defaultConfig.CONSUMER_MAX_BACKGROUND_TASKS = savedMaxBackgroundTasks- defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MS = savedRebalanceTimeout- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)++ const wave1 = await produceTracked(producer, topic, [+ { key: 'a0', partition: 0 },+ { key: 'b0', partition: 1 },+ ])+ await waitFor(() => ledger.length >= wave1.length, 8_000)+ const preGateCommit = new Map<number, number>()+ for (const produced of wave1) {+ preGateCommit.set(produced.partition, produced.offset + 1)+ await waitForAsync(+ async () =>+ (await fetchCommittedOffset(groupId, topic, produced.partition)) ===+ produced.offset + 1,+ 10_000+ )+ }++ gateArmed = true+ const gated = await produceTracked(producer, topic, [+ { key: 'gated-p0', partition: 0 },+ { key: 'gated-p1', partition: 1 },+ ])+ await waitFor(() => gated.every((g) => ledger.some((e) => e.value === g.value)), 8_000)++ // B joins. The drain gives up after 2s with the gate still closed, so the fence+ // engages before the task settles.+ consumerB = makeConsumer(groupId, topic, (msgs) => Promise.resolve(record('B', false)(msgs)), {+ autoOffsetStore: true,+ maxBackgroundTasks: 4,+ rebalanceTimeoutMs: drainBudgetMs,+ })+ const b = consumerB+ await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 20_000)+ releaseGate()++ const movedPartition = b.assignments()[0].partition+ const retainedPartition = movedPartition === 0 ? 1 : 0+ const gatedMoved = gated.find((g) => g.partition === movedPartition)!++ // Fenced: the gated batch's store was skipped, so the moved partition redelivers+ // its message to B from the pre-gate commit. At-least-once is preserved — an+ // unfinished batch must never be committed as done.+ await waitFor(() => ledger.filter((e) => e.value === gatedMoved.value).length >= 2, 20_000)+ expect(+ ledger+ .filter((e) => e.value === gatedMoved.value)+ .map((e) => e.consumerId)+ .sort()+ ).toEqual(['A', 'B'])+ // The fence is per partition. A still holds the retained partition, so the+ // laggard's store for it is valid and lands on the next auto-commit tick.+ const retainedCommit = preGateCommit.get(retainedPartition)! + 1+ await waitForAsync(+ async () => (await fetchCommittedOffset(groupId, topic, retainedPartition)) === retainedCommit,+ 10_000+ )+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)+ }}- })+ )})// The session replay mode: autoCommit stays on but nothing is stored automatically, so@@ -574,685 +606,708 @@ describe('KafkaConsumerV2 rebalance semantics (integration)', () => {// heartbeat thread must keep the member alive for its whole duration — a slow flush is// bounded by max.poll.interval.ms, never the session timeout. The long window also gives// an unassign racing ahead of the hook ample time to manifest.- it('flush-on-revoke outliving the session timeout: hook runs while assigned, stored offsets commit, no reprocessing', async () => {- const flushDurationMs = 7_000- const topic = `v2_int_reb_hook_${randomUUID()}`- const groupId = `v2-int-reb-hook-${randomUUID()}`- await createTopic(topic, 2)-- const ledger: LedgerEntry[] = []- const trackedHighest = new Map<number, number>()- const hook: {- invocations: number- revokedPartitions: { topic: string; partition: number }[]- assignedOnEntry: number- assignedAfterDelay: number- storeError: unknown- completedAt: number- } = {- invocations: 0,- revokedPartitions: [],- assignedOnEntry: -1,- assignedAfterDelay: -1,- storeError: null,- completedAt: 0,- }-- const record = (consumerId: string) => (messages: Message[]) => {- for (const m of messages) {- ledger.push({- consumerId,- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })- trackedHighest.set(m.partition, Math.max(trackedHighest.get(m.partition) ?? -1, m.offset))+ it.concurrent(+ 'flush-on-revoke outliving the session timeout: hook runs while assigned, stored offsets commit, no reprocessing',+ async () => {+ const flushDurationMs = 7_000+ const topic = `v2_int_reb_hook_${randomUUID()}`+ const groupId = `v2-int-reb-hook-${randomUUID()}`+ await createTopic(topic, 2)++ const ledger: LedgerEntry[] = []+ const trackedHighest = new Map<number, number>()+ const hook: {+ invocations: number+ revokedPartitions: { topic: string; partition: number }[]+ assignedOnEntry: number+ assignedAfterDelay: number+ storeError: unknown+ completedAt: number+ } = {+ invocations: 0,+ revokedPartitions: [],+ assignedOnEntry: -1,+ assignedAfterDelay: -1,+ storeError: null,+ completedAt: 0,}- return Promise.resolve()- }- const consumerA = makeConsumer(groupId, topic, record('A'), {- autoOffsetStore: false,- onPartitionsRevoked: async (revoked) => {- if (++hook.invocations > 1) {- return- }- hook.revokedPartitions = revoked.map((tp) => ({ topic: tp.topic, partition: tp.partition }))- hook.assignedOnEntry = consumerA.assignments().length- // The simulated flush: widens the revoke window so an unassign racing- // ahead of the hook reliably manifests — as a shrunken assignment or a- // store error below.- await delay(flushDurationMs)- hook.assignedAfterDelay = consumerA.assignments().length- try {- // Like the real ingester's flush: store offsets for everything tracked,- // not just the partitions being revoked.- consumerA.offsetsStore(- [...trackedHighest.entries()].map(([partition, offset]) => ({- topic,- partition,- offset: offset + 1,- }))- )- } catch (e) {- hook.storeError = e+ const record = (consumerId: string) => (messages: Message[]) => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ trackedHighest.set(m.partition, Math.max(trackedHighest.get(m.partition) ?? -1, m.offset))}- hook.completedAt = Date.now()- },- })- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)-- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 4 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 4 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- await waitFor(() => ledger.length >= wave1.length, 8_000)-- // Precondition: autoOffsetStore is off and nothing was stored, so nothing is- // committed — only the hook's store can move the group offsets.- for (const partition of [0, 1]) {- expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()+ return Promise.resolve()}- consumerB = makeConsumer(groupId, topic, record('B'), { autoOffsetStore: false })- const b = consumerB+ const consumerA = makeConsumer(groupId, topic, record('A'), {+ autoOffsetStore: false,+ onPartitionsRevoked: async (revoked) => {+ if (++hook.invocations > 1) {+ return+ }+ hook.revokedPartitions = revoked.map((tp) => ({ topic: tp.topic, partition: tp.partition }))+ hook.assignedOnEntry = consumerA.assignments().length+ // The simulated flush: widens the revoke window so an unassign racing+ // ahead of the hook reliably manifests — as a shrunken assignment or a+ // store error below.+ await delay(flushDurationMs)+ hook.assignedAfterDelay = consumerA.assignments().length+ try {+ // Like the real ingester's flush: store offsets for everything tracked,+ // not just the partitions being revoked.+ consumerA.offsetsStore(+ [...trackedHighest.entries()].map(([partition, offset]) => ({+ topic,+ partition,+ offset: offset + 1,+ }))+ )+ } catch (e) {+ hook.storeError = e+ }+ hook.completedAt = Date.now()+ },+ })+ let consumerB: KafkaConsumerV2 | undefined- await waitFor(() => hook.completedAt > 0, 15_000)+ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)- // Cooperative revoke: exactly one partition moves, and A still owns BOTH- // partitions for the whole duration of the flush.- expect(hook.revokedPartitions).toHaveLength(1)- expect(hook.assignedOnEntry).toBe(2)- expect(hook.assignedAfterDelay).toBe(2)- expect(hook.storeError).toBeNull()+ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 4 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 4 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ await waitFor(() => ledger.length >= wave1.length, 8_000)- await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 15_000)- const movedPartition = b.assignments()[0].partition+ // Precondition: autoOffsetStore is off and nothing was stored, so nothing is+ // committed — only the hook's store can move the group offsets.+ for (const partition of [0, 1]) {+ expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()+ }- // The moved partition's stored offset is committed as part of the unassign;- // the retained partition's store lands via the auto-commit timer. Both exact.- const expectedCommit = new Map<number, number>()- for (const partition of [0, 1]) {- const highest = Math.max(...wave1.filter((r) => r.partition === partition).map((r) => r.offset))- expectedCommit.set(partition, highest + 1)- }- for (const partition of [movedPartition, movedPartition === 0 ? 1 : 0]) {- await waitForAsync(- async () =>- (await fetchCommittedOffset(groupId, topic, partition)) === expectedCommit.get(partition),- 15_000- )- }+ consumerB = makeConsumer(groupId, topic, record('B'), { autoOffsetStore: false })+ const b = consumerB- // The new owner resumes after the flushed offsets: a second wave lands- // contiguously and nothing from the first wave is ever redelivered.- const wave2 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${4 + i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${4 + i}`, partition: 1 })),- ])- for (const partition of [0, 1]) {- expect(Math.min(...wave2.filter((r) => r.partition === partition).map((r) => r.offset))).toBe(- expectedCommit.get(partition)- )- }+ await waitFor(() => hook.completedAt > 0, 15_000)- const produced = [...wave1, ...wave2]- await waitFor(() => countByPartitionOffset(ledger).size >= produced.length, 15_000)- expect(ledger.length).toBe(produced.length)- const consumedAt = new Map(- ledger.map((e) => [`${e.partition}:${e.offset}`, { key: e.key, value: e.value }])- )- const producedAt = new Map(- produced.map((r) => [`${r.partition}:${r.offset}`, { key: r.key, value: r.value }])- )- expect(consumedAt).toEqual(producedAt)- } finally {- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)- }- })+ // Cooperative revoke: exactly one partition moves, and A still owns BOTH+ // partitions for the whole duration of the flush.+ expect(hook.revokedPartitions).toHaveLength(1)+ expect(hook.assignedOnEntry).toBe(2)+ expect(hook.assignedAfterDelay).toBe(2)+ expect(hook.storeError).toBeNull()- it('a throwing revoke hook still releases the partition: moved partition reprocesses, retained partition is unaffected', async () => {- const topic = `v2_int_reb_hookthrow_${randomUUID()}`- const groupId = `v2-int-reb-hookthrow-${randomUUID()}`- await createTopic(topic, 2)-- const ledger: LedgerEntry[] = []- const record = (consumerId: string) => (messages: Message[]) => {- for (const m of messages) {- ledger.push({- consumerId,- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })+ await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 15_000)+ const movedPartition = b.assignments()[0].partition++ // The moved partition's stored offset is committed as part of the unassign;+ // the retained partition's store lands via the auto-commit timer. Both exact.+ const expectedCommit = new Map<number, number>()+ for (const partition of [0, 1]) {+ const highest = Math.max(...wave1.filter((r) => r.partition === partition).map((r) => r.offset))+ expectedCommit.set(partition, highest + 1)+ }+ for (const partition of [movedPartition, movedPartition === 0 ? 1 : 0]) {+ await waitForAsync(+ async () =>+ (await fetchCommittedOffset(groupId, topic, partition)) ===+ expectedCommit.get(partition),+ 15_000+ )+ }++ // The new owner resumes after the flushed offsets: a second wave lands+ // contiguously and nothing from the first wave is ever redelivered.+ const wave2 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${4 + i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${4 + i}`, partition: 1 })),+ ])+ for (const partition of [0, 1]) {+ expect(Math.min(...wave2.filter((r) => r.partition === partition).map((r) => r.offset))).toBe(+ expectedCommit.get(partition)+ )+ }++ const produced = [...wave1, ...wave2]+ await waitFor(() => countByPartitionOffset(ledger).size >= produced.length, 15_000)+ expect(ledger.length).toBe(produced.length)+ const consumedAt = new Map(+ ledger.map((e) => [`${e.partition}:${e.offset}`, { key: e.key, value: e.value }])+ )+ const producedAt = new Map(+ produced.map((r) => [`${r.partition}:${r.offset}`, { key: r.key, value: r.value }])+ )+ expect(consumedAt).toEqual(producedAt)+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}- return Promise.resolve()}+ )- const consumerA = makeConsumer(groupId, topic, record('A'), {- autoOffsetStore: false,- onPartitionsRevoked: () => {- throw new Error('flush failed during revoke')- },- })- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- await waitFor(() => ledger.length >= wave1.length, 8_000)-- consumerB = makeConsumer(groupId, topic, record('B'), { autoOffsetStore: false })- const b = consumerB-- // The hook threw, so nothing was stored — but the partition must still be given- // up. The new owner starts from earliest and redelivers the moved partition's- // wave; the retained partition stays with A and is never re-read.- await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 20_000)- const movedPartition = b.assignments()[0].partition- const retainedPartition = movedPartition === 0 ? 1 : 0- const movedHighest = Math.max(- ...wave1.filter((r) => r.partition === movedPartition).map((r) => r.offset)- )+ it.concurrent(+ 'a throwing revoke hook still releases the partition: moved partition reprocesses, retained partition is unaffected',+ async () => {+ const topic = `v2_int_reb_hookthrow_${randomUUID()}`+ const groupId = `v2-int-reb-hookthrow-${randomUUID()}`+ await createTopic(topic, 2)- await waitFor(- () =>- ledger.filter(- (e) => e.consumerId === 'B' && e.partition === movedPartition && e.offset === movedHighest- ).length >= 1,- 15_000- )- for (const r of wave1) {- const expectedCount = r.partition === movedPartition ? 2 : 1- expect(ledger.filter((e) => e.value === r.value)).toHaveLength(expectedCount)+ const ledger: LedgerEntry[] = []+ const record = (consumerId: string) => (messages: Message[]) => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ }+ return Promise.resolve()}- expect(await fetchCommittedOffset(groupId, topic, retainedPartition)).toBeNull()- } finally {- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)- }- })- it('a flush exceeding the consumer budget: the hook is abandoned, the rebalance proceeds, the member survives unfenced', async () => {- const topic = `v2_int_reb_hookabandon_${randomUUID()}`- const groupId = `v2-int-reb-hookabandon-${randomUUID()}`- await createTopic(topic, 2)-- // The consumer-side budget for the revoke hook (drainTimeoutMs), well under- // max.poll.interval.ms (30s here): the consumer gives the partitions up itself, so- // the member must NOT get fenced — it keeps its retained partition and stays live.- // The flush outlives the budget by enough that the takeover assertions all land- // while it is still running.- const hookBudgetMs = 3_000- const flushDurationMs = 15_000- const savedRebalanceTimeout = defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MS- defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MS = hookBudgetMs-- const ledger: LedgerEntry[] = []- const tracked = new Map<number, number>()- const hook = { invocations: 0, completedAt: 0 }-- const record = (consumerId: string, track: boolean) => (messages: Message[]) => {- for (const m of messages) {- ledger.push({- consumerId,- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })- if (track) {- tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))+ const consumerA = makeConsumer(groupId, topic, record('A'), {+ autoOffsetStore: false,+ onPartitionsRevoked: () => {+ throw new Error('flush failed during revoke')+ },+ })+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)+ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ await waitFor(() => ledger.length >= wave1.length, 8_000)++ consumerB = makeConsumer(groupId, topic, record('B'), { autoOffsetStore: false })+ const b = consumerB++ // The hook threw, so nothing was stored — but the partition must still be given+ // up. The new owner starts from earliest and redelivers the moved partition's+ // wave; the retained partition stays with A and is never re-read.+ await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 20_000)+ const movedPartition = b.assignments()[0].partition+ const retainedPartition = movedPartition === 0 ? 1 : 0+ const movedHighest = Math.max(+ ...wave1.filter((r) => r.partition === movedPartition).map((r) => r.offset)+ )++ await waitFor(+ () =>+ ledger.filter(+ (e) =>+ e.consumerId === 'B' && e.partition === movedPartition && e.offset === movedHighest+ ).length >= 1,+ 15_000+ )+ for (const r of wave1) {+ const expectedCount = r.partition === movedPartition ? 2 : 1+ expect(ledger.filter((e) => e.value === r.value)).toHaveLength(expectedCount)}+ expect(await fetchCommittedOffset(groupId, topic, retainedPartition)).toBeNull()+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}- return Promise.resolve()}+ )- const consumerA = makeConsumer(groupId, topic, record('A', true), {- autoOffsetStore: false,- onPartitionsRevoked: async () => {- if (++hook.invocations > 1) {- return- }- await delay(flushDurationMs)- // The abandoned flush finally stores its tracked offsets. For the moved- // partition this must never become a group commit — the new owner already- // reprocessed instead.- try {- consumerA.offsetsStore(- [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))- )- } catch {- // Rejection of the moved partition's store is an acceptable outcome too.+ it.concurrent(+ 'a flush exceeding the consumer budget: the hook is abandoned, the rebalance proceeds, the member survives unfenced',+ async () => {+ const topic = `v2_int_reb_hookabandon_${randomUUID()}`+ const groupId = `v2-int-reb-hookabandon-${randomUUID()}`+ await createTopic(topic, 2)++ // The consumer-side budget for the revoke hook (drainTimeoutMs), well under+ // max.poll.interval.ms (30s here): the consumer gives the partitions up itself, so+ // the member must NOT get fenced — it keeps its retained partition and stays live.+ // The flush outlives the budget by enough that the takeover assertions all land+ // while it is still running.+ const hookBudgetMs = 3_000+ const flushDurationMs = 15_000+ const ledger: LedgerEntry[] = []+ const tracked = new Map<number, number>()+ const hook = { invocations: 0, completedAt: 0 }++ const record = (consumerId: string, track: boolean) => (messages: Message[]) => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ if (track) {+ tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))+ }}- hook.completedAt = Date.now()- },- })- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- await waitFor(() => ledger.length >= wave1.length, 8_000)- for (const partition of [0, 1]) {- expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()+ return Promise.resolve()}- consumerB = makeConsumer(groupId, topic, record('B', false), { autoOffsetStore: false })- const b = consumerB-- // The budget releases the rebalance: B owns the moved partition and (with no- // committed offsets) redelivers its wave while the flush is still running.- await waitFor(- () => consumerA.assignments().length === 1 && b.assignments().length === 1,- hookBudgetMs + 10_000- )- const movedPartition = b.assignments()[0].partition- const retainedPartition = movedPartition === 0 ? 1 : 0- await waitFor(- () =>- wave1- .filter((r) => r.partition === movedPartition)- .every((r) =>- ledger.some(- (e) => e.consumerId === 'B' && e.partition === r.partition && e.offset === r.offset- )- ),- 12_000- )- expect(hook.completedAt).toBe(0)-- // Unlike the fencing case, the member survives: it keeps the retained partition- // and keeps consuming new messages on it while the flush is still in progress.- const wave2 = await produceTracked(producer, topic, [- { key: 'retained-post', partition: retainedPartition },- ])- await waitFor(() => ledger.some((e) => e.consumerId === 'A' && e.value === wave2[0].value), 10_000)- expect(consumerA.assignments().length).toBe(1)-- // Once the flush finishes, its late store for the moved partition is discarded:- // the group offset stays unset (B stored nothing; A no longer owns it).- await waitFor(() => hook.completedAt > 0, flushDurationMs + 10_000)- await delay(1_500)- expect(await fetchCommittedOffset(groupId, topic, movedPartition)).toBeNull()- } finally {- defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MS = savedRebalanceTimeout- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)- }- })+ const consumerA = makeConsumer(groupId, topic, record('A', true), {+ autoOffsetStore: false,+ rebalanceTimeoutMs: hookBudgetMs,+ onPartitionsRevoked: async () => {+ if (++hook.invocations > 1) {+ return+ }+ await delay(flushDurationMs)+ // The abandoned flush finally stores its tracked offsets. For the moved+ // partition this must never become a group commit — the new owner already+ // reprocessed instead.+ try {+ consumerA.offsetsStore(+ [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))+ )+ } catch {+ // Rejection of the moved partition's store is an acceptable outcome too.+ }+ hook.completedAt = Date.now()+ },+ })+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)+ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ await waitFor(() => ledger.length >= wave1.length, 8_000)+ for (const partition of [0, 1]) {+ expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()+ }- it('a flush exceeding the rebalance timeout: the broker fences the slow member, the new owner reprocesses, the slow consumer recovers', async () => {- const topic = `v2_int_reb_hookslow_${randomUUID()}`- const groupId = `v2-int-reb-hookslow-${randomUUID()}`- await createTopic(topic, 2)-- // max.poll.interval.ms doubles as the broker-enforced rebalance timeout: a member- // that hasn't rejoined within it is kicked and the rebalance completes without it.- // 6s is the tightest legal value — librdkafka requires it >= session.timeout.ms,- // whose broker-enforced floor is 6s. The flush outlives it by enough that B holds- // both partitions for several seconds mid-flush (once the flush ends, A rejoins and- // takes a partition back — the redelivery assertions need the sole-owner window).- const rebalanceTimeoutMs = 6_000- const flushDurationMs = 12_000-- const ledger: LedgerEntry[] = []- const tracked = new Map<number, number>()- const hook = { invocations: 0, completedAt: 0 }- const record = (consumerId: string) => (messages: Message[]) => {- for (const m of messages) {- ledger.push({- consumerId,- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),+ consumerB = makeConsumer(groupId, topic, record('B', false), {+ autoOffsetStore: false,+ rebalanceTimeoutMs: hookBudgetMs,})- if (consumerId === 'A') {- tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))- }+ const b = consumerB++ // The budget releases the rebalance: B owns the moved partition and (with no+ // committed offsets) redelivers its wave while the flush is still running.+ await waitFor(+ () => consumerA.assignments().length === 1 && b.assignments().length === 1,+ hookBudgetMs + 10_000+ )+ const movedPartition = b.assignments()[0].partition+ const retainedPartition = movedPartition === 0 ? 1 : 0+ await waitFor(+ () =>+ wave1+ .filter((r) => r.partition === movedPartition)+ .every((r) =>+ ledger.some(+ (e) =>+ e.consumerId === 'B' && e.partition === r.partition && e.offset === r.offset+ )+ ),+ 12_000+ )+ expect(hook.completedAt).toBe(0)++ // Unlike the fencing case, the member survives: it keeps the retained partition+ // and keeps consuming new messages on it while the flush is still in progress.+ const wave2 = await produceTracked(producer, topic, [+ { key: 'retained-post', partition: retainedPartition },+ ])+ await waitFor(() => ledger.some((e) => e.consumerId === 'A' && e.value === wave2[0].value), 10_000)+ expect(consumerA.assignments().length).toBe(1)++ // Once the flush finishes, its late store for the moved partition is discarded:+ // the group offset stays unset (B stored nothing; A no longer owns it).+ await waitFor(() => hook.completedAt > 0, flushDurationMs + 10_000)+ await delay(1_500)+ expect(await fetchCommittedOffset(groupId, topic, movedPartition)).toBeNull()+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}- return Promise.resolve()}+ )- const consumerA = makeConsumer(groupId, topic, record('A'), {- autoOffsetStore: false,- onPartitionsRevoked: async () => {- if (++hook.invocations > 1) {- return- }- await delay(flushDurationMs)- // Like a real slow flush, store the tracked offsets once it finally completes —- // the member has been fenced by now, and the test asserts this late store never- // becomes a group commit (the new owner already reprocessed instead).- try {- consumerA.offsetsStore(- [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))- )- } catch {- // Rejection is fine — either outcome must leave the group offsets unset.+ it.concurrent(+ 'a flush exceeding the rebalance timeout: the broker fences the slow member, the new owner reprocesses, the slow consumer recovers',+ async () => {+ const topic = `v2_int_reb_hookslow_${randomUUID()}`+ const groupId = `v2-int-reb-hookslow-${randomUUID()}`+ await createTopic(topic, 2)++ // max.poll.interval.ms doubles as the broker-enforced rebalance timeout: a member+ // that hasn't rejoined within it is kicked and the rebalance completes without it.+ // 6s is the tightest legal value — librdkafka requires it >= session.timeout.ms,+ // whose broker-enforced floor is 6s. The flush outlives it by enough that B holds+ // both partitions for several seconds mid-flush (once the flush ends, A rejoins and+ // takes a partition back — the redelivery assertions need the sole-owner window).+ const rebalanceTimeoutMs = 6_000+ const flushDurationMs = 12_000++ const ledger: LedgerEntry[] = []+ const tracked = new Map<number, number>()+ const hook = { invocations: 0, completedAt: 0 }+ const record = (consumerId: string) => (messages: Message[]) => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ if (consumerId === 'A') {+ tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))+ }}- hook.completedAt = Date.now()- },- rdKafkaOverrides: { 'max.poll.interval.ms': rebalanceTimeoutMs },- })- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- await waitFor(() => ledger.filter((e) => e.consumerId === 'A').length >= wave1.length, 8_000)-- consumerB = makeConsumer(groupId, topic, record('B'), {+ return Promise.resolve()+ }++ const consumerA = makeConsumer(groupId, topic, record('A'), {autoOffsetStore: false,+ onPartitionsRevoked: async () => {+ if (++hook.invocations > 1) {+ return+ }+ await delay(flushDurationMs)+ // Like a real slow flush, store the tracked offsets once it finally completes —+ // the member has been fenced by now, and the test asserts this late store never+ // becomes a group commit (the new owner already reprocessed instead).+ try {+ consumerA.offsetsStore(+ [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))+ )+ } catch {+ // Rejection is fine — either outcome must leave the group offsets unset.+ }+ hook.completedAt = Date.now()+ },rdKafkaOverrides: { 'max.poll.interval.ms': rebalanceTimeoutMs },})+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)+ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ await waitFor(() => ledger.filter((e) => e.consumerId === 'A').length >= wave1.length, 8_000)++ consumerB = makeConsumer(groupId, topic, record('B'), {+ autoOffsetStore: false,+ rdKafkaOverrides: { 'max.poll.interval.ms': rebalanceTimeoutMs },+ })- // The fenced member is kicked entirely, so B takes over BOTH partitions and- // (with nothing committed) redelivers the first wave while A's flush is still- // in progress: a slow flush delays the rebalance only up to the rebalance- // timeout, never indefinitely.- await waitFor(- () =>- wave1.every((r) =>- ledger.some(- (e) => e.consumerId === 'B' && e.partition === r.partition && e.offset === r.offset- )- ),- rebalanceTimeoutMs + 10_000- )- expect(hook.completedAt).toBe(0)- for (const partition of [0, 1]) {- expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()- }+ // The fenced member is kicked entirely, so B takes over BOTH partitions and+ // (with nothing committed) redelivers the first wave while A's flush is still+ // in progress: a slow flush delays the rebalance only up to the rebalance+ // timeout, never indefinitely.+ await waitFor(+ () =>+ wave1.every((r) =>+ ledger.some(+ (e) => e.consumerId === 'B' && e.partition === r.partition && e.offset === r.offset+ )+ ),+ rebalanceTimeoutMs + 10_000+ )+ expect(hook.completedAt).toBe(0)+ for (const partition of [0, 1]) {+ expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()+ }- // The slow consumer must recover rather than wedge: once the flush finishes,- // newly produced messages are still consumed.- await waitFor(() => hook.completedAt > 0, flushDurationMs + 10_000)- const wave2 = await produceTracked(producer, topic, [- { key: 'a-post', partition: 0 },- { key: 'b-post', partition: 1 },- ])- await waitFor(() => wave2.every((r) => ledger.some((e) => e.value === r.value)), 20_000)-- // The fenced flush's late offset store never becomes a group commit: nothing in- // this test stores offsets successfully, so the group offsets must still be unset.- for (const partition of [0, 1]) {- expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()+ // The slow consumer must recover rather than wedge: once the flush finishes,+ // newly produced messages are still consumed.+ await waitFor(() => hook.completedAt > 0, flushDurationMs + 10_000)+ const wave2 = await produceTracked(producer, topic, [+ { key: 'a-post', partition: 0 },+ { key: 'b-post', partition: 1 },+ ])+ await waitFor(() => wave2.every((r) => ledger.some((e) => e.value === r.value)), 20_000)++ // The fenced flush's late offset store never becomes a group commit: nothing in+ // this test stores offsets successfully, so the group offsets must still be unset.+ for (const partition of [0, 1]) {+ expect(await fetchCommittedOffset(groupId, topic, partition)).toBeNull()+ }+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}- } finally {- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)}- })+ )- it('a flushed batch then a batch in flight at revoke: commits advance from the flushed mark to the revoke mark, never backwards', async () => {- const topic = `v2_int_reb_twocycle_${randomUUID()}`- const groupId = `v2-int-reb-twocycle-${randomUUID()}`- await createTopic(topic, 2)-- const ledger: LedgerEntry[] = []- // Replay-style offset manager: track next-to-process per partition (monotonic), store- // everything tracked on flush, clear so the next cycle starts fresh.- const tracked = new Map<number, number>()- const flushOffsets = (): { partition: number; offset: number }[] => {- const stored = [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))- tracked.clear()- if (stored.length > 0) {- consumerA.offsetsStore(stored)+ it.concurrent(+ 'a flushed batch then a batch in flight at revoke: commits advance from the flushed mark to the revoke mark, never backwards',+ async () => {+ const topic = `v2_int_reb_twocycle_${randomUUID()}`+ const groupId = `v2-int-reb-twocycle-${randomUUID()}`+ await createTopic(topic, 2)++ const ledger: LedgerEntry[] = []+ // Replay-style offset manager: track next-to-process per partition (monotonic), store+ // everything tracked on flush, clear so the next cycle starts fresh.+ const tracked = new Map<number, number>()+ const flushOffsets = (): { partition: number; offset: number }[] => {+ const stored = [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))+ tracked.clear()+ if (stored.length > 0) {+ consumerA.offsetsStore(stored)+ }+ return stored.map(({ partition, offset }) => ({ partition, offset }))+ }+ const hook: {+ invocations: number+ storedAtRevoke: { partition: number; offset: number }[]+ completedAt: number+ } = {+ invocations: 0,+ storedAtRevoke: [],+ completedAt: 0,}- return stored.map(({ partition, offset }) => ({ partition, offset }))- }- const hook: {- invocations: number- storedAtRevoke: { partition: number; offset: number }[]- completedAt: number- } = {- invocations: 0,- storedAtRevoke: [],- completedAt: 0,- }- const record = (consumerId: string, track: boolean) => (messages: Message[]) => {- for (const m of messages) {- ledger.push({- consumerId,- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })- if (track) {- tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))+ const record = (consumerId: string, track: boolean) => (messages: Message[]) => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ if (track) {+ tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))+ }}+ return Promise.resolve()}- return Promise.resolve()- }- const consumerA = makeConsumer(groupId, topic, record('A', true), {- autoOffsetStore: false,- onPartitionsRevoked: () => {- if (++hook.invocations === 1) {- hook.storedAtRevoke = flushOffsets()- hook.completedAt = Date.now()+ const consumerA = makeConsumer(groupId, topic, record('A', true), {+ autoOffsetStore: false,+ onPartitionsRevoked: () => {+ if (++hook.invocations === 1) {+ hook.storedAtRevoke = flushOffsets()+ hook.completedAt = Date.now()+ }+ return Promise.resolve()+ },+ })+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)++ // Cycle 1: consumed, then flushed normally (the periodic size/age flush) — the+ // stored offsets reach the broker via the auto-commit timer.+ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ await waitFor(() => ledger.length >= wave1.length, 8_000)+ const flush1 = flushOffsets()+ const mark1 = new Map(flush1.map(({ partition, offset }) => [partition, offset]))+ for (const partition of [0, 1]) {+ await waitForAsync(+ async () =>+ (await fetchCommittedOffset(groupId, topic, partition)) === mark1.get(partition),+ 10_000+ )}- return Promise.resolve()- },- })- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)-- // Cycle 1: consumed, then flushed normally (the periodic size/age flush) — the- // stored offsets reach the broker via the auto-commit timer.- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- await waitFor(() => ledger.length >= wave1.length, 8_000)- const flush1 = flushOffsets()- const mark1 = new Map(flush1.map(({ partition, offset }) => [partition, offset]))- for (const partition of [0, 1]) {- await waitForAsync(- async () => (await fetchCommittedOffset(groupId, topic, partition)) === mark1.get(partition),- 10_000++ // Cycle 2: in flight at the revoke — produced to partition 0 only, so the flush the+ // revoke hook runs must advance partition 0 and leave partition 1 exactly at its+ // cycle-1 mark (tracked state was cleared by the first flush).+ const wave2 = await produceTracked(+ producer,+ topic,+ Array.from({ length: 3 }, (_, i) => ({ key: `a${3 + i}`, partition: 0 })))+ await waitFor(() => wave2.every((r) => ledger.some((e) => e.value === r.value)), 8_000)+ const mark2 = Math.max(...wave2.map((r) => r.offset)) + 1++ consumerB = makeConsumer(groupId, topic, record('B', false), { autoOffsetStore: false })+ const b = consumerB+ await waitFor(() => hook.completedAt > 0, 15_000)++ // The revoke flush stored exactly the in-flight cycle: partition 0 at its new mark,+ // nothing for partition 1.+ expect(hook.storedAtRevoke).toEqual([{ partition: 0, offset: mark2 }])++ await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 15_000)++ // Commits advance, never rewind: partition 0 moves from mark1 to mark2 (the unassign+ // or timer commits the newer store over the earlier cycle-1 commit), partition 1+ // stays exactly at mark1.+ await waitForAsync(async () => (await fetchCommittedOffset(groupId, topic, 0)) === mark2, 15_000)+ expect(await fetchCommittedOffset(groupId, topic, 1)).toBe(mark1.get(1))++ // Resumption is exact on both partitions: a third wave lands contiguously and every+ // message across all three cycles was delivered exactly once — neither flushed cycle+ // is ever reprocessed, regardless of which partition moved.+ const wave3 = await produceTracked(producer, topic, [+ { key: 'a-final', partition: 0 },+ { key: 'b-final', partition: 1 },+ ])+ expect(Math.min(...wave3.filter((r) => r.partition === 0).map((r) => r.offset))).toBe(mark2)+ expect(Math.min(...wave3.filter((r) => r.partition === 1).map((r) => r.offset))).toBe(mark1.get(1))++ const produced = [...wave1, ...wave2, ...wave3]+ await waitFor(() => countByPartitionOffset(ledger).size >= produced.length, 15_000)+ expect(ledger.length).toBe(produced.length)+ const consumedAt = new Map(ledger.map((e) => [`${e.partition}:${e.offset}`, e.value]))+ const producedAt = new Map(produced.map((r) => [`${r.partition}:${r.offset}`, r.value]))+ expect(consumedAt).toEqual(producedAt)+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}-- // Cycle 2: in flight at the revoke — produced to partition 0 only, so the flush the- // revoke hook runs must advance partition 0 and leave partition 1 exactly at its- // cycle-1 mark (tracked state was cleared by the first flush).- const wave2 = await produceTracked(- producer,- topic,- Array.from({ length: 3 }, (_, i) => ({ key: `a${3 + i}`, partition: 0 }))- )- await waitFor(() => wave2.every((r) => ledger.some((e) => e.value === r.value)), 8_000)- const mark2 = Math.max(...wave2.map((r) => r.offset)) + 1-- consumerB = makeConsumer(groupId, topic, record('B', false), { autoOffsetStore: false })- const b = consumerB- await waitFor(() => hook.completedAt > 0, 15_000)-- // The revoke flush stored exactly the in-flight cycle: partition 0 at its new mark,- // nothing for partition 1.- expect(hook.storedAtRevoke).toEqual([{ partition: 0, offset: mark2 }])-- await waitFor(() => consumerA.assignments().length === 1 && b.assignments().length === 1, 15_000)-- // Commits advance, never rewind: partition 0 moves from mark1 to mark2 (the unassign- // or timer commits the newer store over the earlier cycle-1 commit), partition 1- // stays exactly at mark1.- await waitForAsync(async () => (await fetchCommittedOffset(groupId, topic, 0)) === mark2, 15_000)- expect(await fetchCommittedOffset(groupId, topic, 1)).toBe(mark1.get(1))-- // Resumption is exact on both partitions: a third wave lands contiguously and every- // message across all three cycles was delivered exactly once — neither flushed cycle- // is ever reprocessed, regardless of which partition moved.- const wave3 = await produceTracked(producer, topic, [- { key: 'a-final', partition: 0 },- { key: 'b-final', partition: 1 },- ])- expect(Math.min(...wave3.filter((r) => r.partition === 0).map((r) => r.offset))).toBe(mark2)- expect(Math.min(...wave3.filter((r) => r.partition === 1).map((r) => r.offset))).toBe(mark1.get(1))-- const produced = [...wave1, ...wave2, ...wave3]- await waitFor(() => countByPartitionOffset(ledger).size >= produced.length, 15_000)- expect(ledger.length).toBe(produced.length)- const consumedAt = new Map(ledger.map((e) => [`${e.partition}:${e.offset}`, e.value]))- const producedAt = new Map(produced.map((r) => [`${r.partition}:${r.offset}`, r.value]))- expect(consumedAt).toEqual(producedAt)- } finally {- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)}- })+ )// The flush-on-stop contract the session replay consumer's shutdown relies on:// stopConsuming() → final flush stores offsets → disconnect() commits them leaving the// group. The batch in flight when stopConsuming is called must settle before it resolves,// and nothing may be consumed after it — otherwise the final flush races live intake.- it('shutdown flush: stopConsuming drains in-flight work and halts intake, offsets stored before disconnect commit exactly', async () => {- const topic = `v2_int_reb_stop_${randomUUID()}`- const groupId = `v2-int-reb-stop-${randomUUID()}`- await createTopic(topic, 2)-- const ledger: LedgerEntry[] = []- // Replay-style offset manager: track next-to-process per partition; the shutdown- // flush stores everything tracked between stopConsuming and disconnect.- const tracked = new Map<number, number>()- // Once armed, batches don't settle until the test releases them — keeping a batch- // in flight across the stopConsuming call.- let gateArmed = false- let releaseGate: () => void = () => {}- const gate = new Promise<void>((resolve) => {- releaseGate = resolve- })-- const record = (consumerId: string, track: boolean) => (messages: Message[]) => {- for (const m of messages) {- ledger.push({- consumerId,- partition: m.partition,- offset: m.offset,- key: m.key?.toString() ?? '',- value: m.value?.toString() ?? '',- seenAt: Date.now(),- })- if (track) {- tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))+ it.concurrent(+ 'shutdown flush: stopConsuming drains in-flight work and halts intake, offsets stored before disconnect commit exactly',+ async () => {+ const topic = `v2_int_reb_stop_${randomUUID()}`+ const groupId = `v2-int-reb-stop-${randomUUID()}`+ await createTopic(topic, 2)++ const ledger: LedgerEntry[] = []+ // Replay-style offset manager: track next-to-process per partition; the shutdown+ // flush stores everything tracked between stopConsuming and disconnect.+ const tracked = new Map<number, number>()+ // Once armed, batches don't settle until the test releases them — keeping a batch+ // in flight across the stopConsuming call.+ let gateArmed = false+ let releaseGate: () => void = () => {}+ const gate = new Promise<void>((resolve) => {+ releaseGate = resolve+ })++ const record = (consumerId: string, track: boolean) => (messages: Message[]) => {+ for (const m of messages) {+ ledger.push({+ consumerId,+ partition: m.partition,+ offset: m.offset,+ key: m.key?.toString() ?? '',+ value: m.value?.toString() ?? '',+ seenAt: Date.now(),+ })+ if (track) {+ tracked.set(m.partition, Math.max(tracked.get(m.partition) ?? 0, m.offset + 1))+ }}}- }- const consumerA = makeConsumer(- groupId,- topic,- (messages) => {- record('A', true)(messages)- return Promise.resolve(gateArmed ? { backgroundTask: gate } : {})- },- { autoOffsetStore: false }- )- let consumerB: KafkaConsumerV2 | undefined-- try {- await waitFor(() => consumerA.assignments().length === 2, 10_000)-- const wave1 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),- ])- await waitFor(() => ledger.length >= wave1.length, 8_000)-- // One gated message: consumed, but its settle chain hangs on the gate, so its- // batch is still in flight when stopConsuming is called.- gateArmed = true- const gated = await produceTracked(producer, topic, [{ key: 'gated-p0', partition: 0 }])- await waitFor(() => ledger.some((e) => e.value === gated[0].value), 8_000)-- // stopConsuming must not resolve while the in-flight batch is pending — the- // drain guarantee the shutdown flush depends on.- let stopSettled = false- const stopPromise = consumerA.stopConsuming().then(() => {- stopSettled = true- })- await delay(1_500)- expect(stopSettled).toBe(false)- releaseGate()- await stopPromise-- // Intake is halted for good: messages produced after stopConsuming are never- // consumed by A, even though it is still connected and owns the partitions.- const wave2 = await produceTracked(producer, topic, [- ...Array.from({ length: 3 }, (_, i) => ({ key: `a${4 + i}`, partition: 0 })),- ...Array.from({ length: 3 }, (_, i) => ({ key: `b${3 + i}`, partition: 1 })),- ])- await delay(2_000)- expect(ledger.filter((e) => wave2.some((r) => r.value === e.value))).toEqual([])-- // The shutdown flush window: the store must succeed (the partitions are still- // owned), and disconnect commits it as the member leaves the group.- consumerA.offsetsStore(- [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))+ const consumerA = makeConsumer(+ groupId,+ topic,+ (messages) => {+ record('A', true)(messages)+ return Promise.resolve(gateArmed ? { backgroundTask: gate } : {})+ },+ { autoOffsetStore: false })- await consumerA.disconnect()+ let consumerB: KafkaConsumerV2 | undefined++ try {+ await waitFor(() => consumerA.assignments().length === 2, 10_000)++ const wave1 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${i}`, partition: 1 })),+ ])+ await waitFor(() => ledger.length >= wave1.length, 8_000)++ // One gated message: consumed, but its settle chain hangs on the gate, so its+ // batch is still in flight when stopConsuming is called.+ gateArmed = true+ const gated = await produceTracked(producer, topic, [{ key: 'gated-p0', partition: 0 }])+ await waitFor(() => ledger.some((e) => e.value === gated[0].value), 8_000)++ // stopConsuming must not resolve while the in-flight batch is pending — the+ // drain guarantee the shutdown flush depends on.+ let stopSettled = false+ const stopPromise = consumerA.stopConsuming().then(() => {+ stopSettled = true+ })+ await delay(1_500)+ expect(stopSettled).toBe(false)+ releaseGate()+ await stopPromise++ // Intake is halted for good: messages produced after stopConsuming are never+ // consumed by A, even though it is still connected and owns the partitions.+ const wave2 = await produceTracked(producer, topic, [+ ...Array.from({ length: 3 }, (_, i) => ({ key: `a${4 + i}`, partition: 0 })),+ ...Array.from({ length: 3 }, (_, i) => ({ key: `b${3 + i}`, partition: 1 })),+ ])+ await delay(2_000)+ expect(ledger.filter((e) => wave2.some((r) => r.value === e.value))).toEqual([])++ // The shutdown flush window: the store must succeed (the partitions are still+ // owned), and disconnect commits it as the member leaves the group.+ consumerA.offsetsStore(+ [...tracked.entries()].map(([partition, offset]) => ({ topic, partition, offset }))+ )+ await consumerA.disconnect()++ const expectedCommit = new Map(tracked)+ for (const partition of [0, 1]) {+ await waitForAsync(+ async () =>+ (await fetchCommittedOffset(groupId, topic, partition)) ===+ expectedCommit.get(partition),+ 10_000+ )+ }- const expectedCommit = new Map(tracked)- for (const partition of [0, 1]) {- await waitForAsync(- async () =>- (await fetchCommittedOffset(groupId, topic, partition)) === expectedCommit.get(partition),- 10_000+ // A restart resumes exactly after the shutdown flush: the new consumer sees+ // wave2 only — nothing the flush covered is redelivered — each message exactly+ // once at the offset its delivery report announced.+ consumerB = makeConsumer(groupId, topic, (msgs) => Promise.resolve(record('B', false)(msgs)), {+ autoOffsetStore: false,+ })+ await waitFor(+ () => wave2.every((r) => ledger.some((e) => e.consumerId === 'B' && e.value === r.value)),+ 15_000+ )+ const bEntries = ledger.filter((e) => e.consumerId === 'B')+ expect(bEntries).toHaveLength(wave2.length)+ expect(new Map(bEntries.map((e) => [`${e.partition}:${e.offset}`, e.value]))).toEqual(+ new Map(wave2.map((r) => [`${r.partition}:${r.offset}`, r.value])))+ } finally {+ await consumerA.disconnect()+ await consumerB?.disconnect()+ await deleteTopic(topic)}-- // A restart resumes exactly after the shutdown flush: the new consumer sees- // wave2 only — nothing the flush covered is redelivered — each message exactly- // once at the offset its delivery report announced.- consumerB = makeConsumer(groupId, topic, (msgs) => Promise.resolve(record('B', false)(msgs)), {- autoOffsetStore: false,- })- await waitFor(- () => wave2.every((r) => ledger.some((e) => e.consumerId === 'B' && e.value === r.value)),- 15_000- )- const bEntries = ledger.filter((e) => e.consumerId === 'B')- expect(bEntries).toHaveLength(wave2.length)- expect(new Map(bEntries.map((e) => [`${e.partition}:${e.offset}`, e.value]))).toEqual(- new Map(wave2.map((r) => [`${r.partition}:${r.offset}`, r.value]))- )- } finally {- await consumerA.disconnect()- await consumerB?.disconnect()- await deleteTopic(topic)}- })+ )})})diff --git a/nodejs/src/common/kafka/consumer/consumer-v2.ts b/nodejs/src/common/kafka/consumer/consumer-v2.tsindex 7280b0397fcc..7feaff1cf707 100644--- a/nodejs/src/common/kafka/consumer/consumer-v2.ts+++ b/nodejs/src/common/kafka/consumer/consumer-v2.ts@@ -55,6 +55,8 @@ export type KafkaConsumerV2Config = {autoCommit?: booleanenablePartitionEof?: booleanfetchBatchSize?: number+ maxBackgroundTasks?: number+ rebalanceTimeoutMs?: number}export type RdKafkaConsumerOverrides = Omit<@@ -139,9 +141,9 @@ export class KafkaConsumerV2 {this.fetchBatchSize = config.fetchBatchSize ?? defaultConfig.CONSUMER_BATCH_SIZEthis.batchTimeoutMs = this.config.batchTimeoutMs ?? DEFAULT_BATCH_TIMEOUT_MS- this.maxBackgroundTasks = defaultConfig.CONSUMER_MAX_BACKGROUND_TASKS+ this.maxBackgroundTasks = config.maxBackgroundTasks ?? defaultConfig.CONSUMER_MAX_BACKGROUND_TASKSthis.backgroundTaskTimeoutMs = defaultConfig.CONSUMER_BACKGROUND_TASK_TIMEOUT_MS- this.drainTimeoutMs = defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MS+ this.drainTimeoutMs = config.rebalanceTimeoutMs ?? defaultConfig.CONSUMER_REBALANCE_TIMEOUT_MSthis.loopStallThresholdMs = defaultConfig.CONSUMER_LOOP_STALL_THRESHOLD_MS || LOOP_STALL_THRESHOLD_MS_DEFAULTthis.logStatsLevel = defaultConfig.CONSUMER_LOG_STATS_LEVEL@@ -343,6 +345,41 @@ export class KafkaConsumerV2 {} finally {// Drain whatever is left before letting the caller's disconnect() return.await this.drainAll('shutdown')+ this.answerPendingRebalances()+ }+ }++ /**+ * Answer rebalance events that were queued but never handled because the loop stopped.+ * librdkafka blocks disconnect() until the application responds to a pending cooperative+ * rebalance, so leaving one unanswered hangs shutdown. Nothing polls any more, so an+ * assignment answered here delivers no messages and is released by the final REVOKE that+ * disconnect() routes through rebalanceCallback.+ */+ private answerPendingRebalances(): void {+ while (this.rebalanceQueue.length > 0) {+ const event = this.rebalanceQueue.shift()!+ if (event.type === 'ERROR') {+ continue+ }+ try {+ if (this.rdKafkaConsumer.rebalanceProtocol() === 'COOPERATIVE') {+ if (event.type === 'ASSIGN') {+ this.rdKafkaConsumer.incrementalAssign(event.partitions)+ } else {+ this.rdKafkaConsumer.incrementalUnassign(event.partitions)+ }+ } else if (event.type === 'ASSIGN') {+ this.rdKafkaConsumer.assign(event.partitions)+ } else {+ this.rdKafkaConsumer.unassign()+ }+ } catch (error) {+ logger.warn('🔁', 'kafka_consumer_v2_pending_rebalance_answer_failed', {+ type: event.type,+ error: String(error),+ })+ }}}