From 5b7c35121d3a57b08fce053d4eed1794260b536c Mon Sep 17 00:00:00 2001 From: Daniele Sciascia Date: Fri, 24 Jan 2025 15:28:28 +0100 Subject: [PATCH] Check for certification keys Add function wsrep_check_keys() to verify that transactions append a certification key for each table that was MDL locked. --- sql/CMakeLists.txt | 1 + sql/wsrep_key_check.cc | 48 ++++++++++++++++++++++++++++++++++++++ sql/wsrep_key_check.h | 7 ++++++ sql/wsrep_trans_observer.h | 6 +++++ wsrep-lib | 2 +- 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 sql/wsrep_key_check.cc create mode 100644 sql/wsrep_key_check.h diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt index 9e735a5143343..a43c079e6f8a3 100644 --- a/sql/CMakeLists.txt +++ b/sql/CMakeLists.txt @@ -34,6 +34,7 @@ IF(WITH_WSREP AND NOT EMBEDDED_LIBRARY) wsrep_thd.cc wsrep_schema.cc wsrep_plugin.cc + wsrep_key_check.cc service_wsrep.cc ) MYSQL_ADD_PLUGIN(wsrep ${WSREP_SOURCES} MANDATORY NOT_EMBEDDED LINK_LIBRARIES wsrep-lib wsrep_api_v26) diff --git a/sql/wsrep_key_check.cc b/sql/wsrep_key_check.cc new file mode 100644 index 0000000000000..44a9898f01126 --- /dev/null +++ b/sql/wsrep_key_check.cc @@ -0,0 +1,48 @@ +#include "wsrep_key_check.h" +#include "my_global.h" +#include "sql_class.h" +#include "mdl.h" + +static int check_key_for_ticket(MDL_ticket *mdl_ticket, void *arg, + bool granted) +{ + THD *thd= (THD *) arg; + MDL_context *mdl_ctx= mdl_ticket->get_ctx(); + MDL_key *mdl_key= mdl_ticket->get_key(); + + if (!granted) + return 0; + + if (mdl_key->mdl_namespace() != MDL_key::TABLE) + return 0; + + if (thd != mdl_ctx->get_thd()) + return 0; + + if (std::strncmp("performance_schema", mdl_key->db_name(), + mdl_key->db_name_length()) == 0) + return 0; + + if (std::strncmp("gtid_slave_pos", mdl_key->name(), + mdl_key->name_length()) == 0) + return 0; + + wsrep::key key(wsrep::key::shared); + key.append_key_part(mdl_key->db_name(), mdl_key->db_name_length()); + key.append_key_part(mdl_key->name(), mdl_key->name_length()); + + if (!thd->wsrep_trx().has_key(key)) + { + WSREP_WARN("No certification key for MDL lock " + "db: %s name: %s type: %s query: %s", + mdl_key->db_name(), mdl_key->name(), + mdl_ticket->get_type_name()->str, thd->query()); + } + + return 0; +} + +void wsrep_check_keys(THD *thd) +{ + mdl_iterate(check_key_for_ticket, thd); +} diff --git a/sql/wsrep_key_check.h b/sql/wsrep_key_check.h new file mode 100644 index 0000000000000..e4e8fe8ca95e7 --- /dev/null +++ b/sql/wsrep_key_check.h @@ -0,0 +1,7 @@ +#ifndef WSREP_KEY_CHECK_H +#define WSREP_KEY_CHECK_H + +class THD; +void wsrep_check_keys(THD *thd); + +#endif diff --git a/sql/wsrep_trans_observer.h b/sql/wsrep_trans_observer.h index 028882d428b54..ea7d4bc5f5ad8 100644 --- a/sql/wsrep_trans_observer.h +++ b/sql/wsrep_trans_observer.h @@ -23,6 +23,7 @@ #include "wsrep_thd.h" #include "wsrep_binlog.h" /* register/deregister group commit */ #include "my_dbug.h" +#include "wsrep_key_check.h" class THD; @@ -343,6 +344,11 @@ static inline int wsrep_before_commit(THD* thd, bool all) thd->wsrep_trx().ws_meta().gtid(), wsrep_gtid_server.gtid()); wsrep_register_for_group_commit(thd); + + if (wsrep_thd_is_local(thd)) + { + wsrep_check_keys(thd); + } } mysql_mutex_lock(&thd->LOCK_thd_kill); diff --git a/wsrep-lib b/wsrep-lib index 70cd967f5e249..fd1128622f8da 160000 --- a/wsrep-lib +++ b/wsrep-lib @@ -1 +1 @@ -Subproject commit 70cd967f5e249b53d6cce90e2e4198641c564381 +Subproject commit fd1128622f8da6ef8d62e5670f495e7e191efe6b