MXS-2304 Add ctor/dtor and other functions to parameter class

The manipulation functions are currently static so that the container can be initialized
if required. This will be fixed later.

The new functions are taken into use in monitor management.
This commit is contained in:
Esa Korhonen 2019-02-05 13:58:26 +02:00
parent 42b5c39f43
commit ed80680da9
8 changed files with 154 additions and 103 deletions

View File

@ -222,6 +222,7 @@ extern const char CN_LOG_TO_SHM[];
class MXS_CONFIG_PARAMETER
{
public:
~MXS_CONFIG_PARAMETER();
/**
* Get value of key as string.
@ -317,9 +318,42 @@ public:
*/
bool contains(const std::string& key) const;
char* name; /**< The name of the parameter */
char* value; /**< The value of the parameter */
MXS_CONFIG_PARAMETER* next; /**< Next pointer in the linked list */
/**
* Set a key-value combination. If the key doesn't exist, it is added. The function is static
* to handle the special case of params being empty. This is needed until the config management
* has been properly refactored.
*
* @param ppParams Double pointer to the parameters structure to edit
* @param key Parameter key
* @param value Value to set
*/
static void set(MXS_CONFIG_PARAMETER** ppParams, const std::string& key, const std::string& value);
/**
* Copy all key-value pairs from a set to this container. If a key doesn't exist, it is added.
*
* @param destination Destination of the copied parameters
* @param source Parameters to copy
*/
static void set_multiple(MXS_CONFIG_PARAMETER** destination, const MXS_CONFIG_PARAMETER* source);
/**
* Remove a key-value pair from the container.
*
* @param ppParams Parameters container
* @param key Key to remove
*/
static void remove(MXS_CONFIG_PARAMETER** ppParams, const std::string& key);
char* name {nullptr}; /**< The name of the parameter */
char* value {nullptr}; /**< The value of the parameter */
MXS_CONFIG_PARAMETER* next {nullptr}; /**< Next pointer in the linked list */
private:
/**
* TODO: Remove this once using STL container.
*/
MXS_CONFIG_PARAMETER* find(const std::string& key);
};
/**

View File

@ -458,21 +458,6 @@ bool mon_connection_is_ok(mxs_connect_result_t connect_result);
void mon_log_connect_error(MXS_MONITORED_SERVER* database, mxs_connect_result_t rval);
const char* mon_get_event_name(mxs_monitor_event_t event);
/**
* Alter monitor parameters
*
* The monitor parameters should not be altered while the monitor is
* running. To alter a parameter from outside a monitor module, stop the monitor,
* do the alteration and then restart the monitor. The monitor "owns" the parameters
* as long as it is running so if the monitor needs to change its own parameters,
* it can do it without stopping itself.
*
* @param monitor Monitor whose parameter is altered
* @param key Parameter name to alter
* @param value New value for the parameter
*/
void mon_alter_parameter(Monitor* monitor, const char* key, const char* value);
/**
* @brief Hangup connections to failed servers
*

View File

@ -1882,6 +1882,12 @@ bool config_get_compiled_regexes(const MXS_CONFIG_PARAMETER* params,
return rval;
}
MXS_CONFIG_PARAMETER::~MXS_CONFIG_PARAMETER()
{
MXS_FREE(name);
MXS_FREE(value);
}
string MXS_CONFIG_PARAMETER::get_string(const std::string& key) const
{
auto params = this;
@ -1904,14 +1910,10 @@ int64_t MXS_CONFIG_PARAMETER::get_integer(const std::string& key) const
MXS_CONFIG_PARAMETER* config_clone_param(const MXS_CONFIG_PARAMETER* param)
{
MXS_CONFIG_PARAMETER* p2 = (MXS_CONFIG_PARAMETER*)MXS_MALLOC(sizeof(MXS_CONFIG_PARAMETER));
if (p2)
{
p2->name = MXS_STRDUP_A(param->name);
p2->value = MXS_STRDUP_A(param->value);
p2->next = NULL;
}
auto p2 = new MXS_CONFIG_PARAMETER;
p2->name = MXS_STRDUP_A(param->name);
p2->value = MXS_STRDUP_A(param->value);
p2->next = nullptr;
return p2;
}
@ -1920,9 +1922,7 @@ void config_free_one_param(MXS_CONFIG_PARAMETER* p1)
{
if (p1)
{
MXS_FREE(p1->name);
MXS_FREE(p1->value);
MXS_FREE(p1);
delete p1;
}
}
@ -1960,9 +1960,9 @@ bool config_add_param(CONFIG_CONTEXT* obj, const char* key, const char* value)
bool rval = false;
char* my_key = MXS_STRDUP(key);
char* my_value = MXS_STRDUP(value);
MXS_CONFIG_PARAMETER* param = (MXS_CONFIG_PARAMETER*)MXS_MALLOC(sizeof(*param));
auto param = new MXS_CONFIG_PARAMETER;
if (my_key && my_value && param)
if (my_key && my_value)
{
param->name = my_key;
param->value = my_value;
@ -1974,7 +1974,7 @@ bool config_add_param(CONFIG_CONTEXT* obj, const char* key, const char* value)
{
MXS_FREE(my_key);
MXS_FREE(my_value);
MXS_FREE(param);
delete param;
}
return rval;
@ -2004,6 +2004,101 @@ bool config_append_param(CONFIG_CONTEXT* obj, const char* key, const char* value
return rval;
}
void MXS_CONFIG_PARAMETER::set(MXS_CONFIG_PARAMETER** ppParams, const string& key, const string& value)
{
mxb_assert(ppParams);
char* new_value = MXS_STRDUP(value.c_str());
if (*ppParams == nullptr)
{
// This is the first parameter.
auto new_param = new MXS_CONFIG_PARAMETER();
new_param->name = MXS_STRDUP(key.c_str());
new_param->value = new_value;
new_param->next = nullptr;
*ppParams = new_param;
return;
}
MXS_CONFIG_PARAMETER* pParam = *ppParams;
auto param = pParam->find(key);
if (param)
{
MXS_FREE(param->value);
param->value = new_value;
}
else
{
auto new_param = new MXS_CONFIG_PARAMETER();
new_param->name = MXS_STRDUP(key.c_str());
new_param->value = new_value;
new_param->next = nullptr;
// Iterate to the last parameter in the linked list.
auto last_param = pParam;
while (last_param->next)
{
last_param = last_param->next;
}
last_param->next = new_param;
}
}
void MXS_CONFIG_PARAMETER::set_multiple(MXS_CONFIG_PARAMETER** destination, const MXS_CONFIG_PARAMETER* source)
{
auto param = source;
while (param)
{
set(destination, param->name, param->value);
param = param->next;
}
}
void MXS_CONFIG_PARAMETER::remove(MXS_CONFIG_PARAMETER** ppParams, const string& key)
{
mxb_assert(ppParams);
MXS_CONFIG_PARAMETER* current = *ppParams;
if (current)
{
// Handle special case.
if (current->name == key)
{
*ppParams = current->next;
delete current;
}
else
{
MXS_CONFIG_PARAMETER* next = current->next;
while (next)
{
if (next->name == key)
{
current->next = next->next;
delete next;
break;
}
else
{
current = next;
next = next->next;
}
}
}
}
}
MXS_CONFIG_PARAMETER* MXS_CONFIG_PARAMETER::find(const std::string& key)
{
auto param = this;
while (param)
{
if (param->name == key)
{
return param;
}
param = param->next;
}
return param;
}
bool config_replace_param(CONFIG_CONTEXT* obj, const char* key, const char* value)
{
if (MXS_CONFIG_PARAMETER* param = config_get_param(obj->parameters, key))

View File

@ -583,8 +583,7 @@ bool do_alter_monitor(Monitor* monitor, const char* key, const char* value)
}
std::lock_guard<std::mutex> guard(crt_lock);
monitor_set_parameter(monitor, key, value);
MXS_CONFIG_PARAMETER::set(&monitor->parameters, key, value);
bool success = true;
if (strcmp(key, CN_USER) == 0)
{

View File

@ -137,8 +137,6 @@ void monitor_list(DCB*);
bool monitor_add_server(Monitor* mon, SERVER* server);
void monitor_remove_server(Monitor* mon, SERVER* server);
void monitor_add_parameters(Monitor* monitor, const MXS_CONFIG_PARAMETER* params);
bool monitor_remove_parameter(Monitor* monitor, const char* key);
void monitor_set_parameter(Monitor* monitor, const char* key, const char* value);
void monitor_set_journal_max_age(Monitor* mon, time_t value);

View File

@ -222,9 +222,9 @@ bool Monitor::configure_base(const MXS_CONFIG_PARAMETER* params)
if (!error)
{
// Store module name into parameter storage.
monitor_set_parameter(this, CN_MODULE, m_module.c_str());
MXS_CONFIG_PARAMETER::set(&parameters, CN_MODULE, m_module);
// Add all config settings to text-mode storage. Needed for serialization.
monitor_add_parameters(this, params);
MXS_CONFIG_PARAMETER::set_multiple(&parameters, params);
}
return !error;
}
@ -769,60 +769,6 @@ void monitor_add_parameters(Monitor* monitor, const MXS_CONFIG_PARAMETER* params
}
}
void monitor_set_parameter(Monitor* monitor, const char* key, const char* value)
{
monitor_remove_parameter(monitor, key);
MXS_CONFIG_PARAMETER p = {};
p.name = const_cast<char*>(key);
p.value = const_cast<char*>(value);
monitor_add_parameters(monitor, &p);
}
bool monitor_remove_parameter(Monitor* monitor, const char* key)
{
MXS_CONFIG_PARAMETER* prev = NULL;
bool rval = false;
Guard guard(monitor->m_lock);
for (MXS_CONFIG_PARAMETER* p = monitor->parameters; p; p = p->next)
{
if (strcmp(p->name, key) == 0)
{
if (p == monitor->parameters)
{
monitor->parameters = monitor->parameters->next;
}
else
{
prev->next = p->next;
}
p->next = NULL;
config_parameter_free(p);
rval = true;
break;
}
prev = p;
}
return rval;
}
void mon_alter_parameter(Monitor* monitor, const char* key, const char* value)
{
Guard guard(monitor->m_lock);
for (MXS_CONFIG_PARAMETER* p = monitor->parameters; p; p = p->next)
{
if (strcmp(p->name, key) == 0)
{
MXS_FREE(p->value);
p->value = MXS_STRDUP_A(value);
break;
}
}
}
void monitor_stash_current_status(MXS_MONITORED_SERVER* ptr)
{
ptr->mon_prev_status = ptr->server->status;

View File

@ -1133,8 +1133,10 @@ void service_add_parameters(Service* service, const MXS_CONFIG_PARAMETER* param)
void service_add_parameter(Service* service, const char* key, const char* value)
{
MXS_CONFIG_PARAMETER p {const_cast<char*>(key), const_cast<char*>(value), nullptr};
service_add_parameters(service, &p);
auto p = new MXS_CONFIG_PARAMETER;
p->name = MXS_STRDUP(key);
p->value = MXS_STRDUP(value);
service_add_parameters(service, p);
}
void service_remove_parameter(Service* service, const char* key)

View File

@ -698,15 +698,7 @@ void MariaDBMonitor::assign_new_master(MariaDBServer* new_master)
*/
void MariaDBMonitor::disable_setting(const std::string& setting)
{
Worker* worker = static_cast<Worker*>(mxs_rworker_get(MXS_RWORKER_MAIN));
worker->execute([=]() {
MXS_CONFIG_PARAMETER p = {};
p.name = const_cast<char*>(setting.c_str());
p.value = const_cast<char*>("false");
monitor_add_parameters(m_monitor, &p);
},
EXECUTE_AUTO);
MXS_CONFIG_PARAMETER::set(&parameters, setting, "false");
}
/**