MXS-1775 Turn Aurora monitor into a class.

A mechanical change, without any functional modifications.
This commit is contained in:
Johan Wikman
2018-05-15 15:50:49 +03:00
parent d5871e48ae
commit cc7a154e7d

View File

@ -17,24 +17,71 @@
#define MXS_MODULE_NAME "auroramon" #define MXS_MODULE_NAME "auroramon"
#include <maxscale/modinfo.h> #include <maxscale/cppdefs.hh>
#include <maxscale/thread.h>
#include <maxscale/monitor.h>
#include <mysqld_error.h> #include <mysqld_error.h>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/debug.h> #include <maxscale/debug.h>
#include <maxscale/modinfo.h>
#include <maxscale/monitor.hh>
#include <maxscale/mysql_utils.h> #include <maxscale/mysql_utils.h>
#include <maxscale/thread.h>
struct AURORA_MONITOR : public MXS_MONITOR_INSTANCE class AuroraMonitor : public MXS_MONITOR_INSTANCE
{ {
bool shutdown; /**< True if the monitor is stopped */ public:
THREAD thread; /**< Monitor thread */ AuroraMonitor(const AuroraMonitor&) = delete;
char* script; /**< Launchable script */ AuroraMonitor& operator = (const AuroraMonitor&) = delete;
uint64_t events; /**< Enabled monitor events */
MXS_MONITOR* monitor; /**< Pointer to generic monitor structure */ static AuroraMonitor* create(MXS_MONITOR* monitor);
bool checked; /**< Whether server access has been checked */ void destroy();
bool start(const MXS_CONFIG_PARAMETER* param);
void stop();
void diagnostics(DCB* dcb) const;
json_t* diagnostics_json() const;
private:
bool m_shutdown; /**< True if the monitor is stopped */
THREAD m_thread; /**< Monitor thread */
char* m_script; /**< Launchable script */
uint64_t m_events; /**< Enabled monitor events */
MXS_MONITOR* m_monitor; /**< Pointer to generic monitor structure */
bool m_checked; /**< Whether server access has been checked */
AuroraMonitor(MXS_MONITOR* monitor);
~AuroraMonitor();
void main();
static void main(void* data);
}; };
AuroraMonitor::AuroraMonitor(MXS_MONITOR* monitor)
: m_shutdown(false)
, m_thread(0)
, m_script(NULL)
, m_events(0)
, m_monitor(monitor)
, m_checked(false)
{
}
AuroraMonitor::~AuroraMonitor()
{
ss_dassert(!m_thread);
ss_dassert(!m_script);
}
//static
AuroraMonitor* AuroraMonitor::create(MXS_MONITOR* monitor)
{
return new AuroraMonitor(monitor);
}
void AuroraMonitor::destroy()
{
delete this;
}
/** /**
* @brief Update the status of a server * @brief Update the status of a server
* *
@ -109,28 +156,30 @@ void update_server_status(MXS_MONITOR *monitor, MXS_MONITORED_SERVER *database)
* *
* @param arg The MONITOR object for this monitor * @param arg The MONITOR object for this monitor
*/ */
static void //static
monitorMain(void *arg) void AuroraMonitor::main(void* data)
{ {
AURORA_MONITOR *handle = (AURORA_MONITOR*)arg; static_cast<AuroraMonitor*>(data)->main();
MXS_MONITOR *monitor = handle->monitor; }
void AuroraMonitor::main()
{
if (mysql_thread_init()) if (mysql_thread_init())
{ {
MXS_ERROR("mysql_thread_init failed in Aurora monitor. Exiting."); MXS_ERROR("mysql_thread_init failed in Aurora monitor. Exiting.");
return; return;
} }
load_server_journal(monitor, NULL); load_server_journal(m_monitor, NULL);
while (!handle->shutdown) while (!m_shutdown)
{ {
lock_monitor_servers(monitor); lock_monitor_servers(m_monitor);
servers_status_pending_to_current(monitor); servers_status_pending_to_current(m_monitor);
for (MXS_MONITORED_SERVER *ptr = monitor->monitored_servers; ptr; ptr = ptr->next) for (MXS_MONITORED_SERVER *ptr = m_monitor->monitored_servers; ptr; ptr = ptr->next)
{ {
update_server_status(monitor, ptr); update_server_status(m_monitor, ptr);
if (SERVER_IS_DOWN(ptr->server)) if (SERVER_IS_DOWN(ptr->server))
{ {
@ -143,17 +192,17 @@ 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(monitor, handle->script, handle->events); mon_process_state_changes(m_monitor, m_script, m_events);
servers_status_current_to_pending(monitor); servers_status_current_to_pending(m_monitor);
store_server_journal(monitor, NULL); store_server_journal(m_monitor, NULL);
release_monitor_servers(monitor); release_monitor_servers(m_monitor);
/** Sleep until the next monitoring interval */ /** Sleep until the next monitoring interval */
unsigned int ms = 0; unsigned int ms = 0;
while (ms < monitor->interval && !handle->shutdown) while (ms < m_monitor->interval && !m_shutdown)
{ {
if (monitor->server_pending_changes) if (m_monitor->server_pending_changes)
{ {
// Admin has changed something, skip sleep // Admin has changed something, skip sleep
break; break;
@ -166,45 +215,6 @@ monitorMain(void *arg)
mysql_thread_end(); mysql_thread_end();
} }
/**
* Helper function to free the monitor handle
*/
static void auroramon_free(AURORA_MONITOR *handle)
{
if (handle)
{
MXS_FREE(handle->script);
MXS_FREE(handle);
}
}
static
MXS_MONITOR_INSTANCE* createInstance(MXS_MONITOR* mon)
{
AURORA_MONITOR* handle = static_cast<AURORA_MONITOR*>(MXS_CALLOC(1, sizeof(AURORA_MONITOR)));
if (handle)
{
handle->shutdown = false;
handle->thread = 0;
handle->script = NULL;
handle->events = 0;
handle->monitor = mon;
handle->checked = false;
}
return handle;
}
static void destroyInstance(MXS_MONITOR_INSTANCE* mon)
{
AURORA_MONITOR* handle = static_cast<AURORA_MONITOR*>(mon);
ss_dassert(!handle->thread);
ss_dassert(!handle->script);
MXS_FREE(handle);
}
/** /**
* @brief Start the monitor * @brief Start the monitor
* *
@ -214,16 +224,13 @@ static void destroyInstance(MXS_MONITOR_INSTANCE* mon)
* @param opt The configuration parameters for this monitor * @param opt The configuration parameters for this monitor
* @return Monitor handle * @return Monitor handle
*/ */
static bool startMonitor(MXS_MONITOR_INSTANCE *mon, const MXS_CONFIG_PARAMETER *params) bool AuroraMonitor::start(const MXS_CONFIG_PARAMETER *params)
{ {
bool started = false; bool started = false;
AURORA_MONITOR *handle = static_cast<AURORA_MONITOR*>(mon); if (!m_checked)
ss_dassert(handle);
if (!handle->checked)
{ {
if (!check_monitor_permissions(handle->monitor, "SELECT @@aurora_server_id, server_id FROM " if (!check_monitor_permissions(m_monitor, "SELECT @@aurora_server_id, server_id FROM "
"information_schema.replica_host_status " "information_schema.replica_host_status "
"WHERE session_id = 'MASTER_SESSION_ID'")) "WHERE session_id = 'MASTER_SESSION_ID'"))
{ {
@ -231,20 +238,20 @@ static bool startMonitor(MXS_MONITOR_INSTANCE *mon, const MXS_CONFIG_PARAMETER *
} }
else else
{ {
handle->checked = true; m_checked = true;
} }
} }
if (handle->checked) if (m_checked)
{ {
handle->script = config_copy_string(params, "script"); m_script = config_copy_string(params, "script");
handle->events = config_get_enum(params, "events", mxs_monitor_event_enum_values); m_events = config_get_enum(params, "events", mxs_monitor_event_enum_values);
if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL) if (thread_start(&m_thread, &AuroraMonitor::main, this, 0) == NULL)
{ {
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", handle->monitor->name); MXS_ERROR("Failed to start monitor thread for monitor '%s'.", m_monitor->name);
MXS_FREE(handle->script); MXS_FREE(m_script);
handle->script = NULL; m_script = NULL;
} }
else else
{ {
@ -260,19 +267,17 @@ static bool startMonitor(MXS_MONITOR_INSTANCE *mon, const MXS_CONFIG_PARAMETER *
* *
* @param arg Handle on thr running monior * @param arg Handle on thr running monior
*/ */
static void void AuroraMonitor::stop()
stopMonitor(MXS_MONITOR_INSTANCE *mon)
{ {
AURORA_MONITOR *handle = static_cast<AURORA_MONITOR*>(mon); ss_dassert(m_thread);
ss_dassert(handle->thread);
handle->shutdown = true; m_shutdown = true;
thread_wait(handle->thread); thread_wait(m_thread);
handle->thread = 0; m_thread = 0;
handle->shutdown = false; m_shutdown = false;
MXS_FREE(handle->script); MXS_FREE(m_script);
handle->script = NULL; m_script = NULL;
} }
/** /**
@ -281,8 +286,7 @@ stopMonitor(MXS_MONITOR_INSTANCE *mon)
* @param dcb DCB to send output * @param dcb DCB to send output
* @param mon The monitor * @param mon The monitor
*/ */
static void void AuroraMonitor::diagnostics(DCB *dcb) const
diagnostics(const MXS_MONITOR_INSTANCE *mon, DCB *dcb)
{ {
} }
@ -292,13 +296,11 @@ diagnostics(const MXS_MONITOR_INSTANCE *mon, DCB *dcb)
* @param dcb DCB to send output * @param dcb DCB to send output
* @param mon The monitor * @param mon The monitor
*/ */
static json_t* diagnostics_json(const MXS_MONITOR_INSTANCE *mon) json_t* AuroraMonitor::diagnostics_json() const
{ {
return NULL; return NULL;
} }
extern "C"
{
/** /**
* The module entry point routine. It is this routine that must populate the * The module entry point routine. It is this routine that must populate the
* structure that is referred to as the "module object", this is a structure * structure that is referred to as the "module object", this is a structure
@ -306,18 +308,8 @@ extern "C"
* *
* @return The module object * @return The module object
*/ */
MXS_MODULE* MXS_CREATE_MODULE() extern "C" MXS_MODULE* MXS_CREATE_MODULE()
{ {
static MXS_MONITOR_API MyObject =
{
createInstance,
destroyInstance,
startMonitor,
stopMonitor,
diagnostics,
diagnostics_json
};
static MXS_MODULE info = static MXS_MODULE info =
{ {
MXS_MODULE_API_MONITOR, MXS_MODULE_API_MONITOR,
@ -326,7 +318,7 @@ MXS_MODULE* MXS_CREATE_MODULE()
"Aurora monitor", "Aurora monitor",
"V1.0.0", "V1.0.0",
MXS_NO_MODULE_CAPABILITIES, MXS_NO_MODULE_CAPABILITIES,
&MyObject, &maxscale::MonitorApi<AuroraMonitor>::s_api,
NULL, /* Process init. */ NULL, /* Process init. */
NULL, /* Process finish. */ NULL, /* Process finish. */
NULL, /* Thread init. */ NULL, /* Thread init. */
@ -351,5 +343,3 @@ MXS_MODULE* MXS_CREATE_MODULE()
return &info; return &info;
} }
}