From 3631388f755a71e2c5e9a73e8b5f562e8bc559f5 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 17 Oct 2018 13:45:02 +0300 Subject: [PATCH] MXS-1779 Add initial support for classification URL This commit introduces the plumbing support for obtaining classification information of a statement using the REST-API. It introduces a URL like /v1/maxscale/query_classifier/classify?sql=SELECT+1 that in the response will return a JSON object with the information. Subsequent commits will provide the actual information. --- include/maxscale/config.h | 1 + include/maxscale/json_api.h | 27 ++++++++++++------------ server/core/config.cc | 1 + server/core/internal/query_classifier.hh | 10 +++++++++ server/core/query_classifier.cc | 18 ++++++++++++++++ server/core/resource.cc | 9 ++++++++ 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/include/maxscale/config.h b/include/maxscale/config.h index bea9dafc9..a18785363 100644 --- a/include/maxscale/config.h +++ b/include/maxscale/config.h @@ -110,6 +110,7 @@ extern const char CN_AUTH_READ_TIMEOUT[]; extern const char CN_AUTH_WRITE_TIMEOUT[]; extern const char CN_AUTO[]; extern const char CN_CACHE_SIZE[]; +extern const char CN_CLASSIFY[]; extern const char CN_CONNECTION_TIMEOUT[]; extern const char CN_DUMP_LAST_STATEMENTS[]; extern const char CN_DATA[]; diff --git a/include/maxscale/json_api.h b/include/maxscale/json_api.h index 296c75e19..0553b8606 100644 --- a/include/maxscale/json_api.h +++ b/include/maxscale/json_api.h @@ -24,19 +24,20 @@ MXS_BEGIN_DECLS /** Resource endpoints */ -#define MXS_JSON_API_SERVERS "/servers/" -#define MXS_JSON_API_SERVICES "/services/" -#define MXS_JSON_API_FILTERS "/filters/" -#define MXS_JSON_API_MONITORS "/monitors/" -#define MXS_JSON_API_SESSIONS "/sessions/" -#define MXS_JSON_API_MAXSCALE "/maxscale/" -#define MXS_JSON_API_THREADS "/maxscale/threads/" -#define MXS_JSON_API_LOGS "/maxscale/logs/" -#define MXS_JSON_API_TASKS "/maxscale/tasks/" -#define MXS_JSON_API_MODULES "/maxscale/modules/" -#define MXS_JSON_API_QC_STATS "/maxscale/qc_stats/" -#define MXS_JSON_API_QC "/maxscale/query_classifier/" -#define MXS_JSON_API_USERS "/users/" +#define MXS_JSON_API_SERVERS "/servers/" +#define MXS_JSON_API_SERVICES "/services/" +#define MXS_JSON_API_FILTERS "/filters/" +#define MXS_JSON_API_MONITORS "/monitors/" +#define MXS_JSON_API_SESSIONS "/sessions/" +#define MXS_JSON_API_MAXSCALE "/maxscale/" +#define MXS_JSON_API_THREADS "/maxscale/threads/" +#define MXS_JSON_API_LOGS "/maxscale/logs/" +#define MXS_JSON_API_TASKS "/maxscale/tasks/" +#define MXS_JSON_API_MODULES "/maxscale/modules/" +#define MXS_JSON_API_QC_STATS "/maxscale/qc_stats/" +#define MXS_JSON_API_QC "/maxscale/query_classifier/" +#define MXS_JSON_API_QC_CLASSIFY "/maxscale/query_classifier/classify" +#define MXS_JSON_API_USERS "/users/" /** * @brief Create a JSON object diff --git a/server/core/config.cc b/server/core/config.cc index 53b04bd37..b467fc3fe 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -91,6 +91,7 @@ const char CN_AUTH_READ_TIMEOUT[] = "auth_read_timeout"; const char CN_AUTH_WRITE_TIMEOUT[] = "auth_write_timeout"; const char CN_AUTO[] = "auto"; const char CN_CACHE_SIZE[] = "cache_size"; +const char CN_CLASSIFY[] = "classify"; const char CN_CONNECTION_TIMEOUT[] = "connection_timeout"; const char CN_DATA[] = "data"; const char CN_DEFAULT[] = "default"; diff --git a/server/core/internal/query_classifier.hh b/server/core/internal/query_classifier.hh index 2e2f57c6f..7fa83fce4 100644 --- a/server/core/internal/query_classifier.hh +++ b/server/core/internal/query_classifier.hh @@ -56,4 +56,14 @@ std::unique_ptr qc_as_json(const char* zHost); */ bool qc_alter_from_json(json_t* pJson); +/** + * Classify statement + * + * @param zHost The MaxScale host. + * @param statement The statement to be classified. + * + * @return A json object containing information about the statement. + */ +std::unique_ptr qc_classify_as_json(const char* zHost, const std::string& statement); + MXS_END_DECLS diff --git a/server/core/query_classifier.cc b/server/core/query_classifier.cc index 5304719c1..ae79a23d8 100644 --- a/server/core/query_classifier.cc +++ b/server/core/query_classifier.cc @@ -1376,3 +1376,21 @@ bool qc_alter_from_json(json_t* pJson) return rv; } + + +std::unique_ptr qc_classify_as_json(const char* zHost, const std::string& statement) +{ + json_t* pParams = json_object(); + + // TODO: Fill object with classification information. + + json_t* pAttributes = json_object(); + json_object_set_new(pAttributes, CN_PARAMETERS, pParams); + + json_t* pSelf = json_object(); + json_object_set_new(pSelf, CN_ID, json_string(CN_CLASSIFY)); + json_object_set_new(pSelf, CN_TYPE, json_string(CN_CLASSIFY)); + json_object_set_new(pSelf, CN_ATTRIBUTES, pAttributes); + + return std::unique_ptr(mxs_json_resource(zHost, MXS_JSON_API_QC_CLASSIFY, pSelf)); +} diff --git a/server/core/resource.cc b/server/core/resource.cc index d94dc3c91..3666db6f9 100644 --- a/server/core/resource.cc +++ b/server/core/resource.cc @@ -677,6 +677,13 @@ HttpResponse cb_qc(const HttpRequest& request) return HttpResponse(MHD_HTTP_OK, qc_as_json(request.host()).release()); } +HttpResponse cb_qc_classify(const HttpRequest& request) +{ + string sql = request.get_option("sql"); + + return HttpResponse(MHD_HTTP_OK, qc_classify_as_json(request.host(), sql).release()); +} + HttpResponse cb_thread(const HttpRequest& request) { int id = atoi(request.last_uri_part().c_str()); @@ -950,6 +957,8 @@ public: m_get.push_back(SResource(new Resource(cb_maxscale, 1, "maxscale"))); m_get.push_back(SResource(new Resource(cb_qc, 2, "maxscale", "query_classifier"))); + m_get.push_back(SResource(new Resource(cb_qc_classify, 3, + "maxscale", "query_classifier", "classify"))); m_get.push_back(SResource(new Resource(cb_all_threads, 2, "maxscale", "threads"))); m_get.push_back(SResource(new Resource(cb_thread, 3, "maxscale", "threads", ":thread"))); m_get.push_back(SResource(new Resource(cb_logs, 2, "maxscale", "logs")));