From c865988bfa3960f79a999eab886f885635e60588 Mon Sep 17 00:00:00 2001 From: liuyanlei25 <15510772982@163.com> Date: Thu, 16 Aug 2018 13:59:43 +0800 Subject: [PATCH] add dns check --- src/cetus-monitor.c | 38 +++++++++++++++++++++++++++++++++++++ src/chassis-mainloop.h | 1 + src/chassis-options-utils.c | 37 ++++++++++++++++++++++++++++++++++++ src/chassis-options-utils.h | 2 ++ src/mysql-proxy-cli.c | 9 +++++++++ 5 files changed, 87 insertions(+) diff --git a/src/cetus-monitor.c b/src/cetus-monitor.c index 73b42ff3..32f00b07 100644 --- a/src/cetus-monitor.c +++ b/src/cetus-monitor.c @@ -42,6 +42,7 @@ #include "sharding-config.h" #include +#include #define CHECK_ALIVE_INTERVAL 3 #define CHECK_ALIVE_TIMES 2 @@ -384,6 +385,25 @@ group_replication_detect(network_backends_t *bs, cetus_monitor_t *monitor) event_base_set(monitor->evloop, &(monitor->ev_struct));\ evtimer_add(&(monitor->ev_struct), &timeout); +gint +check_hostname(network_backend_t *backend) +{ + gint ret = 0; + gchar *p = NULL; + if (!backend) return ret; + gchar old_addr[INET_ADDRSTRLEN] = {""}; + inet_ntop(AF_INET, &(backend->addr->addr.ipv4.sin_addr), old_addr, sizeof(old_addr)); + if (0 != network_address_set_address(backend->addr, backend->address->str)) { + return ret; + } + char new_addr[INET_ADDRSTRLEN] = {""}; + inet_ntop(AF_INET, &(backend->addr->addr.ipv4.sin_addr), new_addr, sizeof(new_addr)); + if (strcmp (old_addr, new_addr) != 0) { + ret = 1; + } + return ret; + } + static void check_backend_alive(int fd, short what, void *arg) { @@ -407,6 +427,7 @@ check_backend_alive(int fd, short what, void *arg) char *backend_addr = backend->addr->name->str; int check_count = 0; MYSQL *conn = NULL; +hostnameloop: while (++check_count <= CHECK_ALIVE_TIMES) { conn = get_mysql_connection(monitor, backend_addr); if (conn) @@ -414,6 +435,11 @@ check_backend_alive(int fd, short what, void *arg) } if (conn == NULL) { + if (chas->check_dns) { + if (check_hostname(backend)) { + goto hostnameloop; + } + } if (backend->state != BACKEND_STATE_DOWN) { if (backend->type != BACKEND_TYPE_RW) { ret = network_backends_modify(bs, i, backend->type, BACKEND_STATE_DOWN, oldstate); @@ -482,8 +508,14 @@ update_master_timestamp(int fd, short what, void *arg) HEARTBEAT_DB, monitor->config_id, cur_time_str, cur_time_str); char *backend_addr = backend->addr->name->str; +hostnameloop:; MYSQL *conn = get_mysql_connection(monitor, backend_addr); if (conn == NULL) { + if (chas->check_dns) { + if (check_hostname(backend)) { + goto hostnameloop; + } + } g_critical("Could not connect to Backend %s.", backend_addr); } else { if (backend->state != BACKEND_STATE_UP) { @@ -532,8 +564,14 @@ check_slave_timestamp(int fd, short what, void *arg) continue; char *backend_addr = backend->addr->name->str; +hostnameloop:; MYSQL *conn = get_mysql_connection(monitor, backend_addr); if (conn == NULL) { + if (chas->check_dns) { + if (check_hostname(backend)) { + goto hostnameloop; + } + } g_critical("Connection error when read delay from RO backend: %s", backend_addr); if (backend->state != BACKEND_STATE_DOWN) { ret = network_backends_modify(bs, i, backend->type, BACKEND_STATE_DOWN, oldstate); diff --git a/src/chassis-mainloop.h b/src/chassis-mainloop.h index 72b92c1a..15f56eb9 100644 --- a/src/chassis-mainloop.h +++ b/src/chassis-mainloop.h @@ -201,6 +201,7 @@ struct chassis { struct event update_timer_event; struct sql_log_mgr *sql_mgr; + gint check_dns; }; CHASSIS_API chassis *chassis_new(void); diff --git a/src/chassis-options-utils.c b/src/chassis-options-utils.c index cbc0a551..28e6b816 100644 --- a/src/chassis-options-utils.c +++ b/src/chassis-options-utils.c @@ -1444,3 +1444,40 @@ assign_sql_log_maxnum(const gchar *newval, gpointer param) { } return ret; } + +gchar* +show_check_dns(gpointer param) { + struct external_param *opt_param = (struct external_param *)param; + chassis *srv = opt_param->chas; + gint opt_type = opt_param->opt_type; + if (CAN_SHOW_OPTS_PROPERTY(opt_type) || CAN_SAVE_OPTS_PROPERTY(opt_type)) { + return g_strdup_printf("%d", srv->check_dns); + } + return NULL; +} + +gint +assign_check_dns(const gchar *newval, gpointer param) { + gint ret = ASSIGN_ERROR; + struct external_param *opt_param = (struct external_param *)param; + chassis *srv = opt_param->chas; + gint opt_type = opt_param->opt_type; + if (CAN_ASSIGN_OPTS_PROPERTY(opt_type)) { + if (NULL != newval) { + gint value = 0; + if (try_get_int_value(newval, &value)) { + if (value < 0) { + ret = ASSIGN_VALUE_INVALID; + } else { + srv->check_dns = value; + ret = ASSIGN_OK; + } + } else { + ret = ASSIGN_VALUE_INVALID; + } + } else { + ret = ASSIGN_VALUE_INVALID; + } + } + return ret; +} diff --git a/src/chassis-options-utils.h b/src/chassis-options-utils.h index 61750497..5c2a0dfe 100644 --- a/src/chassis-options-utils.h +++ b/src/chassis-options-utils.h @@ -100,6 +100,7 @@ CHASSIS_API gchar* show_sql_log_maxsize(gpointer param); CHASSIS_API gchar* show_sql_log_mode(gpointer param); CHASSIS_API gchar* show_sql_log_idletime(gpointer param); CHASSIS_API gchar* show_sql_log_maxnum(gpointer param); +CHASSIS_API gchar* show_check_dns(gpointer param); /* assign utils */ CHASSIS_API gint assign_log_level(const gchar *newval, gpointer param); @@ -124,6 +125,7 @@ CHASSIS_API gint assign_sql_log_switch(const gchar *newval, gpointer param); CHASSIS_API gint assign_sql_log_mode(const gchar *newval, gpointer param); CHASSIS_API gint assign_sql_log_idletime(const gchar *newval, gpointer param); CHASSIS_API gint assign_sql_log_maxnum(const gchar *newval, gpointer param); +CHASSIS_API gint assign_check_dns(const gchar *newval, gpointer param); CHASSIS_API gint chassis_options_save(GKeyFile *keyfile, chassis_options_t *opts, chassis *chas); diff --git a/src/mysql-proxy-cli.c b/src/mysql-proxy-cli.c index d79f3ce0..154878cc 100644 --- a/src/mysql-proxy-cli.c +++ b/src/mysql-proxy-cli.c @@ -158,6 +158,7 @@ struct chassis_frontend_t { gchar *sql_log_mode; guint sql_log_idletime; gint sql_log_maxnum; + gint check_dns; }; /** @@ -200,6 +201,7 @@ chassis_frontend_new(void) frontend->sql_log_mode = NULL; frontend->sql_log_idletime = 0; frontend->sql_log_maxnum = -1; + frontend->check_dns = 0; return frontend; } @@ -533,6 +535,11 @@ chassis_frontend_set_chassis_options(struct chassis_frontend_t *frontend, chassi 0, 0, OPTION_ARG_INT, &(frontend->sql_log_maxnum), "aximum number of sql log files","", assign_sql_log_maxnum, show_sql_log_maxnum, ALL_OPTS_PROPERTY); + chassis_options_add(opts, + "check-dns", + 0, 0, OPTION_ARG_INT, &(frontend->check_dns), + "check dns when hostname changed","", + assign_check_dns, show_check_dns, ALL_OPTS_PROPERTY); return 0; } @@ -696,6 +703,7 @@ init_parameters(struct chassis_frontend_t *frontend, chassis *srv) srv->long_query_time = MIN(frontend->long_query_time, MAX_QUERY_TIME); srv->cetus_max_allowed_packet = CLAMP(frontend->cetus_max_allowed_packet, MAX_ALLOWED_PACKET_FLOOR, MAX_ALLOWED_PACKET_CEIL); + srv->check_dns = frontend->check_dns; } static void @@ -1191,6 +1199,7 @@ main_cmdline(int argc, char **argv) srv->sql_mgr->sql_log_maxnum = frontend->sql_log_maxnum; } } + srv->check_dns = frontend->check_dns; cetus_monitor_start_thread(srv->priv->monitor, srv); cetus_sql_log_start_thread_once(srv->sql_mgr);