-
Notifications
You must be signed in to change notification settings - Fork 793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fair queuing for block processor #4476
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e119358
Return proper channel type from `nano::test::fake_channel`
pwojcikdev 619258d
Introduce `fair_queue` class
pwojcikdev 6602c7c
Use per peer fair queue in block processor
pwojcikdev bf9d18c
Removing unused headers.
clemahieu 9782234
Simplify source tuple
pwojcikdev 8513ba8
Config tests
pwojcikdev 00348b3
Renamings
pwojcikdev 3787521
Remove beta specific config values
pwojcikdev 676f715
Store origin channel as weak_ptr
pwojcikdev 852347e
Merge branch 'develop' into fair-queuing-3-wip
pwojcikdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
#include <nano/node/fair_queue.hpp> | ||
#include <nano/test_common/system.hpp> | ||
#include <nano/test_common/testutil.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <ranges> | ||
|
||
using namespace std::chrono_literals; | ||
|
||
namespace | ||
{ | ||
enum class source_enum | ||
{ | ||
unknown = 0, | ||
live, | ||
bootstrap, | ||
bootstrap_legacy, | ||
unchecked, | ||
local, | ||
forced, | ||
}; | ||
} | ||
|
||
TEST (fair_queue, construction) | ||
{ | ||
nano::fair_queue<source_enum, int> queue; | ||
ASSERT_EQ (queue.total_size (), 0); | ||
ASSERT_TRUE (queue.empty ()); | ||
} | ||
|
||
TEST (fair_queue, process_one) | ||
{ | ||
nano::fair_queue<int, source_enum> queue; | ||
queue.priority_query = [] (auto const &) { return 1; }; | ||
queue.max_size_query = [] (auto const &) { return 1; }; | ||
|
||
queue.push (7, { source_enum::live }); | ||
ASSERT_EQ (queue.total_size (), 1); | ||
ASSERT_EQ (queue.queues_size (), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::live }), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::bootstrap }), 0); | ||
|
||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 7); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
ASSERT_EQ (origin.channel, nullptr); | ||
|
||
ASSERT_TRUE (queue.empty ()); | ||
} | ||
|
||
TEST (fair_queue, fifo) | ||
{ | ||
nano::fair_queue<int, source_enum> queue; | ||
queue.priority_query = [] (auto const &) { return 1; }; | ||
queue.max_size_query = [] (auto const &) { return 999; }; | ||
|
||
queue.push (7, { source_enum::live }); | ||
queue.push (8, { source_enum::live }); | ||
queue.push (9, { source_enum::live }); | ||
ASSERT_EQ (queue.total_size (), 3); | ||
ASSERT_EQ (queue.queues_size (), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::live }), 3); | ||
|
||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 7); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
} | ||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 8); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
} | ||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 9); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
} | ||
|
||
ASSERT_TRUE (queue.empty ()); | ||
} | ||
|
||
TEST (fair_queue, process_many) | ||
{ | ||
nano::fair_queue<int, source_enum> queue; | ||
queue.priority_query = [] (auto const &) { return 1; }; | ||
queue.max_size_query = [] (auto const &) { return 1; }; | ||
|
||
queue.push (7, { source_enum::live }); | ||
queue.push (8, { source_enum::bootstrap }); | ||
queue.push (9, { source_enum::unchecked }); | ||
ASSERT_EQ (queue.total_size (), 3); | ||
ASSERT_EQ (queue.queues_size (), 3); | ||
ASSERT_EQ (queue.size ({ source_enum::live }), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::bootstrap }), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::unchecked }), 1); | ||
|
||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 7); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
} | ||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 8); | ||
ASSERT_EQ (origin.source, source_enum::bootstrap); | ||
} | ||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 9); | ||
ASSERT_EQ (origin.source, source_enum::unchecked); | ||
} | ||
|
||
ASSERT_TRUE (queue.empty ()); | ||
} | ||
|
||
TEST (fair_queue, max_queue_size) | ||
{ | ||
nano::fair_queue<int, source_enum> queue; | ||
queue.priority_query = [] (auto const &) { return 1; }; | ||
queue.max_size_query = [] (auto const &) { return 2; }; | ||
|
||
queue.push (7, { source_enum::live }); | ||
queue.push (8, { source_enum::live }); | ||
queue.push (9, { source_enum::live }); | ||
ASSERT_EQ (queue.total_size (), 2); | ||
ASSERT_EQ (queue.queues_size (), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::live }), 2); | ||
|
||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 7); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
} | ||
{ | ||
auto [result, origin] = queue.next (); | ||
ASSERT_EQ (result, 8); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
} | ||
|
||
ASSERT_TRUE (queue.empty ()); | ||
} | ||
|
||
TEST (fair_queue, round_robin_with_priority) | ||
{ | ||
nano::fair_queue<int, source_enum> queue; | ||
queue.priority_query = [] (auto const & origin) { | ||
switch (origin.source) | ||
{ | ||
case source_enum::live: | ||
return 1; | ||
case source_enum::bootstrap: | ||
return 2; | ||
case source_enum::unchecked: | ||
return 3; | ||
default: | ||
return 0; | ||
} | ||
}; | ||
queue.max_size_query = [] (auto const &) { return 999; }; | ||
|
||
queue.push (7, { source_enum::live }); | ||
queue.push (8, { source_enum::live }); | ||
queue.push (9, { source_enum::live }); | ||
queue.push (10, { source_enum::bootstrap }); | ||
queue.push (11, { source_enum::bootstrap }); | ||
queue.push (12, { source_enum::bootstrap }); | ||
queue.push (13, { source_enum::unchecked }); | ||
queue.push (14, { source_enum::unchecked }); | ||
queue.push (15, { source_enum::unchecked }); | ||
ASSERT_EQ (queue.total_size (), 9); | ||
|
||
// Processing 1x live, 2x bootstrap, 3x unchecked before moving to the next source | ||
ASSERT_EQ (queue.next ().second.source, source_enum::live); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::bootstrap); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::bootstrap); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::unchecked); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::unchecked); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::unchecked); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::live); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::bootstrap); | ||
ASSERT_EQ (queue.next ().second.source, source_enum::live); | ||
|
||
ASSERT_TRUE (queue.empty ()); | ||
} | ||
|
||
TEST (fair_queue, source_channel) | ||
{ | ||
nano::test::system system{ 1 }; | ||
|
||
nano::fair_queue<int, source_enum> queue; | ||
queue.priority_query = [] (auto const &) { return 1; }; | ||
queue.max_size_query = [] (auto const &) { return 999; }; | ||
|
||
auto channel1 = nano::test::fake_channel (system.node (0)); | ||
auto channel2 = nano::test::fake_channel (system.node (0)); | ||
auto channel3 = nano::test::fake_channel (system.node (0)); | ||
|
||
queue.push (6, { source_enum::live, channel1 }); | ||
queue.push (7, { source_enum::live, channel2 }); | ||
queue.push (8, { source_enum::live, channel3 }); | ||
queue.push (9, { source_enum::live, channel1 }); // Channel 1 has multiple entries | ||
ASSERT_EQ (queue.total_size (), 4); | ||
ASSERT_EQ (queue.queues_size (), 3); // Each <source, channel> pair is a separate queue | ||
|
||
ASSERT_EQ (queue.size ({ source_enum::live, channel1 }), 2); | ||
ASSERT_EQ (queue.size ({ source_enum::live, channel2 }), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::live, channel3 }), 1); | ||
|
||
auto all = queue.next_batch (999); | ||
ASSERT_EQ (all.size (), 4); | ||
|
||
auto filtered = [&] (auto const & channel) { | ||
auto r = all | std::views::filter ([&] (auto const & entry) { | ||
return entry.second.channel == channel; | ||
}); | ||
std::vector vec (r.begin (), r.end ()); | ||
return vec; | ||
}; | ||
|
||
auto channel1_results = filtered (channel1); | ||
ASSERT_EQ (channel1_results.size (), 2); | ||
|
||
{ | ||
auto [result, origin] = channel1_results[0]; | ||
ASSERT_EQ (result, 6); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
ASSERT_EQ (origin.channel, channel1); | ||
} | ||
{ | ||
auto [result, origin] = channel1_results[1]; | ||
ASSERT_EQ (result, 9); | ||
ASSERT_EQ (origin.source, source_enum::live); | ||
ASSERT_EQ (origin.channel, channel1); | ||
} | ||
|
||
ASSERT_TRUE (queue.empty ()); | ||
} | ||
|
||
TEST (fair_queue, cleanup) | ||
{ | ||
nano::test::system system{ 1 }; | ||
|
||
nano::fair_queue<int, source_enum> queue; | ||
queue.priority_query = [] (auto const &) { return 1; }; | ||
queue.max_size_query = [] (auto const &) { return 999; }; | ||
|
||
auto channel1 = nano::test::fake_channel (system.node (0)); | ||
auto channel2 = nano::test::fake_channel (system.node (0)); | ||
auto channel3 = nano::test::fake_channel (system.node (0)); | ||
|
||
queue.push (7, { source_enum::live, channel1 }); | ||
queue.push (8, { source_enum::live, channel2 }); | ||
queue.push (9, { source_enum::live, channel3 }); | ||
ASSERT_EQ (queue.total_size (), 3); | ||
ASSERT_EQ (queue.queues_size (), 3); | ||
|
||
ASSERT_EQ (queue.size ({ source_enum::live, channel1 }), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::live, channel2 }), 1); | ||
ASSERT_EQ (queue.size ({ source_enum::live, channel3 }), 1); | ||
|
||
// Either closing or resetting the channel should remove it from the queue | ||
channel1->close (); | ||
channel2.reset (); | ||
|
||
ASSERT_TRUE (queue.periodic_update ()); | ||
|
||
// Only channel 3 should remain | ||
ASSERT_EQ (queue.total_size (), 1); | ||
ASSERT_EQ (queue.queues_size (), 1); | ||
|
||
ASSERT_EQ (queue.size ({ source_enum::live, channel1 }), 0); | ||
ASSERT_EQ (queue.size ({ source_enum::live, channel2 }), 0); | ||
ASSERT_EQ (queue.size ({ source_enum::live, channel3 }), 1); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These types are reversed compared to other tests.