From bbe0620944d820512834bf3f9c4cfd3d6fd347be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 18 Apr 2017 04:56:37 +0300 Subject: [PATCH] MXS-1220: Add JSON return value to `diagnostics` entry points The modules that implement a diagnostics entry point now return a JSON type object. This removes the need to format data inside the modules. The module implementations of these are not yet complete which means that MaxScale will fail to compile. --- include/maxscale/authenticator.h | 16 +++++++++++++++- include/maxscale/filter.h | 8 ++++++-- include/maxscale/monitor.h | 32 +++++++++++++++++++++++++++++++- include/maxscale/router.h | 9 ++++++--- server/core/filter.cc | 22 ++++++++++++++++++---- server/core/monitor.cc | 15 ++++++++++++--- server/core/service.cc | 22 ++++++++++++++++++---- server/core/session.cc | 10 +++++++--- 8 files changed, 113 insertions(+), 21 deletions(-) diff --git a/include/maxscale/authenticator.h b/include/maxscale/authenticator.h index 94346c909..a37e4b143 100644 --- a/include/maxscale/authenticator.h +++ b/include/maxscale/authenticator.h @@ -21,6 +21,7 @@ #include #include +#include MXS_BEGIN_DECLS @@ -81,7 +82,20 @@ typedef struct mxs_authenticator void (*free)(struct dcb *); void (*destroy)(void *); int (*loadusers)(struct servlistener *); - void (*diagnostic)(struct dcb*, struct servlistener *); + + /** + * @brief Return diagnostic information about the authenticator + * + * The authenticator module should return information about its internal + * state when this function is called. + * + * @params Listener object + * + * @return JSON representation of the listener + * + * @see jansson.h + */ + json_t* (*diagnostic)(struct servlistener *listener); /** This entry point was added to avoid calling authenticator functions * directly when a COM_CHANGE_USER command is executed. */ diff --git a/include/maxscale/filter.h b/include/maxscale/filter.h index a08a5835f..7a16ee084 100644 --- a/include/maxscale/filter.h +++ b/include/maxscale/filter.h @@ -23,6 +23,7 @@ #include #include #include +#include MXS_BEGIN_DECLS @@ -174,9 +175,12 @@ typedef struct mxs_filter_object * * @param instance Filter instance * @param fsession Filter session, NULL if general information about the filter is queried - * @param dcb DCB where the diagnostic information should be written + * + * @return JSON formatted information about the filter + * + * @see jansson.h */ - void (*diagnostics)(MXS_FILTER *instance, MXS_FILTER_SESSION *fsession, DCB *dcb); + json_t* (*diagnostics)(MXS_FILTER *instance, MXS_FILTER_SESSION *fsession); /** * @brief Called to obtain the capabilities of the filter diff --git a/include/maxscale/monitor.h b/include/maxscale/monitor.h index 7370a06a1..397203f57 100644 --- a/include/maxscale/monitor.h +++ b/include/maxscale/monitor.h @@ -23,6 +23,7 @@ #include #include #include +#include MXS_BEGIN_DECLS @@ -54,9 +55,38 @@ typedef struct mxs_monitor MXS_MONITOR; */ typedef struct mxs_monitor_object { + /** + * @brief Start the monitor + * + * This entry point is called when the monitor is started. If the monitor + * requires polling of the servers, it should create a separate monitoring + * thread. + * + * @param monitor The monitor object + * @param params Parameters for this monitor + * + * @return Pointer to the monitor specific data, stored in @c monitor->handle + */ void *(*startMonitor)(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER *params); + + /** + * @brief Stop the monitor + * + * This entry point is called when the monitor is stopped. If the monitor + * uses a polling thread, the thread should be stopped. + * + * @param monitor The monitor object + */ void (*stopMonitor)(MXS_MONITOR *monitor); - void (*diagnostics)(DCB *, const MXS_MONITOR *); + + /** + * @brief Return diagnostic information about the monitor + * + * @return A JSON object representing the state of the monitor + * + * @see jansson.h + */ + json_t* (*diagnostics)(const MXS_MONITOR *monitor); } MXS_MONITOR_OBJECT; /** diff --git a/include/maxscale/router.h b/include/maxscale/router.h index 0b29ad02f..96a2edcbe 100644 --- a/include/maxscale/router.h +++ b/include/maxscale/router.h @@ -24,6 +24,7 @@ #include #include #include +#include MXS_BEGIN_DECLS @@ -144,14 +145,16 @@ typedef struct mxs_router_object */ int32_t (*routeQuery)(MXS_ROUTER *instance, MXS_ROUTER_SESSION *router_session, GWBUF *queue); - /** * @brief Called for diagnostic output * * @param instance Router instance - * @param dcb DCB where the diagnostic information should be written + * + * @return Diagnostic information in JSON format + * + * @see jansson.h */ - void (*diagnostics)(MXS_ROUTER *instance, DCB *dcb); + json_t* (*diagnostics)(MXS_ROUTER *instance); /** * @brief Called for each reply packet diff --git a/server/core/filter.cc b/server/core/filter.cc index 566e0ee56..7df505163 100644 --- a/server/core/filter.cc +++ b/server/core/filter.cc @@ -206,7 +206,12 @@ dprintAllFilters(DCB *dcb) } if (ptr->obj && ptr->filter) { - ptr->obj->diagnostics(ptr->filter, NULL, dcb); + json_t* json = ptr->obj->diagnostics(ptr->filter, NULL); + + if (json) + { + json_decref(json); + } } else { @@ -241,7 +246,12 @@ dprintFilter(DCB *dcb, const MXS_FILTER_DEF *filter) } if (filter->obj && filter->filter) { - filter->obj->diagnostics(filter->filter, NULL, dcb); + json_t* json = filter->obj->diagnostics(filter->filter, NULL); + + if (json) + { + json_decref(json); + } } } @@ -487,8 +497,12 @@ json_t* filter_to_json(const MXS_FILTER_DEF* filter) if (filter->obj && filter->filter) { - // TODO: Add filter diagnostics - //filter->obj->diagnostics(filter->filter, NULL, dcb); + json_t* diag = filter->obj->diagnostics(filter->filter, NULL); + + if (diag) + { + json_object_set_new(rval, "filter_diagnostics", diag); + } } return rval; diff --git a/server/core/monitor.cc b/server/core/monitor.cc index b9a5d2683..87f773124 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -482,7 +482,12 @@ monitorShow(DCB *dcb, MXS_MONITOR *monitor) { if (monitor->module->diagnostics) { - monitor->module->diagnostics(dcb, monitor); + json_t* json = monitor->module->diagnostics(monitor); + + if (json) + { + json_decref(json); + } } else { @@ -1563,8 +1568,12 @@ json_t* monitor_to_json(const MXS_MONITOR* monitor) if (monitor->handle && monitor->module->diagnostics) { - // TODO: Add monitor diagnostics - //monitor->module->diagnostics(dcb, monitor); + json_t* diag = monitor->module->diagnostics(monitor); + + if (diag) + { + json_object_set_new(rval, "monitor_diagnostics", diag); + } } return rval; diff --git a/server/core/service.cc b/server/core/service.cc index 4212378ca..7aa15e460 100644 --- a/server/core/service.cc +++ b/server/core/service.cc @@ -1439,7 +1439,12 @@ void dprintService(DCB *dcb, SERVICE *service) } if (service->router && service->router_instance) { - service->router->diagnostics(service->router_instance, dcb); + json_t* json = service->router->diagnostics(service->router_instance); + + if (json) + { + json_decref(json); + } } dcb_printf(dcb, "\tStarted: %s", asctime_r(localtime_r(&service->stats.started, &result), timebuf)); @@ -2317,7 +2322,12 @@ void service_print_users(DCB *dcb, const SERVICE *service) { if (port->listener && port->listener->authfunc.diagnostic) { - port->listener->authfunc.diagnostic(dcb, port); + json_t* json = port->listener->authfunc.diagnostic(port); + + if (json) + { + json_decref(json); + } } } } @@ -2382,8 +2392,12 @@ json_t* service_to_json(const SERVICE* service) if (service->router && service->router_instance) { - // TODO: Add router diagnostics - //service->router->diagnostics(service->router_instance, dcb); + json_t* diag = service->router->diagnostics(service->router_instance); + + if (diag) + { + json_object_set_new(rval, "router_diagnostics", diag); + } } struct tm result; diff --git a/server/core/session.cc b/server/core/session.cc index 366821f9c..b62207a40 100644 --- a/server/core/session.cc +++ b/server/core/session.cc @@ -530,9 +530,13 @@ dprintSession(DCB *dcb, MXS_SESSION *print_session) { dcb_printf(dcb, "\tFilter: %s\n", print_session->filters[i].filter->name); - print_session->filters[i].filter->obj->diagnostics(print_session->filters[i].instance, - print_session->filters[i].session, - dcb); + json_t* json = print_session->filters[i].filter->obj->diagnostics(print_session->filters[i].instance, + print_session->filters[i].session); + + if (json) + { + json_decref(json); + } } } }