Repository navigation
fix(jetstream): do not queue extra pulls while a consume iterator body runs - #529
Closed
Pushkar236 wants to merge 1 commit into
Closed
Pushkar236 wants to merge 1 commit into
Pushkar236 wants to merge 1 commit into
Conversation
…y runs In iterator mode a pull is only published when the loop asks for its next item. While a slow loop body runs, the queued pull is not sent, no request is open at the server and no heartbeats arrive, so the idle heartbeat monitor fired on every interval: each firing notified heartbeats_missed, asked for the consumer info, zeroed pending and queued another full pull. When the body returned they were all sent at once, so the consumer held many more than max_messages unacknowledged messages, redelivered after ack_wait. A reconnect during the body added one more the same way. Track the pulls queued but not yet published. A reset keeps them in pending and only pulls again when none is queued, and while the iterator runs with a pull held up behind its loop body, the monitor does not count missed heartbeats. An ordered consumer reset clears the queued counts. Resolves nats-io#524 Signed-off-by: Pushkar Kirange <71223317+Pushkar236@users.noreply.github.com>
Member
|
@Pushkar236 thank you for reporting this - I believe while your PR addresses the issue in some way there's a simpler fix without the added accounting - firstly this is an impedance problem for very long processing and other settings (perhaps this is how you are reporting the issue) - we do have 'next() I think the fix is to ignore the heartbeats missed timer when the user callback/iterator is blocked - which is a simpler design. I'll put a PR up and evaluate |
Member
|
@Pushkar236 I have fixed this differently - also solves additional issues |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #524.
In iterator mode (
for awaitoverconsume()), a pull is only published when the loop asks for its next item:pull()pushes a function into the iterator's queue. While a slow loop body runs, the queued pull is not sent, so no request is open at the server and no idle heartbeats arrive. The idle heartbeat monitor then fired on every interval, and each firing:heartbeats_missed,pendingand queued another pull for the fullmax_messages.When the body returned, all the queued pulls were sent at once, so the consumer held many more than
max_messagesunacknowledged messages, which were redelivered afterack_wait. A reconnect during the body added one more pull the same way.This tracks the pulls queued but not yet published (
queued, incremented inpull()and decremented when the iterator runs the pushed function), as suggested in the issue:resetPendingWithInfo/resetPendingNoInfo, used by the monitor and on reconnect) now setspendingto the queued pulls instead of zero, and only pulls again when none is queued, since the queued one is still going to be sent. This alone stops the extra pulls.yielding) and a pull is waiting behind its loop body, the monitor callback does not count missed heartbeats: it callsmonitor.work(), so misses are counted from when the pull goes out, and returns. That removes the per-intervalCONSUMER.INFOrequests and theheartbeats_missednotifications on a healthy server. Before the iterator is started, the monitor behaves as before (the existingconsume - heartbeatstest relies on that).reset()of an ordered consumer clears the queued counts too, and a queued pull skipped because the consumer was reset since no longer counts down, so stale pulls are not counted for the new consumer.In callback mode the pushed function runs immediately, so
queuedgoes back to zero right away and nothing changes there.Tests
Two new tests in
jetstream/tests/consume_test.ts, withmax_messages: 1andexpires: 1000(heartbeats every 500 ms), counting the pull requests on$JS.API.CONSUMER.MSG.NEXT.A.a:num_ack_pendingis 1, and noheartbeats_missedwas notified (onmain:num_ack_pending7);num_ack_pending1 after the reconnect (onmain: 3).Reverting only the monitor change brings back the
heartbeats_missednotifications (6), and reverting only the reset change makes the reconnect test fail (num_ack_pending2).With Deno 2.9.0 and nats-server 2.15.3:
consume_test.ts20/20,deno task test-kv98/98,test-obj104/104, andtest-jetstream405/406, where the failure isjetstream - mirror alternatesinjscluster_test.ts, which doesn't use consumers and also failed once in three runs onmain(it passed three times in a row on this branch).deno fmt --checkanddeno task lint-jetstreamare clean.Signed-off-by: Pushkar Kirange 71223317+Pushkar236@users.noreply.github.com