From 93dc75a011ea45d504f03867ccafb1257ee54bb9 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 27 Aug 2018 10:58:56 +0300 Subject: [PATCH] MXS-2011 Introduce blr_file_read_master_config() One function for writing, another for reading the config. No code changes; just moving code around. --- server/modules/routing/binlogrouter/blr.cc | 190 +----------------- server/modules/routing/binlogrouter/blr.hh | 3 + .../modules/routing/binlogrouter/blr_file.cc | 189 +++++++++++++++++ 3 files changed, 193 insertions(+), 189 deletions(-) diff --git a/server/modules/routing/binlogrouter/blr.cc b/server/modules/routing/binlogrouter/blr.cc index d1b2cfc46..47f461950 100644 --- a/server/modules/routing/binlogrouter/blr.cc +++ b/server/modules/routing/binlogrouter/blr.cc @@ -27,7 +27,6 @@ #include "blr.hh" #include -#include #include #include #include @@ -80,13 +79,6 @@ static void errorReply(MXS_ROUTER *instance, bool *succp); static uint64_t getCapabilities(MXS_ROUTER* instance); -static int blr_handler_config(void *userdata, - const char *section, - const char *name, - const char *value); -static int blr_handle_config_item(const char *name, - const char *value, - ROUTER_INSTANCE *inst); static int blr_load_dbusers(const ROUTER_INSTANCE *router); static int blr_check_binlog(ROUTER_INSTANCE *router); void blr_master_close(ROUTER_INSTANCE *); @@ -108,8 +100,6 @@ GWBUF *blr_cache_read_response(ROUTER_INSTANCE *router, extern bool blr_load_last_mariadb_gtid(ROUTER_INSTANCE *router, MARIADB_GTID_INFO *result); -void blr_log_disabled_heartbeat(const ROUTER_INSTANCE *inst); - static SPINLOCK instlock; static ROUTER_INSTANCE *instances; @@ -827,16 +817,7 @@ static MXS_ROUTER* createInstance(SERVICE *service, MXS_CONFIG_PARAMETER* params * automatic master replication start phase */ - static const char MASTER_INI[] = "/master.ini"; - char filename[strlen(inst->binlogdir) + sizeof(MASTER_INI)]; // sizeof includes the NULL - sprintf(filename, "%s%s", inst->binlogdir, MASTER_INI); - - rc = ini_parse(filename, blr_handler_config, inst); - - MXS_INFO("%s: %s parse result is %d", - inst->service->name, - filename, - rc); + rc = blr_file_read_master_config(inst); /* * retcode: @@ -2654,175 +2635,6 @@ blr_send_custom_error(DCB *dcb, return MXS_SESSION_ROUTE_REPLY(dcb->session, errbuf); } -/** - * Config item handler for the ini file reader - * - * @param userdata The config context element - * @param section The config file section - * @param name The Parameter name - * @param value The Parameter value - * @return zero on error - */ - -static int -blr_handler_config(void *userdata, const char *section, const char *name, const char *value) -{ - ROUTER_INSTANCE *inst = (ROUTER_INSTANCE *) userdata; - SERVICE *service; - - service = inst->service; - - if (strcasecmp(section, "binlog_configuration") == 0) - { - return blr_handle_config_item(name, value, inst); - } - else - { - MXS_ERROR("master.ini has an invalid section [%s], it should be [binlog_configuration]. " - "Service %s", - section, - service->name); - - return 0; - } -} - -/** - * Configuration handler for items in the [binlog_configuration] section - * - * @param name The item name - * @param value The item value - * @param inst The current router instance - * @return 0 on error - */ -static int -blr_handle_config_item(const char *name, const char *value, ROUTER_INSTANCE *inst) -{ - SERVICE *service; - char *ssl_cert; - char *ssl_key; - char *ssl_ca_cert; - SERVER *backend_server; - - service = inst->service; - - backend_server = service->dbref->server; - - if (strcmp(name, "master_host") == 0) - { - server_update_address(service->dbref->server, (char *)value); - } - else if (strcmp(name, "master_port") == 0) - { - server_update_port(service->dbref->server, (short)atoi(value)); - } - else if (strcmp(name, "filestem") == 0) - { - MXS_FREE(inst->fileroot); - inst->fileroot = MXS_STRDUP_A(value); - } - else if (strcmp(name, "master_user") == 0) - { - if (inst->user) - { - MXS_FREE(inst->user); - } - inst->user = MXS_STRDUP_A(value); - } - else if (strcmp(name, "master_password") == 0) - { - if (inst->password) - { - MXS_FREE(inst->password); - } - inst->password = MXS_STRDUP_A(value); - } - /** Checl for SSL options */ - else if (strcmp(name, "master_ssl") == 0) - { - inst->ssl_enabled = config_truth_value((char*)value); - } - else if (strcmp(name, "master_ssl_ca") == 0) - { - MXS_FREE(backend_server->server_ssl->ssl_ca_cert); - backend_server->server_ssl->ssl_ca_cert = value ? MXS_STRDUP_A(value) : NULL; - MXS_FREE(inst->ssl_ca); - inst->ssl_ca = value ? MXS_STRDUP_A(value) : NULL; - } - else if (strcmp(name, "master_ssl_cert") == 0) - { - MXS_FREE(backend_server->server_ssl->ssl_cert); - backend_server->server_ssl->ssl_cert = value ? MXS_STRDUP_A(value) : NULL; - MXS_FREE(inst->ssl_cert); - inst->ssl_cert = value ? MXS_STRDUP_A(value) : NULL; - } - else if (strcmp(name, "master_ssl_key") == 0) - { - MXS_FREE(backend_server->server_ssl->ssl_key); - backend_server->server_ssl->ssl_key = value ? MXS_STRDUP_A(value) : NULL; - MXS_FREE(inst->ssl_key); - inst->ssl_key = value ? MXS_STRDUP_A(value) : NULL; - } - else if (strcmp(name, "master_ssl_version") == 0 || strcmp(name, "master_tls_version") == 0) - { - if (value) - { - if (listener_set_ssl_version(backend_server->server_ssl, (char *)value) != 0) - { - MXS_ERROR("Found unknown optional parameter value for 'ssl_version' for" - " service '%s': %s, ignoring it.", - inst->service->name, - value); - } - else - { - inst->ssl_version = MXS_STRDUP_A(value); - } - } - } - /* Connect options */ - else if (strcmp(name, "master_heartbeat_period") == 0) - { - int new_val = atol((char *)value); - if (new_val < 0) - { - MXS_WARNING("Found invalid 'master_heartbeat_period' value" - " for service '%s': %s, ignoring it.", - inst->service->name, - value); - } - else - { - if (inst->heartbeat > 0 && new_val == 0) - { - blr_log_disabled_heartbeat(inst); - } - inst->heartbeat = new_val; - } - } - else if (strcmp(name, "master_connect_retry") == 0) - { - int new_val = atol((char *)value); - if (new_val <= 0) - { - MXS_WARNING("Found invalid 'master_connect_retry' value" - " for service '%s': %s, ignoring it.", - inst->service->name, - value); - } - else - { - inst->retry_interval = new_val; - } - } - else - { - return 0; - } - - return 1; -} - /** * Extract a numeric field from a packet of the specified number of bits * diff --git a/server/modules/routing/binlogrouter/blr.hh b/server/modules/routing/binlogrouter/blr.hh index 6b4c4935d..47ce8977a 100644 --- a/server/modules/routing/binlogrouter/blr.hh +++ b/server/modules/routing/binlogrouter/blr.hh @@ -986,6 +986,7 @@ static const char *blrs_states[] = /* * Externals within the router */ +extern void blr_log_disabled_heartbeat(const ROUTER_INSTANCE *inst); extern void blr_master_response(ROUTER_INSTANCE *, GWBUF *); extern void blr_master_reconnect(ROUTER_INSTANCE *); extern int blr_master_connected(ROUTER_INSTANCE *); @@ -1009,6 +1010,8 @@ extern int blr_write_binlog_record(ROUTER_INSTANCE *, extern int blr_file_rotate(ROUTER_INSTANCE *, char *, uint64_t); +extern int blr_file_read_master_config(ROUTER_INSTANCE *router); +extern int blr_file_write_master_config(ROUTER_INSTANCE *router, char *error); extern void blr_file_flush(ROUTER_INSTANCE *); extern BLFILE *blr_open_binlog(ROUTER_INSTANCE *, const char *, diff --git a/server/modules/routing/binlogrouter/blr_file.cc b/server/modules/routing/binlogrouter/blr_file.cc index 526166554..b0fa17365 100644 --- a/server/modules/routing/binlogrouter/blr_file.cc +++ b/server/modules/routing/binlogrouter/blr_file.cc @@ -28,6 +28,8 @@ #include #include +#include + #include #include #include @@ -3102,6 +3104,193 @@ blr_file_new_binlog(ROUTER_INSTANCE *router, char *file) return blr_file_create(router, file); } +namespace +{ + +/** + * Configuration handler for items in the [binlog_configuration] section + * + * @param name The item name + * @param value The item value + * @param inst The current router instance + * @return 0 on error + */ +int blr_handle_config_item(const char *name, + const char *value, + ROUTER_INSTANCE *inst) +{ + SERVICE *service; + char *ssl_cert; + char *ssl_key; + char *ssl_ca_cert; + SERVER *backend_server; + + service = inst->service; + + backend_server = service->dbref->server; + + if (strcmp(name, "master_host") == 0) + { + server_update_address(service->dbref->server, (char *)value); + } + else if (strcmp(name, "master_port") == 0) + { + server_update_port(service->dbref->server, (short)atoi(value)); + } + else if (strcmp(name, "filestem") == 0) + { + MXS_FREE(inst->fileroot); + inst->fileroot = MXS_STRDUP_A(value); + } + else if (strcmp(name, "master_user") == 0) + { + if (inst->user) + { + MXS_FREE(inst->user); + } + inst->user = MXS_STRDUP_A(value); + } + else if (strcmp(name, "master_password") == 0) + { + if (inst->password) + { + MXS_FREE(inst->password); + } + inst->password = MXS_STRDUP_A(value); + } + /** Checl for SSL options */ + else if (strcmp(name, "master_ssl") == 0) + { + inst->ssl_enabled = config_truth_value((char*)value); + } + else if (strcmp(name, "master_ssl_ca") == 0) + { + MXS_FREE(backend_server->server_ssl->ssl_ca_cert); + backend_server->server_ssl->ssl_ca_cert = value ? MXS_STRDUP_A(value) : NULL; + MXS_FREE(inst->ssl_ca); + inst->ssl_ca = value ? MXS_STRDUP_A(value) : NULL; + } + else if (strcmp(name, "master_ssl_cert") == 0) + { + MXS_FREE(backend_server->server_ssl->ssl_cert); + backend_server->server_ssl->ssl_cert = value ? MXS_STRDUP_A(value) : NULL; + MXS_FREE(inst->ssl_cert); + inst->ssl_cert = value ? MXS_STRDUP_A(value) : NULL; + } + else if (strcmp(name, "master_ssl_key") == 0) + { + MXS_FREE(backend_server->server_ssl->ssl_key); + backend_server->server_ssl->ssl_key = value ? MXS_STRDUP_A(value) : NULL; + MXS_FREE(inst->ssl_key); + inst->ssl_key = value ? MXS_STRDUP_A(value) : NULL; + } + else if (strcmp(name, "master_ssl_version") == 0 || strcmp(name, "master_tls_version") == 0) + { + if (value) + { + if (listener_set_ssl_version(backend_server->server_ssl, (char *)value) != 0) + { + MXS_ERROR("Found unknown optional parameter value for 'ssl_version' for" + " service '%s': %s, ignoring it.", + inst->service->name, + value); + } + else + { + inst->ssl_version = MXS_STRDUP_A(value); + } + } + } + /* Connect options */ + else if (strcmp(name, "master_heartbeat_period") == 0) + { + int new_val = atol((char *)value); + if (new_val < 0) + { + MXS_WARNING("Found invalid 'master_heartbeat_period' value" + " for service '%s': %s, ignoring it.", + inst->service->name, + value); + } + else + { + if (inst->heartbeat > 0 && new_val == 0) + { + blr_log_disabled_heartbeat(inst); + } + inst->heartbeat = new_val; + } + } + else if (strcmp(name, "master_connect_retry") == 0) + { + int new_val = atol((char *)value); + if (new_val <= 0) + { + MXS_WARNING("Found invalid 'master_connect_retry' value" + " for service '%s': %s, ignoring it.", + inst->service->name, + value); + } + else + { + inst->retry_interval = new_val; + } + } + else + { + return 0; + } + + return 1; +} + +/** + * Config item handler for the ini file reader + * + * @param userdata The config context element + * @param section The config file section + * @param name The Parameter name + * @param value The Parameter value + * @return zero on error + */ + +int blr_handler_config(void *userdata, const char *section, const char *name, const char *value) +{ + ROUTER_INSTANCE *inst = (ROUTER_INSTANCE *) userdata; + SERVICE *service; + + service = inst->service; + + if (strcasecmp(section, "binlog_configuration") == 0) + { + return blr_handle_config_item(name, value, inst); + } + else + { + MXS_ERROR("master.ini has an invalid section [%s], it should be [binlog_configuration]. " + "Service %s", + section, + service->name); + + return 0; + } +} + +} + +int blr_file_read_master_config(ROUTER_INSTANCE *router) +{ + static const char MASTER_INI[] = "/master.ini"; + char filename[strlen(router->binlogdir) + sizeof(MASTER_INI)]; // sizeof includes the NULL + sprintf(filename, "%s%s", router->binlogdir, MASTER_INI); + + int rc = ini_parse(filename, blr_handler_config, router); + + MXS_INFO("%s: %s parse result is %d", router->service->name, filename, rc); + + return rc; +} + /** * Write a new ini file with master configuration *