Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Resource monitor plugin -- develop branch #9196

Merged
merged 12 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/chain_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ if(EOSIO_ENABLE_DEVELOPER_OPTIONS)
endif()

target_link_libraries( chain_plugin eosio_chain appbase resource_monitor_plugin )
target_include_directories( chain_plugin PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/../chain_interface/include" "${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/appbase/include") "${CMAKE_CURRENT_SOURCE_DIR}/../resource_monitor_plugin/include")
target_include_directories( chain_plugin PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/../chain_interface/include" "${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/appbase/include" "${CMAKE_CURRENT_SOURCE_DIR}/../resource_monitor_plugin/include")
1 change: 0 additions & 1 deletion plugins/resource_monitor_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ file(GLOB HEADERS "include/eosio/resource_monitor_plugin/*.hpp")
add_library( resource_monitor_plugin
resource_monitor_plugin.cpp
system_file_space_provider.cpp
resmon_impl.cpp
${HEADERS} )

target_link_libraries( resource_monitor_plugin appbase fc chain_plugin)
Expand Down

This file was deleted.

102 changes: 0 additions & 102 deletions plugins/resource_monitor_plugin/resmon_impl.cpp

This file was deleted.

112 changes: 110 additions & 2 deletions plugins/resource_monitor_plugin/resource_monitor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
**/

#include <eosio/resource_monitor_plugin/resource_monitor_plugin.hpp>
#include <eosio/resource_monitor_plugin/resmon_impl.hpp>
#include <eosio/resource_monitor_plugin/file_space_handler.hpp>
#include <eosio/resource_monitor_plugin/system_file_space_provider.hpp>

Expand All @@ -38,6 +37,114 @@ namespace bfs = boost::filesystem;
namespace eosio {
static appbase::abstract_plugin& _resource_monitor_plugin = app().register_plugin<resource_monitor_plugin>();

class resource_monitor_plugin_impl {
public:
resource_monitor_plugin_impl()
:space_handler(system_file_space_provider(), ctx)
{
}

void set_program_options(options_description&, options_description& cfg) {
cfg.add_options()
( "resource-monitor-interval-seconds", bpo::value<uint32_t>()->default_value(def_interval_in_secs),
"Time in seconds between two consecutive checks of resource usage. Should be between 1 and 300" )
( "resource-monitor-space-threshold", bpo::value<uint32_t>()->default_value(def_space_threshold),
"Threshold in terms of percentage of used space vs total space. If used space is above (threshold - 5%), a warning is generated. If used space is above the threshold and resource-monitor-not-shutdown-on-threshold-exceeded is enabled, a graceful shutdown is initiated. The value should be between 6 and 99" )
( "resource-monitor-not-shutdown-on-threshold-exceeded",
"Used to indicate nodeos will not shutdown when threshold is exceeded." )
;
}

void plugin_initialize(const appbase::variables_map& options) {
dlog("plugin_initialize");

auto interval = options.at("resource-monitor-interval-seconds").as<uint32_t>();
EOS_ASSERT(interval >= interval_low && interval <= interval_high, chain::plugin_config_exception,
"\"resource-monitor-interval-seconds\" must be between ${interval_low} and ${interval_high}", ("interval_low", interval_low) ("interval_high", interval_high));
space_handler.set_sleep_time(interval);
ilog("Monitoring interval set to ${interval}", ("interval", interval));

auto threshold = options.at("resource-monitor-space-threshold").as<uint32_t>();
EOS_ASSERT(threshold >= space_threshold_low && threshold <= space_threshold_high, chain::plugin_config_exception,
"\"resource-monitor-space-threshold\" must be between ${space_threshold_low} and ${space_threshold_high}", ("space_threshold_low", space_threshold_low) ("space_threshold_high", space_threshold_high));
space_handler.set_threshold(threshold, threshold - space_threshold_warning_diff);
ilog("Space usage threshold set to ${threshold}", ("threshold", threshold));

if (options.count("resource-monitor-not-shutdown-on-threshold-exceeded")) {
// If set, not shutdown
space_handler.set_shutdown_on_exceeded(false);
ilog("Shutdown flag when threshold exceeded set to false");
} else {
// Default will shut down
space_handler.set_shutdown_on_exceeded(true);
ilog("Shutdown flag when threshold exceeded set to true");
}
}

// Start main thread
void plugin_startup() {
ilog("Creating and starting monitor thread");

// By now all plugins are initialized.
// Find out filesystems containing the directories requested
// so far.
for ( auto& dir: directories_registered ) {
space_handler.add_file_system( dir );

// A directory like "data" contains subdirectories like
// "block". Those subdirectories can mount on different
// file systems. Make sure they are taken care of.
for (bfs::directory_iterator itr(dir); itr != bfs::directory_iterator(); ++itr) {
if (fc::is_directory(itr->path())) {
space_handler.add_file_system( itr->path() );
}
}
}

monitor_thread = std::thread( [this] {
fc::set_os_thread_name( "resmon" ); // console_appender uses 9 chars for thread name reporting.
space_handler.space_monitor_loop();

ctx.run();
} );
}

// System is shutting down.
void plugin_shutdown() {
ilog("shutdown...");

ctx.stop();

// Wait for the thread to end
monitor_thread.join();

ilog("exit shutdown");
}

void monitor_directory(const bfs::path& path) {
dlog("${path} registered to be monitored", ("path", path.string()));
directories_registered.push_back(path);
}

private:
std::thread monitor_thread;
std::vector<bfs::path> directories_registered;

static constexpr uint32_t def_interval_in_secs = 2;
static constexpr uint32_t interval_low = 1;
static constexpr uint32_t interval_high = 300;

static constexpr uint32_t def_space_threshold = 90; // in percentage
static constexpr uint32_t space_threshold_low = 6; // in percentage
static constexpr uint32_t space_threshold_high = 99; // in percentage
static constexpr uint32_t space_threshold_warning_diff = 5; // Warning issued when space used reached (threshold - space_threshold_warning_diff). space_threshold_warning_diff must be smaller than space_threshold_low

boost::asio::io_context ctx;

using file_space_handler_t = file_space_handler<system_file_space_provider>;
file_space_handler_t space_handler;
};

resource_monitor_plugin::resource_monitor_plugin():my(std::make_unique<resource_monitor_plugin_impl>()) {}

resource_monitor_plugin::~resource_monitor_plugin() {}
Expand All @@ -61,4 +168,5 @@ void resource_monitor_plugin::plugin_shutdown() {
void resource_monitor_plugin::monitor_directory(const bfs::path& path) {
my->monitor_directory( path );
}
}

} // namespace
8 changes: 4 additions & 4 deletions plugins/resource_monitor_plugin/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ target_include_directories( test_add_file_system PUBLIC "${CMAKE_CURRENT_SOURCE_

add_test(NAME test_add_file_system COMMAND plugins/resource_monitor_plugin/test/test_add_file_system WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_executable( test_resmom_impl test_resmom_impl.cpp )
target_link_libraries( test_resmom_impl resource_monitor_plugin )
target_include_directories( test_resmom_impl PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )
add_test(NAME test_resmom_impl COMMAND plugins/resource_monitor_plugin/test/test_resmom_impl WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_executable( test_resmon_plugin test_resmon_plugin.cpp )
target_link_libraries( test_resmon_plugin resource_monitor_plugin )
target_include_directories( test_resmon_plugin PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )
add_test(NAME test_resmon_plugin COMMAND plugins/resource_monitor_plugin/test/test_resmon_plugin WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
Loading