-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[WIP] KAFKA-18376: High CPU load when AsyncKafkaConsumer uses a small max poll value #20521
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
Open
kirktrue
wants to merge
43
commits into
apache:trunk
Choose a base branch
from
kirktrue:KAFKA-18376-chain-events-in-background-thread
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
34932e2
[WIP] KAFKA-18376: High CPU load when AsyncKafkaConsumer uses a small…
kirktrue b5d7d01
[WIP] More work on correctness
kirktrue d4802c7
Re-enabling tests in AsyncKafkaConsumer
kirktrue 9fd7e58
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue d3fa910
Minor clean up from design review
kirktrue dbc4773
Updates to fix inverted logic in maybeInterruptCompositePoll()
kirktrue 09f8cb5
Add documentation for RequiresApplicationThreadExecution
kirktrue 5e794ce
Inject NetworkClientDelegate via supplier for ApplicationEventProcess…
kirktrue 464d5ba
Removed the verbose logging
kirktrue d253b84
Work in progress to get past most of the integration test issues
kirktrue aaefbef
Clean up logic related to metadata errors that can happen along any s…
kirktrue 40f6754
Minor updates for CompletableEventReaper logging
kirktrue 3e0b920
Refactor CompositePollEvent to use Blocker for state management
kirktrue 529aab3
Update AsyncKafkaConsumer.java
kirktrue 784aad2
Moving toward a non-blocking poll() implementation
kirktrue c6a7923
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue 524782c
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thre…
kirktrue 00f9069
Merge branch 'KAFKA-18376-chain-events-in-background-thread' into KAF…
kirktrue 0ac19f9
Clean up
kirktrue ae0ddcc
Add completion tracking to CompositePollEvent
kirktrue 6775aac
Refactor poll event handling and metadata error propagation
kirktrue 2c3547e
Inject NetworkClientDelegate into ApplicationEventProcessor
kirktrue 18f4fa1
Remove BackgroundEventHandler from OffsetsRequestManager
kirktrue ea99a13
Handle immediate metadata errors for CompletableEvents
kirktrue 56062f5
Update NetworkClientDelegate.java
kirktrue 9d65fa2
Merge pull request #10 from kirktrue/KAFKA-18376-chain-events-in-back…
kirktrue 99304db
Remove extra whitespace in NetworkClientDelegate
kirktrue 702b257
Revert removal of contains() from CompletableEventReaper
kirktrue 81b707e
Update NetworkClientDelegate.java
kirktrue 8159884
Refactor application thread requirement handling
kirktrue 7112022
Update AsyncKafkaConsumer.java
kirktrue f40a4ac
Refactor consumer record polling in tests
kirktrue abaa4dc
Reset backoff on event submission in AsyncKafkaConsumer
kirktrue 1570f69
Handle exceptions in AsyncKafkaConsumer poll event
kirktrue 1e52282
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue 2d21fa0
Refactor poll event handling and metadata error management
kirktrue b193770
Refactor CompositePollEventInvoker to standalone class
kirktrue 2302427
Remove debug logging from CompositePollEventInvoker
kirktrue 91e881f
Fix typo in CompositePollPseudoEvent class name
kirktrue 8b33f08
Refactor lambda in waitForConsumerPollException call
kirktrue 2aaca8d
Move pollForRecords helper method in KafkaConsumerTest
kirktrue 1f1ae24
Remove commented-out event processing code
kirktrue 985bbd7
Improve consumer poll reliability in integration tests
kirktrue 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
102 changes: 102 additions & 0 deletions
102
.../src/main/java/org/apache/kafka/clients/consumer/internals/CompositePollEventInvoker.java
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.clients.consumer.internals; | ||
|
||
import org.apache.kafka.clients.consumer.internals.events.ApplicationEvent; | ||
import org.apache.kafka.clients.consumer.internals.events.ApplicationEventHandler; | ||
import org.apache.kafka.clients.consumer.internals.events.CompositePollEvent; | ||
import org.apache.kafka.common.KafkaException; | ||
import org.apache.kafka.common.utils.LogContext; | ||
import org.apache.kafka.common.utils.Time; | ||
import org.apache.kafka.common.utils.Timer; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import static org.apache.kafka.clients.consumer.internals.events.CompletableEvent.calculateDeadlineMs; | ||
|
||
public class CompositePollEventInvoker { | ||
|
||
private final Logger log; | ||
private final Time time; | ||
private final ApplicationEventHandler applicationEventHandler; | ||
private final Runnable backgroundEventProcessor; | ||
private final Runnable offsetCommitProcessor; | ||
private CompositePollEvent inflight; | ||
|
||
public CompositePollEventInvoker(LogContext logContext, | ||
Time time, | ||
ApplicationEventHandler applicationEventHandler, | ||
Runnable backgroundEventProcessor, | ||
Runnable offsetCommitProcessor) { | ||
this.log = logContext.logger(getClass()); | ||
this.time = time; | ||
this.applicationEventHandler = applicationEventHandler; | ||
this.backgroundEventProcessor = backgroundEventProcessor; | ||
this.offsetCommitProcessor = offsetCommitProcessor; | ||
} | ||
|
||
public void poll(Timer timer) { | ||
if (inflight == null) { | ||
log.debug("No existing inflight event, submitting"); | ||
submitEvent(ApplicationEvent.Type.POLL, timer); | ||
} | ||
|
||
try { | ||
if (log.isTraceEnabled()) { | ||
log.trace( | ||
"Attempting to retrieve result from previously submitted {} with {} remaining on timer", | ||
inflight, | ||
timer.remainingMs() | ||
); | ||
} | ||
|
||
CompositePollEvent.Result result = inflight.resultOrError(); | ||
CompositePollEvent.State state = result.state(); | ||
|
||
if (state == CompositePollEvent.State.COMPLETE) { | ||
// Make sure to clear out the inflight request since it's complete. | ||
log.debug("Event {} completed, clearing inflight", inflight); | ||
inflight = null; | ||
} else if (state == CompositePollEvent.State.BACKGROUND_EVENT_PROCESSING_REQUIRED) { | ||
log.debug("About to process background events"); | ||
backgroundEventProcessor.run(); | ||
log.debug("Done processing background events"); | ||
result.nextEventType().ifPresent(t -> submitEvent(t, timer)); | ||
} else if (state == CompositePollEvent.State.OFFSET_COMMIT_CALLBACKS_REQUIRED) { | ||
log.debug("About to process offset commits"); | ||
offsetCommitProcessor.run(); | ||
log.debug("Done processing offset commits"); | ||
result.nextEventType().ifPresent(t -> submitEvent(t, timer)); | ||
} else if (state == CompositePollEvent.State.UNKNOWN) { | ||
throw new KafkaException("Unexpected poll result received"); | ||
} | ||
} catch (Throwable t) { | ||
// If an exception is hit, bubble it up to the user but make sure to clear out the inflight request | ||
// because the error effectively renders it complete. | ||
log.debug("Event {} \"completed\" via error ({}), clearing inflight", inflight, String.valueOf(t)); | ||
inflight = null; | ||
throw ConsumerUtils.maybeWrapAsKafkaException(t); | ||
} | ||
} | ||
|
||
private void submitEvent(ApplicationEvent.Type type, Timer timer) { | ||
long deadlineMs = calculateDeadlineMs(timer); | ||
inflight = new CompositePollEvent(deadlineMs, time.milliseconds(), type); | ||
applicationEventHandler.add(inflight); | ||
log.debug("Submitted new {} with {} remaining on timer", inflight, timer.remainingMs()); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
this bit is also one I wonder if needed, or if we can just trigger the commit callbacks direclty from the app thread poll loop (
offsetCommitCallbackInvoker.executeCallbacks()
) . If that works, no need for this extra state or all the relatedoffsetCommitProcessor
logic