KAFKA-17439: Make polling for new records an explicit action/event in the new consumer - #17035
Conversation
AndrewJSchofield
left a comment
There was a problem hiding this comment.
Thanks for the PR. I'm quite surprised to see how little code needed to change to make the fetching explicit. A couple of points.
First, I think it better not to overload the PollEvent because that's already used in the share consumer.
Second, it seems to me that there is still the potential for over-fetching, and this will still cause churn of the fetch session cache.
In the case where the consumer is only fetching a single partition, I think it works pretty well. The set of fetchable partitions will be empty if there's buffered data, and contain the only partition in the fetch session if there is not. So, you'll only send a Fetch request when there's a need for more data and the fetch session will not churn.
In the case where the consumer is fetching more than one partition on a particular node, if a subset of the partitions is fetchable, then the fetch session will be modified by sending a Fetch request and that seems to have the potential for a lot of churn.
Of course, all of this code is in common between the legacy consumer and the async consumer. The async consumer is still very keen on fetching so I don't properly grasp why this PR would make the fetch session behaviour better.
|
Thanks for the review 👍
Agreed. I've introduced a
Agreed. It aims to lessen the churn. Preventing the churn completely is a future task.
Correct.
Correct again! Any partition with buffered data at the point where the fetch request is being generated will be marked as "removed" from the broker's fetch session cache. That's the crux of the problem 😞 Something that I tend to lose sight of is the fact that it's not a foregone conclusion that a fetch session will be evicted when it has partitions removed. Of course, it will increase its eligibility for eviction if the broker hosting the fetch session is resource-constrained and invokes the eviction process.
I'm not sure I follow. This code is all specific to the
Yep—the design of the Thanks! |
|
@AndrewJSchofield, et al.—it can be helpful to compare the flow of |
… the new consumer Updated the FetchRequestManager to only create and enqueue fetch requests when signaled to do so by a FetchEvent.
AndrewJSchofield
left a comment
There was a problem hiding this comment.
Thanks for the updates and for the explanation of the mechanism.
I think it would be appropriate to test in FetchRequestManagerTest the mechanism of the pending fetch request future in the various permutations.
Allowing sendFetches to block in order to communicate errors, but it will suppress timeouts.
|
@lianetm—tests are passing and all comments have been addressed. Can you make another review pass? Thanks! |
| this::handleFetchFailure | ||
| ); | ||
| ); | ||
| pendingFetchRequestFuture.complete(null); |
There was a problem hiding this comment.
do we need to complete this future also on pollOnClose? there may be a pendingFetchRequestFuture there that won't be completed (not that I'm seeing how leaving that future uncompleted on close will cause a problem but seems safer to complete it, consistently with how we do it here after pollInternal)
There was a problem hiding this comment.
I've moved the Future-handling code to pollInternal() for consistency. LMK what you think.
| } | ||
|
|
||
| // send any new fetches (won't resend pending fetches) | ||
| sendFetches(timer); |
There was a problem hiding this comment.
The actual poll now happens in here (addAndGet that will complete when the background has had one run, called fetchMgr.poll), so should the log line on ln 1538 "Polling for fetches with timeout..." be right before this?
There was a problem hiding this comment.
We're not polling for the incoming responses in sendFetches(), just enqueuing the outgoing requests. This mimics the ClassicKafkaConsumer in that the requests are enqueued in its sendFetches() but then toward the bottom of pollForFetches() client.poll() is invoked to wait for the results of the fetch requests.
There was a problem hiding this comment.
well the sendFetches blocks until the CreateFetchRequestsEvent completes, and that only happens on fetchMgr.poll
So when the
sendFetches completes we did poll the manager right? (and depending on time, maybe we did poll the client.poll too, which happens in the background right after polling all managers). That's why the log for "Polling for fetches" made sense to me before the sendFetches, but am I missing another poll happening after the log line maybe? (where it is now)
There was a problem hiding this comment.
The two ConsumerDelegate implementations work differently:
AsyncKafkaConsumer:FetchRequestManager.poll()will complete the event'sFutureon the background thread before it exits, i.e. before the thread starts the network I/O. Completing theFuturestarts the application thread racing toward logging that message and the background thread racing toward starting network I/O. I'll admit—I haven't dug through the code to surmise the relative costs of each thread's work before either cross their respective finish lines to win.ClassicKafkaConsumer:Fetcher.sendFetchesInternal()callsConsumerNetworkClient.send()to enqueue the request, but then it callsNetworkClient.wakeup(). Since the sameConsumerNetworkClientinstance used by the consumer is also used byAbstractCoordinator.HeartbeatThread, it's technically possible that the heartbeat thread'srun()method could start network I/O when it callsNetworkClient.pollNoWakeup(). Granted, that's a race that the application thread is much more likely to win given that the heartbeat thread runs much less frequently.
Here are some points to consider:
- The definition of the term "poll" as used in the log is open to interpretation. The term "poll" is everywhere, making its meaning ambiguous at any given point of use 😢
- I agree there is a race condition (for both consumers, but more likely for the new consumer) that could result the the log message being emitted after the network I/O has commenced
- For this to pose a problem to users, there needs to be other log entries that we're racing with, right?. We're trying to avoid the condition where the user is confused/mislead because the entries in the log are emitted in non-deterministic ordering.
- The log line in question is only output at level
TRACE, which I assume is very rare for users to enable.
Given the above, I'm of the opinion that it's an exercise in hair splitting to alter the logging. However, I could also just change it which would have been way less effort than researching, thinking, and composing this response 🤣
If we leave the log line as it is, what would the effect be for the user?
There was a problem hiding this comment.
I surely didn't intend for you to put up that long response he he, sorry. It's not about the log line per-se, it's about the alignment on where the poll happens. The classic consumer logs "Polling for records", then calls client.poll. vs us here we do sendFetches (which triggers the client.poll async in the background thread because it blocks until we poll the fetch manager), then log "Polling for fetches...".
That's the diff I saw and just wanted to understand/align on where the poll happens: once we trigger sendFetches (blocking), the client.poll will happen in the background anytime, not controlled by the app thread. Agreed? If so I'm ok with leaving the log unchanged, understanding it could come out after the client.poll happened.
There was a problem hiding this comment.
That's the diff I saw and just wanted to understand/align on where the poll happens: once we trigger sendFetches (blocking), the client.poll will happen in the background anytime, not controlled by the app thread. Agreed?
Agreed—the background thread is going to move from calling each of the RequestManager’s poll() method to NetworkClient.poll() method without the intervention of the application thread.
If so I'm ok with leaving the log unchanged, understanding it could come out after the client.poll happened.
Thanks!
| updateAssignmentMetadataIfNeeded(timer); | ||
| final Fetch<K, V> fetch = pollForFetches(timer); | ||
| if (!fetch.isEmpty()) { | ||
| sendFetches(timer); |
There was a problem hiding this comment.
at this point we may already have records in hand to return (consumed position updated), so we should be very careful to not throw any error here. But this sendFetches could throw interrupted because of the addAndGet right?
Shouldn't we just do a best effort to pipeline the next requests using add instead of addAndGet? It would achieve what we want, removing the risk of errors, and it would actually align better with what the classic does on this sendFetches + transmitSends:
| * | ||
| * <ul> | ||
| * <li> | ||
| * The method will wait for confirmation of the request creation before continuing. |
There was a problem hiding this comment.
This is not true now for prefetching that uses .add instead of .addAndGet, should we remove this line?
There was a problem hiding this comment.
Good catch. Reworded to state that it will not wait for confirmation.
… the new consumer (apache#17035) Reviewers: Andrew Schofield <aschofield@confluent.io>, Lianet Magrans <lmagrans@confluent.io>
… the new consumer (apache#17035) Reviewers: Andrew Schofield <aschofield@confluent.io>, Lianet Magrans <lmagrans@confluent.io>
… the new consumer (apache#17035) Reviewers: Andrew Schofield <aschofield@confluent.io>, Lianet Magrans <lmagrans@confluent.io>
Updated the
FetchRequestManagerto only create and enqueue fetch requests when signaled to do so by aFetchEvent.The application thread and the background thread each contains logic that is performed if there is buffered data from a previous fetch. There's a race condition because the presence of buffered data could change between the two threads’ respective checks. Right now the window for the race condition to occur is wide open; this change aims to make the window ajar.
In the
ClassicKafkaConsumer, the application thread will explicitly issue fetch requests (via theFetcherclass) at specific points in theConsumer.poll()cycle. Prior to this change, theAsyncKafkaConsumerwould issue fetch requests independently from the user callingConsumer.poll(); the fetches would happen nearly continuously as soon as any assigned partition was fetchable. With this change, theAsyncKafkaConsumerintroduces aFetchEventthat signals to the background thread that a fetch request should be issued. The specific points where this is done in theConsumer.poll()cycle of theAsyncKafkaConsumernow match theClassicKafkaConsumer. In short: this makesAsyncKafkaConsumeract nearly identical to theClassicKafkaConsumerin this regard.As mentioned above, this change does not completely solve the problem related to fetch session eviction. Exactly how the window where the race condition can be shut completely is outside the scope of this change.
See KAFKA-17182.
Committer Checklist (excluded from commit message)