Skip to content

KAFKA-8347: Choose next record to process by timestamp #6719

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

Merged
merged 4 commits into from
May 16, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class RecordQueue {
private final RecordDeserializer recordDeserializer;
private final ArrayDeque<ConsumerRecord<byte[], byte[]>> fifoQueue;

private long partitionTime = UNKNOWN;
private StampedRecord headRecord = null;

RecordQueue(final TopicPartition partition,
Expand Down Expand Up @@ -98,7 +97,7 @@ int addRawRecords(final Iterable<ConsumerRecord<byte[], byte[]>> rawRecords) {
fifoQueue.addLast(rawRecord);
}

maybeUpdateTimestamp();
updateHead();

return size();
}
Expand All @@ -112,7 +111,7 @@ public StampedRecord poll() {
final StampedRecord recordToReturn = headRecord;
headRecord = null;

maybeUpdateTimestamp();
updateHead();

return recordToReturn;
}
Expand Down Expand Up @@ -142,7 +141,7 @@ public boolean isEmpty() {
* @return timestamp
*/
public long timestamp() {
return partitionTime;
return headRecord == null ? UNKNOWN : headRecord.timestamp;
}

/**
Expand All @@ -151,10 +150,9 @@ public long timestamp() {
public void clear() {
fifoQueue.clear();
headRecord = null;
partitionTime = UNKNOWN;
}

private void maybeUpdateTimestamp() {
private void updateHead() {
while (headRecord == null && !fifoQueue.isEmpty()) {
final ConsumerRecord<byte[], byte[]> raw = fifoQueue.pollFirst();
final ConsumerRecord<Object, Object> deserialized = recordDeserializer.deserialize(processorContext, raw);
Expand All @@ -166,7 +164,7 @@ private void maybeUpdateTimestamp() {

final long timestamp;
try {
timestamp = timestampExtractor.extract(deserialized, partitionTime);
timestamp = timestampExtractor.extract(deserialized, timestamp());
} catch (final StreamsException internalFatalExtractorException) {
throw internalFatalExtractorException;
} catch (final Exception fatalUserException) {
Expand All @@ -187,11 +185,6 @@ private void maybeUpdateTimestamp() {
}

headRecord = new StampedRecord(deserialized, timestamp);

// update the partition timestamp if the current head record's timestamp has exceed its value
if (timestamp > partitionTime) {
partitionTime = timestamp;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void testTimeTracking() {
record = group.nextRecord(info);
// 1:[3, 5]
// 2:[2, 4, 6]
// st: 2
// st: 1
assertEquals(partition1, info.partition());
verifyTimes(record, 1L, 1L);
verifyBuffered(5, 2, 3);
Expand All @@ -127,7 +127,7 @@ record = group.nextRecord(info);
record = group.nextRecord(info);
// 1:[3, 5]
// 2:[4, 6]
// st: 3
// st: 2
assertEquals(partition2, info.partition());
verifyTimes(record, 2L, 2L);
verifyBuffered(4, 2, 2);
Expand All @@ -141,32 +141,32 @@ record = group.nextRecord(info);
group.addRawRecords(partition1, list3);
// 1:[3, 5, 2, 4]
// 2:[4, 6]
// st: 3 (non-decreasing, so adding 2 doesn't change it)
// st: 2 (just adding records shouldn't change it)
verifyBuffered(6, 4, 2);
assertEquals(2L, group.timestamp());
assertEquals(0.0, metrics.metric(lastLatenessValue).metricValue());

// get one record, time should not be advanced
// get one record, time should be advanced
record = group.nextRecord(info);
// 1:[5, 2, 4]
// 2:[4, 6]
// st: 4 as partition st is now {5, 4}
// st: 3
assertEquals(partition1, info.partition());
verifyTimes(record, 3L, 3L);
verifyBuffered(5, 3, 2);
assertEquals(0.0, metrics.metric(lastLatenessValue).metricValue());

// get one record, time should not be advanced
// get one record, time should be advanced
record = group.nextRecord(info);
// 1:[5, 2, 4]
// 2:[6]
// st: 5 as partition st is now {5, 6}
// st: 4
assertEquals(partition2, info.partition());
verifyTimes(record, 4L, 4L);
verifyBuffered(4, 3, 1);
assertEquals(0.0, metrics.metric(lastLatenessValue).metricValue());

// get one more record, now time should be advanced
// get one more record, time should be advanced
record = group.nextRecord(info);
// 1:[2, 4]
// 2:[6]
Expand All @@ -190,24 +190,72 @@ record = group.nextRecord(info);
record = group.nextRecord(info);
// 1:[]
// 2:[6]
// st: 4 (doesn't advance because 1 is empty, so it's still reporting the last-known time of 4)
// st: 5
assertEquals(partition1, info.partition());
verifyTimes(record, 4L, 5L);
verifyBuffered(1, 0, 1);
assertEquals(1.0, metrics.metric(lastLatenessValue).metricValue());

// get one more record, time should not be advanced
// get one more record, time should be advanced
record = group.nextRecord(info);
// 1:[]
// 2:[]
// st: 4 (1 and 2 are empty, so they are still reporting the last-known times of 4 and 6.)
// st: 6
assertEquals(partition2, info.partition());
verifyTimes(record, 6L, 6L);
verifyBuffered(0, 0, 0);
assertEquals(0.0, metrics.metric(lastLatenessValue).metricValue());

}

@Test
public void shouldChooseNextRecordBasedOnHeadTimestamp() {
assertEquals(0, group.numBuffered());

// add three 3 records with timestamp 1, 5, 3 to partition-1
final List<ConsumerRecord<byte[], byte[]>> list1 = Arrays.asList(
new ConsumerRecord<>("topic", 1, 1L, recordKey, recordValue),
new ConsumerRecord<>("topic", 1, 5L, recordKey, recordValue),
new ConsumerRecord<>("topic", 1, 3L, recordKey, recordValue));

group.addRawRecords(partition1, list1);

verifyBuffered(3, 3, 0);
assertEquals(-1L, group.timestamp());
assertEquals(0.0, metrics.metric(lastLatenessValue).metricValue());

StampedRecord record;
final PartitionGroup.RecordInfo info = new PartitionGroup.RecordInfo();

// get first two records from partition 1
record = group.nextRecord(info);
assertEquals(record.timestamp, 1L);
record = group.nextRecord(info);
assertEquals(record.timestamp, 5L);

// add three 3 records with timestamp 2, 4, 6 to partition-2
final List<ConsumerRecord<byte[], byte[]>> list2 = Arrays.asList(
new ConsumerRecord<>("topic", 2, 2L, recordKey, recordValue),
new ConsumerRecord<>("topic", 2, 4L, recordKey, recordValue),
new ConsumerRecord<>("topic", 2, 6L, recordKey, recordValue));

group.addRawRecords(partition2, list2);
// 1:[3]
// 2:[2, 4, 6]

// get one record, next record should be ts=2 from partition 2
record = group.nextRecord(info);
// 1:[3]
// 2:[4, 6]
assertEquals(record.timestamp, 2L);

// get one record, next up should have ts=3 from partition 1 (even though it has seen a larger max timestamp =5)
record = group.nextRecord(info);
// 1:[]
// 2:[4, 6]
assertEquals(record.timestamp, 3L);
}

private void verifyTimes(final StampedRecord record, final long recordTime, final long streamTime) {
assertEquals(recordTime, record.timestamp);
assertEquals(streamTime, group.timestamp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void testTimeTracking() {
// poll the first record, now with 1, 3
assertEquals(2L, queue.poll().timestamp);
assertEquals(2, queue.size());
assertEquals(2L, queue.timestamp());
assertEquals(1L, queue.timestamp());

// poll the second record, now with 3
assertEquals(1L, queue.poll().timestamp);
Expand All @@ -143,15 +143,15 @@ public void testTimeTracking() {

// poll the rest records
assertEquals(4L, queue.poll().timestamp);
assertEquals(4L, queue.timestamp());
assertEquals(1L, queue.timestamp());

assertEquals(1L, queue.poll().timestamp);
assertEquals(4L, queue.timestamp());
assertEquals(2L, queue.timestamp());

assertEquals(2L, queue.poll().timestamp);
assertTrue(queue.isEmpty());
assertEquals(0, queue.size());
assertEquals(4L, queue.timestamp());
assertEquals(RecordQueue.UNKNOWN, queue.timestamp());

// add three more records with 4, 5, 6
final List<ConsumerRecord<byte[], byte[]>> list3 = Arrays.asList(
Expand Down