Skip to content

Commit 1598cc4

Browse files
author
Dmitry Lenev
committed
WL#8356 "Improve scalability by not acquiring unnecessary locks for internal temp tables".
Do not acquire LOCK_plugin when doing ha_lock_engine() for builtin engines in production builds. This is possible thanks to the fact that in such builds for builtin plugins we don't do reference counting. To implement this optimization introduced new builtin_htons[] array to be able easily identify builtin handlertons. This patch solves the problem with LOCK_plugin mutex showing up as a scalability bottleneck in workloads which often create internal temporary tables, such as 1-table Sysbench SELECT_DISTINCT/InnoDB test.
1 parent 58ee0e2 commit 1598cc4

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

sql/handler.cc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ inline double log2(double x)
175175
*/
176176
st_plugin_int *hton2plugin[MAX_HA];
177177

178+
/**
179+
Array allowing to check if handlerton is builtin without
180+
acquiring LOCK_plugin.
181+
*/
182+
static bool builtin_htons[MAX_HA];
183+
178184
const char *ha_resolve_storage_engine_name(const handlerton *db_type)
179185
{
180186
return db_type == NULL ? "UNKNOWN" : hton2plugin[db_type->slot]->name.str;
@@ -496,10 +502,29 @@ plugin_ref ha_lock_engine(THD *thd, const handlerton *hton)
496502
if (hton)
497503
{
498504
st_plugin_int **plugin= hton2plugin + hton->slot;
499-
505+
500506
#ifdef DBUG_OFF
507+
/*
508+
Take a shortcut for builtin engines -- return pointer to plugin
509+
without acquiring LOCK_plugin mutex. This is safe safe since such
510+
plugins are not deleted until shutdown and we don't do reference
511+
counting in non-debug builds for them.
512+
513+
Since we have reference to handlerton on our hands, this method
514+
can't be called concurrently to non-builtin handlerton initialization/
515+
deinitialization. So it is safe to access builtin_htons[] without
516+
additional locking.
517+
*/
518+
if (builtin_htons[hton->slot])
519+
return *plugin;
520+
501521
return my_plugin_lock(thd, plugin);
502522
#else
523+
/*
524+
We can't take shortcut in debug builds.
525+
At least assert that builtin_htons[slot] is set correctly.
526+
*/
527+
DBUG_ASSERT(builtin_htons[hton->slot] == (plugin[0]->plugin_dl == NULL));
503528
return my_plugin_lock(thd, &plugin);
504529
#endif
505530
}
@@ -720,6 +745,7 @@ int ha_finalize_handlerton(st_plugin_int *plugin)
720745
DBUG_ASSERT(hton2plugin[hton->slot] == plugin);
721746
DBUG_ASSERT(hton->slot < MAX_HA);
722747
hton2plugin[hton->slot]= NULL;
748+
builtin_htons[hton->slot]= false; /* Extra correctness. */
723749
}
724750

725751
my_free(hton);
@@ -817,6 +843,7 @@ int ha_initialize_handlerton(st_plugin_int *plugin)
817843
hton->savepoint_offset= savepoint_alloc_size;
818844
savepoint_alloc_size+= tmp;
819845
hton2plugin[hton->slot]=plugin;
846+
builtin_htons[hton->slot]= (plugin->plugin_dl == NULL);
820847
if (hton->prepare)
821848
total_ha_2pc++;
822849
break;

0 commit comments

Comments
 (0)