Skip to content

Commit

Permalink
Merge pull request #119 from liuyanlei25/master
Browse files Browse the repository at this point in the history
check dns
  • Loading branch information
tsthght authored Aug 16, 2018
2 parents a0ddb98 + d5c0c7c commit a93f46a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/cetus-monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "sharding-config.h"

#include <netdb.h>
#include <arpa/inet.h>

#define CHECK_ALIVE_INTERVAL 3
#define CHECK_ALIVE_TIMES 2
Expand Down Expand Up @@ -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)
{
Expand All @@ -407,13 +427,19 @@ 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)
break;
}

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);
Expand Down Expand Up @@ -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) {
Expand Down Expand 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);
Expand Down
1 change: 1 addition & 0 deletions src/chassis-mainloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions src/chassis-options-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,3 +1444,43 @@ 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)) {
return g_strdup_printf("%s", srv->check_dns ? "true" : "false");
}
if (CAN_SAVE_OPTS_PROPERTY(opt_type)) {
return (srv->check_dns == 0) ? g_strdup("false") : NULL;
}
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;
}
2 changes: 2 additions & 0 deletions src/chassis-options-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions src/mysql-proxy-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ struct chassis_frontend_t {
gchar *sql_log_mode;
guint sql_log_idletime;
gint sql_log_maxnum;

int check_dns;
};

/**
Expand Down Expand Up @@ -200,6 +202,9 @@ 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;
}

Expand Down Expand Up @@ -533,6 +538,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","<int>",
assign_sql_log_maxnum, show_sql_log_maxnum, ALL_OPTS_PROPERTY);
chassis_options_add(opts,
"check-dns",
0, 0, OPTION_ARG_NONE, &(frontend->check_dns),
"check dns when hostname changed",NULL,
assign_check_dns, show_check_dns, ALL_OPTS_PROPERTY);

return 0;
}
Expand Down Expand Up @@ -696,6 +706,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
Expand Down Expand Up @@ -1191,6 +1202,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);
Expand Down

0 comments on commit a93f46a

Please sign in to comment.