346 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			346 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 * Copyright (c) 2018 MariaDB Corporation Ab
 | 
						|
 *
 | 
						|
 * Use of this software is governed by the Business Source License included
 | 
						|
 * in the LICENSE.TXT file and at www.mariadb.com/bsl11.
 | 
						|
 *
 | 
						|
 * Change Date: 2022-01-01
 | 
						|
 *
 | 
						|
 * On the date above, in accordance with the Business Source License, use
 | 
						|
 * of this software will be governed by version 2 or later of the General
 | 
						|
 * Public License.
 | 
						|
 */
 | 
						|
#pragma once
 | 
						|
 | 
						|
#include <maxscale/ccdefs.hh>
 | 
						|
 | 
						|
#include <atomic>
 | 
						|
#include <maxbase/semaphore.hh>
 | 
						|
#include <maxbase/worker.hh>
 | 
						|
#include <maxscale/monitor.h>
 | 
						|
 | 
						|
namespace maxscale
 | 
						|
{
 | 
						|
 | 
						|
class MonitorInstance : public MXS_MONITOR_INSTANCE
 | 
						|
                      , protected maxbase::Worker
 | 
						|
{
 | 
						|
public:
 | 
						|
    MonitorInstance(const MonitorInstance&) = delete;
 | 
						|
    MonitorInstance& operator=(const MonitorInstance&) = delete;
 | 
						|
 | 
						|
    virtual ~MonitorInstance();
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Current state of the monitor.
 | 
						|
     *
 | 
						|
     * Since the state is written to by the admin thread, the value returned in other threads cannot be fully
 | 
						|
     * trusted. The state should only be read in the admin thread or operations launched by the admin thread.
 | 
						|
     *
 | 
						|
     * @return @c MONITOR_STATE_RUNNING if the monitor is running,
 | 
						|
     *         @c MONITOR_STATE_STOPPING if the monitor is stopping, and
 | 
						|
     *         @c MONITOR_STATE_STOPPED if the monitor is stopped.
 | 
						|
     */
 | 
						|
    monitor_state_t monitor_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 monitor_state() == MONITOR_STATE_RUNNING;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Starts the monitor.
 | 
						|
     *
 | 
						|
     * - Calls @c has_sufficient_permissions(), if it has not been done earlier.
 | 
						|
     * - Updates the 'script' and 'events' configuration paramameters.
 | 
						|
     * - Calls @c configure().
 | 
						|
     * - Starts the monitor thread.
 | 
						|
     *
 | 
						|
     * - Once the monitor thread starts, it will
 | 
						|
     *   - Load the server journal and update @c m_master.
 | 
						|
     *   - Call @c pre_loop().
 | 
						|
     *   - Enter a loop where it, until told to shut down, will
 | 
						|
     *     - Check whether there are maintenance requests.
 | 
						|
     *     - Call @c tick().
 | 
						|
     *     - Call @c process_state_changes()
 | 
						|
     *     - Hang up failed servers.
 | 
						|
     *     - Store the server journal (@c m_master assumed to reflect the current situation).
 | 
						|
     *     - Sleep until time for next @c tick().
 | 
						|
     *   - Call @c post_loop().
 | 
						|
     *
 | 
						|
     * @param param  The parameters of the monitor.
 | 
						|
     *
 | 
						|
     * @return True, if the monitor started, false otherwise.
 | 
						|
     */
 | 
						|
    bool start(const MXS_CONFIG_PARAMETER* params);
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Stops the monitor.
 | 
						|
     *
 | 
						|
     * When the function returns, the monitor has stopped.
 | 
						|
     */
 | 
						|
    void stop();
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Write diagnostics
 | 
						|
     *
 | 
						|
     * The implementation should write diagnostic information to the
 | 
						|
     * provided dcb. The default implementation writes nothing.
 | 
						|
     *
 | 
						|
     * @param dcb  The dcb to write to.
 | 
						|
     */
 | 
						|
    virtual void diagnostics(DCB* dcb) const;
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Obtain diagnostics
 | 
						|
     *
 | 
						|
     * The implementation should create a JSON object and fill it with diagnostics
 | 
						|
     * information. The default implementation returns an object that is populated
 | 
						|
     * with the keys 'script' and 'events' if they have been set, otherwise the
 | 
						|
     * object is empty.
 | 
						|
     *
 | 
						|
     * @return An object, if there is information to return, NULL otherwise.
 | 
						|
     */
 | 
						|
    virtual json_t* diagnostics_json() const;
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get current time from the monotonic clock.
 | 
						|
     *
 | 
						|
     * @return Current time
 | 
						|
     */
 | 
						|
    static int64_t get_time_ms();
 | 
						|
 | 
						|
protected:
 | 
						|
    MonitorInstance(MXS_MONITOR* pMonitor);
 | 
						|
 | 
						|
    /**
 | 
						|
     * @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 Should the disk space status be updated.
 | 
						|
     *
 | 
						|
     * @param pMonitored_server  The monitored server in question.
 | 
						|
     *
 | 
						|
     * @return True, if the disk space should be checked, false otherwise.
 | 
						|
     */
 | 
						|
    bool should_update_disk_space_status(const MXS_MONITORED_SERVER* pMonitored_server) const;
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Update the disk space status of a server.
 | 
						|
     *
 | 
						|
     * After the call, the bit @c SERVER_DISK_SPACE_EXHAUSTED will be set on
 | 
						|
     * @c pMonitored_server->pending_status if the disk space is exhausted
 | 
						|
     * or cleared if it is not.
 | 
						|
     */
 | 
						|
    void update_disk_space_status(MXS_MONITORED_SERVER* pMonitored_server);
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Configure the monitor.
 | 
						|
     *
 | 
						|
     * When the monitor is started, this function will be called in order
 | 
						|
     * to allow the concrete implementation to configure itself from
 | 
						|
     * configuration parameters. The default implementation returns true.
 | 
						|
     *
 | 
						|
     * @return True, if the monitor could be configured, false otherwise.
 | 
						|
     *
 | 
						|
     * @note If false is returned, then the monitor will not be started.
 | 
						|
     */
 | 
						|
    virtual bool configure(const MXS_CONFIG_PARAMETER* pParams);
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Check whether the monitor has sufficient rights
 | 
						|
     *
 | 
						|
     * The implementation should check whether the monitor user has sufficient
 | 
						|
     * rights to access the servers. The default implementation returns True.
 | 
						|
     *
 | 
						|
     * @return True, if the monitor user has sufficient rights, false otherwise.
 | 
						|
     */
 | 
						|
    virtual bool has_sufficient_permissions() const;
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Flush pending server status to each server.
 | 
						|
     *
 | 
						|
     * This function is expected to flush the pending status to each server.
 | 
						|
     * The default implementation simply copies monitored_server->pending_status
 | 
						|
     * to server->status.
 | 
						|
     */
 | 
						|
    virtual void flush_server_status();
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Monitor the servers
 | 
						|
     *
 | 
						|
     * This function is called once per monitor round, and the concrete
 | 
						|
     * implementation should probe all servers and set server status bits.
 | 
						|
     */
 | 
						|
    virtual void tick() = 0;
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Called before the monitor loop is started
 | 
						|
     *
 | 
						|
     * The default implementation does nothing.
 | 
						|
     */
 | 
						|
    virtual void pre_loop();
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Called after the monitor loop has ended.
 | 
						|
     *
 | 
						|
     * The default implementation does nothing.
 | 
						|
     */
 | 
						|
    virtual void post_loop();
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Called after tick returns
 | 
						|
     *
 | 
						|
     * The default implementation will call @mon_process_state_changes.
 | 
						|
     */
 | 
						|
    virtual void process_state_changes();
 | 
						|
 | 
						|
    MXS_MONITOR*          m_monitor;    /**< The generic monitor structure. */
 | 
						|
    MXS_MONITORED_SERVER* m_master;     /**< Master server */
 | 
						|
 | 
						|
private:
 | 
						|
    std::atomic<bool> m_thread_running; /**< Thread state. Only visible inside MonitorInstance. */
 | 
						|
    int32_t           m_shutdown;       /**< Non-zero if the monitor should shut down. */
 | 
						|
    bool              m_checked;        /**< Whether server access has been checked. */
 | 
						|
    mxb::Semaphore    m_semaphore;      /**< Semaphore for synchronizing with monitor thread. */
 | 
						|
    int64_t           m_loop_called;    /**< When was the loop called the last time. */
 | 
						|
 | 
						|
    bool pre_run() final;
 | 
						|
    void post_run() final;
 | 
						|
 | 
						|
    bool call_run_one_tick(Worker::Call::action_t action);
 | 
						|
    void run_one_tick();
 | 
						|
};
 | 
						|
 | 
						|
class MonitorInstanceSimple : public MonitorInstance
 | 
						|
{
 | 
						|
public:
 | 
						|
    MonitorInstanceSimple(const MonitorInstanceSimple&) = delete;
 | 
						|
    MonitorInstanceSimple& operator=(const MonitorInstanceSimple&) = delete;
 | 
						|
 | 
						|
protected:
 | 
						|
    MonitorInstanceSimple(MXS_MONITOR* pMonitor)
 | 
						|
        : MonitorInstance(pMonitor)
 | 
						|
    {
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Update server information
 | 
						|
     *
 | 
						|
     * The implementation should probe the server in question and update
 | 
						|
     * the server status bits.
 | 
						|
     */
 | 
						|
    virtual void update_server_status(MXS_MONITORED_SERVER* pMonitored_server) = 0;
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Called right at the beginning of @c tick().
 | 
						|
     *
 | 
						|
     * The default implementation does nothing.
 | 
						|
     */
 | 
						|
    virtual void pre_tick();
 | 
						|
 | 
						|
    /**
 | 
						|
     * @brief Called right before the end of @c tick().
 | 
						|
     *
 | 
						|
     * The default implementation does nothing.
 | 
						|
     */
 | 
						|
    virtual void post_tick();
 | 
						|
 | 
						|
private:
 | 
						|
    /**
 | 
						|
     * @brief Monitor the servers
 | 
						|
     *
 | 
						|
     * This function is called once per monitor round and will for each server:
 | 
						|
     *
 | 
						|
     *   - Do nothing, if the server is in maintenance.
 | 
						|
     *   - Store the previous status of the server.
 | 
						|
     *   - Set the pending status of the monitored server object
 | 
						|
     *     to the status of the corresponding server object.
 | 
						|
     *   - Ensure that there is a connection to the server.
 | 
						|
     *     If there is, @c update_server_status() is called.
 | 
						|
     *     If there is not, the pending status will be updated accordingly and
 | 
						|
     *     @c update_server_status() will *not* be called.
 | 
						|
     *   - After the call, update the error count of the server if it is down.
 | 
						|
     */
 | 
						|
    void tick();    // final
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * The purpose of the template MonitorApi is to provide an implementation
 | 
						|
 * of the monitor C-API. The template is instantiated with a class that
 | 
						|
 * provides the actual behaviour of a monitor.
 | 
						|
 */
 | 
						|
template<class MonitorInstance>
 | 
						|
class MonitorApi
 | 
						|
{
 | 
						|
public:
 | 
						|
    MonitorApi() = delete;
 | 
						|
    MonitorApi(const MonitorApi&) = delete;
 | 
						|
    MonitorApi& operator=(const MonitorApi&) = delete;
 | 
						|
 | 
						|
    static MXS_MONITOR_INSTANCE* createInstance(MXS_MONITOR* pMonitor)
 | 
						|
    {
 | 
						|
        MonitorInstance* pInstance = NULL;
 | 
						|
        MXS_EXCEPTION_GUARD(pInstance = MonitorInstance::create(pMonitor));
 | 
						|
        return pInstance;
 | 
						|
    }
 | 
						|
 | 
						|
    static void destroyInstance(MXS_MONITOR_INSTANCE* pInstance)
 | 
						|
    {
 | 
						|
        MXS_EXCEPTION_GUARD(delete static_cast<MonitorInstance*>(pInstance));
 | 
						|
    }
 | 
						|
 | 
						|
    static bool startMonitor(MXS_MONITOR_INSTANCE* pInstance,
 | 
						|
                             const MXS_CONFIG_PARAMETER* pParams)
 | 
						|
    {
 | 
						|
        bool started = false;
 | 
						|
        MXS_EXCEPTION_GUARD(started = static_cast<MonitorInstance*>(pInstance)->start(pParams));
 | 
						|
        return started;
 | 
						|
    }
 | 
						|
 | 
						|
    static void stopMonitor(MXS_MONITOR_INSTANCE* pInstance)
 | 
						|
    {
 | 
						|
        MXS_EXCEPTION_GUARD(static_cast<MonitorInstance*>(pInstance)->stop());
 | 
						|
    }
 | 
						|
 | 
						|
    static void diagnostics(const MXS_MONITOR_INSTANCE* pInstance, DCB* pDcb)
 | 
						|
    {
 | 
						|
        MXS_EXCEPTION_GUARD(static_cast<const MonitorInstance*>(pInstance)->diagnostics(pDcb));
 | 
						|
    }
 | 
						|
 | 
						|
    static json_t* diagnostics_json(const MXS_MONITOR_INSTANCE* pInstance)
 | 
						|
    {
 | 
						|
        json_t* pJson = NULL;
 | 
						|
        MXS_EXCEPTION_GUARD(pJson = static_cast<const MonitorInstance*>(pInstance)->diagnostics_json());
 | 
						|
        return pJson;
 | 
						|
    }
 | 
						|
 | 
						|
    static MXS_MONITOR_API s_api;
 | 
						|
};
 | 
						|
 | 
						|
template<class MonitorInstance>
 | 
						|
MXS_MONITOR_API MonitorApi<MonitorInstance>::s_api =
 | 
						|
{
 | 
						|
    &MonitorApi<MonitorInstance>::createInstance,
 | 
						|
    &MonitorApi<MonitorInstance>::destroyInstance,
 | 
						|
    &MonitorApi<MonitorInstance>::startMonitor,
 | 
						|
    &MonitorApi<MonitorInstance>::stopMonitor,
 | 
						|
    &MonitorApi<MonitorInstance>::diagnostics,
 | 
						|
    &MonitorApi<MonitorInstance>::diagnostics_json,
 | 
						|
};
 | 
						|
}
 |