Move settings shared between MariaDBMonitor and MariaDBServer to container

Removes the need to communicate these settings via call parameters.
This commit is contained in:
Esa Korhonen
2019-05-09 15:27:49 +03:00
parent d10c7a4140
commit 476dc80bc9
9 changed files with 84 additions and 91 deletions

View File

@ -166,7 +166,7 @@ void MariaDBMonitor::tarjan_scc_visit_node(MariaDBServer* node,
*/
void MariaDBMonitor::build_replication_graph()
{
const bool use_hostnames = m_assume_unique_hostnames;
const bool use_hostnames = m_settings.shared.assume_unique_hostnames;
// First, reset all node data.
for (MariaDBServer* server : m_servers)
{

View File

@ -283,7 +283,7 @@ bool MariaDBMonitor::manual_reset_replication(SERVER* master_server, json_t** er
if (!error)
{
MXB_NOTICE("read_only set on affected servers.");
if (m_handle_event_scheduler)
if (m_settings.shared.handle_event_scheduler)
{
for (MariaDBServer* server : targets)
{
@ -326,7 +326,7 @@ bool MariaDBMonitor::manual_reset_replication(SERVER* master_server, json_t** er
// Point of no return, perform later steps even if an error occurs.
m_next_master = new_master;
if (m_handle_event_scheduler)
if (m_settings.shared.handle_event_scheduler)
{
if (old_master)
{
@ -391,7 +391,7 @@ bool MariaDBMonitor::manual_reset_replication(SERVER* master_server, json_t** er
}
/**
* Generate a CHANGE MASTER TO-query.
* Generate a CHANGE MASTER TO-query. TODO: Use the version in MariaDBServer instead.
*
* @param master_host Master hostname/address
* @param master_port Master port
@ -403,11 +403,11 @@ string MariaDBMonitor::generate_change_master_cmd(const string& master_host, int
change_cmd << "CHANGE MASTER TO MASTER_HOST = '" << master_host << "', ";
change_cmd << "MASTER_PORT = " << master_port << ", ";
change_cmd << "MASTER_USE_GTID = current_pos, ";
if (m_replication_ssl)
if (m_settings.shared.replication_ssl)
{
change_cmd << "MASTER_SSL = 1, ";
}
change_cmd << "MASTER_USER = '" << m_replication_user << "', ";
change_cmd << "MASTER_USER = '" << m_settings.shared.replication_user << "', ";
const char MASTER_PW[] = "MASTER_PASSWORD = '";
const char END[] = "';";
#if defined (SS_DEBUG)
@ -416,7 +416,7 @@ string MariaDBMonitor::generate_change_master_cmd(const string& master_host, int
change_cmd_nopw << MASTER_PW << "******" << END;
MXS_DEBUG("Change master command is '%s'.", change_cmd_nopw.str().c_str());
#endif
change_cmd << MASTER_PW << m_replication_password << END;
change_cmd << MASTER_PW << m_settings.shared.replication_password << END;
return change_cmd.str();
}
@ -627,15 +627,13 @@ uint32_t MariaDBMonitor::do_rejoin(const ServerArray& joinable_servers, json_t**
// Rejoin doesn't have its own time limit setting. Use switchover time limit for now since
// the first phase of standalone rejoin is similar to switchover.
maxbase::Duration time_limit((double)m_switchover_timeout);
GeneralOpData general(m_replication_user, m_replication_password, m_replication_ssl,
output, time_limit);
GeneralOpData general(output, time_limit);
if (joinable->m_slave_status.empty())
{
// Assume that server is an old master which was failed over. Even if this is not really
// the case, the following is unlikely to do damage.
ServerOperation demotion(joinable, true, /* treat as old master */
m_handle_event_scheduler, m_demote_sql_file);
ServerOperation demotion(joinable, true);
if (joinable->demote(general, demotion))
{
MXS_NOTICE("Directing standalone server '%s' to replicate from '%s'.", name, master_name);
@ -1416,10 +1414,8 @@ unique_ptr<MariaDBMonitor::FailoverParams> MariaDBMonitor::failover_prepare(Log
auto time_limit = maxbase::Duration((double)m_failover_timeout);
bool promoting_to_master = (demotion_target == m_master);
ServerOperation promotion(promotion_target, promoting_to_master,
m_handle_event_scheduler, m_promote_sql_file,
demotion_target->m_slave_status, demotion_target->m_enabled_events);
GeneralOpData general(m_replication_user, m_replication_password, m_replication_ssl,
error_out, time_limit);
GeneralOpData general(error_out, time_limit);
rval.reset(new FailoverParams(promotion, demotion_target, general));
}
}
@ -1692,14 +1688,11 @@ MariaDBMonitor::switchover_prepare(SERVER* promotion_server, SERVER* demotion_se
{
maxbase::Duration time_limit((double)m_switchover_timeout);
bool master_swap = (demotion_target == m_master);
ServerOperation promotion(promotion_target, master_swap, m_handle_event_scheduler,
m_promote_sql_file,
ServerOperation promotion(promotion_target, master_swap,
demotion_target->m_slave_status, demotion_target->m_enabled_events);
ServerOperation demotion(demotion_target, master_swap, m_handle_event_scheduler,
m_demote_sql_file, promotion_target->m_slave_status,
ServerOperation demotion(demotion_target, master_swap, promotion_target->m_slave_status,
EventNameSet() /* unused */);
GeneralOpData general(m_replication_user, m_replication_password, m_replication_ssl,
error_out, time_limit);
GeneralOpData general(error_out, time_limit);
rval.reset(new SwitchoverParams(promotion, demotion, general));
}
return rval;

View File

@ -79,8 +79,7 @@ void MariaDBMonitor::reset_server_info()
// Next, initialize the data.
for (auto mon_server : Monitor::m_servers)
{
m_servers.push_back(new MariaDBServer(mon_server, m_servers.size(),
m_assume_unique_hostnames, m_handle_event_scheduler));
m_servers.push_back(new MariaDBServer(mon_server, m_servers.size(), m_settings.shared));
}
}
@ -187,9 +186,9 @@ bool MariaDBMonitor::set_replication_credentials(const MXS_CONFIG_PARAMETER* par
}
}
m_replication_user = repl_user;
m_settings.shared.replication_user = repl_user;
char* decrypted = decrypt_password(repl_pw.c_str());
m_replication_password = decrypted;
m_settings.shared.replication_password = decrypted;
MXS_FREE(decrypted);
return true;
@ -217,7 +216,7 @@ bool MariaDBMonitor::configure(const MXS_CONFIG_PARAMETER* params)
m_settings.detect_stale_slave = params->get_bool("detect_stale_slave");
m_settings.detect_standalone_master = params->get_bool(CN_DETECT_STANDALONE_MASTER);
m_settings.ignore_external_masters = params->get_bool("ignore_external_masters");
m_assume_unique_hostnames = params->get_bool(CN_ASSUME_UNIQUE_HOSTNAMES);
m_settings.shared.assume_unique_hostnames = params->get_bool(CN_ASSUME_UNIQUE_HOSTNAMES);
m_settings.failcount = params->get_integer(CN_FAILCOUNT);
m_failover_timeout = params->get_duration<std::chrono::seconds>(CN_FAILOVER_TIMEOUT).count();
m_switchover_timeout = params->get_duration<std::chrono::seconds>(CN_SWITCHOVER_TIMEOUT).count();
@ -226,12 +225,12 @@ bool MariaDBMonitor::configure(const MXS_CONFIG_PARAMETER* params)
m_settings.enforce_read_only_slaves = params->get_bool(CN_ENFORCE_READONLY);
m_verify_master_failure = params->get_bool(CN_VERIFY_MASTER_FAILURE);
m_master_failure_timeout = params->get_duration<std::chrono::seconds>(CN_MASTER_FAILURE_TIMEOUT).count();
m_promote_sql_file = params->get_string(CN_PROMOTION_SQL_FILE);
m_demote_sql_file = params->get_string(CN_DEMOTION_SQL_FILE);
m_settings.shared.promotion_sql_file = params->get_string(CN_PROMOTION_SQL_FILE);
m_settings.shared.demotion_sql_file = params->get_string(CN_DEMOTION_SQL_FILE);
m_settings.switchover_on_low_disk_space = params->get_bool(CN_SWITCHOVER_ON_LOW_DISK_SPACE);
m_settings.maintenance_on_low_disk_space = params->get_bool(CN_MAINTENANCE_ON_LOW_DISK_SPACE);
m_handle_event_scheduler = params->get_bool(CN_HANDLE_EVENTS);
m_replication_ssl = params->get_bool(CN_REPLICATION_MASTER_SSL);
m_settings.shared.handle_event_scheduler = params->get_bool(CN_HANDLE_EVENTS);
m_settings.shared.replication_ssl = params->get_bool(CN_REPLICATION_MASTER_SSL);
/* Reset all monitored state info. The server dependent values must be reset as servers could have been
* added, removed and modified. */
@ -261,7 +260,7 @@ bool MariaDBMonitor::configure(const MXS_CONFIG_PARAMETER* params)
{
settings_ok = false;
}
if (!m_assume_unique_hostnames)
if (!m_settings.shared.assume_unique_hostnames)
{
const char requires[] = "%s requires that %s is on.";
if (m_settings.auto_failover)
@ -667,16 +666,18 @@ bool MariaDBMonitor::check_sql_files()
const char ERRMSG[] = "%s ('%s') does not exist or cannot be accessed for reading: '%s'.";
bool rval = true;
if (!m_promote_sql_file.empty() && access(m_promote_sql_file.c_str(), R_OK) != 0)
auto prom_file = m_settings.shared.promotion_sql_file;
if (!prom_file.empty() && access(prom_file.c_str(), R_OK) != 0)
{
rval = false;
MXS_ERROR(ERRMSG, CN_PROMOTION_SQL_FILE, m_promote_sql_file.c_str(), mxs_strerror(errno));
MXS_ERROR(ERRMSG, CN_PROMOTION_SQL_FILE, prom_file.c_str(), mxs_strerror(errno));
}
if (!m_demote_sql_file.empty() && access(m_demote_sql_file.c_str(), R_OK) != 0)
auto dem_file = m_settings.shared.demotion_sql_file;
if (!dem_file.empty() && access(dem_file.c_str(), R_OK) != 0)
{
rval = false;
MXS_ERROR(ERRMSG, CN_DEMOTION_SQL_FILE, m_demote_sql_file.c_str(), mxs_strerror(errno));
MXS_ERROR(ERRMSG, CN_DEMOTION_SQL_FILE, dem_file.c_str(), mxs_strerror(errno));
}
return rval;
}

View File

@ -191,7 +191,7 @@ private:
std::string m_external_master_host; /* External master host, for fail/switchover */
int m_external_master_port = PORT_UNKNOWN; /* External master port */
// MariaDB-Monitor specific settings
// MariaDB-Monitor specific settings. These are only written to when configuring the monitor.
class Settings
{
public:
@ -220,28 +220,19 @@ private:
* on disk space to maintenance. */
bool enforce_read_only_slaves {false}; /* If true, the monitor checks and enforces every tick
* that all slaves are in read-only-mode. */
SharedSettings shared; /* Settings required by MariaDBServer objects */
};
Settings m_settings;
bool m_assume_unique_hostnames {true}; /* Are server hostnames consistent between MaxScale and servers */
// Cluster operations additional settings
std::string m_replication_user; /* Replication user for CHANGE MASTER TO-commands */
std::string m_replication_password; /* Replication password for CHANGE MASTER TO-commands */
bool m_replication_ssl = false; /* Set MASTER_SSL = 1 in CHANGE MASTER TO-commands */
bool m_handle_event_scheduler = true;/* Should failover/switchover enable/disable any scheduled
* events on the servers during promote/demote? */
uint32_t m_failover_timeout = 10; /* Time limit in seconds for failover */
uint32_t m_switchover_timeout = 10; /* Time limit in seconds for switchover */
bool m_verify_master_failure = true; /* Is master failure is verified via slaves? */
int m_master_failure_timeout = 10; /* Master failure verification (via slaves) time in seconds */
ServerArray m_excluded_servers; /* Servers which cannot be autoselected when deciding which
* slave to promote during failover switchover. */
std::string m_promote_sql_file; /* File with sql commands which are ran to a server being
* promoted. */
std::string m_demote_sql_file; /* File with sql commands which are ran to a server being
* demoted. */
// Fields controlling logging of various events. TODO: Check these
bool m_log_no_master = true; /* Should it be logged that there is no master? */

View File

@ -31,11 +31,10 @@ using Guard = std::lock_guard<std::mutex>;
using maxscale::MonitorServer;
MariaDBServer::MariaDBServer(MonitorServer* monitored_server, int config_index,
bool assume_unique_hostnames, bool query_events)
const SharedSettings& settings)
: m_server_base(monitored_server)
, m_config_index(config_index)
, m_assume_unique_hostnames(assume_unique_hostnames)
, m_query_events(query_events)
, m_settings(settings)
{
mxb_assert(monitored_server);
}
@ -872,7 +871,7 @@ void MariaDBServer::monitor_server()
{
query_ok = update_gtids(&errmsg);
}
if (query_ok && m_query_events)
if (query_ok && m_settings.handle_event_scheduler)
{
query_ok = update_enabled_events();
}
@ -1210,7 +1209,7 @@ const SlaveStatus* MariaDBServer::slave_connection_status(const MariaDBServer* t
// connected to the parent. TODO: Use the information gathered in 'build_replication_graph'
// to skip this function, as the contents are very similar.
const SlaveStatus* rval = NULL;
if (m_assume_unique_hostnames)
if (m_settings.assume_unique_hostnames)
{
// Can simply compare host:port.
SERVER* target_srv = target->m_server_base->server;
@ -1535,7 +1534,7 @@ bool MariaDBServer::promote(GeneralOpData& general, ServerOperation& promotion,
}
else
{
if (promotion.handle_events)
if (m_settings.handle_event_scheduler)
{
// TODO: Add query replying to enable_events
bool events_enabled = enable_events(promotion.events_to_enable, error_out);
@ -1548,7 +1547,7 @@ bool MariaDBServer::promote(GeneralOpData& general, ServerOperation& promotion,
}
// Run promotion_sql_file if no errors so far.
const string& sql_file = promotion.sql_file;
const string& sql_file = m_settings.promotion_sql_file;
if (!promotion_error && !sql_file.empty())
{
bool file_ran_ok = run_sql_from_file(sql_file, error_out);
@ -1630,7 +1629,7 @@ bool MariaDBServer::demote(GeneralOpData& general, ServerOperation& demotion)
}
else
{
if (demotion.handle_events)
if (m_settings.handle_event_scheduler)
{
// TODO: Add query replying to enable_events
// Step 2b: Using BINLOG_OFF to avoid adding any gtid events,
@ -1645,7 +1644,7 @@ bool MariaDBServer::demote(GeneralOpData& general, ServerOperation& demotion)
}
// Step 2c: Run demotion_sql_file if no errors so far.
const string& sql_file = demotion.sql_file;
const string& sql_file = m_settings.demotion_sql_file;
if (!demotion_error && !sql_file.empty())
{
bool file_ran_ok = run_sql_from_file(sql_file, error_out);
@ -2087,18 +2086,18 @@ string MariaDBServer::generate_change_master_cmd(GeneralOpData& op, const SlaveS
slave_conn.name.c_str(),
slave_conn.master_host.c_str(), slave_conn.master_port);
change_cmd += "MASTER_USE_GTID = current_pos, ";
if (op.replication_ssl)
if (m_settings.replication_ssl)
{
change_cmd += "MASTER_SSL = 1, ";
}
change_cmd += string_printf("MASTER_USER = '%s', ", op.replication_user.c_str());
change_cmd += string_printf("MASTER_USER = '%s', ", m_settings.replication_user.c_str());
const char MASTER_PW[] = "MASTER_PASSWORD = '%s';";
#if defined (SS_DEBUG)
string change_cmd_nopw = change_cmd;
change_cmd_nopw += string_printf(MASTER_PW, "******");
MXS_DEBUG("Change master command is '%s'.", change_cmd_nopw.c_str());
#endif
change_cmd += string_printf(MASTER_PW, op.replication_password.c_str());
change_cmd += string_printf(MASTER_PW, m_settings.replication_password.c_str());
return change_cmd;
}

View File

@ -74,7 +74,7 @@ class MariaDBServer
{
public:
MariaDBServer(mxs::MonitorServer* monitored_server, int config_index,
bool assume_unique_hostnames, bool query_events);
const SharedSettings& settings);
class EventInfo
{
@ -139,19 +139,19 @@ public:
/* Replication lag of the server. Used during calculation so that the actual SERVER struct is
* only written to once. */
int m_replication_lag = SERVER::RLAG_UNDEFINED;
/* Copy of same field in monitor object. TODO: pass in struct when adding concurrent updating. */
bool m_assume_unique_hostnames = true;
/* Has anything that could affect replication topology changed this iteration?
* Causes: server id, slave connections, read-only. */
bool m_topology_changed = true;
/* Miscellaneous replication related settings. These are not normally queried from the server, call
* 'update_replication_settings' before use. */
ReplicationSettings m_rpl_settings;
bool m_query_events; /* Copy of monitor->m_handle_event_scheduler. TODO: move elsewhere */
EventNameSet m_enabled_events; /* Enabled scheduled events */
EventNameSet m_enabled_events; /* Enabled scheduled events */
bool m_print_update_errormsg {true}; /* Should an update error be printed? */
bool m_print_update_errormsg = true; /* Should an update error be printed? */
const SharedSettings& m_settings; /* Settings required for various operations */
/**
* Print server information to a json object.

View File

@ -155,31 +155,23 @@ bool SlaveStatus::should_be_copied(string* ignore_reason_out) const
return accepted;
}
ServerOperation::ServerOperation(MariaDBServer* target, bool was_is_master, bool handle_events,
const std::string& sql_file, const SlaveStatusArray& conns_to_copy,
ServerOperation::ServerOperation(MariaDBServer* target, bool was_is_master,
const SlaveStatusArray& conns_to_copy,
const EventNameSet& events_to_enable)
: target(target)
, to_from_master(was_is_master)
, handle_events(handle_events)
, sql_file(sql_file)
, conns_to_copy(conns_to_copy)
, events_to_enable(events_to_enable)
{
}
ServerOperation::ServerOperation(MariaDBServer* target, bool was_is_master, bool handle_events,
const std::string& sql_file)
: ServerOperation(target, was_is_master, handle_events, sql_file,
SlaveStatusArray() /* empty */, EventNameSet() /* empty */)
ServerOperation::ServerOperation(MariaDBServer* target, bool was_is_master)
: ServerOperation(target, was_is_master, SlaveStatusArray() /* empty */, EventNameSet() /* empty */)
{
}
GeneralOpData::GeneralOpData(const std::string& replication_user, const std::string& replication_password,
bool replication_ssl, json_t** error, maxbase::Duration time_remaining)
: replication_user(replication_user)
, replication_password(replication_password)
, replication_ssl(replication_ssl)
, error_out(error)
GeneralOpData::GeneralOpData(json_t** error, maxbase::Duration time_remaining)
: error_out(error)
, time_remaining(time_remaining)
{
}

View File

@ -220,14 +220,10 @@ enum class OperationType
class GeneralOpData
{
public:
const std::string replication_user; // User for CHANGE MASTER TO ...
const std::string replication_password; // Password for CHANGE MASTER TO ...
const bool replication_ssl; // MASTER_SSL=1 in CHANGE MASTER TO ...
json_t** const error_out; // Json error output
maxbase::Duration time_remaining; // How much time remains to complete the operation
GeneralOpData(const std::string& replication_user, const std::string& replication_password,
bool replication_ssl, json_t** error, maxbase::Duration time_remaining);
GeneralOpData(json_t** error, maxbase::Duration time_remaining);
};
// Operation data which concerns a single server
@ -236,16 +232,37 @@ class ServerOperation
public:
MariaDBServer* const target; // Target server
const bool to_from_master; // Was the target a master / should it become one
const bool handle_events; // Should scheduled server events be disabled/enabled?
const std::string sql_file; // Path to file with SQL commands to run during op
const SlaveStatusArray conns_to_copy; // Slave connections the target should copy/merge
const EventNameSet events_to_enable; // Scheduled event names last seen on master.
ServerOperation(MariaDBServer* target, bool was_is_master, bool handle_events,
const std::string& sql_file, const SlaveStatusArray& conns_to_copy,
ServerOperation(MariaDBServer* target, bool was_is_master,
const SlaveStatusArray& conns_to_copy,
const EventNameSet& events_to_enable);
ServerOperation(MariaDBServer* target, bool was_is_master, bool handle_events,
const std::string& sql_file);
ServerOperation(MariaDBServer* target, bool was_is_master);
};
// Settings shared between the MariaDB-Monitor and the MariaDB-Servers.
// These are only written to when configuring the monitor.
class SharedSettings
{
public:
// Required by cluster operations
std::string replication_user; /**< Username for CHANGE MASTER TO-commands */
std::string replication_password; /**< Password for CHANGE MASTER TO-commands */
bool replication_ssl {false}; /**< Set MASTER_SSL = 1 in CHANGE MASTER TO-commands */
std::string promotion_sql_file; /**< File with sql commands which are ran to a server being
* promoted. */
std::string demotion_sql_file; /**< File with sql commands which are ran to a server being
* demoted. */
/* Should failover/switchover enable/disable any scheduled events on the servers during
* promotion/demotion? */
bool handle_event_scheduler {true};
// Miscellaneous settings
bool assume_unique_hostnames {true}; /**< Are server hostnames consistent between MaxScale and
* servers */
};

View File

@ -152,7 +152,7 @@ int MariaDBMonitor::Test::run_tests()
void MariaDBMonitor::Test::init_servers(int count)
{
clear_servers();
m_monitor->m_assume_unique_hostnames = m_use_hostnames;
m_monitor->m_settings.shared.assume_unique_hostnames = m_use_hostnames;
mxb_assert(m_monitor->m_servers.empty() && m_monitor->m_servers_by_id.empty());
for (int i = 1; i < count + 1; i++)
@ -160,7 +160,7 @@ void MariaDBMonitor::Test::init_servers(int count)
// Server contents mostly undefined
auto base_server = Server::create_test_server();
MonitorServer* mon_server = new MonitorServer(base_server, m_monitor->settings().disk_space_limits);
MariaDBServer* mariadb_server = new MariaDBServer(mon_server, i - 1, m_use_hostnames, true);
MariaDBServer* mariadb_server = new MariaDBServer(mon_server, i - 1, m_monitor->m_settings.shared);
if (m_use_hostnames)
{