Reindent server/core/monitor.c

This commit is contained in:
Johan Wikman
2015-11-30 21:16:21 +02:00
parent b2dd24ada3
commit 74bb291961
2 changed files with 374 additions and 337 deletions

View File

@ -73,7 +73,7 @@ static SPINLOCK monLock = SPINLOCK_INIT;
MONITOR * MONITOR *
monitor_alloc(char *name, char *module) monitor_alloc(char *name, char *module)
{ {
MONITOR *mon; MONITOR *mon;
if ((mon = (MONITOR *)malloc(sizeof(MONITOR))) == NULL) if ((mon = (MONITOR *)malloc(sizeof(MONITOR))) == NULL)
{ {
@ -116,21 +116,27 @@ MONITOR *mon;
void void
monitor_free(MONITOR *mon) monitor_free(MONITOR *mon)
{ {
MONITOR *ptr; MONITOR *ptr;
mon->module->stopMonitor(mon); mon->module->stopMonitor(mon);
mon->state = MONITOR_STATE_FREED; mon->state = MONITOR_STATE_FREED;
spinlock_acquire(&monLock); spinlock_acquire(&monLock);
if (allMonitors == mon) if (allMonitors == mon)
{
allMonitors = mon->next; allMonitors = mon->next;
}
else else
{ {
ptr = allMonitors; ptr = allMonitors;
while (ptr->next && ptr->next != mon) while (ptr->next && ptr->next != mon)
{
ptr = ptr->next; ptr = ptr->next;
}
if (ptr->next) if (ptr->next)
{
ptr->next = mon->next; ptr->next = mon->next;
} }
}
spinlock_release(&monLock); spinlock_release(&monLock);
free_config_parameter(mon->parameters); free_config_parameter(mon->parameters);
free(mon->name); free(mon->name);
@ -177,7 +183,7 @@ void monitorStartAll()
void void
monitorStop(MONITOR *monitor) monitorStop(MONITOR *monitor)
{ {
if(monitor->state != MONITOR_STATE_STOPPED) if (monitor->state != MONITOR_STATE_STOPPED)
{ {
monitor->state = MONITOR_STATE_STOPPING; monitor->state = MONITOR_STATE_STOPPING;
monitor->module->stopMonitor(monitor); monitor->module->stopMonitor(monitor);
@ -191,7 +197,7 @@ monitorStop(MONITOR *monitor)
void void
monitorStopAll() monitorStopAll()
{ {
MONITOR *ptr; MONITOR *ptr;
spinlock_acquire(&monLock); spinlock_acquire(&monLock);
ptr = allMonitors; ptr = allMonitors;
@ -216,7 +222,9 @@ monitorAddServer(MONITOR *mon, SERVER *server)
MONITOR_SERVERS *ptr, *db; MONITOR_SERVERS *ptr, *db;
if ((db = (MONITOR_SERVERS *)malloc(sizeof(MONITOR_SERVERS))) == NULL) if ((db = (MONITOR_SERVERS *)malloc(sizeof(MONITOR_SERVERS))) == NULL)
{
return; return;
}
db->server = server; db->server = server;
db->con = NULL; db->con = NULL;
db->next = NULL; db->next = NULL;
@ -230,12 +238,16 @@ monitorAddServer(MONITOR *mon, SERVER *server)
spinlock_acquire(&mon->lock); spinlock_acquire(&mon->lock);
if (mon->databases == NULL) if (mon->databases == NULL)
{
mon->databases = db; mon->databases = db;
}
else else
{ {
ptr = mon->databases; ptr = mon->databases;
while (ptr->next != NULL) while (ptr->next != NULL)
{
ptr = ptr->next; ptr = ptr->next;
}
ptr->next = db; ptr->next = db;
} }
spinlock_release(&mon->lock); spinlock_release(&mon->lock);
@ -264,7 +276,7 @@ monitorAddUser(MONITOR *mon, char *user, char *passwd)
void void
monitorShowAll(DCB *dcb) monitorShowAll(DCB *dcb)
{ {
MONITOR *ptr; MONITOR *ptr;
spinlock_acquire(&monLock); spinlock_acquire(&monLock);
ptr = allMonitors; ptr = allMonitors;
@ -273,7 +285,9 @@ MONITOR *ptr;
dcb_printf(dcb, "Monitor: %p\n", ptr); dcb_printf(dcb, "Monitor: %p\n", ptr);
dcb_printf(dcb, "\tName: %s\n", ptr->name); dcb_printf(dcb, "\tName: %s\n", ptr->name);
if (ptr->module->diagnostics) if (ptr->module->diagnostics)
{
ptr->module->diagnostics(dcb, ptr); ptr->module->diagnostics(dcb, ptr);
}
ptr = ptr->next; ptr = ptr->next;
} }
spinlock_release(&monLock); spinlock_release(&monLock);
@ -291,7 +305,9 @@ monitorShow(DCB *dcb, MONITOR *monitor)
dcb_printf(dcb, "Monitor: %p\n", monitor); dcb_printf(dcb, "Monitor: %p\n", monitor);
dcb_printf(dcb, "\tName: %s\n", monitor->name); dcb_printf(dcb, "\tName: %s\n", monitor->name);
if (monitor->module->diagnostics) if (monitor->module->diagnostics)
{
monitor->module->diagnostics(dcb, monitor); monitor->module->diagnostics(dcb, monitor);
}
} }
/** /**
@ -302,7 +318,7 @@ monitorShow(DCB *dcb, MONITOR *monitor)
void void
monitorList(DCB *dcb) monitorList(DCB *dcb)
{ {
MONITOR *ptr; MONITOR *ptr;
spinlock_acquire(&monLock); spinlock_acquire(&monLock);
ptr = allMonitors; ptr = allMonitors;
@ -329,14 +345,16 @@ MONITOR *ptr;
MONITOR * MONITOR *
monitor_find(char *name) monitor_find(char *name)
{ {
MONITOR *ptr; MONITOR *ptr;
spinlock_acquire(&monLock); spinlock_acquire(&monLock);
ptr = allMonitors; ptr = allMonitors;
while (ptr) while (ptr)
{ {
if (!strcmp(ptr->name, name)) if (!strcmp(ptr->name, name))
{
break; break;
}
ptr = ptr->next; ptr = ptr->next;
} }
spinlock_release(&monLock); spinlock_release(&monLock);
@ -350,7 +368,7 @@ MONITOR *ptr;
* @param interval The sampling interval in milliseconds * @param interval The sampling interval in milliseconds
*/ */
void void
monitorSetInterval (MONITOR *mon, unsigned long interval) monitorSetInterval(MONITOR *mon, unsigned long interval)
{ {
mon->interval = interval; mon->interval = interval;
} }
@ -369,13 +387,18 @@ monitorSetNetworkTimeout(MONITOR *mon, int type, int value) {
int new_timeout = max_timeout -1; int new_timeout = max_timeout -1;
if (new_timeout <= 0) if (new_timeout <= 0)
{
new_timeout = DEFAULT_CONNECT_TIMEOUT; new_timeout = DEFAULT_CONNECT_TIMEOUT;
}
switch(type) { switch(type) {
case MONITOR_CONNECT_TIMEOUT: case MONITOR_CONNECT_TIMEOUT:
if (value < max_timeout) { if (value < max_timeout)
{
memcpy(&mon->connect_timeout, &value, sizeof(int)); memcpy(&mon->connect_timeout, &value, sizeof(int));
} else { }
else
{
memcpy(&mon->connect_timeout, &new_timeout, sizeof(int)); memcpy(&mon->connect_timeout, &new_timeout, sizeof(int));
MXS_WARNING("Monitor Connect Timeout %i is greater than monitor interval ~%i seconds" MXS_WARNING("Monitor Connect Timeout %i is greater than monitor interval ~%i seconds"
", lowering to %i seconds", value, max_timeout, new_timeout); ", lowering to %i seconds", value, max_timeout, new_timeout);
@ -383,9 +406,12 @@ monitorSetNetworkTimeout(MONITOR *mon, int type, int value) {
break; break;
case MONITOR_READ_TIMEOUT: case MONITOR_READ_TIMEOUT:
if (value < max_timeout) { if (value < max_timeout)
{
memcpy(&mon->read_timeout, &value, sizeof(int)); memcpy(&mon->read_timeout, &value, sizeof(int));
} else { }
else
{
memcpy(&mon->read_timeout, &new_timeout, sizeof(int)); memcpy(&mon->read_timeout, &new_timeout, sizeof(int));
MXS_WARNING("Monitor Read Timeout %i is greater than monitor interval ~%i seconds" MXS_WARNING("Monitor Read Timeout %i is greater than monitor interval ~%i seconds"
", lowering to %i seconds", value, max_timeout, new_timeout); ", lowering to %i seconds", value, max_timeout, new_timeout);
@ -393,9 +419,12 @@ monitorSetNetworkTimeout(MONITOR *mon, int type, int value) {
break; break;
case MONITOR_WRITE_TIMEOUT: case MONITOR_WRITE_TIMEOUT:
if (value < max_timeout) { if (value < max_timeout)
{
memcpy(&mon->write_timeout, &value, sizeof(int)); memcpy(&mon->write_timeout, &value, sizeof(int));
} else { }
else
{
memcpy(&mon->write_timeout, &new_timeout, sizeof(int)); memcpy(&mon->write_timeout, &new_timeout, sizeof(int));
MXS_WARNING("Monitor Write Timeout %i is greater than monitor interval ~%i seconds" MXS_WARNING("Monitor Write Timeout %i is greater than monitor interval ~%i seconds"
", lowering to %i seconds", value, max_timeout, new_timeout); ", lowering to %i seconds", value, max_timeout, new_timeout);
@ -417,11 +446,11 @@ monitorSetNetworkTimeout(MONITOR *mon, int type, int value) {
static RESULT_ROW * static RESULT_ROW *
monitorRowCallback(RESULTSET *set, void *data) monitorRowCallback(RESULTSET *set, void *data)
{ {
int *rowno = (int *)data; int *rowno = (int *)data;
int i = 0;; int i = 0;;
char buf[20]; char buf[20];
RESULT_ROW *row; RESULT_ROW *row;
MONITOR *ptr; MONITOR *ptr;
spinlock_acquire(&monLock); spinlock_acquire(&monLock);
ptr = allMonitors; ptr = allMonitors;
@ -453,11 +482,13 @@ MONITOR *ptr;
RESULTSET * RESULTSET *
monitorGetList() monitorGetList()
{ {
RESULTSET *set; RESULTSET *set;
int *data; int *data;
if ((data = (int *)malloc(sizeof(int))) == NULL) if ((data = (int *)malloc(sizeof(int))) == NULL)
{
return NULL; return NULL;
}
*data = 0; *data = 0;
if ((set = resultset_create(monitorRowCallback, data)) == NULL) if ((set = resultset_create(monitorRowCallback, data)) == NULL)
{ {
@ -489,7 +520,7 @@ bool check_monitor_permissions(MONITOR* monitor)
user = monitor->user; user = monitor->user;
dpasswd = decryptPassword(monitor->password); dpasswd = decryptPassword(monitor->password);
if((mysql = mysql_init(NULL)) == NULL) if ((mysql = mysql_init(NULL)) == NULL)
{ {
MXS_ERROR("[%s] Error: MySQL connection initialization failed.", __FUNCTION__); MXS_ERROR("[%s] Error: MySQL connection initialization failed.", __FUNCTION__);
free(dpasswd); free(dpasswd);
@ -502,7 +533,7 @@ bool check_monitor_permissions(MONITOR* monitor)
/** Connect to the first server. This assumes all servers have identical /** Connect to the first server. This assumes all servers have identical
* user permissions. */ * user permissions. */
if(mysql_real_connect(mysql,server->name,user,dpasswd,NULL,server->port,NULL,0) == NULL) if (mysql_real_connect(mysql, server->name, user, dpasswd, NULL, server->port, NULL, 0) == NULL)
{ {
MXS_ERROR("%s: Failed to connect to server %s(%s:%d) when" MXS_ERROR("%s: Failed to connect to server %s(%s:%d) when"
" checking monitor user credentials and permissions.", " checking monitor user credentials and permissions.",
@ -515,9 +546,9 @@ bool check_monitor_permissions(MONITOR* monitor)
return false; return false;
} }
if(mysql_query(mysql,"show slave status") != 0) if (mysql_query(mysql,"show slave status") != 0)
{ {
if(mysql_errno(mysql) == ER_SPECIFIC_ACCESS_DENIED_ERROR) if (mysql_errno(mysql) == ER_SPECIFIC_ACCESS_DENIED_ERROR)
{ {
MXS_ERROR("%s: User '%s' is missing REPLICATION CLIENT privileges. MySQL error message: %s", MXS_ERROR("%s: User '%s' is missing REPLICATION CLIENT privileges. MySQL error message: %s",
monitor->name,user,mysql_error(mysql)); monitor->name,user,mysql_error(mysql));
@ -531,7 +562,7 @@ bool check_monitor_permissions(MONITOR* monitor)
} }
else else
{ {
if((res = mysql_use_result(mysql)) == NULL) if ((res = mysql_use_result(mysql)) == NULL)
{ {
MXS_ERROR("%s: Result retrieval failed when checking for REPLICATION CLIENT permissions: %s", MXS_ERROR("%s: Result retrieval failed when checking for REPLICATION CLIENT permissions: %s",
monitor->name,mysql_error(mysql)); monitor->name,mysql_error(mysql));
@ -600,13 +631,15 @@ monitor_clear_pending_status(MONITOR_SERVERS *ptr, int bit)
monitor_event_t monitor_event_t
mon_get_event_type(MONITOR_SERVERS* node) mon_get_event_type(MONITOR_SERVERS* node)
{ {
typedef enum { typedef enum
{
DOWN_EVENT, DOWN_EVENT,
UP_EVENT, UP_EVENT,
LOSS_EVENT, LOSS_EVENT,
NEW_EVENT, NEW_EVENT,
UNSUPPORTED_EVENT UNSUPPORTED_EVENT
} general_event_type; } general_event_type;
general_event_type event_type = UNSUPPORTED_EVENT; general_event_type event_type = UNSUPPORTED_EVENT;
unsigned int prev = node->mon_prev_status unsigned int prev = node->mon_prev_status
@ -700,7 +733,7 @@ mon_get_event_name(MONITOR_SERVERS* node)
* @result monitor_event_t Monitor event corresponding to name * @result monitor_event_t Monitor event corresponding to name
*/ */
monitor_event_t monitor_event_t
mon_name_to_event (const char *event_name) mon_name_to_event(const char *event_name)
{ {
monitor_event_t event; monitor_event_t event;

View File

@ -73,7 +73,8 @@
* unregisterServer is called to remove a server from the set of servers that need to be * unregisterServer is called to remove a server from the set of servers that need to be
* monitored. * monitored.
*/ */
typedef struct { typedef struct
{
void *(*startMonitor)(void *, void*); void *(*startMonitor)(void *, void*);
void (*stopMonitor)(void *); void (*stopMonitor)(void *);
void (*diagnostics)(DCB *, void *); void (*diagnostics)(DCB *, void *);
@ -158,34 +159,37 @@ extern const monitor_def_t monitor_event_definitions[];
/** /**
* The linked list of servers that are being monitored by the monitor module. * The linked list of servers that are being monitored by the monitor module.
*/ */
typedef struct monitor_servers { typedef struct monitor_servers
{
SERVER *server; /**< The server being monitored */ SERVER *server; /**< The server being monitored */
MYSQL *con; /**< The MySQL connection */ MYSQL *con; /**< The MySQL connection */
bool log_version_err; bool log_version_err;
int mon_err_count; int mon_err_count;
unsigned int mon_prev_status; unsigned int mon_prev_status;
unsigned int pending_status; /**< Pending Status flag bitmap */ unsigned int pending_status; /**< Pending Status flag bitmap */
struct monitor_servers struct monitor_servers *next; /**< The next server in the list */
*next; /**< The next server in the list */
} MONITOR_SERVERS; } MONITOR_SERVERS;
/** /**
* Representation of the running monitor. * Representation of the running monitor.
*/ */
typedef struct monitor { typedef struct monitor
{
char *name; /**< The name of the monitor module */ char *name; /**< The name of the monitor module */
char* user; /*< Monitor username */ char *user; /*< Monitor username */
char* password; /*< Monitor password */ char *password; /*< Monitor password */
SPINLOCK lock; SPINLOCK lock;
CONFIG_PARAMETER* parameters; /*< configuration parameters */ CONFIG_PARAMETER* parameters; /*< configuration parameters */
MONITOR_SERVERS* databases; /*< List of databases the monitor monitors */ MONITOR_SERVERS* databases; /*< List of databases the monitor monitors */
monitor_state_t state; /**< The state of the monitor */ monitor_state_t state; /**< The state of the monitor */
int connect_timeout; /**< Connect timeout in seconds for mysql_real_connect */ int connect_timeout; /**< Connect timeout in seconds for mysql_real_connect */
int read_timeout; /**< Timeout in seconds to read from the server. 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. * 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. 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. * There are retries and the total effective timeout value is
* two times the option value.
*/ */
MONITOR_OBJECT *module; /**< The "monitor object" */ MONITOR_OBJECT *module; /**< The "monitor object" */
void *handle; /**< Handle returned from startMonitor */ void *handle; /**< Handle returned from startMonitor */