hachikuji commented on a change in pull request #9630:
URL: https://github.com/apache/kafka/pull/9630#discussion_r533539812



##########
File path: core/src/main/scala/kafka/server/ReplicaManager.scala
##########
@@ -1862,23 +1864,51 @@ class ReplicaManager(val config: KafkaConfig,
     }
   }
 
-  def lastOffsetForLeaderEpoch(requestedEpochInfo: Map[TopicPartition, 
OffsetsForLeaderEpochRequest.PartitionData]): Map[TopicPartition, 
EpochEndOffset] = {
-    requestedEpochInfo.map { case (tp, partitionData) =>
-      val epochEndOffset = getPartition(tp) match {
-        case HostedPartition.Online(partition) =>
-          partition.lastOffsetForLeaderEpoch(partitionData.currentLeaderEpoch, 
partitionData.leaderEpoch,
-            fetchOnlyFromLeader = true)
-
-        case HostedPartition.Offline =>
-          new EpochEndOffset(Errors.KAFKA_STORAGE_ERROR, UNDEFINED_EPOCH, 
UNDEFINED_EPOCH_OFFSET)
-
-        case HostedPartition.None if metadataCache.contains(tp) =>
-          new EpochEndOffset(Errors.NOT_LEADER_OR_FOLLOWER, UNDEFINED_EPOCH, 
UNDEFINED_EPOCH_OFFSET)
-
-        case HostedPartition.None =>
-          new EpochEndOffset(Errors.UNKNOWN_TOPIC_OR_PARTITION, 
UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET)
+  def lastOffsetForLeaderEpoch(
+    requestedEpochInfo: Seq[OffsetForLeaderTopic]
+  ): Seq[OffsetForLeaderTopicResult] = {
+    requestedEpochInfo.map { offsetForLeaderTopic =>
+      val partitions = offsetForLeaderTopic.partitions.asScala.map { 
offsetForLeaderPartition =>
+        val tp = new TopicPartition(offsetForLeaderTopic.topic, 
offsetForLeaderPartition.partition)
+        getPartition(tp) match {
+          case HostedPartition.Online(partition) =>
+            val currentLeaderEpochOpt =
+              if (offsetForLeaderPartition.currentLeaderEpoch == 
RecordBatch.NO_PARTITION_LEADER_EPOCH)
+                Optional.empty[Integer]
+              else
+                
Optional.of[Integer](offsetForLeaderPartition.currentLeaderEpoch)
+
+            partition.lastOffsetForLeaderEpoch(
+              currentLeaderEpochOpt,
+              offsetForLeaderPartition.leaderEpoch,
+              fetchOnlyFromLeader = true)
+
+          case HostedPartition.Offline =>
+            new OffsetForLeaderPartitionResult()
+              .setPartition(offsetForLeaderPartition.partition)
+              .setErrorCode(Errors.KAFKA_STORAGE_ERROR.code)
+              .setLeaderEpoch(UNDEFINED_EPOCH)

Review comment:
       I'm wondering if it is reasonable to rely on defaults for some of these. 
I guess there's value in being explicit, but it is a tad vexing to see the same 
code repeated for a few of these cases.

##########
File path: 
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetcherTest.java
##########
@@ -3727,15 +3733,24 @@ public void testOffsetValidationRequestGrouping() {
             assertTrue(expectedPartitions.size() > 0);
             allRequestedPartitions.addAll(expectedPartitions);
 
-            Map<TopicPartition, EpochEndOffset> endOffsets = 
expectedPartitions.stream().collect(Collectors.toMap(
-                Function.identity(),
-                tp -> new EpochEndOffset(Errors.NONE, 4, 0)
-            ));
+            OffsetForLeaderEpochResponseData data = new 
OffsetForLeaderEpochResponseData();
+            expectedPartitions.forEach(tp -> {
+                OffsetForLeaderTopicResult topic = 
data.topics().find(tp.topic());
+                if (topic == null) {

Review comment:
       Not for this patch, but all of this boilerplate we need to build the 
topic groupings gets annoying. It is such a common case that it might be worth 
writing a special type that lets the parser construct `Map<TopicPartition, 
Data>` directly since that is really what the code wants. Alternatively, maybe 
we could flatten the schemas and introduce compression.

##########
File path: 
clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetcherTest.java
##########
@@ -3727,15 +3733,24 @@ public void testOffsetValidationRequestGrouping() {
             assertTrue(expectedPartitions.size() > 0);
             allRequestedPartitions.addAll(expectedPartitions);
 
-            Map<TopicPartition, EpochEndOffset> endOffsets = 
expectedPartitions.stream().collect(Collectors.toMap(
-                Function.identity(),
-                tp -> new EpochEndOffset(Errors.NONE, 4, 0)
-            ));
+            OffsetForLeaderEpochResponseData data = new 
OffsetForLeaderEpochResponseData();
+            expectedPartitions.forEach(tp -> {
+                OffsetForLeaderTopicResult topic = 
data.topics().find(tp.topic());
+                if (topic == null) {
+                    topic = new 
OffsetForLeaderTopicResult().setTopic(tp.topic());
+                    data.topics().add(topic);
+                }
+                topic.partitions().add(new OffsetForLeaderPartitionResult()

Review comment:
       Maybe just me, but `OffsetForLeaderPartitionResult` seems more 
cumbersome and less descriptive than `EpochEndOffset`. Would it be worth 
changing the name in the generated schema? 




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to