-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8635: Skip client poll in Sender loop when no request is sent #7085
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ddd2d3f
KAFKA-8635: Skip client poll in Sender loop when no request is sent
bob-barrett 2da11bc
PR feedback: consolidate awaitReady path, move in-flight check into h…
bob-barrett 0cd3376
Rename awaitLeastLoadedNode, check before calling lookupCoordinator
bob-barrett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| import org.apache.kafka.common.record.MemoryRecords; | ||
| import org.apache.kafka.common.record.RecordBatch; | ||
| import org.apache.kafka.common.requests.AbstractRequest; | ||
| import org.apache.kafka.common.requests.FindCoordinatorRequest; | ||
| import org.apache.kafka.common.requests.InitProducerIdRequest; | ||
| import org.apache.kafka.common.requests.InitProducerIdResponse; | ||
| import org.apache.kafka.common.requests.ProduceRequest; | ||
|
|
@@ -302,9 +303,7 @@ void runOnce() { | |
| transactionManager.transitionToFatalError( | ||
| new KafkaException("The client hasn't received acknowledgment for " + | ||
| "some previously sent messages and can no longer retry them. It isn't safe to continue.")); | ||
| } else if (transactionManager.hasInFlightTransactionalRequest() || maybeSendTransactionalRequest()) { | ||
| // as long as there are outstanding transactional requests, we simply wait for them to return | ||
| client.poll(retryBackoffMs, time.milliseconds()); | ||
| } else if (maybeSendAndPollTransactionalRequest()) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -412,7 +411,16 @@ private long sendProducerData(long now) { | |
| return pollTimeout; | ||
| } | ||
|
|
||
| private boolean maybeSendTransactionalRequest() { | ||
| /** | ||
| * Returns true if a transactional request is sent or polled, or if a FindCoordinator request is enqueued | ||
| */ | ||
| private boolean maybeSendAndPollTransactionalRequest() { | ||
| if (transactionManager.hasInFlightTransactionalRequest()) { | ||
| // as long as there are outstanding transactional requests, we simply wait for them to return | ||
| client.poll(retryBackoffMs, time.milliseconds()); | ||
| return true; | ||
| } | ||
|
|
||
| if (transactionManager.isCompleting() && accumulator.hasIncomplete()) { | ||
| if (transactionManager.isAborting()) | ||
| accumulator.abortUndrainedBatches(new KafkaException("Failing batch since transaction was aborted")); | ||
|
|
@@ -429,48 +437,42 @@ private boolean maybeSendTransactionalRequest() { | |
| return false; | ||
|
|
||
| AbstractRequest.Builder<?> requestBuilder = nextRequestHandler.requestBuilder(); | ||
| while (!forceClose) { | ||
| Node targetNode = null; | ||
| try { | ||
| if (nextRequestHandler.needsCoordinator()) { | ||
| targetNode = transactionManager.coordinator(nextRequestHandler.coordinatorType()); | ||
| if (targetNode == null) { | ||
| transactionManager.lookupCoordinator(nextRequestHandler); | ||
| break; | ||
| } | ||
| if (!NetworkClientUtils.awaitReady(client, targetNode, time, requestTimeoutMs)) { | ||
| transactionManager.lookupCoordinator(nextRequestHandler); | ||
| break; | ||
| } | ||
| } else { | ||
| targetNode = awaitLeastLoadedNodeReady(requestTimeoutMs); | ||
| } | ||
|
|
||
| if (targetNode != null) { | ||
| if (nextRequestHandler.isRetry()) | ||
| time.sleep(nextRequestHandler.retryBackoffMs()); | ||
| long currentTimeMs = time.milliseconds(); | ||
| ClientRequest clientRequest = client.newClientRequest( | ||
| targetNode.idString(), requestBuilder, currentTimeMs, true, requestTimeoutMs, nextRequestHandler); | ||
| log.debug("Sending transactional request {} to node {}", requestBuilder, targetNode); | ||
| client.send(clientRequest, currentTimeMs); | ||
| transactionManager.setInFlightCorrelationId(clientRequest.correlationId()); | ||
| return true; | ||
| } | ||
| } catch (IOException e) { | ||
| log.debug("Disconnect from {} while trying to send request {}. Going " + | ||
| "to back off and retry.", targetNode, requestBuilder, e); | ||
| if (nextRequestHandler.needsCoordinator()) { | ||
| // We break here so that we pick up the FindCoordinator request immediately. | ||
| transactionManager.lookupCoordinator(nextRequestHandler); | ||
| break; | ||
| } | ||
| Node targetNode = null; | ||
| try { | ||
| targetNode = awaitLeastLoadedNodeReady(requestTimeoutMs, nextRequestHandler.coordinatorType()); | ||
| if (targetNode == null) { | ||
| lookupCoordinatorAndRetry(nextRequestHandler); | ||
| return true; | ||
| } | ||
|
|
||
| if (nextRequestHandler.isRetry()) | ||
| time.sleep(nextRequestHandler.retryBackoffMs()); | ||
| long currentTimeMs = time.milliseconds(); | ||
| ClientRequest clientRequest = client.newClientRequest( | ||
| targetNode.idString(), requestBuilder, currentTimeMs, true, requestTimeoutMs, nextRequestHandler); | ||
| log.debug("Sending transactional request {} to node {}", requestBuilder, targetNode); | ||
| client.send(clientRequest, currentTimeMs); | ||
| transactionManager.setInFlightCorrelationId(clientRequest.correlationId()); | ||
| client.poll(retryBackoffMs, time.milliseconds()); | ||
| return true; | ||
| } catch (IOException e) { | ||
| log.debug("Disconnect from {} while trying to send request {}. Going " + | ||
| "to back off and retry.", targetNode, requestBuilder, e); | ||
| // We break here so that we pick up the FindCoordinator request immediately. | ||
| lookupCoordinatorAndRetry(nextRequestHandler); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private void lookupCoordinatorAndRetry(TransactionManager.TxnRequestHandler nextRequestHandler) { | ||
| if (!nextRequestHandler.needsCoordinator()) { | ||
| // For non-coordinator requests, sleep here to prevent a tight loop when no node is available | ||
| time.sleep(retryBackoffMs); | ||
| metadata.requestUpdate(); | ||
| } | ||
|
|
||
| transactionManager.lookupCoordinator(nextRequestHandler); | ||
| transactionManager.retry(nextRequestHandler); | ||
| return true; | ||
| } | ||
|
|
||
| private void maybeAbortBatches(RuntimeException exception) { | ||
|
|
@@ -513,8 +515,11 @@ private ClientResponse sendAndAwaitInitProducerIdRequest(Node node) throws IOExc | |
| return NetworkClientUtils.sendAndReceive(client, request, time); | ||
| } | ||
|
|
||
| private Node awaitLeastLoadedNodeReady(long remainingTimeMs) throws IOException { | ||
| Node node = client.leastLoadedNode(time.milliseconds()); | ||
| private Node awaitLeastLoadedNodeReady(long remainingTimeMs, FindCoordinatorRequest.CoordinatorType coordinatorType) throws IOException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the name should be changed since we do not always use the least loaded node. I noticed we are always using request timeout for the argument. Maybe we can drop the argument? |
||
| Node node = coordinatorType != null ? | ||
| transactionManager.coordinator(coordinatorType) : | ||
| client.leastLoadedNode(time.milliseconds()); | ||
|
|
||
| if (node != null && NetworkClientUtils.awaitReady(client, node, time, remainingTimeMs)) { | ||
| return node; | ||
| } | ||
|
|
@@ -525,7 +530,7 @@ private void maybeWaitForProducerId() { | |
| while (!forceClose && !transactionManager.hasProducerId() && !transactionManager.hasError()) { | ||
| Node node = null; | ||
| try { | ||
| node = awaitLeastLoadedNodeReady(requestTimeoutMs); | ||
| node = awaitLeastLoadedNodeReady(requestTimeoutMs, null); | ||
| if (node != null) { | ||
| ClientResponse response = sendAndAwaitInitProducerIdRequest(node); | ||
| InitProducerIdResponse initProducerIdResponse = (InitProducerIdResponse) response.responseBody(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It reads a bit strange to fall through to
lookupCoordinatorif we know the request doesn't need the coordinator. Maybe clearer with a slight restructure: