summaryrefslogtreecommitdiff
path: root/sql/mdl.cc
diff options
context:
space:
mode:
authorDmitry Lenev <dlenev@mysql.com>2010-02-01 14:43:06 +0300
committerDmitry Lenev <dlenev@mysql.com>2010-02-01 14:43:06 +0300
commitafd15c43a9103c47389835105489acd07d64e014 (patch)
tree818a7077a43f09db8708035a4f1d22d369fdd4da /sql/mdl.cc
parenta63f8480dbc862db00fb8f76c74b1fb99fa4534a (diff)
downloadmariadb-git-afd15c43a9103c47389835105489acd07d64e014.tar.gz
Implement new type-of-operation-aware metadata locks.
Add a wait-for graph based deadlock detector to the MDL subsystem. Fixes bug #46272 "MySQL 5.4.4, new MDL: unnecessary deadlock" and bug #37346 "innodb does not detect deadlock between update and alter table". The first bug manifested itself as an unwarranted abort of a transaction with ER_LOCK_DEADLOCK error by a concurrent ALTER statement, when this transaction tried to repeat use of a table, which it has already used in a similar fashion before ALTER started. The second bug showed up as a deadlock between table-level locks and InnoDB row locks, which was "detected" only after innodb_lock_wait_timeout timeout. A transaction would start using the table and modify a few rows. Then ALTER TABLE would come in, and start copying rows into a temporary table. Eventually it would stumble on the modified records and get blocked on a row lock. The first transaction would try to do more updates, and get blocked on thr_lock.c lock. This situation of circular wait would only get resolved by a timeout. Both these bugs stemmed from inadequate solutions to the problem of deadlocks occurring between different locking subsystems. In the first case we tried to avoid deadlocks between metadata locking and table-level locking subsystems, when upgrading shared metadata lock to exclusive one. Transactions holding the shared lock on the table and waiting for some table-level lock used to be aborted too aggressively. We also allowed ALTER TABLE to start in presence of transactions that modify the subject table. ALTER TABLE acquires TL_WRITE_ALLOW_READ lock at start, and that block all writes against the table (naturally, we don't want any writes to be lost when switching the old and the new table). TL_WRITE_ALLOW_READ lock, in turn, would block the started transaction on thr_lock.c lock, should they do more updates. This, again, lead to the need to abort such transactions. The second bug occurred simply because we didn't have any mechanism to detect deadlocks between the table-level locks in thr_lock.c and row-level locks in InnoDB, other than innodb_lock_wait_timeout. This patch solves both these problems by moving lock conflicts which are causing these deadlocks into the metadata locking subsystem, thus making it possible to avoid or detect such deadlocks inside MDL. To do this we introduce new type-of-operation-aware metadata locks, which allow MDL subsystem to know not only the fact that transaction has used or is going to use some object but also what kind of operation it has carried out or going to carry out on the object. This, along with the addition of a special kind of upgradable metadata lock, allows ALTER TABLE to wait until all transactions which has updated the table to go away. This solves the second issue. Another special type of upgradable metadata lock is acquired by LOCK TABLE WRITE. This second lock type allows to solve the first issue, since abortion of table-level locks in event of DDL under LOCK TABLES becomes also unnecessary. Below follows the list of incompatible changes introduced by this patch: - From now on, ALTER TABLE and CREATE/DROP TRIGGER SQL (i.e. those statements that acquire TL_WRITE_ALLOW_READ lock) wait for all transactions which has *updated* the table to complete. - From now on, LOCK TABLES ... WRITE, REPAIR/OPTIMIZE TABLE (i.e. all statements which acquire TL_WRITE table-level lock) wait for all transaction which *updated or read* from the table to complete. As a consequence, innodb_table_locks=0 option no longer applies to LOCK TABLES ... WRITE. - DROP DATABASE, DROP TABLE, RENAME TABLE no longer abort statements or transactions which use tables being dropped or renamed, and instead wait for these transactions to complete. - Since LOCK TABLES WRITE now takes a special metadata lock, not compatible with with reads or writes against the subject table and transaction-wide, thr_lock.c deadlock avoidance algorithm that used to ensure absence of deadlocks between LOCK TABLES WRITE and other statements is no longer sufficient, even for MyISAM. The wait-for graph based deadlock detector of MDL subsystem may sometimes be necessary and is involved. This may lead to ER_LOCK_DEADLOCK error produced for multi-statement transactions even if these only use MyISAM: session 1: session 2: begin; update t1 ... lock table t2 write, t1 write; -- gets a lock on t2, blocks on t1 update t2 ... (ER_LOCK_DEADLOCK) - Finally, support of LOW_PRIORITY option for LOCK TABLES ... WRITE was abandoned. LOCK TABLE ... LOW_PRIORITY WRITE from now on has the same priority as the usual LOCK TABLE ... WRITE. SELECT HIGH PRIORITY no longer trumps LOCK TABLE ... WRITE in the wait queue. - We do not take upgradable metadata locks on implicitly locked tables. So if one has, say, a view v1 that uses table t1, and issues: LOCK TABLE v1 WRITE; FLUSH TABLE t1; -- (or just 'FLUSH TABLES'), an error is produced. In order to be able to perform DDL on a table under LOCK TABLES, the table must be locked explicitly in the LOCK TABLES list.
Diffstat (limited to 'sql/mdl.cc')
-rw-r--r--sql/mdl.cc1890
1 files changed, 865 insertions, 1025 deletions
diff --git a/sql/mdl.cc b/sql/mdl.cc
index dce917a1a2e..3148571131a 100644
--- a/sql/mdl.cc
+++ b/sql/mdl.cc
@@ -19,6 +19,10 @@
#include <hash.h>
#include <mysqld_error.h>
+
+void notify_shared_lock(THD *thd, MDL_ticket *conflicting_ticket);
+
+
static bool mdl_initialized= 0;
@@ -46,6 +50,41 @@ private:
};
+enum enum_deadlock_weight
+{
+ MDL_DEADLOCK_WEIGHT_DML= 0,
+ MDL_DEADLOCK_WEIGHT_DDL= 100
+};
+
+
+
+/**
+ A context of the recursive traversal through all contexts
+ in all sessions in search for deadlock.
+*/
+
+class Deadlock_detection_context
+{
+public:
+ Deadlock_detection_context(MDL_context *start_arg)
+ : start(start_arg),
+ victim(NULL),
+ current_search_depth(0)
+ { }
+ MDL_context *start;
+ MDL_context *victim;
+ uint current_search_depth;
+ static const uint MAX_SEARCH_DEPTH= 1000;
+};
+
+
+/**
+ Get a bit corresponding to enum_mdl_type value in a granted/waiting bitmaps
+ and compatibility matrices.
+*/
+
+#define MDL_BIT(A) static_cast<MDL_lock::bitmap_t>(1U << A)
+
/**
The lock context. Created internally for an acquired lock.
For a given name, there exists only one MDL_lock instance,
@@ -60,53 +99,98 @@ private:
class MDL_lock
{
public:
- typedef I_P_List<MDL_ticket,
- I_P_List_adapter<MDL_ticket,
- &MDL_ticket::next_in_lock,
- &MDL_ticket::prev_in_lock> >
- Ticket_list;
+ typedef uchar bitmap_t;
- typedef Ticket_list::Iterator Ticket_iterator;
+ class Ticket_list
+ {
+ public:
+ typedef I_P_List<MDL_ticket,
+ I_P_List_adapter<MDL_ticket,
+ &MDL_ticket::next_in_lock,
+ &MDL_ticket::prev_in_lock> >
+ List;
+ operator const List &() const { return m_list; }
+ Ticket_list() :m_bitmap(0) {}
+
+ void add_ticket(MDL_ticket *ticket);
+ void remove_ticket(MDL_ticket *ticket);
+ bool is_empty() const { return m_list.is_empty(); }
+ bitmap_t bitmap() const { return m_bitmap; }
+ private:
+ void clear_bit_if_not_in_list(enum_mdl_type type);
+ private:
+ /** List of tickets. */
+ List m_list;
+ /** Bitmap of types of tickets in this list. */
+ bitmap_t m_bitmap;
+ };
+
+ typedef Ticket_list::List::Iterator Ticket_iterator;
public:
/** The key of the object (data) being protected. */
MDL_key key;
- /** List of granted tickets for this lock. */
- Ticket_list granted;
- /** Tickets for contexts waiting to acquire a shared lock. */
- Ticket_list waiting_shared;
- /**
- Tickets for contexts waiting to acquire an exclusive lock.
- There can be several upgraders and active exclusive
- locks belonging to the same context. E.g.
- in case of RENAME t1 to t2, t2 to t3, we attempt to
- exclusively lock t2 twice.
- */
- Ticket_list waiting_exclusive;
void *cached_object;
mdl_cached_object_release_hook cached_object_release_hook;
- /** Mutex protecting this lock context. */
- pthread_mutex_t m_mutex;
+ /**
+ Read-write lock protecting this lock context.
+
+ TODO/FIXME: Replace with RW-lock which will prefer readers
+ on all platforms and not only on Linux.
+ */
+ rw_lock_t m_rwlock;
bool is_empty() const
{
- return (granted.is_empty() && waiting_shared.is_empty() &&
- waiting_exclusive.is_empty());
+ return (m_granted.is_empty() && m_waiting.is_empty());
}
- bool has_pending_exclusive_lock()
+ virtual const bitmap_t *incompatible_granted_types_bitmap() const = 0;
+ virtual const bitmap_t *incompatible_waiting_types_bitmap() const = 0;
+
+ bool has_pending_conflicting_lock(enum_mdl_type type);
+
+ bool can_grant_lock(enum_mdl_type type, MDL_context *requstor_ctx) const;
+
+ inline static MDL_lock *create(const MDL_key *key);
+
+ void notify_shared_locks(MDL_context *ctx)
{
- bool has_locks;
- pthread_mutex_lock(&m_mutex);
- has_locks= ! waiting_exclusive.is_empty();
- pthread_mutex_unlock(&m_mutex);
- return has_locks;
+ Ticket_iterator it(m_granted);
+ MDL_ticket *conflicting_ticket;
+
+ while ((conflicting_ticket= it++))
+ {
+ if (conflicting_ticket->get_ctx() != ctx)
+ notify_shared_lock(ctx->get_thd(), conflicting_ticket);
+ }
}
- virtual bool can_grant_lock(const MDL_context *requestor_ctx,
- enum_mdl_type type, bool is_upgrade)= 0;
- virtual void wake_up_waiters()= 0;
- inline static MDL_lock *create(const MDL_key *key);
+ /**
+ Wake up contexts which are waiting to acquire lock on the object and
+ which may succeed now, when we released some lock on it or removed
+ some pending request from its waiters list (the latter can happen,
+ for example, when context trying to acquire exclusive on the object
+ lock is killed).
+ */
+ void wake_up_waiters()
+ {
+ MDL_lock::Ticket_iterator it(m_waiting);
+ MDL_ticket *awake_ticket;
+
+ while ((awake_ticket= it++))
+ awake_ticket->get_ctx()->awake(MDL_context::NORMAL_WAKE_UP);
+ }
+ void remove_ticket(Ticket_list MDL_lock::*queue, MDL_ticket *ticket);
+
+ bool find_deadlock(MDL_ticket *waiting_ticket,
+ Deadlock_detection_context *deadlock_ctx);
+
+ /** List of granted tickets for this lock. */
+ Ticket_list m_granted;
+ /** Tickets for contexts waiting to acquire a lock. */
+ Ticket_list m_waiting;
+public:
MDL_lock(const MDL_key *key_arg)
: key(key_arg),
@@ -116,31 +200,31 @@ public:
m_ref_release(0),
m_is_destroyed(FALSE)
{
- pthread_mutex_init(&m_mutex, NULL);
+ my_rwlock_init(&m_rwlock, NULL);
}
virtual ~MDL_lock()
{
- pthread_mutex_destroy(&m_mutex);
+ rwlock_destroy(&m_rwlock);
}
inline static void destroy(MDL_lock *lock);
public:
/**
These three members are used to make it possible to separate
- the mdl_locks.m_mutex mutex and MDL_lock::m_mutex in
+ the mdl_locks.m_mutex mutex and MDL_lock::m_rwlock in
MDL_map::find_or_insert() for increased scalability.
The 'm_is_destroyed' member is only set by destroyers that
- have both the mdl_locks.m_mutex and MDL_lock::m_mutex, thus
+ have both the mdl_locks.m_mutex and MDL_lock::m_rwlock, thus
holding any of the mutexes is sufficient to read it.
The 'm_ref_usage; is incremented under protection by
mdl_locks.m_mutex, but when 'm_is_destroyed' is set to TRUE, this
- member is moved to be protected by the MDL_lock::m_mutex.
+ member is moved to be protected by the MDL_lock::m_rwlock.
This means that the MDL_map::find_or_insert() which only
- holds the MDL_lock::m_mutex can compare it to 'm_ref_release'
+ holds the MDL_lock::m_rwlock can compare it to 'm_ref_release'
without acquiring mdl_locks.m_mutex again and if equal it can also
destroy the lock object safely.
The 'm_ref_release' is incremented under protection by
- MDL_lock::m_mutex.
+ MDL_lock::m_rwlock.
Note since we are only interested in equality of these two
counters we don't have to worry about overflows as long as
their size is big enough to hold maximum number of concurrent
@@ -164,9 +248,18 @@ public:
: MDL_lock(key_arg)
{ }
- virtual bool can_grant_lock(const MDL_context *requestor_ctx,
- enum_mdl_type type, bool is_upgrade);
- virtual void wake_up_waiters();
+ virtual const bitmap_t *incompatible_granted_types_bitmap() const
+ {
+ return m_granted_incompatible;
+ }
+ virtual const bitmap_t *incompatible_waiting_types_bitmap() const
+ {
+ return m_waiting_incompatible;
+ }
+
+private:
+ static const bitmap_t m_granted_incompatible[MDL_TYPE_END];
+ static const bitmap_t m_waiting_incompatible[MDL_TYPE_END];
};
@@ -182,9 +275,18 @@ public:
: MDL_lock(key_arg)
{ }
- virtual bool can_grant_lock(const MDL_context *requestor_ctx,
- enum_mdl_type type, bool is_upgrade);
- virtual void wake_up_waiters();
+ virtual const bitmap_t *incompatible_granted_types_bitmap() const
+ {
+ return m_granted_incompatible;
+ }
+ virtual const bitmap_t *incompatible_waiting_types_bitmap() const
+ {
+ return m_waiting_incompatible;
+ }
+
+private:
+ static const bitmap_t m_granted_incompatible[MDL_TYPE_END];
+ static const bitmap_t m_waiting_incompatible[MDL_TYPE_END];
};
@@ -275,7 +377,7 @@ void MDL_map::destroy()
if it does not exist.
@retval non-NULL - Success. MDL_lock instance for the key with
- locked MDL_lock::m_mutex.
+ locked MDL_lock::m_rwlock.
@retval NULL - Failure (OOM).
*/
@@ -309,7 +411,7 @@ retry:
Find MDL_lock object corresponding to the key.
@retval non-NULL - MDL_lock instance for the key with locked
- MDL_lock::m_mutex.
+ MDL_lock::m_rwlock.
@retval NULL - There was no MDL_lock for the key.
*/
@@ -335,7 +437,7 @@ retry:
/**
- Release mdl_locks.m_mutex mutex and lock MDL_lock::m_mutex for lock
+ Release mdl_locks.m_mutex mutex and lock MDL_lock::m_rwlock for lock
object from the hash. Handle situation when object was released
while the held no mutex.
@@ -357,7 +459,7 @@ bool MDL_map::move_from_hash_to_lock_mutex(MDL_lock *lock)
lock->m_ref_usage++;
pthread_mutex_unlock(&m_mutex);
- pthread_mutex_lock(&lock->m_mutex);
+ rw_wrlock(&lock->m_rwlock);
lock->m_ref_release++;
if (unlikely(lock->m_is_destroyed))
{
@@ -372,7 +474,7 @@ bool MDL_map::move_from_hash_to_lock_mutex(MDL_lock *lock)
*/
uint ref_usage= lock->m_ref_usage;
uint ref_release= lock->m_ref_release;
- pthread_mutex_unlock(&lock->m_mutex);
+ rw_unlock(&lock->m_rwlock);
if (ref_usage == ref_release)
MDL_lock::destroy(lock);
return TRUE;
@@ -391,8 +493,6 @@ void MDL_map::remove(MDL_lock *lock)
{
uint ref_usage, ref_release;
- safe_mutex_assert_owner(&lock->m_mutex);
-
if (lock->cached_object)
(*lock->cached_object_release_hook)(lock->cached_object);
@@ -402,14 +502,14 @@ void MDL_map::remove(MDL_lock *lock)
has the responsibility to release it.
Setting of m_is_destroyed to TRUE while holding _both_
- mdl_locks.m_mutex and MDL_lock::m_mutex mutexes transfers the
+ mdl_locks.m_mutex and MDL_lock::m_rwlock mutexes transfers the
protection of m_ref_usage from mdl_locks.m_mutex to
- MDL_lock::m_mutex while removal of object from the hash makes
- it read-only. Therefore whoever acquires MDL_lock::m_mutex next
+ MDL_lock::m_rwlock while removal of object from the hash makes
+ it read-only. Therefore whoever acquires MDL_lock::m_rwlock next
will see most up to date version of m_ref_usage.
This means that when m_is_destroyed is TRUE and we hold the
- MDL_lock::m_mutex we can safely read the m_ref_usage
+ MDL_lock::m_rwlock we can safely read the m_ref_usage
member.
*/
pthread_mutex_lock(&m_mutex);
@@ -417,7 +517,7 @@ void MDL_map::remove(MDL_lock *lock)
lock->m_is_destroyed= TRUE;
ref_usage= lock->m_ref_usage;
ref_release= lock->m_ref_release;
- pthread_mutex_unlock(&lock->m_mutex);
+ rw_unlock(&lock->m_rwlock);
pthread_mutex_unlock(&m_mutex);
if (ref_usage == ref_release)
MDL_lock::destroy(lock);
@@ -431,10 +531,16 @@ void MDL_map::remove(MDL_lock *lock)
*/
MDL_context::MDL_context()
- :m_lt_or_ha_sentinel(NULL),
- m_thd(NULL)
+ :m_trans_sentinel(NULL),
+ m_thd(NULL),
+ m_needs_thr_lock_abort(FALSE),
+ m_waiting_for(NULL),
+ m_deadlock_weight(0),
+ m_signal(NO_WAKE_UP)
{
- pthread_cond_init(&m_ctx_wakeup_cond, NULL);
+ my_rwlock_init(&m_waiting_for_lock, NULL);
+ pthread_mutex_init(&m_signal_lock, NULL);
+ pthread_cond_init(&m_signal_cond, NULL);
}
@@ -453,7 +559,10 @@ MDL_context::MDL_context()
void MDL_context::destroy()
{
DBUG_ASSERT(m_tickets.is_empty());
- pthread_cond_destroy(&m_ctx_wakeup_cond);
+
+ rwlock_destroy(&m_waiting_for_lock);
+ pthread_mutex_destroy(&m_signal_lock);
+ pthread_cond_destroy(&m_signal_cond);
}
@@ -540,6 +649,13 @@ MDL_request::create(MDL_key::enum_mdl_namespace mdl_namespace, const char *db,
}
+uint MDL_request::get_deadlock_weight() const
+{
+ return key.mdl_namespace() == MDL_key::GLOBAL ||
+ type > MDL_SHARED_NO_WRITE ?
+ MDL_DEADLOCK_WEIGHT_DDL : MDL_DEADLOCK_WEIGHT_DML;
+}
+
/**
Auxiliary functions needed for creation/destruction of MDL_lock objects.
@@ -567,8 +683,6 @@ void MDL_lock::destroy(MDL_lock *lock)
}
-
-
/**
Auxiliary functions needed for creation/destruction of MDL_ticket
objects.
@@ -650,485 +764,463 @@ static inline void mdl_exit_cond(THD *thd,
}
-/**
- Check if request for the global metadata lock can be satisfied given
- its current state,
-
- @param requestor_ctx The context that identifies the owner of the request.
- @param type_arg The requested type of global lock. Usually derived
- from the type of lock on individual object to be
- requested. See table below.
- @param is_upgrade TRUE if we are performing lock upgrade (not unused).
-
- @retval TRUE - Lock request can be satisfied
- @retval FALSE - There is some conflicting lock
-
- Here is a compatibility matrix defined by this function:
-
- | | Satisfied or pending requests
- | | for global metadata lock
- ----------------+-------------+--------------------------------------------
- Type of request | Correspond. |
- for indiv. lock | global lock | Active-S Pending-S Active-IS(**) Active-IX
- ----------------+-------------+--------------------------------------------
- S, high-prio S | IS | + + + +
- upgradable S | IX | - - + +
- X | IX | - - + +
- S upgraded to X | IX (*) | 0 + + +
+MDL_context::mdl_signal_type MDL_context::wait()
+{
+ const char *old_msg;
+ st_my_thread_var *mysys_var= my_thread_var;
+ mdl_signal_type result;
- Here: "+" -- means that request can be satisfied
- "-" -- means that request can't be satisfied and should wait
- "0" -- means impossible situation.
+ pthread_mutex_lock(&m_signal_lock);
- (*) Since for upgradable shared locks we always take intention exclusive
- global lock at the same time when obtaining the shared lock, there
- is no need to obtain such lock during the upgrade itself.
- (**) Since intention shared global locks are compatible with all other
- type of locks we don't even have any accounting for them.
-*/
+ old_msg= MDL_ENTER_COND(m_thd, mysys_var, &m_signal_cond, &m_signal_lock);
-bool
-MDL_global_lock::can_grant_lock(const MDL_context *requestor_ctx,
- enum_mdl_type type_arg,
- bool is_upgrade)
+ while (! m_signal && !mysys_var->abort)
+ pthread_cond_wait(&m_signal_cond, &m_signal_lock);
+
+ result= m_signal;
+
+ MDL_EXIT_COND(m_thd, mysys_var, &m_signal_lock, old_msg);
+
+ return result;
+}
+
+
+MDL_context::mdl_signal_type MDL_context::timed_wait(ulong timeout)
{
- switch (type_arg)
+ struct timespec abstime;
+ const char *old_msg;
+ mdl_signal_type result;
+ st_my_thread_var *mysys_var= my_thread_var;
+
+ pthread_mutex_lock(&m_signal_lock);
+
+ old_msg= MDL_ENTER_COND(m_thd, mysys_var, &m_signal_cond, &m_signal_lock);
+
+ if (! m_signal)
{
- case MDL_SHARED:
- if (! granted.is_empty() && granted.front()->m_type == MDL_INTENTION_EXCLUSIVE)
- {
- /*
- We are going to obtain global shared lock and there is active
- intention exclusive lock. Have to wait.
- */
- return FALSE;
- }
- return TRUE;
- break;
- case MDL_INTENTION_EXCLUSIVE:
- if ((! granted.is_empty() && granted.front()->m_type == MDL_SHARED) ||
- ! waiting_shared.is_empty())
- {
- /*
- We are going to obtain intention exclusive global lock and
- there is active or pending shared global lock. Have to wait.
- */
- return FALSE;
- }
- else
- return TRUE;
- break;
- default:
- DBUG_ASSERT(0);
- break;
+ set_timespec(abstime, timeout);
+ pthread_cond_timedwait(&m_signal_cond, &m_signal_lock, &abstime);
}
- return FALSE;
+
+ result= (m_signal != NO_WAKE_UP) ? m_signal : TIMEOUT_WAKE_UP;
+
+ MDL_EXIT_COND(m_thd, mysys_var, &m_signal_lock, old_msg);
+
+ return result;
}
/**
- Wake up contexts which are waiting to acquire the global
- metadata lock and which may succeed now, when we released it, or
- removed a blocking request for it from the waiters list.
- The latter can happen when the context trying to acquire the
- global shared lock is killed.
+ Clear bit corresponding to the type of metadata lock in bitmap representing
+ set of such types if list of tickets does not contain ticket with such type.
+
+ @param[in,out] bitmap Bitmap representing set of types of locks.
+ @param[in] list List to inspect.
+ @param[in] type Type of metadata lock to look up in the list.
*/
-void MDL_global_lock::wake_up_waiters()
+void MDL_lock::Ticket_list::clear_bit_if_not_in_list(enum_mdl_type type)
{
- /*
- If there are no active locks or they are of INTENTION
- EXCLUSIVE type and there are no pending requests for global
- SHARED lock, wake up contexts waiting for an INTENTION
- EXCLUSIVE lock.
- This happens when we release the global SHARED lock or abort
- or remove a pending request for it, i.e. abort the
- context waiting for it.
- */
- if ((granted.is_empty() ||
- granted.front()->m_type == MDL_INTENTION_EXCLUSIVE) &&
- waiting_shared.is_empty() && ! waiting_exclusive.is_empty())
- {
- MDL_lock::Ticket_iterator it(waiting_exclusive);
- MDL_ticket *awake_ticket;
- while ((awake_ticket= it++))
- awake_ticket->get_ctx()->awake();
- }
+ MDL_lock::Ticket_iterator it(m_list);
+ const MDL_ticket *ticket;
+ while ((ticket= it++))
+ if (ticket->get_type() == type)
+ return;
+ m_bitmap&= ~ MDL_BIT(type);
+}
+
+
+/**
+ Add ticket to MDL_lock's list of waiting requests and
+ update corresponding bitmap of lock types.
+*/
+
+void MDL_lock::Ticket_list::add_ticket(MDL_ticket *ticket)
+{
+ m_list.push_front(ticket);
+ m_bitmap|= MDL_BIT(ticket->get_type());
+}
+
+
+/**
+ Remove ticket from MDL_lock's list of requests and
+ update corresponding bitmap of lock types.
+*/
+
+void MDL_lock::Ticket_list::remove_ticket(MDL_ticket *ticket)
+{
+ m_list.remove(ticket);
/*
- If there are no active locks, wake up contexts waiting for
- the global shared lock (happens when an INTENTION EXCLUSIVE
- lock is released).
-
- We don't wake up contexts waiting for the global shared lock
- if there is an active global shared lock since such situation
- is transient and in it contexts marked as waiting for global
- shared lock must be already woken up and simply have not
- managed to update lock object yet.
+ Check if waiting queue has another ticket with the same type as
+ one which was removed. If there is no such ticket, i.e. we have
+ removed last ticket of particular type, then we need to update
+ bitmap of waiting ticket's types.
+ Note that in most common case, i.e. when shared lock is removed
+ from waiting queue, we are likely to find ticket of the same
+ type early without performing full iteration through the list.
+ So this method should not be too expensive.
*/
- if (granted.is_empty() &&
- ! waiting_shared.is_empty())
- {
- MDL_lock::Ticket_iterator it(waiting_shared);
- MDL_ticket *awake_ticket;
- while ((awake_ticket= it++))
- awake_ticket->get_ctx()->awake();
- }
+ clear_bit_if_not_in_list(ticket->get_type());
}
/**
- Check if request for the per-object lock can be satisfied given current
- state of the lock.
+ Compatibility (or rather "incompatibility") matrices for global metadata
+ lock. Arrays of bitmaps which elements specify which granted/waiting locks
+ are incompatible with type of lock being requested.
+
+ Here is how types of individual locks are translated to type of global lock:
+
+ ----------------+-------------+
+ Type of request | Correspond. |
+ for indiv. lock | global lock |
+ ----------------+-------------+
+ S, SH, SR, SW | IS |
+ SNW, SNRW, X | IX |
+ SNW, SNRW -> X | IX (*) |
+
+ The first array specifies if particular type of request can be satisfied
+ if there is granted global lock of certain type.
+
+ | Type of active |
+ Request | global lock |
+ type | IS(**) IX S |
+ ---------+----------------+
+ IS | + + + |
+ IX | + + - |
+ S | + - + |
+
+ The second array specifies if particular type of request can be satisfied
+ if there is already waiting request for the global lock of certain type.
+ I.e. it specifies what is the priority of different lock types.
+
+ | Pending |
+ Request | global lock |
+ type | IS(**) IX S |
+ ---------+--------------+
+ IS | + + + |
+ IX | + + - |
+ S | + + + |
- @param requestor_ctx The context that identifies the owner of the request.
- @param type_arg The requested lock type.
- @param is_upgrade Must be set to TRUE when we are upgrading
- a shared upgradable lock to exclusive.
+ Here: "+" -- means that request can be satisfied
+ "-" -- means that request can't be satisfied and should wait
- @retval TRUE Lock request can be satisfied
- @retval FALSE There is some conflicting lock.
+ (*) Since for upgradable locks we always take intention exclusive global
+ lock at the same time when obtaining the shared lock, there is no
+ need to obtain such lock during the upgrade itself.
+ (**) Since intention shared global locks are compatible with all other
+ type of locks we don't even have any accounting for them.
+*/
+
+const MDL_lock::bitmap_t MDL_global_lock::m_granted_incompatible[MDL_TYPE_END] =
+{
+ MDL_BIT(MDL_SHARED), MDL_BIT(MDL_INTENTION_EXCLUSIVE), 0, 0, 0, 0, 0, 0
+};
- This function defines the following compatibility matrix for metadata locks:
+const MDL_lock::bitmap_t MDL_global_lock::m_waiting_incompatible[MDL_TYPE_END] =
+{
+ MDL_BIT(MDL_SHARED), 0, 0, 0, 0, 0, 0, 0
+};
- | Satisfied or pending requests which we have in MDL_lock
- ----------------+---------------------------------------------------------
- Current request | Active-S Pending-X Active-X Act-S-pend-upgrade-to-X
- ----------------+---------------------------------------------------------
- S, upgradable S | + - - (*) -
- High-prio S | + + - +
- X | - + - -
- S upgraded to X | - (**) + 0 0
+
+/**
+ Compatibility (or rather "incompatibility") matrices for per-object
+ metadata lock. Arrays of bitmaps which elements specify which granted/
+ waiting locks are incompatible with type of lock being requested.
+
+ The first array specifies if particular type of request can be satisfied
+ if there is granted lock of certain type.
+
+ Request | Granted requests for lock |
+ type | S SH SR SW SNW SNRW X |
+ ----------+------------------------------+
+ S | + + + + + + - |
+ SH | + + + + + + - |
+ SR | + + + + + - - |
+ SW | + + + + - - - |
+ SNW | + + + - - - - |
+ SNRW | + + - - - - - |
+ X | - - - - - - - |
+ SNW -> X | - - - 0 0 0 0 |
+ SNRW -> X | - - 0 0 0 0 0 |
+
+ The second array specifies if particular type of request can be satisfied
+ if there is waiting request for the same lock of certain type. In other
+ words it specifies what is the priority of different lock types.
+
+ Request | Pending requests for lock |
+ type | S SH SR SW SNW SNRW X |
+ ----------+-----------------------------+
+ S | + + + + + + - |
+ SH | + + + + + + + |
+ SR | + + + + + - - |
+ SW | + + + + - - - |
+ SNW | + + + + + + - |
+ SNRW | + + + + + + - |
+ X | + + + + + + + |
+ SNW -> X | + + + + + + + |
+ SNRW -> X | + + + + + + + |
Here: "+" -- means that request can be satisfied
"-" -- means that request can't be satisfied and should wait
"0" -- means impossible situation which will trigger assert
- (*) Unless active exclusive lock belongs to the same context as shared
- lock being requested.
- (**) Unless all active shared locks belong to the same context as one
- being upgraded.
+ @note In cases then current context already has "stronger" type
+ of lock on the object it will be automatically granted
+ thanks to usage of the MDL_context::find_ticket() method.
+*/
+
+const MDL_lock::bitmap_t
+MDL_object_lock::m_granted_incompatible[MDL_TYPE_END] =
+{
+ 0,
+ MDL_BIT(MDL_EXCLUSIVE),
+ MDL_BIT(MDL_EXCLUSIVE),
+ MDL_BIT(MDL_EXCLUSIVE) | MDL_BIT(MDL_SHARED_NO_READ_WRITE),
+ MDL_BIT(MDL_EXCLUSIVE) | MDL_BIT(MDL_SHARED_NO_READ_WRITE) |
+ MDL_BIT(MDL_SHARED_NO_WRITE),
+ MDL_BIT(MDL_EXCLUSIVE) | MDL_BIT(MDL_SHARED_NO_READ_WRITE) |
+ MDL_BIT(MDL_SHARED_NO_WRITE) | MDL_BIT(MDL_SHARED_WRITE),
+ MDL_BIT(MDL_EXCLUSIVE) | MDL_BIT(MDL_SHARED_NO_READ_WRITE) |
+ MDL_BIT(MDL_SHARED_NO_WRITE) | MDL_BIT(MDL_SHARED_WRITE) |
+ MDL_BIT(MDL_SHARED_READ),
+ MDL_BIT(MDL_EXCLUSIVE) | MDL_BIT(MDL_SHARED_NO_READ_WRITE) |
+ MDL_BIT(MDL_SHARED_NO_WRITE) | MDL_BIT(MDL_SHARED_WRITE) |
+ MDL_BIT(MDL_SHARED_READ) | MDL_BIT(MDL_SHARED_HIGH_PRIO) |
+ MDL_BIT(MDL_SHARED)
+};
+
+
+const MDL_lock::bitmap_t
+MDL_object_lock::m_waiting_incompatible[MDL_TYPE_END] =
+{
+ 0,
+ MDL_BIT(MDL_EXCLUSIVE),
+ 0,
+ MDL_BIT(MDL_EXCLUSIVE) | MDL_BIT(MDL_SHARED_NO_READ_WRITE),
+ MDL_BIT(MDL_EXCLUSIVE) | MDL_BIT(MDL_SHARED_NO_READ_WRITE) |
+ MDL_BIT(MDL_SHARED_NO_WRITE),
+ MDL_BIT(MDL_EXCLUSIVE),
+ MDL_BIT(MDL_EXCLUSIVE),
+ 0
+};
+
+
+/**
+ Check if request for the metadata lock can be satisfied given its
+ current state.
+
+ @param type_arg The requested lock type.
+ @param requestor_ctx The MDL context of the requestor.
+
+ @retval TRUE Lock request can be satisfied
+ @retval FALSE There is some conflicting lock.
+
+ @note In cases then current context already has "stronger" type
+ of lock on the object it will be automatically granted
+ thanks to usage of the MDL_context::find_ticket() method.
*/
bool
-MDL_object_lock::can_grant_lock(const MDL_context *requestor_ctx,
- enum_mdl_type type_arg,
- bool is_upgrade)
+MDL_lock::can_grant_lock(enum_mdl_type type_arg,
+ MDL_context *requestor_ctx) const
{
bool can_grant= FALSE;
-
- switch (type_arg) {
- case MDL_SHARED:
- case MDL_SHARED_UPGRADABLE:
- case MDL_SHARED_HIGH_PRIO:
- if (granted.is_empty() || granted.front()->is_shared())
- {
- /* Pending exclusive locks have higher priority over shared locks. */
- if (waiting_exclusive.is_empty() || type_arg == MDL_SHARED_HIGH_PRIO)
- can_grant= TRUE;
- }
- else if (granted.front()->get_ctx() == requestor_ctx)
- {
- /*
- When exclusive lock comes from the same context we can satisfy our
- shared lock. This is required for CREATE TABLE ... SELECT ... and
- ALTER VIEW ... AS ....
- */
+ bitmap_t waiting_incompat_map= incompatible_waiting_types_bitmap()[type_arg];
+ bitmap_t granted_incompat_map= incompatible_granted_types_bitmap()[type_arg];
+ /*
+ New lock request can be satisfied iff:
+ - There are no incompatible types of satisfied requests
+ in other contexts
+ - There are no waiting requests which have higher priority
+ than this request.
+ */
+ if (! (m_waiting.bitmap() & waiting_incompat_map))
+ {
+ if (! (m_granted.bitmap() & granted_incompat_map))
can_grant= TRUE;
- }
- break;
- case MDL_EXCLUSIVE:
- if (is_upgrade)
+ else
{
- /* We are upgrading MDL_SHARED to MDL_EXCLUSIVE. */
- MDL_ticket *conflicting_ticket;
- MDL_lock::Ticket_iterator it(granted);
+ Ticket_iterator it(m_granted);
+ MDL_ticket *ticket;
- /*
- There should be no active exclusive locks since we own shared lock
- on the object.
- */
- DBUG_ASSERT(granted.front()->is_shared());
-
- while ((conflicting_ticket= it++))
+ /* Check that the incompatible lock belongs to some other context. */
+ while ((ticket= it++))
{
- /*
- When upgrading shared lock to exclusive one we can have other shared
- locks for the same object in the same context, e.g. in case when several
- instances of TABLE are open.
- */
- if (conflicting_ticket->get_ctx() != requestor_ctx)
+ if (ticket->get_ctx() != requestor_ctx &&
+ ticket->is_incompatible_when_granted(type_arg))
break;
}
- /* Grant lock if there are no conflicting shared locks. */
- if (conflicting_ticket == NULL)
+ if (ticket == NULL) /* Incompatible locks are our own. */
can_grant= TRUE;
- break;
- }
- else if (granted.is_empty())
- {
- /*
- We are trying to acquire fresh MDL_EXCLUSIVE and there are no active
- shared or exclusive locks.
- */
- can_grant= TRUE;
}
- break;
- default:
- DBUG_ASSERT(0);
}
return can_grant;
}
-/**
- Wake up contexts which are waiting to acquire lock on individual object
- and which may succeed now, when we released some lock on it or removed
- some pending request from its waiters list (the latter can happen, for
- example, when context trying to acquire exclusive lock is killed).
-*/
+/** Remove a ticket from waiting or pending queue and wakeup up waiters. */
-void MDL_object_lock::wake_up_waiters()
+void MDL_lock::remove_ticket(Ticket_list MDL_lock::*list, MDL_ticket *ticket)
{
- /*
- There are no active locks or they are of shared type.
- We have to wake up contexts waiting for shared lock even if there is
- a pending exclusive lock as some them might be trying to acquire high
- priority shared lock.
- */
- if ((granted.is_empty() || granted.front()->is_shared()) &&
- ! waiting_shared.is_empty())
- {
- MDL_lock::Ticket_iterator it(waiting_shared);
- MDL_ticket *waiting_ticket;
- while ((waiting_ticket= it++))
- waiting_ticket->get_ctx()->awake();
- }
-
- /*
- There are no active locks (shared or exclusive).
- Wake up contexts waiting to acquire exclusive locks.
- */
- if (granted.is_empty() && ! waiting_exclusive.is_empty())
+ rw_wrlock(&m_rwlock);
+ (this->*list).remove_ticket(ticket);
+ if (is_empty())
+ mdl_locks.remove(this);
+ else
{
- MDL_lock::Ticket_iterator it(waiting_exclusive);
- MDL_ticket *waiting_ticket;
- while ((waiting_ticket= it++))
- waiting_ticket->get_ctx()->awake();
+ /*
+ There can be some contexts waiting to acquire a lock
+ which now might be able to do it. Wake them up!
+ */
+ wake_up_waiters();
+ rw_unlock(&m_rwlock);
}
}
/**
- Check whether the context already holds a compatible lock ticket
- on an object.
- Start searching the transactional locks. If not
- found in the list of transactional locks, look at LOCK TABLES
- and HANDLER locks.
+ Check if we have any pending locks which conflict with existing
+ shared lock.
- @param mdl_request Lock request object for lock to be acquired
- @param[out] is_lt_or_ha Did we pass beyond m_lt_or_ha_sentinel while
- searching for ticket?
+ @pre The ticket must match an acquired lock.
- @return A pointer to the lock ticket for the object or NULL otherwise.
+ @return TRUE if there is a conflicting lock request, FALSE otherwise.
*/
-MDL_ticket *
-MDL_context::find_ticket(MDL_request *mdl_request,
- bool *is_lt_or_ha)
+bool MDL_lock::has_pending_conflicting_lock(enum_mdl_type type)
{
- MDL_ticket *ticket;
- Ticket_iterator it(m_tickets);
-
- *is_lt_or_ha= FALSE;
-
- while ((ticket= it++))
- {
- if (ticket == m_lt_or_ha_sentinel)
- *is_lt_or_ha= TRUE;
+ bool result;
- if (mdl_request->type == ticket->m_type &&
- mdl_request->key.is_equal(&ticket->m_lock->key))
- break;
- }
+ safe_mutex_assert_not_owner(&LOCK_open);
- return ticket;
+ rw_rdlock(&m_rwlock);
+ result= (m_waiting.bitmap() & incompatible_granted_types_bitmap()[type]);
+ rw_unlock(&m_rwlock);
+ return result;
}
/**
- Try to acquire global intention exclusive lock.
+ Check if ticket represents metadata lock of "stronger" or equal type
+ than specified one. I.e. if metadata lock represented by ticket won't
+ allow any of locks which are not allowed by specified type of lock.
- @param[in/out] mdl_request Lock request object for lock to be acquired
-
- @retval FALSE Success. The lock may have not been acquired.
- One needs to check value of 'MDL_request::ticket'
- to find out what has happened.
- @retval TRUE Error.
+ @return TRUE if ticket has stronger or equal type
+ FALSE otherwise.
*/
-bool
-MDL_context::
-try_acquire_global_intention_exclusive_lock(MDL_request *mdl_request)
+bool MDL_ticket::has_stronger_or_equal_type(enum_mdl_type type) const
{
- DBUG_ASSERT(mdl_request->key.mdl_namespace() == MDL_key::GLOBAL &&
- mdl_request->type == MDL_INTENTION_EXCLUSIVE);
+ const MDL_lock::bitmap_t *
+ granted_incompat_map= m_lock->incompatible_granted_types_bitmap();
+
+ return ! (granted_incompat_map[type] & ~(granted_incompat_map[m_type]));
+}
- if (is_global_lock_owner(MDL_SHARED))
- {
- my_error(ER_CANT_UPDATE_WITH_READLOCK, MYF(0));
- return TRUE;
- }
- return try_acquire_lock_impl(mdl_request);
+bool MDL_ticket::is_incompatible_when_granted(enum_mdl_type type) const
+{
+ return (MDL_BIT(m_type) &
+ m_lock->incompatible_granted_types_bitmap()[type]);
}
-/**
- Acquire one lock with waiting for conflicting locks to go away if needed.
+bool MDL_ticket::is_incompatible_when_waiting(enum_mdl_type type) const
+{
+ return (MDL_BIT(m_type) &
+ m_lock->incompatible_waiting_types_bitmap()[type]);
+}
- @note This is an internal method which should not be used outside of MDL
- subsystem as in most cases simply waiting for conflicting locks to
- go away will lead to deadlock.
- @param mdl_request [in/out] Lock request object for lock to be acquired
+/**
+ Acquire global intention exclusive lock.
- @retval FALSE Success. MDL_request::ticket points to the ticket
- for the lock.
- @retval TRUE Failure (Out of resources or waiting is aborted),
+ @param[in] mdl_request Lock request object for lock to be acquired
+
+ @retval FALSE Success. The lock has been acquired.
+ @retval TRUE Error.
*/
bool
-MDL_context::acquire_lock_impl(MDL_request *mdl_request)
+MDL_context::acquire_global_intention_exclusive_lock(MDL_request *mdl_request)
{
- bool not_used;
- MDL_ticket *ticket;
- MDL_key *key= &mdl_request->key;
- MDL_lock *lock;
- const char *old_msg;
- st_my_thread_var *mysys_var= my_thread_var;
-
- DBUG_ASSERT(mdl_request->ticket == NULL);
- safe_mutex_assert_not_owner(&LOCK_open);
+ DBUG_ASSERT(mdl_request->key.mdl_namespace() == MDL_key::GLOBAL &&
+ mdl_request->type == MDL_INTENTION_EXCLUSIVE);
/*
- Grant lock without waiting if this context already owns this type of lock
- on this object.
-
- The fact that we don't wait in such situation allows to avoid deadlocks
- in cases when pending request for global shared lock pops up after the
- moment when thread has acquired its first intention exclusive lock but
- before it has requested the second instance of such lock.
+ If this is a non-recursive attempt to acquire global intention
+ exclusive lock we might have to wait until active global shared
+ lock or pending requests will go away. Since we won't hold any
+ resources (except associated with open HANDLERs) while doing it
+ deadlocks are not possible.
*/
- if ((mdl_request->ticket= find_ticket(mdl_request, &not_used)))
- return FALSE;
-
- if (! (ticket= MDL_ticket::create(this, mdl_request->type)))
- return TRUE;
-
- /* The below call also implicitly locks MDL_lock::m_mutex. */
- if (! (lock= mdl_locks.find_or_insert(key)))
- {
- MDL_ticket::destroy(ticket);
- return TRUE;
- }
+ DBUG_ASSERT(is_lock_owner(MDL_key::GLOBAL, "", "", MDL_INTENTION_EXCLUSIVE) ||
+ ! has_locks() ||
+ (m_trans_sentinel && m_tickets.front() == m_trans_sentinel));
- old_msg= MDL_ENTER_COND(m_thd, mysys_var, &m_ctx_wakeup_cond,
- &lock->m_mutex);
+ return acquire_lock(mdl_request);
+}
- if (! lock->can_grant_lock(this, mdl_request->type, FALSE))
- {
- if (mdl_request->is_shared())
- lock->waiting_shared.push_front(ticket);
- else
- lock->waiting_exclusive.push_front(ticket);
- do
- {
- pthread_cond_wait(&m_ctx_wakeup_cond, &lock->m_mutex);
- }
- while (! lock->can_grant_lock(this, mdl_request->type, FALSE) &&
- ! mysys_var->abort);
+/**
+ Check whether the context already holds a compatible lock ticket
+ on an object.
+ Start searching the transactional locks. If not
+ found in the list of transactional locks, look at LOCK TABLES
+ and HANDLER locks.
- if (mysys_var->abort)
- {
- /*
- We have to do MDL_EXIT_COND here and then re-acquire the lock
- as there is a chance that we will destroy MDL_lock object and
- won't be able to call MDL_EXIT_COND after it.
- */
- MDL_EXIT_COND(m_thd, mysys_var, &lock->m_mutex, old_msg);
+ @param mdl_request Lock request object for lock to be acquired
+ @param[out] is_transactional FALSE if we pass beyond m_trans_sentinel
+ while searching for ticket, otherwise TRUE.
- pthread_mutex_lock(&lock->m_mutex);
- /* Get rid of pending ticket. */
- if (mdl_request->is_shared())
- lock->waiting_shared.remove(ticket);
- else
- lock->waiting_exclusive.remove(ticket);
- if (lock->is_empty())
- mdl_locks.remove(lock);
- else
- {
- lock->wake_up_waiters();
- pthread_mutex_unlock(&lock->m_mutex);
- }
- MDL_ticket::destroy(ticket);
- return TRUE;
- }
+ @note Tickets which correspond to lock types "stronger" than one
+ being requested are also considered compatible.
- if (mdl_request->is_shared())
- lock->waiting_shared.remove(ticket);
- else
- lock->waiting_exclusive.remove(ticket);
- }
+ @return A pointer to the lock ticket for the object or NULL otherwise.
+*/
- lock->granted.push_front(ticket);
- MDL_EXIT_COND(m_thd, mysys_var, &lock->m_mutex, old_msg);
+MDL_ticket *
+MDL_context::find_ticket(MDL_request *mdl_request,
+ bool *is_transactional)
+{
+ MDL_ticket *ticket;
+ Ticket_iterator it(m_tickets);
- ticket->m_state= MDL_ACQUIRED;
- ticket->m_lock= lock;
+ *is_transactional= TRUE;
- m_tickets.push_front(ticket);
+ while ((ticket= it++))
+ {
+ if (ticket == m_trans_sentinel)
+ *is_transactional= FALSE;
- mdl_request->ticket= ticket;
+ if (mdl_request->key.is_equal(&ticket->m_lock->key) &&
+ ticket->has_stronger_or_equal_type(mdl_request->type))
+ break;
+ }
- return FALSE;
+ return ticket;
}
/**
- Acquire global intention exclusive lock.
+ Acquire one lock with waiting for conflicting locks to go away if needed.
- @param[in] mdl_request Lock request object for lock to be acquired
+ @note This is an internal method which should not be used outside of MDL
+ subsystem as in most cases simply waiting for conflicting locks to
+ go away will lead to deadlock.
- @retval FALSE Success. The lock has been acquired.
- @retval TRUE Error.
+ @param mdl_request [in/out] Lock request object for lock to be acquired
+
+ @retval FALSE Success. MDL_request::ticket points to the ticket
+ for the lock.
+ @retval TRUE Failure (Out of resources or waiting is aborted),
*/
bool
-MDL_context::acquire_global_intention_exclusive_lock(MDL_request *mdl_request)
+MDL_context::acquire_lock(MDL_request *mdl_request)
{
- DBUG_ASSERT(mdl_request->key.mdl_namespace() == MDL_key::GLOBAL &&
- mdl_request->type == MDL_INTENTION_EXCLUSIVE);
-
- if (is_global_lock_owner(MDL_SHARED))
- {
- my_error(ER_CANT_UPDATE_WITH_READLOCK, MYF(0));
- return TRUE;
- }
-
- /*
- If this is a non-recursive attempt to acquire global intention
- exclusive lock we might have to wait until active global shared
- lock or pending requests will go away. Since we won't hold any
- resources (except associated with open HANDLERs) while doing it
- deadlocks are not possible,
- */
- DBUG_ASSERT(is_global_lock_owner(MDL_INTENTION_EXCLUSIVE) ||
- ! has_locks() ||
- (m_lt_or_ha_sentinel &&
- m_tickets.front() == m_lt_or_ha_sentinel));
-
return acquire_lock_impl(mdl_request);
}
@@ -1136,22 +1228,42 @@ MDL_context::acquire_global_intention_exclusive_lock(MDL_request *mdl_request)
/**
Try to acquire one lock.
+ Unlike exclusive locks, shared locks are acquired one by
+ one. This is interface is chosen to simplify introduction of
+ the new locking API to the system. MDL_context::try_acquire_lock()
+ is currently used from open_table(), and there we have only one
+ table to work with.
+
+ This function may also be used to try to acquire an exclusive
+ lock on a destination table, by ALTER TABLE ... RENAME.
+
+ Returns immediately without any side effect if encounters a lock
+ conflict. Otherwise takes the lock.
+
+ FIXME: Compared to lock_table_name_if_not_cached() (from 5.1)
+ it gives slightly more false negatives.
+
@param mdl_request [in/out] Lock request object for lock to be acquired
@retval FALSE Success. The lock may have not been acquired.
Check the ticket, if it's NULL, a conflicting lock
- exists.
+ exists and another attempt should be made after releasing
+ all current locks and waiting for conflicting lock go
+ away (using MDL_context::wait_for_lock()).
@retval TRUE Out of resources, an error has been reported.
*/
bool
-MDL_context::try_acquire_lock_impl(MDL_request *mdl_request)
+MDL_context::try_acquire_lock(MDL_request *mdl_request)
{
MDL_lock *lock;
MDL_key *key= &mdl_request->key;
MDL_ticket *ticket;
- bool is_lt_or_ha;
+ bool is_transactional;
+ DBUG_ASSERT(mdl_request->type < MDL_SHARED_NO_WRITE ||
+ (is_lock_owner(MDL_key::GLOBAL, "", "",
+ MDL_INTENTION_EXCLUSIVE)));
DBUG_ASSERT(mdl_request->ticket == NULL);
/* Don't take chances in production. */
@@ -1162,10 +1274,10 @@ MDL_context::try_acquire_lock_impl(MDL_request *mdl_request)
Check whether the context already holds a shared lock on the object,
and if so, grant the request.
*/
- if ((ticket= find_ticket(mdl_request, &is_lt_or_ha)))
+ if ((ticket= find_ticket(mdl_request, &is_transactional)))
{
- DBUG_ASSERT(ticket->m_state == MDL_ACQUIRED);
- DBUG_ASSERT(ticket->m_type == mdl_request->type);
+ DBUG_ASSERT(ticket->m_lock);
+ DBUG_ASSERT(ticket->m_type >= mdl_request->type);
/*
If the request is for a transactional lock, and we found
a transactional lock, just reuse the found ticket.
@@ -1184,7 +1296,7 @@ MDL_context::try_acquire_lock_impl(MDL_request *mdl_request)
a different alias.
*/
mdl_request->ticket= ticket;
- if (is_lt_or_ha && clone_ticket(mdl_request))
+ if (!is_transactional && clone_ticket(mdl_request))
{
/* Clone failed. */
mdl_request->ticket= NULL;
@@ -1196,19 +1308,18 @@ MDL_context::try_acquire_lock_impl(MDL_request *mdl_request)
if (!(ticket= MDL_ticket::create(this, mdl_request->type)))
return TRUE;
- /* The below call also implicitly locks MDL_lock::m_mutex. */
+ /* The below call implicitly locks MDL_lock::m_rwlock on success. */
if (!(lock= mdl_locks.find_or_insert(key)))
{
MDL_ticket::destroy(ticket);
return TRUE;
}
- if (lock->can_grant_lock(this, mdl_request->type, FALSE))
+ if (lock->can_grant_lock(mdl_request->type, this))
{
- lock->granted.push_front(ticket);
- pthread_mutex_unlock(&lock->m_mutex);
+ lock->m_granted.add_ticket(ticket);
+ rw_unlock(&lock->m_rwlock);
- ticket->m_state= MDL_ACQUIRED;
ticket->m_lock= lock;
m_tickets.push_front(ticket);
@@ -1219,7 +1330,7 @@ MDL_context::try_acquire_lock_impl(MDL_request *mdl_request)
{
/* We can't get here if we allocated a new lock. */
DBUG_ASSERT(! lock->is_empty());
- pthread_mutex_unlock(&lock->m_mutex);
+ rw_unlock(&lock->m_rwlock);
MDL_ticket::destroy(ticket);
}
@@ -1228,42 +1339,10 @@ MDL_context::try_acquire_lock_impl(MDL_request *mdl_request)
/**
- Try to acquire one shared lock.
-
- Unlike exclusive locks, shared locks are acquired one by
- one. This is interface is chosen to simplify introduction of
- the new locking API to the system. MDL_context::try_acquire_shared_lock()
- is currently used from open_table(), and there we have only one
- table to work with.
-
- In future we may consider allocating multiple shared locks at once.
-
- @param mdl_request [in/out] Lock request object for lock to be acquired
-
- @retval FALSE Success. The lock may have not been acquired.
- Check the ticket, if it's NULL, a conflicting lock
- exists and another attempt should be made after releasing
- all current locks and waiting for conflicting lock go
- away (using MDL_context::wait_for_locks()).
- @retval TRUE Out of resources, an error has been reported.
-*/
-
-bool
-MDL_context::try_acquire_shared_lock(MDL_request *mdl_request)
-{
- DBUG_ASSERT(mdl_request->is_shared());
- DBUG_ASSERT(mdl_request->type != MDL_SHARED_UPGRADABLE ||
- is_global_lock_owner(MDL_INTENTION_EXCLUSIVE));
-
- return try_acquire_lock_impl(mdl_request);
-}
-
-
-/**
- Create a copy of a granted ticket.
+ Create a copy of a granted ticket.
This is used to make sure that HANDLER ticket
is never shared with a ticket that belongs to
- a transaction, so that when we HANDLER CLOSE,
+ a transaction, so that when we HANDLER CLOSE,
we don't release a transactional ticket, and
vice versa -- when we COMMIT, we don't mistakenly
release a ticket for an open HANDLER.
@@ -1278,25 +1357,30 @@ MDL_context::clone_ticket(MDL_request *mdl_request)
MDL_ticket *ticket;
safe_mutex_assert_not_owner(&LOCK_open);
- /* Only used for HANDLER. */
- DBUG_ASSERT(mdl_request->ticket && mdl_request->ticket->is_shared());
-
+ /*
+ By submitting mdl_request->type to MDL_ticket::create()
+ we effectively downgrade the cloned lock to the level of
+ the request.
+ */
if (!(ticket= MDL_ticket::create(this, mdl_request->type)))
return TRUE;
- ticket->m_state= MDL_ACQUIRED;
+ /* clone() is not supposed to be used to get a stronger lock. */
+ DBUG_ASSERT(ticket->m_type <= mdl_request->ticket->m_type);
+
ticket->m_lock= mdl_request->ticket->m_lock;
mdl_request->ticket= ticket;
- pthread_mutex_lock(&ticket->m_lock->m_mutex);
- ticket->m_lock->granted.push_front(ticket);
- pthread_mutex_unlock(&ticket->m_lock->m_mutex);
+ rw_wrlock(&ticket->m_lock->m_rwlock);
+ ticket->m_lock->m_granted.add_ticket(ticket);
+ rw_unlock(&ticket->m_lock->m_rwlock);
m_tickets.push_front(ticket);
return FALSE;
}
+
/**
Notify a thread holding a shared metadata lock which
conflicts with a pending exclusive lock.
@@ -1307,21 +1391,20 @@ MDL_context::clone_ticket(MDL_request *mdl_request)
void notify_shared_lock(THD *thd, MDL_ticket *conflicting_ticket)
{
- if (conflicting_ticket->is_shared())
+ /* Only try to abort locks on which we back off. */
+ if (conflicting_ticket->get_type() < MDL_SHARED_NO_WRITE)
{
- THD *conflicting_thd= conflicting_ticket->get_ctx()->get_thd();
+ MDL_context *conflicting_ctx= conflicting_ticket->get_ctx();
+ THD *conflicting_thd= conflicting_ctx->get_thd();
DBUG_ASSERT(thd != conflicting_thd); /* Self-deadlock */
/*
- If the thread that holds the conflicting lock is waiting in MDL
- subsystem it has to be woken up by calling MDL_context::awake().
- */
- conflicting_ticket->get_ctx()->awake();
- /*
- If it is waiting on table-level lock or some other non-MDL resource
- we delegate its waking up to code outside of MDL.
+ If thread which holds conflicting lock is waiting on table-level
+ lock or some other non-MDL resource we might need to wake it up
+ by calling code outside of MDL.
*/
- mysql_notify_thread_having_shared_lock(thd, conflicting_thd);
+ mysql_notify_thread_having_shared_lock(thd, conflicting_thd,
+ conflicting_ctx->get_needs_thr_lock_abort());
}
}
@@ -1331,29 +1414,26 @@ void notify_shared_lock(THD *thd, MDL_ticket *conflicting_ticket)
@param mdl_request Request for the lock to be acqured.
- @note Should not be used outside of MDL subsystem. Instead one should
- call acquire_exclusive_lock() or acquire_exclusive_locks() methods
- which ensure that conditions for deadlock-free lock acquisition are
- fulfilled.
+ @note Should not be used outside of MDL subsystem. Instead one
+ should call acquire_lock() or acquire_locks()
+ methods which ensure that conditions for deadlock-free
+ lock acquisition are fulfilled.
@retval FALSE Success
@retval TRUE Failure
*/
-bool MDL_context::acquire_exclusive_lock_impl(MDL_request *mdl_request)
+bool MDL_context::acquire_lock_impl(MDL_request *mdl_request)
{
MDL_lock *lock;
- const char *old_msg;
MDL_ticket *ticket;
bool not_used;
st_my_thread_var *mysys_var= my_thread_var;
MDL_key *key= &mdl_request->key;
- DBUG_ASSERT(mdl_request->type == MDL_EXCLUSIVE &&
- mdl_request->ticket == NULL);
-
safe_mutex_assert_not_owner(&LOCK_open);
+ DBUG_ASSERT(mdl_request->ticket == NULL);
/* Don't take chances in production. */
mdl_request->ticket= NULL;
@@ -1363,129 +1443,67 @@ bool MDL_context::acquire_exclusive_lock_impl(MDL_request *mdl_request)
*/
if ((ticket= find_ticket(mdl_request, &not_used)))
{
- DBUG_ASSERT(ticket->m_state == MDL_ACQUIRED);
- DBUG_ASSERT(ticket->m_type == MDL_EXCLUSIVE);
+ DBUG_ASSERT(ticket->m_lock);
mdl_request->ticket= ticket;
return FALSE;
}
- DBUG_ASSERT(is_global_lock_owner(MDL_INTENTION_EXCLUSIVE));
+ DBUG_ASSERT(mdl_request->type < MDL_SHARED_NO_WRITE ||
+ is_lock_owner(MDL_key::GLOBAL, "", "", MDL_INTENTION_EXCLUSIVE));
/* Early allocation: ticket will be needed in any case. */
if (!(ticket= MDL_ticket::create(this, mdl_request->type)))
return TRUE;
- /* The below call also implicitly locks MDL_lock::m_mutex. */
+ /* The below call implicitly locks MDL_lock::m_rwlock on success. */
if (!(lock= mdl_locks.find_or_insert(key)))
{
MDL_ticket::destroy(ticket);
return TRUE;
}
- lock->waiting_exclusive.push_front(ticket);
+ ticket->m_lock= lock;
- old_msg= MDL_ENTER_COND(m_thd, mysys_var, &m_ctx_wakeup_cond,
- &lock->m_mutex);
+ lock->m_waiting.add_ticket(ticket);
- while (!lock->can_grant_lock(this, mdl_request->type, FALSE))
+ while (!lock->can_grant_lock(mdl_request->type, this))
{
- if (m_lt_or_ha_sentinel)
- {
- /*
- We're about to start waiting. Don't do it if we have
- HANDLER locks (we can't have any other locks here).
- Waiting with locks may lead to a deadlock.
+ wait_reset();
- We have to do MDL_EXIT_COND here and then re-acquire the
- lock as there is a chance that we will destroy MDL_lock
- object and won't be able to call MDL_EXIT_COND after it.
- */
- MDL_EXIT_COND(m_thd, mysys_var, &lock->m_mutex, old_msg);
-
- pthread_mutex_lock(&lock->m_mutex);
- /* Get rid of pending ticket. */
- lock->waiting_exclusive.remove(ticket);
- if (lock->is_empty())
- mdl_locks.remove(lock);
- else
- {
- /*
- There can be some contexts waiting to acquire shared
- lock which now might be able to do it. Wake them up!
- */
- lock->wake_up_waiters();
- pthread_mutex_unlock(&lock->m_mutex);
- }
- MDL_ticket::destroy(ticket);
- my_error(ER_LOCK_DEADLOCK, MYF(0));
- return TRUE;
- }
+ if (ticket->is_upgradable_or_exclusive())
+ lock->notify_shared_locks(this);
- MDL_ticket *conflicting_ticket;
- MDL_lock::Ticket_iterator it(lock->granted);
+ rw_unlock(&lock->m_rwlock);
- while ((conflicting_ticket= it++))
- notify_shared_lock(m_thd, conflicting_ticket);
+ set_deadlock_weight(mdl_request->get_deadlock_weight());
+ will_wait_for(ticket);
/* There is a shared or exclusive lock on the object. */
- DEBUG_SYNC(m_thd, "mdl_acquire_exclusive_locks_wait");
+ DEBUG_SYNC(m_thd, "mdl_acquire_lock_wait");
- /*
- Another thread might have obtained a shared MDL lock on some table
- but has not yet opened it and/or tried to obtain data lock on it.
- Also invocation of acquire_exclusive_lock() method and consequently
- first call to notify_shared_lock() might have happened right after
- thread holding shared metadata lock in wait_for_locks() method
- checked that there are no pending conflicting locks but before
- it has started waiting.
- In both these cases we need to sleep until these threads will start
- waiting and try to abort them once again.
-
- QQ: What is the optimal value for this sleep?
- */
- struct timespec abstime;
- set_timespec(abstime, 1);
- pthread_cond_timedwait(&m_ctx_wakeup_cond, &lock->m_mutex, &abstime);
+ bool is_deadlock= (find_deadlock() || timed_wait(1) == VICTIM_WAKE_UP);
- if (mysys_var->abort)
- {
- /*
- We have to do MDL_EXIT_COND here and then re-acquire the lock
- as there is a chance that we will destroy MDL_lock object and
- won't be able to call MDL_EXIT_COND after it.
- */
- MDL_EXIT_COND(m_thd, mysys_var, &lock->m_mutex, old_msg);
+ stop_waiting();
- pthread_mutex_lock(&lock->m_mutex);
- /* Get rid of pending ticket. */
- lock->waiting_exclusive.remove(ticket);
- if (lock->is_empty())
- mdl_locks.remove(lock);
- else
- {
- /*
- There can be some contexts waiting to acquire shared
- lock which now might be able to do it. Wake them up!
- */
- lock->wake_up_waiters();
- pthread_mutex_unlock(&lock->m_mutex);
- }
+ if (is_deadlock || mysys_var->abort)
+ {
+ lock->remove_ticket(&MDL_lock::m_waiting, ticket);
MDL_ticket::destroy(ticket);
+ if (is_deadlock)
+ my_error(ER_LOCK_DEADLOCK, MYF(0));
return TRUE;
}
+ rw_wrlock(&lock->m_rwlock);
}
- lock->waiting_exclusive.remove(ticket);
- lock->granted.push_front(ticket);
+ lock->m_waiting.remove_ticket(ticket);
+ lock->m_granted.add_ticket(ticket);
- if (lock->cached_object)
+ if (ticket->get_type() == MDL_EXCLUSIVE && lock->cached_object)
(*lock->cached_object_release_hook)(lock->cached_object);
lock->cached_object= NULL;
- MDL_EXIT_COND(m_thd, mysys_var, &lock->m_mutex, old_msg);
-
- ticket->m_state= MDL_ACQUIRED;
- ticket->m_lock= lock;
+ rw_unlock(&lock->m_rwlock);
m_tickets.push_front(ticket);
@@ -1495,28 +1513,6 @@ bool MDL_context::acquire_exclusive_lock_impl(MDL_request *mdl_request)
}
-/**
- Acquire an exclusive lock.
-
- @param mdl_request Request for the lock to be acqured.
-
- @note Assumes that one already owns global intention exclusive lock.
-
- @retval FALSE Success
- @retval TRUE Failure
-*/
-
-bool MDL_context::acquire_exclusive_lock(MDL_request *mdl_request)
-{
- /* Exclusive locks must always be acquired first, all at once. */
- DBUG_ASSERT(! m_tickets.is_empty() &&
- m_tickets.front()->m_lock->key.mdl_namespace() == MDL_key::GLOBAL &&
- ++Ticket_list::Iterator(m_tickets) == m_lt_or_ha_sentinel);
-
- return acquire_exclusive_lock_impl(mdl_request);
-}
-
-
extern "C" int mdl_request_ptr_cmp(const void* ptr1, const void* ptr2)
{
MDL_request *req1= *(MDL_request**)ptr1;
@@ -1543,37 +1539,37 @@ extern "C" int mdl_request_ptr_cmp(const void* ptr1, const void* ptr2)
@retval TRUE Failure
*/
-bool MDL_context::acquire_exclusive_locks(MDL_request_list *mdl_requests)
+bool MDL_context::acquire_locks(MDL_request_list *mdl_requests)
{
MDL_request_list::Iterator it(*mdl_requests);
- MDL_request **sort_buf;
- uint i;
+ MDL_request **sort_buf, **p_req;
+ uint req_count= mdl_requests->elements();
+
+ if (req_count == 0)
+ return FALSE;
/*
- Exclusive locks must always be acquired first, all at once.
+ To reduce deadlocks, the server acquires all exclusive
+ locks at once. For shared locks, try_acquire_lock() is
+ used instead.
*/
- DBUG_ASSERT(! m_tickets.is_empty() &&
- m_tickets.front()->m_lock->key.mdl_namespace() == MDL_key::GLOBAL &&
- ++Ticket_list::Iterator(m_tickets) == m_lt_or_ha_sentinel);
-
- if (mdl_requests->is_empty())
- return FALSE;
+ DBUG_ASSERT(m_tickets.is_empty() || m_tickets.front() == m_trans_sentinel);
/* Sort requests according to MDL_key. */
- if (! (sort_buf= (MDL_request **)my_malloc(mdl_requests->elements() *
- sizeof(MDL_request *),
+ if (! (sort_buf= (MDL_request **)my_malloc(req_count *
+ sizeof(MDL_request*),
MYF(MY_WME))))
return TRUE;
- for (i= 0; i < mdl_requests->elements(); i++)
- sort_buf[i]= it++;
+ for (p_req= sort_buf; p_req < sort_buf + req_count; p_req++)
+ *p_req= it++;
- my_qsort(sort_buf, mdl_requests->elements(), sizeof(MDL_request*),
+ my_qsort(sort_buf, req_count, sizeof(MDL_request*),
mdl_request_ptr_cmp);
- for (i= 0; i < mdl_requests->elements(); i++)
+ for (p_req= sort_buf; p_req < sort_buf + req_count; p_req++)
{
- if (acquire_exclusive_lock_impl(sort_buf[i]))
+ if (acquire_lock_impl(*p_req))
goto err;
}
my_free(sort_buf, MYF(0));
@@ -1581,11 +1577,12 @@ bool MDL_context::acquire_exclusive_locks(MDL_request_list *mdl_requests)
err:
/* Release locks we have managed to acquire so far. */
- for (i= 0; i < mdl_requests->elements() && sort_buf[i]->ticket; i++)
+ for (req_count= p_req - sort_buf, p_req= sort_buf;
+ p_req < sort_buf + req_count; p_req++)
{
- release_lock(sort_buf[i]->ticket);
+ release_lock((*p_req)->ticket);
/* Reset lock request back to its initial state. */
- sort_buf[i]->ticket= NULL;
+ (*p_req)->ticket= NULL;
}
my_free(sort_buf, MYF(0));
return TRUE;
@@ -1612,251 +1609,198 @@ err:
*/
bool
-MDL_ticket::upgrade_shared_lock_to_exclusive()
+MDL_context::upgrade_shared_lock_to_exclusive(MDL_ticket *mdl_ticket)
{
- const char *old_msg;
- st_my_thread_var *mysys_var= my_thread_var;
- THD *thd= m_ctx->get_thd();
- MDL_ticket *pending_ticket;
+ MDL_request mdl_xlock_request;
+ MDL_ticket *mdl_svp= mdl_savepoint();
+ bool is_new_ticket;
DBUG_ENTER("MDL_ticket::upgrade_shared_lock_to_exclusive");
- DEBUG_SYNC(thd, "mdl_upgrade_shared_lock_to_exclusive");
+ DEBUG_SYNC(get_thd(), "mdl_upgrade_shared_lock_to_exclusive");
- safe_mutex_assert_not_owner(&LOCK_open);
-
- /* Allow this function to be called twice for the same lock request. */
- if (m_type == MDL_EXCLUSIVE)
+ /*
+ Do nothing if already upgraded. Used when we FLUSH TABLE under
+ LOCK TABLES and a table is listed twice in LOCK TABLES list.
+ */
+ if (mdl_ticket->m_type == MDL_EXCLUSIVE)
DBUG_RETURN(FALSE);
- /* Only allow upgrades from MDL_SHARED_UPGRADABLE */
- DBUG_ASSERT(m_type == MDL_SHARED_UPGRADABLE);
+ /* Only allow upgrades from MDL_SHARED_NO_WRITE/NO_READ_WRITE */
+ DBUG_ASSERT(mdl_ticket->m_type == MDL_SHARED_NO_WRITE ||
+ mdl_ticket->m_type == MDL_SHARED_NO_READ_WRITE);
- /*
- Since we should have already acquired an intention exclusive
- global lock this call is only enforcing asserts.
- */
- DBUG_ASSERT(m_ctx->is_global_lock_owner(MDL_INTENTION_EXCLUSIVE));
+ mdl_xlock_request.init(&mdl_ticket->m_lock->key, MDL_EXCLUSIVE);
- /*
- Create an auxiliary ticket to represent a pending exclusive
- lock and add it to the 'waiting' queue for the duration
- of upgrade. During upgrade we abort waits of connections
- that own conflicting locks. A pending request is used
- to signal such connections that upon waking up they
- must back off, rather than fall into sleep again.
- */
- if (! (pending_ticket= MDL_ticket::create(m_ctx, MDL_EXCLUSIVE)))
+ if (acquire_lock_impl(&mdl_xlock_request))
DBUG_RETURN(TRUE);
- pthread_mutex_lock(&m_lock->m_mutex);
+ is_new_ticket= ! has_lock(mdl_svp, mdl_xlock_request.ticket);
- m_lock->waiting_exclusive.push_front(pending_ticket);
+ /* Merge the acquired and the original lock. @todo: move to a method. */
+ rw_wrlock(&mdl_ticket->m_lock->m_rwlock);
+ if (is_new_ticket)
+ mdl_ticket->m_lock->m_granted.remove_ticket(mdl_xlock_request.ticket);
+ /*
+ Set the new type of lock in the ticket. To update state of
+ MDL_lock object correctly we need to temporarily exclude
+ ticket from the granted queue and then include it back.
+ */
+ mdl_ticket->m_lock->m_granted.remove_ticket(mdl_ticket);
+ mdl_ticket->m_type= MDL_EXCLUSIVE;
+ mdl_ticket->m_lock->m_granted.add_ticket(mdl_ticket);
- old_msg= MDL_ENTER_COND(thd, mysys_var, &m_ctx->m_ctx_wakeup_cond,
- &m_lock->m_mutex);
+ rw_unlock(&mdl_ticket->m_lock->m_rwlock);
- while (1)
+ if (is_new_ticket)
{
- if (m_lock->can_grant_lock(m_ctx, MDL_EXCLUSIVE, TRUE))
- break;
+ m_tickets.remove(mdl_xlock_request.ticket);
+ MDL_ticket::destroy(mdl_xlock_request.ticket);
+ }
- MDL_ticket *conflicting_ticket;
- MDL_lock::Ticket_iterator it(m_lock->granted);
+ DBUG_RETURN(FALSE);
+}
- /*
- If m_ctx->lt_or_ha_sentinel(), and this sentinel is for HANDLER,
- we can deadlock. However, HANDLER is not allowed under
- LOCK TABLES, and apart from LOCK TABLES there are only
- two cases of lock upgrade: ALTER TABLE and CREATE/DROP
- TRIGGER (*). This leaves us with the following scenario
- for deadlock:
-
- connection 1 connection 2
- handler t1 open; handler t2 open;
- alter table t2 ... alter table t1 ...
-
- This scenario is quite remote, since ALTER
- (and CREATE/DROP TRIGGER) performs mysql_ha_flush() in
- the beginning, and thus closes open HANDLERS against which
- there is a pending lock upgrade. Still, two ALTER statements
- can interleave and not notice each other's pending lock
- (e.g. if both upgrade their locks at the same time).
- This, however, is quite unlikely, so we do nothing to
- address it.
-
- (*) There is no requirement to upgrade lock in
- CREATE/DROP TRIGGER, it's used there just for convenience.
-
- A temporary work-around to avoid deadlocks/livelocks in
- a situation when in one connection ALTER TABLE tries to
- upgrade its metadata lock and in another connection
- the active transaction already got this lock in some
- of its earlier statements.
- In such case this transaction always succeeds with getting
- a metadata lock on the table -- it already has one.
- But later on it may block on the table level lock, since ALTER
- got TL_WRITE_ALLOW_READ, and subsequently get aborted
- by notify_shared_lock().
- An abort will lead to a back off, and a second attempt to
- get an MDL lock (successful), and a table lock (-> livelock).
-
- The call below breaks this loop by forcing transactions to call
- tdc_wait_for_old_versions() (even if the transaction doesn't need
- any new metadata locks), which in turn will check if someone
- is waiting on the owned MDL lock, and produce ER_LOCK_DEADLOCK.
-
- TODO: Long-term such deadlocks/livelock will be resolved within
- MDL subsystem and thus this call will become unnecessary.
- */
- mysql_abort_transactions_with_shared_lock(&m_lock->key);
- while ((conflicting_ticket= it++))
- {
- if (conflicting_ticket->m_ctx != m_ctx)
- notify_shared_lock(thd, conflicting_ticket);
- }
+bool MDL_lock::find_deadlock(MDL_ticket *waiting_ticket,
+ Deadlock_detection_context *deadlock_ctx)
+{
+ MDL_ticket *ticket;
+ bool result= FALSE;
- /* There is a shared or exclusive lock on the object. */
- DEBUG_SYNC(thd, "mdl_upgrade_shared_lock_to_exclusive_wait");
+ rw_rdlock(&m_rwlock);
- /*
- Another thread might have obtained a shared MDL lock on some table
- but has not yet opened it and/or tried to obtain data lock on it.
- Also invocation of acquire_exclusive_lock() method and consequently
- first call to notify_shared_lock() might have happened right after
- thread holding shared metadata lock in wait_for_locks() method
- checked that there are no pending conflicting locks but before
- it has started waiting.
- In both these cases we need to sleep until these threads will start
- waiting and try to abort them once again.
- */
- struct timespec abstime;
- set_timespec(abstime, 1);
- pthread_cond_timedwait(&m_ctx->m_ctx_wakeup_cond, &m_lock->m_mutex,
- &abstime);
+ Ticket_iterator granted_it(m_granted);
+ Ticket_iterator waiting_it(m_waiting);
- if (mysys_var->abort)
+ while ((ticket= granted_it++))
+ {
+ if (ticket->is_incompatible_when_granted(waiting_ticket->get_type()) &&
+ ticket->get_ctx() != waiting_ticket->get_ctx() &&
+ ticket->get_ctx() == deadlock_ctx->start)
{
- m_lock->waiting_exclusive.remove(pending_ticket);
- /*
- If there are no other pending requests for exclusive locks
- we need to wake up threads waiting for a chance to acquire
- shared lock.
- */
- m_lock->wake_up_waiters();
- MDL_EXIT_COND(thd, mysys_var, &m_lock->m_mutex, old_msg);
- MDL_ticket::destroy(pending_ticket);
- DBUG_RETURN(TRUE);
+ result= TRUE;
+ goto end;
}
}
- /* Set the new type of lock in the ticket. */
- m_type= MDL_EXCLUSIVE;
-
- /* Remove and destroy the auxiliary pending ticket. */
- m_lock->waiting_exclusive.remove(pending_ticket);
-
- if (m_lock->cached_object)
- (*m_lock->cached_object_release_hook)(m_lock->cached_object);
- m_lock->cached_object= 0;
+ while ((ticket= waiting_it++))
+ {
+ if (ticket->is_incompatible_when_waiting(waiting_ticket->get_type()) &&
+ ticket->get_ctx() != waiting_ticket->get_ctx() &&
+ ticket->get_ctx() == deadlock_ctx->start)
+ {
+ result= TRUE;
+ goto end;
+ }
+ }
- MDL_EXIT_COND(thd, mysys_var, &m_lock->m_mutex, old_msg);
+ granted_it.rewind();
+ while ((ticket= granted_it++))
+ {
+ if (ticket->is_incompatible_when_granted(waiting_ticket->get_type()) &&
+ ticket->get_ctx() != waiting_ticket->get_ctx() &&
+ ticket->get_ctx()->find_deadlock(deadlock_ctx))
+ {
+ result= TRUE;
+ goto end;
+ }
+ }
- MDL_ticket::destroy(pending_ticket);
+ waiting_it.rewind();
+ while ((ticket= waiting_it++))
+ {
+ if (ticket->is_incompatible_when_waiting(waiting_ticket->get_type()) &&
+ ticket->get_ctx() != waiting_ticket->get_ctx() &&
+ ticket->get_ctx()->find_deadlock(deadlock_ctx))
+ {
+ result= TRUE;
+ goto end;
+ }
+ }
- DBUG_RETURN(FALSE);
+end:
+ rw_unlock(&m_rwlock);
+ return result;
}
-/**
- Try to acquire an exclusive lock on the object if there are
- no conflicting locks.
-
- Similar to the previous function, but returns
- immediately without any side effect if encounters a lock
- conflict. Otherwise takes the lock.
-
- This function is used in CREATE TABLE ... LIKE to acquire a lock
- on the table to be created. In this statement we don't want to
- block and wait for the lock if the table already exists.
-
- @param mdl_request [in] The lock request
- @param conflict [out] Indicates that conflicting lock exists
-
- @retval TRUE Failure: some error occurred (probably OOM).
- @retval FALSE Success: the lock might have not been acquired,
- check request.ticket to find out.
-
- FIXME: Compared to lock_table_name_if_not_cached()
- it gives slightly more false negatives.
-*/
-
-bool
-MDL_context::try_acquire_exclusive_lock(MDL_request *mdl_request)
+bool MDL_context::find_deadlock(Deadlock_detection_context *deadlock_ctx)
{
- DBUG_ASSERT(mdl_request->type == MDL_EXCLUSIVE);
- DBUG_ASSERT(is_global_lock_owner(MDL_INTENTION_EXCLUSIVE));
+ bool result= FALSE;
- return try_acquire_lock_impl(mdl_request);
-}
+ rw_rdlock(&m_waiting_for_lock);
+ if (m_waiting_for)
+ {
+ /*
+ QQ: should we rather be checking for NO_WAKE_UP ?
-/**
- Acquire the global shared metadata lock.
-
- Holding this lock will block all requests for exclusive locks
- and shared locks which can be potentially upgraded to exclusive.
-
- @retval FALSE Success -- the lock was granted.
- @retval TRUE Failure -- our thread was killed.
-*/
-
-bool MDL_context::acquire_global_shared_lock()
-{
- MDL_request mdl_request;
-
- DBUG_ASSERT(! is_global_lock_owner(MDL_SHARED));
-
- mdl_request.init(MDL_key::GLOBAL, "", "", MDL_SHARED);
+ We want to do check signal only when m_waiting_for is set
+ to avoid reading left-overs from previous kills.
+ */
+ if (peek_signal() != VICTIM_WAKE_UP)
+ {
- if (acquire_lock_impl(&mdl_request))
- return TRUE;
+ if (++deadlock_ctx->current_search_depth >
+ deadlock_ctx->MAX_SEARCH_DEPTH)
+ result= TRUE;
+ else
+ result= m_waiting_for->m_lock->find_deadlock(m_waiting_for,
+ deadlock_ctx);
+ --deadlock_ctx->current_search_depth;
+ }
+ }
- move_ticket_after_lt_or_ha_sentinel(mdl_request.ticket);
+ if (result)
+ {
+ if (! deadlock_ctx->victim)
+ deadlock_ctx->victim= this;
+ else if (deadlock_ctx->victim->m_deadlock_weight >= m_deadlock_weight)
+ {
+ rw_unlock(&deadlock_ctx->victim->m_waiting_for_lock);
+ deadlock_ctx->victim= this;
+ }
+ else
+ rw_unlock(&m_waiting_for_lock);
+ }
+ else
+ rw_unlock(&m_waiting_for_lock);
- return FALSE;
+ return result;
}
-/**
- Implement a simple deadlock detection heuristic: check if there
- are any pending exclusive locks which conflict with shared locks
- held by this thread. In that case waiting can be circular,
- i.e. lead to a deadlock.
-
- @return TRUE If there are any pending conflicting locks.
- FALSE Otherwise.
-*/
-
-bool MDL_context::can_wait_lead_to_deadlock() const
+bool MDL_context::find_deadlock()
{
- Ticket_iterator ticket_it(m_tickets);
- MDL_ticket *ticket;
+ Deadlock_detection_context deadlock_ctx(this);
- while ((ticket= ticket_it++))
+ while (1)
{
- /*
- In MySQL we never call this method while holding exclusive or
- upgradeable shared metadata locks.
- Otherwise we would also have to check for the presence of pending
- requests for conflicting types of global lock.
- In addition MDL_ticket::has_pending_conflicting_lock()
- won't work properly for exclusive type of lock.
- */
- DBUG_ASSERT(! ticket->is_upgradable_or_exclusive());
+ if (! find_deadlock(&deadlock_ctx))
+ {
+ /* No deadlocks are found! */
+ break;
+ }
- if (ticket->has_pending_conflicting_lock())
+ if (deadlock_ctx.victim != this)
+ {
+ deadlock_ctx.victim->awake(VICTIM_WAKE_UP);
+ rw_unlock(&deadlock_ctx.victim->m_waiting_for_lock);
+ /*
+ After adding new arc to waiting graph we found that it participates
+ in some loop (i.e. there is a deadlock). We decided to destroy this
+ loop by removing some arc other than newly added. Since this doesn't
+ guarantee that all loops created by addition of this arc are
+ destroyed we have to repeat search.
+ */
+ continue;
+ }
+ else
+ {
+ DBUG_ASSERT(&deadlock_ctx.victim->m_waiting_for_lock == &m_waiting_for_lock);
+ rw_unlock(&deadlock_ctx.victim->m_waiting_for_lock);
return TRUE;
+ }
}
return FALSE;
}
@@ -1876,16 +1820,15 @@ bool MDL_context::can_wait_lead_to_deadlock() const
*/
bool
-MDL_context::wait_for_locks(MDL_request_list *mdl_requests)
+MDL_context::wait_for_lock(MDL_request *mdl_request)
{
MDL_lock *lock;
- MDL_request *mdl_request;
- MDL_request_list::Iterator it(*mdl_requests);
- const char *old_msg;
st_my_thread_var *mysys_var= my_thread_var;
safe_mutex_assert_not_owner(&LOCK_open);
+ DBUG_ASSERT(mdl_request->ticket == NULL);
+
while (!mysys_var->abort)
{
/*
@@ -1900,87 +1843,45 @@ MDL_context::wait_for_locks(MDL_request_list *mdl_requests)
*/
mysql_ha_flush(m_thd);
- /*
- In cases when we wait while still holding some metadata
- locks deadlocks are possible.
- To avoid them we use the following simple empiric - don't
- wait for new lock request to be satisfied if for one of the
- locks which are already held by this connection there is
- a conflicting request (i.e. this connection should not wait
- if someone waits for it).
- This empiric should work well (e.g. give low number of false
- negatives) in situations when conflicts are rare (in our
- case this is true since DDL statements should be rare).
- */
- if (can_wait_lead_to_deadlock())
+ MDL_key *key= &mdl_request->key;
+
+ /* The below call implicitly locks MDL_lock::m_rwlock on success. */
+ if (! (lock= mdl_locks.find(key)))
+ return FALSE;
+
+ if (lock->can_grant_lock(mdl_request->type, this))
{
- my_error(ER_LOCK_DEADLOCK, MYF(0));
- return TRUE;
+ rw_unlock(&lock->m_rwlock);
+ return FALSE;
}
- it.rewind();
- while ((mdl_request= it++))
+ MDL_ticket *pending_ticket;
+ if (! (pending_ticket= MDL_ticket::create(this, mdl_request->type)))
{
- MDL_key *key= &mdl_request->key;
- DBUG_ASSERT(mdl_request->ticket == NULL);
-
- /*
- To avoid starvation we don't wait if we have a conflict against
- request for MDL_EXCLUSIVE lock.
- */
- if (mdl_request->is_shared() ||
- mdl_request->type == MDL_INTENTION_EXCLUSIVE)
- {
- /* The below call also implicitly locks MDL_lock::m_mutex. */
- if (! (lock= mdl_locks.find(key)))
- continue;
-
- if (lock->can_grant_lock(this, mdl_request->type, FALSE))
- {
- pthread_mutex_unlock(&lock->m_mutex);
- continue;
- }
-
- MDL_ticket *pending_ticket;
- if (! (pending_ticket= MDL_ticket::create(this, mdl_request->type)))
- {
- pthread_mutex_unlock(&lock->m_mutex);
- return TRUE;
- }
- if (mdl_request->is_shared())
- lock->waiting_shared.push_front(pending_ticket);
- else
- lock->waiting_exclusive.push_front(pending_ticket);
-
- old_msg= MDL_ENTER_COND(m_thd, mysys_var, &m_ctx_wakeup_cond,
- &lock->m_mutex);
-
- pthread_cond_wait(&m_ctx_wakeup_cond, &lock->m_mutex);
-
- /*
- We have to do MDL_EXIT_COND here and then re-acquire the lock
- as there is a chance that we will destroy MDL_lock object and
- won't be able to call MDL_EXIT_COND after it.
- */
- MDL_EXIT_COND(m_thd, mysys_var, &lock->m_mutex, old_msg);
-
- pthread_mutex_lock(&lock->m_mutex);
- if (mdl_request->is_shared())
- lock->waiting_shared.remove(pending_ticket);
- else
- lock->waiting_exclusive.remove(pending_ticket);
- if (lock->is_empty())
- mdl_locks.remove(lock);
- else
- pthread_mutex_unlock(&lock->m_mutex);
- MDL_ticket::destroy(pending_ticket);
- break;
- }
+ rw_unlock(&lock->m_rwlock);
+ return TRUE;
}
- if (!mdl_request)
+
+ pending_ticket->m_lock= lock;
+
+ lock->m_waiting.add_ticket(pending_ticket);
+
+ wait_reset();
+ rw_unlock(&lock->m_rwlock);
+
+ set_deadlock_weight(MDL_DEADLOCK_WEIGHT_DML);
+ will_wait_for(pending_ticket);
+
+ bool is_deadlock= (find_deadlock() || wait() == VICTIM_WAKE_UP);
+
+ stop_waiting();
+
+ lock->remove_ticket(&MDL_lock::m_waiting, pending_ticket);
+ MDL_ticket::destroy(pending_ticket);
+ if (is_deadlock)
{
- /* There are no conflicts for any locks! */
- break;
+ my_error(ER_LOCK_DEADLOCK, MYF(0));
+ return TRUE;
}
}
return mysys_var->abort;
@@ -2000,23 +1901,13 @@ void MDL_context::release_lock(MDL_ticket *ticket)
DBUG_PRINT("enter", ("db=%s name=%s", lock->key.db_name(),
lock->key.name()));
- DBUG_ASSERT(this == ticket->m_ctx);
+ DBUG_ASSERT(this == ticket->get_ctx());
safe_mutex_assert_not_owner(&LOCK_open);
- if (ticket == m_lt_or_ha_sentinel)
- m_lt_or_ha_sentinel= ++Ticket_list::Iterator(m_tickets, ticket);
-
- pthread_mutex_lock(&lock->m_mutex);
+ if (ticket == m_trans_sentinel)
+ m_trans_sentinel= ++Ticket_list::Iterator(m_tickets, ticket);
- lock->granted.remove(ticket);
-
- if (lock->is_empty())
- mdl_locks.remove(lock);
- else
- {
- lock->wake_up_waiters();
- pthread_mutex_unlock(&lock->m_mutex);
- }
+ lock->remove_ticket(&MDL_lock::m_granted, ticket);
m_tickets.remove(ticket);
MDL_ticket::destroy(ticket);
@@ -2086,7 +1977,7 @@ void MDL_context::release_all_locks_for_name(MDL_ticket *name)
while ((ticket= it_ticket++))
{
- DBUG_ASSERT(ticket->m_state == MDL_ACQUIRED);
+ DBUG_ASSERT(ticket->m_lock);
/*
We rarely have more than one ticket in this loop,
let's not bother saving on pthread_cond_broadcast().
@@ -2099,89 +1990,43 @@ void MDL_context::release_all_locks_for_name(MDL_ticket *name)
/**
Downgrade an exclusive lock to shared metadata lock.
-*/
-
-void MDL_ticket::downgrade_exclusive_lock()
-{
- safe_mutex_assert_not_owner(&LOCK_open);
-
- if (is_shared())
- return;
-
- pthread_mutex_lock(&m_lock->m_mutex);
- m_type= MDL_SHARED_UPGRADABLE;
-
- if (! m_lock->waiting_shared.is_empty())
- {
- MDL_lock::Ticket_iterator it(m_lock->waiting_shared);
- MDL_ticket *ticket;
- while ((ticket= it++))
- ticket->get_ctx()->awake();
- }
-
- pthread_mutex_unlock(&m_lock->m_mutex);
-}
-
-/**
- Release the global shared metadata lock.
+ @param type Type of lock to which exclusive lock should be downgraded.
*/
-void MDL_context::release_global_shared_lock()
+void MDL_ticket::downgrade_exclusive_lock(enum_mdl_type type)
{
- MDL_request mdl_request;
- MDL_ticket *ticket;
- bool not_used;
-
- mdl_request.init(MDL_key::GLOBAL, "", "", MDL_SHARED);
-
safe_mutex_assert_not_owner(&LOCK_open);
/*
- TODO/QQ/FIXME: In theory we always should be able to find
- ticket here. But in practice this is not
- always TRUE.
+ Do nothing if already downgraded. Used when we FLUSH TABLE under
+ LOCK TABLES and a table is listed twice in LOCK TABLES list.
*/
+ if (m_type != MDL_EXCLUSIVE)
+ return;
- if ((ticket= find_ticket(&mdl_request, &not_used)))
- release_lock(ticket);
-}
-
-
-/**
- Auxiliary function which allows to check if we have exclusive lock
- on the object.
-
- @param mdl_namespace Id of object namespace
- @param db Name of the database
- @param name Name of the object
-
- @return TRUE if current context contains exclusive lock for the object,
- FALSE otherwise.
-*/
-
-bool
-MDL_context::is_exclusive_lock_owner(MDL_key::enum_mdl_namespace mdl_namespace,
- const char *db, const char *name)
-{
- MDL_request mdl_request;
- bool is_lt_or_ha_unused;
- mdl_request.init(mdl_namespace, db, name, MDL_EXCLUSIVE);
- MDL_ticket *ticket= find_ticket(&mdl_request, &is_lt_or_ha_unused);
-
- DBUG_ASSERT(ticket == NULL || ticket->m_state == MDL_ACQUIRED);
-
- return ticket;
+ rw_wrlock(&m_lock->m_rwlock);
+ /*
+ To update state of MDL_lock object correctly we need to temporarily
+ exclude ticket from the granted queue and then include it back.
+ */
+ m_lock->m_granted.remove_ticket(this);
+ m_type= type;
+ m_lock->m_granted.add_ticket(this);
+ m_lock->wake_up_waiters();
+ rw_unlock(&m_lock->m_rwlock);
}
/**
Auxiliary function which allows to check if we have some kind of lock on
- a object.
+ a object. Returns TRUE if we have a lock of a given or stronger type.
@param mdl_namespace Id of object namespace
@param db Name of the database
@param name Name of the object
+ @param mdl_type Lock type. Pass in the weakest type to find
+ out if there is at least some lock.
@return TRUE if current context contains satisfied lock for the object,
FALSE otherwise.
@@ -2189,25 +2034,22 @@ MDL_context::is_exclusive_lock_owner(MDL_key::enum_mdl_namespace mdl_namespace,
bool
MDL_context::is_lock_owner(MDL_key::enum_mdl_namespace mdl_namespace,
- const char *db, const char *name)
+ const char *db, const char *name,
+ enum_mdl_type mdl_type)
{
- MDL_key key(mdl_namespace, db, name);
- MDL_ticket *ticket;
- MDL_context::Ticket_iterator it(m_tickets);
+ MDL_request mdl_request;
+ bool is_transactional_unused;
+ mdl_request.init(mdl_namespace, db, name, mdl_type);
+ MDL_ticket *ticket= find_ticket(&mdl_request, &is_transactional_unused);
- while ((ticket= it++))
- {
- if (ticket->m_lock->key.is_equal(&key))
- break;
- }
+ DBUG_ASSERT(ticket == NULL || ticket->m_lock);
return ticket;
}
/**
- Check if we have any pending exclusive locks which conflict with
- existing shared lock.
+ Check if we have any pending locks which conflict with existing shared lock.
@pre The ticket must match an acquired lock.
@@ -2216,10 +2058,7 @@ MDL_context::is_lock_owner(MDL_key::enum_mdl_namespace mdl_namespace,
bool MDL_ticket::has_pending_conflicting_lock() const
{
- safe_mutex_assert_not_owner(&LOCK_open);
- DBUG_ASSERT(is_shared());
-
- return m_lock->has_pending_exclusive_lock();
+ return m_lock->has_pending_conflicting_lock(m_type);
}
@@ -2304,7 +2143,7 @@ void MDL_context::rollback_to_savepoint(MDL_ticket *mdl_savepoint)
/* If savepoint is NULL, it is from the start of the transaction. */
release_locks_stored_before(mdl_savepoint ?
- mdl_savepoint : m_lt_or_ha_sentinel);
+ mdl_savepoint : m_trans_sentinel);
DBUG_VOID_RETURN;
}
@@ -2322,7 +2161,7 @@ void MDL_context::rollback_to_savepoint(MDL_ticket *mdl_savepoint)
void MDL_context::release_transactional_locks()
{
DBUG_ENTER("MDL_context::release_transactional_locks");
- release_locks_stored_before(m_lt_or_ha_sentinel);
+ release_locks_stored_before(m_trans_sentinel);
DBUG_VOID_RETURN;
}
@@ -2331,20 +2170,21 @@ void MDL_context::release_transactional_locks()
Does this savepoint have this lock?
@retval TRUE The ticket is older than the savepoint and
- is not LT or HA ticket. Thus it belongs to
- the savepoint.
+ is not LT, HA or GLR ticket. Thus it belongs
+ to the savepoint.
@retval FALSE The ticket is newer than the savepoint
- or is an LT or HA ticket.
+ or is an LT, HA or GLR ticket.
*/
bool MDL_context::has_lock(MDL_ticket *mdl_savepoint,
MDL_ticket *mdl_ticket)
{
MDL_ticket *ticket;
+ /* Start from the beginning, most likely mdl_ticket's been just acquired. */
MDL_context::Ticket_iterator it(m_tickets);
bool found_savepoint= FALSE;
- while ((ticket= it++) && ticket != m_lt_or_ha_sentinel)
+ while ((ticket= it++) && ticket != m_trans_sentinel)
{
/*
First met the savepoint. The ticket must be
@@ -2359,28 +2199,28 @@ bool MDL_context::has_lock(MDL_ticket *mdl_savepoint,
if (ticket == mdl_ticket)
return found_savepoint;
}
- /* Reached m_lt_or_ha_sentinel. The ticket must be an LT or HA ticket. */
+ /* Reached m_trans_sentinel. The ticket must be LT, HA or GRL ticket. */
return FALSE;
}
/**
Rearrange the ticket to reside in the part of the list that's
- beyond m_lt_or_ha_sentinel. This effectively changes the ticket
+ beyond m_trans_sentinel. This effectively changes the ticket
life cycle, from automatic to manual: i.e. the ticket is no
longer released by MDL_context::release_transactional_locks() or
MDL_context::rollback_to_savepoint(), it must be released manually.
*/
-void MDL_context::move_ticket_after_lt_or_ha_sentinel(MDL_ticket *mdl_ticket)
+void MDL_context::move_ticket_after_trans_sentinel(MDL_ticket *mdl_ticket)
{
m_tickets.remove(mdl_ticket);
- if (m_lt_or_ha_sentinel == NULL)
+ if (m_trans_sentinel == NULL)
{
- m_lt_or_ha_sentinel= mdl_ticket;
+ m_trans_sentinel= mdl_ticket;
/* sic: linear from the number of transactional tickets acquired so-far! */
m_tickets.push_back(mdl_ticket);
}
else
- m_tickets.insert_after(m_lt_or_ha_sentinel, mdl_ticket);
+ m_tickets.insert_after(m_trans_sentinel, mdl_ticket);
}