MXS-1775 Inherit MariaDBMonitor from mxs::MonitorInstance
Start/stop now provided by MonitorInstance. The thread main function is now virtual and overriden by MariaDBMonitor. Some additional refactoring is necessary in order to be able to allow MonitorInstance to handle the main loop.
This commit is contained in:
@ -103,6 +103,16 @@ protected:
|
||||
const std::string& script() const { return m_script; }
|
||||
uint64_t events() const { return m_events; }
|
||||
|
||||
/**
|
||||
* @brief Should the monitor shut down?
|
||||
*
|
||||
* @return True, if the monitor should shut down, false otherwise.
|
||||
*/
|
||||
bool should_shutdown() const
|
||||
{
|
||||
return atomic_load_int32(&m_shutdown) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure the monitor.
|
||||
*
|
||||
@ -165,6 +175,11 @@ protected:
|
||||
*/
|
||||
virtual void tick();
|
||||
|
||||
/**
|
||||
* TODO: Temporarily virtual so that MariaDBMonitor can override.
|
||||
*/
|
||||
virtual void main();
|
||||
|
||||
MXS_MONITOR* m_monitor; /**< The generic monitor structure. */
|
||||
MXS_MONITORED_SERVER* m_master; /**< Master server */
|
||||
|
||||
@ -177,8 +192,6 @@ private:
|
||||
uint64_t m_events; /**< Enabled monitor events. */
|
||||
Semaphore m_semaphore; /**< Semaphore for synchronizing with monitor thread. */
|
||||
|
||||
void main();
|
||||
|
||||
static void main(void* pArg);
|
||||
};
|
||||
|
||||
|
@ -30,8 +30,6 @@
|
||||
|
||||
using std::string;
|
||||
|
||||
static void monitorMain(void *);
|
||||
|
||||
// Config parameter names
|
||||
const char * const CN_AUTO_FAILOVER = "auto_failover";
|
||||
const char * const CN_PROMOTION_SQL_FILE = "promotion_sql_file";
|
||||
@ -52,10 +50,8 @@ static const char CN_REPLICATION_USER[] = "replication_user";
|
||||
static const char CN_REPLICATION_PASSWORD[] = "replication_password";
|
||||
|
||||
MariaDBMonitor::MariaDBMonitor(MXS_MONITOR* monitor)
|
||||
: m_monitor(monitor)
|
||||
: maxscale::MonitorInstance(monitor)
|
||||
, m_id(config_get_global_options()->id)
|
||||
, m_shutdown(false)
|
||||
, m_state(MXS_MONITOR_STOPPED)
|
||||
, m_master_gtid_domain(GTID_DOMAIN_UNKNOWN)
|
||||
, m_external_master_port(PORT_UNKNOWN)
|
||||
, m_cluster_modified(true)
|
||||
@ -67,11 +63,6 @@ MariaDBMonitor::~MariaDBMonitor()
|
||||
clear_server_info();
|
||||
}
|
||||
|
||||
int32_t MariaDBMonitor::state() const
|
||||
{
|
||||
return m_state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset and initialize server arrays and related data.
|
||||
*/
|
||||
@ -153,30 +144,6 @@ MariaDBMonitor* MariaDBMonitor::create(MXS_MONITOR *monitor)
|
||||
return new MariaDBMonitor(monitor);
|
||||
}
|
||||
|
||||
bool MariaDBMonitor::start(const MXS_CONFIG_PARAMETER* params)
|
||||
{
|
||||
bool error = false;
|
||||
m_shutdown = false;
|
||||
|
||||
if (!configure(params))
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
|
||||
if (!error && (thread_start(&m_thread, monitorMain, this, 0) == NULL))
|
||||
{
|
||||
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", m_monitor->name);
|
||||
error = true;
|
||||
}
|
||||
|
||||
if (error)
|
||||
{
|
||||
MXS_ERROR("Failed to start monitor. See earlier errors for more information.");
|
||||
}
|
||||
|
||||
return !error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load config parameters
|
||||
*
|
||||
@ -231,25 +198,6 @@ bool MariaDBMonitor::configure(const MXS_CONFIG_PARAMETER* params)
|
||||
return settings_ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the monitor.
|
||||
*
|
||||
* @return True, if the monitor had to be stopped. False, if the monitor already was stopped.
|
||||
*/
|
||||
bool MariaDBMonitor::stop()
|
||||
{
|
||||
// There should be no race here as long as admin operations are performed
|
||||
// with the single admin lock locked.
|
||||
bool actually_stopped = false;
|
||||
if (m_state == MXS_MONITOR_RUNNING)
|
||||
{
|
||||
m_shutdown = true;
|
||||
thread_wait(m_thread);
|
||||
actually_stopped = true;
|
||||
}
|
||||
return actually_stopped;
|
||||
}
|
||||
|
||||
void MariaDBMonitor::diagnostics(DCB *dcb) const
|
||||
{
|
||||
dcb_printf(dcb, "Automatic failover: %s\n", m_auto_failover ? "Enabled" : "Disabled");
|
||||
@ -317,19 +265,19 @@ json_t* MariaDBMonitor::diagnostics_json() const
|
||||
return rval;
|
||||
}
|
||||
|
||||
void MariaDBMonitor::update_server_status(MXS_MONITORED_SERVER* monitored_server)
|
||||
{
|
||||
auto i = m_server_info.find(monitored_server);
|
||||
ss_dassert(i != m_server_info.end());
|
||||
|
||||
(*i).second->update_server(m_monitor);
|
||||
}
|
||||
|
||||
void MariaDBMonitor::main()
|
||||
{
|
||||
m_state = MXS_MONITOR_RUNNING;
|
||||
MariaDBServer* root_master = NULL;
|
||||
int log_no_master = 1;
|
||||
|
||||
if (mysql_thread_init())
|
||||
{
|
||||
MXS_ERROR("mysql_thread_init failed in monitor module. Exiting.");
|
||||
m_state = MXS_MONITOR_STOPPED;
|
||||
return;
|
||||
}
|
||||
|
||||
load_journal();
|
||||
|
||||
if (m_detect_replication_lag)
|
||||
@ -347,7 +295,7 @@ void MariaDBMonitor::main()
|
||||
(*iter)->m_server_base->con = NULL;
|
||||
}
|
||||
|
||||
while (!m_shutdown)
|
||||
while (!should_shutdown())
|
||||
{
|
||||
timespec loop_start;
|
||||
/* Coarse time has resolution ~1ms (as opposed to 1ns) but this is enough. */
|
||||
@ -375,7 +323,7 @@ void MariaDBMonitor::main()
|
||||
for (auto iter = m_servers.begin(); iter != m_servers.end(); iter++)
|
||||
{
|
||||
MariaDBServer* server = *iter;
|
||||
server->update_server(m_monitor);
|
||||
update_server_status(server->m_server_base);
|
||||
}
|
||||
|
||||
// Use the information to find the so far best master server.
|
||||
@ -524,7 +472,7 @@ void MariaDBMonitor::main()
|
||||
sleep_time_remaining = MXS_MAX(MXS_MON_BASE_INTERVAL_MS, sleep_time_remaining);
|
||||
/* Sleep in small increments to react faster to some events. This should ideally use some type of
|
||||
* notification mechanism. */
|
||||
while (sleep_time_remaining > 0 && !m_shutdown && !m_monitor->server_pending_changes)
|
||||
while (sleep_time_remaining > 0 && !should_shutdown() && !m_monitor->server_pending_changes)
|
||||
{
|
||||
int small_sleep_ms = (sleep_time_remaining >= MXS_MON_BASE_INTERVAL_MS) ?
|
||||
MXS_MON_BASE_INTERVAL_MS : sleep_time_remaining;
|
||||
@ -532,10 +480,6 @@ void MariaDBMonitor::main()
|
||||
sleep_time_remaining -= small_sleep_ms;
|
||||
}
|
||||
}
|
||||
|
||||
m_state = MXS_MONITOR_STOPPING;
|
||||
mysql_thread_end();
|
||||
m_state = MXS_MONITOR_STOPPED;
|
||||
}
|
||||
|
||||
void MariaDBMonitor::update_gtid_domain()
|
||||
@ -667,17 +611,6 @@ void MariaDBMonitor::handle_auto_rejoin()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The entry point for the monitoring module thread
|
||||
*
|
||||
* @param arg The handle of the monitor. Must be the object returned by startMonitor.
|
||||
*/
|
||||
static void monitorMain(void *arg)
|
||||
{
|
||||
MariaDBMonitor* handle = static_cast<MariaDBMonitor*>(arg);
|
||||
handle->main();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple wrapper for mxs_mysql_query and mysql_num_rows
|
||||
*
|
||||
|
@ -34,7 +34,7 @@ typedef std::tr1::unordered_map<MXS_MONITORED_SERVER*, MariaDBServer*> ServerInf
|
||||
typedef std::vector<MariaDBServer*> ServerArray;
|
||||
|
||||
// MariaDB Monitor instance data
|
||||
class MariaDBMonitor : public MXS_MONITOR_INSTANCE
|
||||
class MariaDBMonitor : public maxscale::MonitorInstance
|
||||
{
|
||||
private:
|
||||
MariaDBMonitor(const MariaDBMonitor&);
|
||||
@ -44,32 +44,6 @@ public:
|
||||
|
||||
~MariaDBMonitor();
|
||||
|
||||
/**
|
||||
* @brief Current state of the monitor.
|
||||
*
|
||||
* Note that in principle the state of the monitor may already have
|
||||
* changed when the current state is returned. The state can be fully
|
||||
* trusted only if it is asked in a context when it is know nobody else
|
||||
* can affect it.
|
||||
*
|
||||
* @return @c MXS_MONITOR_RUNNING if the monitor is running,
|
||||
* @c MXS_MONITOR_STOPPING if the monitor is stopping, and
|
||||
* @c MXS_MONITOR_STOPPED of the monitor is stopped.
|
||||
*/
|
||||
int32_t state() const;
|
||||
|
||||
/**
|
||||
* @brief Find out whether the monitor is running.
|
||||
*
|
||||
* @return True, if the monitor is running, false otherwise.
|
||||
*
|
||||
* @see state().
|
||||
*/
|
||||
bool is_running() const
|
||||
{
|
||||
return state() == MXS_MONITOR_RUNNING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print diagnostics.
|
||||
*
|
||||
@ -84,11 +58,6 @@ public:
|
||||
*/
|
||||
json_t* diagnostics_json() const;
|
||||
|
||||
/**
|
||||
* Runs the main monitor loop. Called from the static monitorMain()-function.
|
||||
*/
|
||||
void main();
|
||||
|
||||
/**
|
||||
* Create the monitor instance and return the instance data.
|
||||
*
|
||||
@ -97,20 +66,6 @@ public:
|
||||
*/
|
||||
static MariaDBMonitor* create(MXS_MONITOR *monitor);
|
||||
|
||||
/**
|
||||
* Start the monitor instance.
|
||||
* This function creates a thread to execute the monitoring.
|
||||
*
|
||||
* @param params Configuration parameters
|
||||
* @return True, if the monitor could be started, false otherwise.
|
||||
*/
|
||||
bool start(const MXS_CONFIG_PARAMETER* params);
|
||||
|
||||
/**
|
||||
* Stop the monitor. Waits until monitor has stopped.
|
||||
*/
|
||||
bool stop();
|
||||
|
||||
/**
|
||||
* Handle switchover
|
||||
*
|
||||
@ -139,12 +94,12 @@ public:
|
||||
*/
|
||||
bool manual_rejoin(SERVER* rejoin_server, json_t** output);
|
||||
|
||||
protected:
|
||||
void update_server_status(MXS_MONITORED_SERVER* pMonitored_server);
|
||||
void main();
|
||||
|
||||
private:
|
||||
MXS_MONITOR* m_monitor; /**< Generic monitor object */
|
||||
THREAD m_thread; /**< Monitor thread */
|
||||
unsigned long m_id; /**< Monitor ID */
|
||||
volatile bool m_shutdown; /**< Should the monitor shut down? */
|
||||
volatile int m_state; /**< Monitor state */
|
||||
ServerArray m_servers; /**< Servers of the monitor */
|
||||
ServerInfoMap m_server_info; /**< Map from server base struct to MariaDBServer */
|
||||
|
||||
|
Reference in New Issue
Block a user