From 1184b6dce04b0c5b735ceb40c61fe12e24c26ba8 Mon Sep 17 00:00:00 2001 From: Kai-Uwe Hermann Date: Tue, 26 Sep 2023 15:06:46 +0200 Subject: [PATCH] Make controller RPC timeout configurable Also make it available to the admin panel Signed-off-by: Kai-Uwe Hermann --- include/framework/runtime.hpp | 2 ++ lib/runtime.cpp | 7 +++++++ schemas/config.yaml | 2 ++ src/controller/command_api.cpp | 2 ++ src/controller/command_api.hpp | 1 + src/controller/controller.cpp | 1 + src/controller/rpc.cpp | 4 ++-- src/controller/rpc.hpp | 1 + src/manager.cpp | 1 + 9 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/framework/runtime.hpp b/include/framework/runtime.hpp index cdf3afa5..253bb7cb 100644 --- a/include/framework/runtime.hpp +++ b/include/framework/runtime.hpp @@ -72,6 +72,7 @@ inline constexpr auto LOGGING_CONFIG_NAME = "default_logging.cfg"; inline constexpr auto WWW_DIR = "www"; inline constexpr auto CONTROLLER_PORT = 8849; +inline constexpr auto CONTROLLER_RPC_TIMEOUT_MS = 2000; inline constexpr auto MQTT_BROKER_HOST = "localhost"; inline constexpr auto MQTT_BROKER_PORT = 1883; inline constexpr auto MQTT_EVEREST_PREFIX = "everest"; @@ -103,6 +104,7 @@ struct RuntimeSettings { fs::path config_file; fs::path www_dir; int controller_port; + int controller_rpc_timeout_ms; std::string mqtt_broker_host; int mqtt_broker_port; std::string mqtt_everest_prefix; diff --git a/lib/runtime.cpp b/lib/runtime.cpp index fbcb5fb4..c5e4b8d5 100644 --- a/lib/runtime.cpp +++ b/lib/runtime.cpp @@ -240,6 +240,13 @@ RuntimeSettings::RuntimeSettings(const std::string& prefix_, const std::string& controller_port = defaults::CONTROLLER_PORT; } + const auto settings_controller_rpc_timeout_ms_it = settings.find("controller_rpc_timeout_ms"); + if (settings_controller_rpc_timeout_ms_it != settings.end()) { + controller_rpc_timeout_ms = settings_controller_rpc_timeout_ms_it->get(); + } else { + controller_rpc_timeout_ms = defaults::CONTROLLER_RPC_TIMEOUT_MS; + } + const auto settings_mqtt_broker_host_it = settings.find("mqtt_broker_host"); if (settings_mqtt_broker_host_it != settings.end()) { mqtt_broker_host = settings_mqtt_broker_host_it->get(); diff --git a/schemas/config.yaml b/schemas/config.yaml index eda2af80..43eaa649 100644 --- a/schemas/config.yaml +++ b/schemas/config.yaml @@ -45,6 +45,8 @@ properties: type: string controller_port: type: integer + controller_rpc_timeout_ms: + type: integer mqtt_broker_host: type: string mqtt_broker_port: diff --git a/src/controller/command_api.cpp b/src/controller/command_api.cpp index abd17292..aae2511a 100644 --- a/src/controller/command_api.cpp +++ b/src/controller/command_api.cpp @@ -123,6 +123,8 @@ nlohmann::json CommandApi::handle(const std::string& cmd, const json& params) { this->rpc.ipc_request("restart_modules", nullptr, true); return nullptr; + } else if (cmd == "get_rpc_timeout") { + return this->config.controller_rpc_timeout_ms; } throw CommandApiMethodNotFound(fmt::format("Command '{}' unknown", cmd)); diff --git a/src/controller/command_api.hpp b/src/controller/command_api.hpp index 20115f18..9c6abace 100644 --- a/src/controller/command_api.hpp +++ b/src/controller/command_api.hpp @@ -24,6 +24,7 @@ class CommandApi { std::string module_dir; std::string interface_dir; std::string configs_dir; + int controller_rpc_timeout_ms; }; CommandApi(const Config& config, RPC& rpc); diff --git a/src/controller/controller.cpp b/src/controller/controller.cpp index 1bf5f522..674e1710 100644 --- a/src/controller/controller.cpp +++ b/src/controller/controller.cpp @@ -42,6 +42,7 @@ int main(int argc, char* argv[]) { config_params.at("module_dir"), config_params.at("interface_dir"), config_params.at("configs_dir"), + config_params.at("controller_rpc_timeout_ms"), }; RPC rpc(socket_fd, config); diff --git a/src/controller/rpc.cpp b/src/controller/rpc.cpp index d02c1289..c7436c3a 100644 --- a/src/controller/rpc.cpp +++ b/src/controller/rpc.cpp @@ -91,6 +91,7 @@ class RPCRequest { RPC::RPC(int ipc_fd, const CommandApi::Config& config) : ipc_fd(ipc_fd) { this->api = std::make_unique(config, *this); + this->rpc_timeout = std::chrono::milliseconds(config.controller_rpc_timeout_ms); } void RPC::run(const NotificationHandler& notification_handler) { @@ -192,8 +193,7 @@ nlohmann::json RPC::ipc_request(const std::string& method, const nlohmann::json& Everest::controller_ipc::send_message(this->ipc_fd, {{"method", method}, {"params", params}, {"id", id}}); - // FIXME (aw): timeout constant! - auto status = call_result_future.wait_for(std::chrono::milliseconds(2000)); + auto status = call_result_future.wait_for(this->rpc_timeout); lock.lock(); this->ipc_calls.erase(new_call.first); diff --git a/src/controller/rpc.hpp b/src/controller/rpc.hpp index de40dfe1..7d8461f5 100644 --- a/src/controller/rpc.hpp +++ b/src/controller/rpc.hpp @@ -29,6 +29,7 @@ class RPC { int ipc_fd; std::unique_ptr api; NotificationHandler notification_handler; + std::chrono::milliseconds rpc_timeout; // FIXME (aw): what type of cafe? std::mt19937 rng{0xcafe}; diff --git a/src/manager.cpp b/src/manager.cpp index 04f330c4..75350715 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -486,6 +486,7 @@ static ControllerHandle start_controller(const RuntimeSettings& rs) { {"configs_dir", rs.configs_dir.string()}, {"logging_config_file", rs.logging_config_file.string()}, {"controller_port", rs.controller_port}, + {"controller_rpc_timeout_ms", rs.controller_rpc_timeout_ms}, }}, });