From 2e127ef38eddaef53bfc95ced5c8a17798191825 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Tue, 1 Dec 2015 10:11:29 +0200 Subject: [PATCH] Fix to MXS-429: Missing parameter no longer causes a crash If a server is missing a weighting parameter the default weight will be used. --- server/modules/routing/readconnroute.c | 105 +++++++++++++++---------- 1 file changed, 65 insertions(+), 40 deletions(-) diff --git a/server/modules/routing/readconnroute.c b/server/modules/routing/readconnroute.c index 6433d768e..299e422b2 100644 --- a/server/modules/routing/readconnroute.c +++ b/server/modules/routing/readconnroute.c @@ -248,46 +248,71 @@ char *weightby; } inst->servers[n] = NULL; - if ((weightby = serviceGetWeightingParameter(service)) != NULL) - { - int total = 0; - for (n = 0; inst->servers[n]; n++) - { - backend = inst->servers[n]; - total += atoi(serverGetParameter(backend->server, - weightby)); - } - if (total == 0) - { - MXS_WARNING("Weighting Parameter for service '%s' " - "will be ignored as no servers have values " - "for the parameter '%s'.\n", - service->name, weightby); - } - else - { - for (n = 0; inst->servers[n]; n++) - { - int perc, wght; - backend = inst->servers[n]; - perc = ((wght = atoi(serverGetParameter(backend->server, - weightby))) * 1000) / total; - if (perc == 0 && wght != 0) - perc = 1; - backend->weight = perc; - if (perc == 0) - { - MXS_ERROR("Server '%s' has no value " - "for weighting parameter '%s', " - "no queries will be routed to " - "this server.\n", - inst->servers[n]->server->unique_name, - weightby); - } - - } - } - } + if ((weightby = serviceGetWeightingParameter(service)) != NULL) + { + int total = 0; + + for (int n = 0; inst->servers[n]; n++) + { + BACKEND *backend = inst->servers[n]; + char *param = serverGetParameter(backend->server, weightby); + if (param) + { + total += atoi(param); + } + } + if (total == 0) + { + MXS_WARNING("Weighting Parameter for service '%s' " + "will be ignored as no servers have values " + "for the parameter '%s'.", + service->name, weightby); + } + else if (total < 0) + { + MXS_ERROR("Sum of weighting parameter '%s' for service '%s' exceeds " + "maximum value of %d. Weighting will be ignored.", + weightby, service->name, INT_MAX); + } + else + { + for (int n = 0; inst->servers[n]; n++) + { + BACKEND *backend = inst->servers[n]; + char *param = serverGetParameter(backend->server, weightby); + if (param) + { + int wght = atoi(param); + int perc = (wght * 1000) / total; + + if (perc == 0) + { + perc = 1; + MXS_ERROR("Weighting parameter '%s' with a value of %d for" + " server '%s' rounds down to zero with total weight" + " of %d for service '%s'. No queries will be " + "routed to this server.", weightby, wght, + backend->server->unique_name, total, + service->name); + } + else if (perc < 0) + { + MXS_ERROR("Weighting parameter '%s' for server '%s' is too large, " + "maximum value is %d. No weighting will be used for this server.", + weightby, backend->server->unique_name, INT_MAX / 1000); + perc = 1000; + } + backend->weight = perc; + } + else + { + MXS_WARNING("Server '%s' has no parameter '%s' used for weighting" + " for service '%s'.", backend->server->unique_name, + weightby, service->name); + } + } + } + } /* * Process the options