From 3f4d6391b519f075a23ae5f48473b703f1c88e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Fri, 18 May 2018 13:53:03 +0300 Subject: [PATCH] MXS-553: Add diagnostics_json to protocol API The protocol now allows protocol modules to return JSON formatted information about the protocol module internals. Currently this is only implemented by the mariadbbackend module and it returns the current connection ID on the backend server. --- examples/testprotocol.c | 1 + include/maxscale/protocol.h | 10 ++++++++++ server/core/dcb.cc | 7 +++++++ server/modules/protocol/CDC/cdc.cc | 1 + server/modules/protocol/HTTPD/httpd.cc | 3 ++- .../protocol/MySQL/mariadbbackend/mysql_backend.cc | 12 +++++++++++- .../protocol/MySQL/mariadbclient/mysql_client.cc | 1 + server/modules/protocol/maxscaled/maxscaled.cc | 3 ++- server/modules/protocol/telnetd/telnetd.cc | 3 ++- 9 files changed, 37 insertions(+), 4 deletions(-) diff --git a/examples/testprotocol.c b/examples/testprotocol.c index 06aab3e57..8b100685d 100644 --- a/examples/testprotocol.c +++ b/examples/testprotocol.c @@ -107,6 +107,7 @@ MXS_MODULE* MXS_CREATE_MODULE() test_auth, /**< Authentication */ test_default_auth, /**< Default authenticator */ test_connection_limit, /**< Connection limit */ + NULL, NULL }; diff --git a/include/maxscale/protocol.h b/include/maxscale/protocol.h index 28263b159..810e932b6 100644 --- a/include/maxscale/protocol.h +++ b/include/maxscale/protocol.h @@ -20,6 +20,7 @@ #include #include +#include MXS_BEGIN_DECLS @@ -169,6 +170,15 @@ typedef struct mxs_protocol */ bool (*established)(struct dcb* ); + /** + * Provide JSON formatted diagnostics about a DCB + * + * @param dcb DCB to diagnose + * + * @return JSON representation of the DCB + */ + json_t* (*diagnostics_json)(struct dcb* dcb); + } MXS_PROTOCOL; /** diff --git a/server/core/dcb.cc b/server/core/dcb.cc index 8bafe0336..26088c882 100644 --- a/server/core/dcb.cc +++ b/server/core/dcb.cc @@ -3759,5 +3759,12 @@ json_t* dcb_to_json(DCB* dcb) json_object_set_new(obj, "id", json_string(buf)); json_object_set_new(obj, "server", json_string(dcb->server->name)); + if (dcb->func.diagnostics_json) + { + json_t* json = dcb->func.diagnostics_json(dcb); + ss_dassert(json); + json_object_set_new(obj, "protocol_diagnostics", json); + } + return obj; } diff --git a/server/modules/protocol/CDC/cdc.cc b/server/modules/protocol/CDC/cdc.cc index 74e0dc3ad..b6af56b21 100644 --- a/server/modules/protocol/CDC/cdc.cc +++ b/server/modules/protocol/CDC/cdc.cc @@ -89,6 +89,7 @@ MXS_MODULE* MXS_CREATE_MODULE() cdc_default_auth, /* default authentication */ NULL, NULL, + NULL, }; static MXS_MODULE info = diff --git a/server/modules/protocol/HTTPD/httpd.cc b/server/modules/protocol/HTTPD/httpd.cc index cdac18355..17f444351 100644 --- a/server/modules/protocol/HTTPD/httpd.cc +++ b/server/modules/protocol/HTTPD/httpd.cc @@ -83,7 +83,8 @@ MXS_MODULE* MXS_CREATE_MODULE() NULL, /**< Authentication */ httpd_default_auth, /**< Default authenticator */ NULL, /**< Connection limit reached */ - NULL + NULL, + NULL, }; static MXS_MODULE info = diff --git a/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc b/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc index 64901cbfd..4d8eaa28d 100644 --- a/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc +++ b/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc @@ -57,6 +57,7 @@ static void gw_send_proxy_protocol_header(DCB *backend_dcb); static bool get_ip_string_and_port(struct sockaddr_storage *sa, char *ip, int iplen, in_port_t *port_out); static bool gw_connection_established(DCB* dcb); +json_t* gw_json_diagnostics(DCB* dcb); extern "C" { @@ -84,7 +85,8 @@ MXS_MODULE* MXS_CREATE_MODULE() gw_change_user, /* Authentication */ gw_backend_default_auth, /* Default authenticator */ NULL, /* Connection limit reached */ - gw_connection_established + gw_connection_established, + gw_json_diagnostics, }; static MXS_MODULE info = @@ -2199,3 +2201,11 @@ static bool gw_connection_established(DCB* dcb) (proto->ignore_replies == 0) && !proto->stored_query; } + +json_t* gw_json_diagnostics(DCB* dcb) +{ + MySQLProtocol* proto = static_cast(dcb->protocol); + json_t* obj = json_object(); + json_object_set_new(obj, "connection_id", json_integer(proto->thread_id)); + return obj; +} diff --git a/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc b/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc index a8b59e7d9..9b6d3a1f1 100644 --- a/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc +++ b/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc @@ -107,6 +107,7 @@ extern "C" NULL, /* Authentication */ gw_default_auth, /* Default authenticator */ gw_connection_limit, /* Send error connection limit */ + NULL, NULL }; diff --git a/server/modules/protocol/maxscaled/maxscaled.cc b/server/modules/protocol/maxscaled/maxscaled.cc index ad289a285..f7e2c4e94 100644 --- a/server/modules/protocol/maxscaled/maxscaled.cc +++ b/server/modules/protocol/maxscaled/maxscaled.cc @@ -185,7 +185,8 @@ MXS_MODULE* MXS_CREATE_MODULE() NULL, /**< Authentication */ mxsd_default_auth, /**< Default authenticator */ NULL, /**< Connection limit reached */ - NULL + NULL, + NULL, }; static MXS_MODULE info = diff --git a/server/modules/protocol/telnetd/telnetd.cc b/server/modules/protocol/telnetd/telnetd.cc index 28b8402c9..9ef734184 100644 --- a/server/modules/protocol/telnetd/telnetd.cc +++ b/server/modules/protocol/telnetd/telnetd.cc @@ -104,7 +104,8 @@ MXS_MODULE* MXS_CREATE_MODULE() NULL, /**< Authentication */ telnetd_default_auth, /**< Default authenticator */ NULL, /**< Connection limit reached */ - NULL + NULL, + NULL, }; static MXS_MODULE info =