From 3d2219e8ef63e871209ffb5c2f6a3fbe82ca08f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 19 Apr 2017 11:08:07 +0300 Subject: [PATCH] MXS-1220: Add missing relationships The relationships from servers to services and monitors and filters to services were not implemented. Now each server lists the services and monitors that use it and each filter lists the services that use the filter. This enables the creation of a server and linking of that server to services and monitors in one atomic operation. --- include/maxscale/monitor.h | 10 +++++++ include/maxscale/service.h | 20 +++++++++++++ server/core/filter.cc | 18 +++++++++++- server/core/monitor.cc | 29 +++++++++++++++++++ server/core/server.cc | 26 +++++++++++++++++ server/core/service.cc | 58 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 160 insertions(+), 1 deletion(-) diff --git a/include/maxscale/monitor.h b/include/maxscale/monitor.h index 7aaee2eed..e89947013 100644 --- a/include/maxscale/monitor.h +++ b/include/maxscale/monitor.h @@ -293,4 +293,14 @@ json_t* monitor_to_json(const MXS_MONITOR* monitor, const char* host); */ json_t* monitor_list_to_json(const char* host); +/** + * @brief Get links to monitors that relate to a server + * + * @param server Server to inspect + * @param host Hostname of this server + * + * @return Array of monitor links + */ +json_t* monitor_relations_to_server(const SERVER* server, const char* host); + MXS_END_DECLS diff --git a/include/maxscale/service.h b/include/maxscale/service.h index beba7a5b2..5a0d56390 100644 --- a/include/maxscale/service.h +++ b/include/maxscale/service.h @@ -298,6 +298,26 @@ json_t* service_to_json(const SERVICE* service, const char* host); */ json_t* service_list_to_json(const char* host); +/** + * @brief Get links to services that relate to a server + * + * @param server Server to inspect + * @param host Hostname of this server + * + * @return Array of service links + */ +json_t* service_relations_to_server(const SERVER* server, const char* host); + +/** + * @brief Get links to services that relate to a filter + * + * @param filter Filter to inspect + * @param host Hostname of this server + * + * @return Array of service links + */ +json_t* service_relations_to_filter(const MXS_FILTER_DEF* filter, const char* host); + void dprintAllServices(DCB *dcb); void dprintService(DCB *dcb, SERVICE *service); void dListServices(DCB *dcb); diff --git a/server/core/filter.cc b/server/core/filter.cc index 624c6b8b4..cae496c76 100644 --- a/server/core/filter.cc +++ b/server/core/filter.cc @@ -24,8 +24,9 @@ #include #include #include -#include "maxscale/filter.h" +#include +#include "maxscale/filter.h" #include "maxscale/config.h" #include "maxscale/modules.h" @@ -505,6 +506,21 @@ json_t* filter_to_json(const MXS_FILTER_DEF* filter, const char* host) } } + json_t* rel = json_object(); + + json_t* arr = service_relations_to_filter(filter, host); + + if (json_array_size(arr) > 0) + { + json_object_set_new(rel, "services", arr); + } + else + { + json_decref(arr); + } + + json_object_set_new(rval, "relationships", rel); + return rval; } diff --git a/server/core/monitor.cc b/server/core/monitor.cc index 5cbebe56b..da0d2c210 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -1605,3 +1605,32 @@ json_t* monitor_list_to_json(const char* host) return rval; } + +json_t* monitor_relations_to_server(const SERVER* server, const char* host) +{ + json_t* arr = json_array(); + + spinlock_acquire(&monLock); + + for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next) + { + spinlock_acquire(&mon->lock); + + for (MXS_MONITOR_SERVERS* db = mon->databases; db; db = db->next) + { + if (db->server == server) + { + string m = host; + m += "/monitors/"; + m += mon->name; + json_array_append_new(arr, json_string(m.c_str())); + } + } + + spinlock_release(&mon->lock); + } + + spinlock_release(&monLock); + + return arr; +} diff --git a/server/core/server.cc b/server/core/server.cc index 4fdaa8698..479382671 100644 --- a/server/core/server.cc +++ b/server/core/server.cc @@ -1457,5 +1457,31 @@ json_t* server_to_json(const SERVER* server, const char* host) json_object_set_new(rval, "statictics", stats); + json_t* rel = json_object(); + + json_t* arr = service_relations_to_server(server, host); + + if (json_array_size(arr) > 0) + { + json_object_set_new(rel, "services", arr); + } + else + { + json_decref(arr); + } + + arr = monitor_relations_to_server(server, host); + + if (json_array_size(arr) > 0) + { + json_object_set_new(rel, "monitors", arr); + } + else + { + json_decref(arr); + } + + json_object_set_new(rval, "relationships", rel); + return rval; } diff --git a/server/core/service.cc b/server/core/service.cc index 9281ed29b..c6fe5c19d 100644 --- a/server/core/service.cc +++ b/server/core/service.cc @@ -2502,3 +2502,61 @@ json_t* service_list_to_json(const char* host) return rval; } + +static void add_service_relation(json_t* arr, const char* host, const SERVICE* service) +{ + string svc = host; + svc += "/services/"; + svc += service->name; + json_array_append_new(arr, json_string(svc.c_str())); +} + +json_t* service_relations_to_filter(const MXS_FILTER_DEF* filter, const char* host) +{ + json_t* arr = json_array(); + spinlock_acquire(&service_spin); + + for (SERVICE *service = allServices; service; service = service->next) + { + spinlock_acquire(&service->spin); + + for (int i = 0; i < service->n_filters; i++) + { + if (service->filters[i] == filter) + { + add_service_relation(arr, host, service); + } + } + + spinlock_release(&service->spin); + } + + spinlock_release(&service_spin); + + return arr; +} + +json_t* service_relations_to_server(const SERVER* server, const char* host) +{ + json_t* arr = json_array(); + spinlock_acquire(&service_spin); + + for (SERVICE *service = allServices; service; service = service->next) + { + spinlock_acquire(&service->spin); + + for (SERVER_REF *ref = service->dbref; ref; ref = ref->next) + { + if (ref->server == server) + { + add_service_relation(arr, host, service); + } + } + + spinlock_release(&service->spin); + } + + spinlock_release(&service_spin); + + return arr; +}