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 2021/02/01 21:14:06 UTC

[GitHub] [kafka] abbccdda opened a new pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

abbccdda opened a new pull request #10017:
URL: https://github.com/apache/kafka/pull/10017


   As mentioned in the ticket, returning `null` for `consumer#partitionsFor` is not a good client side agreement. Addressing this problem by returning the empty list instead.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


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



[GitHub] [kafka] abbccdda commented on a change in pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
abbccdda commented on a change in pull request #10017:
URL: https://github.com/apache/kafka/pull/10017#discussion_r568845018



##########
File path: clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
##########
@@ -1948,7 +1948,7 @@ public OffsetAndMetadata committed(TopicPartition partition, final Duration time
             Timer timer = time.timer(timeout);
             Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(
                     new MetadataRequest.Builder(Collections.singletonList(topic), metadata.allowAutoTopicCreation()), timer);
-            return topicMetadata.get(topic);
+            return topicMetadata.getOrDefault(topic, Collections.emptyList());

Review comment:
       I don't think you could have a topic with 0 partition.




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



[GitHub] [kafka] chia7712 commented on a change in pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10017:
URL: https://github.com/apache/kafka/pull/10017#discussion_r568403080



##########
File path: clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
##########
@@ -1948,7 +1948,7 @@ public OffsetAndMetadata committed(TopicPartition partition, final Duration time
             Timer timer = time.timer(timeout);
             Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(
                     new MetadataRequest.Builder(Collections.singletonList(topic), metadata.allowAutoTopicCreation()), timer);
-            return topicMetadata.get(topic);
+            return topicMetadata.getOrDefault(topic, Collections.emptyList());

Review comment:
       just curious.
   
   Is it possible that an existent topic has no partitions? If so, returning empty list may confuse users who querying nonexistent topic. Maybe we should throw ```UnknownTopicOrPartitionException``` for nonexistent topic and return empty list for existent topic.

##########
File path: clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
##########
@@ -1948,7 +1948,7 @@ public OffsetAndMetadata committed(TopicPartition partition, final Duration time
             Timer timer = time.timer(timeout);
             Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(
                     new MetadataRequest.Builder(Collections.singletonList(topic), metadata.allowAutoTopicCreation()), timer);
-            return topicMetadata.get(topic);
+            return topicMetadata.getOrDefault(topic, Collections.emptyList());

Review comment:
       BTW, it is worth writing docs for this case (return empty list).




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



[GitHub] [kafka] chia7712 commented on pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
chia7712 commented on pull request #10017:
URL: https://github.com/apache/kafka/pull/10017#issuecomment-774852045


   @abbccdda Thanks for updating code. Two minor comments are left.
   
   1. The docs of ```KafkaConsumer#partitionsFor(String topic, Duration timeout)``` is not updated. 
   1. ```MockConsumer``` still return null when `topic` is nonexistent. Should we make it return empty collection as well?


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



[GitHub] [kafka] abbccdda commented on a change in pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
abbccdda commented on a change in pull request #10017:
URL: https://github.com/apache/kafka/pull/10017#discussion_r568845018



##########
File path: clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
##########
@@ -1948,7 +1948,7 @@ public OffsetAndMetadata committed(TopicPartition partition, final Duration time
             Timer timer = time.timer(timeout);
             Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(
                     new MetadataRequest.Builder(Collections.singletonList(topic), metadata.allowAutoTopicCreation()), timer);
-            return topicMetadata.get(topic);
+            return topicMetadata.getOrDefault(topic, Collections.emptyList());

Review comment:
       I don't think you could have a topic with 0 partition.




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



[GitHub] [kafka] abbccdda merged pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
abbccdda merged pull request #10017:
URL: https://github.com/apache/kafka/pull/10017


   


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



[GitHub] [kafka] abbccdda commented on pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
abbccdda commented on pull request #10017:
URL: https://github.com/apache/kafka/pull/10017#issuecomment-775301979


   @chia7712 Sounds good


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



[GitHub] [kafka] chia7712 commented on a change in pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10017:
URL: https://github.com/apache/kafka/pull/10017#discussion_r568403080



##########
File path: clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
##########
@@ -1948,7 +1948,7 @@ public OffsetAndMetadata committed(TopicPartition partition, final Duration time
             Timer timer = time.timer(timeout);
             Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(
                     new MetadataRequest.Builder(Collections.singletonList(topic), metadata.allowAutoTopicCreation()), timer);
-            return topicMetadata.get(topic);
+            return topicMetadata.getOrDefault(topic, Collections.emptyList());

Review comment:
       just curious.
   
   Is it possible that an existent topic has no partitions? If so, returning empty list may confuse users who querying nonexistent topic. Maybe we should throw ```UnknownTopicOrPartitionException``` for nonexistent topic and return empty list for existent topic.




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



[GitHub] [kafka] chia7712 commented on a change in pull request #10017: KAFKA-12260: Avoid hitting NPE for partitionsFor

Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10017:
URL: https://github.com/apache/kafka/pull/10017#discussion_r568411777



##########
File path: clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
##########
@@ -1948,7 +1948,7 @@ public OffsetAndMetadata committed(TopicPartition partition, final Duration time
             Timer timer = time.timer(timeout);
             Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(
                     new MetadataRequest.Builder(Collections.singletonList(topic), metadata.allowAutoTopicCreation()), timer);
-            return topicMetadata.get(topic);
+            return topicMetadata.getOrDefault(topic, Collections.emptyList());

Review comment:
       BTW, it is worth writing docs for this case (return empty list).




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