From c8b6debc43acf78f95d75e4622736191c614b5a8 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Fri, 12 Apr 2024 20:09:50 +0100 Subject: [PATCH 1/2] Clean up write_queue::wait. Add consistency check to ensure the writer token is not already queued since they're waited for by value. Change wait-loop to use the predicated overload of wait. --- nano/store/write_queue.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/nano/store/write_queue.cpp b/nano/store/write_queue.cpp index cdc7f41ff8..b50809a32c 100644 --- a/nano/store/write_queue.cpp +++ b/nano/store/write_queue.cpp @@ -73,18 +73,14 @@ nano::store::write_guard nano::store::write_queue::wait (writer writer) } nano::unique_lock lk (mutex); + debug_assert (std::none_of (queue.cbegin (), queue.cend (), [writer] (auto const & item) { return item == writer; })); // Add writer to the end of the queue if it's not already waiting auto exists = std::find (queue.cbegin (), queue.cend (), writer) != queue.cend (); if (!exists) { queue.push_back (writer); } - - while (queue.front () != writer) - { - cv.wait (lk); - } - + cv.wait (lk, [&] () { return queue.front () == writer; }); return write_guard (guard_finish_callback); } From f220b450195324f2a9363c784e087b089c5b3deb Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sat, 13 Apr 2024 11:40:52 +0100 Subject: [PATCH 2/2] Change vote_generator to use write_queue when acquiring db transaction. Previously the vote_generator was not respecting the write_queue. --- nano/node/vote_generator.cpp | 1 + nano/store/write_queue.hpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/nano/node/vote_generator.cpp b/nano/node/vote_generator.cpp index addc721921..c5cac3b77b 100644 --- a/nano/node/vote_generator.cpp +++ b/nano/node/vote_generator.cpp @@ -94,6 +94,7 @@ void nano::vote_generator::process_batch (std::deque & batch) { std::deque candidates_new; { + auto guard = ledger.store.write_queue.wait (is_final ? nano::store::writer::voting_final : nano::store::writer::voting); auto transaction = ledger.store.tx_begin_write ({ tables::final_votes }); for (auto & [root, hash] : batch) diff --git a/nano/store/write_queue.hpp b/nano/store/write_queue.hpp index 674a7742bc..e01a6bf360 100644 --- a/nano/store/write_queue.hpp +++ b/nano/store/write_queue.hpp @@ -14,6 +14,8 @@ enum class writer confirmation_height, process_batch, pruning, + voting, + voting_final, testing // Used in tests to emulate a write lock };