Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve offset commit handling #775

Merged
merged 7 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/consumer/offsetManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = class OffsetManager {
}

let offset = this.resolvedOffsets[topic][partition]
if (Long.fromValue(offset).equals(-1)) {
if (isInvalidOffset(offset)) {
offset = '0'
}

Expand Down Expand Up @@ -101,23 +101,22 @@ module.exports = class OffsetManager {
* @returns {Long}
*/
countResolvedOffsets() {
const toPartitions = topic => keys(this.resolvedOffsets[topic])
const committedOffsets = this.committedOffsets()

const subtractOffsets = (resolvedOffset, committedOffset) => {
const resolvedOffsetLong = Long.fromValue(resolvedOffset)
return committedOffset === '-1'
return isInvalidOffset(committedOffset)
? resolvedOffsetLong
: resolvedOffsetLong.subtract(Long.fromValue(committedOffset))
}

const subtractPartitionOffsets = (topic, partition) =>
subtractOffsets(
this.resolvedOffsets[topic][partition],
this.committedOffsets()[topic][partition]
const subtractPartitionOffsets = (resolvedTopicOffsets, committedTopicOffsets) =>
keys(resolvedTopicOffsets).map(partition =>
subtractOffsets(resolvedTopicOffsets[partition], committedTopicOffsets[partition])
)

const subtractTopicOffsets = topic =>
toPartitions(topic).map(partition => subtractPartitionOffsets(topic, partition))
subtractPartitionOffsets(this.resolvedOffsets[topic], committedOffsets[topic])

const offsetsDiff = this.topics.map(subtractTopicOffsets)
return flatten(offsetsDiff).reduce((sum, offset) => sum.add(offset), Long.fromValue(0))
Expand Down
3 changes: 1 addition & 2 deletions src/consumer/offsetManager/isInvalidOffset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Long = require('long')
const isNumber = number => /^-?\d+$/.test(number)

module.exports = offset => !isNumber(offset) || Long.fromValue(offset).compare(0) === -1
module.exports = offset => (!offset && offset !== 0) || Long.fromValue(offset).isNegative()