Skip to content

Commit 7601d17

Browse files
committed
Implement logic to be able to get a consistent GTID
This adds logic so that with a `START TRANSACTION WITH CONSISTENT SNAPSHOT`, we can also get a correct GTID that is appropriate at the time the transaction is started. It locks around the logic when a consistent snapshot is created in the underlying storage engine and grabs the current GTID under lock at that point. That means that no other threads can update the GTID at the exact same point in time avoiding the race that currently exists if you execute `SELECT @@global.gtid_executed` which doesn't enforce such locking. The locking overhead is should be relatively small. It's only done for consistent snapshots and all Vitess usages of that also need the GTID so we'd lock for a call to `SELECT @@global.gtid_executed` already implicitly as well. In fact, the optimization here is that we don't additionally need a `LOCK TABLES` which is much more heavy handed than locking around the current GTID tracking state. Signed-off-by: Dirkjan Bussink <[email protected]>
1 parent 1f8bbe8 commit 7601d17

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

sql/handler.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,8 +2414,18 @@ static bool snapshot_handlerton(THD *thd, plugin_ref plugin, void *arg) {
24142414
int ha_start_consistent_snapshot(THD *thd) {
24152415
bool warn = true;
24162416

2417+
/*
2418+
Grabbing the write lock here since we end up calling
2419+
store_start_gtid => notify_start_gtid_determined => ensure_sidno
2420+
which wants the write lock to potentially expand storage for sidnos.
2421+
*/
2422+
global_sid_lock->wrlock();
2423+
thd->rpl_thd_ctx.session_gtids_ctx().notify_start_gtid_determined(thd);
2424+
24172425
plugin_foreach(thd, snapshot_handlerton, MYSQL_STORAGE_ENGINE_PLUGIN, &warn);
24182426

2427+
global_sid_lock->unlock();
2428+
24192429
/*
24202430
Same idea as when one wants to CREATE TABLE in one engine which does not
24212431
exist:

sql/rpl_context.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ bool Session_consistency_gtids_ctx::notify_after_gtid_executed_update(
136136
return res;
137137
}
138138

139+
void Session_consistency_gtids_ctx::notify_start_gtid_determined(const THD *thd) {
140+
DBUG_TRACE;
141+
assert(thd);
142+
143+
if (m_listener == nullptr || m_curr_session_track_gtids != SESSION_TRACK_GTIDS_START_GTID) return;
144+
145+
if (m_gtid_set->add_gtid_set(gtid_state->get_executed_gtids()) == RETURN_STATUS_OK) {
146+
notify_ctx_change_listener();
147+
}
148+
}
149+
139150
void Session_consistency_gtids_ctx::
140151
update_tracking_activeness_from_session_variable(const THD *thd) {
141152
m_curr_session_track_gtids = thd->variables.session_track_gtids;

sql/rpl_context.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ class Session_consistency_gtids_ctx {
215215
*/
216216
void update_tracking_activeness_from_session_variable(const THD *thd);
217217

218+
/**
219+
This function SHALL be called once the starting GTID for the given transaction has
220+
has been determined.
221+
222+
This function SHALL store the data if the
223+
thd->variables.session_track_gtids is set to START_GTID.
224+
225+
@param thd The thread context.
226+
*/
227+
virtual void notify_start_gtid_determined(const THD *thd);
228+
218229
private:
219230
// not implemented
220231
Session_consistency_gtids_ctx(const Session_consistency_gtids_ctx &rsc);

sql/sys_vars.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ static bool on_session_track_gtids_update(sys_var *, THD *thd, enum_var_type) {
15381538
}
15391539

15401540
static const char *session_track_gtids_names[] = {"OFF", "OWN_GTID",
1541-
"ALL_GTIDS", NullS};
1541+
"ALL_GTIDS", "START_GTID", NullS};
15421542
static Sys_var_enum Sys_session_track_gtids(
15431543
"session_track_gtids",
15441544
"Controls the amount of global transaction ids to be "

sql/system_variables.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ enum enum_transaction_write_set_hashing_algorithm {
9090
enum enum_session_track_gtids {
9191
SESSION_TRACK_GTIDS_OFF = 0,
9292
SESSION_TRACK_GTIDS_OWN_GTID = 1,
93-
SESSION_TRACK_GTIDS_ALL_GTIDS = 2
93+
SESSION_TRACK_GTIDS_ALL_GTIDS = 2,
94+
SESSION_TRACK_GTIDS_START_GTID = 3
9495
};
9596

9697
/** Values for use_secondary_engine sysvar. */

0 commit comments

Comments
 (0)