MINOR: Call logSegments.toBuffer only when required
Reviewers: Ismael Juma Satish Duggana Chia-Ping Tsai
@@ -1648,9 +1648,6 @@ class Log(@volatile private var _dir: File,
s"for partition $topicPartition is ${config.messageFormatVersion} which is earlier than the minimum " +
s"required version $KAFKA_0_10_0_IV0")
- // Cache to avoid race conditions. `toBuffer` is faster than most alternatives and provides
- // constant time access while being safe to use with concurrent collections unlike `toArray`.
- val segmentsCopy = logSegments.toBuffer
// For the earliest and latest, we do not need to return the timestamp.
if (targetTimestamp == ListOffsetsRequest.EARLIEST_TIMESTAMP) {
// The first cached epoch usually corresponds to the log start offset, but we have to verify this since
@@ -1667,6 +1664,9 @@ class Log(@volatile private var _dir: File,
val epochOptional = Optional.ofNullable(latestEpochOpt.orNull)
Some(new TimestampAndOffset(RecordBatch.NO_TIMESTAMP, logEndOffset, epochOptional))
} else {
+ // Cache to avoid race conditions. `toBuffer` is faster than most alternatives and provides
+ // constant time access while being safe to use with concurrent collections unlike `toArray`.
+ val segmentsCopy = logSegments.toBuffer
// We need to search the first segment whose largest timestamp is >= the target timestamp if there is one.
val targetSeg = segmentsCopy.find(_.largestTimestamp >= targetTimestamp)
targetSeg.flatMap(_.findOffsetByTimestamp(targetTimestamp, logStartOffset))
