chia7712 commented on code in PR #22271:
URL: https://github.com/apache/kafka/pull/22271#discussion_r3520143081
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/SubscriptionState.java:
##########
@@ -1004,8 +1062,42 @@ synchronized void movePartitionToEnd(TopicPartition tp) {
assignment.moveToEnd(tp);
}
- public synchronized Optional<ConsumerRebalanceListener>
rebalanceListener() {
- return rebalanceListener;
+ private synchronized Optional<RebalanceListener> rebalanceListener() {
+ return Optional.ofNullable(rebalanceListener.get());
+ }
+
+ private synchronized Consumer<?, ?> rebalanceConsumerOrThrow() {
+ return Optional.ofNullable(rebalanceConsumer.get())
+ .orElseThrow(() -> new IllegalStateException("Rebalance
consumer has not been initialized"));
+ }
+
+ /** @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) {
+ rebalanceListener().ifPresent(rl ->
invokeRebalanceListener(partitions, rl::onPartitionsAssigned));
Review Comment:
Both values are not atomic, right? It is possible that `rebalanceListener`
is not null, but `consumer` is, right?
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/SubscriptionState.java:
##########
@@ -328,8 +362,30 @@ public synchronized void
assignFromSubscribed(Collection<TopicPartition> assignm
this.assignment.set(assignedPartitionStates);
}
- private void registerRebalanceListener(Optional<ConsumerRebalanceListener>
listener) {
- this.rebalanceListener = Objects.requireNonNull(listener,
"RebalanceListener cannot be null");
+ /**
+ * Visible for tests only. Used when a consumer instance is unavailable
for registration such as in simple
+ * tests or membership management.
+ * @param listener the listener; must not be null
+ */
+ public void setRebalanceListener(RebalanceListener listener) {
+ Objects.requireNonNull(listener, "Listener must not be null when
setting a rebalance listener");
+ this.rebalanceListener.set(listener);
+ }
+
+ /**
+ * Sets the current rebalance listener for this subscription.
+ * @param listener the listener
+ * @param consumer the consumer instance to orchestrate callbacks in
{@link RebalanceConsumer}. must not be null
+ */
+ public void setRebalanceListener(RebalanceListener listener, Consumer<?,
?> consumer) {
+ if (listener != null) {
+ Objects.requireNonNull(consumer, "Consumer must not be null when a
listener is provided");
+ this.rebalanceListener.set(listener);
+ this.rebalanceConsumer.set(consumer);
+ } else {
Review Comment:
Why do we need this branch?
--
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]