Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -895,12 +895,14 @@ private void close(final Duration timeout, final boolean swallowException) {
closeTimer.update();

// 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",
() -> handleCompletedAcknowledgements(true), firstException);
if (applicationEventHandler != null && backgroundEventReaper != null && backgroundEventQueue != null) {
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This close is only dependent on applicationEventHandler hence do not require all 3 to be non-null i.e. applicationEventHandler != null && backgroundEventReaper != null && backgroundEventQueue != null. Please simplify for stopFindCoordinatorOnClose.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, also the handleCompletedAcknowledgements seems to only need the background components?

Wonder if it would be clearer to do the null checks inside each func (where we can easily see the component they need, and will avoid facing this same issue again if they end up being reused)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @apoorvmittal10 and @lianetm for the review.
I have added the null checks inside each function now to make it clearer and ensure we do not face this issue again if we re-use the function later.

swallow(log, Level.ERROR, "Failed invoking acknowledgement commit callback",
() -> handleCompletedAcknowledgements(true), firstException);
}
if (applicationEventHandler != null)
closeQuietly(() -> applicationEventHandler.close(Duration.ofMillis(closeTimer.remainingMs())), "Failed shutting down network thread", firstException);
closeTimer.update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.kafka.common.metrics.Metrics;
import org.apache.kafka.common.protocol.Errors;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.utils.LogCaptureAppender;
import org.apache.kafka.common.utils.LogContext;
import org.apache.kafka.common.utils.MockTime;
import org.apache.kafka.common.utils.Time;
Expand Down Expand Up @@ -77,6 +78,7 @@
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -207,11 +209,19 @@ public void testFailConstructor() {
props.put(ConsumerConfig.GROUP_ID_CONFIG, "group-id");
props.put(ConsumerConfig.METRIC_REPORTER_CLASSES_CONFIG, "an.invalid.class");
final ConsumerConfig config = new ConsumerConfig(props);

LogCaptureAppender appender = LogCaptureAppender.createAndRegister();
KafkaException ce = assertThrows(
KafkaException.class,
() -> newConsumer(config));
assertTrue(ce.getMessage().contains("Failed to construct Kafka share consumer"), "Unexpected exception message: " + ce.getMessage());
assertTrue(ce.getCause().getMessage().contains("Class an.invalid.class cannot be found"), "Unexpected cause: " + ce.getCause());

boolean npeLogged = appender.getEvents().stream()
.flatMap(event -> event.getThrowableInfo().stream())
.anyMatch(str -> str.contains("NullPointerException"));

assertFalse(npeLogged, "Unexpected NullPointerException during consumer construction");
}

@Test
Expand Down