1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-01 03:47:19 +03:00

MDEV-27247: Add keywords "SQL_BEFORE_GTIDS" and "SQL_AFTER_GTIDS" for START SLAVE UNTIL

New Feature:
============
This patch extends the START SLAVE UNTIL command with options
SQL_BEFORE_GTIDS and SQL_AFTER_GTIDS to allow user control of
whether the replica stops before or after a provided GTID state. Its
syntax is:

START SLAVE UNTIL (SQL_BEFORE_GTIDS|SQL_AFTER_GTIDS)=”<gtid_list>”

When providing SQL_BEFORE_GTIDS=”<gtid_list>”, for each domain
specified in the gtid_list, the replica will execute transactions up
to the GTID found, and immediately stop processing events in that
domain (without executing the transaction of the specified GTID).
Once all domains have stopped, the replica will stop. Events
originating from domains that are not specified in the list are not
replicated.

START SLAVE UNTIL SQL_AFTER_GTIDS=”<gtid_list>” is an alias to the
default behavior of START SLAVE UNTIL master_gtid_pos=”<gtid_list>”.
That is, the replica will only execute transactions originating from
domain ids provided in the list, and will stop once all transactions
provided in the UNTIL list have all been executed.

Example:
=========
If a primary server has a binary log consisting of the following GTIDs:

0-1-1
1-1-1
0-1-2
1-1-2
0-1-3
1-1-3

If a fresh replica (i.e. one with an empty GTID position,
@@gtid_slave_pos='') is started with SQL_BEFORE_GTIDS, i.e.

START SLAVE UNTIL SQL_BEFORE_GTIDS=”1-1-2”

The resulting gtid_slave_pos of the replica will be “1-1-1”.
This is because the replica will execute only events from domain 1
until it sees the transaction with sequence number 2, and
immediately stop without executing it.

If the replica is started with SQL_AFTER_GTIDS, i.e.

START SLAVE UNTIL SQL_AFTER_GTIDS=”1-1-2”

then the resulting gtid_slave_pos of the replica will be “1-1-2”.
This is because it will only execute events from domain 1 until it
has executed the provided GTID.

Reviewed By:
============
Kristian Nielson <knielsen@knielsen-hq.org>
This commit is contained in:
Brandon Nesterenko
2023-08-04 15:48:13 -06:00
parent 0e8dfcfd42
commit 0c1bf5e247
12 changed files with 772 additions and 34 deletions

View File

@ -162,6 +162,7 @@ struct binlog_send_info {
bool clear_initial_log_pos;
bool should_stop;
size_t dirlen;
bool is_until_before_gtids;
binlog_send_info(THD *thd_arg, String *packet_arg, ushort flags_arg,
char *lfn)
@ -180,7 +181,7 @@ struct binlog_send_info {
hb_info_counter(0),
#endif
clear_initial_log_pos(false),
should_stop(false)
should_stop(false), is_until_before_gtids(false)
{
error_text[0] = 0;
bzero(&error_gtid, sizeof(error_gtid));
@ -808,6 +809,18 @@ get_slave_until_gtid(THD *thd, String *out_str)
return entry && entry->val_str(&null_value, out_str, 0) && !null_value;
}
static bool
get_slave_gtid_until_before_gtids(THD *thd)
{
bool null_value;
const LEX_CSTRING name= { STRING_WITH_LEN("slave_gtid_until_before_gtids") };
user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry && entry->val_int(&null_value) && !null_value;
}
/*
Function prepares and sends repliation heartbeat event.
@ -1867,8 +1880,9 @@ send_event_to_slave(binlog_send_info *info, Log_event_type event_type,
This domain already reached the START SLAVE UNTIL stop condition,
so skip this event group.
*/
info->gtid_skip_group = (flags2 & Gtid_log_event::FL_STANDALONE ?
GTID_SKIP_STANDALONE : GTID_SKIP_TRANSACTION);
info->gtid_skip_group= (flags2 & Gtid_log_event::FL_STANDALONE
? GTID_SKIP_STANDALONE
: GTID_SKIP_TRANSACTION);
}
else if (event_gtid.server_id == gtid->server_id &&
event_gtid.seq_no >= gtid->seq_no)
@ -1885,14 +1899,19 @@ send_event_to_slave(binlog_send_info *info, Log_event_type event_type,
info->gtid_until_group= (flags2 & Gtid_log_event::FL_STANDALONE ?
GTID_UNTIL_STOP_AFTER_STANDALONE :
GTID_UNTIL_STOP_AFTER_TRANSACTION);
if (event_gtid.seq_no > until_seq_no)
if (event_gtid.seq_no > until_seq_no ||
info->is_until_before_gtids)
{
/*
Stop processing events now and skip the current event group
because either:
The GTID in START SLAVE UNTIL condition is missing in our binlog.
This should normally not happen (user error), but since we can be
sure that we are now beyond the position that the UNTIL condition
should be in, we can just stop now. And we also need to skip this
event group (as it is beyond the UNTIL condition).
should be in, we can just stop now.
Or the until condition is specified as SQL_BEFORE_GTIDS
*/
info->gtid_skip_group = (flags2 & Gtid_log_event::FL_STANDALONE ?
GTID_SKIP_STANDALONE : GTID_SKIP_TRANSACTION);
@ -2139,7 +2158,10 @@ static int init_binlog_sender(binlog_send_info *info,
info->slave_gtid_strict_mode= get_slave_gtid_strict_mode(thd);
info->slave_gtid_ignore_duplicates= get_slave_gtid_ignore_duplicates(thd);
if (get_slave_until_gtid(thd, &slave_until_gtid_str))
{
info->until_gtid_state= &info->until_gtid_state_obj;
info->is_until_before_gtids= get_slave_gtid_until_before_gtids(thd);
}
}
DBUG_EXECUTE_IF("binlog_force_reconnect_after_22_events",
@ -3228,6 +3250,7 @@ int start_slave(THD* thd , Master_info* mi, bool net_report)
goto err;
}
mi->rli.until_condition= Relay_log_info::UNTIL_GTID;
mi->rli.is_until_before_gtids= thd->lex->mi.is_until_before_gtids;
}
else
mi->rli.clear_until_condition();