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

Support LOG_APPEND_TIME record timestamps #838

Merged
merged 3 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/broker/__tests__/fetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Broker > Fetch', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: 0,
producerId: '-1',
timestampType: 0,
...options,
})

Expand Down
1 change: 1 addition & 0 deletions src/protocol/recordBatch/fixtures/v0_recordbatch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"Buffer","data":[0,0,0,0,0,0,0,0,0,0,0,113,0,0,0,0,2,228,195,36,165,0,9,0,0,0,0,0,0,1,115,237,245,255,167,0,0,1,115,237,245,255,201,255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,1,31,139,8,0,0,0,0,0,0,19,11,99,96,96,224,202,78,173,212,53,112,40,75,204,41,77,213,53,208,53,50,48,50,208,53,176,208,53,52,9,49,52,183,50,52,182,50,176,208,51,179,48,138,98,0,0,181,227,241,167,44,0,0,0]}
19 changes: 15 additions & 4 deletions src/protocol/recordBatch/record/v0/decoder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Long = require('long')
const HeaderDecoder = require('../../header/v0/decoder')
const TimestampTypes = require('../../../timestampTypes')

/**
* v0
Expand All @@ -16,13 +17,23 @@ const HeaderDecoder = require('../../header/v0/decoder')
*/

module.exports = (decoder, batchContext = {}) => {
const { firstOffset, firstTimestamp, magicByte, isControlBatch = false } = batchContext
const {
firstOffset,
firstTimestamp,
magicByte,
isControlBatch = false,
timestampType,
maxTimestamp,
} = batchContext
const attributes = decoder.readInt8()

const timestampDelta = decoder.readVarLong()
const timestamp = Long.fromValue(firstTimestamp)
.add(timestampDelta)
.toString()
const timestamp =
timestampType === TimestampTypes.LOG_APPEND_TIME && maxTimestamp
? maxTimestamp
: Long.fromValue(firstTimestamp)
.add(timestampDelta)
.toString()

const offsetDelta = decoder.readVarInt()
const offset = Long.fromValue(firstOffset)
Expand Down
17 changes: 17 additions & 0 deletions src/protocol/recordBatch/record/v0/decoder.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Decoder = require('../../../decoder')
const recordDecoder = require('./decoder')
const TimestampTypes = require('../../../timestampTypes')

describe('Protocol > RecordBatch > Record > v0', () => {
test('decodes', async () => {
Expand Down Expand Up @@ -28,6 +29,22 @@ describe('Protocol > RecordBatch > Record > v0', () => {
})
})

test('uses record batch maxTimestamp when topic is configured with timestamp type LOG_APPEND_TIME', async () => {
const decoder = new Decoder(Buffer.from(require('../../fixtures/v0_record.json')))
const batchContext = {
firstOffset: '0',
firstTimestamp: '1509827900073',
magicByte: 2,
maxTimestamp: '1597425188809',
timestampType: TimestampTypes.LOG_APPEND_TIME,
}

const decoded = recordDecoder(decoder, batchContext)

expect(decoded.batchContext).toEqual(batchContext)
expect(decoded.timestamp).toEqual(batchContext.maxTimestamp)
})

test('decodes control record', async () => {
const decoder = new Decoder(Buffer.from(require('../../fixtures/v0_record.json')))

Expand Down
8 changes: 8 additions & 0 deletions src/protocol/recordBatch/v0/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const Decoder = require('../../decoder')
const { KafkaJSPartialMessageError } = require('../../../errors')
const { lookupCodecByRecordBatchAttributes } = require('../../message/compression')
const RecordDecoder = require('../record/v0/decoder')
const TimestampTypes = require('../../timestampTypes')

const TIMESTAMP_TYPE_FLAG_MASK = 0x8
const TRANSACTIONAL_FLAG_MASK = 0x10
const CONTROL_FLAG_MASK = 0x20

Expand Down Expand Up @@ -58,6 +60,11 @@ module.exports = async fetchDecoder => {

const inTransaction = (attributes & TRANSACTIONAL_FLAG_MASK) > 0
const isControlBatch = (attributes & CONTROL_FLAG_MASK) > 0
const timestampType =
(attributes & TIMESTAMP_TYPE_FLAG_MASK) > 0
? TimestampTypes.LOG_APPEND_TIME
: TimestampTypes.CREATE_TIME

const codec = lookupCodecByRecordBatchAttributes(attributes)

const recordContext = {
Expand All @@ -71,6 +78,7 @@ module.exports = async fetchDecoder => {
producerEpoch,
firstSequence,
maxTimestamp,
timestampType,
}

const records = await decodeRecords(codec, decoder, { ...recordContext, magicByte })
Expand Down
24 changes: 24 additions & 0 deletions src/protocol/recordBatch/v0/decoder.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Decoder = require('../../decoder')
const recordBatchDecoder = require('./decoder')

describe('Protocol > RecordBatch > v0', () => {
test('decodes', async () => {
const decoder = new Decoder(Buffer.from(require('../fixtures/v0_recordbatch.json')))
const decoded = await recordBatchDecoder(decoder)

expect(decoded).toEqual({
firstOffset: '0',
firstTimestamp: '1597425188775',
partitionLeaderEpoch: 0,
inTransaction: false,
isControlBatch: false,
lastOffsetDelta: 0,
producerId: '-1',
producerEpoch: 0,
firstSequence: 0,
maxTimestamp: '1597425188809',
timestampType: 1,
records: expect.any(Object),
})
})
})
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v10/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Protocol > Requests > Fetch > v10', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v11/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Protocol > Requests > Fetch > v11', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v4/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Protocol > Requests > Fetch > v4', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v5/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Protocol > Requests > Fetch > v5', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v6/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Protocol > Requests > Fetch > v6', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v7/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Protocol > Requests > Fetch > v7', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v8/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Protocol > Requests > Fetch > v8', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/protocol/requests/fetch/v9/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Protocol > Requests > Fetch > v9', () => {
partitionLeaderEpoch: expect.any(Number),
producerEpoch: expect.any(Number),
producerId: expect.any(String),
timestampType: expect.any(Number),
}

test('response', async () => {
Expand Down
15 changes: 15 additions & 0 deletions src/protocol/timestampTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Enum for timestamp types
* @readonly
* @enum {TimestampType}
*/
module.exports = {
// Timestamp type is unknown
NO_TIMESTAMP: -1,

// Timestamp relates to message creation time as set by a Kafka client
CREATE_TIME: 0,

// Timestamp relates to the time a message was appended to a Kafka log
LOG_APPEND_TIME: 1,
}