From c2fc80f122490bf6f44b492c50cf5fcb0b6adf7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 20 Mar 2019 18:17:08 +0200 Subject: [PATCH] Fix monitor creation When the monitor was created, no parameters were passed to it. --- server/core/config_runtime.cc | 45 ++++++++++++++------- server/core/internal/config_runtime.hh | 6 ++- server/modules/routing/debugcli/debugcmd.cc | 2 +- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/server/core/config_runtime.cc b/server/core/config_runtime.cc index c4be67836..d7f0f197c 100644 --- a/server/core/config_runtime.cc +++ b/server/core/config_runtime.cc @@ -1219,7 +1219,7 @@ bool runtime_destroy_listener(Service* service, const char* name) return rval; } -bool runtime_create_monitor(const char* name, const char* module) +bool runtime_create_monitor(const char* name, const char* module, MXS_CONFIG_PARAMETER* params) { std::lock_guard guard(crt_lock); bool rval = false; @@ -1235,13 +1235,18 @@ bool runtime_create_monitor(const char* name, const char* module) } else if (config_is_valid_name(name, &reason)) { - MXS_CONFIG_PARAMETER params; + MXS_CONFIG_PARAMETER final_params; bool ok; - tie(ok, params) = load_defaults(module, MODULE_MONITOR, CN_MONITOR); + tie(ok, final_params) = load_defaults(module, MODULE_MONITOR, CN_MONITOR); if (ok) { - if ((monitor = MonitorManager::create_monitor(name, module, ¶ms)) == NULL) + if (params) + { + final_params.set_multiple(*params); + } + + if ((monitor = MonitorManager::create_monitor(name, module, &final_params)) == NULL) { config_runtime_error("Could not create monitor '%s' with module '%s'", name, module); } @@ -2254,6 +2259,24 @@ static bool validate_monitor_json(json_t* json) return rval; } +MXS_CONFIG_PARAMETER extract_parameters(json_t* json) +{ + MXS_CONFIG_PARAMETER params; + + if (json_t* parameters = mxs_json_pointer(json, MXS_JSON_PTR_PARAMETERS)) + { + const char* key; + json_t* value; + + json_object_foreach(parameters, key, value) + { + params.set(key, mxs::json_to_string(value)); + } + } + + return params; +} + Monitor* runtime_create_monitor_from_json(json_t* json) { Monitor* rval = NULL; @@ -2263,21 +2286,13 @@ Monitor* runtime_create_monitor_from_json(json_t* json) { const char* name = json_string_value(mxs_json_pointer(json, MXS_JSON_PTR_ID)); const char* module = json_string_value(mxs_json_pointer(json, MXS_JSON_PTR_MODULE)); + auto params = extract_parameters(json); - if (runtime_create_monitor(name, module)) + if (runtime_create_monitor(name, module, ¶ms)) { rval = MonitorManager::find_monitor(name); mxb_assert(rval); - - if (!runtime_alter_monitor_from_json(rval, json)) - { - runtime_destroy_monitor(rval); - rval = NULL; - } - else - { - MonitorManager::start_monitor(rval); - } + MonitorManager::start_monitor(rval); } } diff --git a/server/core/internal/config_runtime.hh b/server/core/internal/config_runtime.hh index 848fee96f..f4a15adb8 100644 --- a/server/core/internal/config_runtime.hh +++ b/server/core/internal/config_runtime.hh @@ -217,11 +217,13 @@ bool runtime_destroy_listener(Service* service, const char* name); /** * @brief Create a new monitor * - * @param name Name of the monitor + * @param name Name of the monitor * @param module Monitor module + * @param params Parameters for the monitor + * * @return True if new monitor was created and persisted */ -bool runtime_create_monitor(const char* name, const char* module); +bool runtime_create_monitor(const char* name, const char* module, MXS_CONFIG_PARAMETER* params); /** * @brief Create a new filter diff --git a/server/modules/routing/debugcli/debugcmd.cc b/server/modules/routing/debugcli/debugcmd.cc index 507700326..df9bcd98d 100644 --- a/server/modules/routing/debugcli/debugcmd.cc +++ b/server/modules/routing/debugcli/debugcmd.cc @@ -1299,7 +1299,7 @@ static void createMonitor(DCB* dcb, const char* name, const char* module) { dcb_printf(dcb, "Monitor '%s' already exists\n", name); } - else if (runtime_create_monitor(name, module)) + else if (runtime_create_monitor(name, module, nullptr)) { dcb_printf(dcb, "Created monitor '%s'\n", name); }