From a55019774d9e0b1ea6ae71e73886cea8a26a925c Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 28 May 2018 10:17:30 +0300 Subject: [PATCH] MXS-1775 Expose the state of the monitor --- include/maxscale/monitor.hh | 16 +++++++++++++++- server/core/monitor.cc | 20 ++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/include/maxscale/monitor.hh b/include/maxscale/monitor.hh index 54fa26fed..e53877bb1 100644 --- a/include/maxscale/monitor.hh +++ b/include/maxscale/monitor.hh @@ -28,6 +28,20 @@ public: virtual ~MonitorInstance(); + /** + * @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 known that 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 Starts the monitor. * @@ -143,7 +157,7 @@ protected: MXS_MONITORED_SERVER* m_master; /**< Master server */ private: - int32_t m_status; /**< The current status of the monitor. */ + int32_t m_state; /**< The current state of the monitor. */ THREAD m_thread; /**< The thread handle of the monitoring thread. */ int32_t m_shutdown; /**< Non-zero if the monitor should shut down. */ bool m_checked; /**< Whether server access has been checked. */ diff --git a/server/core/monitor.cc b/server/core/monitor.cc index 982c10c08..0f216459f 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -2546,7 +2546,7 @@ namespace maxscale MonitorInstance::MonitorInstance(MXS_MONITOR* pMonitor) : m_monitor(pMonitor) , m_master(NULL) - , m_status(MXS_MONITOR_STOPPED) + , m_state(MXS_MONITOR_STOPPED) , m_thread(0) , m_shutdown(0) , m_checked(false) @@ -2559,15 +2559,21 @@ MonitorInstance::~MonitorInstance() ss_dassert(!m_thread); } +int32_t MonitorInstance::state() const +{ + return atomic_load_int32(&m_state); +} + void MonitorInstance::stop() { // This is always called in single-thread context. ss_dassert(m_thread); - ss_dassert(m_status == MXS_MONITOR_RUNNING); + ss_dassert(m_state == MXS_MONITOR_RUNNING); - atomic_store_int32(&m_status, MXS_MONITOR_STOPPING); + atomic_store_int32(&m_state, MXS_MONITOR_STOPPING); atomic_store_int32(&m_shutdown, 1); thread_wait(m_thread); + atomic_store_int32(&m_state, MXS_MONITOR_STOPPED); m_thread = 0; m_shutdown = 0; @@ -2616,7 +2622,7 @@ bool MonitorInstance::start(const MXS_CONFIG_PARAMETER* pParams) ss_dassert(!m_shutdown); ss_dassert(!m_thread); - ss_dassert(m_status == MXS_MONITOR_STOPPED); + ss_dassert(m_state == MXS_MONITOR_STOPPED); if (!m_checked) { @@ -2648,7 +2654,7 @@ bool MonitorInstance::start(const MXS_CONFIG_PARAMETER* pParams) // state has been updated. m_semaphore.wait(); - started = (atomic_load_int32(&m_status) == MXS_MONITOR_RUNNING); + started = (atomic_load_int32(&m_state) == MXS_MONITOR_RUNNING); if (!started) { @@ -2793,13 +2799,11 @@ void MonitorInstance::main(void* pArg) if (mysql_thread_init() == 0) { - atomic_store_int32(&pThis->m_status, MXS_MONITOR_RUNNING); + atomic_store_int32(&pThis->m_state, MXS_MONITOR_RUNNING); pThis->m_semaphore.post(); pThis->main(); - atomic_store_int32(&pThis->m_status, MXS_MONITOR_STOPPED); - mysql_thread_end(); } else