Skip to content

Commit

Permalink
Merge pull request #1012 from tulios/dont-commit-on-consumer-seek
Browse files Browse the repository at this point in the history
Dont commit on consumer seek
  • Loading branch information
Nevon authored Feb 1, 2021
2 parents addcd6f + 18c78d1 commit 76ec563
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/Consuming.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ consumer.seek({ topic: 'example', partition: 0, offset: 12384 })

Upon seeking to an offset, any messages in active batches are marked as stale and discarded, making sure the next message read for the partition is from the offset sought to. Make sure to check `isStale()` before processing a message using [the `eachBatch` interface](#each-batch) of `consumer.run`.

By default, the consumer will commit the offset seeked. To disable this, set the [`autoCommit`](#auto-commit) option to `false` on the consumer.

```javascript
consumer.run({
autoCommit: false,
eachMessage: async ({ topic, message }) => true
})
// This will now only resolve the previous offset, not commit it
consumer.seek({ topic: 'example', partition: 0, offset: 12384 })
```

## <a name="custom-partition-assigner"></a> Custom partition assigner

It's possible to configure the strategy the consumer will use to distribute partitions amongst the consumer group. KafkaJS has a round robin assigner configured by default.
Expand Down
63 changes: 63 additions & 0 deletions src/consumer/__tests__/seek.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const createAdmin = require('../../admin')
const createProducer = require('../../producer')
const createConsumer = require('../index')
const { KafkaJSNonRetriableError } = require('../../errors')
Expand Down Expand Up @@ -163,5 +164,67 @@ describe('Consumer', () => {
},
])
})

describe('When "autoCommit" is false', () => {
let admin

beforeEach(() => {
admin = createAdmin({ logger: newLogger(), cluster })
})

afterEach(async () => {
admin && (await admin.disconnect())
})

it('should not commit the offset', async () => {
await Promise.all([consumer, producer, admin].map(client => client.connect()))

await producer.send({
acks: 1,
topic: topicName,
messages: [1, 2, 3].map(n => ({ key: `key-${n}`, value: `value-${n}` })),
})
await consumer.subscribe({ topic: topicName, fromBeginning: true })

let messagesConsumed = []
consumer.run({
autoCommit: false,
eachMessage: async event => messagesConsumed.push(event),
})
consumer.seek({ topic: topicName, partition: 0, offset: 2 })

await waitForConsumerToJoinGroup(consumer)
await expect(waitForMessages(messagesConsumed, { number: 1 })).resolves.toEqual([
{
topic: topicName,
partition: 0,
message: expect.objectContaining({ offset: '2' }),
},
])

await expect(admin.fetchOffsets({ groupId, topic: topicName })).resolves.toEqual([
expect.objectContaining({
partition: 0,
offset: '-1',
}),
])

messagesConsumed = []
consumer.seek({ topic: topicName, partition: 0, offset: 1 })

await expect(waitForMessages(messagesConsumed, { number: 2 })).resolves.toEqual([
{
topic: topicName,
partition: 0,
message: expect.objectContaining({ offset: '1' }),
},
{
topic: topicName,
partition: 0,
message: expect.objectContaining({ offset: '2' }),
},
])
})
})
})
})
3 changes: 3 additions & 0 deletions src/consumer/consumerGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = class ConsumerGroup {
minBytes,
maxBytes,
maxWaitTimeInMs,
autoCommit,
autoCommitInterval,
autoCommitThreshold,
isolationLevel,
Expand All @@ -77,6 +78,7 @@ module.exports = class ConsumerGroup {
this.minBytes = minBytes
this.maxBytes = maxBytes
this.maxWaitTime = maxWaitTimeInMs
this.autoCommit = autoCommit
this.autoCommitInterval = autoCommitInterval
this.autoCommitThreshold = autoCommitThreshold
this.isolationLevel = isolationLevel
Expand Down Expand Up @@ -274,6 +276,7 @@ module.exports = class ConsumerGroup {
}),
{}
),
autoCommit: this.autoCommit,
autoCommitInterval: this.autoCommitInterval,
autoCommitThreshold: this.autoCommitThreshold,
coordinator,
Expand Down
4 changes: 3 additions & 1 deletion src/consumer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = ({
)
}

const createConsumerGroup = ({ autoCommitInterval, autoCommitThreshold }) => {
const createConsumerGroup = ({ autoCommit, autoCommitInterval, autoCommitThreshold }) => {
return new ConsumerGroup({
logger: rootLogger,
topics: keys(topics),
Expand All @@ -98,6 +98,7 @@ module.exports = ({
maxBytes,
maxWaitTimeInMs,
instrumentationEmitter,
autoCommit,
autoCommitInterval,
autoCommitThreshold,
isolationLevel,
Expand Down Expand Up @@ -218,6 +219,7 @@ module.exports = ({
}

consumerGroup = createConsumerGroup({
autoCommit,
autoCommitInterval,
autoCommitThreshold,
})
Expand Down
14 changes: 14 additions & 0 deletions src/consumer/offsetManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = class OffsetManager {
* @param {import("../../../types").Cluster} options.cluster
* @param {import("../../../types").Broker} options.coordinator
* @param {import("../../../types").IMemberAssignment} options.memberAssignment
* @param {boolean} options.autoCommit
* @param {number | null} options.autoCommitInterval
* @param {number | null} options.autoCommitThreshold
* @param {{[topic: string]: { fromBeginning: boolean }}} options.topicConfigurations
Expand All @@ -30,6 +31,7 @@ module.exports = class OffsetManager {
cluster,
coordinator,
memberAssignment,
autoCommit,
autoCommitInterval,
autoCommitThreshold,
topicConfigurations,
Expand All @@ -54,6 +56,7 @@ module.exports = class OffsetManager {
this.generationId = generationId
this.memberId = memberId

this.autoCommit = autoCommit
this.autoCommitInterval = autoCommitInterval
this.autoCommitThreshold = autoCommitThreshold
this.lastCommit = Date.now()
Expand Down Expand Up @@ -166,6 +169,17 @@ module.exports = class OffsetManager {
return
}

if (!this.autoCommit) {
this.resolveOffset({
topic,
partition,
offset: Long.fromValue(offset)
.subtract(1)
.toString(),
})
return
}

const { groupId, generationId, memberId } = this
const coordinator = await this.getCoordinator()

Expand Down
2 changes: 1 addition & 1 deletion src/consumer/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ module.exports = class Runner extends EventEmitter {

this.consumerGroup.resolveOffset({ topic, partition, offset: message.offset })
await this.consumerGroup.heartbeat({ interval: this.heartbeatInterval })
await this.consumerGroup.commitOffsetsIfNecessary()
await this.autoCommitOffsetsIfNecessary()
}
}

Expand Down

0 comments on commit 76ec563

Please sign in to comment.