apoorvmittal10 commented on code in PR #24393:
URL: https://github.com/apache/kafka/pull/24393#discussion_r4698958736


##########
core/src/main/scala/kafka/server/KafkaApis.scala:
##########
@@ -3721,7 +3721,7 @@ class KafkaApis(val requestChannel: RequestChannel,
       request.context,
       READ,
       TOPIC,
-      topicIdPartitionSeq.asJava,
+      topicIdPartitionSeq.filter(_.topicPartition.topic != null).asJava,

Review Comment:
   Same as above.



##########
core/src/test/scala/unit/kafka/server/KafkaApisTest.scala:
##########
@@ -7560,6 +7613,68 @@ class KafkaApisTest extends Logging {
     assertEquals(Errors.GROUP_AUTHORIZATION_FAILED.code, 
responseData.errorCode)
   }
 
+  @Test
+  def testHandleShareAcknowledgeRequestUnknownTopicIdWithAuthorizer(): Unit = {
+    val topicName = "foo"
+    val topicId = Uuid.randomUuid()
+    val topicIdPartition = new TopicIdPartition(topicId, new 
TopicPartition(topicName, 0))
+    // Not added to the metadata cache, so the topic ID cannot be resolved to 
a name.
+    val unknownTopicId = Uuid.randomUuid()
+    metadataCache = initializeMetadataCacheWithShareGroupsEnabled()
+    addTopicToMetadataCache(topicName, 1, topicId = topicId)
+
+    doNothing().when(sharePartitionManager).acknowledgeSessionUpdate(any(), 
any(), any())
+    when(sharePartitionManager.acknowledge(any(), any(), any())).thenReturn(
+      CompletableFuture.completedFuture(util.Map.of[TopicIdPartition, 
ShareAcknowledgeResponseData.PartitionData](
+        topicIdPartition, new 
ShareAcknowledgeResponseData.PartitionData().setPartitionIndex(0). 
setErrorCode(Errors.NONE.code)

Review Comment:
   Nit: extra space in `. setErrorCode(...)`.



##########
core/src/main/scala/kafka/server/KafkaApis.scala:
##########
@@ -3395,7 +3395,7 @@ class KafkaApis(val requestChannel: RequestChannel,
       request.context,
       READ,
       TOPIC,
-      topicIdPartitionSeq.asJava,
+      topicIdPartitionSeq.filter(_.topicPartition.topic != null).asJava,

Review Comment:
   The filter looks good.
   
   nit: Just to confirm my understanding, partitions with an unresolved topic 
ID already get `UNKNOWN_TOPIC_ID` earlier, via `ErroneousAndValidPartitionData` 
for the initial epoch and via 
`ShareSessionContext.getErroneousAndValidTopicIdPartitions` for subsequent ones 
(which also covers partitions newly added by a later request through 
`ShareSession.update`), and piggybacked acks via 
`getAcknowledgeBatchesFromShareFetchRequest`. So the filter only keeps those 
null names out of the authorizer input, and their error responses are 
unaffected as they come from the `erroneous` maps. Is that correct? If so, a 
short comment above the filter saying this would help us next time.



##########
core/src/test/scala/unit/kafka/server/KafkaApisTest.scala:
##########
@@ -7332,6 +7332,59 @@ class KafkaApisTest extends Logging {
     verify(sharePartitionManager).newContext(any(), any(), any(), any(), 
any(), any(), any())
   }
 
+  @Test
+  def testHandleShareFetchRequestUnknownTopicIdWithAuthorizer(): Unit = {

Review Comment:
   Could we extend this test with two changes:
   
   1. **Add a known topic.** Register a known topic (e.g. "foo") in the 
metadata cache and include it in the request next to the unknown topic. Then 
assert that (a) the known partition is passed to 
`sharePartitionManager.fetchMessages`, (b) the unknown partition gets 
`UNKNOWN_TOPIC_ID`, and (c) the authorizer is called only with the known topic 
name. Right now only the unknown topic is present, so we do not check that the 
filter keeps authorizing known topics.
   2. **Add a second test for a subsequent request.** Here `newContext` is 
mocked to return `new ShareSessionContext(1, list)`, an initial context. Please 
add a test like the existing ones that mock `newContext` to return `new 
ShareSessionContext(1, new ShareSession(key, cachedSharePartitions, ...))` (see 
e.g. `testHandleShareFetchRequestInvalidRequestOnInitialEpoch`), with 
`cachedSharePartitions` holding the known topic partition plus a 
`CachedSharePartition` for an unknown topic ID with a null topic name, which is 
what `ShareSession.update` leaves in the session when a later request adds an 
unresolved topic. With an authorizer, assert that the known partition is 
fetched and the unknown one gets `UNKNOWN_TOPIC_ID` in the response.



##########
core/src/test/scala/unit/kafka/server/KafkaApisTest.scala:
##########
@@ -7560,6 +7613,68 @@ class KafkaApisTest extends Logging {
     assertEquals(Errors.GROUP_AUTHORIZATION_FAILED.code, 
responseData.errorCode)
   }
 
+  @Test
+  def testHandleShareAcknowledgeRequestUnknownTopicIdWithAuthorizer(): Unit = {
+    val topicName = "foo"
+    val topicId = Uuid.randomUuid()
+    val topicIdPartition = new TopicIdPartition(topicId, new 
TopicPartition(topicName, 0))
+    // Not added to the metadata cache, so the topic ID cannot be resolved to 
a name.
+    val unknownTopicId = Uuid.randomUuid()
+    metadataCache = initializeMetadataCacheWithShareGroupsEnabled()
+    addTopicToMetadataCache(topicName, 1, topicId = topicId)
+
+    doNothing().when(sharePartitionManager).acknowledgeSessionUpdate(any(), 
any(), any())
+    when(sharePartitionManager.acknowledge(any(), any(), any())).thenReturn(
+      CompletableFuture.completedFuture(util.Map.of[TopicIdPartition, 
ShareAcknowledgeResponseData.PartitionData](
+        topicIdPartition, new 
ShareAcknowledgeResponseData.PartitionData().setPartitionIndex(0). 
setErrorCode(Errors.NONE.code)
+      ))
+    )
+
+    def acknowledgeTopic(id: Uuid): 
ShareAcknowledgeRequestData.AcknowledgeTopic =
+      new ShareAcknowledgeRequestData.AcknowledgeTopic()
+        .setTopicId(id)
+        .setPartitions(new 
ShareAcknowledgeRequestData.AcknowledgePartitionCollection(util.List.of(
+          new ShareAcknowledgeRequestData.AcknowledgePartition()
+            .setPartitionIndex(0)
+            .setAcknowledgementBatches(util.List.of(
+              new ShareAcknowledgeRequestData.AcknowledgementBatch()
+                .setFirstOffset(0)
+                .setLastOffset(9)
+                .setAcknowledgeTypes(util.List.of(1.toByte))
+            ))
+        ).iterator))
+
+    val shareAcknowledgeRequestData = new ShareAcknowledgeRequestData()
+      .setGroupId("group")
+      .setMemberId(Uuid.randomUuid().toString)
+      .setShareSessionEpoch(1)
+      .setTopics(new ShareAcknowledgeRequestData.AcknowledgeTopicCollection(
+        util.List.of(acknowledgeTopic(topicId), 
acknowledgeTopic(unknownTopicId)
+      ).iterator))
+
+    // With an authorizer, AuthHelper builds a ResourcePattern per topic name.
+    val authorizer: Authorizer = mock(classOf[Authorizer])
+    when(authorizer.authorize(any(), any())).thenAnswer { invocation =>
+      invocation.getArgument[util.List[Action]](1).asScala.map(_ => 
AuthorizationResult.ALLOWED).asJava
+    }
+
+    val shareAcknowledgeRequest = new 
ShareAcknowledgeRequest.Builder(shareAcknowledgeRequestData).build(ApiKeys.SHARE_FETCH.latestVersion)

Review Comment:
   This builds a `ShareAcknowledgeRequest` with 
`ApiKeys.SHARE_FETCH.latestVersion`. Should it not be 
`ApiKeys.SHARE_ACKNOWLEDGE.latestVersion`, as the other share acknowledge tests 
use? It only works today because both APIs happen to share the same latest 
version, correct?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to