MXS-1703: refactor startup & shutdown

Startup now done in a static method. Constructor initializes some values.
Config parameters loaded in a separate method. Some things still need
looking.
This commit is contained in:
Esa Korhonen
2018-02-21 17:08:40 +02:00
parent 7a505d9976
commit 87911dc6b0
2 changed files with 120 additions and 83 deletions

View File

@ -142,7 +142,12 @@ const int64_t SERVER_ID_UNKNOWN = -1;
/** Default port */ /** Default port */
const int PORT_UNKNOWN = 0; const int PORT_UNKNOWN = 0;
MariaDBMonitor::MariaDBMonitor() MariaDBMonitor::MariaDBMonitor(MXS_MONITOR* monitor_base)
: monitor(monitor_base)
, id(config_get_global_options()->id)
, warn_set_standalone_master(true)
, master_gtid_domain(-1)
, external_master_port(PORT_UNKNOWN)
{} {}
MariaDBMonitor::~MariaDBMonitor() MariaDBMonitor::~MariaDBMonitor()
@ -824,112 +829,125 @@ static bool server_is_excluded(const MariaDBMonitor *handle, const MXS_MONITORED
return false; return false;
} }
/** MariaDBMonitor* MariaDBMonitor::start_monitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
* Start the instance of the monitor, returning a handle on the monitor.
*
* This function creates a thread to execute the actual monitoring.
*
* @param arg The current handle - NULL if first start
* @param opt Configuration parameters
* @return A handle to use when interacting with the monitor
*/
static void *
startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
{ {
bool error = false; bool error = false;
MariaDBMonitor *handle = (MariaDBMonitor*) monitor->handle; MariaDBMonitor *handle = static_cast<MariaDBMonitor*>(monitor->handle);
if (handle) if (handle == NULL)
{ {
handle->shutdown = 0; handle = new MariaDBMonitor(monitor);
handle->script.clear();
handle->excluded_servers.clear();
}
else
{
handle = new MariaDBMonitor;
handle->shutdown = 0;
handle->id = config_get_global_options()->id;
handle->warn_set_standalone_master = true;
handle->master_gtid_domain = -1;
handle->external_master_port = PORT_UNKNOWN;
handle->monitor = monitor;
} }
/** This should always be reset to NULL */ /** Always reset these values */
handle->master = NULL; handle->shutdown = 0;
handle->master = NULL; // TODO: Is this correct? May not be ok to delete this info every time monitor
// restarts.
init_server_info(handle); // The above question also applies to this.
handle->detectStaleMaster = config_get_bool(params, "detect_stale_master"); if (!handle->load_config_params(params))
handle->detectStaleSlave = config_get_bool(params, "detect_stale_slave");
handle->replicationHeartbeat = config_get_bool(params, "detect_replication_lag");
handle->multimaster = config_get_bool(params, "multimaster");
handle->ignore_external_masters = config_get_bool(params, "ignore_external_masters");
handle->detect_standalone_master = config_get_bool(params, "detect_standalone_master");
handle->failcount = config_get_integer(params, CN_FAILCOUNT);
handle->allow_cluster_recovery = config_get_bool(params, "allow_cluster_recovery");
handle->mysql51_replication = config_get_bool(params, "mysql51_replication");
handle->script = config_get_string(params, "script");
handle->events = config_get_enum(params, "events", mxs_monitor_event_enum_values);
handle->failover_timeout = config_get_integer(params, CN_FAILOVER_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->master_failure_timeout = config_get_integer(params, CN_MASTER_FAILURE_TIMEOUT);
MXS_MONITORED_SERVER** excluded_array = NULL;
int n_excluded = mon_config_get_servers(params, CN_NO_PROMOTE_SERVERS, monitor, &excluded_array);
for (int i = 0; i < n_excluded; i++)
{ {
handle->excluded_servers.push_back(excluded_array[i]);
}
MXS_FREE(excluded_array);
if (!handle->set_replication_credentials(params))
{
MXS_ERROR("Both '%s' and '%s' must be defined", CN_REPLICATION_USER, CN_REPLICATION_PASSWORD);
error = true; error = true;
} }
if (!check_monitor_permissions(monitor, "SHOW SLAVE STATUS")) if (!check_monitor_permissions(monitor, "SHOW SLAVE STATUS"))
{ {
MXS_ERROR("Failed to start monitor. See earlier errors for more information.");
error = true; error = true;
} }
init_server_info(handle); if (!error)
if (error)
{ {
delete handle; if (thread_start(&handle->m_thread, monitorMain, handle, 0) == NULL)
handle = NULL;
}
else
{
handle->status = MXS_MONITOR_RUNNING;
if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL)
{ {
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name); MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name);
delete handle; error = true;
handle = NULL; }
else
{
handle->status = MXS_MONITOR_RUNNING;
} }
} }
if (error)
{
MXS_ERROR("Failed to start monitor. See earlier errors for more information.");
delete handle;
handle = NULL;
}
return handle; return handle;
} }
/**
* Load config parameters
*
* @param params Config parameters
* @return True if settings are ok
*/
bool MariaDBMonitor::load_config_params(const MXS_CONFIG_PARAMETER* params)
{
detectStaleMaster = config_get_bool(params, "detect_stale_master");
detectStaleSlave = config_get_bool(params, "detect_stale_slave");
replicationHeartbeat = config_get_bool(params, "detect_replication_lag");
multimaster = config_get_bool(params, "multimaster");
ignore_external_masters = config_get_bool(params, "ignore_external_masters");
detect_standalone_master = config_get_bool(params, "detect_standalone_master");
failcount = config_get_integer(params, CN_FAILCOUNT);
allow_cluster_recovery = config_get_bool(params, "allow_cluster_recovery");
mysql51_replication = config_get_bool(params, "mysql51_replication");
script = config_get_string(params, "script");
events = config_get_enum(params, "events", mxs_monitor_event_enum_values);
failover_timeout = config_get_integer(params, CN_FAILOVER_TIMEOUT);
switchover_timeout = config_get_integer(params, CN_SWITCHOVER_TIMEOUT);
auto_failover = config_get_bool(params, CN_AUTO_FAILOVER);
auto_rejoin = config_get_bool(params, CN_AUTO_REJOIN);
verify_master_failure = config_get_bool(params, CN_VERIFY_MASTER_FAILURE);
master_failure_timeout = config_get_integer(params, CN_MASTER_FAILURE_TIMEOUT);
excluded_servers.clear();
MXS_MONITORED_SERVER** excluded_array = NULL;
int n_excluded = mon_config_get_servers(params, CN_NO_PROMOTE_SERVERS, monitor, &excluded_array);
for (int i = 0; i < n_excluded; i++)
{
excluded_servers.push_back(excluded_array[i]);
}
MXS_FREE(excluded_array);
bool settings_ok = true;
if (!set_replication_credentials(params))
{
MXS_ERROR("Both '%s' and '%s' must be defined", CN_REPLICATION_USER, CN_REPLICATION_PASSWORD);
settings_ok = false;
}
return settings_ok;
}
/**
* Start the monitor instance and return the instance data. This function creates a thread to
* execute the monitoring. Use stopMonitor() to stop the thread.
*
* @param monitor General monitor data
* @param params Configuration parameters
* @return A pointer to MariaDBMonitor specific data. Should be stored in MXS_MONITOR's "handle"-field.
*/
static void* startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
{
return MariaDBMonitor::start_monitor(monitor, params);
}
void MariaDBMonitor::stop_monitor()
{
shutdown = 1;
thread_wait(m_thread);
}
/** /**
* Stop a running monitor * Stop a running monitor
* *
* @param mon The monitor that should be stopped. * @param mon The monitor that should be stopped.
*/ */
static void static void stopMonitor(MXS_MONITOR *mon)
stopMonitor(MXS_MONITOR *mon)
{ {
MariaDBMonitor *handle = (MariaDBMonitor *) mon->handle; MariaDBMonitor *handle = static_cast<MariaDBMonitor*>(mon->handle);
handle->stop_monitor();
handle->shutdown = 1;
thread_wait(handle->thread);
} }
/** /**

View File

@ -51,8 +51,21 @@ private:
MariaDBMonitor(const MariaDBMonitor&); MariaDBMonitor(const MariaDBMonitor&);
MariaDBMonitor& operator = (const MariaDBMonitor&); MariaDBMonitor& operator = (const MariaDBMonitor&);
public: public:
MariaDBMonitor();
~MariaDBMonitor(); /**
* Start the monitor instance and return the instance data, creating it if starting for the first time.
* This function creates a thread to execute the monitoring.
*
* @param monitor General monitor data
* @param params Configuration parameters
* @return A pointer to MariaDBMonitor specific data.
*/
static MariaDBMonitor* start_monitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params);
/**
* Stop the monitor. Waits until monitor has stopped.
*/
void stop_monitor();
/** /**
* (Re)join given servers to the cluster. The servers in the array are assumed to be joinable. * (Re)join given servers to the cluster. The servers in the array are assumed to be joinable.
@ -71,13 +84,13 @@ public:
bool cluster_can_be_joined(); bool cluster_can_be_joined();
MXS_MONITOR* monitor; /**< Generic monitor object */ MXS_MONITOR* monitor; /**< Generic monitor object */
THREAD thread; /**< Monitor thread */ volatile int shutdown; /**< Flag to shutdown the monitor thread.
int shutdown; /**< Flag to shutdown the monitor thread */ * Accessed from multiple threads. */
int status; /**< Monitor status */ int status; /**< Monitor status. TODO: This should be in MXS_MONITOR */
MXS_MONITORED_SERVER *master; /**< Master server for MySQL Master/Slave replication */ MXS_MONITORED_SERVER *master; /**< Master server for MySQL Master/Slave replication */
ServerInfoMap server_info; /**< Contains server specific information */ ServerInfoMap 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 */
bool warn_set_standalone_master; /**< Log a warning when setting standalone master */
// Values updated by monitor // Values updated by monitor
int64_t master_gtid_domain; /**< Gtid domain currently used by the master */ int64_t master_gtid_domain; /**< Gtid domain currently used by the master */
@ -111,10 +124,16 @@ public:
bool allow_cluster_recovery; /**< Allow failed servers to rejoin the cluster */ bool allow_cluster_recovery; /**< Allow failed servers to rejoin the cluster */
private: private:
THREAD m_thread; /**< Monitor thread */
// Failover, switchover and rejoin settings // Failover, switchover and rejoin settings
string m_replication_user; /**< Replication user for CHANGE MASTER TO-commands */ string m_replication_user; /**< Replication user for CHANGE MASTER TO-commands */
string m_replication_password; /**< Replication password for CHANGE MASTER TO-commands */ string m_replication_password; /**< Replication password for CHANGE MASTER TO-commands */
MariaDBMonitor(MXS_MONITOR* monitor_base);
~MariaDBMonitor();
bool load_config_params(const MXS_CONFIG_PARAMETER* params);
public: public:
// Following methods should be private, change it once refactoring is done. // Following methods should be private, change it once refactoring is done.
string generate_change_master_cmd(const string& master_host, int master_port); string generate_change_master_cmd(const string& master_host, int master_port);