-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting manual scheduler in to its own file.
This changes the order in which elections are scheduled. Both manual and prioritized elections are scheduled in parallel. Previously, manually scheduled elections would always be started before general prioritized elections.
- Loading branch information
Showing
13 changed files
with
177 additions
and
50 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include <nano/node/node.hpp> | ||
#include <nano/node/scheduler/manual.hpp> | ||
|
||
nano::scheduler::manual::manual (nano::node & node) : | ||
node{ node } | ||
{ | ||
} | ||
|
||
nano::scheduler::manual::~manual () | ||
{ | ||
// Thread must be stopped before destruction | ||
debug_assert (!thread.joinable ()); | ||
} | ||
|
||
void nano::scheduler::manual::start () | ||
{ | ||
debug_assert (!thread.joinable ()); | ||
|
||
thread = std::thread{ [this] () { | ||
nano::thread_role::set (nano::thread_role::name::election_scheduler); | ||
run (); | ||
} }; | ||
} | ||
|
||
void nano::scheduler::manual::stop () | ||
{ | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
stopped = true; | ||
} | ||
notify (); | ||
nano::join_or_pass (thread); | ||
} | ||
|
||
void nano::scheduler::manual::notify () | ||
{ | ||
condition.notify_all (); | ||
} | ||
|
||
void nano::scheduler::manual::push (std::shared_ptr<nano::block> const & block_a, boost::optional<nano::uint128_t> const & previous_balance_a, nano::election_behavior election_behavior_a) | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
queue.push_back (std::make_tuple (block_a, previous_balance_a, election_behavior_a)); | ||
notify (); | ||
} | ||
|
||
bool nano::scheduler::manual::predicate () const | ||
{ | ||
return !queue.empty (); | ||
} | ||
|
||
void nano::scheduler::manual::run () | ||
{ | ||
nano::unique_lock<nano::mutex> lock{ mutex }; | ||
while (!stopped) | ||
{ | ||
condition.wait (lock, [this] () { | ||
return stopped || predicate (); | ||
}); | ||
debug_assert ((std::this_thread::yield (), true)); // Introduce some random delay in debug builds | ||
if (!stopped) | ||
{ | ||
node.stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::loop); | ||
|
||
if (predicate ()) | ||
{ | ||
auto const [block, previous_balance, election_behavior] = queue.front (); | ||
queue.pop_front (); | ||
lock.unlock (); | ||
node.stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::insert_manual); | ||
auto result = node.active.insert (block, election_behavior); | ||
if (result.election != nullptr) | ||
{ | ||
result.election->transition_active (); | ||
} | ||
} | ||
else | ||
{ | ||
lock.unlock (); | ||
} | ||
notify (); | ||
lock.lock (); | ||
} | ||
} | ||
} | ||
|
||
std::unique_ptr<nano::container_info_component> nano::scheduler::manual::collect_container_info (std::string const & name) | ||
{ | ||
nano::unique_lock<nano::mutex> lock{ mutex }; | ||
|
||
auto composite = std::make_unique<container_info_composite> (name); | ||
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "queue", queue.size (), sizeof (decltype (queue)::value_type) })); | ||
return composite; | ||
} |
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,46 @@ | ||
#pragma once | ||
#include <nano/lib/locks.hpp> | ||
#include <nano/lib/numbers.hpp> | ||
#include <nano/node/active_transactions.hpp> | ||
|
||
#include <boost/optional.hpp> | ||
|
||
#include <deque> | ||
#include <memory> | ||
#include <mutex> | ||
|
||
namespace nano | ||
{ | ||
class block; | ||
class node; | ||
} | ||
|
||
namespace nano::scheduler | ||
{ | ||
class buckets; | ||
class manual final | ||
{ | ||
std::deque<std::tuple<std::shared_ptr<nano::block>, boost::optional<nano::uint128_t>, nano::election_behavior>> queue; | ||
nano::node & node; | ||
nano::mutex mutex; | ||
nano::condition_variable condition; | ||
bool stopped{ false }; | ||
std::thread thread; | ||
void notify (); | ||
bool predicate () const; | ||
void run (); | ||
|
||
public: | ||
manual (nano::node & node); | ||
~manual (); | ||
|
||
void start (); | ||
void stop (); | ||
|
||
// Manualy start an election for a block | ||
// Call action with confirmed block, may be different than what we started with | ||
void push (std::shared_ptr<nano::block> const &, boost::optional<nano::uint128_t> const & = boost::none, nano::election_behavior = nano::election_behavior::normal); | ||
|
||
std::unique_ptr<container_info_component> collect_container_info (std::string const & name); | ||
}; // class manual | ||
} // nano::scheduler |
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.