From 6be05ceb05609b1aa7382776b4e27ad134808eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= Date: Mon, 27 Apr 2020 09:40:51 +0300 Subject: [PATCH] MDEV-22203: WSREP_ON is unnecessarily expensive to evaluate This is a backport of the applicable part of commit 93475aff8de80a0ef53cbee924bcb70de6e86f2c and commit 2c39f69d34e64a5cf94720e82e78c0ee91bd4649 from 10.4. Before 10.4 and Galera 4, WSREP_ON is a macro that points to a global Boolean variable, so it is not that expensive to evaluate, but we will add an unlikely() hint around it. WSREP_ON_NEW: Remove. This macro was introduced in commit c863159c320008676aff978a7cdde5732678f975 when reverting WSREP_ON to its previous definition. We replace some use of WSREP_ON with WSREP(thd), like it was done in 93475aff8de80a0ef53cbee924bcb70de6e86f2c. Note: the macro WSREP() in 10.1 is equivalent to WSREP_NNULL() in 10.4. Item_func_rand::seed_random(): Avoid invoking current_thd when WSREP is not enabled. --- mysql-test/suite/galera/t/MW-86-wait1.test | 1 + sql/item_func.cc | 23 +++++++++++----------- sql/log.cc | 9 +++++---- sql/log_event.cc | 8 ++++---- sql/mysqld.cc | 2 ++ sql/slave.cc | 8 +++++--- sql/sql_base.cc | 4 ++-- sql/sql_parse.cc | 8 ++++---- sql/sql_table.cc | 4 ++-- sql/wsrep_mysqld.h | 8 +------- 10 files changed, 38 insertions(+), 37 deletions(-) diff --git a/mysql-test/suite/galera/t/MW-86-wait1.test b/mysql-test/suite/galera/t/MW-86-wait1.test index 40a7882829b80..a7476b74e6893 100644 --- a/mysql-test/suite/galera/t/MW-86-wait1.test +++ b/mysql-test/suite/galera/t/MW-86-wait1.test @@ -5,6 +5,7 @@ # --source include/galera_cluster.inc --source include/have_binlog_format_row.inc +--source include/have_debug.inc --source include/have_debug_sync.inc --connection node_2 diff --git a/sql/item_func.cc b/sql/item_func.cc index 302ec58a70827..8407dc881befd 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. - Copyright (c) 2009, 2017, MariaDB + Copyright (c) 2009, 2020, MariaDB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -2650,19 +2650,20 @@ void Item_func_rand::seed_random(Item *arg) TODO: do not do reinit 'rand' for every execute of PS/SP if args[0] is a constant. */ - uint32 tmp; + uint32 tmp= (uint32) arg->val_int(); #ifdef WITH_WSREP - THD *thd= current_thd; - if (WSREP(thd)) + if (WSREP_ON) { - if (thd->wsrep_exec_mode==REPL_RECV) - tmp= thd->wsrep_rand; - else - tmp= thd->wsrep_rand= (uint32) arg->val_int(); - } - else + THD *thd= current_thd; + if (thd->variables.wsrep_on) + { + if (thd->wsrep_exec_mode==REPL_RECV) + tmp= thd->wsrep_rand; + else + thd->wsrep_rand= tmp; + } + } #endif /* WITH_WSREP */ - tmp= (uint32) arg->val_int(); my_rnd_init(rand, (uint32) (tmp*0x10001L+55555555L), (uint32) (tmp*0x10000001L)); diff --git a/sql/log.cc b/sql/log.cc index 0efef6d1e2930..076dd36a21930 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -1791,7 +1791,7 @@ binlog_commit_flush_stmt_cache(THD *thd, bool all, #ifdef WITH_WSREP if (thd->wsrep_mysql_replicated > 0) { - DBUG_ASSERT(WSREP_ON); + DBUG_ASSERT(WSREP(thd)); WSREP_DEBUG("avoiding binlog_commit_flush_trx_cache: %d", thd->wsrep_mysql_replicated); return 0; @@ -6612,14 +6612,15 @@ int MYSQL_BIN_LOG::rotate(bool force_rotate, bool* check_purge) int error= 0; DBUG_ENTER("MYSQL_BIN_LOG::rotate"); - if (wsrep_to_isolation) +#ifdef WITH_WSREP + if (WSREP_ON && wsrep_to_isolation) { - DBUG_ASSERT(WSREP_ON); *check_purge= false; - WSREP_DEBUG("avoiding binlog rotate due to TO isolation: %d", + WSREP_DEBUG("avoiding binlog rotate due to TO isolation: %d", wsrep_to_isolation); DBUG_RETURN(0); } +#endif /* WITH_WSREP */ //todo: fix the macro def and restore safe_mutex_assert_owner(&LOCK_log); *check_purge= false; diff --git a/sql/log_event.cc b/sql/log_event.cc index d731c39d9c598..bb3465ae9f0cc 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -1,6 +1,6 @@ /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. - Copyright (c) 2009, 2019, MariaDB + Copyright (c) 2009, 2020, MariaDB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -4770,7 +4770,7 @@ Query_log_event::do_shall_skip(rpl_group_info *rgi) } } #ifdef WITH_WSREP - else if (WSREP_ON && wsrep_mysql_replication_bundle && opt_slave_domain_parallel_threads == 0 && + else if (WSREP(thd) && wsrep_mysql_replication_bundle && opt_slave_domain_parallel_threads == 0 && thd->wsrep_mysql_replicated > 0 && (is_begin() || is_commit())) { @@ -4784,7 +4784,7 @@ Query_log_event::do_shall_skip(rpl_group_info *rgi) thd->wsrep_mysql_replicated = 0; } } -#endif +#endif /* WITH_WSREP */ DBUG_RETURN(Log_event::do_shall_skip(rgi)); } @@ -7755,7 +7755,7 @@ Xid_log_event::do_shall_skip(rpl_group_info *rgi) DBUG_RETURN(Log_event::EVENT_SKIP_COUNT); } #ifdef WITH_WSREP - else if (wsrep_mysql_replication_bundle && WSREP_ON && + else if (WSREP(thd) && wsrep_mysql_replication_bundle && opt_slave_domain_parallel_threads == 0) { if (++thd->wsrep_mysql_replicated < (int)wsrep_mysql_replication_bundle) diff --git a/sql/mysqld.cc b/sql/mysqld.cc index e2cc73903850a..ff3faaa92bcd8 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -5781,8 +5781,10 @@ int mysqld_main(int argc, char **argv) set_user(mysqld_user, user_info); } +#ifdef WITH_WSREP if (WSREP_ON && wsrep_check_opts()) global_system_variables.wsrep_on= 0; +#endif if (opt_bin_log && !global_system_variables.server_id) { diff --git a/sql/slave.cc b/sql/slave.cc index cbfc3fd841352..26fabc2cc5281 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2017, Oracle and/or its affiliates. - Copyright (c) 2009, 2017, MariaDB Corporation + Copyright (c) 2009, 2020, MariaDB Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -4998,8 +4998,10 @@ pthread_handler_t handle_slave_sql(void *arg) if (!sql_slave_killed(serial_rgi)) { slave_output_error_info(serial_rgi, thd); - if (WSREP_ON && rli->last_error().number == ER_UNKNOWN_COM_ERROR) + if (WSREP(thd) && rli->last_error().number == ER_UNKNOWN_COM_ERROR) + { wsrep_node_dropped= TRUE; + } } goto err; } @@ -5131,7 +5133,7 @@ pthread_handler_t handle_slave_sql(void *arg) If slave stopped due to node going non primary, we set global flag to trigger automatic restart of slave when node joins back to cluster. */ - if (WSREP_ON && wsrep_node_dropped && wsrep_restart_slave) + if (WSREP(thd) && wsrep_node_dropped && wsrep_restart_slave) { if (wsrep_ready_get()) { diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 1c4e7600d87b1..1dbed8c55d153 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2016, Oracle and/or its affiliates. - Copyright (c) 2010, 2016, MariaDB + Copyright (c) 2010, 2020, MariaDB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -4784,7 +4784,7 @@ bool open_tables(THD *thd, const DDL_options_st &options, } } - if (WSREP_ON && + if (WSREP(thd) && wsrep_replicate_myisam && (*start) && (*start)->table && diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index e5626ccbd7c52..d19cbf5ad9bee 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2017, Oracle and/or its affiliates. - Copyright (c) 2008, 2019, MariaDB + Copyright (c) 2008, 2020, MariaDB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -1493,7 +1493,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, if (parser_state.init(thd, thd->query(), thd->query_length())) break; - if (WSREP_ON) + if (WSREP(thd)) wsrep_mysql_parse(thd, thd->query(), thd->query_length(), &parser_state); else mysql_parse(thd, thd->query(), thd->query_length(), &parser_state); @@ -1574,13 +1574,13 @@ bool dispatch_command(enum enum_server_command command, THD *thd, */ statistic_increment(thd->status_var.questions, &LOCK_status); - if(!WSREP(thd)) + if (!WSREP(thd)) thd->set_time(); /* Reset the query start time. */ parser_state.reset(beginning_of_next_stmt, length); /* TODO: set thd->lex->sql_command to SQLCOM_END here */ - if (WSREP_ON) + if (WSREP(thd)) wsrep_mysql_parse(thd, beginning_of_next_stmt, length, &parser_state); else mysql_parse(thd, beginning_of_next_stmt, length, &parser_state); diff --git a/sql/sql_table.cc b/sql/sql_table.cc index e694c52e2722d..ef6a8c018433b 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -1,6 +1,6 @@ /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. - Copyright (c) 2010, 2019, MariaDB + Copyright (c) 2010, 2020, MariaDB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -5338,7 +5338,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, DBUG_ENTER("mysql_create_like_table"); #ifdef WITH_WSREP - if (WSREP_ON && !thd->wsrep_applier && + if (WSREP(thd) && !thd->wsrep_applier && wsrep_create_like_table(thd, table, src_table, create_info)) DBUG_RETURN(res); #endif diff --git a/sql/wsrep_mysqld.h b/sql/wsrep_mysqld.h index a5ec69268d465..1531abe78f8f6 100644 --- a/sql/wsrep_mysqld.h +++ b/sql/wsrep_mysqld.h @@ -179,13 +179,7 @@ extern void wsrep_prepend_PATH (const char* path); /* Other global variables */ extern wsrep_seqno_t wsrep_locked_seqno; -#define WSREP_ON \ - (global_system_variables.wsrep_on) - -#define WSREP_ON_NEW \ - ((global_system_variables.wsrep_on) && \ - wsrep_provider && \ - strcmp(wsrep_provider, WSREP_NONE)) +#define WSREP_ON unlikely(global_system_variables.wsrep_on) #define WSREP(thd) \ (WSREP_ON && thd->variables.wsrep_on)