diff --git a/capiocl/configuration.h b/capiocl/configuration.h index 3a00cc7..2c56d21 100644 --- a/capiocl/configuration.h +++ b/capiocl/configuration.h @@ -21,10 +21,14 @@ struct capiocl::configuration::defaults { static ConfigurationEntry DEFAULT_MONITOR_MCAST_PORT; /// @brief Multicast monitor delay before following operation static ConfigurationEntry DEFAULT_MONITOR_MCAST_DELAY; + /// @brief Enable multicast monitor by default + static ConfigurationEntry DEFAULT_MONITOR_MCAST_ENABLED; /// @brief Multicast monitor homenode IP static ConfigurationEntry DEFAULT_MONITOR_HOMENODE_IP; /// @brief Multicast monitor homenode PORT static ConfigurationEntry DEFAULT_MONITOR_HOMENODE_PORT; + /// @brief Enable File system monitor by default + static ConfigurationEntry DEFAULT_MONITOR_FS_ENABLED; }; /// @brief Load configuration and store it from a CAPIO-CL TOML configuration file @@ -47,15 +51,19 @@ class capiocl::configuration::CapioClConfiguration { void set(const ConfigurationEntry &entry); public: - explicit CapioClConfiguration(); ~CapioClConfiguration() = default; /** - * Load a configuiration from a TOML file + * Load a configuration from a TOML file * @param path */ void load(const std::filesystem::path &path); + /** + * Load default values for the configuration + */ + void loadDefaults(); + /** * Get a string value * @param key key of option to get diff --git a/doxygen/configuration.md b/doxygen/configuration.md index 96d49a2..dd929c6 100644 --- a/doxygen/configuration.md +++ b/doxygen/configuration.md @@ -18,13 +18,15 @@ The configuration file uses a structured namespace under the top-level table `mo Available configuration parameters: -| Key | Type | Default | Description | -|----------------------------------|----------|----------------|----------------------------------------------------------------------------------------------| -| `monitor.mcast.commit.ip` | string | `224.224.224.1` | Multicast IP address used for commit messages | -| `monitor.mcast.commit.port` | integer | `12345` | UDP port for commit messages | -| `monitor.mcast.delay_ms` | integer | `300` | Artificial delay (in milliseconds) inserted before sending multicast messages. Useful for debugging or simulating slower networks. | -| `monitor.mcast.homenode.ip` | string | `224.224.224.2` | IP address of the home node for monitoring operations | -| `monitor.mcast.homenode.port` | integer | `12345` | Port associated with the home node monitoring endpoint | +| Key | Type | Default | Description | +|-------------------------------|---------|-----------------|------------------------------------------------------------------------------------------------------------------------------------| +| `monitor.filesystem.enabled` | boolean | `false` | Enable FileSystem commit monitor | +| `monitor.mcast.enabled` | boolean | `false` | Enable Multicast commit monitor | +| `monitor.mcast.commit.ip` | string | `224.224.224.1` | Multicast IP address used for commit messages | +| `monitor.mcast.commit.port` | integer | `12345` | UDP port for commit messages | +| `monitor.mcast.delay_ms` | integer | `300` | Artificial delay (in milliseconds) inserted before sending multicast messages. Useful for debugging or simulating slower networks. | +| `monitor.mcast.homenode.ip` | string | `224.224.224.2` | IP address of the home node for monitoring operations | +| `monitor.mcast.homenode.port` | integer | `12345` | Port associated with the home node monitoring endpoint | --- @@ -34,7 +36,10 @@ Below is a complete example of a `config.toml` file: # Example CAPIO-CL TOML configuration + monitor.filesystem.enabled = true + [monitor.mcast] + enabled = true # Multicast settings for commit messages commit.ip = "224.224.224.1" @@ -52,10 +57,12 @@ Below is a complete example of a `config.toml` file: ## How CAPIO-CL Uses These Settings ### `commit.ip` and `commit.port` + These fields define where CAPIO-CL sends **commit broadcast messages**. Commit messages are used for consistency coordination across distributed nodes. ### `delay_ms` + A small configurable delay may help with: - debugging message-ordering issues, @@ -65,5 +72,6 @@ A small configurable delay may help with: A value of `0` means no delay. ### `homenode.ip` and `homenode.port` + These define the **central monitoring endpoint** (the “home node”). CAPIO-CL uses this endpoint to coordinate monitoring metadata and cluster-wide communication. diff --git a/src/Engine.cpp b/src/Engine.cpp index 0f0e7e2..b333d1e 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "capiocl.hpp" @@ -762,18 +763,40 @@ bool capiocl::engine::Engine::operator==(const capiocl::engine::Engine &other) c void capiocl::engine::Engine::loadConfiguration(const std::string &path) { configuration.load(path); - monitor.registerMonitorBackend(new monitor::MulticastMonitor(configuration)); - monitor.registerMonitorBackend(new monitor::FileSystemMonitor()); + std::string multicast_monitor_enabled, fs_monitor_enabled; + + try { + configuration.getParameter("monitor.mcast.enabled", &multicast_monitor_enabled); + } catch (...) { + multicast_monitor_enabled = "false"; + } + + if (multicast_monitor_enabled == "true") { + monitor.registerMonitorBackend(new monitor::MulticastMonitor(configuration)); + } else { + printer::print(printer::CLI_LEVEL_WARNING, "Skipping registration of MulticastMonitor"); + } + + try { + configuration.getParameter("monitor.filesystem.enabled", &fs_monitor_enabled); + } catch (...) { + fs_monitor_enabled = "false"; + } + + if (fs_monitor_enabled == "true") { + monitor.registerMonitorBackend(new monitor::FileSystemMonitor()); + } else { + printer::print(printer::CLI_LEVEL_WARNING, "Skipping registration of FileSystemMonitor"); + } } void capiocl::engine::Engine::useDefaultConfiguration() { + configuration.loadDefaults(); - const auto def_config = configuration::CapioClConfiguration(); - - monitor.registerMonitorBackend(new monitor::MulticastMonitor(def_config)); + // TODO: add a vector with registered instances of backends to avoid multiple instantiations + monitor.registerMonitorBackend(new monitor::MulticastMonitor(configuration)); monitor.registerMonitorBackend(new monitor::FileSystemMonitor()); } void capiocl::engine::Engine::startApiServer(const std::string &address, const int port) { - webapi_server = std::unique_ptr( - new webapi::CapioClWebApiServer(this, address, port)); + webapi_server = std::make_unique(this, address, port); } \ No newline at end of file diff --git a/src/configuration.cpp b/src/configuration.cpp index 39d96fc..abc3324 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -5,8 +5,9 @@ #include "capiocl/printer.h" #include "toml++/toml.hpp" -void flatten_table(const toml::table &tbl, std::unordered_map &map, - const std::string &prefix = "") { +void load_config_to_memory(const toml::table &tbl, + std::unordered_map &map, + const std::string &prefix = "") { for (const auto &[key, value] : tbl) { std::string full_key; if (prefix.empty()) { @@ -16,10 +17,16 @@ void flatten_table(const toml::table &tbl, std::unordered_mapget(); + } else if (value.is_boolean()) { + if (value.as_boolean()->get()) { + map[full_key] = "true"; + } else { + map[full_key] = "false"; + } } else { map[full_key] = std::to_string(value.as_integer()->get()); } @@ -27,12 +34,14 @@ void flatten_table(const toml::table &tbl, std::unordered_mapset(defaults::DEFAULT_MONITOR_MCAST_IP); this->set(defaults::DEFAULT_MONITOR_MCAST_PORT); this->set(defaults::DEFAULT_MONITOR_HOMENODE_IP); this->set(defaults::DEFAULT_MONITOR_HOMENODE_PORT); this->set(defaults::DEFAULT_MONITOR_MCAST_DELAY); + this->set(defaults::DEFAULT_MONITOR_FS_ENABLED); + this->set(defaults::DEFAULT_MONITOR_MCAST_ENABLED); } void capiocl::configuration::CapioClConfiguration::set(const std::string &key, std::string value) { @@ -54,7 +63,9 @@ void capiocl::configuration::CapioClConfiguration::load(const std::filesystem::p } catch (const toml::parse_error &err) { throw CapioClConfigurationException(err.what()); } - flatten_table(tbl, config); + + // copy into the local configuration the parameter from the toml config file + load_config_to_memory(tbl, config); } void capiocl::configuration::CapioClConfiguration::getParameter(const std::string &key, diff --git a/src/defaults.cpp b/src/defaults.cpp index 64d357f..b27d922 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -1,4 +1,4 @@ -#include "capiocl//configuration.h" +#include "capiocl/configuration.h" ConfigurationEntry capiocl::configuration::defaults::DEFAULT_MONITOR_MCAST_IP{ "monitor.mcast.commit.ip", "224.224.224.1"}; @@ -14,3 +14,9 @@ ConfigurationEntry capiocl::configuration::defaults::DEFAULT_MONITOR_HOMENODE_IP ConfigurationEntry capiocl::configuration::defaults::DEFAULT_MONITOR_HOMENODE_PORT{ "monitor.mcast.homenode.port", "12345"}; + +ConfigurationEntry capiocl::configuration::defaults::DEFAULT_MONITOR_MCAST_ENABLED{ + "monitor.mcast.enabled", "true"}; + +ConfigurationEntry capiocl::configuration::defaults::DEFAULT_MONITOR_FS_ENABLED{ + "monitor.filesystem.enabled", "true"}; \ No newline at end of file diff --git a/tests/cpp/test_configuration.hpp b/tests/cpp/test_configuration.hpp index 18a94c6..d752e67 100644 --- a/tests/cpp/test_configuration.hpp +++ b/tests/cpp/test_configuration.hpp @@ -38,4 +38,24 @@ TEST(CONFIGURATION_SUITE_NAME, TestFailureParsingTOML) { capiocl::configuration::CapioClConfigurationException); } +TEST(CONFIGURATION_SUITE_NAME, testNoBackendLoaded) { + capiocl::engine::Engine engine(false); + engine.loadConfiguration("/tmp/capio_cl_tomls/sample2.toml"); + + EXPECT_FALSE(engine.isCommitted("test")); +} + +TEST(CONFIGURATION_SUITE_NAME, testNoBackendLoadedWithExplicitNoLoadOption) { + capiocl::configuration::CapioClConfiguration config; + config.load("/tmp/capio_cl_tomls/sample3.toml"); + + std::string value; + config.getParameter("monitor.mcast.enabled", &value); + EXPECT_TRUE("false" == value); + value = ""; + + config.getParameter("monitor.filesystem.enabled", &value); + EXPECT_TRUE("false" == value); +} + #endif // CAPIO_CL_TEST_CONFIGURATION_HPP diff --git a/tests/tomls/sample1.toml b/tests/tomls/sample1.toml index b8e486f..6098bed 100644 --- a/tests/tomls/sample1.toml +++ b/tests/tomls/sample1.toml @@ -1,5 +1,9 @@ +[monitor.filesystem] +enabled = true + [monitor.mcast] delay_ms = 300 +enabled = true [monitor.mcast.commit] ip = "224.224.224.3" diff --git a/tests/tomls/sample2.toml b/tests/tomls/sample2.toml new file mode 100644 index 0000000..b8e486f --- /dev/null +++ b/tests/tomls/sample2.toml @@ -0,0 +1,10 @@ +[monitor.mcast] +delay_ms = 300 + +[monitor.mcast.commit] +ip = "224.224.224.3" +port = 11223 + +[monitor.mcast.homenode] +ip = "224.224.224.4" +port = 22334 diff --git a/tests/tomls/sample3.toml b/tests/tomls/sample3.toml new file mode 100644 index 0000000..ee66795 --- /dev/null +++ b/tests/tomls/sample3.toml @@ -0,0 +1,3 @@ +[monitor] +filesystem.enabled = false +mcast.enabled = false