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.
This commit is contained in:
Markus Makela 2015-12-01 10:11:29 +02:00
parent 1c1d4b205a
commit 2e127ef38e

View File

@ -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