Move monitor script processing and launching into the core

This removes parts of the nearly identical code from all monitors.

The removal of monitor type specific event checking is done based on the
assumption that only the monitor that is monitoring the server can be the
cause for a state change. This removes the need to actually check that the
state change is relevant for each monitor and allows the event handling to
be moved into the core.
This commit is contained in:
Markus Mäkelä
2017-01-11 11:11:11 +02:00
parent 7ef5e4bfd9
commit a196420c2d
11 changed files with 206 additions and 680 deletions

View File

@ -125,6 +125,19 @@ MXS_MODULE* MXS_CREATE_MODULE()
{"multimaster", MXS_MODULE_PARAM_BOOL, "false"},
{"failover", MXS_MODULE_PARAM_BOOL, "false"},
{"failcount", MXS_MODULE_PARAM_COUNT, "5"},
{
"script",
MXS_MODULE_PARAM_PATH,
NULL,
MXS_MODULE_OPT_PATH_X_OK
},
{
"events",
MXS_MODULE_PARAM_ENUM,
MONITOR_EVENT_DEFAULT_VALUE,
MXS_MODULE_OPT_NONE,
monitor_event_enum_values
},
{MXS_END_MODULE_PARAMS}
}
};
@ -229,11 +242,11 @@ static void *
startMonitor(MONITOR *monitor, const CONFIG_PARAMETER* params)
{
MYSQL_MONITOR *handle = (MYSQL_MONITOR*) monitor->handle;
bool have_events = false, script_error = false, error = false;
if (handle)
{
handle->shutdown = 0;
MXS_FREE(handle->script);
}
else
{
@ -252,9 +265,7 @@ startMonitor(MONITOR *monitor, const CONFIG_PARAMETER* params)
handle->server_info = server_info;
handle->shutdown = 0;
handle->id = config_get_gateway_id();
handle->script = NULL;
handle->warn_failover = true;
memset(handle->events, false, sizeof(handle->events));
spinlock_init(&handle->lock);
}
@ -268,34 +279,10 @@ startMonitor(MONITOR *monitor, const CONFIG_PARAMETER* params)
handle->failover = config_get_bool(params, "failover");
handle->failcount = config_get_integer(params, "failcount");
handle->mysql51_replication = config_get_bool(params, "mysql51_replication");
handle->script = config_copy_string(params, "script");
handle->events = config_get_enum(params, "events", monitor_event_enum_values);
while (params)
{
if (!strcmp(params->name, "script"))
{
if (externcmd_can_execute(params->value))
{
MXS_FREE(handle->script);
handle->script = MXS_STRDUP_A(params->value);
}
else
{
script_error = true;
}
}
else if (!strcmp(params->name, "events"))
{
if (mon_parse_event_string((bool *)handle->events, sizeof(handle->events), params->value) != 0)
{
script_error = true;
}
else
{
have_events = true;
}
}
params = params->next;
}
bool error = false;
if (!check_monitor_permissions(monitor, "SHOW SLAVE STATUS"))
{
@ -303,17 +290,6 @@ startMonitor(MONITOR *monitor, const CONFIG_PARAMETER* params)
error = true;
}
if (script_error)
{
MXS_ERROR("[%s] Errors were found in the script configuration parameters ", monitor->name);
error = true;
}
else if (!have_events)
{
/** If no specific events are given, enable them all */
memset(handle->events, true, sizeof(handle->events));
}
if (!init_server_info(handle, monitor->databases))
{
error = true;
@ -1330,25 +1306,11 @@ monitorMain(void *arg)
}
}
ptr = mon->databases;
monitor_event_t evtype;
while (ptr)
{
/** Execute monitor script if a server state has changed */
if (mon_status_changed(ptr))
{
evtype = mon_get_event_type(ptr);
if (isMySQLEvent(evtype))
{
mon_log_state_change(ptr);
if (handle->script && handle->events[evtype])
{
monitor_launch_script(mon, ptr, handle->script);
}
}
}
ptr = ptr->next;
}
/**
* After updating the status of all servers, check if monitor events
* need to be launched.
*/
mon_process_state_changes(mon, handle->script, handle->events);
/* log master detection failure of first master becomes available after failure */
if (root_master &&
@ -1878,39 +1840,6 @@ static int add_slave_to_master(long *slaves_list, int list_size, long node_id)
return 0;
}
static monitor_event_t mysql_events[] =
{
MASTER_DOWN_EVENT,
MASTER_UP_EVENT,
SLAVE_DOWN_EVENT,
SLAVE_UP_EVENT,
SERVER_DOWN_EVENT,
SERVER_UP_EVENT,
LOST_MASTER_EVENT,
LOST_SLAVE_EVENT,
NEW_MASTER_EVENT,
NEW_SLAVE_EVENT,
MAX_MONITOR_EVENT
};
/**
* Check if the MySQL monitor is monitoring this event type.
* @param event Event to check
* @return True if the event is monitored, false if it is not
* */
bool isMySQLEvent(monitor_event_t event)
{
int i;
for (i = 0; mysql_events[i] != MAX_MONITOR_EVENT; i++)
{
if (event == mysql_events[i])
{
return true;
}
}
return false;
}
/**
* Check if replicate_ignore_table is defined and if maxscale_schema.replication_hearbeat
* table is in the list.