Skip to content

Commit

Permalink
Add functionality to set keepalive tcp options
Browse files Browse the repository at this point in the history
  • Loading branch information
leoleovich committed Jun 8, 2017
1 parent 22e5e64 commit 24ff21e
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/violite.h
Expand Up @@ -84,6 +84,7 @@ my_bool vio_is_blocking(Vio *vio);
int vio_fastsend(Vio *vio);
/* setsockopt SO_KEEPALIVE at SOL_SOCKET level, when possible */
int vio_keepalive(Vio *vio, my_bool onoff);
int vio_keepalive_options(Vio *vio, int option, int value);
/* Whenever we should retry the last read/write operation. */
my_bool vio_should_retry(Vio *vio);
/* Check that operation was timed out */
Expand Down
1 change: 1 addition & 0 deletions sql/mysqld.cc
Expand Up @@ -533,6 +533,7 @@ ulong slave_retried_transactions;
ulonglong slave_skipped_errors;
ulong feature_files_opened_with_delayed_keys= 0, feature_check_constraint= 0;
ulonglong denied_connections;
ulong keepalive_time=0, keepalive_intvl=0, keepalive_probes=0;
my_decimal decimal_zero;

/*
Expand Down
1 change: 1 addition & 0 deletions sql/mysqld.h
Expand Up @@ -269,6 +269,7 @@ extern ulong log_warnings;
extern my_bool encrypt_binlog;
extern my_bool encrypt_tmp_disk_tables, encrypt_tmp_files;
extern ulong encryption_algorithm;
extern ulong keepalive_time, keepalive_intvl, keepalive_probes;
extern const char *encryption_algorithm_names[];

#ifdef HAVE_PSI_INTERFACE
Expand Down
1 change: 1 addition & 0 deletions sql/sql_class.h
Expand Up @@ -701,6 +701,7 @@ typedef struct system_variables
my_bool session_track_state_change;

ulong threadpool_priority;
ulong keepalive_time, keepalive_intvl, keepalive_probes;
} SV;

/**
Expand Down
28 changes: 27 additions & 1 deletion sql/sql_connect.cc
Expand Up @@ -1000,7 +1000,33 @@ static int check_connection(THD *thd)
bzero((char*) &net->vio->remote, sizeof(net->vio->remote));
}
vio_keepalive(net->vio, TRUE);


#ifdef TCP_KEEPCNT
if (global_system_variables.keepalive_probes != 9)
{
vio_keepalive_options(net->vio, TCP_KEEPCNT, global_system_variables.keepalive_probes);
}
#endif

#ifdef TCP_KEEPIDLE
if (global_system_variables.keepalive_time != 7200)
{
vio_keepalive_options(net->vio, TCP_KEEPIDLE, global_system_variables.keepalive_time);
}
#else
if (global_system_variables.keepalive_time != 7200)
{
vio_keepalive_options(net->vio, TCP_KEEPALIVE, global_system_variables.keepalive_time);
}
#endif

#ifdef TCP_KEEPINTVL
if (global_system_variables.keepalive_intvl != 75)
{
vio_keepalive_options(net->vio, TCP_KEEPINTVL, global_system_variables.keepalive_intvl);
}
#endif

if (thd->packet.alloc(thd->variables.net_buffer_length))
{
/*
Expand Down
3 changes: 3 additions & 0 deletions sql/sql_const.h
Expand Up @@ -112,6 +112,9 @@
#define ACL_CACHE_SIZE 256
#define MAX_PASSWORD_LENGTH 32
#define HOST_CACHE_SIZE 128
#define KEEPALIVE_TIME 7200
#define KEEPALIVE_INTVL 75
#define KEEPALIVE_PROBES 9
#define MAX_ACCEPT_RETRY 10 // Test accept this many times
#define MAX_FIELDS_BEFORE_HASH 32
#define USER_VARS_HASH_SIZE 16
Expand Down
27 changes: 27 additions & 0 deletions sql/sys_vars.cc
Expand Up @@ -5073,6 +5073,33 @@ static Sys_var_ulong Sys_host_cache_size(
NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL),
ON_UPDATE(fix_host_cache_size));

static Sys_var_ulong Sys_keepalive_time_seconds(
"keepalive_time",
"The interval between the last data packet sent and the first keepalive probe.",
AUTO_SET GLOBAL_VAR(keepalive_time),
CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 65536),
DEFAULT(KEEPALIVE_TIME),
BLOCK_SIZE(1),
NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL));

static Sys_var_ulong Sys_keepalive_intvl(
"keepalive_intvl",
"The interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime.",
AUTO_SET GLOBAL_VAR(keepalive_intvl),
CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 65536),
DEFAULT(KEEPALIVE_INTVL),
BLOCK_SIZE(1),
NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL));

static Sys_var_ulong Sys_keepalive_probes(
"keepalive_probes",
"The number of unacknowledged probes to send before considering the connection dead and notifying the application layer.",
AUTO_SET GLOBAL_VAR(keepalive_probes),
CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 65536),
DEFAULT(KEEPALIVE_PROBES),
BLOCK_SIZE(1),
NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL));

static Sys_var_charptr Sys_ignore_db_dirs(
"ignore_db_dirs",
"Specifies a directory to add to the ignore list when collecting "
Expand Down
1 change: 1 addition & 0 deletions vio/vio.c
Expand Up @@ -125,6 +125,7 @@ static void vio_init(Vio *vio, enum enum_vio_type type,
vio->write =vio_write_shared_memory;
vio->fastsend =vio_fastsend;
vio->viokeepalive =vio_keepalive;
vio->viokeepaliveoption =vio_keepalive_options;
vio->should_retry =vio_should_retry;
vio->was_timeout =vio_was_timeout;
vio->vioclose =vio_close_shared_memory;
Expand Down
26 changes: 26 additions & 0 deletions vio/viosocket.c
Expand Up @@ -37,6 +37,14 @@
# include <sys/filio.h>
#endif

#ifndef SOL_TCP
# ifdef IPPROTO_TCP
# define SOL_TCP IPPROTO_TCP
# else
# define SOL_TCP 6
# endif /* IPPROTO_TCP */
#endif /* SOL_TCP */

/* Network io wait callbacks for threadpool */
static void (*before_io_wait)(void)= 0;
static void (*after_io_wait)(void)= 0;
Expand Down Expand Up @@ -522,6 +530,24 @@ int vio_keepalive(Vio* vio, my_bool set_keep_alive)
DBUG_RETURN(r);
}

/*
Set socket options for keepalive e.g., TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL
*/
int vio_keepalive_options(Vio* vio, int option, int value)
{
int r=0;
DBUG_ENTER("vio_keepalive_options");
DBUG_PRINT("enter", ("sd: %d set_keep_alive_options: %d, %d",
mysql_socket_getfd(vio->mysql_socket),
option, value));

if (vio->type != VIO_TYPE_NAMEDPIPE && vio->type != VIO_TYPE_SHARED_MEMORY)
{
r = mysql_socket_setsockopt(vio->mysql_socket, SOL_TCP, option,
(char *)&value, sizeof(value));
}
DBUG_RETURN(r);
}

/**
Indicate whether a I/O operation must be retried later.
Expand Down

0 comments on commit 24ff21e

Please sign in to comment.