Skip to content

Commit

Permalink
Issue #1792: move config reloading watcher to a separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
theohax committed Apr 9, 2021
1 parent f8e21b5 commit f67aff6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
3 changes: 3 additions & 0 deletions nano/lib/threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ std::string nano::thread_role::get_string (nano::thread_role::name role)
case nano::thread_role::name::db_parallel_traversal:
thread_role_name_string = "DB par traversl";
break;
case nano::thread_role::name::config_reload_watcher:
thread_role_name_string = "Reload cfg";
break;
}

/*
Expand Down
3 changes: 2 additions & 1 deletion nano/lib/threading.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace thread_role
request_aggregator,
state_block_signature_verification,
epoch_upgrader,
db_parallel_traversal
db_parallel_traversal,
config_reload_watcher
};
/*
* Get/Set the identifier for the current thread
Expand Down
85 changes: 55 additions & 30 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,7 @@
namespace
{
volatile sig_atomic_t sig_int_or_term = 0;

void handle_interruption_signal (boost::asio::io_context& io_ctx)
{
io_ctx.stop ();
sig_int_or_term = 1;
}

void handle_fatal_signal ()
{
nano::dump_crash_stacktrace ();
nano::create_load_memory_address_files ();
}

void handle_hup_signal (std::shared_ptr<nano::node> const & node, boost::filesystem::path const & data_path, nano::node_flags const & flags)
{
nano::daemon_config config (data_path);

auto error = nano::read_node_config_toml (data_path, config, flags.config_overrides);
if (!error)
{
error = nano::flags_config_conflicts (flags, config.node);
if (!error)
{
node->config_set(config.node);
}
}
}
volatile sig_atomic_t sig_hup = 0;

void install_signal_handler ()
{
Expand Down Expand Up @@ -177,20 +151,70 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano::
}
}

nano::mutex sighup_received_mutex{};
nano::condition_variable sighup_received_condition{};
boost::thread ([&]() {
nano::thread_role::set (nano::thread_role::name::config_reload_watcher);
while (true)
{
{
nano::unique_lock<nano::mutex> lockGuard (sighup_received_mutex);
sighup_received_condition.wait (lockGuard, []()
{
return sig_hup || sig_int_or_term;
});
}

// If we were notified because of sig_int_or_term, then we stop.
if (sig_int_or_term)
{
return;
}

// Otherwise, we were notified because of sig_hup, so we reset the flag,
// do the config reloading and get back to continue listening for notifications.
sig_hup = 0;

nano::daemon_config config (data_path);

auto error = nano::read_node_config_toml (data_path, config, flags.config_overrides);
if (!error)
{
error = nano::flags_config_conflicts (flags, config.node);
if (!error)
{
node->config_set(config.node);
}
}
}
}).detach();

debug_assert (!nano::signal_handler_impl);
nano::signal_handler_impl = [&](int signal)
{
if (signal == SIGINT || signal == SIGTERM)
{
handle_interruption_signal(io_ctx);
{
nano::lock_guard<nano::mutex> lockGuard (sighup_received_mutex);
sig_int_or_term = 1;
}

sighup_received_condition.notify_one();
io_ctx.stop();
}
else if (signal == SIGSEGV || signal == SIGABRT)
{
handle_fatal_signal();
nano::dump_crash_stacktrace ();
nano::create_load_memory_address_files ();
}
else if (signal == SIGHUP)
{
handle_hup_signal(node, data_path, flags);
{
nano::lock_guard<nano::mutex> lockGuard (sighup_received_mutex);
sig_hup = 1;
}

sighup_received_condition.notify_one();
}
};

Expand All @@ -201,6 +225,7 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano::

if (sig_int_or_term == 1)
{
sighup_received_condition.notify_one();
ipc_server.stop ();
node->stop ();
if (rpc)
Expand Down

0 comments on commit f67aff6

Please sign in to comment.