-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-2391 [WIP]: add timeout to KafkaConsumer blocking calls #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
clients/src/main/java/org/apache/kafka/clients/consumer/CommittedTimeoutKafkaConsumer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE | ||
| * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file | ||
| * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
| package org.apache.kafka.clients.consumer; | ||
|
|
||
| import org.apache.kafka.common.TopicPartition; | ||
|
|
||
| import java.util.Properties; | ||
|
|
||
|
|
||
| public class CommittedTimeoutKafkaConsumer { | ||
| public static void main(String[] args) { | ||
| KafkaConsumer<String, String> consumer = makeConsumer(); | ||
| System.out.println("COMMITTED: " + consumer.committed(new TopicPartition("t", 0))); | ||
| } | ||
|
|
||
| public static KafkaConsumer<String, String> makeConsumer() { | ||
| Properties props = new Properties(); | ||
| props.put("bootstrap.servers", "okaraman-ld1.linkedin.biz:9092"); | ||
| props.put("group.id", System.currentTimeMillis() + ""); | ||
| props.put("partition.assignment.strategy", "roundrobin"); | ||
| props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); | ||
| props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); | ||
| props.put("session.timeout.ms", 30 * 1000); | ||
| return new KafkaConsumer<>(props); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
clients/src/main/java/org/apache/kafka/clients/consumer/ListTopicsTimeoutKafkaConsumer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE | ||
| * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file | ||
| * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
| package org.apache.kafka.clients.consumer; | ||
|
|
||
| import java.util.Properties; | ||
|
|
||
|
|
||
| public class ListTopicsTimeoutKafkaConsumer { | ||
| public static void main(String[] args) { | ||
| KafkaConsumer<String, String> consumer = makeConsumer(); | ||
| System.out.println("LIST TOPICS: " + consumer.listTopics()); | ||
| } | ||
|
|
||
| public static KafkaConsumer<String, String> makeConsumer() { | ||
| Properties props = new Properties(); | ||
| props.put("bootstrap.servers", "okaraman-ld1.linkedin.biz:9092"); | ||
| props.put("group.id", System.currentTimeMillis() + ""); | ||
| props.put("partition.assignment.strategy", "roundrobin"); | ||
| props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); | ||
| props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); | ||
| props.put("session.timeout.ms", 30 * 1000); | ||
| props.put("enable.auto.commit", false); | ||
| props.put("request.timeout.ms", 20 * 1000); | ||
| return new KafkaConsumer<>(props); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
...ts/src/main/java/org/apache/kafka/clients/consumer/PartitionsForTimeoutKafkaConsumer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE | ||
| * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file | ||
| * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
| package org.apache.kafka.clients.consumer; | ||
|
|
||
| import java.util.Properties; | ||
|
|
||
|
|
||
| public class PartitionsForTimeoutKafkaConsumer { | ||
| public static void main(String[] args) { | ||
| KafkaConsumer<String, String> consumer = makeConsumer(); | ||
| System.out.println("PARTITIONS FOR: " + consumer.partitionsFor("t")); | ||
| } | ||
|
|
||
| public static KafkaConsumer<String, String> makeConsumer() { | ||
| Properties props = new Properties(); | ||
| props.put("bootstrap.servers", "okaraman-ld1.linkedin.biz:9092"); | ||
| props.put("group.id", System.currentTimeMillis() + ""); | ||
| props.put("partition.assignment.strategy", "roundrobin"); | ||
| props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); | ||
| props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); | ||
| props.put("session.timeout.ms", 30 * 1000); | ||
| props.put("enable.auto.commit", false); | ||
| props.put("request.timeout.ms", 20 * 1000); | ||
| return new KafkaConsumer<>(props); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question: what is our plan for cases where the metadata update fails? A few options that I can think of:
If I understand correctly, we are doing 3 at the moment (please correct me if I am wrong). I think 1 or 2 would be better although perhaps more work. We can talk about implementation issues after we decide what we want to do though. cc @hachikuji
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tough part about the consumer is that the handling is context-dependent. If the call to awaitMetadataUpdate happens in KafkaConsumer.poll(), we might want to ignore the timeout and just return an empty record set to the user. If it's a blocking call, we want to throw the exception. I wonder if it would be a good idea to let this variant return a boolean indicating whether the update happened so that the caller can more easily choose what to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick reply @ijuma. Yeah the point you raise is valid.
It looks like KafkaConsumer.listTopics (more specifically, its call to Fetcher.getAllTopics) in this RB is actually doing 1, but KafkaConsumer.partitionsFor (more specifically, its call to ConsumerNetworkClient.awaitMetadataUpdate) is doing 3. Maybe we should make them both act the same.
Also just a heads up, this PR is very much WIP. It only has listTopics and partitionsFor with timeouts so far. I put this PR up partially so that it shows the flaw in using request.timeout.ms as the timeout. From the jira ticket:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good points @hachikuji and @onurkaraman. By the way, I totally understand that this is a WIP. I just thought it would be worth discussing this sooner rather than later. :)
It does seem that a variant that lets the caller decide what to do would be good to have. Not sure if a boolean is good enough to represent the 3 possible states (or if the callers need to know about the 3 possible states).