Skip to content

Commit

Permalink
KAFKA-16217: Stop the abort transaction try loop when closing produce…
Browse files Browse the repository at this point in the history
…rs (#15541)

This is a mitigation fix for the https://issues.apache.org/jira/browse/KAFKA-16217. Exceptions should not block closing the producers.
This PR reverts a part of the change #13591

Reviewers: Kirk True <[email protected]>, Justine Olshan <[email protected]>
  • Loading branch information
CalvinConfluent authored Mar 29, 2024
1 parent 2118d85 commit 6e4a098
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,14 @@ public void run() {
while (!forceClose && transactionManager != null && transactionManager.hasOngoingTransaction()) {
if (!transactionManager.isCompleting()) {
log.info("Aborting incomplete transaction due to shutdown");

try {
// It is possible for the transaction manager to throw errors when aborting. Catch these
// so as not to interfere with the rest of the shutdown logic.
transactionManager.beginAbort();
} catch (Exception e) {
log.error("Error in kafka producer I/O thread while aborting transaction: ", e);
log.error("Error in kafka producer I/O thread while aborting transaction when during closing: ", e);
// Force close in case the transactionManager is in error states.
forceClose = true;
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class SenderTest {
private static final int MAX_REQUEST_SIZE = 1024 * 1024;
Expand Down Expand Up @@ -3274,6 +3276,26 @@ public void testProducerBatchRetriesWhenPartitionLeaderChanges() throws Exceptio
}
}

// This test is expected to run fast. If timeout, the sender is not able to close properly.
@Timeout(5)
@Test
public void testSenderShouldCloseWhenTransactionManagerInErrorState() throws Exception {
metrics.close();
Map<String, String> clientTags = Collections.singletonMap("client-id", "clientA");
metrics = new Metrics(new MetricConfig().tags(clientTags));
TransactionManager transactionManager = mock(TransactionManager.class);
SenderMetricsRegistry metricsRegistry = new SenderMetricsRegistry(metrics);
Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL,
1, metricsRegistry, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, transactionManager, apiVersions);
when(transactionManager.hasOngoingTransaction()).thenReturn(true);
when(transactionManager.beginAbort()).thenThrow(new IllegalStateException());
sender.initiateClose();

// The sender should directly get closed.
sender.run();
verify(transactionManager, times(1)).close();
}

/**
* Test the scenario that FetchResponse returns NOT_LEADER_OR_FOLLOWER, indicating change in leadership, but it
* does not contain new leader info(defined in KIP-951).
Expand Down

0 comments on commit 6e4a098

Please sign in to comment.