diff --git a/include/maxscale/config.h b/include/maxscale/config.h index 7c9cd5211..4fdc580a2 100644 --- a/include/maxscale/config.h +++ b/include/maxscale/config.h @@ -108,6 +108,7 @@ extern const char CN_AUTH_CONNECT_TIMEOUT[]; 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_CONNECTION_TIMEOUT[]; extern const char CN_DUMP_LAST_STATEMENTS[]; extern const char CN_DATA[]; diff --git a/server/core/config.cc b/server/core/config.cc index ca51c4028..35e71c96a 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -87,6 +87,7 @@ const char CN_AUTH_CONNECT_TIMEOUT[] = "auth_connect_timeout"; 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_CONNECTION_TIMEOUT[] = "connection_timeout"; const char CN_DATA[] = "data"; const char CN_DEFAULT[] = "default"; diff --git a/server/core/config_runtime.cc b/server/core/config_runtime.cc index 680086106..b5a2db53b 100644 --- a/server/core/config_runtime.cc +++ b/server/core/config_runtime.cc @@ -2541,39 +2541,7 @@ bool runtime_alter_maxscale_from_json(json_t* new_json) return rval; } -bool validate_qc_json(json_t* json) -{ - json_t* param = mxs_json_pointer(json, MXS_JSON_PTR_PARAMETERS); - bool rval = false; - - if (param && json_is_object(param)) - { - rval = runtime_is_count_or_null(param, "cache_size"); - } - - return rval; -} - -// TODO: Expose everything needed so that this function could be -// TODO: part of the query classifier API. bool runtime_alter_qc_from_json(json_t* json) { - bool rval = false; - - if (validate_qc_json(json)) - { - rval = true; - - json_t* param = mxs_json_pointer(json, MXS_JSON_PTR_PARAMETERS); - json_t* value; - - if ((value = mxs_json_pointer(param, "cache_size"))) - { - QC_CACHE_PROPERTIES cache_properties = { json_integer_value(value) }; - - qc_set_cache_properties(&cache_properties); - } - } - - return rval; + return qc_alter_from_json(json); } diff --git a/server/core/internal/query_classifier.hh b/server/core/internal/query_classifier.hh index f17d510db..129b4d059 100644 --- a/server/core/internal/query_classifier.hh +++ b/server/core/internal/query_classifier.hh @@ -38,12 +38,22 @@ typedef enum qc_trx_parse_using uint32_t qc_get_trx_type_mask_using(GWBUF* stmt, qc_trx_parse_using_t use); /** - * Generic query classifier information. + * Common query classifier properties as JSON. * * @param zHost The MaxScale host. * - * @return A json object containing information. + * @return A json object containing properties. */ std::unique_ptr qc_as_json(const char* zHost); +/** + * Alter common query classifier properties. + * + * @param pJson A JSON object. + * + * @return True, if the object was valid and parameters could be changed, + * false otherwise. + */ +bool qc_alter_from_json(json_t* pJson); + MXS_END_DECLS diff --git a/server/core/query_classifier.cc b/server/core/query_classifier.cc index 3a6a6a4f5..e6d3f13fe 100644 --- a/server/core/query_classifier.cc +++ b/server/core/query_classifier.cc @@ -27,6 +27,7 @@ #include #include +#include "internal/config_runtime.h" #include "internal/modules.h" #include "internal/trxboundaryparser.hh" @@ -1258,15 +1259,54 @@ json_t* qc_get_cache_stats_as_json() std::unique_ptr qc_as_json(const char* zHost) { json_t* pParams = json_object(); - json_object_set_new(pParams, "cache_size", json_integer(this_unit.cache_max_size())); + json_object_set_new(pParams, CN_CACHE_SIZE, json_integer(this_unit.cache_max_size())); 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("query_classifier")); - json_object_set_new(pSelf, CN_TYPE, json_string("query_classifier")); + json_object_set_new(pSelf, CN_ID, json_string(CN_QUERY_CLASSIFIER)); + json_object_set_new(pSelf, CN_TYPE, json_string(CN_QUERY_CLASSIFIER)); json_object_set_new(pSelf, CN_ATTRIBUTES, pAttributes); return std::unique_ptr(mxs_json_resource(zHost, MXS_JSON_API_QC, pSelf)); } + +namespace +{ + +json_t* get_params(json_t* pJson) +{ + json_t* pParams = mxs_json_pointer(pJson, MXS_JSON_PTR_PARAMETERS); + + if (pParams && json_is_object(pParams)) + { + if (!runtime_is_count_or_null(pParams, CN_CACHE_SIZE)) + { + pParams = nullptr; + } + } + + return pParams; +} + +} + +bool qc_alter_from_json(json_t* pJson) +{ + json_t* pParams = get_params(pJson); + + if (pParams) + { + json_t* pValue; + + if ((pValue = mxs_json_pointer(pParams, CN_CACHE_SIZE))) + { + QC_CACHE_PROPERTIES cache_properties = { json_integer_value(pValue) }; + + qc_set_cache_properties(&cache_properties); + } + } + + return pParams != nullptr; +}