20 #include "sql_class.h"
22 #include "transaction.h"
25 #include "sql_parse.h"
41 int gtid_acquire_ownership_single(THD *thd)
43 DBUG_ENTER(
"gtid_acquire_ownership_single");
45 const Gtid gtid_next= thd->variables.gtid_next.gtid;
61 my_thread_id owner= gtid_state->
get_owner(gtid_next);
67 thd->owned_gtid= gtid_next;
73 DBUG_ASSERT(owner != thd->id);
81 if (thd->killed || abort_loop)
83 #ifdef HAVE_REPLICATION
87 if ((thd->system_thread &
88 (SYSTEM_THREAD_SLAVE_SQL | SYSTEM_THREAD_SLAVE_WORKER)) != 0)
91 DBUG_ASSERT(active_mi != NULL && active_mi->rli != NULL);
92 if (active_mi->rli->abort_slave)
95 #endif // HAVE_REPLICATION
108 #ifdef HAVE_GTID_NEXT_LIST
109 int gtid_acquire_ownership_multiple(THD *thd)
111 const Gtid_set *gtid_next_list= thd->get_gtid_next_list_const();
112 rpl_sidno greatest_sidno= 0;
113 DBUG_ENTER(
"gtid_acquire_ownership_multiple");
119 my_thread_id owner= 0;
120 rpl_sidno last_sidno= 0;
121 global_sid_lock->
rdlock();
125 if (g.
sidno != last_sidno)
133 DBUG_ASSERT(owner != thd->id);
138 greatest_sidno= g.
sidno;
150 for (rpl_sidno sidno= 1; sidno < g.
sidno; sidno++)
162 if (thd->killed || abort_loop)
164 #ifdef HAVE_REPLICATION
168 if ((thd->system_thread &
169 (SYSTEM_THREAD_SLAVE_SQL | SYSTEM_THREAD_SLAVE_WORKER)) != 0)
171 DBUG_ASSERT(active_mi != NULL && active_mi->rli != NULL);
172 if (active_mi->rli->abort_slave)
175 #endif // HAVE_REPLICATION
179 thd->owned_gtid_set.ensure_sidno(greatest_sidno);
196 thd->owned_gtid_set._add_gtid(g))
205 }
while (g.
sidno != 0);
209 for (rpl_sidno sidno= 1; sidno <= max_sidno; sidno++)
213 global_sid_lock->
unlock();
229 static inline bool is_already_logged_transaction(
const THD *thd)
231 DBUG_ENTER(
"is_already_logged_transaction");
234 const Gtid_set *gtid_next_list= thd->get_gtid_next_list_const();
236 if (gtid_next_list == NULL)
238 if (gtid_next->
type == GTID_GROUP)
240 if (thd->owned_gtid.sidno == 0)
243 DBUG_ASSERT(thd->owned_gtid.equals(gtid_next->
gtid));
246 DBUG_ASSERT(thd->owned_gtid.sidno == 0);
250 #ifdef HAVE_GTID_NEXT_LIST
251 if (gtid_next->
type == GTID_GROUP)
254 if (!thd->owned_gtid_set.contains_gtid(gtid_next->
gtid))
274 static inline enum_gtid_statement_status skip_statement(
const THD *thd)
276 DBUG_ENTER(
"skip_statement");
278 DBUG_PRINT(
"info", (
"skipping statement '%s'. "
279 "gtid_next->type=%d sql_command=%d "
280 "thd->thread_id=%lu",
282 thd->variables.gtid_next.type,
283 thd->lex->sql_command,
288 global_sid_lock->
rdlock();
289 DBUG_ASSERT(logged_gtids->
contains_gtid(thd->variables.gtid_next.gtid));
290 global_sid_lock->
unlock();
293 DBUG_RETURN(GTID_STATEMENT_SKIP);
297 enum_gtid_statement_status gtid_pre_statement_checks(
const THD *thd)
299 DBUG_ENTER(
"gtid_pre_statement_checks");
301 if (enforce_gtid_consistency && !thd->is_ddl_gtid_compatible())
304 DBUG_RETURN(GTID_STATEMENT_CANCEL);
308 if (stmt_causes_implicit_commit(thd, CF_IMPLICIT_COMMIT_BEGIN) &&
309 thd->in_active_multi_stmt_transaction() &&
310 gtid_next->
type != AUTOMATIC_GROUP)
312 my_error(ER_CANT_DO_IMPLICIT_COMMIT_IN_TRX_WHEN_GTID_NEXT_IS_SET, MYF(0));
313 DBUG_RETURN(GTID_STATEMENT_CANCEL);
325 enum_sql_command sql_command= thd->lex->sql_command;
326 if (sql_command == SQLCOM_COMMIT || sql_command == SQLCOM_BEGIN ||
327 sql_command == SQLCOM_ROLLBACK ||
328 ((sql_command == SQLCOM_SELECT ||
329 (sql_command == SQLCOM_SET_OPTION && !thd->lex->is_set_password_sql)) &&
330 !thd->lex->uses_stored_routines()))
331 DBUG_RETURN(GTID_STATEMENT_EXECUTE);
354 if (UNDEFINED_GROUP == gtid_next->
type)
357 global_sid_lock->
rdlock();
358 gtid_next->
to_string(global_sid_map, buf);
359 global_sid_lock->
unlock();
360 my_error(ER_GTID_NEXT_TYPE_UNDEFINED_GROUP, MYF(0), buf);
361 DBUG_RETURN(GTID_STATEMENT_CANCEL);
364 const Gtid_set *gtid_next_list= thd->get_gtid_next_list_const();
366 DBUG_PRINT(
"info", (
"gtid_next_list=%p gtid_next->type=%d "
367 "thd->owned_gtid.gtid.{sidno,gno}={%d,%lld} "
368 "thd->thread_id=%lu",
369 gtid_next_list, gtid_next->
type,
370 thd->owned_gtid.sidno,
372 (ulong)thd->thread_id));
374 const bool skip_transaction= is_already_logged_transaction(thd);
375 if (gtid_next_list == NULL)
377 if (skip_transaction)
378 DBUG_RETURN(skip_statement(thd));
379 DBUG_RETURN(GTID_STATEMENT_EXECUTE);
383 #ifdef HAVE_GTID_NEXT_LIST
384 switch (gtid_next->
type)
386 case AUTOMATIC_GROUP:
387 my_error(ER_GTID_NEXT_CANT_BE_AUTOMATIC_IF_GTID_NEXT_LIST_IS_NON_NULL,
389 DBUG_RETURN(GTID_STATEMENT_CANCEL);
391 if (skip_transaction)
392 DBUG_RETURN(skip_statement(thd));
394 case ANONYMOUS_GROUP:
395 DBUG_RETURN(GTID_STATEMENT_EXECUTE);
404 DBUG_RETURN(GTID_STATEMENT_CANCEL);
408 void gtid_post_statement_checks(THD *thd)
410 DBUG_ENTER(
"gtid_post_statement_checks");
411 const enum_sql_command sql_command= thd->lex->sql_command;
432 if (thd->variables.gtid_next.type == GTID_GROUP &&
433 thd->get_command() != COM_STMT_PREPARE &&
434 (stmt_causes_implicit_commit(thd, CF_IMPLICIT_COMMIT_BEGIN) ||
435 (sql_command == SQLCOM_SET_OPTION && thd->lex->is_set_password_sql) ||
436 sql_command == SQLCOM_COMMIT ||
437 sql_command == SQLCOM_ROLLBACK))
438 thd->variables.gtid_next.set_undefined();
444 int gtid_rollback(THD *thd)
446 DBUG_ENTER(
"gtid_rollback");
448 global_sid_lock->
rdlock();
450 global_sid_lock->
unlock();