Repository navigation
DPL: make the stats command queue safe for concurrent updates and flushes - #16248
Merged
Merged
Conversation
…shes updateStats appends to a fixed queue of commands and the one which finds it full flushes it. With a thread pool the loop and the pool threads update the same stats, and that went wrong several ways, all upstream: - A thread finding the queue full while another flushed waited on `while (cmds.size())`, which is the size of a std::array and never false, and it still counted as pending, which is what the flush waits for. Both waited for ever: SlowConsumer hung with the loop asleep inside updateStats, reached from DataRelayer::relay, and its producer blocked in Send. - processCommandQueue reopened the queue by storing nextCmd = 0 before insertedCmds = 0, both relaxed, so a command taken from the new queue could have its count wiped and never be processed. The flushing thread then reset both counters a second time after processCommandQueue had reopened the queue, losing whatever other threads had added in between. - processCommandQueue is also called directly, by the metrics flush on the loop, while pool threads may be adding commands. So processCommandQueue now takes a mutex, closes the queue before looking at it (whoever takes a command from then on finds it full and waits on that mutex), waits for those already writing, and reopens it in an order which cannot lose a count, also when a command throws. Taking a command and closing the queue are sequentially consistent with the pending count, so that either the flush sees a writer pending or the writer sees the queue closed. A thread finding the queue full stops counting as pending, waits for the mutex and flushes the queue only if nobody has reopened it meanwhile, so that threads which found it full together do not flush it one after the other, then goes round again. The fast path stays lock-free, and single-threaded behaviour, nextCmd back to 0 after a flush included, is unchanged. The new DataProcessingStatsConcurrentUpdates test has four threads add 1 twenty thousand times each while a fifth flushes the queue directly. Before, 5 runs out of 5 gave up after 10 s with threads still stuck; after, 200 runs out of 200 pass with the exact total.
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.
updateStats appends to a fixed queue of commands and the one which finds it
full flushes it. With a thread pool the loop and the pool threads update the
same stats, and that went wrong several ways, all upstream:
while (cmds.size()), which is the size of a std::array and never false, andit still counted as pending, which is what the flush waits for. Both waited
for ever: SlowConsumer hung with the loop asleep inside updateStats, reached
from DataRelayer::relay, and its producer blocked in Send.
insertedCmds = 0, both relaxed, so a command taken from the new queue could
have its count wiped and never be processed. The flushing thread then reset
both counters a second time after processCommandQueue had reopened the
queue, losing whatever other threads had added in between.
loop, while pool threads may be adding commands.
So processCommandQueue now takes a mutex, closes the queue before looking at it
(whoever takes a command from then on finds it full and waits on that mutex),
waits for those already writing, and reopens it in an order which cannot lose
a count, also when a command throws. Taking a command and closing the queue are
sequentially consistent with the pending count, so that either the flush sees
a writer pending or the writer sees the queue closed. A thread finding the
queue full stops counting as pending, waits for the mutex and flushes the queue
only if nobody has reopened it meanwhile, so that threads which found it full
together do not flush it one after the other, then goes round again. The fast
path stays lock-free, and single-threaded behaviour, nextCmd back to 0 after a
flush included, is unchanged.
The new DataProcessingStatsConcurrentUpdates test has four threads add 1 twenty
thousand times each while a fifth flushes the queue directly. Before, 5 runs
out of 5 gave up after 10 s with threads still stuck; after, 200 runs out of
200 pass with the exact total.