From b8d3da496845664acd700489ab12ce494cf02458 Mon Sep 17 00:00:00 2001 From: Esa Korhonen Date: Thu, 8 Feb 2018 18:43:27 +0200 Subject: [PATCH] Add error tolerance to "servers_no_promotion" Previously, if the list contained servers that were not monitored by the monitor yet were valid servers, an error value would be returned and the monitor failed to start. With this update, the non-monitored servers are simply ignored when forming the final list. Also, added printing of the list to diagnostics. --- include/maxscale/monitor.h | 9 +++--- server/core/monitor.cc | 28 +++++++++--------- .../modules/monitor/mariadbmon/mysql_mon.cc | 29 +++++++++++++++++-- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/include/maxscale/monitor.h b/include/maxscale/monitor.h index 4b0b56941..e23cea728 100644 --- a/include/maxscale/monitor.h +++ b/include/maxscale/monitor.h @@ -385,14 +385,15 @@ void load_server_journal(MXS_MONITOR *monitor, MXS_MONITORED_SERVER **master); MXS_MONITORED_SERVER* mon_get_monitored_server(const MXS_MONITOR* mon, SERVER* search_server); /** - * Get an array of monitored servers. All the servers defined in the config setting must be monitored by - * the given monitor. + * Get an array of monitored servers. If a server defined in the config setting is not monitored by + * the given monitor, that server is ignored and not inserted into the output array. * * @param params Config parameters * @param key Setting name * @param mon Monitor which should monitor the servers - * @param monitored_servers_out Where to save output. The caller should free the array, but not the elements. - * @return Output array size if successful, negative value otherwise + * @param monitored_servers_out Where to save output array. The caller should free the array, but not the + * elements. The output must contain NULL before calling this function. + * @return Output array size. */ int mon_config_get_servers(const MXS_CONFIG_PARAMETER* params, const char* key, const MXS_MONITOR* mon, MXS_MONITORED_SERVER*** monitored_array_out); diff --git a/server/core/monitor.cc b/server/core/monitor.cc index 67c97656a..113f56b80 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -2444,41 +2444,41 @@ MXS_MONITORED_SERVER* mon_get_monitored_server(const MXS_MONITOR* mon, SERVER* s int mon_config_get_servers(const MXS_CONFIG_PARAMETER* params, const char* key, const MXS_MONITOR* mon, MXS_MONITORED_SERVER*** monitored_servers_out) { - ss_dassert(*monitored_servers_out == NULL); + ss_dassert(monitored_servers_out != NULL && *monitored_servers_out == NULL); SERVER** servers = NULL; int servers_size = config_get_server_list(params, key, &servers); - int rval = 0; + int found = 0; // All servers in the array must be monitored by the given monitor. if (servers_size > 0) { MXS_MONITORED_SERVER** monitored_array = (MXS_MONITORED_SERVER**)MXS_CALLOC(servers_size, sizeof(MXS_MONITORED_SERVER*)); - bool error = false; - for (int i = 0; i < servers_size && !error; i++) + for (int i = 0; i < servers_size; i++) { MXS_MONITORED_SERVER* mon_serv = mon_get_monitored_server(mon, servers[i]); if (mon_serv != NULL) { - monitored_array[i] = mon_serv; + monitored_array[found++] = mon_serv; } else { - MXS_ERROR("Server '%s' is not monitored by monitor '%s'.", servers[i]->unique_name, mon->name); - error = true; + MXS_WARNING("Server '%s' is not monitored by monitor '%s'.", + servers[i]->unique_name, mon->name); } } MXS_FREE(servers); - if (error) + ss_dassert(found <= servers_size); + if (found == 0) { MXS_FREE(monitored_array); - rval = -1; + monitored_array = NULL; } - else + else if (found < servers_size) { - *monitored_servers_out = monitored_array; - rval = servers_size; + monitored_array = (MXS_MONITORED_SERVER**)MXS_REALLOC(monitored_array, found); } + *monitored_servers_out = monitored_array; } - return rval; -} \ No newline at end of file + return found; +} diff --git a/server/modules/monitor/mariadbmon/mysql_mon.cc b/server/modules/monitor/mariadbmon/mysql_mon.cc index 7a9554d1e..49b0512cf 100644 --- a/server/modules/monitor/mariadbmon/mysql_mon.cc +++ b/server/modules/monitor/mariadbmon/mysql_mon.cc @@ -1176,6 +1176,21 @@ static bool stop_monitor(MXS_MONITOR* mon) return actually_stopped; } +static string monitored_servers_to_string(MXS_MONITORED_SERVER** array, size_t array_size) +{ + string rval; + if (array_size > 0) + { + const char* separator = ""; + for (size_t i = 0; i < array_size; i++) + { + rval += separator; + rval += array[i]->server->unique_name; + separator = ","; + } + } + return rval; +} /** * Daignostic interface * @@ -1196,8 +1211,14 @@ static void diagnostics(DCB *dcb, const MXS_MONITOR *mon) "Enabled" : "Disabled"); dcb_printf(dcb, "Detect stale master: %s\n", (handle->detectStaleMaster == 1) ? "Enabled" : "Disabled"); - dcb_printf(dcb, "\nServer information:\n-------------------\n\n"); + if (handle->n_excluded > 0) + { + dcb_printf(dcb, "Non-promotable servers (failover): "); + dcb_printf(dcb, "%s\n", + monitored_servers_to_string(handle->excluded_servers, handle->n_excluded).c_str()); + } + dcb_printf(dcb, "\nServer information:\n-------------------\n\n"); for (MXS_MONITORED_SERVER *db = mon->monitored_servers; db; db = db->next) { MySqlServerInfo *serv_info = get_server_info(handle, db); @@ -1265,7 +1286,11 @@ static json_t* diagnostics_json(const MXS_MONITOR *mon) { json_object_set_new(rval, "script", json_string(handle->script)); } - + if (handle->n_excluded > 0) + { + string list = monitored_servers_to_string(handle->excluded_servers, handle->n_excluded); + json_object_set_new(rval, CN_NO_PROMOTE_SERVERS, json_string(list.c_str())); + } if (mon->monitored_servers) { json_t* arr = json_array();