Add runtime schemarouter reconfiguration

The schemarouter now also uses versioned configurations implemented by
shared pointers to configuration objects. Moved all the configuration
management into the Config class. Removed router options from
schemarouter.
This commit is contained in:
Markus Mäkelä 2018-07-09 13:34:04 +03:00
parent 7f67fe6f14
commit 5903e194a7
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
6 changed files with 47 additions and 119 deletions

View File

@ -13,9 +13,33 @@
#include "schemarouter.hh"
#include <maxscale/utils.hh>
namespace schemarouter
{
Config::Config(MXS_CONFIG_PARAMETER* conf):
refresh_min_interval(config_get_integer(conf, "refresh_interval")),
refresh_databases(config_get_bool(conf, "refresh_databases")),
debug(config_get_bool(conf, "debug")),
ignore_regex(config_get_compiled_regex(conf, "ignore_databases_regex", 0, NULL)),
ignore_match_data(ignore_regex ? pcre2_match_data_create_from_pattern(ignore_regex, NULL) : NULL),
preferred_server(config_get_server(conf, "preferred_server"))
{
ignored_dbs.insert("mysql");
ignored_dbs.insert("information_schema");
ignored_dbs.insert("performance_schema");
// TODO: Don't process this in the router
if (MXS_CONFIG_PARAMETER* p = config_get_param(conf, "ignore_databases"))
{
for (auto&& a: mxs::strtok(p->value, ", \t"))
{
ignored_dbs.insert(a);
}
}
}
void SRBackend::set_mapped(bool value)
{
m_mapped = value;

View File

@ -48,17 +48,17 @@ struct Config
std::set<std::string> ignored_dbs; /**< Set of ignored databases */
SERVER* preferred_server; /**< Server to prefer in conflict situations */
Config():
refresh_min_interval(0.0),
refresh_databases(false),
debug(false),
ignore_regex(NULL),
ignore_match_data(NULL),
preferred_server(NULL)
Config(MXS_CONFIG_PARAMETER* conf);
~Config()
{
pcre2_match_data_free(ignore_match_data);
pcre2_code_free(ignore_regex);
}
};
typedef std::shared_ptr<Config> SConfig;
/**
* Router statistics
*/

View File

@ -42,7 +42,7 @@ namespace schemarouter
* @file schemarouter.c The entry points for the simple sharding router module.
*/
SchemaRouter::SchemaRouter(SERVICE *service, Config& config):
SchemaRouter::SchemaRouter(SERVICE *service, SConfig config):
mxs::Router<SchemaRouter, SchemaRouterSession>(service),
m_config(config),
m_service(service)
@ -52,10 +52,6 @@ SchemaRouter::SchemaRouter(SERVICE *service, Config& config):
SchemaRouter::~SchemaRouter()
{
if (m_config.ignore_regex)
{
pcre2_code_free(m_config.ignore_regex);
}
}
SchemaRouter* SchemaRouter::create(SERVICE* pService, char** pzOptions)
@ -69,108 +65,15 @@ SchemaRouter* SchemaRouter::create(SERVICE* pService, char** pzOptions)
pService->users_from_all = true;
}
Config config;
MXS_CONFIG_PARAMETER* param;
SConfig config(new Config(pService->svc_config_param));
return new SchemaRouter(pService, config);
}
config.refresh_databases = config_get_bool(conf, "refresh_databases");
config.refresh_min_interval = config_get_integer(conf, "refresh_interval");
config.debug = config_get_bool(conf, "debug");
config.preferred_server = config_get_server(conf, "preferred_server");
/** Add default system databases to ignore */
config.ignored_dbs.insert("mysql");
config.ignored_dbs.insert("information_schema");
config.ignored_dbs.insert("performance_schema");
if ((param = config_get_param(conf, "ignore_databases_regex")))
{
int errcode;
PCRE2_SIZE erroffset;
pcre2_code* re = pcre2_compile((PCRE2_SPTR)param->value, PCRE2_ZERO_TERMINATED, 0,
&errcode, &erroffset, NULL);
if (re == NULL)
{
PCRE2_UCHAR errbuf[512];
pcre2_get_error_message(errcode, errbuf, sizeof(errbuf));
MXS_ERROR("Regex compilation failed at %d for regex '%s': %s",
(int)erroffset, param->value, errbuf);
return NULL;
}
pcre2_match_data* match_data = pcre2_match_data_create_from_pattern(re, NULL);
if (match_data == NULL)
{
pcre2_code_free(re);
return NULL;
}
config.ignore_regex = re;
config.ignore_match_data = match_data;
}
if ((param = config_get_param(conf, "ignore_databases")))
{
char val[strlen(param->value) + 1];
strcpy(val, param->value);
const char *sep = ", \t";
char *sptr;
char *tok = strtok_r(val, sep, &sptr);
while (tok)
{
config.ignored_dbs.insert(tok);
tok = strtok_r(NULL, sep, &sptr);
}
}
bool success = true;
for (int i = 0; pzOptions && pzOptions[i]; i++)
{
char* value = strchr(pzOptions[i], '=');
if (value == NULL)
{
MXS_ERROR("Unknown router options for %s", pzOptions[i]);
success = false;
break;
}
*value = '\0';
value++;
if (strcmp(pzOptions[i], "max_sescmd_history") == 0)
{
MXS_WARNING("Use of 'max_sescmd_history' is deprecated");
}
else if (strcmp(pzOptions[i], "disable_sescmd_history") == 0)
{
MXS_WARNING("Use of 'disable_sescmd_history' is deprecated");
}
else if (strcmp(pzOptions[i], "refresh_databases") == 0)
{
config.refresh_databases = config_truth_value(value);
}
else if (strcmp(pzOptions[i], "refresh_interval") == 0)
{
config.refresh_min_interval = atof(value);
}
else if (strcmp(pzOptions[i], "debug") == 0)
{
config.debug = config_truth_value(value);
}
else
{
MXS_ERROR("Unknown router options for %s", pzOptions[i]);
success = false;
break;
}
}
return success ? new SchemaRouter(pService, config) : NULL;
bool SchemaRouter::configure(MXS_CONFIG_PARAMETER* params)
{
SConfig config(new Config(params));
m_config = config;
return true;
}
/**
@ -364,7 +267,7 @@ json_t* SchemaRouter::diagnostics_json() const
uint64_t SchemaRouter::getCapabilities()
{
return RCAP_TYPE_NONE;
return RCAP_TYPE_CONTIGUOUS_INPUT | RCAP_TYPE_RUNTIME_CONFIG;
}
}
@ -386,7 +289,7 @@ extern "C" MXS_MODULE* MXS_CREATE_MODULE()
MXS_ROUTER_VERSION,
"A database sharding router for simple sharding",
"V1.0.0",
RCAP_TYPE_CONTIGUOUS_INPUT,
RCAP_TYPE_CONTIGUOUS_INPUT | RCAP_TYPE_RUNTIME_CONFIG,
&schemarouter::SchemaRouter::s_object,
NULL, /* Process init. */
NULL, /* Process finish. */

View File

@ -39,15 +39,16 @@ public:
void diagnostics(DCB* pDcb);
json_t* diagnostics_json() const;
uint64_t getCapabilities();
bool configure(MXS_CONFIG_PARAMETER* param);
private:
friend class SchemaRouterSession;
/** Internal functions */
SchemaRouter(SERVICE *service, Config& config);
SchemaRouter(SERVICE *service, SConfig config);
/** Member variables */
Config m_config; /*< expanded config info from SERVICE */
SConfig m_config; /*< expanded config info from SERVICE */
ShardManager m_shard_manager; /*< Shard maps hashed by user name */
SERVICE* m_service; /*< Pointer to service */
SPINLOCK m_lock; /*< Lock for the instance data */

View File

@ -40,7 +40,7 @@ SchemaRouterSession::SchemaRouterSession(MXS_SESSION* session, SchemaRouter* rou
m_client(session->client_dcb),
m_mysql_session((MYSQL_session*)session->client_dcb->data),
m_backends(backends),
m_config(&router->m_config),
m_config(router->m_config),
m_router(router),
m_shard(m_router->m_shard_manager.get_shard(m_client->user, m_config->refresh_min_interval)),
m_state(0),

View File

@ -153,7 +153,7 @@ private:
DCB* m_client; /**< The client DCB */
MYSQL_session* m_mysql_session; /**< Session client data (username, password, SHA1). */
SSRBackendList m_backends; /**< Backend references */
Config* m_config; /**< Pointer to router config */
SConfig m_config; /**< Session specific configuration */
SchemaRouter* m_router; /**< The router instance */
Shard m_shard; /**< Database to server mapping */
std::string m_connect_db; /**< Database the user was trying to connect to */