diff --git a/server/core/gateway.c b/server/core/gateway.c index efce2d97a..79c2585fe 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -1688,8 +1688,13 @@ static void log_flush_cb( static void unlink_pidfile(void) { if (strlen(pidfile)) { - if (unlink(pidfile)) { - fprintf(stderr, "MaxScale failed to remove pidfile %s: error %d, %s\n", pidfile, errno, strerror(errno)); + if (unlink(pidfile)) + { + fprintf(stderr, + "MaxScale failed to remove pidfile %s: error %d, %s\n", + pidfile, + errno, + strerror(errno)); } } } diff --git a/server/core/hint.c b/server/core/hint.c index 2a463585e..2d716771a 100644 --- a/server/core/hint.c +++ b/server/core/hint.c @@ -17,6 +17,7 @@ */ #include #include +#include #include /** diff --git a/server/core/monitor.c b/server/core/monitor.c index 6227afd5d..3504be917 100644 --- a/server/core/monitor.c +++ b/server/core/monitor.c @@ -60,8 +60,9 @@ MONITOR *mon; { return NULL; } - + mon->state = MONITOR_STATE_ALLOC; mon->name = strdup(name); + if ((mon->module = load_module(module, MODULE_MONITOR)) == NULL) { LOGIF(LE, (skygw_log_write_flush( @@ -73,7 +74,8 @@ MONITOR *mon; return NULL; } mon->handle = (*mon->module->startMonitor)(NULL); - mon->state |= MONITOR_STATE_RUNNING; + mon->state = MONITOR_STATE_RUNNING; + spinlock_acquire(&monLock); mon->next = allMonitors; allMonitors = mon; @@ -94,7 +96,7 @@ monitor_free(MONITOR *mon) MONITOR *ptr; mon->module->stopMonitor(mon->handle); - mon->state &= ~MONITOR_STATE_RUNNING; + mon->state = MONITOR_STATE_FREED; spinlock_acquire(&monLock); if (allMonitors == mon) allMonitors = mon->next; @@ -121,7 +123,7 @@ void monitorStart(MONITOR *monitor) { monitor->handle = (*monitor->module->startMonitor)(monitor->handle); - monitor->state |= MONITOR_STATE_RUNNING; + monitor->state = MONITOR_STATE_RUNNING; } /** @@ -132,8 +134,9 @@ monitorStart(MONITOR *monitor) void monitorStop(MONITOR *monitor) { + monitor->state = MONITOR_STATE_STOPPING; monitor->module->stopMonitor(monitor->handle); - monitor->state &= ~MONITOR_STATE_RUNNING; + monitor->state = MONITOR_STATE_STOPPED; } /** diff --git a/server/core/poll.c b/server/core/poll.c index ff8cfe7b3..e3decdba0 100644 --- a/server/core/poll.c +++ b/server/core/poll.c @@ -659,7 +659,7 @@ DCB *zombies = NULL; } } /*< for */ no_op = FALSE; - } + } /*< if (nfds > 0) */ process_zombies: if (thread_data) { diff --git a/server/include/monitor.h b/server/include/monitor.h index 5d018c16f..04337d761 100644 --- a/server/include/monitor.h +++ b/server/include/monitor.h @@ -69,7 +69,7 @@ typedef struct { void (*unregisterServer)(void *, SERVER *); void (*defaultUser)(void *, char *, char *); void (*diagnostics)(DCB *, void *); - void (*setInterval)(void *, unsigned long); + void (*setInterval)(void *, size_t); void (*defaultId)(void *, unsigned long); void (*replicationHeartbeat)(void *, int); void (*detectStaleMaster)(void *, int); @@ -81,21 +81,30 @@ typedef struct { */ #define MONITOR_VERSION {1, 0, 0} +/** Monitor's poll frequency */ +#define MON_BASE_INTERVAL_MS 100 + /** * Monitor state bit mask values */ -#define MONITOR_STATE_RUNNING 0x0001 - +typedef enum +{ + MONITOR_STATE_ALLOC = 0x00, + MONITOR_STATE_RUNNING = 0x01, + MONITOR_STATE_STOPPING = 0x02, + MONITOR_STATE_STOPPED = 0x04, + MONITOR_STATE_FREED = 0x08 +} monitor_state_t; /** * Representation of the running monitor. */ typedef struct monitor { char *name; /**< The name of the monitor module */ - unsigned int state; /**< The monitor status */ + monitor_state_t state; /**< The state of the monitor */ MONITOR_OBJECT *module; /**< The "monitor object" */ void *handle; /**< Handle returned from startMonitor */ - int interval; /**< The monitor interval */ + size_t interval; /**< The monitor interval */ struct monitor *next; /**< Next monitor in the linked list */ } MONITOR; diff --git a/server/modules/monitor/galera_mon.c b/server/modules/monitor/galera_mon.c index 211407f86..d2ad38264 100644 --- a/server/modules/monitor/galera_mon.c +++ b/server/modules/monitor/galera_mon.c @@ -67,9 +67,20 @@ static void registerServer(void *, SERVER *); static void unregisterServer(void *, SERVER *); static void defaultUsers(void *, char *, char *); static void diagnostics(DCB *, void *); -static void setInterval(void *, unsigned long); +static void setInterval(void *, size_t); -static MONITOR_OBJECT MyObject = { startMonitor, stopMonitor, registerServer, unregisterServer, defaultUsers, diagnostics, setInterval, NULL, NULL, NULL }; +static MONITOR_OBJECT MyObject = { + startMonitor, + stopMonitor, + registerServer, + unregisterServer, + defaultUsers, + diagnostics, + setInterval, + NULL, + NULL, + NULL, +}; /** * Implementation of the mandatory version entry point @@ -413,6 +424,7 @@ monitorMain(void *arg) MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg; MONITOR_SERVERS *ptr; long master_id; +size_t nrounds = 0; if (mysql_thread_init()) { @@ -423,10 +435,9 @@ long master_id; return; } handle->status = MONITOR_RUNNING; + while (1) { - master_id = -1; - if (handle->shutdown) { handle->status = MONITOR_STOPPING; @@ -434,7 +445,16 @@ long master_id; handle->status = MONITOR_STOPPED; return; } - + /** Wait base interval */ + thread_millisleep(MON_BASE_INTERVAL_MS); + nrounds += 1; + + /** If monitor interval time isn't consumed skip checks */ + if ((nrounds*MON_BASE_INTERVAL_MS)%handle->interval != 0) + { + continue; + } + master_id = -1; ptr = handle->databases; while (ptr) @@ -491,7 +511,6 @@ long master_id; ptr = ptr->next; } - thread_millisleep(handle->interval); } } @@ -502,7 +521,7 @@ long master_id; * @param interval The interval to set in monitor struct, in milliseconds */ static void -setInterval(void *arg, unsigned long interval) +setInterval(void *arg, size_t interval) { MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg; memcpy(&handle->interval, &interval, sizeof(unsigned long)); diff --git a/server/modules/monitor/mysql_mon.c b/server/modules/monitor/mysql_mon.c index 976aa315d..d7e0b34bd 100644 --- a/server/modules/monitor/mysql_mon.c +++ b/server/modules/monitor/mysql_mon.c @@ -80,7 +80,7 @@ static void registerServer(void *, SERVER *); static void unregisterServer(void *, SERVER *); static void defaultUser(void *, char *, char *); static void diagnostics(DCB *, void *); -static void setInterval(void *, unsigned long); +static void setInterval(void *, size_t); static void defaultId(void *, unsigned long); static void replicationHeartbeat(void *, int); static void detectStaleMaster(void *, int); @@ -95,7 +95,18 @@ static int add_slave_to_master(long *, int, long); static void monitor_set_pending_status(MONITOR_SERVERS *, int); static void monitor_clear_pending_status(MONITOR_SERVERS *, int); -static MONITOR_OBJECT MyObject = { startMonitor, stopMonitor, registerServer, unregisterServer, defaultUser, diagnostics, setInterval, defaultId, replicationHeartbeat, detectStaleMaster }; +static MONITOR_OBJECT MyObject = { + startMonitor, + stopMonitor, + registerServer, + unregisterServer, + defaultUser, + diagnostics, + setInterval, + defaultId, + replicationHeartbeat, + detectStaleMaster +}; /** * Implementation of the mandatory version entry point @@ -577,6 +588,7 @@ int replication_heartbeat = handle->replicationHeartbeat; int detect_stale_master = handle->detectStaleMaster; int num_servers=0; MONITOR_SERVERS *root_master; +size_t nrounds = 0; if (mysql_thread_init()) { @@ -586,8 +598,8 @@ MONITOR_SERVERS *root_master; "module. Exiting.\n"))); return; } - handle->status = MONITOR_RUNNING; + while (1) { if (handle->shutdown) @@ -597,6 +609,15 @@ MONITOR_SERVERS *root_master; handle->status = MONITOR_STOPPED; return; } + /** Wait base interval */ + thread_millisleep(MON_BASE_INTERVAL_MS); + nrounds += 1; + + /** If monitor interval time isn't consumed skip checks */ + if ((nrounds*MON_BASE_INTERVAL_MS)%handle->interval != 0) + { + continue; + } /* reset num_servers */ num_servers = 0; @@ -686,10 +707,7 @@ MONITOR_SERVERS *root_master; ptr = ptr->next; } } - - /* wait for the configured interval */ - thread_millisleep(handle->interval); - } + } /*< while (1) */ } /** @@ -704,7 +722,7 @@ defaultId(void *arg, unsigned long id) MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg; memcpy(&handle->id, &id, sizeof(unsigned long)); } - + /** * Set the monitor sampling interval. * @@ -712,7 +730,7 @@ MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg; * @param interval The interval to set in monitor struct, in milliseconds */ static void -setInterval(void *arg, unsigned long interval) +setInterval(void *arg, size_t interval) { MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg; memcpy(&handle->interval, &interval, sizeof(unsigned long)); diff --git a/server/modules/monitor/ndbcluster_mon.c b/server/modules/monitor/ndbcluster_mon.c index 840e30691..2e009e000 100644 --- a/server/modules/monitor/ndbcluster_mon.c +++ b/server/modules/monitor/ndbcluster_mon.c @@ -61,9 +61,20 @@ static void registerServer(void *, SERVER *); static void unregisterServer(void *, SERVER *); static void defaultUsers(void *, char *, char *); static void diagnostics(DCB *, void *); -static void setInterval(void *, unsigned long); +static void setInterval(void *, size_t); -static MONITOR_OBJECT MyObject = { startMonitor, stopMonitor, registerServer, unregisterServer, defaultUsers, diagnostics, setInterval, NULL, NULL, NULL }; +static MONITOR_OBJECT MyObject = { + startMonitor, + stopMonitor, + registerServer, + unregisterServer, + defaultUsers, + diagnostics, + setInterval, + NULL, + NULL, + NULL +}; /** * Implementation of the mandatory version entry point @@ -410,6 +421,7 @@ monitorMain(void *arg) MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg; MONITOR_SERVERS *ptr; long master_id; +size_t nrounds = 0; if (mysql_thread_init()) { @@ -420,10 +432,9 @@ long master_id; return; } handle->status = MONITOR_RUNNING; + while (1) { - master_id = -1; - if (handle->shutdown) { handle->status = MONITOR_STOPPING; @@ -432,6 +443,16 @@ long master_id; return; } + /** Wait base interval */ + thread_millisleep(MON_BASE_INTERVAL_MS); + nrounds += 1; + + /** If monitor interval time isn't consumed skip checks */ + if ((nrounds*MON_BASE_INTERVAL_MS)%handle->interval != 0) + { + continue; + } + master_id = -1; ptr = handle->databases; while (ptr) @@ -452,8 +473,6 @@ long master_id; ptr = ptr->next; } - - thread_millisleep(handle->interval); } } @@ -464,7 +483,7 @@ long master_id; * @param interval The interval to set in monitor struct, in milliseconds */ static void -setInterval(void *arg, unsigned long interval) +setInterval(void *arg, size_t interval) { MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg; memcpy(&handle->interval, &interval, sizeof(unsigned long));