Repository navigation
Fix BulkIngester hanging when retries fill the buffer - #2256
emreekiziltoprak wants to merge 6 commits into
Conversation
Retried operations go back into the buffer before their backoff delay has passed, and they are re-added on retryScheduler, which blocks in innerAdd() while the buffer is full. The flushes that drain the buffer were scheduled on the same executor, so once all its threads were blocked those flushes never ran and add() and close() hung. Schedule the retry flushes on their own executor. Also flush after a retry is re-added if it is already sendable, since its scheduled flushes may have run while it was waiting for space in the buffer. Signed-off-by: emreekiziltoprak <emre.kzltprkk@gmail.com>
Signed-off-by: emreekiziltoprak <emre.kzltprkk@gmail.com>
A completed request signals a single thread waiting to send one. If that thread found nothing to send it returned without passing the signal on, leaving other waiting threads parked with no request in flight to wake them, which hung add() and close(). Signed-off-by: emreekiziltoprak <emre.kzltprkk@gmail.com>
…etry-deadlock Signed-off-by: emreekiziltoprak <emre.kzltprkk@gmail.com> # Conflicts: # CHANGELOG.md
rursprung
left a comment
There was a problem hiding this comment.
thanks for taking a stab at this!
while i'm not a maintainer on this repo i raised the original report. i had a quick glance at it and let copilot run a review. review feedback is from copilot.
| @Test(timeout = 30_000) | ||
| public void retriesWithShortBackoffAreFlushedOnceAddedToTheBuffer() throws Exception { | ||
| // The scheduled flushes are likely to run while the retries are still waiting for space in the buffer | ||
| retriesFillingTheBufferTest(1L); |
There was a problem hiding this comment.
A 1 ms delay does not guarantee a late re-add. The description acknowledges that no test fails specifically without the added flush(). Use gates or latches to hold a retry outside the buffer until its scheduled flushes have finished, then release it and verify completion without another flush trigger.
| } | ||
|
|
||
| if (canRun != null && !canRun.getAsBoolean()) { | ||
| // This thread may have used up a signal without doing anything with it: pass it on to the next one |
There was a problem hiding this comment.
Add a focused test with two waiting threads: the first wakes but can no longer run; the second must receive the forwarded signal. The temporary stress test is useful evidence, but it does not provide permanent regression coverage.
| try { | ||
| innerAdd(repeatableOp); | ||
| // The scheduled flushes may have run while this operation was waiting for space in the buffer | ||
| if (repeatableOp.isSendable()) { |
There was a problem hiding this comment.
I don't think we need this, the innerAdd does schedule flush() if needed
There was a problem hiding this comment.
Kept it: innerAdd() only flushes when the buffer becomes full. A retry that is added after both of its scheduled flushes have run stays in a buffer that is not full, and close() then waits forever. Without this flush, a stress loop over the reproduction hung in close() with all ingester threads idle. The new retryAddedAfterItsScheduledFlushesIsSent test, which holds the retry back until its flushes have run, fails without it.
There was a problem hiding this comment.
Still disagree, sorry:
- close() does call flush()
- the fact close() stuck in wait forever seems to be because of the fact we have retries scheduled
Intuitively, I think the way we should fix that is by making sure we a) cleared out the pending retries b) did not scheduled retries again (by checking isClosed status), hope it makes sense, thank you
There was a problem hiding this comment.
Makes sense, thanks. I removed the flush from addRetry() and close() now drops the retries that are still pending:
- the retries waiting in the buffer are removed before
close()flushes it, - a retry that was waiting to be added back is dropped instead of being added once the ingester is closed,
- responses that come back after
close()no longer schedule retries, their items go to the listener as they are.
Operations added by the application before close() are still sent.
Wake up the next waiting flush from BulkIngester.flush() when a flush had nothing to send, instead of changing FnCondition, so the ingester keeps control over which threads are woken up. Run the retry flushes on the periodic flush scheduler when there is one, and only create a dedicated one otherwise. Replace the timing-based short backoff test with one that holds the retry back until its scheduled flushes have run, and cover the retry flushes running on the periodic flush scheduler. Signed-off-by: emreekiziltoprak <emre.kzltprkk@gmail.com>
Instead of flushing a retry that is added back after its scheduled flushes have run, close() now drops the retries that are still pending: the ones waiting in the buffer, the ones waiting to be added back, and no new retries are scheduled for responses that come back after close(). Signed-off-by: emreekiziltoprak <emre.kzltprkk@gmail.com>
| IngesterOperation<Context> ingestOp = IngesterOperation.of(repeatableOp, client._transport().jsonpMapper()); | ||
|
|
||
| addCondition.whenReady(() -> { | ||
| if (isClosed && repeatableOp.retries() != null) { |
There was a problem hiding this comment.
Why do we need to check for repeatableOp.retries() here? I would assume isClosed is enough to signal and exit?
|
|
||
| isClosed = true; | ||
| // Drop the retries that are still pending | ||
| dropPendingRetries(); |
There was a problem hiding this comment.
I think dropping is not the ideal option here (I probably miscommunicated the intent of clearing out retries, my apologies for that, let me explain): the flushing logic (flush() call below) would attempt to send all buffered operation, which do include retries, if I am not mistaken. If that is the case, we don't need to drop anything BUT we should make sure that flush() does not attempt to reschedule retries anymore (when isClosed is set), but just does a best effort, last attempt. Does it make sense or I am missing something? Thank you @emreekiziltoprak
|
|
||
| if (failedRequestsCanRetry.isEmpty() || backoffPolicy.equals(BackoffPolicy.noBackoff())) { | ||
| // Total success! ...or there's no retry policy implemented. Either way, can call | ||
| if (failedRequestsCanRetry.isEmpty() || backoffPolicy.equals(BackoffPolicy.noBackoff()) || isClosed) { |
There was a problem hiding this comment.
I think the failedRequestsCanRetry has to be empty when isClosed is set (not need to make do isClosed check here), that somewhat reflected by https://github.com/opensearch-project/opensearch-java/pull/2256/changes#r4659188876
Description
Fixes the
BulkIngesterdeadlock described in #2248.Retried operations go back into the buffer before their backoff delay has passed, and
addRetry()re-adds them onretryScheduler, whereinnerAdd()blocks while the buffer is full.scheduleRetries()scheduled the flushes that drain the buffer on that same executor. Once all of its threads were blocked, those flushes stayed in its queue, so without aflushIntervaladd()andclose()never returned.This takes option 1 from the issue, as the smallest change that keeps today's buffer behaviour and backpressure:
retryScheduler, so threads blocked ininnerAdd()cannot starve them. They run on the periodic flushschedulerwhenflushIntervalis set, and otherwise on a dedicated single-thread executor. That thread can only wait for a free request slot, which does not depend onretryScheduler.addRetry()now flushes after the re-add if the operation is already sendable.innerAdd()only flushes when the buffer becomes full, so a retry added after both of its scheduled flushes had run stayed in the buffer, andclose()waited forever.flush()now callssendRequestCondition.signalIfReady()when it sends nothing. A completed request wakes one thread waiting to send. If that thread found nothing to send, the other waiters stayed parked with no request in flight to wake them, which hungadd()andclose()under load. This was already possible, but the retry path makes it much more likely because more threads callflush()at once.Issues Resolved
Closes #2248
Testing
New tests in
BulkIngesterTest, where the test transport answers the first operations with429:retriesFillingTheBufferDoNotBlockTheirFlushis the reproduction from the issue. It times out onmain.retriesFillingTheBufferDoNotBlockTheirFlushOnTheFlushIntervalSchedulercovers the same scenario with the retry flushes on the periodic flush scheduler.retryAddedAfterItsScheduledFlushesIsSentholds the retry back until its scheduled flushes have run, then checks that it is sent without another flush. It fails without the flush inaddRetry().The lost wakeup has no deterministic test, because it needs another flush to take the operations between the signal and the woken thread. A temporary stress loop over the reproduction, with delays from 1 ms to 200 ms under CPU load, hung within about 30 seconds without the
signalIfReady()call, and also hung inclose()without the flush inaddRetry(). With both changes, 1398 rounds passed. The loop is not part of this PR.Check List
--signoff.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache License, Version 2.0.