diff --git a/include/maxscale/monitor.hh b/include/maxscale/monitor.hh index cf0abe418..f1f72909e 100644 --- a/include/maxscale/monitor.hh +++ b/include/maxscale/monitor.hh @@ -123,12 +123,33 @@ enum mxs_monitor_event_t NEW_NDB_EVENT = (1 << 21), /**< new_ndb */ }; +enum credentials_approach_t +{ + CREDENTIALS_INCLUDE, + CREDENTIALS_EXCLUDE, +}; + /** * The linked list of servers that are being monitored by the monitor module. */ class MXS_MONITORED_SERVER { public: + class ConnectionSettings + { + public: + std::string username; /**< Monitor username */ + std::string password; /**< Monitor password */ + int connect_timeout {1}; /**< Connect timeout in seconds for mysql_real_connect */ + int write_timeout {1}; /**< Timeout in seconds for each attempt to write to the server. + * There are retries and the total effective timeout value is two + * times the option value. */ + int read_timeout {1}; /**< Timeout in seconds to read from the server. There are retries + * and the total effective timeout value is three times the + * option value. */ + int connect_attempts {1}; /**< How many times a connection is attempted */ + }; + MXS_MONITORED_SERVER(SERVER* server); /** @@ -140,6 +161,16 @@ public: static const int BEING_DRAINED_OFF = 3; static const int BEING_DRAINED_ON = 4; + /** + * Ping or connect to a database. If connection does not exist or ping fails, a new connection is created. + * This will always leave a valid database handle in the database->con pointer, allowing the user to call + * MySQL C API functions to find out the reason of the failure. + * + * @param settings Connection settings + * @return Connection status. + */ + mxs_connect_result_t ping_or_connect(const ConnectionSettings& settings); + SERVER* server = nullptr; /**< The server being monitored */ MYSQL* con = nullptr; /**< The MySQL connection */ bool log_version_err = true; @@ -231,6 +262,39 @@ public: */ bool clear_server_status(SERVER* srv, int bit, std::string* errmsg_out); + /** + * Set Monitor timeouts for connect/read/write + * + * @param type The timeout handling type + * @param value The timeout to set + * @param key Timeout setting name + */ + bool set_network_timeout(int, int, const char*); + + /** + * Set username used to connect to backends. + * + * @param user The default username to use when connecting + */ + void set_user(const std::string& user); + + /** + * Set password used to connect to backends. + * + * @param passwd The password in encrypted form + */ + void set_password(const std::string& passwd); + + /** + * Create a list of running servers + * + * @param dest Destination where the string is appended, must be null terminated + * @param len Length of @c dest + * @param approach Whether credentials should be included or not. + */ + void append_node_names(char* dest, int len, int status, + credentials_approach_t approach = CREDENTIALS_EXCLUDE); + void show(DCB* dcb); const char* const m_name; /**< Monitor instance name. TODO: change to string */ @@ -250,17 +314,6 @@ public: MXS_CONFIG_PARAMETER* parameters = nullptr; /**< Configuration parameters */ std::vector m_servers; /**< Monitored servers */ - char user[MAX_MONITOR_USER_LEN]; /**< Monitor username */ - char password[MAX_MONITOR_PASSWORD_LEN]; /**< Monitor password */ - - int connect_timeout; /**< Connect timeout in seconds for mysql_real_connect */ - int connect_attempts; /**< How many times a connection is attempted */ - int read_timeout; /**< Timeout in seconds to read from the server. There are retries - * and the total effective timeout value is three times the option value. */ - int write_timeout; /**< Timeout in seconds for each attempt to write to the server. - * There are retries and the total effective timeout value is two times - * the option value. */ - time_t journal_max_age; /**< Maximum age of journal file */ uint32_t script_timeout; /**< Timeout in seconds for the monitor scripts */ const char* script; /**< Launchable script. */ @@ -291,6 +344,8 @@ protected: * How often should a disk space check be made at most, in milliseconds. Negative values imply * disabling. */ int64_t disk_space_check_interval = -1; + + MXS_MONITORED_SERVER::ConnectionSettings conn_settings; }; Settings m_settings; @@ -355,24 +410,14 @@ bool mon_print_fail_status(MXS_MONITORED_SERVER* mon_srv); * is created. This will always leave a valid database handle in @c *ppCon, allowing the user * to call MySQL C API functions to find out the reason of the failure. * - * @param mon A monitor. - * @param pServer A server. - * @param ppCon Address of pointer to a MYSQL instance. The instance should either be - * valid or NULL. + * @param sett Connection settings + * @param pServer A server + * @param ppConn Address of pointer to a MYSQL instance. The instance should either be + * valid or NULL. * @return Connection status. */ -mxs_connect_result_t mon_ping_or_connect_to_db(const Monitor& mon, SERVER& server, MYSQL** ppCon); - -/** - * Ping or connect to a database. If connection does not exist or ping fails, a new connection is created. - * This will always leave a valid database handle in the database->con pointer, allowing the user to call - * MySQL C API functions to find out the reason of the failure. - * - * @param mon Monitor - * @param database Monitored database - * @return Connection status. - */ -mxs_connect_result_t mon_ping_or_connect_to_db(Monitor* mon, MXS_MONITORED_SERVER* database); +mxs_connect_result_t mon_ping_or_connect_to_db(const MXS_MONITORED_SERVER::ConnectionSettings& sett, + SERVER& server, MYSQL** ppConn); bool mon_connection_is_ok(mxs_connect_result_t connect_result); void mon_log_connect_error(MXS_MONITORED_SERVER* database, mxs_connect_result_t rval); diff --git a/server/core/config_runtime.cc b/server/core/config_runtime.cc index ec7481ae6..63ecc58bc 100644 --- a/server/core/config_runtime.cc +++ b/server/core/config_runtime.cc @@ -588,11 +588,11 @@ bool do_alter_monitor(Monitor* monitor, const char* key, const char* value) bool success = true; if (strcmp(key, CN_USER) == 0) { - monitor_add_user(monitor, value, monitor->password); + monitor->set_user(value); } else if (strcmp(key, CN_PASSWORD) == 0) { - monitor_add_user(monitor, monitor->user, value); + monitor->set_password(value); } else if (strcmp(key, CN_MONITOR_INTERVAL) == 0) { @@ -605,40 +605,28 @@ bool do_alter_monitor(Monitor* monitor, const char* key, const char* value) { if (auto ival = get_positive_int(value)) { - monitor_set_network_timeout(monitor, - MONITOR_CONNECT_TIMEOUT, - ival, - CN_BACKEND_CONNECT_TIMEOUT); + monitor->set_network_timeout(MONITOR_CONNECT_TIMEOUT, ival, CN_BACKEND_CONNECT_TIMEOUT); } } else if (strcmp(key, CN_BACKEND_WRITE_TIMEOUT) == 0) { if (auto ival = get_positive_int(value)) { - monitor_set_network_timeout(monitor, - MONITOR_WRITE_TIMEOUT, - ival, - CN_BACKEND_WRITE_TIMEOUT); + monitor->set_network_timeout(MONITOR_WRITE_TIMEOUT, ival, CN_BACKEND_WRITE_TIMEOUT); } } else if (strcmp(key, CN_BACKEND_READ_TIMEOUT) == 0) { if (auto ival = get_positive_int(value)) { - monitor_set_network_timeout(monitor, - MONITOR_READ_TIMEOUT, - ival, - CN_BACKEND_READ_TIMEOUT); + monitor->set_network_timeout(MONITOR_READ_TIMEOUT, ival, CN_BACKEND_READ_TIMEOUT); } } else if (strcmp(key, CN_BACKEND_CONNECT_ATTEMPTS) == 0) { if (auto ival = get_positive_int(value)) { - monitor_set_network_timeout(monitor, - MONITOR_CONNECT_ATTEMPTS, - ival, - CN_BACKEND_CONNECT_ATTEMPTS); + monitor->set_network_timeout(MONITOR_CONNECT_ATTEMPTS, ival, CN_BACKEND_CONNECT_ATTEMPTS); } } else if (strcmp(key, CN_JOURNAL_MAX_AGE) == 0) diff --git a/server/core/internal/monitor.hh b/server/core/internal/monitor.hh index 7648964b5..3a539d9fa 100644 --- a/server/core/internal/monitor.hh +++ b/server/core/internal/monitor.hh @@ -134,12 +134,10 @@ void monitor_list(DCB*); bool monitor_add_server(Monitor* mon, SERVER* server); void monitor_remove_server(Monitor* mon, SERVER* server); -void monitor_add_user(Monitor*, const char*, const char*); void monitor_add_parameters(Monitor* monitor, const MXS_CONFIG_PARAMETER* params); bool monitor_remove_parameter(Monitor* monitor, const char* key); void monitor_set_parameter(Monitor* monitor, const char* key, const char* value); -bool monitor_set_network_timeout(Monitor*, int, int, const char*); void monitor_set_journal_max_age(Monitor* mon, time_t value); void monitor_set_script_timeout(Monitor* mon, uint32_t value); diff --git a/server/core/monitor.cc b/server/core/monitor.cc index e2c5a7f74..04493f820 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -188,17 +188,18 @@ Monitor::Monitor(const string& name, const string& module) bool Monitor::configure_base(const MXS_CONFIG_PARAMETER* params) { - read_timeout = config_get_integer(params, CN_BACKEND_READ_TIMEOUT); - write_timeout = config_get_integer(params, CN_BACKEND_WRITE_TIMEOUT); - connect_timeout = config_get_integer(params, CN_BACKEND_CONNECT_TIMEOUT); - connect_attempts = config_get_integer(params, CN_BACKEND_CONNECT_ATTEMPTS); + m_settings.conn_settings.read_timeout = config_get_integer(params, CN_BACKEND_READ_TIMEOUT); + m_settings.conn_settings.write_timeout = config_get_integer(params, CN_BACKEND_WRITE_TIMEOUT); + m_settings.conn_settings.connect_timeout = config_get_integer(params, CN_BACKEND_CONNECT_TIMEOUT); + m_settings.conn_settings.connect_attempts = config_get_integer(params, CN_BACKEND_CONNECT_ATTEMPTS); m_settings.interval = config_get_integer(params, CN_MONITOR_INTERVAL); journal_max_age = config_get_integer(params, CN_JOURNAL_MAX_AGE); script_timeout = config_get_integer(params, CN_SCRIPT_TIMEOUT); script = config_get_string(params, CN_SCRIPT); events = config_get_enum(params, CN_EVENTS, mxs_monitor_event_enum_values); m_settings.disk_space_check_interval = config_get_integer(params, CN_DISK_SPACE_CHECK_INTERVAL); - monitor_add_user(this, config_get_string(params, CN_USER), config_get_string(params, CN_PASSWORD)); + m_settings.conn_settings.username = config_get_string(params, CN_USER); + m_settings.conn_settings.password = config_get_string(params, CN_PASSWORD); for (auto& s : mxs::strtok(config_get_string(params, CN_SERVERS), ",")) { @@ -447,25 +448,14 @@ void monitor_remove_server(Monitor* mon, SERVER* server) } } -/** - * Add a default user to the monitor. This user is used to connect to the - * monitored databases but may be overriden on a per server basis. - * - * @param mon The monitor instance - * @param user The default username to use when connecting - * @param passwd The default password associated to the default user. - */ -void monitor_add_user(Monitor* mon, const char* user, const char* passwd) +void Monitor::set_user(const string& user) { - if (user != mon->user) - { - snprintf(mon->user, sizeof(mon->user), "%s", user); - } + m_settings.conn_settings.username = user; +} - if (passwd != mon->password) - { - snprintf(mon->password, sizeof(mon->password), "%s", passwd); - } +void Monitor::set_password(const string& passwd) +{ + m_settings.conn_settings.password = passwd; } /** @@ -502,10 +492,10 @@ void Monitor::show(DCB* dcb) dcb_printf(dcb, "State: %s\n", monitor_state_to_string(m_state)); dcb_printf(dcb, "Times monitored: %lu\n", m_ticks); dcb_printf(dcb, "Sampling interval: %lu milliseconds\n", m_settings.interval); - dcb_printf(dcb, "Connect Timeout: %i seconds\n", monitor->connect_timeout); - dcb_printf(dcb, "Read Timeout: %i seconds\n", monitor->read_timeout); - dcb_printf(dcb, "Write Timeout: %i seconds\n", monitor->write_timeout); - dcb_printf(dcb, "Connect attempts: %i \n", monitor->connect_attempts); + dcb_printf(dcb, "Connect Timeout: %i seconds\n", m_settings.conn_settings.connect_timeout); + dcb_printf(dcb, "Read Timeout: %i seconds\n", m_settings.conn_settings.read_timeout); + dcb_printf(dcb, "Write Timeout: %i seconds\n", m_settings.conn_settings.write_timeout); + dcb_printf(dcb, "Connect attempts: %i \n", m_settings.conn_settings.connect_attempts); dcb_printf(dcb, "Monitored servers: "); const char* sep = ""; @@ -620,14 +610,7 @@ void monitor_set_script_timeout(Monitor* mon, uint32_t value) mon->script_timeout = value; } -/** - * Set Monitor timeouts for connect/read/write - * - * @param mon The monitor instance - * @param type The timeout handling type - * @param value The timeout to set - */ -bool monitor_set_network_timeout(Monitor* mon, int type, int value, const char* key) +bool Monitor::set_network_timeout(int type, int value, const char* key) { bool rval = true; @@ -636,19 +619,19 @@ bool monitor_set_network_timeout(Monitor* mon, int type, int value, const char* switch (type) { case MONITOR_CONNECT_TIMEOUT: - mon->connect_timeout = value; + m_settings.conn_settings.connect_timeout = value; break; case MONITOR_READ_TIMEOUT: - mon->read_timeout = value; + m_settings.conn_settings.read_timeout = value; break; case MONITOR_WRITE_TIMEOUT: - mon->write_timeout = value; + m_settings.conn_settings.write_timeout = value; break; case MONITOR_CONNECT_ATTEMPTS: - mon->connect_attempts = value; + m_settings.conn_settings.connect_attempts = value; break; default: @@ -660,7 +643,7 @@ bool monitor_set_network_timeout(Monitor* mon, int type, int value, const char* } else { - MXS_ERROR("Value '%s' for monitor '%s' is not a positive integer: %d", key, mon->m_name, value); + MXS_ERROR("Value '%s' for monitor '%s' is not a positive integer: %d", key, m_name, value); rval = false; } return rval; @@ -690,13 +673,12 @@ bool Monitor::test_permissions(const string& query) return true; } - char* user = monitor->user; - char* dpasswd = decrypt_password(monitor->password); + char* dpasswd = decrypt_password(m_settings.conn_settings.password.c_str()); bool rval = false; for (MXS_MONITORED_SERVER* mondb : monitor->m_servers) { - if (!mon_connection_is_ok(mon_ping_or_connect_to_db(monitor, mondb))) + if (!mon_connection_is_ok(mondb->ping_or_connect(m_settings.conn_settings))) { MXS_ERROR("[%s] Failed to connect to server '%s' ([%s]:%d) when" " checking monitor user credentials and permissions: %s", @@ -735,7 +717,8 @@ bool Monitor::test_permissions(const string& query) } MXS_ERROR("[%s] Failed to execute query '%s' with user '%s'. MySQL error message: %s", - monitor->m_name, query.c_str(), user, mysql_error(mondb->con)); + m_name, query.c_str(), m_settings.conn_settings.username.c_str(), + mysql_error(mondb->con)); } else { @@ -1014,32 +997,14 @@ static const char* mon_get_event_name(MXS_MONITORED_SERVER* node) return mon_get_event_name((mxs_monitor_event_t)node->server->last_event); } -enum credentials_approach_t -{ - CREDENTIALS_INCLUDE, - CREDENTIALS_EXCLUDE, -}; - -/** - * Create a list of running servers - * - * @param mon The monitor - * @param dest Destination where the string is appended, must be null terminated - * @param len Length of @c dest - * @param approach Whether credentials should be included or not. - */ -static void mon_append_node_names(Monitor* mon, - char* dest, - int len, - int status, - credentials_approach_t approach = CREDENTIALS_EXCLUDE) +void Monitor::append_node_names(char* dest, int len, int status, credentials_approach_t approach) { const char* separator = ""; // Some extra space for port and separator char arr[SERVER::MAX_MONUSER_LEN + SERVER::MAX_MONPW_LEN + SERVER::MAX_ADDRESS_LEN + 64]; dest[0] = '\0'; - for (auto iter = mon->m_servers.begin(); iter != mon->m_servers.end() && len; ++iter) + for (auto iter = m_servers.begin(); iter != m_servers.end() && len; ++iter) { Server* server = static_cast((*iter)->server); if (status == 0 || server->status & status) @@ -1055,8 +1020,8 @@ static void mon_append_node_names(Monitor* mon, } else { - string user = mon->user; - string password = mon->password; + string user = m_settings.conn_settings.username; + string password = m_settings.conn_settings.password; string server_specific_monuser = server->monitor_user(); if (!server_specific_monuser.empty()) { @@ -1213,37 +1178,37 @@ int monitor_launch_command(Monitor* mon, MXS_MONITORED_SERVER* ptr, EXTERNCMD* c if (externcmd_matches(cmd, "$CREDENTIALS")) { // We provide the credentials for _all_ servers. - mon_append_node_names(mon, nodelist, sizeof(nodelist), 0, CREDENTIALS_INCLUDE); + mon->append_node_names(nodelist, sizeof(nodelist), 0, CREDENTIALS_INCLUDE); externcmd_substitute_arg(cmd, "[$]CREDENTIALS", nodelist); } if (externcmd_matches(cmd, "$NODELIST")) { - mon_append_node_names(mon, nodelist, sizeof(nodelist), SERVER_RUNNING); + mon->append_node_names(nodelist, sizeof(nodelist), SERVER_RUNNING); externcmd_substitute_arg(cmd, "[$]NODELIST", nodelist); } if (externcmd_matches(cmd, "$LIST")) { - mon_append_node_names(mon, nodelist, sizeof(nodelist), 0); + mon->append_node_names(nodelist, sizeof(nodelist), 0); externcmd_substitute_arg(cmd, "[$]LIST", nodelist); } if (externcmd_matches(cmd, "$MASTERLIST")) { - mon_append_node_names(mon, nodelist, sizeof(nodelist), SERVER_MASTER); + mon->append_node_names(nodelist, sizeof(nodelist), SERVER_MASTER); externcmd_substitute_arg(cmd, "[$]MASTERLIST", nodelist); } if (externcmd_matches(cmd, "$SLAVELIST")) { - mon_append_node_names(mon, nodelist, sizeof(nodelist), SERVER_SLAVE); + mon->append_node_names(nodelist, sizeof(nodelist), SERVER_SLAVE); externcmd_substitute_arg(cmd, "[$]SLAVELIST", nodelist); } if (externcmd_matches(cmd, "$SYNCEDLIST")) { - mon_append_node_names(mon, nodelist, sizeof(nodelist), SERVER_JOINED); + mon->append_node_names(nodelist, sizeof(nodelist), SERVER_JOINED); externcmd_substitute_arg(cmd, "[$]SYNCEDLIST", nodelist); } @@ -1341,45 +1306,47 @@ int monitor_launch_script(Monitor* mon, MXS_MONITORED_SERVER* ptr, const char* s return rv; } -mxs_connect_result_t mon_ping_or_connect_to_db(const Monitor& mon, SERVER& server, MYSQL** ppCon) +mxs_connect_result_t mon_ping_or_connect_to_db(const MXS_MONITORED_SERVER::ConnectionSettings& sett, + SERVER& server, MYSQL** ppConn) { - if (*ppCon) + mxb_assert(ppConn); + auto pConn = *ppConn; + if (pConn) { /** Return if the connection is OK */ - if (mysql_ping(*ppCon) == 0) + if (mysql_ping(pConn) == 0) { return MONITOR_CONN_EXISTING_OK; } /** Otherwise close the handle. */ - mysql_close(*ppCon); + mysql_close(pConn); } mxs_connect_result_t conn_result = MONITOR_CONN_REFUSED; - if ((*ppCon = mysql_init(NULL))) + if ((pConn = mysql_init(NULL)) != nullptr) { - string uname = mon.user; - string passwd = mon.password; - const Server& s = static_cast(server); // Clean this up later. - string server_specific_monuser = s.monitor_user(); + string uname = sett.username; + string passwd = sett.password; + const Server& srv = static_cast(server); // Clean this up later. + string server_specific_monuser = srv.monitor_user(); if (!server_specific_monuser.empty()) { uname = server_specific_monuser; - passwd = s.monitor_password(); + passwd = srv.monitor_password(); } - char* dpwd = decrypt_password(passwd.c_str()); - mysql_optionsv(*ppCon, MYSQL_OPT_CONNECT_TIMEOUT, (void*) &mon.connect_timeout); - mysql_optionsv(*ppCon, MYSQL_OPT_READ_TIMEOUT, (void*) &mon.read_timeout); - mysql_optionsv(*ppCon, MYSQL_OPT_WRITE_TIMEOUT, (void*) &mon.write_timeout); - mysql_optionsv(*ppCon, MYSQL_PLUGIN_DIR, get_connector_plugindir()); + mysql_optionsv(pConn, MYSQL_OPT_CONNECT_TIMEOUT, &sett.connect_timeout); + mysql_optionsv(pConn, MYSQL_OPT_READ_TIMEOUT, &sett.read_timeout); + mysql_optionsv(pConn, MYSQL_OPT_WRITE_TIMEOUT, &sett.write_timeout); + mysql_optionsv(pConn, MYSQL_PLUGIN_DIR, get_connector_plugindir()); time_t start = 0; time_t end = 0; - for (int i = 0; i < mon.connect_attempts; i++) + for (int i = 0; i < sett.connect_attempts; i++) { start = time(NULL); - bool result = (mxs_mysql_real_connect(*ppCon, &server, uname.c_str(), dpwd) != NULL); + bool result = (mxs_mysql_real_connect(pConn, &server, uname.c_str(), dpwd) != NULL); end = time(NULL); if (result) @@ -1389,23 +1356,20 @@ mxs_connect_result_t mon_ping_or_connect_to_db(const Monitor& mon, SERVER& serve } } - if (conn_result == MONITOR_CONN_REFUSED && (int)difftime(end, start) >= mon.connect_timeout) + if (conn_result == MONITOR_CONN_REFUSED && difftime(end, start) >= sett.connect_timeout) { conn_result = MONITOR_CONN_TIMEOUT; } MXS_FREE(dpwd); } + *ppConn = pConn; return conn_result; } -mxs_connect_result_t mon_ping_or_connect_to_db(Monitor* mon, MXS_MONITORED_SERVER* database) +mxs_connect_result_t MXS_MONITORED_SERVER::ping_or_connect(const ConnectionSettings& settings) { - mxb_assert(mon); - mxb_assert(database); - mxb_assert(database->server); - - return mon_ping_or_connect_to_db(*mon, *database->server, &database->con); + return mon_ping_or_connect_to_db(settings, *server, &con); } /** @@ -2817,7 +2781,7 @@ void MonitorWorkerSimple::tick() pMs->mon_prev_status = pMs->server->status; pMs->pending_status = pMs->server->status; - mxs_connect_result_t rval = mon_ping_or_connect_to_db(m_monitor, pMs); + mxs_connect_result_t rval = pMs->ping_or_connect(m_settings.conn_settings); if (mon_connection_is_ok(rval)) { diff --git a/server/modules/monitor/clustrixmon/clustrix.cc b/server/modules/monitor/clustrixmon/clustrix.cc index 6bfd77099..c011d354f 100644 --- a/server/modules/monitor/clustrixmon/clustrix.cc +++ b/server/modules/monitor/clustrixmon/clustrix.cc @@ -154,11 +154,11 @@ bool Clustrix::is_part_of_the_quorum(const SERVER& server, MYSQL* pCon) return rv; } -bool Clustrix::ping_or_connect_to_hub(const Monitor& mon, SERVER& server, MYSQL** ppCon) +bool Clustrix::ping_or_connect_to_hub(const MXS_MONITORED_SERVER::ConnectionSettings& sett, SERVER& server, + MYSQL** ppCon) { bool connected = false; - - mxs_connect_result_t rv = mon_ping_or_connect_to_db(mon, server, ppCon); + mxs_connect_result_t rv = mon_ping_or_connect_to_db(sett, server, ppCon); if (mon_connection_is_ok(rv)) { diff --git a/server/modules/monitor/clustrixmon/clustrix.hh b/server/modules/monitor/clustrixmon/clustrix.hh index 40e501cf4..b5c6dc001 100644 --- a/server/modules/monitor/clustrixmon/clustrix.hh +++ b/server/modules/monitor/clustrixmon/clustrix.hh @@ -68,7 +68,7 @@ inline bool is_part_of_the_quorum(MXS_MONITORED_SERVER& ms) * Ping or create connection to server and check whether it can be used * as hub. * - * @param mon The monitor. + * @param sett Connection settings * @param server Server object referring to a Clustrix node. * @param ppCon Address of pointer to MYSQL object referring to @server * (@c *ppCon may also be NULL). @@ -77,20 +77,21 @@ inline bool is_part_of_the_quorum(MXS_MONITORED_SERVER& ms) * * @note Upon return @c *ppCon will be non-NULL. */ -bool ping_or_connect_to_hub(const Monitor& mon, SERVER& server, MYSQL** ppCon); +bool ping_or_connect_to_hub(const MXS_MONITORED_SERVER::ConnectionSettings& sett, SERVER& server, + MYSQL** ppCon); /** * Ping or create connection to server and check whether it can be used * as hub. * - * @param mon The monitor. * @param ms Monitored server object referring to a Clustrix node. - * + * @param sett Connection settings * @return True, if the server can be used as hub, false otherwise. */ -inline bool ping_or_connect_to_hub(const Monitor& mon, MXS_MONITORED_SERVER& ms) +inline bool ping_or_connect_to_hub(const MXS_MONITORED_SERVER::ConnectionSettings& sett, + MXS_MONITORED_SERVER& ms) { - return ping_or_connect_to_hub(mon, *ms.server, &ms.con); + return ping_or_connect_to_hub(sett, *ms.server, &ms.con); } } diff --git a/server/modules/monitor/clustrixmon/clustrixmonitor.cc b/server/modules/monitor/clustrixmon/clustrixmonitor.cc index a4c143e65..943ea4a19 100644 --- a/server/modules/monitor/clustrixmon/clustrixmonitor.cc +++ b/server/modules/monitor/clustrixmon/clustrixmonitor.cc @@ -115,7 +115,7 @@ void ClustrixMonitor::choose_hub() auto& element = *it; ClustrixNode& node = element.second; - if (node.can_be_used_as_hub(*this)) + if (node.can_be_used_as_hub(m_settings.conn_settings)) { pHub_con = node.release_connection(); pHub_server = node.server(); @@ -135,7 +135,7 @@ void ClustrixMonitor::choose_hub() if (ips.find(ms.server->address) == ips.end()) { - if (Clustrix::ping_or_connect_to_hub(*this, ms)) + if (Clustrix::ping_or_connect_to_hub(m_settings.conn_settings, ms)) { pHub_con = ms.con; pHub_server = ms.server; @@ -339,7 +339,7 @@ void ClustrixMonitor::check_hub() mxb_assert(m_pHub_con); mxb_assert(m_pHub_server); - if (!Clustrix::ping_or_connect_to_hub(*this, *m_pHub_server, &m_pHub_con)) + if (!Clustrix::ping_or_connect_to_hub(m_settings.conn_settings, *m_pHub_server, &m_pHub_con)) { mysql_close(m_pHub_con); m_pHub_con = nullptr; diff --git a/server/modules/monitor/clustrixmon/clustrixnode.cc b/server/modules/monitor/clustrixmon/clustrixnode.cc index 0027b5f54..2aa687b0b 100644 --- a/server/modules/monitor/clustrixmon/clustrixnode.cc +++ b/server/modules/monitor/clustrixmon/clustrixnode.cc @@ -14,11 +14,10 @@ #include "clustrixnode.hh" #include "clustrix.hh" -bool ClustrixNode::can_be_used_as_hub(const Monitor& mon) +bool ClustrixNode::can_be_used_as_hub(const MXS_MONITORED_SERVER::ConnectionSettings& sett) { mxb_assert(m_pServer); - - bool rv = Clustrix::ping_or_connect_to_hub(mon, *m_pServer, &m_pCon); + bool rv = Clustrix::ping_or_connect_to_hub(sett, *m_pServer, &m_pCon); if (!rv) { diff --git a/server/modules/monitor/clustrixmon/clustrixnode.hh b/server/modules/monitor/clustrixmon/clustrixnode.hh index 47a2a00d5..7f8d1f1ff 100644 --- a/server/modules/monitor/clustrixmon/clustrixnode.hh +++ b/server/modules/monitor/clustrixmon/clustrixnode.hh @@ -160,7 +160,7 @@ public: m_pServer->is_active = false; } - bool can_be_used_as_hub(const Monitor& mon); + bool can_be_used_as_hub(const MXS_MONITORED_SERVER::ConnectionSettings& sett); SERVER* server() const { diff --git a/server/modules/monitor/mariadbmon/mariadbmon.cc b/server/modules/monitor/mariadbmon/mariadbmon.cc index af18e8bfe..95682e820 100644 --- a/server/modules/monitor/mariadbmon/mariadbmon.cc +++ b/server/modules/monitor/mariadbmon/mariadbmon.cc @@ -157,8 +157,8 @@ bool MariaDBMonitor::set_replication_credentials(const MXS_CONFIG_PARAMETER* par if (repl_user.empty() && repl_pw.empty()) { // No replication credentials defined, use monitor credentials - repl_user = m_monitor->user; - repl_pw = m_monitor->password; + repl_user = m_settings.conn_settings.username; + repl_pw = m_settings.conn_settings.password; } if (!repl_user.empty() && !repl_pw.empty()) @@ -345,7 +345,7 @@ json_t* MariaDBMonitor::to_json() const void MariaDBMonitor::update_server(MariaDBServer* server) { MXS_MONITORED_SERVER* mon_srv = server->m_server_base; - mxs_connect_result_t conn_status = mon_ping_or_connect_to_db(m_monitor, mon_srv); + mxs_connect_result_t conn_status = mon_srv->ping_or_connect(m_settings.conn_settings); MYSQL* conn = mon_srv->con; // mon_ping_or_connect_to_db() may have reallocated the MYSQL struct. if (mon_connection_is_ok(conn_status))