From 3125d1babfce7f278083008ad9440486fbe55118 Mon Sep 17 00:00:00 2001 From: Esa Korhonen Date: Thu, 17 Jan 2019 10:22:28 +0200 Subject: [PATCH] MXS-2271 Convert MXS_MONITOR and MXS_MONITORED_SERVER to classes First step in monitor refactoring and cleanup. --- include/maxscale/monitor.hh | 110 +++++++++--------- server/core/monitor.cc | 32 ++--- .../mariadbmon/test/test_cycle_find.cc | 8 +- 3 files changed, 72 insertions(+), 78 deletions(-) diff --git a/include/maxscale/monitor.hh b/include/maxscale/monitor.hh index 3e1638324..b648f12d1 100644 --- a/include/maxscale/monitor.hh +++ b/include/maxscale/monitor.hh @@ -29,7 +29,7 @@ #include #include -struct MXS_MONITOR; +class MXS_MONITOR; /** * An opaque type representing a monitor instance. @@ -78,7 +78,7 @@ struct MXS_MONITOR_API * @return Pointer to the monitor specific data. Will be stored * in @c monitor->handle. */ - MXS_MONITOR_INSTANCE*(*createInstance)(MXS_MONITOR * monitor); + MXS_MONITOR_INSTANCE* (* createInstance)(MXS_MONITOR* monitor); /** * @brief Destroy the monitor. @@ -133,7 +133,7 @@ struct MXS_MONITOR_API * * @see jansson.h */ - json_t* (*diagnostics_json)(const MXS_MONITOR_INSTANCE * monitor); + json_t* (* diagnostics_json)(const MXS_MONITOR_INSTANCE* monitor); }; /** @@ -204,16 +204,20 @@ enum mxs_monitor_event_t /** * The linked list of servers that are being monitored by the monitor module. */ -struct MXS_MONITORED_SERVER +class MXS_MONITORED_SERVER { - SERVER* server;/**< The server being monitored */ - MYSQL* con; /**< The MySQL connection */ - bool log_version_err; - int mon_err_count; - uint64_t mon_prev_status; /**< Status before starting the current monitor loop */ - uint64_t pending_status; /**< Status during current monitor loop */ - int64_t disk_space_checked;/**< When was the disk space checked the last time */ - struct MXS_MONITORED_SERVER* next; /**< The next server in the list */ +public: + MXS_MONITORED_SERVER(SERVER* server); + + SERVER* server = nullptr; /**< The server being monitored */ + MYSQL* con = nullptr; /**< The MySQL connection */ + bool log_version_err = true; + int mon_err_count = 0; + uint64_t mon_prev_status = -1; /**< Status before starting the current monitor loop */ + uint64_t pending_status = 0; /**< Status during current monitor loop */ + int64_t disk_space_checked = 0; /**< When was the disk space checked the last time */ + + MXS_MONITORED_SERVER* next = nullptr; /**< The next server in the list */ }; namespace std @@ -237,48 +241,48 @@ inline mxb::intrusive_slist_iterator end(MXS_MONITORED_SER /** * Representation of the running monitor. */ -struct MXS_MONITOR +class MXS_MONITOR { - char* name; /**< The name of the monitor module */ - char user[MAX_MONITOR_USER_LEN]; /*< Monitor username */ - char password[MAX_MONITOR_PASSWORD_LEN]; /*< Monitor password */ - pthread_mutex_t lock; - MXS_CONFIG_PARAMETER* parameters; /*< configuration parameters */ - MXS_MONITORED_SERVER* monitored_servers; /*< List of servers the monitor monitors */ - monitor_state_t state; /**< The state of the monitor. This should ONLY be - * written to by the admin - * thread. */ - int connect_timeout; /**< Connect timeout in seconds for - * mysql_real_connect */ - int connect_attempts; /**< How many times a connection is attempted */ - int read_timeout; /**< Timeout in seconds to read from the server. - * There are retries and the total effective - * timeout - * value is three times the option value. - */ - int write_timeout; /**< Timeout in seconds for each attempt to write - * to the server. - * There are retries and the total effective - * timeout value is - * two times the option value. - */ - MXS_MONITOR_API* api; /**< The monitor api */ - char* module_name; /**< Name of the monitor module */ - MXS_MONITOR_INSTANCE* instance; /**< Instance returned from startMonitor */ - size_t interval; /**< The monitor interval */ - int check_maintenance_flag; /**< Set when admin requests a maintenance status - * change. */ - bool active; /**< True if monitor is active */ - time_t journal_max_age; /**< Maximum age of journal file */ - uint32_t script_timeout; /**< Timeout in seconds for the monitor scripts */ - const char* script; /**< Launchable script. */ - uint64_t events; /**< Enabled monitor events. */ - uint8_t journal_hash[SHA_DIGEST_LENGTH]; /**< SHA1 hash of the latest written journal */ - MxsDiskSpaceThreshold* disk_space_threshold; /**< Disk space thresholds */ - int64_t disk_space_check_interval; /**< How often should a disk space check be made - * at most. */ - uint64_t ticks; /**< Number of performed monitoring intervals */ - struct MXS_MONITOR* next; /**< Next monitor in the linked list */ +public: + char* name; /**< Monitor instance name */ + char* module_name; /**< Name of the monitor module */ + + MXS_MONITOR_API* api; /**< The monitor api */ + MXS_MONITOR_INSTANCE* instance; /**< Instance returned from startMonitor */ + MXS_MONITOR* next; /**< Next monitor in the linked list */ + + bool active; /**< True if monitor is active */ + pthread_mutex_t lock; + monitor_state_t state; /**< The state of the monitor. This should ONLY be written to by the + * admin thread. */ + + int check_maintenance_flag; /**< Set when admin requests a maintenance status change. */ + uint64_t ticks; /**< Number of performed monitoring intervals */ + uint8_t journal_hash[SHA_DIGEST_LENGTH]; /**< SHA1 hash of the latest written journal */ + + MXS_CONFIG_PARAMETER* parameters; /**< Configuration parameters */ + MXS_MONITORED_SERVER* monitored_servers;/**< List of servers the monitor monitors */ + + char user[MAX_MONITOR_USER_LEN]; /**< Monitor username */ + char password[MAX_MONITOR_PASSWORD_LEN]; /**< Monitor password */ + + int connect_timeout; /**< Connect timeout in seconds for mysql_real_connect */ + int connect_attempts; /**< How many times a connection is attempted */ + int read_timeout; /**< Timeout in seconds to read from the server. There are retries + * and the total effective timeout value is three times the option value. */ + int write_timeout; /**< Timeout in seconds for each attempt to write to the server. + * There are retries and the total effective timeout value is two times + * the option value. */ + + size_t interval; /**< The monitor interval */ + time_t journal_max_age; /**< Maximum age of journal file */ + uint32_t script_timeout; /**< Timeout in seconds for the monitor scripts */ + const char* script; /**< Launchable script. */ + uint64_t events; /**< Enabled monitor events. */ + + MxsDiskSpaceThreshold* disk_space_threshold; /**< Disk space thresholds */ + int64_t disk_space_check_interval; /**< How often should a disk space check be made + * at most. */ }; /** diff --git a/server/core/monitor.cc b/server/core/monitor.cc index 59013b3e4..b90bb79e8 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -115,11 +115,11 @@ MXS_MONITOR* monitor_create(const char* name, const char* module, MXS_CONFIG_PAR char* my_name = MXS_STRDUP(name); char* my_module = MXS_STRDUP(module); - MXS_MONITOR* mon = (MXS_MONITOR*)MXS_MALLOC(sizeof(MXS_MONITOR)); + MXS_MONITOR* mon = new (std::nothrow) MXS_MONITOR(); if (!mon || !my_module || !my_name) { - MXS_FREE(mon); + delete mon; MXS_FREE(my_name); MXS_FREE(my_module); return NULL; @@ -189,7 +189,7 @@ MXS_MONITOR* monitor_create(const char* name, const char* module, MXS_CONFIG_PAR } else { - MXS_FREE(mon); + delete mon; mon = NULL; MXS_FREE(my_module); MXS_FREE(my_name); @@ -234,7 +234,7 @@ void monitor_destroy(MXS_MONITOR* mon) monitor_server_free_all(mon->monitored_servers); MXS_FREE(mon->name); MXS_FREE(mon->module_name); - MXS_FREE(mon); + delete mon; } void monitor_destroy_all() @@ -376,23 +376,9 @@ bool monitor_add_server(MXS_MONITOR* mon, SERVER* server) else { rval = true; - MXS_MONITORED_SERVER* db = (MXS_MONITORED_SERVER*)MXS_MALLOC(sizeof(MXS_MONITORED_SERVER)); + MXS_MONITORED_SERVER* db = new (std::nothrow) MXS_MONITORED_SERVER(server); MXS_ABORT_IF_NULL(db); - db->server = server; - db->con = NULL; - db->next = NULL; - db->mon_err_count = 0; - db->log_version_err = true; - // Pretend disk space was just checked. - db->disk_space_checked = maxscale::MonitorInstance::get_time_ms(); - - - /** Server status is uninitialized */ - db->mon_prev_status = -1; - /* pending status is updated by get_replication_tree */ - db->pending_status = 0; - monitor_state_t old_state = mon->state; if (old_state == MONITOR_STATE_RUNNING) @@ -434,7 +420,7 @@ static void monitor_server_free(MXS_MONITORED_SERVER* tofree) { mysql_close(tofree->con); } - MXS_FREE(tofree); + delete tofree; } } @@ -2960,3 +2946,9 @@ bool MonitorInstance::immediate_tick_required() const return false; } } + +MXS_MONITORED_SERVER::MXS_MONITORED_SERVER(SERVER* server) + : server(server) + , disk_space_checked(maxscale::MonitorInstance::get_time_ms()) // Pretend disk space was just checked. +{ +} diff --git a/server/modules/monitor/mariadbmon/test/test_cycle_find.cc b/server/modules/monitor/mariadbmon/test/test_cycle_find.cc index 348eea299..ebb5681ed 100644 --- a/server/modules/monitor/mariadbmon/test/test_cycle_find.cc +++ b/server/modules/monitor/mariadbmon/test/test_cycle_find.cc @@ -156,11 +156,9 @@ void MariaDBMonitor::Test::init_servers(int count) for (int i = 1; i < count + 1; i++) { - auto base_server = Server::create_test_server(); // Contents mostly undefined - - MXS_MONITORED_SERVER* mon_server = new MXS_MONITORED_SERVER; // Contents mostly undefined - mon_server->server = base_server; - + // Server contents mostly undefined + auto base_server = Server::create_test_server(); + MXS_MONITORED_SERVER* mon_server = new MXS_MONITORED_SERVER(base_server); MariaDBServer* mariadb_server = new MariaDBServer(mon_server, i - 1, m_use_hostnames); if (m_use_hostnames)