Skip to content

Commit f711297

Browse files
Tor Didriksenbjornmu
authored andcommitted
Bug#20785598 USE ATOMICS TO AVOID MUTEX CONTENTION ON LOCK_PLUGIN DUE TO QUERY REWRITE FRAMEW
Use atomic counters when loading/unloading pre/post parse plugins. Check counters in mysql_parse. (cherry picked from commit 153316aaa82f8001ed7d52e4601e834993dad315)
1 parent 134c413 commit f711297

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

sql/sql_parse.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5032,18 +5032,23 @@ void mysql_parse(THD *thd, Parser_state *parser_state)
50325032
mysql_reset_thd_for_next_command(thd);
50335033
lex_start(thd);
50345034

5035+
int32 num_preparse= my_atomic_load32(&num_pre_parse_plugins);
5036+
int32 num_postparse= my_atomic_load32(&num_post_parse_plugins);
5037+
50355038
thd->m_parser_state= parser_state;
5036-
invoke_pre_parse_rewrite_plugins(thd);
5039+
if (num_preparse > 0)
5040+
invoke_pre_parse_rewrite_plugins(thd);
50375041
thd->m_parser_state= NULL;
50385042

5039-
enable_digest_if_any_plugin_needs_it(thd, parser_state);
5043+
if (num_postparse > 0)
5044+
enable_digest_if_any_plugin_needs_it(thd, parser_state);
50405045

50415046
if (query_cache.send_result_to_client(thd, thd->query()) <= 0)
50425047
{
50435048
LEX *lex= thd->lex;
50445049

50455050
bool err= parse_sql(thd, parser_state, NULL);
5046-
if (!err)
5051+
if (num_postparse > 0 && !err)
50475052
err= invoke_post_parse_rewrite_plugins(thd, false);
50485053

50495054
const char *found_semicolon= parser_state->m_lip.found_semicolon;

sql/sql_plugin.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ using std::max;
5555
static PSI_memory_key key_memory_plugin_ref;
5656
#endif
5757

58+
volatile int32 num_pre_parse_plugins= 0;
59+
volatile int32 num_post_parse_plugins= 0;
60+
5861
static PSI_memory_key key_memory_plugin_mem_root;
5962
static PSI_memory_key key_memory_plugin_init_tmp;
6063
static PSI_memory_key key_memory_plugin_int_mem_root;
@@ -955,6 +958,12 @@ static bool plugin_add(MEM_ROOT *tmp_root,
955958
{
956959
init_alloc_root(key_memory_plugin_int_mem_root,
957960
&tmp_plugin_ptr->mem_root, 4096, 4096);
961+
962+
if (plugin->type == MYSQL_REWRITE_PRE_PARSE_PLUGIN)
963+
my_atomic_add32(&num_pre_parse_plugins, 1);
964+
if (plugin->type == MYSQL_REWRITE_POST_PARSE_PLUGIN)
965+
my_atomic_add32(&num_post_parse_plugins, 1);
966+
958967
DBUG_RETURN(FALSE);
959968
}
960969
tmp_plugin_ptr->state= PLUGIN_IS_FREED;
@@ -1029,6 +1038,12 @@ static void plugin_del(st_plugin_int *plugin)
10291038
restore_pluginvar_names(plugin->system_vars);
10301039
plugin_vars_free_values(plugin->system_vars);
10311040
my_hash_delete(&plugin_hash[plugin->plugin->type], (uchar*)plugin);
1041+
1042+
if (plugin->plugin->type == MYSQL_REWRITE_PRE_PARSE_PLUGIN)
1043+
my_atomic_add32(&num_pre_parse_plugins, -1);
1044+
if (plugin->plugin->type == MYSQL_REWRITE_POST_PARSE_PLUGIN)
1045+
my_atomic_add32(&num_post_parse_plugins, -1);
1046+
10321047
if (plugin->plugin_dl)
10331048
plugin_dl_del(&plugin->plugin_dl->dl);
10341049
plugin->state= PLUGIN_IS_FREED;

sql/sql_plugin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,8 @@ extern bool plugin_foreach_with_mask(THD *thd, plugin_foreach_func *func,
177177
int type, uint state_mask, void *arg);
178178
int lock_plugin_data();
179179
int unlock_plugin_data();
180+
181+
extern volatile int32 num_pre_parse_plugins;
182+
extern volatile int32 num_post_parse_plugins;
183+
180184
#endif

sql/sql_prepare.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ When one supplies long data for a placeholder:
106106
#include "mysql/psi/mysql_ps.h" // MYSQL_EXECUTE_PS
107107
#include "binlog.h"
108108
#include "sql_audit.h" // mysql_global_audit_mask
109-
109+
#include "sql_plugin.h"
110110

111111
#ifdef EMBEDDED_LIBRARY
112112
/* include MYSQL_BIND headers */
@@ -3281,7 +3281,10 @@ bool Prepared_statement::prepare(const char *packet, size_t packet_len)
32813281
digest.reset(token_array, max_digest_length);
32823282
thd->m_digest= &digest;
32833283

3284-
enable_digest_if_any_plugin_needs_it(thd, &parser_state);
3284+
int32 num_postparse= my_atomic_load32(&num_post_parse_plugins);
3285+
3286+
if (num_postparse > 0)
3287+
enable_digest_if_any_plugin_needs_it(thd, &parser_state);
32853288
#ifndef EMBEDDED_LIBRARY
32863289
if (is_any_audit_plugin_active(thd))
32873290
parser_state.m_input.m_compute_digest= true;
@@ -3293,7 +3296,8 @@ bool Prepared_statement::prepare(const char *packet, size_t packet_len)
32933296

32943297
if (!error)
32953298
{ // We've just created the statement maybe there is a rewrite
3296-
invoke_post_parse_rewrite_plugins(thd, true);
3299+
if (num_postparse > 0)
3300+
invoke_post_parse_rewrite_plugins(thd, true);
32973301
error= init_param_array(this);
32983302
}
32993303

0 commit comments

Comments
 (0)