Permalink
Browse files

WL#8355 "Improve scalability by partitioning LOCK_grant lock."

This patch tries to solve scalability bottleneck caused by LOCK_grant
for some workloads (e.g. Sysbench POINT_SELECT/InnoDB).

The idea is to partition LOCK_grant rwlock into several partitions
and to make read lock requests to acquire read lock on only one
partition (write lock requests lock still have to lock all partitions).
The specific partition to be used is determined by thread id.
As result concurrent acquisitions of read lock by different threads
are likely to use different partitions reducing negative effects of
cache invalidation/concurrent atomic operations associated with
LOCK_grant rwlock.

New class class implementing rwlock partitioned by THD/thread id
was added. Code in ACL subsystem was adjusted to use object of
this class instead of LOCK_grant rwlock.
  • Loading branch information...
Dmitry Lenev
Dmitry Lenev committed Feb 25, 2015
1 parent 1598cc4 commit 75fd4b3defc68176e3f38407fe9f6aa538424562
@@ -9,7 +9,7 @@ where NAME = 'wait/synch/mutex/sql/LOCK_open';
NAME
wait/synch/mutex/sql/LOCK_open
select NAME from performance_schema.rwlock_instances
where NAME = 'wait/synch/rwlock/sql/LOCK_grant';
where NAME = 'wait/synch/rwlock/sql/LOCK_grant' limit 1;
NAME
wait/synch/rwlock/sql/LOCK_grant
DROP TABLE IF EXISTS t1;
@@ -120,10 +120,6 @@ where name like "wait/synch/mutex/sql/tz_LOCK";
count(name)
1
select count(name) from rwlock_instances
where name like "wait/synch/rwlock/sql/LOCK_grant";
count(name)
1
select count(name) from rwlock_instances
where name like "wait/synch/rwlock/sql/LOCK_sys_init_connect";
count(name)
1
@@ -21,7 +21,7 @@ flush status;
select NAME from performance_schema.mutex_instances
where NAME = 'wait/synch/mutex/sql/LOCK_open';
select NAME from performance_schema.rwlock_instances
where NAME = 'wait/synch/rwlock/sql/LOCK_grant';
where NAME = 'wait/synch/rwlock/sql/LOCK_grant' limit 1;
--disable_warnings
DROP TABLE IF EXISTS t1;
@@ -123,9 +123,6 @@ select count(name) from mutex_instances
# Verify that these global rwlocks have been properly initilized in sql
select count(name) from rwlock_instances
where name like "wait/synch/rwlock/sql/LOCK_grant";
select count(name) from rwlock_instances
where name like "wait/synch/rwlock/sql/LOCK_sys_init_connect";
@@ -0,0 +1,187 @@
#ifndef PARTITIONED_RWLOCK_INCLUDED
#define PARTITIONED_RWLOCK_INCLUDED
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "mysql/psi/mysql_thread.h"
/**
Implementation of read-write lock partitioned by thread id.
This rwlock provides better scalability in read-heavy environments by
employing the following simple trick:
*) Read lock is acquired only on one of its partitions. The specific
partition is chosen according to thread id.
*) Write lock is acquired on all partitions.
This way concurrent request for read lock made by different threads
have a good chance not to disturb each other by doing cache invalidation
and atomic operations. As result scalability in this scenario improves.
OTOH acquisition of write lock becomes more expensive. So this rwlock
is not supposed to be used in cases when number of write requests is
significant.
*/
class Partitioned_rwlock
{
public:
Partitioned_rwlock() {}
/**
@param parts Number of partitions.
@param psi_key P_S instrumentation key to use for rwlock instances
for partitions.
*/
bool init(uint parts, PSI_rwlock_key psi_key)
{
m_parts= parts;
if (!(m_locks_array= new (std::nothrow) mysql_rwlock_t[m_parts]))
return true;
for (uint i= 0 ; i < m_parts; ++i)
mysql_rwlock_init(psi_key, &m_locks_array[i]);
return false;
}
void destroy()
{
for (uint i= 0 ; i < m_parts; ++i)
mysql_rwlock_destroy(&m_locks_array[i]);
delete [] m_locks_array;
}
void wrlock()
{
for (uint i= 0 ; i < m_parts; ++i)
mysql_rwlock_wrlock(&m_locks_array[i]);
}
void wrunlock()
{
for (uint i= 0 ; i < m_parts; ++i)
mysql_rwlock_unlock(&m_locks_array[i]);
}
void rdlock(uint thread_id)
{
mysql_rwlock_rdlock(&m_locks_array[thread_id%m_parts]);
}
/*
One should use the same thread number for releasing read lock
as was used for acquiring it,
*/
void rdunlock(uint thread_id)
{
mysql_rwlock_unlock(&m_locks_array[thread_id%m_parts]);
}
private:
mysql_rwlock_t* m_locks_array;
uint m_parts;
Partitioned_rwlock(const Partitioned_rwlock&); // Non-copyable
Partitioned_rwlock& operator=(const Partitioned_rwlock&); // Non-copyable
};
/**
Read lock guard class for Partitioned_rwlock. Supports early unlocking.
*/
class Partitioned_rwlock_read_guard
{
public:
/**
Acquires read lock on partitioned rwlock on behalf of thread.
Automatically release lock in destructor.
*/
Partitioned_rwlock_read_guard(Partitioned_rwlock *rwlock, uint thread_id)
: m_rwlock(rwlock), m_thread_id(thread_id)
{
m_rwlock->rdlock(m_thread_id);
}
~Partitioned_rwlock_read_guard()
{
if (m_rwlock)
m_rwlock->rdunlock(m_thread_id);
}
/** Release read lock. Optional method for early unlocking. */
void unlock()
{
m_rwlock->rdunlock(m_thread_id);
m_rwlock= NULL;
}
private:
/**
Pointer to partitioned rwlock which was acquired. NULL if lock was
released early so destructor should not do anything.
*/
Partitioned_rwlock *m_rwlock;
/**
Id of thread on which behalf lock was acquired and which is to be used for
unlocking.
*/
uint m_thread_id;
// Non-copyable
Partitioned_rwlock_read_guard(const Partitioned_rwlock_read_guard&);
Partitioned_rwlock_read_guard& operator=(const
Partitioned_rwlock_read_guard&);
};
/**
Write lock guard class for Partitioned_rwlock. Supports early unlocking.
*/
class Partitioned_rwlock_write_guard
{
public:
/**
Acquires write lock on partitioned rwlock.
Automatically release it in destructor.
*/
explicit Partitioned_rwlock_write_guard(Partitioned_rwlock *rwlock)
: m_rwlock(rwlock)
{
m_rwlock->wrlock();
}
~Partitioned_rwlock_write_guard()
{
if (m_rwlock)
m_rwlock->wrunlock();
}
/** Release write lock. Optional method for early unlocking. */
void unlock()
{
m_rwlock->wrunlock();
m_rwlock= NULL;
}
private:
/**
Pointer to partitioned rwlock which was acquired. NULL if lock was
released early so destructor should not do anything.
*/
Partitioned_rwlock *m_rwlock;
// Non-copyable
Partitioned_rwlock_write_guard(const Partitioned_rwlock_write_guard&);
Partitioned_rwlock_write_guard& operator=(const
Partitioned_rwlock_write_guard&);
};
#endif /* PARTITIONED_RWLOCK_INCLUDED */
View
@@ -65,6 +65,15 @@ bool initialized=0;
bool allow_all_hosts=1;
uint grant_version=0; /* Version of priv tables */
my_bool validate_user_plugins= TRUE;
/**
Flag to track if rwlocks in ACL subsystem were initialized.
Necessary because acl_free() can be called in some error scenarios
without prior call to acl_init().
*/
bool rwlocks_initialized= false;
const uint LOCK_GRANT_PARTITIONS= 32;
Partitioned_rwlock LOCK_grant;
#define FIRST_NON_YN_FIELD 26
@@ -1333,6 +1342,9 @@ my_bool acl_init(bool dont_read_acl_tables)
&my_charset_utf8_bin);
mysql_rwlock_init(key_rwlock_proxy_users, &proxy_users_rwlock);
LOCK_grant.init(LOCK_GRANT_PARTITIONS, key_rwlock_LOCK_grant);
rwlocks_initialized= true;
/*
cache built-in native authentication plugins,
to avoid hash searches and a global mutex lock on every connect
@@ -1922,6 +1934,13 @@ void acl_free(bool end)
plugin_unlock(0, native_password_plugin);
delete acl_cache;
acl_cache=0;
if (rwlocks_initialized)
{
LOCK_grant.destroy();
mysql_rwlock_destroy(&proxy_users_rwlock);
rwlocks_initialized= false;
}
}
}
@@ -2357,7 +2376,8 @@ static my_bool grant_reload_procs_priv(THD *thd)
DBUG_RETURN(TRUE);
}
mysql_rwlock_wrlock(&LOCK_grant);
Partitioned_rwlock_write_guard lock(&LOCK_grant);
/* Save a copy of the current hash if we need to undo the grant load */
old_proc_priv_hash= proc_priv_hash;
old_func_priv_hash= func_priv_hash;
@@ -2375,7 +2395,6 @@ static my_bool grant_reload_procs_priv(THD *thd)
my_hash_free(&old_proc_priv_hash);
my_hash_free(&old_func_priv_hash);
}
mysql_rwlock_unlock(&LOCK_grant);
DBUG_RETURN(return_val);
}
@@ -2427,7 +2446,8 @@ my_bool grant_reload(THD *thd)
goto end;
}
mysql_rwlock_wrlock(&LOCK_grant);
LOCK_grant.wrlock();
old_column_priv_hash= column_priv_hash;
/*
@@ -2450,7 +2470,9 @@ my_bool grant_reload(THD *thd)
my_hash_free(&old_column_priv_hash);
free_root(&old_mem,MYF(0));
}
mysql_rwlock_unlock(&LOCK_grant);
LOCK_grant.wrunlock();
close_acl_tables(thd);
/*
@@ -2460,9 +2482,9 @@ my_bool grant_reload(THD *thd)
if (grant_reload_procs_priv(thd))
return_val= 1;
mysql_rwlock_wrlock(&LOCK_grant);
LOCK_grant.wrlock();
grant_version++;
mysql_rwlock_unlock(&LOCK_grant);
LOCK_grant.wrunlock();
end:
close_acl_tables(thd);
@@ -24,6 +24,7 @@
#include "hash_filo.h" // HASH, hash_filo
#include "records.h" // READ_RECORD
#include "read_write_lock.h" // Write_lock, Read_lock, lock_at
#include "partitioned_rwlock.h" // Partitioned_rwlock
#include "prealloced_array.h"
@@ -273,6 +274,7 @@ extern HASH acl_check_hosts;
extern mysql_rwlock_t proxy_users_rwlock;
extern bool allow_all_hosts;
extern uint grant_version; /* Version of priv tables */
extern Partitioned_rwlock LOCK_grant;
GRANT_NAME *name_hash_search(HASH *name_hash,
const char *host,const char* ip,
Oops, something went wrong.

0 comments on commit 75fd4b3

Please sign in to comment.