Fix readwritesplit server selection

If a server with zero weight was chosen as the only candidate, it was
possible that the starting minimum value was smaller than the server
score. This would mean that a candidate wouldn't be chosen if the score
was too high. To preven this, the values are capped to a value smaller
than the initial minimum score.
This commit is contained in:
Markus Mäkelä 2019-03-26 23:44:37 +02:00
parent 906f0ed3cd
commit 5ee9b74770
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19

View File

@ -53,6 +53,7 @@ static bool valid_for_slave(const RWBackend* backend, const RWBackend* master)
PRWBackends::iterator best_score(PRWBackends& sBackends,
std::function<double(SERVER_REF* server)> server_score)
{
const double max_score = std::nexttoward(std::numeric_limits<double>::max(), 0.0);
double min {std::numeric_limits<double>::max()};
auto best = sBackends.end();
for (auto ite = sBackends.begin(); ite != sBackends.end(); ++ite)
@ -65,6 +66,13 @@ PRWBackends::iterator best_score(PRWBackends& sBackends,
score = (score + 5.0) * 1.5;
}
if (score > max_score)
{
// Cap values to a maximum value. This guarantees that we choose a server from the set of
// available candidates.
score = max_score;
}
if (min > score)
{
min = score;
@ -72,6 +80,9 @@ PRWBackends::iterator best_score(PRWBackends& sBackends,
}
}
mxb_assert_message(best != sBackends.end() || sBackends.empty(),
"A candidate must be chosen if we have candidates");
return best;
}