MXS-2313: Use 64-bit integers to store rank

Although the default value is the maximum value of a signed 32-bit
integer, the value is stored as a 64-bit integer. The integer type
conversion functions return 64-bit values so storing it as one makes
sense.

Currently values higher than the default are allowed but the accepted
range of input should be restricted in the future.
This commit is contained in:
Markus Mäkelä
2019-03-10 11:17:58 +02:00
parent aeca0f8a31
commit 9b6b5270f1
7 changed files with 22 additions and 21 deletions

View File

@ -282,7 +282,7 @@ public:
*
* @return The server rank
*/
virtual int rank() const = 0;
virtual int64_t rank() const = 0;
/*
* Update server address. TODO: Move this to internal class once blr is gone.

View File

@ -425,7 +425,7 @@ const MXS_MODULE_PARAM config_server_params[] =
{CN_SSL_CERT_VERIFY_DEPTH, MXS_MODULE_PARAM_COUNT, "9"},
{CN_SSL_VERIFY_PEER_CERTIFICATE, MXS_MODULE_PARAM_BOOL, "true"},
{CN_DISK_SPACE_THRESHOLD, MXS_MODULE_PARAM_STRING},
{CN_RANK, MXS_MODULE_PARAM_INT, "-1"},
{CN_RANK, MXS_MODULE_PARAM_COUNT, "2147483647"},
{NULL}
};

View File

@ -621,9 +621,13 @@ bool runtime_alter_server(Server* server, const char* key, const char* value)
}
if (strcmp(key, CN_RANK) == 0)
{
if (is_valid_integer(value))
if (auto i = get_positive_int(value))
{
server->set_rank(atoi(value));
server->set_rank(i);
}
else
{
config_runtime_error("Invalid value for '%s': %s", CN_RANK, value);
}
}
else

View File

@ -62,7 +62,7 @@ public:
m_settings.persistmaxtime = persistmaxtime;
}
void set_rank(int rank)
void set_rank(int64_t rank)
{
m_settings.rank = rank;
}
@ -122,7 +122,7 @@ public:
return m_settings.authenticator;
}
int rank() const override
int64_t rank() const override
{
return m_settings.rank;
}
@ -341,7 +341,7 @@ private:
long persistpoolmax = 0; /**< Maximum size of persistent connections pool */
long persistmaxtime = 0; /**< Maximum number of seconds connection can live */
int rank; /*< The ranking of this server, used to prioritize certain servers over others */
int64_t rank; /*< The ranking of this server, used to prioritize certain servers over others */
/** Disk space thresholds. Can be queried from modules at any time so access must be protected
* by mutex. */

View File

@ -252,11 +252,8 @@ Server* Server::server_alloc(const char* name, MXS_CONFIG_PARAMETER* params)
server->persistent = persistent;
server->last_event = SERVER_UP_EVENT;
server->status = SERVER_RUNNING;
if ((server->m_settings.rank = params->get_integer(CN_RANK)) == -1)
{
server->m_settings.rank = std::numeric_limits<int>::max();
}
server->m_settings.rank = params->get_integer(CN_RANK);
mxb_assert(server->m_settings.rank > 0);
if (!monuser.empty())
{

View File

@ -83,14 +83,14 @@ extern "C" MXS_MODULE* MXS_CREATE_MODULE()
*/
SERVER_REF* RCR::get_root_master()
{
int best_rank {std::numeric_limits<int>::max()};
auto best_rank = std::numeric_limits<int64_t>::max();
SERVER_REF* master_host = nullptr;
for (SERVER_REF* ref = m_pService->dbref; ref; ref = ref->next)
{
if (server_ref_is_active(ref) && ref->server->is_master())
{
int rank = ref->server->rank();
auto rank = ref->server->rank();
if (!master_host)
{
@ -230,7 +230,7 @@ RCRSession* RCR::newSession(MXS_SESSION* session)
* connection router.
*/
SERVER_REF* candidate = nullptr;
int best_rank {std::numeric_limits<int>::max()};
auto best_rank = std::numeric_limits<int64_t>::max();
/*
* Loop over all the servers and find any that have fewer connections

View File

@ -243,14 +243,14 @@ PRWBackends::iterator find_best_backend(PRWBackends& backends,
{
// Group backends by priority and rank (lower is better). The set of best backends will then compete.
int best_priority {INT_MAX};
int best_rank {std::numeric_limits<int>::max()};
auto best_rank = std::numeric_limits<int64_t>::max();
thread_local PRWBackends candidates;
candidates.clear();
for (auto& psBackend : backends)
{
int priority = get_backend_priority(psBackend, masters_accepts_reads);
int rank = psBackend->server()->rank();
auto rank = psBackend->server()->rank();
if (rank < best_rank || (rank == best_rank && priority < best_priority))
{
@ -271,9 +271,9 @@ PRWBackends::iterator find_best_backend(PRWBackends& backends,
return rval;
}
void add_backend_with_rank(RWBackend* backend, PRWBackends* candidates, int* best_rank)
void add_backend_with_rank(RWBackend* backend, PRWBackends* candidates, int64_t* best_rank)
{
int rank = backend->server()->rank();
auto rank = backend->server()->rank();
if (rank < *best_rank)
{
@ -366,7 +366,7 @@ RWBackend* get_root_master(const PRWBackends& backends, RWBackend* current_maste
thread_local PRWBackends candidates;
candidates.clear();
int best_rank {std::numeric_limits<int>::max()};
auto best_rank = std::numeric_limits<int64_t>::max();
for (const auto& backend : backends)
{
@ -449,7 +449,7 @@ bool RWSplitSession::open_connections()
int n_slaves = get_slave_counts(m_raw_backends, master).second;
int max_nslaves = m_router->max_slave_count();
int best_rank {std::numeric_limits<int>::max()};
auto best_rank = std::numeric_limits<int64_t>::max();
PRWBackends candidates;
mxb_assert(n_slaves <= max_nslaves || max_nslaves == 0);