-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-12981 Ensure LogSegment.maxTimestampSoFar and LogSegment.offsetOfMaxTimestampSoFar are read/updated in sync #10960
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
dajac
merged 10 commits into
apache:trunk
from
thomaskwscott:KAFKA-12981_maxTimestampOffset_consistent
Jul 6, 2021
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9c7a7c0
KAFKA-12981 refactor LogSegment.offsetOfMaxTimestampSoFar and LogSegm…
thomaskwscott 861a329
KAFKA-12981 refactored tuple to case class
thomaskwscott c805b84
KAFKA-12981 refactor to use existing TimestampOffset over new case class
thomaskwscott 8614827
KAFKA-12981 fixes per pr review
thomaskwscott 4174b34
KAFKA-12981 fixes per pr review
thomaskwscott 97df12f
KAFKA-12981 fixes per pr review
thomaskwscott b26d763
KAFKA-12981 fixes per pr review
thomaskwscott 1ad2126
Trigger Build
thomaskwscott 33779c6
KAFKA-12981 fixes per pr review
thomaskwscott 9bb34bf
KAFKA-12981 fixes per pr review
thomaskwscott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,21 +99,22 @@ class LogSegment private[log] (val log: FileRecords, | |
| // volatile for LogCleaner to see the update | ||
| @volatile private var rollingBasedTimestamp: Option[Long] = None | ||
|
|
||
| /* The maximum timestamp and offset we see so far */ | ||
| @volatile private var _maxTimestampAndOffsetSoFar: TimestampOffset = TimestampOffset.Unknown | ||
| def maxTimestampAndOffsetSoFar_= (timestampOffset: TimestampOffset) : Unit = _maxTimestampAndOffsetSoFar = timestampOffset | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There is an extra space before
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed this one and found a couple more. |
||
| def maxTimestampAndOffsetSoFar: TimestampOffset = { | ||
| if (_maxTimestampAndOffsetSoFar == TimestampOffset.Unknown) | ||
| _maxTimestampAndOffsetSoFar = timeIndex.lastEntry | ||
| _maxTimestampAndOffsetSoFar | ||
| } | ||
|
|
||
| /* The maximum timestamp we see so far */ | ||
| @volatile private var _maxTimestampSoFar: Option[Long] = None | ||
| def maxTimestampSoFar_=(timestamp: Long): Unit = _maxTimestampSoFar = Some(timestamp) | ||
| def maxTimestampSoFar: Long = { | ||
| if (_maxTimestampSoFar.isEmpty) | ||
| _maxTimestampSoFar = Some(timeIndex.lastEntry.timestamp) | ||
| _maxTimestampSoFar.get | ||
| maxTimestampAndOffsetSoFar.timestamp | ||
| } | ||
|
|
||
| @volatile private var _offsetOfMaxTimestampSoFar: Option[Long] = None | ||
| def offsetOfMaxTimestampSoFar_=(offset: Long): Unit = _offsetOfMaxTimestampSoFar = Some(offset) | ||
| def offsetOfMaxTimestampSoFar: Long = { | ||
| if (_offsetOfMaxTimestampSoFar.isEmpty) | ||
| _offsetOfMaxTimestampSoFar = Some(timeIndex.lastEntry.offset) | ||
| _offsetOfMaxTimestampSoFar.get | ||
| maxTimestampAndOffsetSoFar.offset | ||
| } | ||
|
|
||
| /* Return the size in bytes of this log segment */ | ||
|
|
@@ -158,8 +159,7 @@ class LogSegment private[log] (val log: FileRecords, | |
| trace(s"Appended $appendedBytes to ${log.file} at end offset $largestOffset") | ||
| // Update the in memory max timestamp and corresponding offset. | ||
| if (largestTimestamp > maxTimestampSoFar) { | ||
| maxTimestampSoFar = largestTimestamp | ||
| offsetOfMaxTimestampSoFar = shallowOffsetOfMaxTimestamp | ||
| maxTimestampAndOffsetSoFar = TimestampOffset(largestTimestamp, shallowOffsetOfMaxTimestamp) | ||
| } | ||
| // append an entry to the index (if needed) | ||
| if (bytesSinceLastIndexEntry > indexIntervalBytes) { | ||
|
|
@@ -338,16 +338,15 @@ class LogSegment private[log] (val log: FileRecords, | |
| txnIndex.reset() | ||
| var validBytes = 0 | ||
| var lastIndexEntry = 0 | ||
| maxTimestampSoFar = RecordBatch.NO_TIMESTAMP | ||
| _maxTimestampAndOffsetSoFar = TimestampOffset.Unknown | ||
|
thomaskwscott marked this conversation as resolved.
Outdated
|
||
| try { | ||
| for (batch <- log.batches.asScala) { | ||
| batch.ensureValid() | ||
| ensureOffsetInRange(batch.lastOffset) | ||
|
|
||
| // The max timestamp is exposed at the batch level, so no need to iterate the records | ||
| if (batch.maxTimestamp > maxTimestampSoFar) { | ||
| maxTimestampSoFar = batch.maxTimestamp | ||
| offsetOfMaxTimestampSoFar = batch.lastOffset | ||
| _maxTimestampAndOffsetSoFar = TimestampOffset(batch.maxTimestamp, batch.lastOffset) | ||
|
thomaskwscott marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // Build offset index | ||
|
|
@@ -386,15 +385,13 @@ class LogSegment private[log] (val log: FileRecords, | |
| private def loadLargestTimestamp(): Unit = { | ||
| // Get the last time index entry. If the time index is empty, it will return (-1, baseOffset) | ||
| val lastTimeIndexEntry = timeIndex.lastEntry | ||
| maxTimestampSoFar = lastTimeIndexEntry.timestamp | ||
| offsetOfMaxTimestampSoFar = lastTimeIndexEntry.offset | ||
| _maxTimestampAndOffsetSoFar = TimestampOffset(lastTimeIndexEntry.timestamp, lastTimeIndexEntry.offset) | ||
|
thomaskwscott marked this conversation as resolved.
Outdated
|
||
|
|
||
| val offsetPosition = offsetIndex.lookup(lastTimeIndexEntry.offset) | ||
| // Scan the rest of the messages to see if there is a larger timestamp after the last time index entry. | ||
| val maxTimestampOffsetAfterLastEntry = log.largestTimestampAfter(offsetPosition.position) | ||
| if (maxTimestampOffsetAfterLastEntry.timestamp > lastTimeIndexEntry.timestamp) { | ||
| maxTimestampSoFar = maxTimestampOffsetAfterLastEntry.timestamp | ||
| offsetOfMaxTimestampSoFar = maxTimestampOffsetAfterLastEntry.offset | ||
| _maxTimestampAndOffsetSoFar = TimestampOffset(maxTimestampOffsetAfterLastEntry.timestamp, maxTimestampOffsetAfterLastEntry.offset) | ||
|
thomaskwscott marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -584,7 +581,7 @@ class LogSegment private[log] (val log: FileRecords, | |
| * Close this log segment | ||
| */ | ||
| def close(): Unit = { | ||
| if (_maxTimestampSoFar.nonEmpty || _offsetOfMaxTimestampSoFar.nonEmpty) | ||
| if (_maxTimestampAndOffsetSoFar != TimestampOffset.Unknown) | ||
| CoreUtils.swallow(timeIndex.maybeAppend(maxTimestampSoFar, offsetOfMaxTimestampSoFar, | ||
| skipFullCheck = true), this) | ||
| CoreUtils.swallow(lazyOffsetIndex.close(), this) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this another instance of unnecessary copying? Are there others?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this is a different type (
TimestampAndOffset).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a check through and can't see any other copy instances