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:
@ -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,98 +829,114 @@ 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 = new MariaDBMonitor(monitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Always reset these values */
|
||||||
handle->shutdown = 0;
|
handle->shutdown = 0;
|
||||||
handle->script.clear();
|
handle->master = NULL; // TODO: Is this correct? May not be ok to delete this info every time monitor
|
||||||
handle->excluded_servers.clear();
|
// restarts.
|
||||||
}
|
init_server_info(handle); // The above question also applies to this.
|
||||||
else
|
|
||||||
|
if (!handle->load_config_params(params))
|
||||||
{
|
{
|
||||||
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 */
|
|
||||||
handle->master = NULL;
|
|
||||||
|
|
||||||
handle->detectStaleMaster = config_get_bool(params, "detect_stale_master");
|
|
||||||
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;
|
{
|
||||||
|
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name);
|
||||||
|
error = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
handle->status = MXS_MONITOR_RUNNING;
|
handle->status = MXS_MONITOR_RUNNING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL)
|
if (error)
|
||||||
{
|
{
|
||||||
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name);
|
MXS_ERROR("Failed to start monitor. See earlier errors for more information.");
|
||||||
delete handle;
|
delete handle;
|
||||||
handle = NULL;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -923,13 +944,10 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
|
|||||||
*
|
*
|
||||||
* @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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
|
Reference in New Issue
Block a user