Skip to content

Commit

Permalink
misc. sql/slave.cc & co. refactor
Browse files Browse the repository at this point in the history
* `sql/mysqld.cc`: init `master-retry-count` with `master_retry_count`
* `get_master_version_and_clock()` de-duplicate label using fall-through
* `io_slave_killed()` & `check_io_slave_killed()`:
  * reüse the result from the level lower
  * add distinguishing docs
* `try_to_reconnect()`: extract `'` from `if`-`else`
* `handle_slave_io()`: Both `while`s have the same condition;
  looks like the outer `while` can simply be an `if`.
* `connect_to_master()`:
  * assume `mysql_errno()` is not 0 on connection error
  * utilize 0’s falsiness in the loop
  * remove kill check around error reporting –
    other kill checks don’t even use this result
  * extend docs
  • Loading branch information
ParadoxV5 committed Jan 16, 2025
1 parent 7eee388 commit 4b78bf8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
1 change: 0 additions & 1 deletion mysql-test/include/check-testcase.inc
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,3 @@ if (`SELECT COUNT(*)=1 FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'wsre
}

--enable_query_log

4 changes: 2 additions & 2 deletions sql/mysqld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ File_parser_dummy_hook file_parser_dummy_hook;

/* replication parameters, if master_host is not NULL, we are a slave */
uint report_port= 0;
ulong master_retry_count=0;
ulong master_retry_count=100000;
char *master_info_file;
char *relay_log_info_file, *report_user, *report_password, *report_host;
char *opt_relay_logname = 0, *opt_relaylog_index_name=0;
Expand Down Expand Up @@ -6820,7 +6820,7 @@ struct my_option my_long_options[]=
{"master-retry-count", 0,
"The number of tries the slave will make to connect to the master before giving up",
&master_retry_count, &master_retry_count, 0, GET_ULONG,
REQUIRED_ARG, 100000, 0, 0, 0, 0, 0},
REQUIRED_ARG, static_cast<longlong>(master_retry_count), 0, 0, 0, 0, 0},
#ifdef HAVE_REPLICATION
{"init-rpl-role", 0, "Set the replication role",
&rpl_status, &rpl_status, &rpl_role_typelib,
Expand Down
60 changes: 27 additions & 33 deletions sql/slave.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1293,14 +1293,15 @@ void end_slave()
DBUG_VOID_RETURN;
}

/** @return whether the replica's Master_info is marked as killed */
static bool io_slave_killed(Master_info* mi)
{
DBUG_ENTER("io_slave_killed");

DBUG_ASSERT(mi->slave_running); // tracking buffer overrun
if (mi->abort_slave || mi->io_thd->killed)
bool is_io_slave_killed= mi->abort_slave || mi->io_thd->killed;
if (is_io_slave_killed)
DBUG_PRINT("info", ("killed"));
DBUG_RETURN(mi->abort_slave || mi->io_thd->killed);
DBUG_RETURN(is_io_slave_killed);
}

/**
Expand Down Expand Up @@ -2585,10 +2586,6 @@ when it try to get the value of TIME_ZONE global variable from master.";
DBUG_RETURN(0);

network_err:
if (master_res)
mysql_free_result(master_res);
DBUG_RETURN(2);

slave_killed_err:
if (master_res)
mysql_free_result(master_res);
Expand Down Expand Up @@ -4361,15 +4358,13 @@ on this slave.\
}


/** Return io_slave_killed(); if it's `true`, also log the given `info`. */
static bool check_io_slave_killed(Master_info *mi, const char *info)
{
if (io_slave_killed(mi))
{
if (info && global_system_variables.log_warnings)
sql_print_information("%s", info);
return TRUE;
}
return FALSE;
bool is_io_slave_killed= io_slave_killed(mi);
if (is_io_slave_killed && info && global_system_variables.log_warnings)
sql_print_information("%s", info);
return is_io_slave_killed;
}

/**
Expand Down Expand Up @@ -4416,11 +4411,10 @@ static int try_to_reconnect(THD *thd, MYSQL *mysql, Master_info *mi,
{
tmp.append(STRING_WITH_LEN("; GTID position '"));
mi->gtid_current_pos.append_to_string(&tmp);
if (mi->events_queued_since_last_gtid == 0)
tmp.append(STRING_WITH_LEN("'"));
else
tmp.append('\'');
if (mi->events_queued_since_last_gtid)
{
tmp.append(STRING_WITH_LEN("', GTID event skip "));
tmp.append(STRING_WITH_LEN(", GTID event skip "));
tmp.append_ulonglong((ulonglong)mi->events_queued_since_last_gtid);
}
}
Expand Down Expand Up @@ -4664,7 +4658,7 @@ pthread_handler_t handle_slave_io(void *arg)

DBUG_PRINT("info",("Starting reading binary log from master"));
thd->set_command(COM_SLAVE_IO);
while (!io_slave_killed(mi))
if (!io_slave_killed(mi))
{
const uchar *event_buf;

Expand Down Expand Up @@ -6917,20 +6911,20 @@ static int safe_connect(THD* thd, MYSQL* mysql, Master_info* mi)
}


/*
SYNPOSIS
connect_to_master()
IMPLEMENTATION
Try to connect until successful or slave killed or we have retried
mi->retry_count times
/**
@brief Re/connect to the primary
@details
After preparations, this repeatedly calls the low-level connection function
up to mi->retry_count times until success or when the replica's killed.
Logs describe either config mistakes or generic connection statuses.
@param reconnect whether this is a reconnection or a new first-time connection
@return errno: 1 if error or 0 if successful
*/

static int connect_to_master(THD* thd, MYSQL* mysql, Master_info* mi,
bool reconnect, bool suppress_warnings)
{
int slave_was_killed;
int last_errno= -2; // impossible error
unsigned int last_errno= 0; // initialize with not-error
mi->connects_tried= 0; // reset retry counter
my_bool my_true= 1;
DBUG_ENTER("connect_to_master");
Expand Down Expand Up @@ -6991,13 +6985,13 @@ static int connect_to_master(THD* thd, MYSQL* mysql, Master_info* mi,
"terminated.");
DBUG_RETURN(1);
}
while (!(slave_was_killed = io_slave_killed(mi)) &&
(reconnect ? mysql_reconnect(mysql) != 0 :
mysql_real_connect(mysql, mi->host, mi->user, mi->password, 0,
mi->port, 0, client_flag) == 0))
while (!(slave_was_killed= io_slave_killed(mi)) &&
(reconnect ? mysql_reconnect(mysql) :
!mysql_real_connect(mysql, mi->host, mi->user, mi->password, 0,
mi->port, 0, client_flag)))
{
/* Don't repeat last error */
if ((int)mysql_errno(mysql) != last_errno && !io_slave_killed(mi))
if (mysql_errno(mysql) != last_errno)
{
last_errno=mysql_errno(mysql);
suppress_warnings= 0;
Expand Down

0 comments on commit 4b78bf8

Please sign in to comment.