Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
34932e2
[WIP] KAFKA-18376: High CPU load when AsyncKafkaConsumer uses a small…
kirktrue Sep 10, 2025
b5d7d01
[WIP] More work on correctness
kirktrue Sep 11, 2025
d4802c7
Re-enabling tests in AsyncKafkaConsumer
kirktrue Sep 11, 2025
9fd7e58
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Sep 15, 2025
d3fa910
Minor clean up from design review
kirktrue Sep 15, 2025
dbc4773
Updates to fix inverted logic in maybeInterruptCompositePoll()
kirktrue Sep 15, 2025
09f8cb5
Add documentation for RequiresApplicationThreadExecution
kirktrue Sep 15, 2025
5e794ce
Inject NetworkClientDelegate via supplier for ApplicationEventProcess…
kirktrue Sep 15, 2025
464d5ba
Removed the verbose logging
kirktrue Sep 16, 2025
d253b84
Work in progress to get past most of the integration test issues
kirktrue Sep 17, 2025
aaefbef
Clean up logic related to metadata errors that can happen along any s…
kirktrue Sep 17, 2025
40f6754
Minor updates for CompletableEventReaper logging
kirktrue Sep 17, 2025
3e0b920
Refactor CompositePollEvent to use Blocker for state management
kirktrue Sep 18, 2025
529aab3
Update AsyncKafkaConsumer.java
kirktrue Sep 18, 2025
784aad2
Moving toward a non-blocking poll() implementation
kirktrue Sep 19, 2025
c6a7923
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Sep 19, 2025
524782c
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thre…
kirktrue Sep 19, 2025
00f9069
Merge branch 'KAFKA-18376-chain-events-in-background-thread' into KAF…
kirktrue Sep 19, 2025
0ac19f9
Clean up
kirktrue Sep 19, 2025
ae0ddcc
Add completion tracking to CompositePollEvent
kirktrue Sep 20, 2025
6775aac
Refactor poll event handling and metadata error propagation
kirktrue Sep 20, 2025
2c3547e
Inject NetworkClientDelegate into ApplicationEventProcessor
kirktrue Sep 20, 2025
18f4fa1
Remove BackgroundEventHandler from OffsetsRequestManager
kirktrue Sep 20, 2025
ea99a13
Handle immediate metadata errors for CompletableEvents
kirktrue Sep 20, 2025
56062f5
Update NetworkClientDelegate.java
kirktrue Sep 20, 2025
9d65fa2
Merge pull request #10 from kirktrue/KAFKA-18376-chain-events-in-back…
kirktrue Sep 20, 2025
99304db
Remove extra whitespace in NetworkClientDelegate
kirktrue Sep 20, 2025
702b257
Revert removal of contains() from CompletableEventReaper
kirktrue Sep 20, 2025
81b707e
Update NetworkClientDelegate.java
kirktrue Sep 20, 2025
8159884
Refactor application thread requirement handling
kirktrue Sep 20, 2025
7112022
Update AsyncKafkaConsumer.java
kirktrue Sep 20, 2025
f40a4ac
Refactor consumer record polling in tests
kirktrue Sep 23, 2025
abaa4dc
Reset backoff on event submission in AsyncKafkaConsumer
kirktrue Sep 23, 2025
1570f69
Handle exceptions in AsyncKafkaConsumer poll event
kirktrue Sep 23, 2025
1e52282
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Sep 23, 2025
2d21fa0
Refactor poll event handling and metadata error management
kirktrue Sep 23, 2025
b193770
Refactor CompositePollEventInvoker to standalone class
kirktrue Sep 23, 2025
2302427
Remove debug logging from CompositePollEventInvoker
kirktrue Sep 24, 2025
91e881f
Fix typo in CompositePollPseudoEvent class name
kirktrue Sep 24, 2025
8b33f08
Refactor lambda in waitForConsumerPollException call
kirktrue Sep 24, 2025
2aaca8d
Move pollForRecords helper method in KafkaConsumerTest
kirktrue Sep 24, 2025
1f1ae24
Remove commented-out event processing code
kirktrue Sep 24, 2025
985bbd7
Improve consumer poll reliability in integration tests
kirktrue Sep 24, 2025
5af8fc4
Updates and fixes for a couple of integration tests
kirktrue Sep 29, 2025
52c0845
Refactoring and clean up
kirktrue Sep 29, 2025
bfcd7ec
More clean up and refactoring
kirktrue Sep 29, 2025
f45b70e
Refactoring
kirktrue Sep 29, 2025
8235ed2
Refactoring and clean up
kirktrue Sep 29, 2025
a157071
Refactor and cleanup
kirktrue Sep 29, 2025
164dfdd
Remove unused KafkaException import in ConsumerPollTestUtils
kirktrue Sep 29, 2025
72472c5
Refactor composite poll event processing and context
kirktrue Sep 29, 2025
5c99d81
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Sep 30, 2025
0aed4af
Minor refactoring and added documentation
kirktrue Sep 30, 2025
ab42fca
Fixed streams tests in PlaintextAdminIntegrationTest
kirktrue Sep 30, 2025
cc49db8
Update CompositePollEvent.java
kirktrue Sep 30, 2025
7ba6047
Improve logging and comments in poll event classes
kirktrue Sep 30, 2025
bc660d6
Reverting changes made to omit pending-callback partitions from being…
kirktrue Sep 30, 2025
e9dbc61
Notify FetchBuffer from background thread if pausing due to applicati…
kirktrue Oct 1, 2025
5d1a34d
Relying on reading records to determine group membership for streams …
kirktrue Oct 1, 2025
5041a36
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 1, 2025
f6864a3
Refactor POLL into ASYNC_POLL and SHARE_POLL
kirktrue Oct 2, 2025
0256bdf
Moving the regex subscription check out of the poll path
kirktrue Oct 2, 2025
a233e90
Remove unused processUpdatePatternSubscriptionEvent method
kirktrue Oct 3, 2025
2b2f70c
Using Consumer.poll() helper methods from TestUtils
kirktrue Oct 3, 2025
461ffdd
Change debug to trace in ApplicationEventProcessor
kirktrue Oct 3, 2025
9fb9ee9
Refactoring
kirktrue Oct 3, 2025
a8ccdb6
Replace pollRecordsUntilTrue with waitUntilTrue in verifyConsumerWith…
kirktrue Oct 4, 2025
e1cf7b7
Replace awaitNonEmptyRecords with waitForRecords in PlaintextConsumer…
kirktrue Oct 4, 2025
7ca4cc8
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 6, 2025
eace3ee
Add detailed Javadoc to MetadataErrorNotifiable
kirktrue Oct 6, 2025
767316b
Refactoring to remove interim callback step
kirktrue Oct 6, 2025
f4eb161
Inline trackCheckAndUpdatePositionsForTimeout in process(AsyncPollEvent)
kirktrue Oct 6, 2025
ddf0423
Revert wakeup() method visibility back to package-private
kirktrue Oct 6, 2025
dcbe761
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 6, 2025
b5f4c86
Minor refactoring, cleanup, extraneous whitespace removal
kirktrue Oct 6, 2025
cac80ef
Reverted minor import change with SaslClientsWithInvalidCredentialsTest
kirktrue Oct 6, 2025
fa784e5
Refactored AsyncPollEvent to remove State
kirktrue Oct 6, 2025
5be7210
Update ApplicationEventProcessor.java
kirktrue Oct 6, 2025
19ce01a
Removed check-and-update-positions future timeout and removed AsyncPo…
kirktrue Oct 6, 2025
b04b0a2
Removed superfluous imports
kirktrue Oct 6, 2025
d939675
Added comment to NetworkClientDelegate.doSend() change
kirktrue Oct 6, 2025
91409bc
Make ConsumerIntegrationTest.testRackAwareAssignment() poll more freq…
kirktrue Oct 6, 2025
e7b5386
Fix stupid typo for poll timeout
kirktrue Oct 7, 2025
4985c7d
Updates for MetadataErrorNotifiableEvent
kirktrue Oct 7, 2025
2e5a982
Revert change to AsyncKafkaConsumer
kirktrue Oct 7, 2025
68f5bc1
Whitespace change
kirktrue Oct 7, 2025
5215030
Update AsyncKafkaConsumer.java
kirktrue Oct 7, 2025
f660b15
Refactor regex subscription evaluation
kirktrue Oct 7, 2025
dfdd693
Removed logging from process(AsyncPollEvent) path
kirktrue Oct 8, 2025
626bb67
Fixed variable name typo
kirktrue Oct 8, 2025
ef5e4da
Added brief documentation for the inner OnSubscriptionUpdatedCallback…
kirktrue Oct 8, 2025
3595e82
Renamed OnSubscriptionUpdatedCallback -> MembershipManagerShim
kirktrue Oct 8, 2025
4e9c313
Revert unnecessary changes to ConsumerIntegrationTest
kirktrue Oct 8, 2025
f9c0345
Minor correction in code comment
kirktrue Oct 8, 2025
293f08b
ConsumerIntegrationTest needs more time to resolve the assignment
kirktrue Oct 8, 2025
f26e832
Reworking logic in pollForFetches
kirktrue Oct 8, 2025
dfd3e7f
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 8, 2025
ba0fdac
Removed an outdated comment in ApplicationEventProcessor
kirktrue Oct 8, 2025
c664ddf
Revert change for onConsumerPoll() to limit to the async consumer and…
kirktrue Oct 8, 2025
45af571
Revert some changes in KafkaConsumerTest
kirktrue Oct 8, 2025
328cb13
Minor tweaks to KafkaConsumerTest to minimize diff noise
kirktrue Oct 8, 2025
5109b82
Revert changes to PlaintextConsumerTest
kirktrue Oct 8, 2025
0bde790
Update PlaintextConsumerTest.java
kirktrue Oct 8, 2025
0e1180f
Reverting changes to PlaintextConsumerCommitTest
kirktrue Oct 8, 2025
1a4fe5d
Update PlaintextConsumerCommitTest.java
kirktrue Oct 8, 2025
802d7d1
Revert more changes to AsyncKafkaConsumerTest
kirktrue Oct 8, 2025
9a673fb
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 8, 2025
cd516e3
Fixed code tweak
kirktrue Oct 8, 2025
089bb24
Test clean up
kirktrue Oct 9, 2025
8e86cf4
Removed whitespace diffs
kirktrue Oct 9, 2025
22b4806
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 9, 2025
8ccf306
Merge branch 'apache:trunk' into KAFKA-18376-chain-events-in-backgrou…
kirktrue Oct 9, 2025
2baf96e
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 9, 2025
9833010
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 9, 2025
36dae3d
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 9, 2025
ef07ee1
Trigger build
kirktrue Oct 10, 2025
afed9fc
Merge branch 'trunk' into KAFKA-18376-chain-events-in-background-thread
kirktrue Oct 10, 2025
1bf56bf
Reverted NetworkClientDelegate authentication error check
kirktrue Oct 10, 2025
7df60c7
Refactored completeExceptionally to set the error _first_, then set t…
kirktrue Oct 10, 2025
390ac04
Update AsyncKafkaConsumer.java
kirktrue Oct 11, 2025
7eaa225
Merge branch 'apache:trunk' into KAFKA-18376-chain-events-in-backgrou…
kirktrue Oct 11, 2025
1775d73
Merge branch 'apache:trunk' into KAFKA-18376-chain-events-in-backgrou…
kirktrue Oct 12, 2025
4772675
Remove group.id configuration from testConsumeUsingAssignWithNoAccess
kirktrue Oct 12, 2025
eb77ad8
Added whitespace to README.md to force rebuild
kirktrue Oct 13, 2025
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
Next Next commit
[WIP] KAFKA-18376: High CPU load when AsyncKafkaConsumer uses a small…
… max poll value

Introduces CompositePollEvent and CompositePollResult to refactor and streamline the poll event handling in AsyncKafkaConsumer and ApplicationEventProcessor. The new approach enables multi-step polling logic, improves callback and background event processing, and enhances testability. Also adds size methods to BackgroundEventHandler and OffsetCommitCallbackInvoker, disables several tests, and updates related classes to support the new event flow.
  • Loading branch information
kirktrue committed Sep 10, 2025
commit 34932e22224f7c108959da155c7069d141c56972
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import org.apache.kafka.clients.consumer.internals.events.CompletableApplicationEvent;
import org.apache.kafka.clients.consumer.internals.events.CompletableEvent;
import org.apache.kafka.clients.consumer.internals.events.CompletableEventReaper;
import org.apache.kafka.clients.consumer.internals.events.CompositePollEvent;
import org.apache.kafka.clients.consumer.internals.events.CompositePollResult;
import org.apache.kafka.clients.consumer.internals.events.ConsumerRebalanceListenerCallbackCompletedEvent;
import org.apache.kafka.clients.consumer.internals.events.ConsumerRebalanceListenerCallbackNeededEvent;
import org.apache.kafka.clients.consumer.internals.events.CreateFetchRequestsEvent;
Expand All @@ -59,7 +61,6 @@
import org.apache.kafka.clients.consumer.internals.events.LeaveGroupOnCloseEvent;
import org.apache.kafka.clients.consumer.internals.events.ListOffsetsEvent;
import org.apache.kafka.clients.consumer.internals.events.PausePartitionsEvent;
import org.apache.kafka.clients.consumer.internals.events.PollEvent;
import org.apache.kafka.clients.consumer.internals.events.ResetOffsetEvent;
import org.apache.kafka.clients.consumer.internals.events.ResumePartitionsEvent;
import org.apache.kafka.clients.consumer.internals.events.SeekUnvalidatedEvent;
Expand Down Expand Up @@ -500,7 +501,10 @@ public AsyncKafkaConsumer(final ConsumerConfig config,
final Supplier<ApplicationEventProcessor> applicationEventProcessorSupplier = ApplicationEventProcessor.supplier(logContext,
metadata,
subscriptions,
requestManagersSupplier);
requestManagersSupplier,
backgroundEventHandler,
Optional.of(offsetCommitCallbackInvoker)
);
this.applicationEventHandler = applicationEventHandlerFactory.build(
logContext,
time,
Expand Down Expand Up @@ -689,7 +693,9 @@ public AsyncKafkaConsumer(final ConsumerConfig config,
logContext,
metadata,
subscriptions,
requestManagersSupplier
requestManagersSupplier,
backgroundEventHandler,
Optional.of(offsetCommitCallbackInvoker)
);
this.applicationEventHandler = new ApplicationEventHandler(logContext,
time,
Expand Down Expand Up @@ -865,23 +871,32 @@ public ConsumerRecords<K, V> poll(final Duration timeout) {
}

do {
PollEvent event = new PollEvent(timer.currentTimeMs());
// Make sure to let the background thread know that we are still polling.
// This will trigger async auto-commits of consumed positions when hitting
// the interval time or reconciling new assignments
applicationEventHandler.add(event);
// Wait for reconciliation and auto-commit to be triggered, to ensure all commit requests
// retrieve the positions to commit before proceeding with fetching new records
ConsumerUtils.getResult(event.reconcileAndAutoCommit(), defaultApiTimeoutMs.toMillis());

// We must not allow wake-ups between polling for fetches and returning the records.
// If the polled fetches are not empty the consumed position has already been updated in the polling
// of the fetches. A wakeup between returned fetches and returning records would lead to never
// returning the records in the fetches. Thus, we trigger a possible wake-up before we poll fetches.
wakeupTrigger.maybeTriggerWakeup();

updateAssignmentMetadataIfNeeded(timer);
final Fetch<K, V> fetch = pollForFetches(timer);
long pollTimeMs = timer.currentTimeMs();
long deadlineMs = calculateDeadlineMs(timer);
ApplicationEvent.Type nextStep = ApplicationEvent.Type.POLL;

while (true) {
CompositePollEvent event = new CompositePollEvent(pollTimeMs, deadlineMs, nextStep);
CompositePollResult result = applicationEventHandler.addAndGet(event);

if (result == CompositePollResult.NEEDS_OFFSET_COMMIT_CALLBACKS) {
offsetCommitCallbackInvoker.executeCallbacks();
nextStep = ApplicationEvent.Type.UPDATE_SUBSCRIPTION_METADATA;
} else if (result == CompositePollResult.NEEDS_BACKGROUND_EVENT_PROCESSING) {
processBackgroundEvents();
nextStep = ApplicationEvent.Type.CHECK_AND_UPDATE_POSITIONS;
} else if (result == CompositePollResult.COMPLETE) {
break;
}
}

final Fetch<K, V> fetch = collectFetch();
if (!fetch.isEmpty()) {
// before returning the fetched records, we can send off the next round of fetches
// and avoid block waiting for their responses to enable pipelining while the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ protected void maybeThrowAuthFailure(Node node) {
* @return Future on which the caller can wait to ensure that the requests have been created
*/
public CompletableFuture<Void> createFetchRequests() {
if (!fetchBuffer.isEmpty())
return CompletableFuture.completedFuture(null);

CompletableFuture<Void> future = new CompletableFuture<>();

if (pendingFetchRequestFuture != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public void enqueueInterceptorInvocation(final Map<TopicPartition, OffsetAndMeta
}
}

public int size() {
return callbackQueue.size();
}

public void enqueueUserCallbackInvocation(final OffsetCommitCallback callback,
final Map<TopicPartition, OffsetAndMetadata> offsets,
final Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ private void process(final ShareAcknowledgementCommitCallbackEvent event) {
logContext,
metadata,
subscriptions,
requestManagersSupplier
requestManagersSupplier,
backgroundEventHandler,
Optional.empty()
);

this.applicationEventHandler = applicationEventHandlerFactory.build(
Expand Down Expand Up @@ -407,7 +409,9 @@ private void process(final ShareAcknowledgementCommitCallbackEvent event) {
logContext,
metadata,
subscriptions,
requestManagersSupplier
requestManagersSupplier,
backgroundEventHandler,
Optional.empty()
);

this.applicationEventHandler = new ApplicationEventHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public abstract class ApplicationEvent {

public enum Type {
COMMIT_ASYNC, COMMIT_SYNC, POLL, FETCH_COMMITTED_OFFSETS, NEW_TOPICS_METADATA_UPDATE, ASSIGNMENT_CHANGE,
COMMIT_ASYNC, COMMIT_SYNC, COMPOSITE_POLL, POLL, FETCH_COMMITTED_OFFSETS, NEW_TOPICS_METADATA_UPDATE, ASSIGNMENT_CHANGE,
LIST_OFFSETS, CHECK_AND_UPDATE_POSITIONS, RESET_OFFSET, TOPIC_METADATA, ALL_TOPICS_METADATA,
TOPIC_SUBSCRIPTION_CHANGE, TOPIC_PATTERN_SUBSCRIPTION_CHANGE, TOPIC_RE2J_PATTERN_SUBSCRIPTION_CHANGE,
UPDATE_SUBSCRIPTION_METADATA, UNSUBSCRIBE,
Expand Down
Loading