-
Notifications
You must be signed in to change notification settings - Fork 1
name single threads of kafka live event listener #97
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||
import java.util.concurrent.ExecutorService; | ||||||
import java.util.concurrent.Executors; | ||||||
import java.util.concurrent.Future; | ||||||
import java.util.concurrent.ThreadFactory; | ||||||
import java.util.concurrent.TimeUnit; | ||||||
import java.util.function.BiConsumer; | ||||||
import org.apache.kafka.clients.consumer.Consumer; | ||||||
|
@@ -64,7 +65,7 @@ public void close() throws Exception { | |||||
public static final class Builder<K, V> { | ||||||
private final Collection<BiConsumer<? super K, ? super V>> callbacks = | ||||||
new ConcurrentLinkedQueue<>(); | ||||||
private ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||||||
private ExecutorService executorService; | ||||||
private boolean cleanupExecutor = | ||||||
true; // if builder creates executor shutdown executor while closing event listener | ||||||
|
||||||
|
@@ -84,10 +85,26 @@ public Builder<K, V> withExecutorService( | |||||
|
||||||
public KafkaLiveEventListener<K, V> build( | ||||||
String consumerName, Config kafkaConfig, Consumer<K, V> kafkaConsumer) { | ||||||
if (executorService == null) { | ||||||
executorService = | ||||||
Executors.newSingleThreadExecutor(new ListenerThreadFactory(consumerName)); | ||||||
} | ||||||
return new KafkaLiveEventListener<>( | ||||||
new KafkaLiveEventListenerCallable<>(consumerName, kafkaConfig, kafkaConsumer, callbacks), | ||||||
executorService, | ||||||
cleanupExecutor); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
class ListenerThreadFactory implements ThreadFactory { | ||||||
private final String name; | ||||||
|
||||||
public ListenerThreadFactory(String consumerName) { | ||||||
this.name = "kafka-live-event-listener-" + consumerName + "-" + System.currentTimeMillis(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
} | ||||||
|
||||||
public Thread newThread(Runnable r) { | ||||||
return new Thread(r, name); | ||||||
} | ||||||
} |
Oops, something went wrong.
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.
Is it common pattern to add time in name?
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.
I added here intentionally to detect if we have multiple threads for same consumer name
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.
Ideally for one topic there should be only one live event listener hence one thread only. But if it is wrongly used (calling build again and again instead of only once) there might be more than one, which we want to detect and fix
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.
If there are 8 StreamThread, there will be 8 Consumers.
If, its more than that, who is caller? How would we fix this? I think, is this called by our application code or by kafka framework itself?
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.
No even if there are 8 stream threads we only want one consumer per topic - singleton instance
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.
This is used by application code, mainly to listen to change events. If we see more than one thread for a consumer name that means we are rebuilding live event listener under some condition, which is wrong. To fix, you might need to organise the building of live event listener to the place where code is exactly once executed - guice singleton or build topology and pass it down appropriately