2009-11-30 16:55:03 +01:00
|
|
|
/* Copyright (C) 2007-2008 MySQL AB
|
|
|
|
|
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
|
|
#include "mdl.h"
|
2009-12-04 00:29:40 +01:00
|
|
|
#include "debug_sync.h"
|
2009-12-02 17:31:57 +01:00
|
|
|
#include <hash.h>
|
|
|
|
#include <mysqld_error.h>
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
static bool mdl_initialized= 0;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
The lock context. Created internally for an acquired lock.
|
2009-12-04 00:52:05 +01:00
|
|
|
For a given name, there exists only one MDL_lock instance,
|
2009-12-04 00:34:19 +01:00
|
|
|
and it exists only when the lock has been granted.
|
|
|
|
Can be seen as an MDL subsystem's version of TABLE_SHARE.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
class MDL_lock
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
public:
|
|
|
|
typedef I_P_List<MDL_ticket,
|
|
|
|
I_P_List_adapter<MDL_ticket,
|
|
|
|
&MDL_ticket::next_in_lock,
|
|
|
|
&MDL_ticket::prev_in_lock> >
|
2009-12-04 00:29:40 +01:00
|
|
|
Ticket_list;
|
|
|
|
|
|
|
|
typedef Ticket_list::Iterator Ticket_iterator;
|
|
|
|
|
|
|
|
/** The type of lock (shared or exclusive). */
|
|
|
|
enum
|
|
|
|
{
|
2009-12-04 00:55:26 +01:00
|
|
|
MDL_LOCK_SHARED,
|
|
|
|
MDL_LOCK_EXCLUSIVE,
|
2009-12-04 00:29:40 +01:00
|
|
|
} type;
|
|
|
|
/** The key of the object (data) being protected. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_key key;
|
2009-12-04 00:29:40 +01:00
|
|
|
/** List of granted tickets for this lock. */
|
|
|
|
Ticket_list granted;
|
2009-12-04 00:34:19 +01:00
|
|
|
/**
|
2009-11-30 16:55:03 +01:00
|
|
|
There can be several upgraders and active exclusive
|
2009-12-04 00:52:05 +01:00
|
|
|
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.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
2009-12-04 00:29:40 +01:00
|
|
|
Ticket_list waiting;
|
2009-11-30 16:55:03 +01:00
|
|
|
void *cached_object;
|
|
|
|
mdl_cached_object_release_hook cached_object_release_hook;
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool is_empty() const
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
return (granted.is_empty() && waiting.is_empty());
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool can_grant_lock(const MDL_context *requestor_ctx,
|
|
|
|
enum_mdl_type type, bool is_upgrade);
|
|
|
|
|
|
|
|
inline static MDL_lock *create(const MDL_key *key);
|
|
|
|
inline static void destroy(MDL_lock *lock);
|
|
|
|
private:
|
|
|
|
MDL_lock(const MDL_key *key_arg)
|
2009-12-04 00:55:26 +01:00
|
|
|
: type(MDL_LOCK_SHARED),
|
2009-12-04 00:52:05 +01:00
|
|
|
key(key_arg),
|
|
|
|
cached_object(NULL),
|
|
|
|
cached_object_release_hook(NULL)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
static pthread_mutex_t LOCK_mdl;
|
|
|
|
static pthread_cond_t COND_mdl;
|
|
|
|
static HASH mdl_locks;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-11-30 17:44:46 +01:00
|
|
|
/**
|
2009-12-04 00:52:05 +01:00
|
|
|
An implementation of the global metadata lock. The only
|
|
|
|
locking modes which are supported at the moment are SHARED and
|
|
|
|
INTENTION EXCLUSIVE. Note, that SHARED global metadata lock
|
|
|
|
is acquired automatically when one tries to acquire an EXCLUSIVE
|
|
|
|
or UPGRADABLE SHARED metadata lock on an individual object.
|
2009-11-30 17:44:46 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
class MDL_global_lock
|
2009-11-30 17:44:46 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
public:
|
2009-11-30 23:33:22 +01:00
|
|
|
uint waiting_shared;
|
|
|
|
uint active_shared;
|
|
|
|
uint active_intention_exclusive;
|
2009-12-04 00:52:05 +01:00
|
|
|
|
|
|
|
bool is_empty() const
|
|
|
|
{
|
|
|
|
return (waiting_shared == 0 && active_shared == 0 &&
|
|
|
|
active_intention_exclusive == 0);
|
|
|
|
}
|
|
|
|
bool is_lock_type_compatible(enum_mdl_type type, bool is_upgrade) const;
|
|
|
|
};
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
static MDL_global_lock global_lock;
|
|
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
static uchar *
|
2009-12-04 00:29:40 +01:00
|
|
|
mdl_locks_key(const uchar *record, size_t *length,
|
|
|
|
my_bool not_used __attribute__((unused)))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock *lock=(MDL_lock*) record;
|
|
|
|
*length= lock->key.length();
|
|
|
|
return (uchar*) lock->key.ptr();
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
} /* extern "C" */
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Initialize the metadata locking subsystem.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This function is called at server startup.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
In particular, initializes the new global mutex and
|
|
|
|
the associated condition variable: LOCK_mdl and COND_mdl.
|
|
|
|
These locking primitives are implementation details of the MDL
|
|
|
|
subsystem and are private to it.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Note, that even though the new implementation adds acquisition
|
|
|
|
of a new global mutex to the execution flow of almost every SQL
|
|
|
|
statement, the design capitalizes on that to later save on
|
|
|
|
look ups in the table definition cache. This leads to reduced
|
|
|
|
contention overall and on LOCK_open in particular.
|
2009-12-04 00:52:05 +01:00
|
|
|
Please see the description of MDL_context::acquire_shared_lock()
|
|
|
|
for details.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
void mdl_init()
|
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(! mdl_initialized);
|
|
|
|
mdl_initialized= TRUE;
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_init(&LOCK_mdl, NULL);
|
|
|
|
pthread_cond_init(&COND_mdl, NULL);
|
|
|
|
my_hash_init(&mdl_locks, &my_charset_bin, 16 /* FIXME */, 0, 0,
|
|
|
|
mdl_locks_key, 0, 0);
|
2009-12-04 00:52:05 +01:00
|
|
|
/* The global lock is zero-initialized by the loader. */
|
|
|
|
DBUG_ASSERT(global_lock.is_empty());
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Release resources of metadata locking subsystem.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Destroys the global mutex and the condition variable.
|
|
|
|
Called at server shutdown.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
void mdl_destroy()
|
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
if (mdl_initialized)
|
|
|
|
{
|
|
|
|
mdl_initialized= FALSE;
|
|
|
|
DBUG_ASSERT(!mdl_locks.records);
|
|
|
|
DBUG_ASSERT(global_lock.is_empty());
|
|
|
|
pthread_mutex_destroy(&LOCK_mdl);
|
|
|
|
pthread_cond_destroy(&COND_mdl);
|
|
|
|
my_hash_free(&mdl_locks);
|
|
|
|
}
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Initialize a metadata locking context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This is to be called when a new server connection is created.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::init(THD *thd_arg)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
m_has_global_shared_lock= FALSE;
|
|
|
|
m_thd= thd_arg;
|
|
|
|
/*
|
|
|
|
FIXME: In reset_n_backup_open_tables_state,
|
|
|
|
we abuse "init" as a reset, i.e. call it on an already
|
|
|
|
constructed non-empty object. This is why we can't
|
|
|
|
rely here on the default constructors of I_P_List
|
|
|
|
to empty the list.
|
|
|
|
*/
|
|
|
|
m_requests.empty();
|
|
|
|
m_tickets.empty();
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Destroy metadata locking context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Assumes and asserts that there are no active or pending locks
|
|
|
|
associated with this context at the time of the destruction.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Currently does nothing. Asserts that there are no pending
|
|
|
|
or satisfied lock requests. The pending locks must be released
|
|
|
|
prior to destruction. This is a new way to express the assertion
|
|
|
|
that all tables are closed before a connection is destroyed.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::destroy()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(m_requests.is_empty());
|
|
|
|
DBUG_ASSERT(m_tickets.is_empty());
|
|
|
|
DBUG_ASSERT(! m_has_global_shared_lock);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Backup and reset state of meta-data locking context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
mdl_context_backup_and_reset(), mdl_context_restore() and
|
|
|
|
mdl_context_merge() are used by HANDLER implementation which
|
|
|
|
needs to open table for new HANDLER independently of already
|
|
|
|
open HANDLERs and add this table/metadata lock to the set of
|
|
|
|
tables open/metadata locks for HANDLERs afterwards.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::backup_and_reset(MDL_context *backup)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(backup->m_requests.is_empty());
|
|
|
|
DBUG_ASSERT(backup->m_tickets.is_empty());
|
|
|
|
|
|
|
|
m_requests.swap(backup->m_requests);
|
|
|
|
m_tickets.swap(backup->m_tickets);
|
|
|
|
|
|
|
|
backup->m_has_global_shared_lock= m_has_global_shared_lock;
|
|
|
|
/*
|
|
|
|
When the main context is swapped out, one can not take
|
|
|
|
the global shared lock, and one can not rely on it:
|
|
|
|
the functionality in this mode is reduced, since it exists as
|
|
|
|
a temporary hack to support ad-hoc opening of system tables.
|
|
|
|
*/
|
|
|
|
m_has_global_shared_lock= FALSE;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Restore state of meta-data locking context from backup.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::restore_from_backup(MDL_context *backup)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(m_requests.is_empty());
|
|
|
|
DBUG_ASSERT(m_tickets.is_empty());
|
|
|
|
DBUG_ASSERT(m_has_global_shared_lock == FALSE);
|
|
|
|
|
|
|
|
m_requests.swap(backup->m_requests);
|
|
|
|
m_tickets.swap(backup->m_tickets);
|
|
|
|
m_has_global_shared_lock= backup->m_has_global_shared_lock;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Merge meta-data locks from one context into another.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::merge(MDL_context *src)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *ticket;
|
|
|
|
MDL_request *mdl_request;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(m_thd == src->m_thd);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!src->m_requests.is_empty())
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
Request_iterator it(src->m_requests);
|
|
|
|
while ((mdl_request= it++))
|
|
|
|
m_requests.push_front(mdl_request);
|
|
|
|
src->m_requests.empty();
|
2009-12-04 00:29:40 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!src->m_tickets.is_empty())
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
Ticket_iterator it(src->m_tickets);
|
2009-12-04 00:29:40 +01:00
|
|
|
while ((ticket= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(ticket->m_ctx);
|
|
|
|
ticket->m_ctx= this;
|
|
|
|
m_tickets.push_front(ticket);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
src->m_tickets.empty();
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
/*
|
|
|
|
MDL_context::merge() is a hack used in one place only: to open
|
|
|
|
an SQL handler. We never acquire the global shared lock there.
|
|
|
|
*/
|
|
|
|
DBUG_ASSERT(! src->m_has_global_shared_lock);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Initialize a lock request.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This is to be used for every lock request.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Note that initialization and allocation are split into two
|
|
|
|
calls. This is to allow flexible memory management of lock
|
|
|
|
requests. Normally a lock request is stored in statement memory
|
|
|
|
(e.g. is a member of struct TABLE_LIST), but we would also like
|
|
|
|
to allow allocation of lock requests in other memory roots,
|
|
|
|
for example in the grant subsystem, to lock privilege tables.
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
The MDL subsystem does not own or manage memory of lock requests.
|
|
|
|
Instead it assumes that the life time of every lock request (including
|
2009-12-04 00:52:05 +01:00
|
|
|
encompassed members db/name) encloses calls to MDL_context::add_request()
|
|
|
|
and MDL_context::remove_request() or MDL_context::remove_all_requests().
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param type Id of type of object to be locked
|
|
|
|
@param db Name of database to which the object belongs
|
|
|
|
@param name Name of of the object
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
The initialized lock request will have MDL_SHARED type.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Suggested lock types: TABLE - 0 PROCEDURE - 1 FUNCTION - 2
|
|
|
|
Note that tables and views must have the same lock type, since
|
|
|
|
they share the same name space in the SQL standard.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_request::init(unsigned char type_arg,
|
|
|
|
const char *db_arg,
|
|
|
|
const char *name_arg)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
key.mdl_key_init(type_arg, db_arg, name_arg);
|
|
|
|
type= MDL_SHARED;
|
|
|
|
ticket= NULL;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Allocate and initialize one lock request.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Same as mdl_init_lock(), but allocates the lock and the key buffer
|
|
|
|
on a memory root. Necessary to lock ad-hoc tables, e.g.
|
|
|
|
mysql.* tables of grant and data dictionary subsystems.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param type Id of type of object to be locked
|
|
|
|
@param db Name of database to which object belongs
|
|
|
|
@param name Name of of object
|
|
|
|
@param root MEM_ROOT on which object should be allocated
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@note The allocated lock request will have MDL_SHARED type.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval 0 Error if out of memory
|
|
|
|
@retval non-0 Pointer to an object representing a lock request
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_request *
|
|
|
|
MDL_request::create(unsigned char type, const char *db,
|
|
|
|
const char *name, MEM_ROOT *root)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_request *mdl_request;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!(mdl_request= (MDL_request*) alloc_root(root, sizeof(MDL_request))))
|
2009-11-30 16:55:03 +01:00
|
|
|
return NULL;
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
mdl_request->init(type, db, name);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
return mdl_request;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Add a lock request to the list of lock requests of the context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
The procedure to acquire metadata locks is:
|
2009-12-04 00:52:05 +01:00
|
|
|
- allocate and initialize lock requests
|
|
|
|
(MDL_request::create())
|
|
|
|
- associate them with a context (MDL_context::add_request())
|
|
|
|
- call MDL_context::acquire_shared_lock() and
|
|
|
|
MDL_context::release_lock() (maybe repeatedly).
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Associates a lock request with the given context.
|
2009-12-04 00:52:05 +01:00
|
|
|
There should be no more than one context per connection, to
|
|
|
|
avoid deadlocks.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param mdl_request The lock request to be added.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::add_request(MDL_request *mdl_request)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ENTER("MDL_context::add_request");
|
|
|
|
DBUG_ASSERT(mdl_request->ticket == NULL);
|
|
|
|
m_requests.push_front(mdl_request);
|
2009-11-30 16:55:03 +01:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 17:15:40 +01:00
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Remove a lock request from the list of lock requests.
|
2009-12-02 17:15:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Disassociates a lock request from the given context.
|
2009-12-02 17:15:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param mdl_request The lock request to be removed.
|
2009-12-02 17:15:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@pre The lock request being removed should correspond to a ticket that
|
|
|
|
was released or was not acquired.
|
2009-12-02 17:15:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@note Resets lock request back to its initial state
|
|
|
|
(i.e. sets type to MDL_SHARED).
|
2009-12-02 17:15:40 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::remove_request(MDL_request *mdl_request)
|
2009-12-02 17:15:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ENTER("MDL_context::remove_request");
|
2009-12-02 17:15:40 +01:00
|
|
|
/* Reset lock request back to its initial state. */
|
2009-12-04 00:52:05 +01:00
|
|
|
mdl_request->type= MDL_SHARED;
|
|
|
|
mdl_request->ticket= NULL;
|
|
|
|
m_requests.remove(mdl_request);
|
2009-12-02 17:15:40 +01:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Clear all lock requests in the context.
|
|
|
|
Disassociates lock requests from the context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Also resets lock requests back to their initial state (i.e. MDL_SHARED).
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::remove_all_requests()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_request *mdl_request;
|
|
|
|
Request_iterator it(m_requests);
|
|
|
|
while ((mdl_request= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
/* Reset lock request back to its initial state. */
|
2009-12-04 00:52:05 +01:00
|
|
|
mdl_request->type= MDL_SHARED;
|
|
|
|
mdl_request->ticket= NULL;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
m_requests.empty();
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:52:05 +01:00
|
|
|
Auxiliary functions needed for creation/destruction of MDL_lock objects.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@todo This naive implementation should be replaced with one that saves
|
|
|
|
on memory allocation by reusing released objects.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
inline MDL_lock *MDL_lock::create(const MDL_key *mdl_key)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
return new MDL_lock(mdl_key);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_lock::destroy(MDL_lock *lock)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
delete lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
/**
|
2009-12-04 00:52:05 +01:00
|
|
|
Auxiliary functions needed for creation/destruction of MDL_ticket
|
2009-12-04 00:34:19 +01:00
|
|
|
objects.
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@todo This naive implementation should be replaced with one that saves
|
|
|
|
on memory allocation by reusing released objects.
|
2009-12-04 00:29:40 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *MDL_ticket::create(MDL_context *ctx_arg, enum_mdl_type type_arg)
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
return new MDL_ticket(ctx_arg, type_arg);
|
2009-12-04 00:29:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_ticket::destroy(MDL_ticket *ticket)
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
|
|
|
delete ticket;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 17:31:57 +01:00
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Helper functions and macros to be used for killable waiting in metadata
|
|
|
|
locking subsystem.
|
2009-12-02 17:31:57 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@sa THD::enter_cond()/exit_cond()/killed.
|
2009-12-02 17:31:57 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@note We can't use THD::enter_cond()/exit_cond()/killed directly here
|
Backport of revno ## 2617.31.1, 2617.31.3, 2617.31.4, 2617.31.5,
2617.31.12, 2617.31.15, 2617.31.15, 2617.31.16, 2617.43.1
- initial changeset that introduced the fix for
Bug#989 and follow up fixes for all test suite failures
introduced in the initial changeset.
------------------------------------------------------------
revno: 2617.31.1
committer: Davi Arnaut <Davi.Arnaut@Sun.COM>
branch nick: 4284-6.0
timestamp: Fri 2009-03-06 19:17:00 -0300
message:
Bug#989: If DROP TABLE while there's an active transaction, wrong binlog order
WL#4284: Transactional DDL locking
Currently the MySQL server does not keep metadata locks on
schema objects for the duration of a transaction, thus failing
to guarantee the integrity of the schema objects being used
during the transaction and to protect then from concurrent
DDL operations. This also poses a problem for replication as
a DDL operation might be replicated even thought there are
active transactions using the object being modified.
The solution is to defer the release of metadata locks until
a active transaction is either committed or rolled back. This
prevents other statements from modifying the table for the
entire duration of the transaction. This provides commitment
ordering for guaranteeing serializability across multiple
transactions.
- Incompatible change:
If MySQL's metadata locking system encounters a lock conflict,
the usual schema is to use the try and back-off technique to
avoid deadlocks -- this schema consists in releasing all locks
and trying to acquire them all in one go.
But in a transactional context this algorithm can't be utilized
as its not possible to release locks acquired during the course
of the transaction without breaking the transaction commitments.
To avoid deadlocks in this case, the ER_LOCK_DEADLOCK will be
returned if a lock conflict is encountered during a transaction.
Let's consider an example:
A transaction has two statements that modify table t1, then table
t2, and then commits. The first statement of the transaction will
acquire a shared metadata lock on table t1, and it will be kept
utill COMMIT to ensure serializability.
At the moment when the second statement attempts to acquire a
shared metadata lock on t2, a concurrent ALTER or DROP statement
might have locked t2 exclusively. The prescription of the current
locking protocol is that the acquirer of the shared lock backs off
-- gives up all his current locks and retries. This implies that
the entire multi-statement transaction has to be rolled back.
- Incompatible change:
FLUSH commands such as FLUSH PRIVILEGES and FLUSH TABLES WITH READ
LOCK won't cause locked tables to be implicitly unlocked anymore.
2009-12-05 00:02:48 +01:00
|
|
|
since this will make metadata subsystem dependent on THD class
|
2009-12-04 00:34:19 +01:00
|
|
|
and thus prevent us from writing unit tests for it. And usage of
|
|
|
|
wrapper functions to access THD::killed/enter_cond()/exit_cond()
|
|
|
|
will probably introduce too much overhead.
|
2009-12-02 17:31:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define MDL_ENTER_COND(A, B) mdl_enter_cond(A, B, __func__, __FILE__, __LINE__)
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
static inline const char *mdl_enter_cond(THD *thd,
|
2009-12-02 17:31:57 +01:00
|
|
|
st_my_thread_var *mysys_var,
|
|
|
|
const char *calling_func,
|
|
|
|
const char *calling_file,
|
|
|
|
const unsigned int calling_line)
|
|
|
|
{
|
|
|
|
safe_mutex_assert_owner(&LOCK_mdl);
|
|
|
|
|
|
|
|
mysys_var->current_mutex= &LOCK_mdl;
|
|
|
|
mysys_var->current_cond= &COND_mdl;
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DEBUG_SYNC(thd, "mdl_enter_cond");
|
|
|
|
|
|
|
|
return set_thd_proc_info(thd, "Waiting for table",
|
2009-12-02 17:31:57 +01:00
|
|
|
calling_func, calling_file, calling_line);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MDL_EXIT_COND(A, B, C) mdl_exit_cond(A, B, C, __func__, __FILE__, __LINE__)
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
static inline void mdl_exit_cond(THD *thd,
|
2009-12-02 17:31:57 +01:00
|
|
|
st_my_thread_var *mysys_var,
|
|
|
|
const char* old_msg,
|
|
|
|
const char *calling_func,
|
|
|
|
const char *calling_file,
|
|
|
|
const unsigned int calling_line)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(&LOCK_mdl == mysys_var->current_mutex);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
pthread_mutex_lock(&mysys_var->mutex);
|
|
|
|
mysys_var->current_mutex= 0;
|
|
|
|
mysys_var->current_cond= 0;
|
|
|
|
pthread_mutex_unlock(&mysys_var->mutex);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DEBUG_SYNC(thd, "mdl_exit_cond");
|
|
|
|
|
|
|
|
(void) set_thd_proc_info(thd, old_msg, calling_func,
|
2009-12-02 17:31:57 +01:00
|
|
|
calling_file, calling_line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-01 14:55:45 +01:00
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Check if request for the lock on particular object can be satisfied given
|
|
|
|
current state of the global metadata lock.
|
|
|
|
|
|
|
|
@note In other words, we're trying to check that the individual lock
|
|
|
|
request, implying a form of lock on the global metadata, is
|
|
|
|
compatible with the current state of the global metadata lock.
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param mdl_request Request for lock on an individual object, implying a
|
|
|
|
certain kind of global metadata lock.
|
2009-12-04 00:34:19 +01:00
|
|
|
|
|
|
|
@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 + + +
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
(*) 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.
|
2009-12-01 14:55:45 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_global_lock::is_lock_type_compatible(enum_mdl_type type,
|
|
|
|
bool is_upgrade) const
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
2009-12-04 00:29:40 +01:00
|
|
|
switch (type)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
|
|
|
case MDL_SHARED:
|
2009-12-01 14:59:11 +01:00
|
|
|
case MDL_SHARED_HIGH_PRIO:
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
case MDL_SHARED_UPGRADABLE:
|
2009-12-04 00:52:05 +01:00
|
|
|
if (active_shared || waiting_shared)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
case MDL_EXCLUSIVE:
|
2009-12-04 00:29:40 +01:00
|
|
|
if (is_upgrade)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
We are upgrading MDL_SHARED to MDL_EXCLUSIVE.
|
|
|
|
|
|
|
|
There should be no conflicting global locks since for each upgradable
|
|
|
|
shared lock we obtain intention exclusive global lock first.
|
|
|
|
*/
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(active_shared == 0 && active_intention_exclusive);
|
2009-12-01 14:55:45 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
if (active_shared || waiting_shared)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
We are going to obtain intention exclusive global lock and
|
|
|
|
there is active or pending shared global lock.
|
|
|
|
*/
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(0);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Check if request for the lock can be satisfied given current state of lock.
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param lock Lock.
|
|
|
|
@param mdl_request Request for lock.
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval TRUE Lock request can be satisfied
|
|
|
|
@retval FALSE There is some conflicting lock.
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This function defines the following compatibility matrix for metadata locks:
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
| Satisfied or pending requests which we have in MDL_lock
|
2009-12-04 00:34:19 +01:00
|
|
|
----------------+---------------------------------------------------------
|
|
|
|
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
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
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
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
(*) 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.
|
2009-12-01 14:55:45 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_lock::can_grant_lock(const MDL_context *requestor_ctx, enum_mdl_type type_arg,
|
|
|
|
bool is_upgrade)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
2009-12-04 00:29:40 +01:00
|
|
|
bool can_grant= FALSE;
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
switch (type_arg) {
|
2009-12-01 14:55:45 +01:00
|
|
|
case MDL_SHARED:
|
2009-12-01 14:59:11 +01:00
|
|
|
case MDL_SHARED_UPGRADABLE:
|
|
|
|
case MDL_SHARED_HIGH_PRIO:
|
2009-12-04 00:55:26 +01:00
|
|
|
if (type == MDL_lock::MDL_LOCK_SHARED)
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
|
|
|
/* Pending exclusive locks have higher priority over shared locks. */
|
2009-12-04 00:52:05 +01:00
|
|
|
if (waiting.is_empty() || type_arg == MDL_SHARED_HIGH_PRIO)
|
2009-12-04 00:29:40 +01:00
|
|
|
can_grant= TRUE;
|
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
else if (granted.head()->get_ctx() == requestor_ctx)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
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 ....
|
|
|
|
*/
|
2009-12-04 00:29:40 +01:00
|
|
|
can_grant= TRUE;
|
2009-12-01 14:55:45 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MDL_EXCLUSIVE:
|
2009-12-04 00:29:40 +01:00
|
|
|
if (is_upgrade)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
|
|
|
/* We are upgrading MDL_SHARED to MDL_EXCLUSIVE. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *conflicting_ticket;
|
|
|
|
MDL_lock::Ticket_iterator it(granted);
|
2009-12-01 14:55:45 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
There should be no active exclusive locks since we own shared lock
|
|
|
|
on the object.
|
|
|
|
*/
|
2009-12-04 00:55:26 +01:00
|
|
|
DBUG_ASSERT(type == MDL_lock::MDL_LOCK_SHARED);
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((conflicting_ticket= it++))
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2009-12-04 00:52:05 +01:00
|
|
|
if (conflicting_ticket->get_ctx() != requestor_ctx)
|
2009-12-04 00:29:40 +01:00
|
|
|
break;
|
2009-12-01 14:55:45 +01:00
|
|
|
}
|
2009-12-04 00:29:40 +01:00
|
|
|
/* Grant lock if there are no conflicting shared locks. */
|
2009-12-04 00:52:05 +01:00
|
|
|
if (conflicting_ticket == NULL)
|
2009-12-04 00:29:40 +01:00
|
|
|
can_grant= TRUE;
|
|
|
|
break;
|
2009-12-01 14:55:45 +01:00
|
|
|
}
|
2009-12-04 00:55:26 +01:00
|
|
|
else if (type == MDL_lock::MDL_LOCK_SHARED)
|
2009-12-01 14:55:45 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
can_grant= granted.is_empty();
|
2009-12-01 14:55:45 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(0);
|
|
|
|
}
|
2009-12-04 00:29:40 +01:00
|
|
|
return can_grant;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Check whether the context already holds a compatible lock ticket
|
|
|
|
on a object. Only shared locks can be recursive.
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param mdl_request Lock request object for lock to be acquired
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@return A pointer to the lock ticket for the object or NULL otherwise.
|
2009-12-04 00:29:40 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *
|
|
|
|
MDL_context::find_ticket(MDL_request *mdl_request)
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *ticket;
|
|
|
|
Ticket_iterator it(m_tickets);
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(mdl_request->is_shared());
|
2009-12-04 00:29:40 +01:00
|
|
|
|
|
|
|
while ((ticket= it++))
|
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
if (mdl_request->type == ticket->m_type &&
|
|
|
|
mdl_request->key.is_equal(&ticket->m_lock->key))
|
2009-12-04 00:29:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticket;
|
2009-12-01 14:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Try to acquire one shared lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Unlike exclusive locks, shared locks are acquired one by
|
|
|
|
one. This is interface is chosen to simplify introduction of
|
2009-12-04 00:52:05 +01:00
|
|
|
the new locking API to the system. MDL_context::acquire_shared_lock()
|
2009-12-04 00:34:19 +01:00
|
|
|
is currently used from open_table(), and there we have only one
|
|
|
|
table to work with.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
In future we may consider allocating multiple shared locks at once.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This function must be called after the lock is added to a context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param mdl_request [in] Lock request object for lock to be acquired
|
2009-12-04 00:34:19 +01:00
|
|
|
@param retry [out] Indicates that conflicting lock exists and another
|
|
|
|
attempt should be made after releasing all current
|
|
|
|
locks and waiting for conflicting lock go away
|
2009-12-04 00:52:05 +01:00
|
|
|
(using MDL_context::wait_for_locks()).
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval FALSE Success.
|
2009-12-04 00:52:05 +01:00
|
|
|
@retval TRUE Failure. Either error occurred or conflicting lock exists.
|
2009-12-04 00:34:19 +01:00
|
|
|
In the latter case "retry" parameter is set to TRUE.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_context::acquire_shared_lock(MDL_request *mdl_request, bool *retry)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock *lock;
|
|
|
|
MDL_key *key= &mdl_request->key;
|
|
|
|
MDL_ticket *ticket;
|
2009-11-30 16:55:03 +01:00
|
|
|
*retry= FALSE;
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(mdl_request->is_shared() && mdl_request->ticket == NULL);
|
2009-12-01 16:20:43 +01:00
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (m_has_global_shared_lock &&
|
|
|
|
mdl_request->type == MDL_SHARED_UPGRADABLE)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
my_error(ER_CANT_UPDATE_WITH_READLOCK, MYF(0));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
/*
|
|
|
|
Check whether the context already holds a shared lock on the object,
|
|
|
|
and if so, grant the request.
|
|
|
|
*/
|
2009-12-04 00:52:05 +01:00
|
|
|
if ((ticket= find_ticket(mdl_request)))
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(ticket->m_state == MDL_ACQUIRED);
|
|
|
|
mdl_request->ticket= ticket;
|
2009-12-04 00:29:40 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!global_lock.is_lock_type_compatible(mdl_request->type, FALSE))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
*retry= TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!(ticket= MDL_ticket::create(this, mdl_request->type)))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:29:40 +01:00
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!(lock= (MDL_lock*) my_hash_search(&mdl_locks,
|
2009-12-04 00:29:40 +01:00
|
|
|
key->ptr(), key->length())))
|
|
|
|
{
|
2009-12-04 00:55:26 +01:00
|
|
|
/* Default lock type is MDL_lock::MDL_LOCK_SHARED */
|
2009-12-04 00:52:05 +01:00
|
|
|
lock= MDL_lock::create(key);
|
2009-12-04 00:29:40 +01:00
|
|
|
if (!lock || my_hash_insert(&mdl_locks, (uchar*)lock))
|
2009-12-02 17:15:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock::destroy(lock);
|
|
|
|
MDL_ticket::destroy(ticket);
|
2009-12-02 17:15:40 +01:00
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-12-04 00:29:40 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (lock->can_grant_lock(this, mdl_request->type, FALSE))
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
mdl_request->ticket= ticket;
|
2009-12-04 00:29:40 +01:00
|
|
|
lock->granted.push_front(ticket);
|
2009-12-04 00:52:05 +01:00
|
|
|
m_tickets.push_front(ticket);
|
|
|
|
ticket->m_state= MDL_ACQUIRED;
|
|
|
|
ticket->m_lock= lock;
|
|
|
|
if (mdl_request->type == MDL_SHARED_UPGRADABLE)
|
2009-11-30 23:33:22 +01:00
|
|
|
global_lock.active_intention_exclusive++;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-04 00:29:40 +01:00
|
|
|
/* We can't get here if we allocated a new lock. */
|
|
|
|
DBUG_ASSERT(! lock->is_empty());
|
|
|
|
*retry= TRUE;
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket::destroy(ticket);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
|
|
|
|
return *retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
/**
|
2009-12-04 00:52:05 +01:00
|
|
|
Notify a thread holding a shared metadata lock which
|
|
|
|
conflicts with a pending exclusive lock.
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param thd Current thread context
|
2009-12-04 00:52:05 +01:00
|
|
|
@param conflicting_ticket Conflicting metadata lock
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval TRUE A thread was woken up
|
|
|
|
@retval FALSE Lock is not a shared one or no thread was woken up
|
2009-12-04 00:29:40 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
static bool notify_shared_lock(THD *thd, MDL_ticket *conflicting_ticket)
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
|
|
|
bool woke= FALSE;
|
2009-12-04 00:52:05 +01:00
|
|
|
if (conflicting_ticket->is_shared())
|
|
|
|
{
|
|
|
|
THD *conflicting_thd= conflicting_ticket->get_ctx()->get_thd();
|
Backport of revno ## 2617.31.1, 2617.31.3, 2617.31.4, 2617.31.5,
2617.31.12, 2617.31.15, 2617.31.15, 2617.31.16, 2617.43.1
- initial changeset that introduced the fix for
Bug#989 and follow up fixes for all test suite failures
introduced in the initial changeset.
------------------------------------------------------------
revno: 2617.31.1
committer: Davi Arnaut <Davi.Arnaut@Sun.COM>
branch nick: 4284-6.0
timestamp: Fri 2009-03-06 19:17:00 -0300
message:
Bug#989: If DROP TABLE while there's an active transaction, wrong binlog order
WL#4284: Transactional DDL locking
Currently the MySQL server does not keep metadata locks on
schema objects for the duration of a transaction, thus failing
to guarantee the integrity of the schema objects being used
during the transaction and to protect then from concurrent
DDL operations. This also poses a problem for replication as
a DDL operation might be replicated even thought there are
active transactions using the object being modified.
The solution is to defer the release of metadata locks until
a active transaction is either committed or rolled back. This
prevents other statements from modifying the table for the
entire duration of the transaction. This provides commitment
ordering for guaranteeing serializability across multiple
transactions.
- Incompatible change:
If MySQL's metadata locking system encounters a lock conflict,
the usual schema is to use the try and back-off technique to
avoid deadlocks -- this schema consists in releasing all locks
and trying to acquire them all in one go.
But in a transactional context this algorithm can't be utilized
as its not possible to release locks acquired during the course
of the transaction without breaking the transaction commitments.
To avoid deadlocks in this case, the ER_LOCK_DEADLOCK will be
returned if a lock conflict is encountered during a transaction.
Let's consider an example:
A transaction has two statements that modify table t1, then table
t2, and then commits. The first statement of the transaction will
acquire a shared metadata lock on table t1, and it will be kept
utill COMMIT to ensure serializability.
At the moment when the second statement attempts to acquire a
shared metadata lock on t2, a concurrent ALTER or DROP statement
might have locked t2 exclusively. The prescription of the current
locking protocol is that the acquirer of the shared lock backs off
-- gives up all his current locks and retries. This implies that
the entire multi-statement transaction has to be rolled back.
- Incompatible change:
FLUSH commands such as FLUSH PRIVILEGES and FLUSH TABLES WITH READ
LOCK won't cause locked tables to be implicitly unlocked anymore.
2009-12-05 00:02:48 +01:00
|
|
|
DBUG_ASSERT(thd != conflicting_thd); /* Self-deadlock */
|
2009-12-04 00:52:05 +01:00
|
|
|
woke= mysql_notify_thread_having_shared_lock(thd, conflicting_thd);
|
|
|
|
}
|
2009-12-04 00:29:40 +01:00
|
|
|
return woke;
|
|
|
|
}
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Acquire exclusive locks. The context must contain the list of
|
|
|
|
locks to be acquired. There must be no granted locks in the
|
|
|
|
context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This is a replacement of lock_table_names(). It is used in
|
|
|
|
RENAME, DROP and other DDL SQL statements.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@note The MDL context may not have non-exclusive lock requests
|
|
|
|
or acquired locks.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval FALSE Success
|
|
|
|
@retval TRUE Failure
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool MDL_context::acquire_exclusive_locks()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock *lock;
|
2009-11-30 16:55:03 +01:00
|
|
|
bool signalled= FALSE;
|
|
|
|
const char *old_msg;
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_request *mdl_request;
|
|
|
|
MDL_ticket *ticket;
|
2009-12-02 17:31:57 +01:00
|
|
|
st_my_thread_var *mysys_var= my_thread_var;
|
2009-12-04 00:52:05 +01:00
|
|
|
Request_iterator it(m_requests);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (m_has_global_shared_lock)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
my_error(ER_CANT_UPDATE_WITH_READLOCK, MYF(0));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
old_msg= MDL_ENTER_COND(m_thd, mysys_var);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((mdl_request= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_key *key= &mdl_request->key;
|
|
|
|
DBUG_ASSERT(mdl_request->type == MDL_EXCLUSIVE &&
|
|
|
|
mdl_request->ticket == NULL);
|
2009-12-04 00:29:40 +01:00
|
|
|
|
|
|
|
/* Early allocation: ticket is used as a shortcut to the lock. */
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!(ticket= MDL_ticket::create(this, mdl_request->type)))
|
2009-12-04 00:29:40 +01:00
|
|
|
goto err;
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!(lock= (MDL_lock*) my_hash_search(&mdl_locks,
|
2009-12-04 00:29:40 +01:00
|
|
|
key->ptr(), key->length())))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
lock= MDL_lock::create(key);
|
2009-12-04 00:29:40 +01:00
|
|
|
if (!lock || my_hash_insert(&mdl_locks, (uchar*)lock))
|
2009-12-02 17:15:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket::destroy(ticket);
|
|
|
|
MDL_lock::destroy(lock);
|
2009-12-02 17:15:40 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
mdl_request->ticket= ticket;
|
2009-12-04 00:29:40 +01:00
|
|
|
lock->waiting.push_front(ticket);
|
2009-12-04 00:52:05 +01:00
|
|
|
ticket->m_lock= lock;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
it.rewind();
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((mdl_request= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
lock= mdl_request->ticket->m_lock;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!global_lock.is_lock_type_compatible(mdl_request->type, FALSE))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
/*
|
2009-12-04 00:52:05 +01:00
|
|
|
Someone owns or wants to acquire the global shared lock so
|
|
|
|
we have to wait until he goes away.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
signalled= TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
else if (!lock->can_grant_lock(this, mdl_request->type, FALSE))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *conflicting_ticket;
|
|
|
|
MDL_lock::Ticket_iterator it(lock->granted);
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:55:26 +01:00
|
|
|
signalled= (lock->type == MDL_lock::MDL_LOCK_EXCLUSIVE);
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((conflicting_ticket= it++))
|
|
|
|
signalled|= notify_shared_lock(m_thd, conflicting_ticket);
|
2009-12-01 14:55:45 +01:00
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!mdl_request)
|
2009-11-30 16:55:03 +01:00
|
|
|
break;
|
2009-12-04 00:29:40 +01:00
|
|
|
|
|
|
|
/* There is a shared or exclusive lock on the object. */
|
2009-12-04 00:52:05 +01:00
|
|
|
DEBUG_SYNC(m_thd, "mdl_acquire_exclusive_locks_wait");
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
if (signalled)
|
|
|
|
pthread_cond_wait(&COND_mdl, &LOCK_mdl);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2009-12-04 00:52:05 +01:00
|
|
|
Another thread obtained a shared MDL lock on some table but
|
2009-11-30 16:55:03 +01:00
|
|
|
has not yet opened it and/or tried to obtain data lock on
|
|
|
|
it. In this case we need to wait until this happens and try
|
|
|
|
to abort this thread once again.
|
|
|
|
*/
|
|
|
|
struct timespec abstime;
|
|
|
|
set_timespec(abstime, 10);
|
|
|
|
pthread_cond_timedwait(&COND_mdl, &LOCK_mdl, &abstime);
|
|
|
|
}
|
2009-12-02 17:31:57 +01:00
|
|
|
if (mysys_var->abort)
|
2009-12-02 17:15:40 +01:00
|
|
|
goto err;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
it.rewind();
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((mdl_request= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-11-30 23:33:22 +01:00
|
|
|
global_lock.active_intention_exclusive++;
|
2009-12-04 00:52:05 +01:00
|
|
|
ticket= mdl_request->ticket;
|
|
|
|
lock= ticket->m_lock;
|
2009-12-04 00:55:26 +01:00
|
|
|
lock->type= MDL_lock::MDL_LOCK_EXCLUSIVE;
|
2009-12-04 00:29:40 +01:00
|
|
|
lock->waiting.remove(ticket);
|
|
|
|
lock->granted.push_front(ticket);
|
2009-12-04 00:52:05 +01:00
|
|
|
m_tickets.push_front(ticket);
|
|
|
|
ticket->m_state= MDL_ACQUIRED;
|
2009-11-30 23:33:22 +01:00
|
|
|
if (lock->cached_object)
|
|
|
|
(*lock->cached_object_release_hook)(lock->cached_object);
|
|
|
|
lock->cached_object= NULL;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-02 17:31:57 +01:00
|
|
|
/* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
|
2009-11-30 23:33:22 +01:00
|
|
|
return FALSE;
|
2009-12-02 17:15:40 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
/*
|
|
|
|
Remove our pending lock requests from the locks.
|
|
|
|
Ignore those lock requests which were not made MDL_PENDING.
|
|
|
|
*/
|
|
|
|
it.rewind();
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((mdl_request= it++) && mdl_request->ticket)
|
2009-12-02 17:15:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
ticket= mdl_request->ticket;
|
|
|
|
DBUG_ASSERT(ticket->m_state == MDL_PENDING);
|
|
|
|
lock= ticket->m_lock;
|
2009-12-04 00:29:40 +01:00
|
|
|
lock->waiting.remove(ticket);
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket::destroy(ticket);
|
2009-12-04 00:29:40 +01:00
|
|
|
/* Reset lock request back to its initial state. */
|
2009-12-04 00:52:05 +01:00
|
|
|
mdl_request->ticket= NULL;
|
2009-12-04 00:29:40 +01:00
|
|
|
if (lock->is_empty())
|
|
|
|
{
|
|
|
|
my_hash_delete(&mdl_locks, (uchar *)lock);
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock::destroy(lock);
|
2009-12-04 00:29:40 +01:00
|
|
|
}
|
2009-12-02 17:15:40 +01:00
|
|
|
}
|
|
|
|
/* May be some pending requests for shared locks can be satisfied now. */
|
|
|
|
pthread_cond_broadcast(&COND_mdl);
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
|
2009-12-02 17:15:40 +01:00
|
|
|
return TRUE;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Upgrade a shared metadata lock to exclusive.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Used in ALTER TABLE, when a copy of the table with the
|
|
|
|
new definition has been constructed.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@note In case of failure to upgrade lock (e.g. because upgrader
|
|
|
|
was killed) leaves lock in its original state (locked in
|
|
|
|
shared mode).
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@note There can be only one upgrader for a lock or we will have deadlock.
|
|
|
|
This invariant is ensured by code outside of metadata subsystem usually
|
|
|
|
by obtaining some sort of exclusive table-level lock (e.g. TL_WRITE,
|
|
|
|
TL_WRITE_ALLOW_READ) before performing upgrade of metadata lock.
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval FALSE Success
|
|
|
|
@retval TRUE Failure (thread was killed)
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_ticket::upgrade_shared_lock_to_exclusive()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
const char *old_msg;
|
2009-12-02 17:31:57 +01:00
|
|
|
st_my_thread_var *mysys_var= my_thread_var;
|
2009-12-04 00:52:05 +01:00
|
|
|
THD *thd= m_ctx->get_thd();
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ENTER("MDL_ticket::upgrade_shared_lock_to_exclusive");
|
|
|
|
DEBUG_SYNC(thd, "mdl_upgrade_shared_lock_to_exclusive");
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
2009-11-30 23:39:13 +01:00
|
|
|
/* Allow this function to be called twice for the same lock request. */
|
2009-12-04 00:52:05 +01:00
|
|
|
if (m_type == MDL_EXCLUSIVE)
|
2009-11-30 23:39:13 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
old_msg= MDL_ENTER_COND(thd, mysys_var);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-11-30 23:39:13 +01:00
|
|
|
/*
|
2009-12-04 00:29:40 +01:00
|
|
|
Since we should have already acquired an intention exclusive
|
|
|
|
global lock this call is only enforcing asserts.
|
2009-11-30 23:39:13 +01:00
|
|
|
*/
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(global_lock.is_lock_type_compatible(MDL_EXCLUSIVE, TRUE));
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
if (m_lock->can_grant_lock(m_ctx, MDL_EXCLUSIVE, TRUE))
|
2009-12-01 14:55:45 +01:00
|
|
|
break;
|
|
|
|
|
2009-11-30 23:39:13 +01:00
|
|
|
bool signalled= FALSE;
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *conflicting_ticket;
|
|
|
|
MDL_lock::Ticket_iterator it(m_lock->granted);
|
2009-11-30 23:39:13 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((conflicting_ticket= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
if (conflicting_ticket->m_ctx != m_ctx)
|
|
|
|
signalled|= notify_shared_lock(thd, conflicting_ticket);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-11-30 23:39:13 +01:00
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
if (signalled)
|
|
|
|
pthread_cond_wait(&COND_mdl, &LOCK_mdl);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2009-12-04 00:52:05 +01:00
|
|
|
Another thread obtained a shared MDL lock on some table but
|
2009-11-30 16:55:03 +01:00
|
|
|
has not yet opened it and/or tried to obtain data lock on
|
|
|
|
it. In this case we need to wait until this happens and try
|
|
|
|
to abort this thread once again.
|
|
|
|
*/
|
|
|
|
struct timespec abstime;
|
|
|
|
set_timespec(abstime, 10);
|
|
|
|
DBUG_PRINT("info", ("Failed to wake-up from table-level lock ... sleeping"));
|
|
|
|
pthread_cond_timedwait(&COND_mdl, &LOCK_mdl, &abstime);
|
|
|
|
}
|
2009-12-02 17:31:57 +01:00
|
|
|
if (mysys_var->abort)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
/* Pending requests for shared locks can be satisfied now. */
|
|
|
|
pthread_cond_broadcast(&COND_mdl);
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_EXIT_COND(thd, mysys_var, old_msg);
|
2009-11-30 16:55:03 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-04 00:55:26 +01:00
|
|
|
m_lock->type= MDL_lock::MDL_LOCK_EXCLUSIVE;
|
2009-12-04 00:29:40 +01:00
|
|
|
/* Set the new type of lock in the ticket. */
|
2009-12-04 00:52:05 +01:00
|
|
|
m_type= MDL_EXCLUSIVE;
|
|
|
|
if (m_lock->cached_object)
|
|
|
|
(*m_lock->cached_object_release_hook)(m_lock->cached_object);
|
|
|
|
m_lock->cached_object= 0;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-02 17:31:57 +01:00
|
|
|
/* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_EXIT_COND(thd, mysys_var, old_msg);
|
2009-11-30 16:55:03 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Try to acquire an exclusive lock on the object if there are
|
|
|
|
no conflicting locks.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Similar to the previous function, but returns
|
|
|
|
immediately without any side effect if encounters a lock
|
|
|
|
conflict. Otherwise takes the lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
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.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param mdl_request [in] The lock request
|
|
|
|
@param conflict [out] Indicates that conflicting lock exists
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval TRUE Failure either conflicting lock exists or some error
|
Backport of revno ## 2617.31.1, 2617.31.3, 2617.31.4, 2617.31.5,
2617.31.12, 2617.31.15, 2617.31.15, 2617.31.16, 2617.43.1
- initial changeset that introduced the fix for
Bug#989 and follow up fixes for all test suite failures
introduced in the initial changeset.
------------------------------------------------------------
revno: 2617.31.1
committer: Davi Arnaut <Davi.Arnaut@Sun.COM>
branch nick: 4284-6.0
timestamp: Fri 2009-03-06 19:17:00 -0300
message:
Bug#989: If DROP TABLE while there's an active transaction, wrong binlog order
WL#4284: Transactional DDL locking
Currently the MySQL server does not keep metadata locks on
schema objects for the duration of a transaction, thus failing
to guarantee the integrity of the schema objects being used
during the transaction and to protect then from concurrent
DDL operations. This also poses a problem for replication as
a DDL operation might be replicated even thought there are
active transactions using the object being modified.
The solution is to defer the release of metadata locks until
a active transaction is either committed or rolled back. This
prevents other statements from modifying the table for the
entire duration of the transaction. This provides commitment
ordering for guaranteeing serializability across multiple
transactions.
- Incompatible change:
If MySQL's metadata locking system encounters a lock conflict,
the usual schema is to use the try and back-off technique to
avoid deadlocks -- this schema consists in releasing all locks
and trying to acquire them all in one go.
But in a transactional context this algorithm can't be utilized
as its not possible to release locks acquired during the course
of the transaction without breaking the transaction commitments.
To avoid deadlocks in this case, the ER_LOCK_DEADLOCK will be
returned if a lock conflict is encountered during a transaction.
Let's consider an example:
A transaction has two statements that modify table t1, then table
t2, and then commits. The first statement of the transaction will
acquire a shared metadata lock on table t1, and it will be kept
utill COMMIT to ensure serializability.
At the moment when the second statement attempts to acquire a
shared metadata lock on t2, a concurrent ALTER or DROP statement
might have locked t2 exclusively. The prescription of the current
locking protocol is that the acquirer of the shared lock backs off
-- gives up all his current locks and retries. This implies that
the entire multi-statement transaction has to be rolled back.
- Incompatible change:
FLUSH commands such as FLUSH PRIVILEGES and FLUSH TABLES WITH READ
LOCK won't cause locked tables to be implicitly unlocked anymore.
2009-12-05 00:02:48 +01:00
|
|
|
occurred (probably OOM).
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval FALSE Success, lock was acquired.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
FIXME: Compared to lock_table_name_if_not_cached()
|
2009-12-04 00:52:05 +01:00
|
|
|
it gives slightly more false negatives.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_context::try_acquire_exclusive_lock(MDL_request *mdl_request,
|
|
|
|
bool *conflict)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock *lock;
|
|
|
|
MDL_ticket *ticket;
|
|
|
|
MDL_key *key= &mdl_request->key;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(mdl_request->type == MDL_EXCLUSIVE &&
|
|
|
|
mdl_request->ticket == NULL);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
2009-12-02 17:15:40 +01:00
|
|
|
*conflict= FALSE;
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!(lock= (MDL_lock*) my_hash_search(&mdl_locks,
|
2009-12-04 00:29:40 +01:00
|
|
|
key->ptr(), key->length())))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
ticket= MDL_ticket::create(this, mdl_request->type);
|
|
|
|
lock= MDL_lock::create(key);
|
2009-12-04 00:29:40 +01:00
|
|
|
if (!ticket || !lock || my_hash_insert(&mdl_locks, (uchar*)lock))
|
2009-12-02 17:15:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket::destroy(ticket);
|
|
|
|
MDL_lock::destroy(lock);
|
2009-12-02 17:15:40 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
mdl_request->ticket= ticket;
|
2009-12-04 00:55:26 +01:00
|
|
|
lock->type= MDL_lock::MDL_LOCK_EXCLUSIVE;
|
2009-12-04 00:29:40 +01:00
|
|
|
lock->granted.push_front(ticket);
|
2009-12-04 00:52:05 +01:00
|
|
|
m_tickets.push_front(ticket);
|
|
|
|
ticket->m_state= MDL_ACQUIRED;
|
|
|
|
ticket->m_lock= lock;
|
2009-11-30 23:33:22 +01:00
|
|
|
global_lock.active_intention_exclusive++;
|
2009-12-02 17:15:40 +01:00
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
return FALSE;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
2009-12-02 17:15:40 +01:00
|
|
|
/* There is some lock for the object. */
|
|
|
|
*conflict= TRUE;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-02 17:15:40 +01:00
|
|
|
err:
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
return TRUE;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:52:05 +01:00
|
|
|
Acquire the global shared metadata lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Holding this lock will block all requests for exclusive locks
|
|
|
|
and shared locks which can be potentially upgraded to exclusive.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval FALSE Success -- the lock was granted.
|
|
|
|
@retval TRUE Failure -- our thread was killed.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool MDL_context::acquire_global_shared_lock()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-02 17:31:57 +01:00
|
|
|
st_my_thread_var *mysys_var= my_thread_var;
|
2009-11-30 16:55:03 +01:00
|
|
|
const char *old_msg;
|
|
|
|
|
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(!m_has_global_shared_lock);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
|
|
|
|
2009-11-30 23:33:22 +01:00
|
|
|
global_lock.waiting_shared++;
|
2009-12-04 00:52:05 +01:00
|
|
|
old_msg= MDL_ENTER_COND(m_thd, mysys_var);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-02 17:31:57 +01:00
|
|
|
while (!mysys_var->abort && global_lock.active_intention_exclusive)
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_cond_wait(&COND_mdl, &LOCK_mdl);
|
|
|
|
|
2009-11-30 23:33:22 +01:00
|
|
|
global_lock.waiting_shared--;
|
2009-12-02 17:31:57 +01:00
|
|
|
if (mysys_var->abort)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-02 17:31:57 +01:00
|
|
|
/* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
|
2009-11-30 16:55:03 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
2009-11-30 23:33:22 +01:00
|
|
|
global_lock.active_shared++;
|
2009-12-04 00:52:05 +01:00
|
|
|
m_has_global_shared_lock= TRUE;
|
2009-12-02 17:31:57 +01:00
|
|
|
/* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
|
2009-11-30 16:55:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Wait until there will be no locks that conflict with lock requests
|
|
|
|
in the context.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This is a part of the locking protocol and must be used by the
|
|
|
|
acquirer of shared locks after a back-off.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
Does not acquire the locks!
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@retval FALSE Success. One can try to obtain metadata locks.
|
|
|
|
@retval TRUE Failure (thread was killed)
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_context::wait_for_locks()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock *lock;
|
|
|
|
MDL_request *mdl_request;
|
|
|
|
Request_iterator it(m_requests);
|
2009-11-30 16:55:03 +01:00
|
|
|
const char *old_msg;
|
2009-12-02 17:31:57 +01:00
|
|
|
st_my_thread_var *mysys_var= my_thread_var;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
2009-12-02 17:31:57 +01:00
|
|
|
while (!mysys_var->abort)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
We have to check if there are some HANDLERs open by this thread
|
|
|
|
which conflict with some pending exclusive locks. Otherwise we
|
|
|
|
might have a deadlock in situations when we are waiting for
|
|
|
|
pending writer to go away, which in its turn waits for HANDLER
|
|
|
|
open by our thread.
|
|
|
|
|
|
|
|
TODO: investigate situations in which we need to broadcast on
|
|
|
|
COND_mdl because of above scenario.
|
|
|
|
*/
|
2009-12-04 00:52:05 +01:00
|
|
|
mysql_ha_flush(m_thd);
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
2009-12-04 00:52:05 +01:00
|
|
|
old_msg= MDL_ENTER_COND(m_thd, mysys_var);
|
2009-11-30 16:55:03 +01:00
|
|
|
it.rewind();
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((mdl_request= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_key *key= &mdl_request->key;
|
|
|
|
DBUG_ASSERT(mdl_request->ticket == NULL);
|
|
|
|
if (!global_lock.is_lock_type_compatible(mdl_request->type, FALSE))
|
2009-11-30 16:55:03 +01:00
|
|
|
break;
|
|
|
|
/*
|
2009-12-02 17:15:40 +01:00
|
|
|
To avoid starvation we don't wait if we have a conflict against
|
|
|
|
request for MDL_EXCLUSIVE lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
2009-12-04 00:52:05 +01:00
|
|
|
if (mdl_request->is_shared() &&
|
|
|
|
(lock= (MDL_lock*) my_hash_search(&mdl_locks, key->ptr(),
|
2009-12-04 00:29:40 +01:00
|
|
|
key->length())) &&
|
2009-12-04 00:52:05 +01:00
|
|
|
!lock->can_grant_lock(this, mdl_request->type, FALSE))
|
2009-11-30 16:55:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-12-04 00:52:05 +01:00
|
|
|
if (!mdl_request)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pthread_cond_wait(&COND_mdl, &LOCK_mdl);
|
2009-12-02 17:31:57 +01:00
|
|
|
/* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-02 17:31:57 +01:00
|
|
|
return mysys_var->abort;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Auxiliary function which allows to release particular lock
|
|
|
|
ownership of which is represented by a lock ticket object.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::release_ticket(MDL_ticket *ticket)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock *lock= ticket->m_lock;
|
2009-12-04 00:29:40 +01:00
|
|
|
DBUG_ENTER("release_ticket");
|
|
|
|
DBUG_PRINT("enter", ("db=%s name=%s", lock->key.db_name(),
|
|
|
|
lock->key.table_name()));
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
safe_mutex_assert_owner(&LOCK_mdl);
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
m_tickets.remove(ticket);
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
switch (ticket->m_type)
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
|
|
|
case MDL_SHARED_UPGRADABLE:
|
|
|
|
global_lock.active_intention_exclusive--;
|
|
|
|
/* Fallthrough. */
|
|
|
|
case MDL_SHARED:
|
|
|
|
case MDL_SHARED_HIGH_PRIO:
|
|
|
|
lock->granted.remove(ticket);
|
|
|
|
break;
|
|
|
|
case MDL_EXCLUSIVE:
|
2009-12-04 00:55:26 +01:00
|
|
|
lock->type= MDL_lock::MDL_LOCK_SHARED;
|
2009-12-04 00:29:40 +01:00
|
|
|
lock->granted.remove(ticket);
|
|
|
|
global_lock.active_intention_exclusive--;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(0);
|
|
|
|
}
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket::destroy(ticket);
|
2009-12-02 17:15:40 +01:00
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
if (lock->is_empty())
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-11-30 23:33:22 +01:00
|
|
|
my_hash_delete(&mdl_locks, (uchar *)lock);
|
2009-11-30 16:55:03 +01:00
|
|
|
DBUG_PRINT("info", ("releasing cached_object cached_object=%p",
|
2009-11-30 23:33:22 +01:00
|
|
|
lock->cached_object));
|
|
|
|
if (lock->cached_object)
|
|
|
|
(*lock->cached_object_release_hook)(lock->cached_object);
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_lock::destroy(lock);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Release all locks associated with the context, but leave them
|
|
|
|
in the context as lock requests.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
This function is used to back off in case of a lock conflict.
|
|
|
|
It is also used to release shared locks in the end of an SQL
|
|
|
|
statement.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::release_all_locks()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *ticket;
|
|
|
|
Ticket_iterator it(m_tickets);
|
|
|
|
DBUG_ENTER("MDL_context::release_all_locks");
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
/* Detach lock tickets from the requests for back off. */
|
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_request *mdl_request;
|
|
|
|
Request_iterator it(m_requests);
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((mdl_request= it++))
|
|
|
|
mdl_request->ticket= NULL;
|
2009-12-04 00:29:40 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (m_tickets.is_empty())
|
2009-12-04 00:29:40 +01:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
2009-12-04 00:29:40 +01:00
|
|
|
while ((ticket= it++))
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:29:40 +01:00
|
|
|
DBUG_PRINT("info", ("found lock to release ticket=%p", ticket));
|
2009-12-04 00:52:05 +01:00
|
|
|
release_ticket(ticket);
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
/* Inefficient but will do for a while */
|
|
|
|
pthread_cond_broadcast(&COND_mdl);
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
m_tickets.empty();
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Release a lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param ticket Lock to be released
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::release_lock(MDL_ticket *ticket)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(this == ticket->m_ctx);
|
2009-11-30 16:55:03 +01:00
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
2009-12-04 00:52:05 +01:00
|
|
|
release_ticket(ticket);
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_cond_broadcast(&COND_mdl);
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Release all locks in the context which correspond to the same name/
|
|
|
|
object as this lock request, remove lock requests from the context.
|
2009-11-30 23:39:13 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param ticket One of the locks for the name/object for which all
|
|
|
|
locks should be released.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::release_all_locks_for_name(MDL_ticket *name)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
/* Use MDL_ticket::lock to identify other locks for the same object. */
|
|
|
|
MDL_lock *lock= name->m_lock;
|
2009-12-04 00:29:40 +01:00
|
|
|
|
|
|
|
/* Remove matching lock requests from the context. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_request *mdl_request;
|
|
|
|
Request_iterator it_mdl_request(m_requests);
|
2009-11-30 23:39:13 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((mdl_request= it_mdl_request++))
|
2009-11-30 23:39:13 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(mdl_request->ticket &&
|
|
|
|
mdl_request->ticket->m_state == MDL_ACQUIRED);
|
|
|
|
|
|
|
|
if (mdl_request->ticket->m_lock == lock)
|
|
|
|
remove_request(mdl_request);
|
2009-12-04 00:29:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove matching lock tickets from the context. */
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *ticket;
|
|
|
|
Ticket_iterator it_ticket(m_tickets);
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((ticket= it_ticket++))
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(ticket->m_state == MDL_ACQUIRED);
|
|
|
|
/*
|
|
|
|
We rarely have more than one ticket in this loop,
|
|
|
|
let's not bother saving on pthread_cond_broadcast().
|
|
|
|
*/
|
|
|
|
if (ticket->m_lock == lock)
|
|
|
|
release_lock(ticket);
|
2009-11-30 23:39:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Downgrade an exclusive lock to shared metadata lock.
|
2009-11-30 23:39:13 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_ticket::downgrade_exclusive_lock()
|
2009-11-30 23:39:13 +01:00
|
|
|
{
|
2009-12-04 00:29:40 +01:00
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
2009-11-30 23:39:13 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
if (is_shared())
|
2009-11-30 23:39:13 +01:00
|
|
|
return;
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
2009-12-04 00:55:26 +01:00
|
|
|
m_lock->type= MDL_lock::MDL_LOCK_SHARED;
|
2009-12-04 00:52:05 +01:00
|
|
|
m_type= MDL_SHARED_UPGRADABLE;
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_cond_broadcast(&COND_mdl);
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:52:05 +01:00
|
|
|
Release the global shared metadata lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::release_global_shared_lock()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(m_has_global_shared_lock);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
2009-11-30 23:33:22 +01:00
|
|
|
global_lock.active_shared--;
|
2009-12-04 00:52:05 +01:00
|
|
|
m_has_global_shared_lock= FALSE;
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_cond_broadcast(&COND_mdl);
|
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Auxiliary function which allows to check if we have exclusive lock
|
|
|
|
on the object.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param type Id of object type
|
|
|
|
@param db Name of the database
|
|
|
|
@param name Name of the object
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@return TRUE if current context contains exclusive lock for the object,
|
|
|
|
FALSE otherwise.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_context::is_exclusive_lock_owner(unsigned char type,
|
|
|
|
const char *db, const char *name)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_key key(type, db, name);
|
|
|
|
MDL_ticket *ticket;
|
|
|
|
MDL_context::Ticket_iterator it(m_tickets);
|
2009-12-04 00:29:40 +01:00
|
|
|
|
|
|
|
while ((ticket= it++))
|
|
|
|
{
|
2009-12-04 00:55:26 +01:00
|
|
|
if (ticket->m_lock->type == MDL_lock::MDL_LOCK_EXCLUSIVE &&
|
2009-12-04 00:52:05 +01:00
|
|
|
ticket->m_lock->key.is_equal(&key))
|
2009-12-04 00:29:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticket;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Auxiliary function which allows to check if we have some kind of lock on
|
|
|
|
a object.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param type Id of object type
|
|
|
|
@param db Name of the database
|
|
|
|
@param name Name of the object
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@return TRUE if current context contains satisfied lock for the object,
|
|
|
|
FALSE otherwise.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool
|
|
|
|
MDL_context::is_lock_owner(unsigned char type,
|
|
|
|
const char *db, const char *name)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_key key(type, db, name);
|
|
|
|
MDL_ticket *ticket;
|
|
|
|
MDL_context::Ticket_iterator it(m_tickets);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
while ((ticket= it++))
|
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
if (ticket->m_lock->key.is_equal(&key))
|
2009-12-04 00:29:40 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:29:40 +01:00
|
|
|
return ticket;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Check if we have any pending exclusive locks which conflict with
|
|
|
|
existing shared lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@pre The ticket must match an acquired lock.
|
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param ticket Shared lock against which check should be performed.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@return TRUE if there are any conflicting locks, FALSE otherwise.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
bool MDL_ticket::has_pending_conflicting_lock() const
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(is_shared());
|
2009-11-30 16:55:03 +01:00
|
|
|
safe_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&LOCK_mdl);
|
2009-12-04 00:52:05 +01:00
|
|
|
result= !m_lock->waiting.is_empty();
|
2009-11-30 16:55:03 +01:00
|
|
|
pthread_mutex_unlock(&LOCK_mdl);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Associate pointer to an opaque object with a lock.
|
|
|
|
|
|
|
|
@param cached_object Pointer to the object
|
|
|
|
@param release_hook Cleanup function to be called when MDL subsystem
|
|
|
|
decides to remove lock or associate another object.
|
|
|
|
|
|
|
|
This is used to cache a pointer to TABLE_SHARE in the lock
|
|
|
|
structure. Such caching can save one acquisition of LOCK_open
|
|
|
|
and one table definition cache lookup for every table.
|
|
|
|
|
|
|
|
Since the pointer may be stored only inside an acquired lock,
|
|
|
|
the caching is only effective when there is more than one lock
|
|
|
|
granted on a given table.
|
|
|
|
|
|
|
|
This function has the following usage pattern:
|
|
|
|
- try to acquire an MDL lock
|
|
|
|
- when done, call for mdl_get_cached_object(). If it returns NULL, our
|
|
|
|
thread has the only lock on this table.
|
|
|
|
- look up TABLE_SHARE in the table definition cache
|
|
|
|
- call mdl_set_cache_object() to assign the share to the opaque pointer.
|
|
|
|
|
|
|
|
The release hook is invoked when the last shared metadata
|
|
|
|
lock on this name is released.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void
|
|
|
|
MDL_ticket::set_cached_object(void *cached_object,
|
|
|
|
mdl_cached_object_release_hook release_hook)
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
|
|
|
DBUG_ENTER("mdl_set_cached_object");
|
2009-12-04 00:29:40 +01:00
|
|
|
DBUG_PRINT("enter", ("db=%s name=%s cached_object=%p",
|
2009-12-04 00:52:05 +01:00
|
|
|
m_lock->key.db_name(), m_lock->key.table_name(),
|
2009-12-04 00:29:40 +01:00
|
|
|
cached_object));
|
2009-11-30 16:55:03 +01:00
|
|
|
/*
|
2009-12-04 00:52:05 +01:00
|
|
|
TODO: This assumption works now since we do get_cached_object()
|
|
|
|
and set_cached_object() in the same critical section. Once
|
2009-11-30 16:55:03 +01:00
|
|
|
this becomes false we will have to call release_hook here and
|
|
|
|
use additional mutex protecting 'cached_object' member.
|
|
|
|
*/
|
2009-12-04 00:52:05 +01:00
|
|
|
DBUG_ASSERT(!m_lock->cached_object);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
m_lock->cached_object= cached_object;
|
|
|
|
m_lock->cached_object_release_hook= release_hook;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-04 00:34:19 +01:00
|
|
|
Get a pointer to an opaque object that associated with the lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@param ticket Lock ticket for the lock which the object is associated to.
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2009-12-04 00:34:19 +01:00
|
|
|
@return Pointer to an opaque object associated with the lock.
|
2009-11-30 16:55:03 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void *MDL_ticket::get_cached_object()
|
2009-11-30 16:55:03 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
return m_lock->cached_object;
|
2009-11-30 16:55:03 +01:00
|
|
|
}
|
2009-12-04 00:29:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Releases metadata locks that were acquired after a specific savepoint.
|
|
|
|
|
|
|
|
@note Used to release tickets acquired during a savepoint unit.
|
|
|
|
@note It's safe to iterate and unlock any locks after taken after this
|
|
|
|
savepoint because other statements that take other special locks
|
|
|
|
cause a implicit commit (ie LOCK TABLES).
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
@param mdl_savepont The last acquired MDL lock when the
|
|
|
|
savepoint was set.
|
2009-12-04 00:29:40 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
void MDL_context::rollback_to_savepoint(MDL_ticket *mdl_savepoint)
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
2009-12-04 00:52:05 +01:00
|
|
|
MDL_ticket *ticket;
|
|
|
|
Ticket_iterator it(m_tickets);
|
|
|
|
DBUG_ENTER("MDL_context::rollback_to_savepoint");
|
2009-12-04 00:29:40 +01:00
|
|
|
|
2009-12-04 00:52:05 +01:00
|
|
|
while ((ticket= it++))
|
2009-12-04 00:29:40 +01:00
|
|
|
{
|
|
|
|
/* Stop when lock was acquired before this savepoint. */
|
2009-12-04 00:52:05 +01:00
|
|
|
if (ticket == mdl_savepoint)
|
2009-12-04 00:29:40 +01:00
|
|
|
break;
|
2009-12-04 00:52:05 +01:00
|
|
|
release_lock(ticket);
|
2009-12-04 00:29:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|