You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2020/10/25 16:40:51 UTC

[GitHub] [kafka] ijuma commented on a change in pull request #9162: MINOR: refactor Log to get rid of "return" in nested anonymous function

ijuma commented on a change in pull request #9162:
URL: https://github.com/apache/kafka/pull/9162#discussion_r511617718



##########
File path: core/src/main/scala/kafka/log/Log.scala
##########
@@ -1535,42 +1533,48 @@ class Log(@volatile private var _dir: File,
       }
 
       if (startOffset == maxOffsetMetadata.messageOffset) {
-        return emptyFetchDataInfo(maxOffsetMetadata, includeAbortedTxns)
+        emptyFetchDataInfo(maxOffsetMetadata, includeAbortedTxns)
       } else if (startOffset > maxOffsetMetadata.messageOffset) {
         val startOffsetMetadata = convertToOffsetMetadataOrThrow(startOffset)
-        return emptyFetchDataInfo(startOffsetMetadata, includeAbortedTxns)
-      }
+        emptyFetchDataInfo(startOffsetMetadata, includeAbortedTxns)
+      } else {
 
-      // Do the read on the segment with a base offset less than the target offset
-      // but if that segment doesn't contain any messages with an offset greater than that
-      // continue to read from successive segments until we get some messages or we reach the end of the log
-      while (segmentEntry != null) {
-        val segment = segmentEntry.getValue
+        // Do the read on the segment with a base offset less than the target offset
+        // but if that segment doesn't contain any messages with an offset greater than that
+        // continue to read from successive segments until we get some messages or we reach the end of the log
+        var done = segmentEntry == null
+        var fetchDataInfo: FetchDataInfo = null
+        while (!done) {
+          val segment = segmentEntry.getValue
+
+          val maxPosition = {
+            // Use the max offset position if it is on this segment; otherwise, the segment size is the limit.
+            if (maxOffsetMetadata.segmentBaseOffset == segment.baseOffset) {
+              maxOffsetMetadata.relativePositionInSegment
+            } else {
+              segment.size
+            }

Review comment:
       Nit: we can remove the braces from the if/else.

##########
File path: core/src/main/scala/kafka/log/Log.scala
##########
@@ -1535,42 +1533,48 @@ class Log(@volatile private var _dir: File,
       }
 
       if (startOffset == maxOffsetMetadata.messageOffset) {
-        return emptyFetchDataInfo(maxOffsetMetadata, includeAbortedTxns)
+        emptyFetchDataInfo(maxOffsetMetadata, includeAbortedTxns)
       } else if (startOffset > maxOffsetMetadata.messageOffset) {
         val startOffsetMetadata = convertToOffsetMetadataOrThrow(startOffset)
-        return emptyFetchDataInfo(startOffsetMetadata, includeAbortedTxns)
-      }
+        emptyFetchDataInfo(startOffsetMetadata, includeAbortedTxns)
+      } else {
 
-      // Do the read on the segment with a base offset less than the target offset
-      // but if that segment doesn't contain any messages with an offset greater than that
-      // continue to read from successive segments until we get some messages or we reach the end of the log
-      while (segmentEntry != null) {
-        val segment = segmentEntry.getValue
+        // Do the read on the segment with a base offset less than the target offset
+        // but if that segment doesn't contain any messages with an offset greater than that
+        // continue to read from successive segments until we get some messages or we reach the end of the log
+        var done = segmentEntry == null
+        var fetchDataInfo: FetchDataInfo = null
+        while (!done) {
+          val segment = segmentEntry.getValue
+
+          val maxPosition = {
+            // Use the max offset position if it is on this segment; otherwise, the segment size is the limit.
+            if (maxOffsetMetadata.segmentBaseOffset == segment.baseOffset) {
+              maxOffsetMetadata.relativePositionInSegment
+            } else {
+              segment.size
+            }
+          }
 
-        val maxPosition = {
-          // Use the max offset position if it is on this segment; otherwise, the segment size is the limit.
-          if (maxOffsetMetadata.segmentBaseOffset == segment.baseOffset) {
-            maxOffsetMetadata.relativePositionInSegment
+          val fetchInfo = segment.read(startOffset, maxLength, maxPosition, minOneMessage)

Review comment:
       Maybe something like the following may be clearer:
   
   ```scala
   fetchDataInfo = segment.read(startOffset, maxLength, maxPosition, minOneMessage)
   if (fetchDataInfo != null) {
     if (includeAbortedTxns)
       fetchDataInfo = addAbortedTransactions(startOffset, segmentEntry, fetchDataInfo)
   } else segmentEntry = segments.higherEntry(segmentEntry.getKey)
   
   done = fetchDataInfo != null || segmentEntry == null
   ```
   
   What do you think?

##########
File path: core/src/main/scala/kafka/log/Log.scala
##########
@@ -1535,42 +1533,48 @@ class Log(@volatile private var _dir: File,
       }
 
       if (startOffset == maxOffsetMetadata.messageOffset) {
-        return emptyFetchDataInfo(maxOffsetMetadata, includeAbortedTxns)
+        emptyFetchDataInfo(maxOffsetMetadata, includeAbortedTxns)
       } else if (startOffset > maxOffsetMetadata.messageOffset) {
         val startOffsetMetadata = convertToOffsetMetadataOrThrow(startOffset)
-        return emptyFetchDataInfo(startOffsetMetadata, includeAbortedTxns)
-      }
+        emptyFetchDataInfo(startOffsetMetadata, includeAbortedTxns)
+      } else {
 
-      // Do the read on the segment with a base offset less than the target offset
-      // but if that segment doesn't contain any messages with an offset greater than that
-      // continue to read from successive segments until we get some messages or we reach the end of the log
-      while (segmentEntry != null) {
-        val segment = segmentEntry.getValue
+        // Do the read on the segment with a base offset less than the target offset
+        // but if that segment doesn't contain any messages with an offset greater than that
+        // continue to read from successive segments until we get some messages or we reach the end of the log
+        var done = segmentEntry == null
+        var fetchDataInfo: FetchDataInfo = null
+        while (!done) {
+          val segment = segmentEntry.getValue
+
+          val maxPosition = {
+            // Use the max offset position if it is on this segment; otherwise, the segment size is the limit.
+            if (maxOffsetMetadata.segmentBaseOffset == segment.baseOffset) {
+              maxOffsetMetadata.relativePositionInSegment
+            } else {
+              segment.size
+            }
+          }
 
-        val maxPosition = {
-          // Use the max offset position if it is on this segment; otherwise, the segment size is the limit.
-          if (maxOffsetMetadata.segmentBaseOffset == segment.baseOffset) {
-            maxOffsetMetadata.relativePositionInSegment
+          val fetchInfo = segment.read(startOffset, maxLength, maxPosition, minOneMessage)
+          if (fetchInfo == null) {
+            segmentEntry = segments.higherEntry(segmentEntry.getKey)
+            done = segmentEntry == null
           } else {
-            segment.size
+            fetchDataInfo = if (includeAbortedTxns) addAbortedTransactions(startOffset, segmentEntry, fetchInfo)
+            else fetchInfo

Review comment:
       The formatting here is a bit weird (the else is aligned the same as the variable. I think it's easier to understand if the if/else are aligned and indented.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org