MXS-1703 Rename typedefs in preparation for more changes

Also moved some code around.
This commit is contained in:
Esa Korhonen
2018-04-06 14:35:13 +03:00
parent 71004a0ebc
commit 8b642dbb5e
7 changed files with 86 additions and 130 deletions

View File

@ -19,6 +19,11 @@
#include <maxscale/mysql_utils.h>
#include "utilities.hh"
using std::string;
static void print_redirect_errors(MXS_MONITORED_SERVER* first_server, const MonServerArray& servers,
json_t** err_out);
bool MariaDBMonitor::manual_switchover(MXS_MONITORED_SERVER* new_master,
MXS_MONITORED_SERVER* given_current_master,
json_t** error_out)
@ -160,7 +165,7 @@ bool MariaDBMonitor::manual_rejoin(SERVER* rejoin_server, json_t** output)
{
if (can_replicate_from(mon_server, server_info, master_info))
{
ServerVector joinable_server;
MonServerArray joinable_server;
joinable_server.push_back(mon_server);
if (do_rejoin(joinable_server) == 1)
{
@ -239,14 +244,14 @@ string MariaDBMonitor::generate_change_master_cmd(const string& master_host, int
* @param redirected_slaves A vector where to insert successfully redirected slaves.
* @return The number of slaves successfully redirected.
*/
int MariaDBMonitor::redirect_slaves(MXS_MONITORED_SERVER* new_master, const ServerVector& slaves,
ServerVector* redirected_slaves)
int MariaDBMonitor::redirect_slaves(MXS_MONITORED_SERVER* new_master, const MonServerArray& slaves,
MonServerArray* redirected_slaves)
{
ss_dassert(redirected_slaves != NULL);
MXS_NOTICE("Redirecting slaves to new master.");
string change_cmd = generate_change_master_cmd(new_master->server->name, new_master->server->port);
int successes = 0;
for (ServerVector::const_iterator iter = slaves.begin(); iter != slaves.end(); iter++)
for (MonServerArray::const_iterator iter = slaves.begin(); iter != slaves.end(); iter++)
{
if (redirect_one_slave(*iter, change_cmd.c_str()))
{
@ -354,14 +359,14 @@ bool MariaDBMonitor::redirect_one_slave(MXS_MONITORED_SERVER* slave, const char*
* @param joinable_servers Which servers to rejoin
* @return The number of servers successfully rejoined
*/
uint32_t MariaDBMonitor::do_rejoin(const ServerVector& joinable_servers)
uint32_t MariaDBMonitor::do_rejoin(const MonServerArray& joinable_servers)
{
SERVER* master_server = m_master->server;
uint32_t servers_joined = 0;
if (!joinable_servers.empty())
{
string change_cmd = generate_change_master_cmd(master_server->name, master_server->port);
for (ServerVector::const_iterator iter = joinable_servers.begin();
for (MonServerArray::const_iterator iter = joinable_servers.begin();
iter != joinable_servers.end();
iter++)
{
@ -411,14 +416,14 @@ bool MariaDBMonitor::cluster_can_be_joined()
* @return False, if there were possible rejoinable servers but communications error to master server
* prevented final checks.
*/
bool MariaDBMonitor::get_joinable_servers(ServerVector* output)
bool MariaDBMonitor::get_joinable_servers(MonServerArray* output)
{
ss_dassert(output);
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
// easiest to test. Go though all slaves and construct a preliminary list.
ServerVector suspects;
MonServerArray suspects;
for (MXS_MONITORED_SERVER* server = m_monitor_base->monitored_servers;
server != NULL;
server = server->next)
@ -577,7 +582,7 @@ bool MariaDBMonitor::do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MON
// Step 1: Select promotion candidate, save all slaves except promotion target to an array. If we have a
// user-defined master candidate, check it. Otherwise, autoselect.
MXS_MONITORED_SERVER* promotion_target = NULL;
ServerVector redirectable_slaves;
MonServerArray redirectable_slaves;
if (new_master)
{
if (switchover_check_preferred_master(new_master, err_out))
@ -622,7 +627,7 @@ bool MariaDBMonitor::do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MON
seconds_remaining -= difftime(step2_time, start_time);
// Step 3: Wait for the slaves (including promotion target) to catch up with master.
ServerVector catchup_slaves = redirectable_slaves;
MonServerArray catchup_slaves = redirectable_slaves;
catchup_slaves.push_back(promotion_target);
if (switchover_wait_slaves_catchup(catchup_slaves, curr_master_info->gtid_binlog_pos,
seconds_remaining, m_monitor_base->read_timeout, err_out))
@ -637,7 +642,7 @@ bool MariaDBMonitor::do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MON
{
catchup_and_promote_success = true;
// Step 5: Redirect slaves and start replication on old master.
ServerVector redirected_slaves;
MonServerArray redirected_slaves;
bool start_ok = switchover_start_slave(demotion_target, promotion_target->server);
if (start_ok)
{
@ -722,7 +727,7 @@ bool MariaDBMonitor::do_failover(json_t** err_out)
int seconds_remaining = m_failover_timeout;
time_t start_time = time(NULL);
// Step 1: Select new master. Also populate a vector with all slaves not the selected master.
ServerVector redirectable_slaves;
MonServerArray redirectable_slaves;
MXS_MONITORED_SERVER* new_master = select_new_master(&redirectable_slaves, err_out);
if (new_master == NULL)
{
@ -744,7 +749,7 @@ bool MariaDBMonitor::do_failover(json_t** err_out)
if (promote_new_master(new_master, err_out))
{
// Step 4: Redirect slaves.
ServerVector redirected_slaves;
MonServerArray redirected_slaves;
int redirects = redirect_slaves(new_master, redirectable_slaves, &redirected_slaves);
bool success = redirectable_slaves.empty() ? true : redirects > 0;
if (success)
@ -961,13 +966,13 @@ bool MariaDBMonitor::switchover_demote_master(MXS_MONITORED_SERVER* current_mast
* @param err_out json object for error printing. Can be NULL.
* @return True, if target gtid was reached within allotted time for all servers
*/
bool MariaDBMonitor::switchover_wait_slaves_catchup(const ServerVector& slaves, const GtidList& gtid,
bool MariaDBMonitor::switchover_wait_slaves_catchup(const MonServerArray& slaves, const GtidList& gtid,
int total_timeout, int read_timeout, json_t** err_out)
{
bool success = true;
int seconds_remaining = total_timeout;
for (ServerVector::const_iterator iter = slaves.begin();
for (MonServerArray::const_iterator iter = slaves.begin();
iter != slaves.end() && success;
iter++)
{
@ -1001,7 +1006,7 @@ bool MariaDBMonitor::switchover_wait_slaves_catchup(const ServerVector& slaves,
* @param seconds_remaining How long can we wait
* @return True, if at least one slave got the new event within the time limit
*/
bool MariaDBMonitor::wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master, const ServerVector& slaves,
bool MariaDBMonitor::wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master, const MonServerArray& slaves,
int seconds_remaining)
{
ss_dassert(!slaves.empty());
@ -1016,7 +1021,7 @@ bool MariaDBMonitor::wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master
int repl_fails = 0;
int successes = 0;
const GtidList& target = new_master_info->gtid_current_pos;
ServerVector wait_list = slaves; // Check all the servers in the list
MonServerArray wait_list = slaves; // Check all the servers in the list
bool first_round = true;
bool time_is_up = false;
@ -1068,7 +1073,7 @@ bool MariaDBMonitor::wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master
}
}
ServerVector::size_type fails = repl_fails + query_fails + wait_list.size();
MonServerArray::size_type fails = repl_fails + query_fails + wait_list.size();
if (fails > 0)
{
const char MSG[] = "Replication from the new master could not be confirmed for %lu slaves. "
@ -1153,7 +1158,7 @@ bool MariaDBMonitor::promote_new_master(MXS_MONITORED_SERVER* new_master, json_t
* @param err_out json object for error printing. Can be NULL.
* @return The found master, or NULL if not found
*/
MXS_MONITORED_SERVER* MariaDBMonitor::select_new_master(ServerVector* slaves_out, json_t** err_out)
MXS_MONITORED_SERVER* MariaDBMonitor::select_new_master(MonServerArray* slaves_out, json_t** err_out)
{
ss_dassert(slaves_out && slaves_out->size() == 0);
/* Select a new master candidate. Selects the one with the latest event in relay log.
@ -1161,7 +1166,7 @@ MXS_MONITORED_SERVER* MariaDBMonitor::select_new_master(ServerVector* slaves_out
MXS_MONITORED_SERVER* current_best = NULL;
MariaDBServer* current_best_info = NULL;
// Servers that cannot be selected because of exclusion, but seem otherwise ok.
ServerVector valid_but_excluded;
MonServerArray valid_but_excluded;
// Index of the current best candidate in slaves_out
int master_vector_index = -1;
@ -1200,13 +1205,13 @@ MXS_MONITORED_SERVER* MariaDBMonitor::select_new_master(ServerVector* slaves_out
if (current_best)
{
// Remove the selected master from the vector.
ServerVector::iterator remove_this = slaves_out->begin();
MonServerArray::iterator remove_this = slaves_out->begin();
remove_this += master_vector_index;
slaves_out->erase(remove_this);
}
// Check if any of the excluded servers would be better than the best candidate.
for (ServerVector::const_iterator iter = valid_but_excluded.begin();
for (MonServerArray::const_iterator iter = valid_but_excluded.begin();
iter != valid_but_excluded.end();
iter++)
{
@ -1529,36 +1534,6 @@ bool MariaDBMonitor::mon_process_failover(bool* cluster_modified_out)
return rval;
}
/**
* Print a redirect error to logs. If err_out exists, generate a combined error message by querying all
* the server parameters for connection errors and append these errors to err_out.
*
* @param demotion_target If not NULL, this is the first server to query.
* @param redirectable_slaves Other servers to query for errors.
* @param err_out If not null, the error output object.
*/
void print_redirect_errors(MXS_MONITORED_SERVER* first_server, const ServerVector& servers,
json_t** err_out)
{
// Individual server errors have already been printed to the log.
// For JSON, gather the errors again.
const char MSG[] = "Could not redirect any slaves to the new master.";
MXS_ERROR(MSG);
if (err_out)
{
ServerVector failed_slaves;
if (first_server)
{
failed_slaves.push_back(first_server);
}
failed_slaves.insert(failed_slaves.end(),
servers.begin(), servers.end());
string combined_error = get_connection_errors(failed_slaves);
*err_out = mxs_json_error_append(*err_out,
"%s Errors: %s.", MSG, combined_error.c_str());
}
}
bool MariaDBMonitor::uses_gtid(MXS_MONITORED_SERVER* mon_server, json_t** error_out)
{
bool rval = false;
@ -1624,3 +1599,33 @@ bool MariaDBMonitor::slave_receiving_events()
}
return received_event;
}
/**
* Print a redirect error to logs. If err_out exists, generate a combined error message by querying all
* the server parameters for connection errors and append these errors to err_out.
*
* @param demotion_target If not NULL, this is the first server to query.
* @param redirectable_slaves Other servers to query for errors.
* @param err_out If not null, the error output object.
*/
static void print_redirect_errors(MXS_MONITORED_SERVER* first_server, const MonServerArray& servers,
json_t** err_out)
{
// Individual server errors have already been printed to the log.
// For JSON, gather the errors again.
const char MSG[] = "Could not redirect any slaves to the new master.";
MXS_ERROR(MSG);
if (err_out)
{
MonServerArray failed_slaves;
if (first_server)
{
failed_slaves.push_back(first_server);
}
failed_slaves.insert(failed_slaves.end(),
servers.begin(), servers.end());
string combined_error = get_connection_errors(failed_slaves);
*err_out = mxs_json_error_append(*err_out,
"%s Errors: %s.", MSG, combined_error.c_str());
}
}

View File

@ -16,7 +16,9 @@
#include <algorithm>
#include "utilities.hh"
GtidList GtidList::from_string(const std::string& gtid_string)
using std::string;
GtidList GtidList::from_string(const string& gtid_string)
{
ss_dassert(gtid_string.size());
GtidList rval;

View File

@ -946,7 +946,7 @@ void MariaDBMonitor::log_master_changes(MXS_MONITORED_SERVER* root_master, int*
void MariaDBMonitor::handle_auto_rejoin()
{
ServerVector joinable_servers;
MonServerArray joinable_servers;
if (get_joinable_servers(&joinable_servers))
{
uint32_t joins = do_rejoin(joinable_servers);

View File

@ -33,10 +33,9 @@ class MariaDBMonitor;
// 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!
// TODO: Most of following should be class methods
void print_redirect_errors(MXS_MONITORED_SERVER* first_server, const ServerVector& servers, json_t** err_out);
typedef std::vector<MariaDBServer> ServerArray; // TODO: Rename/get rid of ServerVector typedef!
// Server pointer array, used for temporary server collections
typedef std::vector<MariaDBServer*> ServerRefArray;
// MariaDB Monitor instance data
class MariaDBMonitor
@ -140,7 +139,7 @@ private:
unsigned long m_id; /**< Monitor ID */
volatile int m_shutdown; /**< Flag to shutdown the monitor thread. */
volatile int m_status; /**< Monitor status. */
ServerContainer m_servers; /**< Servers of the monitor. */
ServerArray m_servers; /**< Servers of the monitor. */
ServerInfoMap m_server_info; /**< Contains server specific information */
// Values updated by monitor
@ -171,7 +170,7 @@ private:
bool m_auto_failover; /**< If automatic master failover is enabled */
bool m_auto_rejoin; /**< Attempt to start slave replication on standalone servers or servers
* replicating from the wrong master automatically. */
ServerVector m_excluded_servers; /**< Servers banned for master promotion during auto-failover. */
MonServerArray m_excluded_servers; /**< Servers banned for master promotion during auto-failover. */
// Other settings
std::string m_script; /**< Script to call when state changes occur on servers */
@ -190,13 +189,13 @@ private:
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, MariaDBServer* info,
json_t** err_out);
bool switchover_wait_slaves_catchup(const ServerVector& slaves, const GtidList& gtid, int total_timeout,
bool switchover_wait_slaves_catchup(const MonServerArray& slaves, const GtidList& gtid, int total_timeout,
int read_timeout, json_t** err_out);
bool wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master, const ServerVector& slaves,
bool wait_cluster_stabilization(MXS_MONITORED_SERVER* new_master, const MonServerArray& slaves,
int seconds_remaining);
bool switchover_check_preferred_master(MXS_MONITORED_SERVER* preferred, 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(MonServerArray* slaves_out, json_t** err_out);
bool server_is_excluded(const MXS_MONITORED_SERVER* server);
bool is_candidate_better(const MariaDBServer* current_best_info, const MariaDBServer* candidate_info,
uint32_t gtid_domain);
@ -208,13 +207,13 @@ private:
bool set_standalone_master(MXS_MONITORED_SERVER *db);
bool failover_not_possible();
std::string generate_change_master_cmd(const std::string& master_host, int master_port);
int redirect_slaves(MXS_MONITORED_SERVER* new_master, const ServerVector& slaves,
ServerVector* redirected_slaves);
int redirect_slaves(MXS_MONITORED_SERVER* new_master, const MonServerArray& slaves,
MonServerArray* redirected_slaves);
bool set_replication_credentials(const MXS_CONFIG_PARAMETER* params);
bool start_external_replication(MXS_MONITORED_SERVER* new_master, json_t** err_out);
bool switchover_start_slave(MXS_MONITORED_SERVER* old_master, SERVER* new_master);
bool redirect_one_slave(MXS_MONITORED_SERVER* slave, const char* change_cmd);
bool get_joinable_servers(ServerVector* output);
bool get_joinable_servers(MonServerArray* output);
bool join_cluster(MXS_MONITORED_SERVER* server, const char* change_cmd);
void set_master_heartbeat(MXS_MONITORED_SERVER *);
void set_slave_heartbeat(MXS_MONITORED_SERVER *);
@ -224,7 +223,7 @@ private:
bool do_switchover(MXS_MONITORED_SERVER* current_master, MXS_MONITORED_SERVER* new_master,
json_t** err_out);
bool do_failover(json_t** err_out);
uint32_t do_rejoin(const ServerVector& joinable_servers);
uint32_t do_rejoin(const MonServerArray& joinable_servers);
bool mon_process_failover(bool* cluster_modified_out);
bool server_is_rejoin_suspect(MXS_MONITORED_SERVER* server, MariaDBServer* master_info,
json_t** output);

View File

@ -19,6 +19,8 @@
#include <maxscale/thread.h>
#include "utilities.hh"
using std::string;
SlaveStatusInfo::SlaveStatusInfo()
: master_server_id(SERVER_ID_UNKNOWN)
, master_port(0)

View File

@ -20,6 +20,8 @@
#include <maxscale/debug.h>
#include <maxscale/mysql_utils.h>
using std::string;
/** Server id default value */
const int64_t SERVER_ID_UNKNOWN = -1;
@ -39,50 +41,11 @@ int64_t scan_server_id(const char* id_string)
return server_id;
}
bool query_one_row(MXS_MONITORED_SERVER *database, const char* query, unsigned int expected_cols,
StringVector* output)
{
bool rval = false;
MYSQL_RES *result;
if (mxs_mysql_query(database->con, query) == 0 && (result = mysql_store_result(database->con)) != NULL)
{
unsigned int columns = mysql_field_count(database->con);
if (columns != expected_cols)
{
mysql_free_result(result);
MXS_ERROR("Unexpected result for '%s'. Expected %d columns, got %d. Server version: %s",
query, expected_cols, columns, database->server->version_string);
}
else
{
MYSQL_ROW row = mysql_fetch_row(result);
if (row)
{
for (unsigned int i = 0; i < columns; i++)
{
output->push_back((row[i] != NULL) ? row[i] : "");
}
rval = true;
}
else
{
MXS_ERROR("Query '%s' returned no rows.", query);
}
mysql_free_result(result);
}
}
else
{
mon_report_query_error(database);
}
return rval;
}
string get_connection_errors(const ServerVector& servers)
string get_connection_errors(const MonServerArray& servers)
{
// Get errors from all connections, form a string.
std::stringstream ss;
for (ServerVector::const_iterator iter = servers.begin(); iter != servers.end(); iter++)
for (MonServerArray::const_iterator iter = servers.begin(); iter != servers.end(); iter++)
{
const char* error = mysql_error((*iter)->con);
ss_dassert(*error); // Every connection should have an error.
@ -95,7 +58,7 @@ string get_connection_errors(const ServerVector& servers)
return ss.str();
}
string monitored_servers_to_string(const ServerVector& array)
string monitored_servers_to_string(const MonServerArray& array)
{
string rval;
size_t array_size = array.size();

View File

@ -29,10 +29,7 @@
}\
} while (false)
using std::string;
typedef std::vector<string> StringVector;
typedef std::vector<MXS_MONITORED_SERVER*> ServerVector;
typedef std::vector<MXS_MONITORED_SERVER*> MonServerArray;
extern const int64_t SERVER_ID_UNKNOWN;
@ -44,25 +41,13 @@ extern const int64_t SERVER_ID_UNKNOWN;
*/
int64_t scan_server_id(const char* id_string);
/**
* Query one row of results, save strings to array. Any additional rows are ignored.
*
* @param database The database to query.
* @param query The query to execute.
* @param expected_cols How many columns the result should have.
* @param output The output array to populate.
* @return True on success.
*/
bool query_one_row(MXS_MONITORED_SERVER *database, const char* query, unsigned int expected_cols,
StringVector* output);
/**
* Get MariaDB connection error strings from all the given servers, form one string.
*
* @param slaves Servers with errors
* @return Concatenated string.
*/
string get_connection_errors(const ServerVector& servers);
std::string get_connection_errors(const MonServerArray& servers);
/**
* Generates a list of server names separated by ', '
@ -70,7 +55,7 @@ string get_connection_errors(const ServerVector& servers);
* @param array The servers
* @return Server names
*/
string monitored_servers_to_string(const ServerVector& array);
std::string monitored_servers_to_string(const MonServerArray& array);
/**
* Helper class for simplifying working with resultsets. Used in MariaDBServer.
@ -112,7 +97,7 @@ public:
* @param col_name Column name
* @return Index or -1 if not found.
*/
int64_t get_col_index(const string& col_name) const;
int64_t get_col_index(const std::string& col_name) const;
/**
* Read a string value from the current row and given column. Empty string and (null) are both interpreted
@ -121,7 +106,7 @@ public:
* @param column_ind Column index
* @return Value as string
*/
string get_string(int64_t column_ind) const;
std::string get_string(int64_t column_ind) const;
/**
* Read a non-negative integer value from the current row and given column.
@ -141,7 +126,7 @@ public:
private:
MYSQL_RES* m_resultset; // Underlying result set, freed at dtor.
std::tr1::unordered_map<string, int64_t> m_col_indexes; // Map of column name -> index
std::tr1::unordered_map<std::string, int64_t> m_col_indexes; // Map of column name -> index
int64_t m_columns; // How many columns does the data have. Usually equal to column index map size.
MYSQL_ROW m_rowdata; // Data for current row
int64_t m_current_row; // Index of current row