From 638debcdc0dac6f5e5c01e0982dfa8ab73988163 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 29 Apr 2019 10:38:25 +0300 Subject: [PATCH] MXS-2329 Allow the restriction of duration units It's now possible to specify in the config parameter declaration that the smallest allowed unit is seconds. For parameters whose granularity is seconds, allowing to specify a duration in milliseconds would open up a possibility for hard to detect errors. --- include/maxscale/modinfo.h | 1 + server/core/config.cc | 16 +++++++++++++++- server/core/test/test_config.cc | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/maxscale/modinfo.h b/include/maxscale/modinfo.h index 4dafa1a68..710638c7e 100644 --- a/include/maxscale/modinfo.h +++ b/include/maxscale/modinfo.h @@ -109,6 +109,7 @@ enum mxs_module_param_options MXS_MODULE_OPT_PATH_F_OK = (1 << 4), /**< PATH: Path must exist */ MXS_MODULE_OPT_PATH_CREAT = (1 << 5), /**< PATH: Create path if it doesn't exist */ MXS_MODULE_OPT_ENUM_UNIQUE = (1 << 6), /**< ENUM: Only one value can be defined */ + MXS_MODULE_OPT_DURATION_S = (1 << 7), /**< DURATION: Cannot be specified in milliseconds */ /**< Parameter is deprecated: Causes a warning to be logged if the parameter * is used but will not cause a configuration error. */ diff --git a/server/core/config.cc b/server/core/config.cc index ea9b3d24a..d84e30277 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -4484,13 +4484,27 @@ bool config_param_is_valid(const MXS_MODULE_PARAM* params, { valid = true; - if (unit == mxs::config::DURATION_IN_DEFAULT) + switch (unit) { + case mxs::config::DURATION_IN_MILLISECONDS: + if (params[i].options & MXS_MODULE_OPT_DURATION_S) + { + MXS_ERROR("Currently the granularity of '%s' is seconds. The value " + "cannot be specified in milliseconds.", params[i].name); + valid = false; + } + break; + + case mxs::config::DURATION_IN_DEFAULT: MXS_WARNING("Specifying durations without a suffix denoting the unit " "has been deprecated: '%s=%s'. Use the suffixes 'h' (hour), " "'m' (minute) 's' (second) or 'ms' (milliseconds). " "For instance, '%s=%ss' or '%s=%sms.", key, value, key, value, key, value); + break; + + default: + break; } } } diff --git a/server/core/test/test_config.cc b/server/core/test/test_config.cc index 206684279..eaf457522 100644 --- a/server/core/test/test_config.cc +++ b/server/core/test/test_config.cc @@ -44,6 +44,7 @@ int test_validity() {"p7", MXS_MODULE_PARAM_SERVICE, "my-service" }, {"p8", MXS_MODULE_PARAM_ENUM, "a", MXS_MODULE_OPT_ENUM_UNIQUE, enum_values}, {"p9", MXS_MODULE_PARAM_DURATION, "4711s" }, + {"p10", MXS_MODULE_PARAM_DURATION, "4711s", MXS_MODULE_OPT_DURATION_S }, {MXS_END_MODULE_PARAMS} }; @@ -107,6 +108,7 @@ int test_validity() TEST(config_param_is_valid(params, "p9", "4711S", &ctx)); TEST(config_param_is_valid(params, "p9", "4711MS", &ctx)); TEST(!config_param_is_valid(params, "p9", "4711q", &ctx)); + TEST(!config_param_is_valid(params, "p10", "4711ms", &ctx)); /** Service parameter */ CONFIG_CONTEXT svc("test-service");