Skip to content

MINOR: Port changes from KAFKA-18569 for ShareConsumers #19402

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.kafka.clients.consumer.internals.events.ShareFetchEvent;
import org.apache.kafka.clients.consumer.internals.events.ShareSubscriptionChangeEvent;
import org.apache.kafka.clients.consumer.internals.events.ShareUnsubscribeEvent;
import org.apache.kafka.clients.consumer.internals.events.StopFindCoordinatorOnCloseEvent;
import org.apache.kafka.clients.consumer.internals.metrics.AsyncConsumerMetrics;
import org.apache.kafka.clients.consumer.internals.metrics.KafkaShareConsumerMetrics;
import org.apache.kafka.common.KafkaException;
Expand Down Expand Up @@ -886,6 +887,8 @@ private void close(final Duration timeout, final boolean swallowException) {
// Prepare shutting down the network thread
swallow(log, Level.ERROR, "Failed to release assignment before closing consumer",
() -> sendAcknowledgementsAndLeaveGroup(closeTimer, firstException), firstException);
swallow(log, Level.ERROR, "Failed to stop finding coordinator",
this::stopFindCoordinatorOnClose, firstException);
swallow(log, Level.ERROR, "Failed invoking acknowledgement commit callback",
this::handleCompletedAcknowledgements, firstException);
if (applicationEventHandler != null)
Expand Down Expand Up @@ -914,6 +917,11 @@ private void close(final Duration timeout, final boolean swallowException) {
}
}

private void stopFindCoordinatorOnClose() {
log.debug("Stop finding coordinator during consumer close");
applicationEventHandler.add(new StopFindCoordinatorOnCloseEvent());
}

private Timer createTimerForCloseRequests(Duration timeout) {
// this.time could be null if an exception occurs in constructor prior to setting the this.time field
final Time time = (this.time == null) ? Time.SYSTEM : this.time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.kafka.clients.consumer.internals.events.ShareFetchEvent;
import org.apache.kafka.clients.consumer.internals.events.ShareSubscriptionChangeEvent;
import org.apache.kafka.clients.consumer.internals.events.ShareUnsubscribeEvent;
import org.apache.kafka.clients.consumer.internals.events.StopFindCoordinatorOnCloseEvent;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.TopicIdPartition;
import org.apache.kafka.common.TopicPartition;
Expand All @@ -48,6 +49,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InOrder;
import org.mockito.Mockito;

import java.time.Duration;
Expand Down Expand Up @@ -77,6 +79,7 @@
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -340,6 +343,25 @@ public void testCloseWithTopicAuthorizationException() {
assertDoesNotThrow(() -> consumer.close());
}

@Test
public void testStopFindCoordinatorOnClose() {
SubscriptionState subscriptions = new SubscriptionState(new LogContext(), AutoOffsetResetStrategy.NONE);
consumer = newConsumer(subscriptions);

// Setup the expected successful completion of close events
completeShareAcknowledgeOnCloseApplicationEventSuccessfully();
completeShareUnsubscribeApplicationEventSuccessfully(subscriptions);

// Close the consumer
consumer.close();

// Verify events are sent in correct order using InOrder
InOrder inOrder = inOrder(applicationEventHandler);
inOrder.verify(applicationEventHandler).addAndGet(any(ShareAcknowledgeOnCloseEvent.class));
inOrder.verify(applicationEventHandler).add(any(ShareUnsubscribeEvent.class));
inOrder.verify(applicationEventHandler).add(any(StopFindCoordinatorOnCloseEvent.class));
}

@Test
public void testVerifyApplicationEventOnShutdown() {
SubscriptionState subscriptions = new SubscriptionState(new LogContext(), AutoOffsetResetStrategy.NONE);
Expand Down