chia7712 commented on code in PR #22271:
URL: https://github.com/apache/kafka/pull/22271#discussion_r3580421800
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/SubscriptionState.java:
##########
@@ -1004,8 +1062,55 @@ synchronized void movePartitionToEnd(TopicPartition tp) {
assignment.moveToEnd(tp);
}
- public synchronized Optional<ConsumerRebalanceListener>
rebalanceListener() {
- return rebalanceListener;
+ /** @return true if a {@link RebalanceListener} has been registered with
this subscription. */
+ public synchronized boolean hasRebalanceListener() {
+ return rebalanceListener.get() != null;
+ }
+
+ /** Invoke {@link RebalanceListener#onPartitionsAssigned(Collection,
RebalanceConsumer)} on the given partitions.*/
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ dispatch(partitions, RebalanceListener::onPartitionsAssigned);
+ }
+
+ /** Invoke {@link RebalanceListener#onPartitionsRevoked(Collection,
RebalanceConsumer)} on the given partitions.*/
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ dispatch(partitions, RebalanceListener::onPartitionsRevoked);
+ }
+
+ /** Invoke {@link RebalanceListener#onPartitionsLost(Collection,
RebalanceConsumer)} on the given partitions.*/
+ public void onPartitionsLost(Collection<TopicPartition> partitions) {
+ dispatch(partitions, RebalanceListener::onPartitionsLost);
+ }
+
+ private void dispatch(
+ Collection<TopicPartition> partitions,
+ TriConsumer<RebalanceListener, Collection<TopicPartition>,
RebalanceConsumer> method) {
+ RebalanceListener rl;
+ Consumer<?, ?> rc;
+ synchronized (this) {
Review Comment:
This feels a bit smelly. Would you mind creating a small wrapper class to
hold both `RebalanceListener` and `Consumer` together to ensure atomic updates?
```java
private static class ListenerContext {
final RebalanceListener listener;
final Consumer<?, ?> consumer;
ListenerContext(RebalanceListener listener, Consumer<?, ?> consumer) {
this.listener = listener;
this.consumer = consumer;
}
}
private volatile ListenerContext listenerContext = null;
```
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/SubscriptionState.java:
##########
@@ -189,20 +196,47 @@ else if (this.subscriptionType != type)
throw new IllegalStateException(SUBSCRIPTION_EXCEPTION_MESSAGE);
}
+ /**
+ * deprecated Visible for testing only. Will be removed in a follow-on
cleanup PR.
Review Comment:
We should add `@Deprecated` as well. Also, it should start with `@deprecated`
--
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]