Mmmon formatting changes

Fixed indentation, bracket alignment and other minor things.
This commit is contained in:
Markus Makela
2015-11-16 14:03:42 +02:00
parent 797d9dc03d
commit 6846b0b6b6

View File

@ -37,21 +37,23 @@ static void monitorMain(void *);
static char *version_str = "V1.1.1";
MODULE_INFO info = {
MODULE_INFO info =
{
MODULE_API_MONITOR,
MODULE_BETA_RELEASE,
MONITOR_VERSION,
"A Multi-Master Multi Master monitor"
};
static void *startMonitor(void *,void*);
static void *startMonitor(void *, void*);
static void stopMonitor(void *);
static void diagnostics(DCB *, void *);
static void detectStaleMaster(void *, int);
static MONITOR_SERVERS *get_current_master(MONITOR *);
bool isMySQLEvent(monitor_event_t event);
static MONITOR_OBJECT MyObject = {
static MONITOR_OBJECT MyObject =
{
startMonitor,
stopMonitor,
diagnostics
@ -104,12 +106,12 @@ GetModuleObject()
* @return A handle to use when interacting with the monitor
*/
static void *
startMonitor(void *arg,void* opt)
startMonitor(void *arg, void* opt)
{
MONITOR* mon = (MONITOR*)arg;
MONITOR* mon = (MONITOR*) arg;
MM_MONITOR *handle = mon->handle;
CONFIG_PARAMETER* params = (CONFIG_PARAMETER*)opt;
bool have_events = false,script_error = false;
CONFIG_PARAMETER* params = (CONFIG_PARAMETER*) opt;
bool have_events = false, script_error = false;
if (handle)
{
@ -117,24 +119,26 @@ startMonitor(void *arg,void* opt)
}
else
{
if ((handle = (MM_MONITOR *)malloc(sizeof(MM_MONITOR))) == NULL)
if ((handle = (MM_MONITOR *) malloc(sizeof(MM_MONITOR))) == NULL)
{
return NULL;
}
handle->shutdown = 0;
handle->id = MONITOR_DEFAULT_ID;
handle->master = NULL;
handle->script = NULL;
handle->detectStaleMaster = false;
memset(handle->events,false,sizeof(handle->events));
memset(handle->events, false, sizeof(handle->events));
spinlock_init(&handle->lock);
}
while(params)
while (params)
{
if(!strcmp(params->name,"detect_stale_master"))
if (!strcmp(params->name, "detect_stale_master"))
{
handle->detectStaleMaster = config_truth_value(params->value);
}
else if(!strcmp(params->name,"script"))
else if (!strcmp(params->name, "script"))
{
if (externcmd_can_execute(params->value))
{
@ -146,28 +150,33 @@ startMonitor(void *arg,void* opt)
script_error = true;
}
}
else if(!strcmp(params->name,"events"))
else if (!strcmp(params->name, "events"))
{
if (mon_parse_event_string((bool*) & handle->events,
sizeof(handle->events), params->value) != 0)
{
if(mon_parse_event_string((bool*)&handle->events,sizeof(handle->events),params->value) != 0)
script_error = true;
}
else
{
have_events = true;
}
}
params = params->next;
}
if(script_error)
if (script_error)
{
skygw_log_write(LE,"Error: Errors were found in the script configuration parameters "
"for the monitor '%s'. The script will not be used.",mon->name);
skygw_log_write(LE, "Error: Errors were found in the script configuration parameters "
"for the monitor '%s'. The script will not be used.", mon->name);
free(handle->script);
handle->script = NULL;
}
/** If no specific events are given, enable them all */
if(!have_events)
if (!have_events)
{
memset(handle->events,true,sizeof(handle->events));
memset(handle->events, true, sizeof(handle->events));
}
handle->tid = (THREAD)thread_start(monitorMain, mon);
handle->tid = (THREAD) thread_start(monitorMain, mon);
return handle;
}
@ -180,10 +189,10 @@ static void
stopMonitor(void *arg)
{
MONITOR* mon = arg;
MM_MONITOR *handle = (MM_MONITOR *)mon->handle;
MM_MONITOR *handle = (MM_MONITOR *) mon->handle;
handle->shutdown = 1;
thread_wait((void *)handle->tid);
thread_wait((void *) handle->tid);
}
/**
@ -194,10 +203,10 @@ stopMonitor(void *arg)
*/
static void diagnostics(DCB *dcb, void *arg)
{
MONITOR* mon = (MONITOR*)arg;
MM_MONITOR *handle = (MM_MONITOR *)mon->handle;
MONITOR_SERVERS *db;
char *sep;
MONITOR* mon = (MONITOR*) arg;
MM_MONITOR *handle = (MM_MONITOR *) mon->handle;
MONITOR_SERVERS *db;
char *sep;
switch (handle->status)
{
@ -212,8 +221,8 @@ char *sep;
break;
}
dcb_printf(dcb,"\tSampling interval:\t%lu milliseconds\n", mon->interval);
dcb_printf(dcb,"\tDetect Stale Master:\t%s\n", (handle->detectStaleMaster == 1) ? "enabled" : "disabled");
dcb_printf(dcb, "\tSampling interval:\t%lu milliseconds\n", mon->interval);
dcb_printf(dcb, "\tDetect Stale Master:\t%s\n", (handle->detectStaleMaster == 1) ? "enabled" : "disabled");
dcb_printf(dcb, "\tMonitored servers: ");
db = mon->databases;
@ -240,16 +249,18 @@ char *sep;
static void
monitorDatabase(MONITOR* mon, MONITOR_SERVERS *database)
{
MYSQL_ROW row;
MYSQL_RES *result;
int isslave = 0;
int ismaster = 0;
unsigned long int server_version = 0;
char *server_string;
MYSQL_ROW row;
MYSQL_RES *result;
int isslave = 0;
int ismaster = 0;
unsigned long int server_version = 0;
char *server_string;
/* Don't probe servers in maintenance mode */
if (SERVER_IN_MAINT(database->server))
{
return;
}
/** Store previous status */
database->mon_prev_status = database->server->status;
@ -295,7 +306,7 @@ char *server_string;
server_version = mysql_get_server_version(database->con);
/* get server version string */
server_string = (char *)mysql_get_server_info(database->con);
server_string = (char *) mysql_get_server_info(database->con);
if (server_string)
{
server_set_version_string(database->server, server_string);
@ -307,11 +318,11 @@ char *server_string;
{
long server_id = -1;
if(mysql_field_count(database->con) != 1)
if (mysql_field_count(database->con) != 1)
{
mysql_free_result(result);
skygw_log_write(LE,"Error: Unexpected result for 'SELECT @@server_id'. Expected 1 column."
" MySQL Version: %s",version_str);
skygw_log_write(LE, "Error: Unexpected result for 'SELECT @@server_id'. Expected 1 column."
" MySQL Version: %s", version_str);
return;
}
@ -333,7 +344,8 @@ char *server_string;
*/
/* Check first for MariaDB 10.x.x and get status for multimaster replication */
if (server_version >= 100000) {
if (server_version >= 100000)
{
if (mysql_query(database->con, "SHOW ALL SLAVES STATUS") == 0
&& (result = mysql_store_result(database->con)) != NULL)
@ -341,12 +353,12 @@ char *server_string;
int i = 0;
long master_id = -1;
if(mysql_field_count(database->con) < 42)
if (mysql_field_count(database->con) < 42)
{
mysql_free_result(result);
skygw_log_write(LE,"Error: \"SHOW ALL SLAVES STATUS\" "
skygw_log_write(LE, "Error: \"SHOW ALL SLAVES STATUS\" "
"returned less than the expected amount of columns. Expected 42 columns"
" MySQL Version: %s",version_str);
" MySQL Version: %s", version_str);
return;
}
@ -354,7 +366,8 @@ char *server_string;
{
/* get Slave_IO_Running and Slave_SQL_Running values*/
if (strncmp(row[12], "Yes", 3) == 0
&& strncmp(row[13], "Yes", 3) == 0) {
&& strncmp(row[13], "Yes", 3) == 0)
{
isslave += 1;
}
@ -363,12 +376,15 @@ char *server_string;
* root master server.
* Please note, there could be no slaves at all if Slave_SQL_Running == 'No'
*/
if (strncmp(row[12], "Yes", 3) == 0) {
if (strncmp(row[12], "Yes", 3) == 0)
{
/* get Master_Server_Id values */
master_id = atol(row[41]);
if (master_id == 0)
{
master_id = -1;
}
}
i++;
}
@ -379,36 +395,42 @@ char *server_string;
/* If all configured slaves are running set this node as slave */
if (isslave > 0 && isslave == i)
{
isslave = 1;
}
else
{
isslave = 0;
}
} else {
}
}
else
{
if (mysql_query(database->con, "SHOW SLAVE STATUS") == 0
&& (result = mysql_store_result(database->con)) != NULL)
{
long master_id = -1;
if(mysql_field_count(database->con) < 40)
if (mysql_field_count(database->con) < 40)
{
mysql_free_result(result);
if(server_version < 5*10000 + 5*100)
if (server_version < 5 * 10000 + 5 * 100)
{
if(database->log_version_err)
if (database->log_version_err)
{
skygw_log_write(LE,"Error: \"SHOW SLAVE STATUS\" "
skygw_log_write(LE, "Error: \"SHOW SLAVE STATUS\" "
" for versions less than 5.5 does not have master_server_id, "
"replication tree cannot be resolved for server %s."
" MySQL Version: %s",database->server->unique_name,version_str);
" MySQL Version: %s", database->server->unique_name, version_str);
database->log_version_err = false;
}
}
else
{
skygw_log_write(LE,"Error: \"SHOW SLAVE STATUS\" "
skygw_log_write(LE, "Error: \"SHOW SLAVE STATUS\" "
"returned less than the expected amount of columns. Expected 40 columns."
" MySQL Version: %s",version_str);
" MySQL Version: %s", version_str);
}
return;
}
@ -417,7 +439,8 @@ char *server_string;
{
/* get Slave_IO_Running and Slave_SQL_Running values*/
if (strncmp(row[10], "Yes", 3) == 0
&& strncmp(row[11], "Yes", 3) == 0) {
&& strncmp(row[11], "Yes", 3) == 0)
{
isslave = 1;
}
@ -426,13 +449,16 @@ char *server_string;
* root master server.
* Please note, there could be no slaves at all if Slave_SQL_Running == 'No'
*/
if (strncmp(row[10], "Yes", 3) == 0) {
if (strncmp(row[10], "Yes", 3) == 0)
{
/* get Master_Server_Id values */
master_id = atol(row[39]);
if (master_id == 0)
{
master_id = -1;
}
}
}
/* store master_id of current node */
memcpy(&database->server->master_id, &master_id, sizeof(long));
@ -444,19 +470,22 @@ char *server_string;
if (mysql_query(database->con, "SHOW GLOBAL VARIABLES LIKE 'read_only'") == 0
&& (result = mysql_store_result(database->con)) != NULL)
{
if(mysql_field_count(database->con) < 2)
if (mysql_field_count(database->con) < 2)
{
mysql_free_result(result);
skygw_log_write(LE,"Error: Unexpected result for \"SHOW GLOBAL VARIABLES LIKE 'read_only'\". Expected 2 columns."
" MySQL Version: %s",version_str);
skygw_log_write(LE, "Error: Unexpected result for \"SHOW GLOBAL VARIABLES LIKE 'read_only'\". Expected 2 columns."
" MySQL Version: %s", version_str);
return;
}
while ((row = mysql_fetch_row(result)))
{
if (strncasecmp(row[1], "OFF", 3) == 0) {
if (strncasecmp(row[1], "OFF", 3) == 0)
{
ismaster = 1;
} else {
}
else
{
isslave = 1;
}
}
@ -475,7 +504,9 @@ char *server_string;
/* Set replication depth to 1 */
database->server->depth = 1;
} else {
}
else
{
/* Avoid any possible Master/Slave stale state */
monitor_clear_pending_status(database, SERVER_SLAVE);
monitor_clear_pending_status(database, SERVER_MASTER);
@ -501,17 +532,17 @@ char *server_string;
static void
monitorMain(void *arg)
{
MONITOR* mon = (MONITOR*)arg;
MM_MONITOR *handle;
MONITOR_SERVERS *ptr;
int detect_stale_master = false;
MONITOR_SERVERS *root_master = NULL;
size_t nrounds = 0;
MONITOR* mon = (MONITOR*) arg;
MM_MONITOR *handle;
MONITOR_SERVERS *ptr;
int detect_stale_master = false;
MONITOR_SERVERS *root_master = NULL;
size_t nrounds = 0;
spinlock_acquire(&mon->lock);
handle = (MM_MONITOR *)mon->handle;
spinlock_release(&mon->lock);
detect_stale_master = handle->detectStaleMaster;
spinlock_acquire(&mon->lock);
handle = (MM_MONITOR *) mon->handle;
spinlock_release(&mon->lock);
detect_stale_master = handle->detectStaleMaster;
if (mysql_thread_init())
{
@ -542,7 +573,7 @@ detect_stale_master = handle->detectStaleMaster;
* round.
*/
if (nrounds != 0 &&
((nrounds*MON_BASE_INTERVAL_MS)%mon->interval) >=
((nrounds * MON_BASE_INTERVAL_MS) % mon->interval) >=
MON_BASE_INTERVAL_MS)
{
nrounds += 1;
@ -598,15 +629,19 @@ detect_stale_master = handle->detectStaleMaster;
ptr = mon->databases;
while (ptr)
{
if (! SERVER_IN_MAINT(ptr->server)) {
if (!SERVER_IN_MAINT(ptr->server))
{
/* If "detect_stale_master" option is On, let's use the previus master */
if (detect_stale_master && root_master && (!strcmp(ptr->server->name, root_master->server->name) && ptr->server->port == root_master->server->port) && (ptr->server->status & SERVER_MASTER) && !(ptr->pending_status & SERVER_MASTER)) {
if (detect_stale_master && root_master && (!strcmp(ptr->server->name, root_master->server->name) && ptr->server->port == root_master->server->port) && (ptr->server->status & SERVER_MASTER) && !(ptr->pending_status & SERVER_MASTER))
{
/* in this case server->status will not be updated from pending_status */
LOGIF(LM, (skygw_log_write_flush(
LOGFILE_MESSAGE, "[mysql_mon]: root server [%s:%i] is no longer Master, let's use it again even if it could be a stale master, you have been warned!", ptr->server->name, ptr->server->port)));
/* Set the STALE bit for this server in server struct */
server_set_status(ptr->server, SERVER_STALE_STATUS);
} else {
}
else
{
ptr->server->status = ptr->pending_status;
}
}
@ -615,20 +650,20 @@ detect_stale_master = handle->detectStaleMaster;
ptr = mon->databases;
monitor_event_t evtype;
while(ptr)
while (ptr)
{
if(mon_status_changed(ptr))
if (mon_status_changed(ptr))
{
evtype = mon_get_event_type(ptr);
if(isMySQLEvent(evtype))
if (isMySQLEvent(evtype))
{
skygw_log_write(LOGFILE_TRACE,"Server changed state: %s[%s:%u]: %s",
skygw_log_write(LOGFILE_TRACE, "Server changed state: %s[%s:%u]: %s",
ptr->server->unique_name,
ptr->server->name,ptr->server->port,
ptr->server->name, ptr->server->port,
mon_get_event_name(ptr));
if(handle->script && handle->events[evtype])
if (handle->script && handle->events[evtype])
{
monitor_launch_script(mon,ptr,handle->script);
monitor_launch_script(mon, ptr, handle->script);
}
}
}
@ -648,8 +683,8 @@ detect_stale_master = handle->detectStaleMaster;
static void
detectStaleMaster(void *arg, int enable)
{
MONITOR* mon = (MONITOR*)arg;
MM_MONITOR *handle = (MM_MONITOR *)mon->handle;
MONITOR* mon = (MONITOR*) arg;
MM_MONITOR *handle = (MM_MONITOR *) mon->handle;
memcpy(&handle->detectStaleMaster, &enable, sizeof(int));
}
@ -663,9 +698,10 @@ MM_MONITOR *handle = (MM_MONITOR *)mon->handle;
* @return The server at root level with SERVER_MASTER bit
*/
static MONITOR_SERVERS *get_current_master(MONITOR *mon) {
static MONITOR_SERVERS *get_current_master(MONITOR *mon)
{
MM_MONITOR* handle = mon->handle;
MONITOR_SERVERS *ptr;
MONITOR_SERVERS *ptr;
ptr = mon->databases;
@ -675,12 +711,14 @@ MONITOR_SERVERS *ptr;
* that means SERVER_IS_RUNNING returns 0
* Let's check only for SERVER_IS_DOWN: server is not running
*/
if (SERVER_IS_DOWN(ptr->server)) {
if (SERVER_IS_DOWN(ptr->server))
{
ptr = ptr->next;
continue;
}
if (ptr->server->depth == 0) {
if (ptr->server->depth == 0)
{
handle->master = ptr;
}
@ -692,14 +730,20 @@ MONITOR_SERVERS *ptr;
* Return the root master
*/
if (handle->master != NULL) {
if (handle->master != NULL)
{
/* If the root master is in MAINT, return NULL */
if (SERVER_IN_MAINT(handle->master->server)) {
if (SERVER_IN_MAINT(handle->master->server))
{
return NULL;
} else {
}
else
{
return handle->master;
}
} else {
}
else
{
return NULL;
}
}
@ -718,6 +762,7 @@ static monitor_event_t mysql_events[] = {
NEW_SLAVE_EVENT,
MAX_MONITOR_EVENT
};
/**
* Check if the MM monitor is monitoring this event type.
* @param event Event to check
@ -726,10 +771,12 @@ static monitor_event_t mysql_events[] = {
bool isMySQLEvent(monitor_event_t event)
{
int i;
for(i = 0;mysql_events[i] != MAX_MONITOR_EVENT;i++)
for (i = 0; mysql_events[i] != MAX_MONITOR_EVENT; i++)
{
if (event == mysql_events[i])
{
if(event == mysql_events[i])
return true;
}
}
return false;
}