MXS-1703 Rename MySqlServerInfo, prepare to use it as the primary class

Renamed to MariaDBServer. The objects have a pointer to the underlying
MXS_MONITORED_SERVER. The purpose is to have the monitor mainly use
MariaDBServer instead of the current mix of MXS_MONITORED_SERVER* and
MySqlServerInfo and to simplify the mapping between the two. Also,
many methods can be moved to the MariaDBServer class later on.

Some functions have been converted from MXS_MONITORED_SERVER* to
MariaDBServer.
This commit is contained in:
Esa Korhonen
2018-03-16 11:26:13 +02:00
parent d03787b2c9
commit 0b5601a6c8
6 changed files with 129 additions and 113 deletions

View File

@ -16,7 +16,7 @@
#include <maxscale/mysql_utils.h> #include <maxscale/mysql_utils.h>
static int add_slave_to_master(long *slaves_list, int list_size, long node_id); static int add_slave_to_master(long *slaves_list, int list_size, long node_id);
static void read_server_variables(MXS_MONITORED_SERVER* database, MySqlServerInfo* serv_info); static void read_server_variables(MariaDBServer* serv_info);
static bool report_version_err = true; static bool report_version_err = true;
@ -239,7 +239,7 @@ MXS_MONITORED_SERVER* MariaDBMonitor::get_replication_tree(int num_servers)
m_master = master_cand; m_master = master_cand;
} }
MySqlServerInfo* info = get_server_info(master_cand); MariaDBServer* info = get_server_info(master_cand);
if (SERVER_IS_RUNNING(master_cand->server)) if (SERVER_IS_RUNNING(master_cand->server))
{ {
@ -354,7 +354,7 @@ MXS_MONITORED_SERVER* getSlaveOfNodeId(MXS_MONITORED_SERVER *ptr, long node_id,
return NULL; return NULL;
} }
bool MariaDBMonitor::do_show_slave_status(MySqlServerInfo* serv_info, MXS_MONITORED_SERVER* database) bool MariaDBMonitor::do_show_slave_status(MariaDBServer* serv_info, MXS_MONITORED_SERVER* database)
{ {
/** Column positions for SHOW SLAVE STATUS */ /** Column positions for SHOW SLAVE STATUS */
const size_t MYSQL55_STATUS_MASTER_LOG_POS = 5; const size_t MYSQL55_STATUS_MASTER_LOG_POS = 5;
@ -537,7 +537,7 @@ struct graph_node
int cycle; int cycle;
bool active; bool active;
struct graph_node *parent; struct graph_node *parent;
MySqlServerInfo *info; MariaDBServer *info;
MXS_MONITORED_SERVER *db; MXS_MONITORED_SERVER *db;
}; };
@ -746,12 +746,13 @@ void find_graph_cycles(MariaDBMonitor *handle, MXS_MONITORED_SERVER *database, i
} }
/** /**
* Monitor an individual server. * Monitor an individual server. TODO: this will likely end up as method of MariaDBServer class.
* *
* @param database The database to probe * @param database The database to probe
*/ */
void MariaDBMonitor::monitor_database(MXS_MONITORED_SERVER *database) void MariaDBMonitor::monitor_database(MariaDBServer* serv_info)
{ {
MXS_MONITORED_SERVER* database = serv_info->server_base;
/* Don't probe servers in maintenance mode */ /* Don't probe servers in maintenance mode */
if (SERVER_IN_MAINT(database->server)) if (SERVER_IN_MAINT(database->server))
{ {
@ -796,7 +797,6 @@ void MariaDBMonitor::monitor_database(MXS_MONITORED_SERVER *database)
server_set_status_nolock(database->server, SERVER_RUNNING); server_set_status_nolock(database->server, SERVER_RUNNING);
monitor_set_pending_status(database, SERVER_RUNNING); monitor_set_pending_status(database, SERVER_RUNNING);
MySqlServerInfo *serv_info = get_server_info(database);
/* Check whether current server is MaxScale Binlog Server */ /* Check whether current server is MaxScale Binlog Server */
MYSQL_RES *result; MYSQL_RES *result;
if (mxs_mysql_query(database->con, "SELECT @@maxscale_version") == 0 && if (mxs_mysql_query(database->con, "SELECT @@maxscale_version") == 0 &&
@ -827,22 +827,22 @@ void MariaDBMonitor::monitor_database(MXS_MONITORED_SERVER *database)
serv_info->version = MYSQL_SERVER_VERSION_51; serv_info->version = MYSQL_SERVER_VERSION_51;
} }
/* Query a few settings. */ /* Query a few settings. */
read_server_variables(database, serv_info); read_server_variables(serv_info);
/* If gtid domain exists and server is 10.0, update gtid:s */ /* If gtid domain exists and server is 10.0, update gtid:s */
if (m_master_gtid_domain >= 0 && serv_info->version == MYSQL_SERVER_VERSION_100) if (m_master_gtid_domain >= 0 && serv_info->version == MYSQL_SERVER_VERSION_100)
{ {
update_gtids(database, serv_info); update_gtids(serv_info);
} }
/* Check for MariaDB 10.x.x and get status for multi-master replication */ /* Check for MariaDB 10.x.x and get status for multi-master replication */
if (serv_info->version == MYSQL_SERVER_VERSION_100 || serv_info->version == MYSQL_SERVER_VERSION_55) if (serv_info->version == MYSQL_SERVER_VERSION_100 || serv_info->version == MYSQL_SERVER_VERSION_55)
{ {
monitor_mysql_db(database, serv_info); monitor_mysql_db(serv_info);
} }
else else
{ {
if (m_mysql51_replication) if (m_mysql51_replication)
{ {
monitor_mysql_db(database, serv_info); monitor_mysql_db(serv_info);
} }
else if (report_version_err) else if (report_version_err)
{ {
@ -855,13 +855,13 @@ void MariaDBMonitor::monitor_database(MXS_MONITORED_SERVER *database)
} }
/** /**
* Read server_id, read_only and (if 10.X) gtid_domain_id. * Read server_id, read_only and (if 10.X) gtid_domain_id. TODO: Move to MariaDBServer
* *
* @param database Database to update
* @param serv_info Where to save results * @param serv_info Where to save results
*/ */
static void read_server_variables(MXS_MONITORED_SERVER* database, MySqlServerInfo* serv_info) static void read_server_variables(MariaDBServer* serv_info)
{ {
MXS_MONITORED_SERVER* database = serv_info->server_base;
string query = "SELECT @@global.server_id, @@read_only;"; string query = "SELECT @@global.server_id, @@read_only;";
int columns = 2; int columns = 2;
if (serv_info->version == MYSQL_SERVER_VERSION_100) if (serv_info->version == MYSQL_SERVER_VERSION_100)
@ -896,11 +896,11 @@ static void read_server_variables(MXS_MONITORED_SERVER* database, MySqlServerInf
/** /**
* Monitor a database with given server info. * Monitor a database with given server info.
* *
* @param database Database to monitor
* @param serv_info Server info for database * @param serv_info Server info for database
*/ */
void MariaDBMonitor::monitor_mysql_db(MXS_MONITORED_SERVER* database, MySqlServerInfo *serv_info) void MariaDBMonitor::monitor_mysql_db(MariaDBServer* serv_info)
{ {
MXS_MONITORED_SERVER* database = serv_info->server_base;
/** Clear old states */ /** Clear old states */
monitor_clear_pending_status(database, SERVER_SLAVE | SERVER_MASTER | SERVER_RELAY_MASTER | monitor_clear_pending_status(database, SERVER_SLAVE | SERVER_MASTER | SERVER_RELAY_MASTER |
SERVER_SLAVE_OF_EXTERNAL_MASTER); SERVER_SLAVE_OF_EXTERNAL_MASTER);
@ -923,13 +923,12 @@ void MariaDBMonitor::monitor_mysql_db(MXS_MONITORED_SERVER* database, MySqlServe
* Query gtid_current_pos and gtid_binlog_pos and save the values to the server info object. * Query gtid_current_pos and gtid_binlog_pos and save the values to the server info object.
* Only the cluster master domain is parsed. * Only the cluster master domain is parsed.
* *
* @param mon Cluster monitor * @param info Server info structure for saving result TODO: move to MariaDBServer
* @param database The server to query
* @param info Server info structure for saving result TODO: remove
* @return True if successful * @return True if successful
*/ */
bool MariaDBMonitor::update_gtids(MXS_MONITORED_SERVER *database, MySqlServerInfo* info) bool MariaDBMonitor::update_gtids(MariaDBServer* info)
{ {
MXS_MONITORED_SERVER* database = info->server_base;
StringVector row; StringVector row;
const char query[] = "SELECT @@gtid_current_pos, @@gtid_binlog_pos;"; const char query[] = "SELECT @@gtid_current_pos, @@gtid_binlog_pos;";
const int ind_current_pos = 0; const int ind_current_pos = 0;
@ -954,12 +953,12 @@ bool MariaDBMonitor::update_gtids(MXS_MONITORED_SERVER *database, MySqlServerInf
* @param server Slave to update * @param server Slave to update
* @return Slave server info. NULL on error, or if server is not a slave. * @return Slave server info. NULL on error, or if server is not a slave.
*/ */
MySqlServerInfo* MariaDBMonitor::update_slave_info(MXS_MONITORED_SERVER* server) MariaDBServer* MariaDBMonitor::update_slave_info(MXS_MONITORED_SERVER* server)
{ {
MySqlServerInfo* info = get_server_info(server); MariaDBServer* info = get_server_info(server);
if (info->slave_status.slave_sql_running && if (info->slave_status.slave_sql_running &&
update_replication_settings(server, info) && update_replication_settings(server, info) &&
update_gtids(server, info) && update_gtids(info) &&
do_show_slave_status(info, server)) do_show_slave_status(info, server))
{ {
return info; return info;
@ -974,7 +973,7 @@ MySqlServerInfo* MariaDBMonitor::update_slave_info(MXS_MONITORED_SERVER* server)
* @param info Where to save results * @param info Where to save results
* @return True on success * @return True on success
*/ */
bool MariaDBMonitor::update_replication_settings(MXS_MONITORED_SERVER *database, MySqlServerInfo* info) bool MariaDBMonitor::update_replication_settings(MXS_MONITORED_SERVER *database, MariaDBServer* info)
{ {
StringVector row; StringVector row;
bool ok = query_one_row(database, "SELECT @@gtid_strict_mode, @@log_bin, @@log_slave_updates;", 3, &row); bool ok = query_one_row(database, "SELECT @@gtid_strict_mode, @@log_bin, @@log_slave_updates;", 3, &row);

View File

@ -150,12 +150,12 @@ bool MariaDBMonitor::manual_rejoin(SERVER* rejoin_server, json_t** output)
if (mon_server) if (mon_server)
{ {
const char* master_name = m_master->server->unique_name; const char* master_name = m_master->server->unique_name;
MySqlServerInfo* master_info = get_server_info(m_master); MariaDBServer* master_info = get_server_info(m_master);
MySqlServerInfo* server_info = get_server_info(mon_server); MariaDBServer* server_info = get_server_info(mon_server);
if (server_is_rejoin_suspect(mon_server, master_info, output)) if (server_is_rejoin_suspect(mon_server, master_info, output))
{ {
if (update_gtids(m_master, master_info)) if (update_gtids(master_info))
{ {
if (can_replicate_from(mon_server, server_info, master_info)) if (can_replicate_from(mon_server, server_info, master_info))
{ {
@ -356,7 +356,7 @@ uint32_t MariaDBMonitor::do_rejoin(const ServerVector& joinable_servers)
MXS_MONITORED_SERVER* joinable = *iter; MXS_MONITORED_SERVER* joinable = *iter;
const char* name = joinable->server->unique_name; const char* name = joinable->server->unique_name;
const char* master_name = master_server->unique_name; const char* master_name = master_server->unique_name;
MySqlServerInfo* redir_info = get_server_info(joinable); MariaDBServer* redir_info = get_server_info(joinable);
bool op_success; bool op_success;
if (redir_info->n_slaves_configured == 0) if (redir_info->n_slaves_configured == 0)
@ -402,7 +402,7 @@ bool MariaDBMonitor::cluster_can_be_joined()
bool MariaDBMonitor::get_joinable_servers(ServerVector* output) bool MariaDBMonitor::get_joinable_servers(ServerVector* output)
{ {
ss_dassert(output); ss_dassert(output);
MySqlServerInfo *master_info = get_server_info(m_master); MariaDBServer *master_info = get_server_info(m_master);
// Whether a join operation should be attempted or not depends on several criteria. Start with the ones // Whether a join operation should be attempted or not depends on several criteria. Start with the ones
// easiest to test. Go though all slaves and construct a preliminary list. // easiest to test. Go though all slaves and construct a preliminary list.
@ -421,12 +421,12 @@ bool MariaDBMonitor::get_joinable_servers(ServerVector* output)
bool comm_ok = true; bool comm_ok = true;
if (!suspects.empty()) if (!suspects.empty())
{ {
if (update_gtids(m_master, master_info)) if (update_gtids(master_info))
{ {
for (size_t i = 0; i < suspects.size(); i++) for (size_t i = 0; i < suspects.size(); i++)
{ {
MXS_MONITORED_SERVER* suspect = suspects[i]; MXS_MONITORED_SERVER* suspect = suspects[i];
MySqlServerInfo* suspect_info = get_server_info(suspect); MariaDBServer* suspect_info = get_server_info(suspect);
if (can_replicate_from(suspect, suspect_info, master_info)) if (can_replicate_from(suspect, suspect_info, master_info))
{ {
output->push_back(suspect); output->push_back(suspect);
@ -476,12 +476,12 @@ bool MariaDBMonitor::join_cluster(MXS_MONITORED_SERVER* server, const char* chan
* @return True, if server is a rejoin suspect. * @return True, if server is a rejoin suspect.
*/ */
bool MariaDBMonitor::server_is_rejoin_suspect(MXS_MONITORED_SERVER* rejoin_server, bool MariaDBMonitor::server_is_rejoin_suspect(MXS_MONITORED_SERVER* rejoin_server,
MySqlServerInfo* master_info, json_t** output) MariaDBServer* master_info, json_t** output)
{ {
bool is_suspect = false; bool is_suspect = false;
if (!SERVER_IS_MASTER(rejoin_server->server) && SERVER_IS_RUNNING(rejoin_server->server)) if (!SERVER_IS_MASTER(rejoin_server->server) && SERVER_IS_RUNNING(rejoin_server->server))
{ {
MySqlServerInfo* server_info = get_server_info(rejoin_server); MariaDBServer* server_info = get_server_info(rejoin_server);
SlaveStatusInfo* slave_status = &server_info->slave_status; SlaveStatusInfo* slave_status = &server_info->slave_status;
// Has no slave connection, yet is not a master. // Has no slave connection, yet is not a master.
if (server_info->n_slaves_configured == 0) if (server_info->n_slaves_configured == 0)
@ -558,7 +558,7 @@ bool MariaDBMonitor::do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MON
{ {
if (slave != promotion_target) if (slave != promotion_target)
{ {
MySqlServerInfo* slave_info = update_slave_info(slave); MariaDBServer* slave_info = update_slave_info(slave);
// If master is replicating from external master, it is updated but not added to array. // If master is replicating from external master, it is updated but not added to array.
if (slave_info && slave != current_master) if (slave_info && slave != current_master)
{ {
@ -578,7 +578,7 @@ bool MariaDBMonitor::do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MON
} }
bool rval = false; bool rval = false;
MySqlServerInfo* curr_master_info = get_server_info(demotion_target); MariaDBServer* curr_master_info = get_server_info(demotion_target);
// Step 2: Set read-only to on, flush logs, update master gtid:s // Step 2: Set read-only to on, flush logs, update master gtid:s
if (switchover_demote_master(demotion_target, curr_master_info, err_out)) if (switchover_demote_master(demotion_target, curr_master_info, err_out))
@ -764,7 +764,7 @@ bool MariaDBMonitor::do_failover(json_t** err_out)
bool MariaDBMonitor::failover_wait_relay_log(MXS_MONITORED_SERVER* new_master, int seconds_remaining, bool MariaDBMonitor::failover_wait_relay_log(MXS_MONITORED_SERVER* new_master, int seconds_remaining,
json_t** err_out) json_t** err_out)
{ {
MySqlServerInfo* master_info = get_server_info(new_master); MariaDBServer* master_info = get_server_info(new_master);
time_t begin = time(NULL); time_t begin = time(NULL);
bool query_ok = true; bool query_ok = true;
bool io_pos_stable = true; bool io_pos_stable = true;
@ -780,7 +780,7 @@ bool MariaDBMonitor::failover_wait_relay_log(MXS_MONITORED_SERVER* new_master, i
Gtid old_gtid_io_pos = master_info->slave_status.gtid_io_pos; Gtid old_gtid_io_pos = master_info->slave_status.gtid_io_pos;
// Update gtid:s first to make sure Gtid_IO_Pos is the more recent value. // Update gtid:s first to make sure Gtid_IO_Pos is the more recent value.
// It doesn't matter here, but is a general rule. // It doesn't matter here, but is a general rule.
query_ok = update_gtids(new_master, master_info) && query_ok = update_gtids(master_info) &&
do_show_slave_status(master_info, new_master); do_show_slave_status(master_info, new_master);
io_pos_stable = (old_gtid_io_pos == master_info->slave_status.gtid_io_pos); io_pos_stable = (old_gtid_io_pos == master_info->slave_status.gtid_io_pos);
} }
@ -823,7 +823,7 @@ bool MariaDBMonitor::failover_wait_relay_log(MXS_MONITORED_SERVER* new_master, i
* @param err_out json object for error printing. Can be NULL. * @param err_out json object for error printing. Can be NULL.
* @return True if successful. * @return True if successful.
*/ */
bool MariaDBMonitor::switchover_demote_master(MXS_MONITORED_SERVER* current_master, MySqlServerInfo* info, bool MariaDBMonitor::switchover_demote_master(MXS_MONITORED_SERVER* current_master, MariaDBServer* info,
json_t** err_out) json_t** err_out)
{ {
MXS_NOTICE("Demoting server '%s'.", current_master->server->unique_name); MXS_NOTICE("Demoting server '%s'.", current_master->server->unique_name);
@ -869,7 +869,7 @@ bool MariaDBMonitor::switchover_demote_master(MXS_MONITORED_SERVER* current_mast
if (!query_error) if (!query_error)
{ {
query = ""; query = "";
if (update_gtids(current_master, info)) if (update_gtids(info))
{ {
success = true; success = true;
} }
@ -1034,10 +1034,10 @@ bool MariaDBMonitor::wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master
ss_dassert(!slaves.empty()); ss_dassert(!slaves.empty());
bool rval = false; bool rval = false;
time_t begin = time(NULL); time_t begin = time(NULL);
MySqlServerInfo* new_master_info = get_server_info(new_master); MariaDBServer* new_master_info = get_server_info(new_master);
if (mxs_mysql_query(new_master->con, "FLUSH TABLES;") == 0 && if (mxs_mysql_query(new_master->con, "FLUSH TABLES;") == 0 &&
update_gtids(new_master, new_master_info)) update_gtids(new_master_info))
{ {
int query_fails = 0; int query_fails = 0;
int repl_fails = 0; int repl_fails = 0;
@ -1059,8 +1059,8 @@ bool MariaDBMonitor::wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master
while (i >= 0) while (i >= 0)
{ {
MXS_MONITORED_SERVER* slave = wait_list[i]; MXS_MONITORED_SERVER* slave = wait_list[i];
MySqlServerInfo* slave_info = get_server_info(slave); MariaDBServer* slave_info = get_server_info(slave);
if (update_gtids(slave, slave_info) && do_show_slave_status(slave_info, slave)) if (update_gtids(slave_info) && do_show_slave_status(slave_info, slave))
{ {
if (!slave_info->slave_status.last_error.empty()) if (!slave_info->slave_status.last_error.empty())
{ {
@ -1122,7 +1122,7 @@ bool MariaDBMonitor::switchover_check_preferred_master(MXS_MONITORED_SERVER* pre
{ {
ss_dassert(preferred); ss_dassert(preferred);
bool rval = true; bool rval = true;
MySqlServerInfo* preferred_info = update_slave_info(preferred); MariaDBServer* preferred_info = update_slave_info(preferred);
if (preferred_info == NULL || !check_replication_settings(preferred, preferred_info)) if (preferred_info == NULL || !check_replication_settings(preferred, preferred_info))
{ {
PRINT_MXS_JSON_ERROR(err_out, "The requested server '%s' is not a valid promotion candidate.", PRINT_MXS_JSON_ERROR(err_out, "The requested server '%s' is not a valid promotion candidate.",
@ -1184,7 +1184,7 @@ MXS_MONITORED_SERVER* MariaDBMonitor::select_new_master(ServerVector* slaves_out
/* Select a new master candidate. Selects the one with the latest event in relay log. /* Select a new master candidate. Selects the one with the latest event in relay log.
* If multiple slaves have same number of events, select the one with most processed events. */ * If multiple slaves have same number of events, select the one with most processed events. */
MXS_MONITORED_SERVER* current_best = NULL; MXS_MONITORED_SERVER* current_best = NULL;
MySqlServerInfo* current_best_info = NULL; MariaDBServer* current_best_info = NULL;
// Servers that cannot be selected because of exclusion, but seem otherwise ok. // Servers that cannot be selected because of exclusion, but seem otherwise ok.
ServerVector valid_but_excluded; ServerVector valid_but_excluded;
// Index of the current best candidate in slaves_out // Index of the current best candidate in slaves_out
@ -1194,7 +1194,7 @@ MXS_MONITORED_SERVER* MariaDBMonitor::select_new_master(ServerVector* slaves_out
{ {
// If a server cannot be connected to, it won't be considered for promotion or redirected. // If a server cannot be connected to, it won't be considered for promotion or redirected.
// Do not worry about the exclusion list yet, querying the excluded servers is ok. // Do not worry about the exclusion list yet, querying the excluded servers is ok.
MySqlServerInfo* cand_info = update_slave_info(cand); MariaDBServer* cand_info = update_slave_info(cand);
// If master is replicating from external master, it is updated but not added to array. // If master is replicating from external master, it is updated but not added to array.
if (cand_info && cand != m_master) if (cand_info && cand != m_master)
{ {
@ -1234,7 +1234,7 @@ MXS_MONITORED_SERVER* MariaDBMonitor::select_new_master(ServerVector* slaves_out
iter != valid_but_excluded.end(); iter != valid_but_excluded.end();
iter++) iter++)
{ {
MySqlServerInfo* excluded_info = get_server_info(*iter); MariaDBServer* excluded_info = get_server_info(*iter);
const char* excluded_name = (*iter)->server->unique_name; const char* excluded_name = (*iter)->server->unique_name;
if (current_best == NULL) if (current_best == NULL)
{ {
@ -1289,8 +1289,8 @@ bool MariaDBMonitor::server_is_excluded(const MXS_MONITORED_SERVER* server)
* @param candidate_info Server info of new candidate * @param candidate_info Server info of new candidate
* @return True if candidate is better * @return True if candidate is better
*/ */
bool MariaDBMonitor::is_candidate_better(const MySqlServerInfo* current_best_info, bool MariaDBMonitor::is_candidate_better(const MariaDBServer* current_best_info,
const MySqlServerInfo* candidate_info) const MariaDBServer* candidate_info)
{ {
uint64_t cand_io = candidate_info->slave_status.gtid_io_pos.sequence; uint64_t cand_io = candidate_info->slave_status.gtid_io_pos.sequence;
uint64_t cand_processed = candidate_info->gtid_current_pos.sequence; uint64_t cand_processed = candidate_info->gtid_current_pos.sequence;
@ -1456,7 +1456,7 @@ bool MariaDBMonitor::failover_check(json_t** error_out)
* @param print_on Print warnings or not * @param print_on Print warnings or not
* @return True if log_bin is on * @return True if log_bin is on
*/ */
bool check_replication_settings(const MXS_MONITORED_SERVER* server, MySqlServerInfo* server_info, bool check_replication_settings(const MXS_MONITORED_SERVER* server, MariaDBServer* server_info,
print_repl_warnings_t print_warnings) print_repl_warnings_t print_warnings)
{ {
bool rval = true; bool rval = true;
@ -1501,10 +1501,10 @@ bool check_replication_settings(const MXS_MONITORED_SERVER* server, MySqlServerI
* @return True if slave can replicate from master * @return True if slave can replicate from master
*/ */
bool MariaDBMonitor::can_replicate_from(MXS_MONITORED_SERVER* slave, bool MariaDBMonitor::can_replicate_from(MXS_MONITORED_SERVER* slave,
MySqlServerInfo* slave_info, MySqlServerInfo* master_info) MariaDBServer* slave_info, MariaDBServer* master_info)
{ {
bool rval = false; bool rval = false;
if (update_gtids(slave, slave_info)) if (update_gtids(slave_info))
{ {
Gtid slave_gtid = slave_info->gtid_current_pos; Gtid slave_gtid = slave_info->gtid_current_pos;
Gtid master_gtid = master_info->gtid_binlog_pos; Gtid master_gtid = master_info->gtid_binlog_pos;
@ -1637,7 +1637,7 @@ void print_redirect_errors(MXS_MONITORED_SERVER* first_server, const ServerVecto
bool MariaDBMonitor::uses_gtid(MXS_MONITORED_SERVER* mon_server, json_t** error_out) bool MariaDBMonitor::uses_gtid(MXS_MONITORED_SERVER* mon_server, json_t** error_out)
{ {
bool rval = false; bool rval = false;
const MySqlServerInfo* info = get_server_info(mon_server); const MariaDBServer* info = get_server_info(mon_server);
if (info->slave_status.gtid_io_pos.server_id == SERVER_ID_UNKNOWN) if (info->slave_status.gtid_io_pos.server_id == SERVER_ID_UNKNOWN)
{ {
string slave_not_gtid_msg = string("Slave server ") + mon_server->server->unique_name + string slave_not_gtid_msg = string("Slave server ") + mon_server->server->unique_name +
@ -1657,7 +1657,7 @@ bool MariaDBMonitor::failover_not_possible()
for (MXS_MONITORED_SERVER* s = m_monitor_base->monitored_servers; s; s = s->next) for (MXS_MONITORED_SERVER* s = m_monitor_base->monitored_servers; s; s = s->next)
{ {
MySqlServerInfo* info = get_server_info(s); MariaDBServer* info = get_server_info(s);
if (info->n_slaves_configured > 1) if (info->n_slaves_configured > 1)
{ {
@ -1682,7 +1682,7 @@ bool MariaDBMonitor::slave_receiving_events()
int64_t master_id = m_master->server->node_id; int64_t master_id = m_master->server->node_id;
for (MXS_MONITORED_SERVER* server = m_monitor_base->monitored_servers; server; server = server->next) for (MXS_MONITORED_SERVER* server = m_monitor_base->monitored_servers; server; server = server->next)
{ {
MySqlServerInfo* info = get_server_info(server); MariaDBServer* info = get_server_info(server);
if (info->slave_configured && if (info->slave_configured &&
info->slave_status.slave_io_running && info->slave_status.slave_io_running &&

View File

@ -72,21 +72,30 @@ MariaDBMonitor::~MariaDBMonitor()
*/ */
void MariaDBMonitor::init_server_info() void MariaDBMonitor::init_server_info()
{ {
m_server_info.clear(); m_servers.clear();
for (auto server = m_monitor_base->monitored_servers; server; server = server->next) for (auto server = m_monitor_base->monitored_servers; server; server = server->next)
{ {
ServerInfoMap::value_type new_val(server, MySqlServerInfo()); m_servers.push_back(MariaDBServer(server));
}
// All servers have been inserted into the vector. The data in the vector should no longer move so it's
// safe to take addresses.
m_server_info.clear();
for (auto iter = m_servers.begin(); iter != m_servers.end(); iter++)
{
ss_dassert(m_server_info.count(iter->server_base) == 0);
ServerInfoMap::value_type new_val(iter->server_base, &*iter);
m_server_info.insert(new_val); m_server_info.insert(new_val);
} }
} }
MySqlServerInfo* MariaDBMonitor::get_server_info(const MXS_MONITORED_SERVER* db) MariaDBServer* MariaDBMonitor::get_server_info(MXS_MONITORED_SERVER* db)
{ {
ss_dassert(m_server_info.count(db) == 1); // Should always exist in the map ss_dassert(m_server_info.count(db) == 1); // Should always exist in the map
return &m_server_info[db]; return m_server_info[db];
} }
const MySqlServerInfo* MariaDBMonitor::get_server_info(const MXS_MONITORED_SERVER* db) const const MariaDBServer* MariaDBMonitor::get_server_info(const MXS_MONITORED_SERVER* db) const
{ {
return const_cast<MariaDBMonitor*>(this)->get_server_info(db); return const_cast<MariaDBMonitor*>(this)->get_server_info(db);
} }
@ -242,7 +251,7 @@ void MariaDBMonitor::diagnostics(DCB *dcb) const
dcb_printf(dcb, "\nServer information:\n-------------------\n\n"); dcb_printf(dcb, "\nServer information:\n-------------------\n\n");
for (MXS_MONITORED_SERVER *db = m_monitor_base->monitored_servers; db; db = db->next) for (MXS_MONITORED_SERVER *db = m_monitor_base->monitored_servers; db; db = db->next)
{ {
const MySqlServerInfo* serv_info = get_server_info(db); const MariaDBServer* serv_info = get_server_info(db);
dcb_printf(dcb, "Server: %s\n", db->server->unique_name); dcb_printf(dcb, "Server: %s\n", db->server->unique_name);
dcb_printf(dcb, "Server ID: %" PRId64 "\n", serv_info->server_id); dcb_printf(dcb, "Server ID: %" PRId64 "\n", serv_info->server_id);
dcb_printf(dcb, "Read only: %s\n", serv_info->read_only ? "YES" : "NO"); dcb_printf(dcb, "Read only: %s\n", serv_info->read_only ? "YES" : "NO");
@ -317,7 +326,7 @@ json_t* MariaDBMonitor::diagnostics_json() const
for (MXS_MONITORED_SERVER *db = m_monitor_base->monitored_servers; db; db = db->next) for (MXS_MONITORED_SERVER *db = m_monitor_base->monitored_servers; db; db = db->next)
{ {
json_t* srv = json_object(); json_t* srv = json_object();
const MySqlServerInfo* serv_info = get_server_info(db); const MariaDBServer* serv_info = get_server_info(db);
json_object_set_new(srv, "name", json_string(db->server->unique_name)); json_object_set_new(srv, "name", json_string(db->server->unique_name));
json_object_set_new(srv, "server_id", json_integer(serv_info->server_id)); json_object_set_new(srv, "server_id", json_integer(serv_info->server_id));
json_object_set_new(srv, "master_id", json_integer(serv_info->slave_status.master_server_id)); json_object_set_new(srv, "master_id", json_integer(serv_info->slave_status.master_server_id));
@ -373,7 +382,7 @@ bool MariaDBMonitor::standalone_master_required(MXS_MONITORED_SERVER *db)
if (SERVER_IS_RUNNING(db->server)) if (SERVER_IS_RUNNING(db->server))
{ {
candidates++; candidates++;
MySqlServerInfo *server_info = get_server_info(db); MariaDBServer *server_info = get_server_info(db);
if (server_info->read_only || server_info->slave_configured || candidates > 1) if (server_info->read_only || server_info->slave_configured || candidates > 1)
{ {
@ -507,7 +516,7 @@ void MariaDBMonitor::main_loop()
ptr->pending_status = ptr->server->status; ptr->pending_status = ptr->server->status;
/* monitor current node */ /* monitor current node */
monitor_database(ptr); monitor_database(get_server_info(ptr));
/* reset the slave list of current node */ /* reset the slave list of current node */
memset(&ptr->server->slaves, 0, sizeof(ptr->server->slaves)); memset(&ptr->server->slaves, 0, sizeof(ptr->server->slaves));
@ -595,7 +604,7 @@ void MariaDBMonitor::main_loop()
if (m_master != NULL && SERVER_IS_MASTER(m_master->server)) if (m_master != NULL && SERVER_IS_MASTER(m_master->server))
{ {
MySqlServerInfo* master_info = get_server_info(m_master); MariaDBServer* master_info = get_server_info(m_master);
// Update cluster gtid domain // Update cluster gtid domain
int64_t domain = master_info->gtid_domain_id; int64_t domain = master_info->gtid_domain_id;
if (m_master_gtid_domain >= 0 && domain != m_master_gtid_domain) if (m_master_gtid_domain >= 0 && domain != m_master_gtid_domain)
@ -642,7 +651,7 @@ void MariaDBMonitor::main_loop()
ptr = m_monitor_base->monitored_servers; ptr = m_monitor_base->monitored_servers;
while (ptr) while (ptr)
{ {
MySqlServerInfo *serv_info = get_server_info(ptr); MariaDBServer *serv_info = get_server_info(ptr);
ss_dassert(serv_info); ss_dassert(serv_info);
if (ptr->server->node_id > 0 && ptr->server->master_id > 0 && if (ptr->server->node_id > 0 && ptr->server->master_id > 0 &&
@ -671,7 +680,7 @@ void MariaDBMonitor::main_loop()
{ {
if (!SERVER_IN_MAINT(ptr->server)) if (!SERVER_IN_MAINT(ptr->server))
{ {
MySqlServerInfo *serv_info = get_server_info(ptr); MariaDBServer *serv_info = get_server_info(ptr);
/** If "detect_stale_master" option is On, let's use the previous master. /** If "detect_stale_master" option is On, let's use the previous master.
* *
@ -870,7 +879,7 @@ void MariaDBMonitor::main_loop()
while (ptr) while (ptr)
{ {
MySqlServerInfo *serv_info = get_server_info(ptr); MariaDBServer *serv_info = get_server_info(ptr);
if ((!SERVER_IN_MAINT(ptr->server)) && SERVER_IS_RUNNING(ptr->server)) if ((!SERVER_IN_MAINT(ptr->server)) && SERVER_IS_RUNNING(ptr->server))
{ {

View File

@ -31,7 +31,10 @@ extern const char * const CN_AUTO_FAILOVER;
class MariaDBMonitor; class MariaDBMonitor;
typedef std::tr1::unordered_map<const MXS_MONITORED_SERVER*, MySqlServerInfo> ServerInfoMap; // Map of base struct to MariaDBServer. Does not own the server objects. May not be needed at the end.
typedef std::tr1::unordered_map<MXS_MONITORED_SERVER*, MariaDBServer*> ServerInfoMap;
// Server container, owns the server objects.
typedef std::vector<MariaDBServer> ServerContainer; // TODO: Rename/get rid of ServerVector typedef!
enum print_repl_warnings_t enum print_repl_warnings_t
{ {
@ -47,7 +50,7 @@ enum slave_down_setting_t
// TODO: Most of following should be class methods // TODO: Most of following should be class methods
void print_redirect_errors(MXS_MONITORED_SERVER* first_server, const ServerVector& servers, json_t** err_out); void print_redirect_errors(MXS_MONITORED_SERVER* first_server, const ServerVector& servers, json_t** err_out);
bool check_replication_settings(const MXS_MONITORED_SERVER* server, MySqlServerInfo* server_info, bool check_replication_settings(const MXS_MONITORED_SERVER* server, MariaDBServer* server_info,
print_repl_warnings_t print_warnings = WARNINGS_ON); print_repl_warnings_t print_warnings = WARNINGS_ON);
MXS_MONITORED_SERVER* getServerByNodeId(MXS_MONITORED_SERVER *, long); MXS_MONITORED_SERVER* getServerByNodeId(MXS_MONITORED_SERVER *, long);
MXS_MONITORED_SERVER* getSlaveOfNodeId(MXS_MONITORED_SERVER *, long, slave_down_setting_t); MXS_MONITORED_SERVER* getSlaveOfNodeId(MXS_MONITORED_SERVER *, long, slave_down_setting_t);
@ -143,12 +146,12 @@ public:
* @param db Server to get info for. Must be a valid server or function crashes. * @param db Server to get info for. Must be a valid server or function crashes.
* @return The server info. * @return The server info.
*/ */
MySqlServerInfo* get_server_info(const MXS_MONITORED_SERVER* db); MariaDBServer* get_server_info(MXS_MONITORED_SERVER* db);
/** /**
* Constant version of get_server_info(). * Constant version of get_server_info().
*/ */
const MySqlServerInfo* get_server_info(const MXS_MONITORED_SERVER* db) const; const MariaDBServer* get_server_info(const MXS_MONITORED_SERVER* db) const;
bool detectStaleMaster; /**< Monitor flag for MySQL replication Stale Master detection */ bool detectStaleMaster; /**< Monitor flag for MySQL replication Stale Master detection */
@ -158,13 +161,14 @@ private:
unsigned long m_id; /**< Monitor ID */ unsigned long m_id; /**< Monitor ID */
volatile int m_shutdown; /**< Flag to shutdown the monitor thread. */ volatile int m_shutdown; /**< Flag to shutdown the monitor thread. */
volatile int m_status; /**< Monitor status. */ volatile int m_status; /**< Monitor status. */
ServerContainer m_servers; /**< Servers of the monitor. */
ServerInfoMap m_server_info; /**< Contains server specific information */
// Values updated by monitor // Values updated by monitor
int64_t m_master_gtid_domain; /**< Gtid domain currently used by the master */ int64_t m_master_gtid_domain; /**< Gtid domain currently used by the master */
string m_external_master_host; /**< External master host, for fail/switchover */ string m_external_master_host; /**< External master host, for fail/switchover */
int m_external_master_port; /**< External master port */ int m_external_master_port; /**< External master port */
MXS_MONITORED_SERVER *m_master; /**< Master server for MySQL Master/Slave replication */ MXS_MONITORED_SERVER *m_master; /**< Master server for MySQL Master/Slave replication */
ServerInfoMap m_server_info; /**< Contains server specific information */
// Replication topology detection settings // Replication topology detection settings
bool m_mysql51_replication; /**< Use MySQL 5.1 replication */ bool m_mysql51_replication; /**< Use MySQL 5.1 replication */
@ -198,7 +202,7 @@ private:
~MariaDBMonitor(); ~MariaDBMonitor();
bool load_config_params(const MXS_CONFIG_PARAMETER* params); bool load_config_params(const MXS_CONFIG_PARAMETER* params);
bool failover_wait_relay_log(MXS_MONITORED_SERVER* new_master, int seconds_remaining, json_t** err_out); bool failover_wait_relay_log(MXS_MONITORED_SERVER* new_master, int seconds_remaining, json_t** err_out);
bool switchover_demote_master(MXS_MONITORED_SERVER* current_master, MySqlServerInfo* info, bool switchover_demote_master(MXS_MONITORED_SERVER* current_master, MariaDBServer* info,
json_t** err_out); json_t** err_out);
bool switchover_wait_slaves_catchup(const ServerVector& slaves, const Gtid& gtid, int total_timeout, bool switchover_wait_slaves_catchup(const ServerVector& slaves, const Gtid& gtid, int total_timeout,
int read_timeout, json_t** err_out); int read_timeout, json_t** err_out);
@ -210,13 +214,13 @@ private:
bool promote_new_master(MXS_MONITORED_SERVER* new_master, json_t** err_out); bool promote_new_master(MXS_MONITORED_SERVER* new_master, json_t** err_out);
MXS_MONITORED_SERVER* select_new_master(ServerVector* slaves_out, json_t** err_out); MXS_MONITORED_SERVER* select_new_master(ServerVector* slaves_out, json_t** err_out);
bool server_is_excluded(const MXS_MONITORED_SERVER* server); bool server_is_excluded(const MXS_MONITORED_SERVER* server);
bool is_candidate_better(const MySqlServerInfo* current_best_info, const MySqlServerInfo* candidate_info); bool is_candidate_better(const MariaDBServer* current_best_info, const MariaDBServer* candidate_info);
MySqlServerInfo* update_slave_info(MXS_MONITORED_SERVER* server); MariaDBServer* update_slave_info(MXS_MONITORED_SERVER* server);
bool do_show_slave_status(MySqlServerInfo* serv_info, MXS_MONITORED_SERVER* database); bool do_show_slave_status(MariaDBServer* serv_info, MXS_MONITORED_SERVER* database);
bool update_replication_settings(MXS_MONITORED_SERVER *database, MySqlServerInfo* info); bool update_replication_settings(MXS_MONITORED_SERVER *database, MariaDBServer* info);
void init_server_info(); void init_server_info();
bool slave_receiving_events(); bool slave_receiving_events();
void monitor_database(MXS_MONITORED_SERVER *database); void monitor_database(MariaDBServer* param_db);
bool standalone_master_required(MXS_MONITORED_SERVER *db); bool standalone_master_required(MXS_MONITORED_SERVER *db);
bool set_standalone_master(MXS_MONITORED_SERVER *db); bool set_standalone_master(MXS_MONITORED_SERVER *db);
bool failover_not_possible(); bool failover_not_possible();
@ -233,21 +237,21 @@ private:
void set_slave_heartbeat(MXS_MONITORED_SERVER *); void set_slave_heartbeat(MXS_MONITORED_SERVER *);
MXS_MONITORED_SERVER* build_mysql51_replication_tree(); MXS_MONITORED_SERVER* build_mysql51_replication_tree();
MXS_MONITORED_SERVER* get_replication_tree(int num_servers); MXS_MONITORED_SERVER* get_replication_tree(int num_servers);
void monitor_mysql_db(MXS_MONITORED_SERVER* database, MySqlServerInfo *serv_info); void monitor_mysql_db(MariaDBServer *serv_info);
bool do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MONITORED_SERVER* new_master, bool do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MONITORED_SERVER* new_master,
json_t** err_out); json_t** err_out);
bool do_failover(json_t** err_out); bool do_failover(json_t** err_out);
uint32_t do_rejoin(const ServerVector& joinable_servers); uint32_t do_rejoin(const ServerVector& joinable_servers);
bool mon_process_failover(bool* cluster_modified_out); bool mon_process_failover(bool* cluster_modified_out);
bool server_is_rejoin_suspect(MXS_MONITORED_SERVER* server, MySqlServerInfo* master_info, bool server_is_rejoin_suspect(MXS_MONITORED_SERVER* server, MariaDBServer* master_info,
json_t** output); json_t** output);
bool cluster_can_be_joined(); bool cluster_can_be_joined();
bool failover_check(json_t** error_out); bool failover_check(json_t** error_out);
bool update_gtids(MXS_MONITORED_SERVER *database, MySqlServerInfo* info); bool update_gtids(MariaDBServer* info);
void disable_setting(const char* setting); void disable_setting(const char* setting);
bool switchover_check_new(const MXS_MONITORED_SERVER* monitored_server, json_t** error); bool switchover_check_new(const MXS_MONITORED_SERVER* monitored_server, json_t** error);
bool switchover_check_current(const MXS_MONITORED_SERVER* suggested_curr_master, bool switchover_check_current(const MXS_MONITORED_SERVER* suggested_curr_master,
json_t** error_out) const; json_t** error_out) const;
bool can_replicate_from(MXS_MONITORED_SERVER* slave, MySqlServerInfo* slave_info, bool can_replicate_from(MXS_MONITORED_SERVER* slave, MariaDBServer* slave_info,
MySqlServerInfo* master_info); MariaDBServer* master_info);
}; };

View File

@ -18,6 +18,7 @@
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <maxscale/dcb.h>
#include <maxscale/debug.h> #include <maxscale/debug.h>
#include <maxscale/mysql_utils.h> #include <maxscale/mysql_utils.h>
@ -87,8 +88,10 @@ SlaveStatusInfo::SlaveStatusInfo()
, read_master_log_pos(0) , read_master_log_pos(0)
{} {}
MySqlServerInfo::MySqlServerInfo() MariaDBServer::MariaDBServer(MXS_MONITORED_SERVER* monitored_server)
: server_id(SERVER_ID_UNKNOWN) : server_base(monitored_server)
, version(MYSQL_SERVER_VERSION_51)
, server_id(SERVER_ID_UNKNOWN)
, group(0) , group(0)
, read_only(false) , read_only(false)
, slave_configured(false) , slave_configured(false)
@ -99,10 +102,9 @@ MySqlServerInfo::MySqlServerInfo()
, heartbeat_period(0) , heartbeat_period(0)
, latest_event(0) , latest_event(0)
, gtid_domain_id(-1) , gtid_domain_id(-1)
, version(MYSQL_SERVER_VERSION_51)
{} {}
int64_t MySqlServerInfo::relay_log_events() int64_t MariaDBServer::relay_log_events()
{ {
if (slave_status.gtid_io_pos.server_id != SERVER_ID_UNKNOWN && if (slave_status.gtid_io_pos.server_id != SERVER_ID_UNKNOWN &&
gtid_current_pos.server_id != SERVER_ID_UNKNOWN && gtid_current_pos.server_id != SERVER_ID_UNKNOWN &&
@ -208,4 +210,4 @@ string monitored_servers_to_string(const ServerVector& array)
} }
} }
return rval; return rval;
} }

View File

@ -104,35 +104,37 @@ public:
}; };
/** /**
* Monitor specific information about a server * Monitor specific information about a server. Eventually, this will be the primary data structure handled
* * by the monitor. These are initialized in @c init_server_info.
* Note: These are initialized in @c init_server_info
*/ */
class MySqlServerInfo class MariaDBServer
{ {
public: public:
int64_t server_id; /**< Value of @@server_id. Valid values are 32bit unsigned. */ MXS_MONITORED_SERVER* const server_base; /**< Monitored server base class/struct. MariaDBServer does not
int group; /**< Multi-master group where this server belongs, * own the struct, it is not freed (or connection closed) when
* 0 for servers not in groups */ * a MariaDBServer is destroyed. */
bool read_only; /**< Value of @@read_only */
bool slave_configured; /**< Whether SHOW SLAVE STATUS returned rows */
bool binlog_relay; /** Server is a Binlog Relay */
int n_slaves_configured; /**< Number of configured slave connections*/
int n_slaves_running; /**< Number of running slave connections */
int slave_heartbeats; /**< Number of received heartbeats */
double heartbeat_period; /**< The time interval between heartbeats */
time_t latest_event; /**< Time when latest event was received from the master */
int64_t gtid_domain_id; /**< The value of gtid_domain_id, the domain which is used for
* new non-replicated events. */
Gtid gtid_current_pos; /**< Gtid of latest event. Only shows the triplet
* with the current master domain. */
Gtid gtid_binlog_pos; /**< Gtid of latest event written to binlog. Only shows
* the triplet with the current master domain. */
SlaveStatusInfo slave_status; /**< Data returned from SHOW SLAVE STATUS */
ReplicationSettings rpl_settings; /**< Miscellaneous replication related settings */
mysql_server_version version; /**< Server version, 10.X, 5.5 or 5.1 */ mysql_server_version version; /**< Server version, 10.X, 5.5 or 5.1 */
int64_t server_id; /**< Value of @@server_id. Valid values are 32bit unsigned. */
int group; /**< Multi-master group where this server belongs,
* 0 for servers not in groups */
bool read_only; /**< Value of @@read_only */
bool slave_configured; /**< Whether SHOW SLAVE STATUS returned rows */
bool binlog_relay; /** Server is a Binlog Relay */
int n_slaves_configured; /**< Number of configured slave connections*/
int n_slaves_running; /**< Number of running slave connections */
int slave_heartbeats; /**< Number of received heartbeats */
double heartbeat_period; /**< The time interval between heartbeats */
time_t latest_event; /**< Time when latest event was received from the master */
int64_t gtid_domain_id; /**< The value of gtid_domain_id, the domain which is used for
* new non-replicated events. */
Gtid gtid_current_pos; /**< Gtid of latest event. Only shows the triplet
* with the current master domain. */
Gtid gtid_binlog_pos; /**< Gtid of latest event written to binlog. Only shows
* the triplet with the current master domain. */
SlaveStatusInfo slave_status; /**< Data returned from SHOW SLAVE STATUS */
ReplicationSettings rpl_settings; /**< Miscellaneous replication related settings */
MySqlServerInfo(); MariaDBServer(MXS_MONITORED_SERVER* monitored_server);
/** /**
* Calculate how many events are left in the relay log. If gtid_current_pos is ahead of Gtid_IO_Pos, * Calculate how many events are left in the relay log. If gtid_current_pos is ahead of Gtid_IO_Pos,