Skip to content

Commit 5091986

Browse files
committed
misc. sql/slave.cc & co. refactor
* `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 * extend docs * `sql/sql_show.cc`: refactor SHOW ALL REPLICAS filter’s condition * `sql/mysqld.cc`: init `master-retry-count` with `master_retry_count` Reviewed-by: Kristian Nielsen <knielsen@knielsen-hq.org>
1 parent e2dbd9b commit 5091986

File tree

4 files changed

+34
-46
lines changed

4 files changed

+34
-46
lines changed

mysql-test/include/check-testcase.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,3 @@ if (`SELECT COUNT(*)=1 FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'wsre
163163
}
164164

165165
--enable_query_log
166-

sql/mysqld.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,9 @@ mysql_cond_t COND_server_started;
778778
int mysqld_server_started=0, mysqld_server_initialized= 0;
779779
File_parser_dummy_hook file_parser_dummy_hook;
780780

781-
/* replication parameters, if master_host is not NULL, we are a slave */
781+
/* replication parameters */
782782
uint report_port= 0;
783-
ulong master_retry_count=0;
783+
ulong master_retry_count=100000;
784784
char *master_info_file;
785785
char *relay_log_info_file, *report_user, *report_password, *report_host;
786786
char *opt_relay_logname = 0, *opt_relaylog_index_name=0;
@@ -6817,7 +6817,7 @@ struct my_option my_long_options[]=
68176817
{"master-retry-count", 0,
68186818
"The number of tries the slave will make to connect to the master before giving up",
68196819
&master_retry_count, &master_retry_count, 0, GET_ULONG,
6820-
REQUIRED_ARG, 100000, 0, 0, 0, 0, 0},
6820+
REQUIRED_ARG, static_cast<longlong>(master_retry_count), 0, 0, 0, 0, 0},
68216821
#ifdef HAVE_REPLICATION
68226822
{"init-rpl-role", 0, "Set the replication role",
68236823
&rpl_status, &rpl_status, &rpl_role_typelib,

sql/slave.cc

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,14 +1308,15 @@ void end_slave()
13081308
DBUG_VOID_RETURN;
13091309
}
13101310

1311+
/** @return whether the slave's Master_info is marked as killed */
13111312
static bool io_slave_killed(Master_info* mi)
13121313
{
13131314
DBUG_ENTER("io_slave_killed");
1314-
13151315
DBUG_ASSERT(mi->slave_running); // tracking buffer overrun
1316-
if (mi->abort_slave || mi->io_thd->killed)
1316+
bool is_io_slave_killed= mi->abort_slave || mi->io_thd->killed;
1317+
if (is_io_slave_killed)
13171318
DBUG_PRINT("info", ("killed"));
1318-
DBUG_RETURN(mi->abort_slave || mi->io_thd->killed);
1319+
DBUG_RETURN(is_io_slave_killed);
13191320
}
13201321

13211322
/**
@@ -2600,10 +2601,6 @@ when it try to get the value of TIME_ZONE global variable from master.";
26002601
DBUG_RETURN(0);
26012602

26022603
network_err:
2603-
if (master_res)
2604-
mysql_free_result(master_res);
2605-
DBUG_RETURN(2);
2606-
26072604
slave_killed_err:
26082605
if (master_res)
26092606
mysql_free_result(master_res);
@@ -4377,15 +4374,13 @@ on this slave.\
43774374
}
43784375

43794376

4377+
/** Return io_slave_killed(); if it's `true`, also log the given `info`. */
43804378
static bool check_io_slave_killed(Master_info *mi, const char *info)
43814379
{
4382-
if (io_slave_killed(mi))
4383-
{
4384-
if (info && global_system_variables.log_warnings)
4385-
sql_print_information("%s", info);
4386-
return TRUE;
4387-
}
4388-
return FALSE;
4380+
bool is_io_slave_killed= io_slave_killed(mi);
4381+
if (is_io_slave_killed && info && global_system_variables.log_warnings)
4382+
sql_print_information("%s", info);
4383+
return is_io_slave_killed;
43894384
}
43904385

43914386
/**
@@ -4430,11 +4425,10 @@ static int try_to_reconnect(THD *thd, MYSQL *mysql, Master_info *mi,
44304425
{
44314426
tmp.append(STRING_WITH_LEN("; GTID position '"));
44324427
mi->gtid_current_pos.append_to_string(&tmp);
4433-
if (mi->events_queued_since_last_gtid == 0)
4434-
tmp.append(STRING_WITH_LEN("'"));
4435-
else
4428+
tmp.append('\'');
4429+
if (mi->events_queued_since_last_gtid)
44364430
{
4437-
tmp.append(STRING_WITH_LEN("', GTID event skip "));
4431+
tmp.append(STRING_WITH_LEN(", GTID event skip "));
44384432
tmp.append_ulonglong((ulonglong)mi->events_queued_since_last_gtid);
44394433
}
44404434
}
@@ -4678,7 +4672,7 @@ pthread_handler_t handle_slave_io(void *arg)
46784672

46794673
DBUG_PRINT("info",("Starting reading binary log from master"));
46804674
thd->set_command(COM_SLAVE_IO);
4681-
while (!io_slave_killed(mi))
4675+
if (!io_slave_killed(mi))
46824676
{
46834677
const uchar *event_buf;
46844678

@@ -6931,20 +6925,21 @@ static int safe_connect(THD* thd, MYSQL* mysql, Master_info* mi)
69316925
}
69326926

69336927

6934-
/*
6935-
SYNPOSIS
6936-
connect_to_master()
6937-
6938-
IMPLEMENTATION
6939-
Try to connect until successful or slave killed or we have retried
6940-
mi->retry_count times
6928+
/**
6929+
@brief Re/connect to the master `mi`
6930+
@details
6931+
After preparations and config validations,
6932+
this repeatedly calls the low-level connection function (and logs statuses)
6933+
up to mi->retry_count times until success or when io_slave_killed().
6934+
@param reconnect
6935+
whether this connection is a new first-time or reconnects an existing one
6936+
@return errno: 1 if error or 0 if successful
69416937
*/
6942-
69436938
static int connect_to_master(THD* thd, MYSQL* mysql, Master_info* mi,
69446939
bool reconnect, bool suppress_warnings)
69456940
{
69466941
int slave_was_killed;
6947-
int last_errno= -2; // impossible error
6942+
unsigned int last_errno= 0; // initialize with not-error
69486943
mi->connects_tried= 0; // reset retry counter
69496944
my_bool my_true= 1;
69506945
DBUG_ENTER("connect_to_master");
@@ -7005,13 +7000,13 @@ static int connect_to_master(THD* thd, MYSQL* mysql, Master_info* mi,
70057000
"terminated.");
70067001
DBUG_RETURN(1);
70077002
}
7008-
while (!(slave_was_killed = io_slave_killed(mi)) &&
7009-
(reconnect ? mysql_reconnect(mysql) != 0 :
7010-
mysql_real_connect(mysql, mi->host, mi->user, mi->password, 0,
7011-
mi->port, 0, client_flag) == 0))
7003+
while (!(slave_was_killed= io_slave_killed(mi)) &&
7004+
(reconnect ? mysql_reconnect(mysql) :
7005+
!mysql_real_connect(mysql, mi->host, mi->user, mi->password, 0,
7006+
mi->port, 0, client_flag)))
70127007
{
7013-
/* Don't repeat last error */
7014-
if ((int)mysql_errno(mysql) != last_errno && !io_slave_killed(mi))
7008+
/* Don't repeat last error and don't report killed error */
7009+
if (mysql_errno(mysql) != last_errno && !io_slave_killed(mi))
70157010
{
70167011
last_errno=mysql_errno(mysql);
70177012
suppress_warnings= 0;

sql/sql_show.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9226,18 +9226,12 @@ static int make_slave_status_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
92269226
for (uint i=0; !field_info->end_marker(); field_info++, i++)
92279227
{
92289228
if (all_slaves ||
9229-
// not SLAVE_STATUS_COL_CONNECTION_NAME,
9230-
// SLAVE_STATUS_COL_SLAVE_SQL_STATE
9231-
// and less
9232-
// SLAVE_STATUS_COL_RETRIED_TRANSACTIONS
9233-
!(i <= SLAVE_STATUS_COL_SLAVE_SQL_STATE ||
9234-
i >= SLAVE_STATUS_COL_RETRIED_TRANSACTIONS))
9229+
(i > SLAVE_STATUS_COL_SLAVE_SQL_STATE &&
9230+
i < SLAVE_STATUS_COL_RETRIED_TRANSACTIONS))
92359231
{
92369232
LEX_CSTRING field_name= field_info->name();
92379233
Item_field *field= new (thd->mem_root)
92389234
Item_field(thd, context, field_name);
9239-
DBUG_ASSERT(all_slaves || (i > SLAVE_STATUS_COL_SLAVE_SQL_STATE &&
9240-
i < SLAVE_STATUS_COL_RETRIED_TRANSACTIONS));
92419235
if (!field || add_item_to_list(thd, field))
92429236
return 1;
92439237
}

0 commit comments

Comments
 (0)