MXS-1703: Turn MariaDB Monitor struct to class with public fields

Allows using std::string for strings. Also, cleanup.
This commit is contained in:
Esa Korhonen
2018-02-19 18:24:40 +02:00
parent 1de8f5ffdc
commit 8ef1385d1a
3 changed files with 61 additions and 65 deletions

View File

@ -1773,7 +1773,7 @@ void mon_process_state_changes(MXS_MONITOR *monitor, const char *script, uint64_
master_up = true; master_up = true;
} }
if (script && (events & event)) if (script && *script && (events & event))
{ {
monitor_launch_script(monitor, ptr, script, monitor->script_timeout); monitor_launch_script(monitor, ptr, script, monitor->script_timeout);
} }

View File

@ -31,6 +31,7 @@
#include <maxscale/modulecmd.h> #include <maxscale/modulecmd.h>
#include <maxscale/modutil.h> #include <maxscale/modutil.h>
#include <maxscale/mysql_utils.h> #include <maxscale/mysql_utils.h>
#include <maxscale/secrets.h>
#include <maxscale/utils.h> #include <maxscale/utils.h>
// TODO: For monitorAddParameters // TODO: For monitorAddParameters
#include "../../../core/internal/monitor.h" #include "../../../core/internal/monitor.h"
@ -989,8 +990,10 @@ static bool set_replication_credentials(MYSQL_MONITOR *handle, const MXS_CONFIG_
if (*repl_user && *repl_pw) if (*repl_user && *repl_pw)
{ {
handle->replication_user = MXS_STRDUP_A(repl_user); handle->replication_user = repl_user;
handle->replication_password = decrypt_password(repl_pw); char* decrypted = decrypt_password(repl_pw);
handle->replication_password = decrypted;
MXS_FREE(decrypted);
rval = true; rval = true;
} }
@ -1033,22 +1036,22 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
if (handle) if (handle)
{ {
handle->shutdown = 0; handle->shutdown = 0;
MXS_FREE(handle->script); handle->script.clear();
MXS_FREE(handle->replication_user); handle->replication_user.clear();
MXS_FREE(handle->replication_password); handle->replication_password.clear();
MXS_FREE(handle->excluded_servers); MXS_FREE(handle->excluded_servers);
handle->excluded_servers = NULL; handle->excluded_servers = NULL;
handle->n_excluded = 0; handle->n_excluded = 0;
} }
else else
{ {
handle = (MYSQL_MONITOR *) MXS_MALLOC(sizeof(MYSQL_MONITOR)); handle = new MYSQL_MONITOR;
HASHTABLE *server_info = hashtable_alloc(MAX_NUM_SLAVES, HASHTABLE *server_info = hashtable_alloc(MAX_NUM_SLAVES,
hashtable_item_strhash, hashtable_item_strcmp); hashtable_item_strhash, hashtable_item_strcmp);
if (handle == NULL || server_info == NULL) if (server_info == NULL)
{ {
MXS_FREE(handle); delete handle;
hashtable_free(server_info); hashtable_free(server_info);
return NULL; return NULL;
} }
@ -1060,7 +1063,6 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
handle->id = config_get_global_options()->id; handle->id = config_get_global_options()->id;
handle->warn_set_standalone_master = true; handle->warn_set_standalone_master = true;
handle->master_gtid_domain = -1; handle->master_gtid_domain = -1;
handle->external_master_host[0] = '\0';
handle->external_master_port = PORT_UNKNOWN; handle->external_master_port = PORT_UNKNOWN;
handle->monitor = monitor; handle->monitor = monitor;
} }
@ -1077,15 +1079,14 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
handle->failcount = config_get_integer(params, CN_FAILCOUNT); handle->failcount = config_get_integer(params, CN_FAILCOUNT);
handle->allow_cluster_recovery = config_get_bool(params, "allow_cluster_recovery"); handle->allow_cluster_recovery = config_get_bool(params, "allow_cluster_recovery");
handle->mysql51_replication = config_get_bool(params, "mysql51_replication"); handle->mysql51_replication = config_get_bool(params, "mysql51_replication");
handle->script = config_copy_string(params, "script"); handle->script = config_get_string(params, "script");
handle->events = config_get_enum(params, "events", mxs_monitor_event_enum_values); handle->events = config_get_enum(params, "events", mxs_monitor_event_enum_values);
handle->auto_failover = config_get_bool(params, CN_AUTO_FAILOVER);
handle->failover_timeout = config_get_integer(params, CN_FAILOVER_TIMEOUT); handle->failover_timeout = config_get_integer(params, CN_FAILOVER_TIMEOUT);
handle->switchover_timeout = config_get_integer(params, CN_SWITCHOVER_TIMEOUT); handle->switchover_timeout = config_get_integer(params, CN_SWITCHOVER_TIMEOUT);
handle->auto_failover = config_get_bool(params, CN_AUTO_FAILOVER);
handle->auto_rejoin = config_get_bool(params, CN_AUTO_REJOIN);
handle->verify_master_failure = config_get_bool(params, CN_VERIFY_MASTER_FAILURE); handle->verify_master_failure = config_get_bool(params, CN_VERIFY_MASTER_FAILURE);
handle->master_failure_timeout = config_get_integer(params, CN_MASTER_FAILURE_TIMEOUT); handle->master_failure_timeout = config_get_integer(params, CN_MASTER_FAILURE_TIMEOUT);
handle->auto_rejoin = config_get_bool(params, CN_AUTO_REJOIN);
handle->excluded_servers = NULL; handle->excluded_servers = NULL;
handle->n_excluded = mon_config_get_servers(params, CN_NO_PROMOTE_SERVERS, monitor, handle->n_excluded = mon_config_get_servers(params, CN_NO_PROMOTE_SERVERS, monitor,
&handle->excluded_servers); &handle->excluded_servers);
@ -1114,9 +1115,8 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
if (error) if (error)
{ {
hashtable_free(handle->server_info); hashtable_free(handle->server_info);
MXS_FREE(handle->script);
MXS_FREE(handle->excluded_servers); MXS_FREE(handle->excluded_servers);
MXS_FREE(handle); delete handle;
handle = NULL; handle = NULL;
} }
else else
@ -1127,8 +1127,7 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
{ {
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name); MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name);
hashtable_free(handle->server_info); hashtable_free(handle->server_info);
MXS_FREE(handle->script); delete handle;
MXS_FREE(handle);
handle = NULL; handle = NULL;
} }
} }
@ -1207,8 +1206,7 @@ static void diagnostics(DCB *dcb, const MXS_MONITOR *mon)
dcb_printf(dcb, "Switchover timeout: %u\n", handle->switchover_timeout); dcb_printf(dcb, "Switchover timeout: %u\n", handle->switchover_timeout);
dcb_printf(dcb, "Automatic rejoin: %s\n", handle->auto_rejoin ? "Enabled" : "Disabled"); dcb_printf(dcb, "Automatic rejoin: %s\n", handle->auto_rejoin ? "Enabled" : "Disabled");
dcb_printf(dcb, "MaxScale monitor ID: %lu\n", handle->id); dcb_printf(dcb, "MaxScale monitor ID: %lu\n", handle->id);
dcb_printf(dcb, "Detect replication lag: %s\n", (handle->replicationHeartbeat == 1) ? dcb_printf(dcb, "Detect replication lag: %s\n", (handle->replicationHeartbeat) ? "Enabled" : "Disabled");
"Enabled" : "Disabled");
dcb_printf(dcb, "Detect stale master: %s\n", (handle->detectStaleMaster == 1) ? dcb_printf(dcb, "Detect stale master: %s\n", (handle->detectStaleMaster == 1) ?
"Enabled" : "Disabled"); "Enabled" : "Disabled");
if (handle->n_excluded > 0) if (handle->n_excluded > 0)
@ -1282,9 +1280,9 @@ static json_t* diagnostics_json(const MXS_MONITOR *mon)
json_object_set_new(rval, CN_SWITCHOVER_TIMEOUT, json_integer(handle->switchover_timeout)); json_object_set_new(rval, CN_SWITCHOVER_TIMEOUT, json_integer(handle->switchover_timeout));
json_object_set_new(rval, CN_AUTO_REJOIN, json_boolean(handle->auto_rejoin)); json_object_set_new(rval, CN_AUTO_REJOIN, json_boolean(handle->auto_rejoin));
if (handle->script) if (!handle->script.empty())
{ {
json_object_set_new(rval, "script", json_string(handle->script)); json_object_set_new(rval, "script", json_string(handle->script.c_str()));
} }
if (handle->n_excluded > 0) if (handle->n_excluded > 0)
{ {
@ -2112,7 +2110,7 @@ monitorMain(void *arg)
MYSQL_MONITOR *handle = (MYSQL_MONITOR *) arg; MYSQL_MONITOR *handle = (MYSQL_MONITOR *) arg;
MXS_MONITOR* mon = handle->monitor; MXS_MONITOR* mon = handle->monitor;
MXS_MONITORED_SERVER *ptr; MXS_MONITORED_SERVER *ptr;
int replication_heartbeat; bool replication_heartbeat;
bool detect_stale_master; bool detect_stale_master;
int num_servers = 0; int num_servers = 0;
MXS_MONITORED_SERVER *root_master = NULL; MXS_MONITORED_SERVER *root_master = NULL;
@ -2285,21 +2283,20 @@ monitorMain(void *arg)
if (master_info->slave_status.master_host != handle->external_master_host || if (master_info->slave_status.master_host != handle->external_master_host ||
master_info->slave_status.master_port != handle->external_master_port) master_info->slave_status.master_port != handle->external_master_port)
{ {
const char* new_ext_host = master_info->slave_status.master_host.c_str(); const string new_ext_host = master_info->slave_status.master_host;
const int new_ext_port = master_info->slave_status.master_port; const int new_ext_port = master_info->slave_status.master_port;
if (handle->external_master_port == PORT_UNKNOWN) if (handle->external_master_port == PORT_UNKNOWN)
{ {
MXS_NOTICE("Cluster master server is replicating from an external master: %s:%d", MXS_NOTICE("Cluster master server is replicating from an external master: %s:%d",
new_ext_host, new_ext_port); new_ext_host.c_str(), new_ext_port);
} }
else else
{ {
MXS_NOTICE("The external master of the cluster has changed: %s:%d -> %s:%d.", MXS_NOTICE("The external master of the cluster has changed: %s:%d -> %s:%d.",
handle->external_master_host, handle->external_master_port, handle->external_master_host.c_str(), handle->external_master_port,
new_ext_host, new_ext_port); new_ext_host.c_str(), new_ext_port);
} }
snprintf(handle->external_master_host, sizeof(handle->external_master_host), handle->external_master_host = new_ext_host;
"%s", new_ext_host);
handle->external_master_port = new_ext_port; handle->external_master_port = new_ext_port;
} }
} }
@ -2309,7 +2306,7 @@ monitorMain(void *arg)
{ {
MXS_NOTICE("Cluster lost the external master."); MXS_NOTICE("Cluster lost the external master.");
} }
handle->external_master_host[0] = '\0'; handle->external_master_host.clear();
handle->external_master_port = PORT_UNKNOWN; handle->external_master_port = PORT_UNKNOWN;
} }
} }
@ -2470,7 +2467,7 @@ monitorMain(void *arg)
* After updating the status of all servers, check if monitor events * After updating the status of all servers, check if monitor events
* need to be launched. * need to be launched.
*/ */
mon_process_state_changes(mon, handle->script, handle->events); mon_process_state_changes(mon, handle->script.c_str(), handle->events);
bool failover_performed = false; // Has an automatic failover been performed this loop? bool failover_performed = false; // Has an automatic failover been performed this loop?
if (handle->auto_failover) if (handle->auto_failover)
@ -3669,7 +3666,7 @@ bool start_external_replication(MYSQL_MONITOR* mon, MXS_MONITORED_SERVER* new_ma
mxs_mysql_query(new_master->con, "START SLAVE;") == 0) mxs_mysql_query(new_master->con, "START SLAVE;") == 0)
{ {
MXS_NOTICE("New master starting replication from external master %s:%d.", MXS_NOTICE("New master starting replication from external master %s:%d.",
mon->external_master_host, mon->external_master_port); mon->external_master_host.c_str(), mon->external_master_port);
rval = true; rval = true;
} }
else else

View File

@ -14,61 +14,60 @@
*/ */
#include <maxscale/cppdefs.hh> #include <maxscale/cppdefs.hh>
#include <stdio.h> #include <string>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <mysqld_error.h>
#include <maxscale/config.h> #include <maxscale/config.h>
#include <maxscale/dcb.h> #include <maxscale/dcb.h>
#include <maxscale/hashtable.h> #include <maxscale/hashtable.h>
#include <maxscale/log_manager.h>
#include <maxscale/modinfo.h>
#include <maxscale/monitor.h> #include <maxscale/monitor.h>
#include <maxscale/spinlock.h>
#include <maxscale/secrets.h>
#include <maxscale/thread.h> #include <maxscale/thread.h>
using std::string;
// MariaDB Monitor instance data // MariaDB Monitor instance data
struct MYSQL_MONITOR class MYSQL_MONITOR
{ {
public:
MXS_MONITOR* monitor; /**< Generic monitor object */
THREAD thread; /**< Monitor thread */ THREAD thread; /**< Monitor thread */
int shutdown; /**< Flag to shutdown the monitor thread */ int shutdown; /**< Flag to shutdown the monitor thread */
int status; /**< Monitor status */ int status; /**< Monitor status */
MXS_MONITORED_SERVER *master; /**< Master server for MySQL Master/Slave replication */
HASHTABLE *server_info; /**< Contains server specific information */
bool warn_set_standalone_master; /**< Log a warning when setting standalone master */
unsigned long id; /**< Monitor ID */ unsigned long id; /**< Monitor ID */
int replicationHeartbeat; /**< Monitor flag for MySQL replication heartbeat */
// Values updated by monitor
int64_t master_gtid_domain; /**< Gtid domain currently used by the master */
string external_master_host; /**< External master host, for fail/switchover */
int external_master_port; /**< External master port */
// Replication topology detection settings
bool mysql51_replication; /**< Use MySQL 5.1 replication */
bool detectStaleMaster; /**< Monitor flag for MySQL replication Stale Master detection */ bool detectStaleMaster; /**< Monitor flag for MySQL replication Stale Master detection */
bool detectStaleSlave; /**< Monitor flag for MySQL replication Stale Master detection */ bool detectStaleSlave; /**< Monitor flag for MySQL replication Stale Master detection */
bool multimaster; /**< Detect and handle multi-master topologies */ bool multimaster; /**< Detect and handle multi-master topologies */
bool ignore_external_masters; /**< Ignore masters outside of the monitor configuration */ bool ignore_external_masters; /**< Ignore masters outside of the monitor configuration */
int disableMasterFailback; /**< Monitor flag for Galera Cluster Master failback */
int availableWhenDonor; /**< Monitor flag for Galera Cluster Donor availability */
int disableMasterRoleSetting; /**< Monitor flag to disable setting master role */
bool mysql51_replication; /**< Use MySQL 5.1 replication */
MXS_MONITORED_SERVER *master; /**< Master server for MySQL Master/Slave replication */
char* script; /**< Script to call when state changes occur on servers */
uint64_t events; /**< enabled events */
HASHTABLE *server_info; /**< Contains server specific information */
bool detect_standalone_master; /**< If standalone master are detected */ bool detect_standalone_master; /**< If standalone master are detected */
int failcount; /**< How many monitoring cycles servers must be bool replicationHeartbeat; /**< Monitor flag for MySQL replication heartbeat */
down before failover is initiated */
bool allow_cluster_recovery; /**< Allow failed servers to rejoin the cluster */ // Failover, switchover and rejoin settings
bool warn_set_standalone_master; /**< Log a warning when setting standalone master */ string replication_user; /**< Replication user for CHANGE MASTER TO-commands */
bool auto_failover; /**< If automatic master failover is enabled */ string replication_password; /**< Replication password for CHANGE MASTER TO-commands */
int failcount; /**< How many monitoring cycles master must be down before auto-failover
* begins */
uint32_t failover_timeout; /**< Timeout in seconds for the master failover */ uint32_t failover_timeout; /**< Timeout in seconds for the master failover */
uint32_t switchover_timeout; /**< Timeout in seconds for the master switchover */ uint32_t switchover_timeout; /**< Timeout in seconds for the master switchover */
char* replication_user; /**< Replication user for failover */
char* replication_password; /**< Replication password for failover*/
bool verify_master_failure; /**< Whether master failure is verified via slaves */ bool verify_master_failure; /**< Whether master failure is verified via slaves */
int master_failure_timeout; /**< Time in seconds to wait before doing failover */ int master_failure_timeout; /**< Master failure verification (via slaves) time in seconds */
int64_t master_gtid_domain; /**< Gtid domain currently used by the master */ bool auto_failover; /**< If automatic master failover is enabled */
char external_master_host[MAX_SERVER_ADDRESS_LEN]; /**< External master host, for fail/switchover */
int external_master_port; /**< External master port */
bool auto_rejoin; /**< Attempt to start slave replication on standalone servers or servers bool auto_rejoin; /**< Attempt to start slave replication on standalone servers or servers
replicating from the wrong master. */ * replicating from the wrong master automatically. */
int n_excluded; /**< Number of excluded servers */
MXS_MONITORED_SERVER** excluded_servers; /**< Servers banned for master promotion during auto-failover. */ MXS_MONITORED_SERVER** excluded_servers; /**< Servers banned for master promotion during auto-failover. */
int n_excluded; /**< Number of excluded servers */
MXS_MONITOR* monitor; // Other settings
string script; /**< Script to call when state changes occur on servers */
uint64_t events; /**< enabled events */
bool allow_cluster_recovery; /**< Allow failed servers to rejoin the cluster */
}; };