MXS-1929: Serialize objects using parameters
The main "non-static" objects are now serialized using their parameters. This makes it possible to use common code for most modules.
This commit is contained in:
parent
e2ace578d2
commit
a9ff2a7056
@ -4666,6 +4666,7 @@ MXS_CONFIG_PARAMETER* ParamList::params()
|
||||
return m_ctx.parameters;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void dump_if_changed(const MXS_MODULE_PARAM* params, int file,
|
||||
const std::string& key, const std::string& value)
|
||||
@ -4694,4 +4695,17 @@ void dump_if_changed(const MXS_MODULE_PARAM* params, int file,
|
||||
}
|
||||
}
|
||||
|
||||
void dump_param_list(int file, MXS_CONFIG_PARAMETER* list,
|
||||
const std::unordered_set<std::string>& ignored,
|
||||
const MXS_MODULE_PARAM* common_params,
|
||||
const MXS_MODULE_PARAM* module_params)
|
||||
{
|
||||
for (auto p = list; p; p = p->next)
|
||||
{
|
||||
if (ignored.count(p->name) == 0 && *p->value)
|
||||
{
|
||||
dump_if_changed(common_params, file, p->name, p->value);
|
||||
dump_if_changed(module_params, file, p->name, p->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -585,6 +585,7 @@ bool runtime_alter_service(Service *service, const char* zKey, const char* zValu
|
||||
|
||||
if (service->is_basic_parameter(key))
|
||||
{
|
||||
service_replace_parameter(service, zKey, zValue);
|
||||
service->update_basic_parameter(key, value);
|
||||
}
|
||||
else
|
||||
|
@ -534,18 +534,12 @@ static bool create_filter_config(const SFilterDef& filter, const char *filename)
|
||||
|
||||
dprintf(file, "[%s]\n", filter->name.c_str());
|
||||
dprintf(file, "%s=%s\n", CN_TYPE, CN_FILTER);
|
||||
dprintf(file, "%s=%s\n", CN_MODULE, filter->module.c_str());
|
||||
|
||||
std::set<std::string> param_set{CN_TYPE, CN_MODULE};
|
||||
|
||||
for (MXS_CONFIG_PARAMETER* p = filter->parameters; p; p = p->next)
|
||||
{
|
||||
if (param_set.count(p->name) == 0)
|
||||
{
|
||||
dprintf(file, "%s=%s\n", p->name, p->value);
|
||||
}
|
||||
}
|
||||
const MXS_MODULE* mod = get_module(filter->module.c_str(), NULL);
|
||||
ss_dassert(mod);
|
||||
|
||||
MXS_MODULE_PARAM no_common_params = {};
|
||||
dump_param_list(file, filter->parameters, {CN_TYPE}, &no_common_params, mod->parameters);
|
||||
close(file);
|
||||
|
||||
return true;
|
||||
|
@ -20,6 +20,7 @@
|
||||
|
||||
#include <sstream>
|
||||
#include <initializer_list>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <maxbase/jansson.h>
|
||||
#include <maxscale/ssl.h>
|
||||
@ -218,60 +219,8 @@ bool is_normal_server_parameter(const char *param);
|
||||
*/
|
||||
bool get_suffixed_size(const char* value, uint64_t* dest);
|
||||
|
||||
namespace maxscale
|
||||
{
|
||||
|
||||
// Internal function
|
||||
void dump_if_changed(const MXS_MODULE_PARAM* params, int file,
|
||||
const std::string& key, const std::string& value);
|
||||
|
||||
|
||||
/**
|
||||
* Dump a parameter into a file descriptor
|
||||
*
|
||||
* The function detects only literal matches to the string format default values.
|
||||
* If the string conversion results in an empty string, the value will not be
|
||||
* dumped. This can only happen if an empty string is passed as the value.
|
||||
*
|
||||
* The function writes a single key-value pair into the file and terminates
|
||||
* the line with a newline. This is intended to be used with configuration
|
||||
* dumping code in the core.
|
||||
*
|
||||
* Note: Does not work with enum type parameters, they'll get converted into
|
||||
* integers. Convert them to string format at the call site.
|
||||
*
|
||||
* @param file File descriptor where the line is written
|
||||
* @param key Name of the parameter
|
||||
* @param value The parameter value
|
||||
* @param params List of module parameters to use
|
||||
*/
|
||||
template <class T>
|
||||
inline void dump_param(int file, const std::string& key, const T value, std::initializer_list<const MXS_MODULE_PARAM*> params)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << value;
|
||||
auto strval = ss.str();
|
||||
|
||||
if (!strval.empty())
|
||||
{
|
||||
// Don't dump empty values
|
||||
for (auto a : params)
|
||||
{
|
||||
dump_if_changed(a, file, key, strval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Specialization required to dump booleans in the same format as they used in
|
||||
// the defaults. This requires that all defaults use either "true" or "false"
|
||||
// for the values.
|
||||
template <>
|
||||
inline void dump_param(int file, const std::string& key, bool value, std::initializer_list<const MXS_MODULE_PARAM*> params)
|
||||
{
|
||||
for (auto a : params)
|
||||
{
|
||||
dump_if_changed(a, file, key, value ? "true" : "false");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// Dump a parameter list into a file as `key=value` pairs
|
||||
void dump_param_list(int file, MXS_CONFIG_PARAMETER* list,
|
||||
const std::unordered_set<std::string>& ignored,
|
||||
const MXS_MODULE_PARAM* common_params,
|
||||
const MXS_MODULE_PARAM* module_params);
|
||||
|
@ -1562,16 +1562,6 @@ static bool create_monitor_config(const MXS_MONITOR *monitor, const char *filena
|
||||
|
||||
dprintf(file, "[%s]\n", monitor->name);
|
||||
dprintf(file, "%s=monitor\n", CN_TYPE);
|
||||
dprintf(file, "%s=%s\n", CN_MODULE, monitor->module_name);
|
||||
dprintf(file, "%s=%s\n", CN_USER, monitor->user);
|
||||
dprintf(file, "%s=%s\n", CN_PASSWORD, monitor->password);
|
||||
dprintf(file, "%s=%lu\n", CN_MONITOR_INTERVAL, monitor->interval);
|
||||
dprintf(file, "%s=%d\n", CN_BACKEND_CONNECT_TIMEOUT, monitor->connect_timeout);
|
||||
dprintf(file, "%s=%d\n", CN_BACKEND_WRITE_TIMEOUT, monitor->write_timeout);
|
||||
dprintf(file, "%s=%d\n", CN_BACKEND_READ_TIMEOUT, monitor->read_timeout);
|
||||
dprintf(file, "%s=%d\n", CN_BACKEND_CONNECT_ATTEMPTS, monitor->connect_attempts);
|
||||
dprintf(file, "%s=%ld\n", CN_JOURNAL_MAX_AGE, monitor->journal_max_age);
|
||||
dprintf(file, "%s=%d\n", CN_SCRIPT_TIMEOUT, monitor->script_timeout);
|
||||
|
||||
if (monitor->monitored_servers)
|
||||
{
|
||||
@ -1587,35 +1577,12 @@ static bool create_monitor_config(const MXS_MONITOR *monitor, const char *filena
|
||||
dprintf(file, "\n");
|
||||
}
|
||||
|
||||
const char* params[] =
|
||||
{
|
||||
CN_TYPE,
|
||||
CN_MODULE,
|
||||
CN_USER,
|
||||
CN_PASSWORD,
|
||||
"passwd", // TODO: Remove this
|
||||
CN_MONITOR_INTERVAL,
|
||||
CN_BACKEND_CONNECT_TIMEOUT,
|
||||
CN_BACKEND_WRITE_TIMEOUT,
|
||||
CN_BACKEND_READ_TIMEOUT,
|
||||
CN_BACKEND_CONNECT_ATTEMPTS,
|
||||
CN_JOURNAL_MAX_AGE,
|
||||
CN_SCRIPT_TIMEOUT,
|
||||
CN_SERVERS
|
||||
};
|
||||
|
||||
std::set<std::string> param_set(params, params + sizeof(params) / sizeof(params[0]));
|
||||
|
||||
for (MXS_CONFIG_PARAMETER* p = monitor->parameters; p; p = p->next)
|
||||
{
|
||||
if (param_set.find(p->name) == param_set.end())
|
||||
{
|
||||
dprintf(file, "%s=%s\n", p->name, p->value);
|
||||
}
|
||||
}
|
||||
const MXS_MODULE* mod = get_module(monitor->module_name, NULL);
|
||||
ss_dassert(mod);
|
||||
|
||||
dump_param_list(file, monitor->parameters, {CN_TYPE, CN_SERVERS},
|
||||
config_monitor_params, mod->parameters);
|
||||
spinlock_release(&monitor->lock);
|
||||
|
||||
close(file);
|
||||
|
||||
return true;
|
||||
|
@ -1779,23 +1779,6 @@ bool Service::dump_config(const char *filename) const
|
||||
*/
|
||||
dprintf(file, "[%s]\n", m_name.c_str());
|
||||
dprintf(file, "%s=service\n", CN_TYPE);
|
||||
dprintf(file, "%s=%s\n", CN_ROUTER, m_router_name.c_str());
|
||||
dprintf(file, "%s=%s\n", CN_USER, m_user.c_str());
|
||||
dprintf(file, "%s=%s\n", CN_PASSWORD, m_password.c_str());
|
||||
|
||||
const MXS_MODULE_PARAM* mp = config_service_params;
|
||||
|
||||
dump_param(file, CN_ENABLE_ROOT_USER, enable_root, {mp});
|
||||
dump_param(file, CN_MAX_RETRY_INTERVAL, max_retry_interval, {mp});
|
||||
dump_param(file, CN_MAX_CONNECTIONS, max_connections, {mp});
|
||||
dump_param(file, CN_CONNECTION_TIMEOUT, conn_idle_timeout, {mp});
|
||||
dump_param(file, CN_AUTH_ALL_SERVERS, users_from_all, {mp});
|
||||
dump_param(file, CN_STRIP_DB_ESC, strip_db_esc, {mp});
|
||||
dump_param(file, CN_LOCALHOST_MATCH_WILDCARD_HOST, localhost_match_wildcard_host, {mp});
|
||||
dump_param(file, CN_LOG_AUTH_WARNINGS, log_auth_warnings, {mp});
|
||||
dump_param(file, CN_RETRY_ON_FAILURE, retry_start, {mp});
|
||||
dump_param(file, CN_VERSION_STRING, m_version_string, {mp});
|
||||
dump_param(file, CN_WEIGHTBY, m_weightby, {mp});
|
||||
|
||||
if (!m_filters.empty())
|
||||
{
|
||||
@ -1827,36 +1810,11 @@ bool Service::dump_config(const char *filename) const
|
||||
dprintf(file, "\n");
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> common_params
|
||||
{
|
||||
CN_TYPE,
|
||||
CN_USER,
|
||||
CN_PASSWORD,
|
||||
CN_ENABLE_ROOT_USER,
|
||||
CN_MAX_RETRY_INTERVAL,
|
||||
CN_MAX_CONNECTIONS,
|
||||
CN_CONNECTION_TIMEOUT,
|
||||
CN_AUTH_ALL_SERVERS,
|
||||
CN_STRIP_DB_ESC,
|
||||
CN_LOCALHOST_MATCH_WILDCARD_HOST,
|
||||
CN_LOG_AUTH_WARNINGS,
|
||||
CN_RETRY_ON_FAILURE,
|
||||
CN_VERSION_STRING,
|
||||
CN_WEIGHTBY,
|
||||
CN_SERVERS
|
||||
};
|
||||
|
||||
const MXS_MODULE* mod = get_module(m_router_name.c_str(), NULL);
|
||||
ss_dassert(mod);
|
||||
|
||||
// Dump router specific parameters
|
||||
for (auto p = svc_config_param; p; p = p->next)
|
||||
{
|
||||
if (common_params.count(p->name) == 0)
|
||||
{
|
||||
dump_param(file, p->name, p->value, {mod->parameters});
|
||||
}
|
||||
}
|
||||
dump_param_list(file, svc_config_param, {CN_TYPE, CN_FILTERS, CN_SERVERS},
|
||||
config_service_params, mod->parameters);
|
||||
|
||||
close(file);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user