diff options
author | Konstantin Osipov <kostja@sun.com> | 2009-12-04 02:52:05 +0300 |
---|---|---|
committer | Konstantin Osipov <kostja@sun.com> | 2009-12-04 02:52:05 +0300 |
commit | a3a23ec4d3b9d9c22b8bdbc9adf2cd0f157c6a3f (patch) | |
tree | 6865c45e4fd723b4972eb4705acf049a4d4f9fff /sql | |
parent | 195adcd201b78357ebdf8a30777d3f867829418a (diff) | |
download | mariadb-git-a3a23ec4d3b9d9c22b8bdbc9adf2cd0f157c6a3f.tar.gz |
Backport of:
----------------------------------------------------------
revno: 2617.23.20
committer: Konstantin Osipov <kostja@sun.com>
branch nick: mysql-6.0-runtime
timestamp: Wed 2009-03-04 16:31:31 +0300
message:
WL#4284 "Transactional DDL locking"
Review comments: "Objectify" the MDL API.
MDL_request and MDL_context still need manual construction and
destruction, since they are used in environment that is averse
to constructors/destructors.
Diffstat (limited to 'sql')
-rw-r--r-- | sql/ha_ndbcluster_binlog.cc | 6 | ||||
-rw-r--r-- | sql/lock.cc | 28 | ||||
-rw-r--r-- | sql/log_event.cc | 9 | ||||
-rw-r--r-- | sql/mdl.cc | 912 | ||||
-rw-r--r-- | sql/mdl.h | 260 | ||||
-rw-r--r-- | sql/rpl_rli.cc | 2 | ||||
-rw-r--r-- | sql/sp_head.cc | 16 | ||||
-rw-r--r-- | sql/sql_base.cc | 128 | ||||
-rw-r--r-- | sql/sql_class.cc | 8 | ||||
-rw-r--r-- | sql/sql_class.h | 10 | ||||
-rw-r--r-- | sql/sql_delete.cc | 26 | ||||
-rw-r--r-- | sql/sql_handler.cc | 30 | ||||
-rw-r--r-- | sql/sql_parse.cc | 6 | ||||
-rw-r--r-- | sql/sql_plist.h | 1 | ||||
-rw-r--r-- | sql/sql_show.cc | 27 | ||||
-rw-r--r-- | sql/sql_table.cc | 117 | ||||
-rw-r--r-- | sql/sql_trigger.cc | 8 | ||||
-rw-r--r-- | sql/table.cc | 4 | ||||
-rw-r--r-- | sql/table.h | 8 |
19 files changed, 819 insertions, 787 deletions
diff --git a/sql/ha_ndbcluster_binlog.cc b/sql/ha_ndbcluster_binlog.cc index aa1c50eaf5b..08abb88e768 100644 --- a/sql/ha_ndbcluster_binlog.cc +++ b/sql/ha_ndbcluster_binlog.cc @@ -140,7 +140,7 @@ static Uint64 *p_latest_trans_gci= 0; */ static TABLE *ndb_binlog_index= 0; static TABLE_LIST binlog_tables; -static MDL_LOCK_REQUEST binlog_mdl_lock_request; +static MDL_request binlog_mdl_request; /* Helper functions @@ -2342,8 +2342,8 @@ static int open_ndb_binlog_index(THD *thd, TABLE **ndb_binlog_index) tables->alias= tables->table_name= reptable; tables->lock_type= TL_WRITE; thd->proc_info= "Opening " NDB_REP_DB "." NDB_REP_TABLE; - mdl_request_init(&binlog_mdl_lock_request, 0, tables->db, tables->table_name); - tables->mdl_lock_request= &binlog_mdl_lock_request; + binlog_mdl_request.init(0, tables->db, tables->table_name); + tables->mdl_request= &binlog_mdl_request; tables->required_type= FRMTYPE_TABLE; uint counter; thd->clear_error(); diff --git a/sql/lock.cc b/sql/lock.cc index 40bceae4a99..170007d8f66 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -944,7 +944,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, @note This function assumes that no metadata locks were acquired before calling it. Also it cannot be called while holding LOCK_open mutex. Both these invariants are enforced by asserts - in mdl_acquire_exclusive_locks() functions. + in MDL_context::acquire_exclusive_locks(). @retval FALSE Success. @retval TRUE Failure (OOM or thread was killed). @@ -953,24 +953,24 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, bool lock_table_names(THD *thd, TABLE_LIST *table_list) { TABLE_LIST *lock_table; - MDL_LOCK_REQUEST *mdl_lock_req; + MDL_request *mdl_request; for (lock_table= table_list; lock_table; lock_table= lock_table->next_local) { - mdl_lock_req= mdl_request_alloc(0, lock_table->db, lock_table->table_name, - thd->mem_root); - if (!mdl_lock_req) + mdl_request= MDL_request::create(0, lock_table->db, lock_table->table_name, + thd->mem_root); + if (!mdl_request) goto end; - mdl_request_set_type(mdl_lock_req, MDL_EXCLUSIVE); - mdl_request_add(&thd->mdl_context, mdl_lock_req); - lock_table->mdl_lock_request= mdl_lock_req; + mdl_request->set_type(MDL_EXCLUSIVE); + thd->mdl_context.add_request(mdl_request); + lock_table->mdl_request= mdl_request; } - if (mdl_acquire_exclusive_locks(&thd->mdl_context)) + if (thd->mdl_context.acquire_exclusive_locks()) goto end; return 0; end: - mdl_request_remove_all(&thd->mdl_context); + thd->mdl_context.remove_all_requests(); return 1; } @@ -986,8 +986,8 @@ end: void unlock_table_names(THD *thd) { DBUG_ENTER("unlock_table_names"); - mdl_ticket_release_all(&thd->mdl_context); - mdl_request_remove_all(&thd->mdl_context); + thd->mdl_context.release_all_locks(); + thd->mdl_context.remove_all_requests(); DBUG_VOID_RETURN; } @@ -1147,7 +1147,7 @@ bool lock_global_read_lock(THD *thd) redundancy between metadata locks, global read lock and DDL blocker (see WL#4399 and WL#4400). */ - if (mdl_acquire_global_shared_lock(&thd->mdl_context)) + if (thd->mdl_context.acquire_global_shared_lock()) { /* Our thread was killed -- return back to initial state. */ pthread_mutex_lock(&LOCK_global_read_lock); @@ -1181,7 +1181,7 @@ void unlock_global_read_lock(THD *thd) ("global_read_lock: %u global_read_lock_blocks_commit: %u", global_read_lock, global_read_lock_blocks_commit)); - mdl_release_global_shared_lock(&thd->mdl_context); + thd->mdl_context.release_global_shared_lock(); pthread_mutex_lock(&LOCK_global_read_lock); tmp= --global_read_lock; diff --git a/sql/log_event.cc b/sql/log_event.cc index 9f4369b901e..e41bb690e8a 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -8060,7 +8060,7 @@ int Table_map_log_event::do_apply_event(Relay_log_info const *rli) { RPL_TABLE_LIST *table_list; char *db_mem, *tname_mem; - MDL_LOCK_REQUEST *mdl_lock_request; + MDL_request *mdl_request; size_t dummy_len; void *memory; DBUG_ENTER("Table_map_log_event::do_apply_event(Relay_log_info*)"); @@ -8075,7 +8075,7 @@ int Table_map_log_event::do_apply_event(Relay_log_info const *rli) &table_list, (uint) sizeof(RPL_TABLE_LIST), &db_mem, (uint) NAME_LEN + 1, &tname_mem, (uint) NAME_LEN + 1, - &mdl_lock_request, sizeof(MDL_LOCK_REQUEST), + &mdl_request, sizeof(MDL_request), NullS))) DBUG_RETURN(HA_ERR_OUT_OF_MEM); @@ -8088,9 +8088,8 @@ int Table_map_log_event::do_apply_event(Relay_log_info const *rli) table_list->updating= 1; strmov(table_list->db, rpl_filter->get_rewrite_db(m_dbnam, &dummy_len)); strmov(table_list->table_name, m_tblnam); - mdl_request_init(mdl_lock_request, 0, table_list->db, - table_list->table_name); - table_list->mdl_lock_request= mdl_lock_request; + mdl_request->init(0, table_list->db, table_list->table_name); + table_list->mdl_request= mdl_request; int error= 0; diff --git a/sql/mdl.cc b/sql/mdl.cc index e3f285f3a55..101d90d7e42 100644 --- a/sql/mdl.cc +++ b/sql/mdl.cc @@ -14,26 +14,27 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - #include "mdl.h" #include "debug_sync.h" #include <hash.h> #include <mysqld_error.h> +static bool mdl_initialized= 0; /** The lock context. Created internally for an acquired lock. - For a given name, there exists only one MDL_LOCK instance, + For a given name, there exists only one MDL_lock instance, and it exists only when the lock has been granted. Can be seen as an MDL subsystem's version of TABLE_SHARE. */ -struct MDL_LOCK +class MDL_lock { - typedef I_P_List<MDL_LOCK_TICKET, - I_P_List_adapter<MDL_LOCK_TICKET, - &MDL_LOCK_TICKET::next_in_lock, - &MDL_LOCK_TICKET::prev_in_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 Ticket_list::Iterator Ticket_iterator; @@ -45,60 +46,82 @@ struct MDL_LOCK EXCLUSIVE, } type; /** The key of the object (data) being protected. */ - MDL_KEY key; + MDL_key key; /** List of granted tickets for this lock. */ Ticket_list granted; /** There can be several upgraders and active exclusive - belonging to the same context. + 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; void *cached_object; mdl_cached_object_release_hook cached_object_release_hook; - MDL_LOCK(const MDL_KEY *mdl_key) - : type(SHARED), cached_object(0), cached_object_release_hook(0) + bool is_empty() const { - key.mdl_key_init(mdl_key); - granted.empty(); - waiting.empty(); + return (granted.is_empty() && waiting.is_empty()); } - bool is_empty() + 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) + : type(SHARED), + key(key_arg), + cached_object(NULL), + cached_object_release_hook(NULL) { - return (granted.is_empty() && waiting.is_empty()); } }; -pthread_mutex_t LOCK_mdl; -pthread_cond_t COND_mdl; -HASH mdl_locks; +static pthread_mutex_t LOCK_mdl; +static pthread_cond_t COND_mdl; +static HASH mdl_locks; /** - Structure implementing global metadata lock. The only types - of locks which are supported at the moment are shared and - intention exclusive locks. Note that the latter type of global - lock acquired automatically when one tries to acquire exclusive - or shared upgradable lock on particular object. + 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. */ -struct MDL_GLOBAL_LOCK +class MDL_global_lock { +public: uint waiting_shared; uint active_shared; uint active_intention_exclusive; -} global_lock; + + 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; +}; -extern "C" uchar * +static MDL_global_lock global_lock; + + +extern "C" +{ +static uchar * mdl_locks_key(const uchar *record, size_t *length, my_bool not_used __attribute__((unused))) { - MDL_LOCK *entry=(MDL_LOCK*) record; - *length= entry->key.length(); - return (uchar*) entry->key.ptr(); + MDL_lock *lock=(MDL_lock*) record; + *length= lock->key.length(); + return (uchar*) lock->key.ptr(); } +} /* extern "C" */ /** @@ -116,17 +139,20 @@ mdl_locks_key(const uchar *record, size_t *length, 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. - Please see the description of mdl_acquire_shared_lock() for details. + Please see the description of MDL_context::acquire_shared_lock() + for details. */ void mdl_init() { + DBUG_ASSERT(! mdl_initialized); + mdl_initialized= TRUE; 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); - global_lock.waiting_shared= global_lock.active_shared= 0; - global_lock.active_intention_exclusive= 0; + /* The global lock is zero-initialized by the loader. */ + DBUG_ASSERT(global_lock.is_empty()); } @@ -139,10 +165,15 @@ void mdl_init() void mdl_destroy() { - DBUG_ASSERT(!mdl_locks.records); - pthread_mutex_destroy(&LOCK_mdl); - pthread_cond_destroy(&COND_mdl); - my_hash_free(&mdl_locks); + 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); + } } @@ -152,12 +183,19 @@ void mdl_destroy() This is to be called when a new server connection is created. */ -void mdl_context_init(MDL_CONTEXT *context, THD *thd) +void MDL_context::init(THD *thd_arg) { - context->requests.empty(); - context->tickets.empty(); - context->thd= thd; - context->has_global_shared_lock= FALSE; + 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(); } @@ -173,11 +211,11 @@ void mdl_context_init(MDL_CONTEXT *context, THD *thd) that all tables are closed before a connection is destroyed. */ -void mdl_context_destroy(MDL_CONTEXT *context) +void MDL_context::destroy() { - DBUG_ASSERT(context->requests.is_empty()); - DBUG_ASSERT(context->tickets.is_empty()); - DBUG_ASSERT(!context->has_global_shared_lock); + DBUG_ASSERT(m_requests.is_empty()); + DBUG_ASSERT(m_tickets.is_empty()); + DBUG_ASSERT(! m_has_global_shared_lock); } @@ -191,12 +229,22 @@ void mdl_context_destroy(MDL_CONTEXT *context) tables open/metadata locks for HANDLERs afterwards. */ -void mdl_context_backup_and_reset(MDL_CONTEXT *ctx, MDL_CONTEXT *backup) +void MDL_context::backup_and_reset(MDL_context *backup) { - backup->requests.empty(); - backup->tickets.empty(); - ctx->requests.swap(backup->requests); - ctx->tickets.swap(backup->tickets); + 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; } @@ -204,12 +252,15 @@ void mdl_context_backup_and_reset(MDL_CONTEXT *ctx, MDL_CONTEXT *backup) Restore state of meta-data locking context from backup. */ -void mdl_context_restore(MDL_CONTEXT *ctx, MDL_CONTEXT *backup) +void MDL_context::restore_from_backup(MDL_context *backup) { - DBUG_ASSERT(ctx->requests.is_empty()); - DBUG_ASSERT(ctx->tickets.is_empty()); - ctx->requests.swap(backup->requests); - ctx->tickets.swap(backup->tickets); + 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; } @@ -217,32 +268,37 @@ void mdl_context_restore(MDL_CONTEXT *ctx, MDL_CONTEXT *backup) Merge meta-data locks from one context into another. */ -void mdl_context_merge(MDL_CONTEXT *dst, MDL_CONTEXT *src) +void MDL_context::merge(MDL_context *src) { - MDL_LOCK_TICKET *ticket; - MDL_LOCK_REQUEST *lock_req; + MDL_ticket *ticket; + MDL_request *mdl_request; - DBUG_ASSERT(dst->thd == src->thd); + DBUG_ASSERT(m_thd == src->m_thd); - if (!src->requests.is_empty()) + if (!src->m_requests.is_empty()) { - MDL_CONTEXT::Request_iterator it(src->requests); - while ((lock_req= it++)) - dst->requests.push_front(lock_req); - src->requests.empty(); + Request_iterator it(src->m_requests); + while ((mdl_request= it++)) + m_requests.push_front(mdl_request); + src->m_requests.empty(); } - if (!src->tickets.is_empty()) + if (!src->m_tickets.is_empty()) { - MDL_CONTEXT::Ticket_iterator it(src->tickets); + Ticket_iterator it(src->m_tickets); while ((ticket= it++)) { - DBUG_ASSERT(ticket->ctx); - ticket->ctx= dst; - dst->tickets.push_front(ticket); + DBUG_ASSERT(ticket->m_ctx); + ticket->m_ctx= this; + m_tickets.push_front(ticket); } - src->tickets.empty(); + src->m_tickets.empty(); } + /* + 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); } @@ -260,10 +316,9 @@ void mdl_context_merge(MDL_CONTEXT *dst, MDL_CONTEXT *src) The MDL subsystem does not own or manage memory of lock requests. Instead it assumes that the life time of every lock request (including - encompassed members db/name) encloses calls to mdl_request_add() - and mdl_request_remove() or mdl_request_remove_all(). + encompassed members db/name) encloses calls to MDL_context::add_request() + and MDL_context::remove_request() or MDL_context::remove_all_requests(). - @param lock_req Pointer to an MDL_LOCK_REQUEST object to initialize @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 @@ -275,12 +330,13 @@ void mdl_context_merge(MDL_CONTEXT *dst, MDL_CONTEXT *src) they share the same name space in the SQL standard. */ -void mdl_request_init(MDL_LOCK_REQUEST *lock_req, unsigned char type, - const char *db, const char *name) +void MDL_request::init(unsigned char type_arg, + const char *db_arg, + const char *name_arg) { - lock_req->key.mdl_key_init(type, db, name); - lock_req->type= MDL_SHARED; - lock_req->ticket= NULL; + key.mdl_key_init(type_arg, db_arg, name_arg); + type= MDL_SHARED; + ticket= NULL; } @@ -302,19 +358,18 @@ void mdl_request_init(MDL_LOCK_REQUEST *lock_req, unsigned char type, @retval non-0 Pointer to an object representing a lock request */ -MDL_LOCK_REQUEST * -mdl_request_alloc(unsigned char type, const char *db, - const char *name, MEM_ROOT *root) +MDL_request * +MDL_request::create(unsigned char type, const char *db, + const char *name, MEM_ROOT *root) { - MDL_LOCK_REQUEST *lock_req; + MDL_request *mdl_request; - if (!(lock_req= (MDL_LOCK_REQUEST*) alloc_root(root, - sizeof(MDL_LOCK_REQUEST)))) + if (!(mdl_request= (MDL_request*) alloc_root(root, sizeof(MDL_request)))) return NULL; - mdl_request_init(lock_req, type, db, name); + mdl_request->init(type, db, name); - return lock_req; + return mdl_request; } @@ -322,23 +377,24 @@ mdl_request_alloc(unsigned char type, const char *db, Add a lock request to the list of lock requests of the context. The procedure to acquire metadata locks is: - - allocate and initialize lock requests (mdl_request_alloc()) - - associate them with a context (mdl_request_add()) - - call mdl_acquire_shared_lock()/mdl_ticket_release() (maybe repeatedly). + - 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). Associates a lock request with the given context. + There should be no more than one context per connection, to + avoid deadlocks. - @param context The MDL context to associate the lock with. - There should be no more than one context per - connection, to avoid deadlocks. - @param lock_req The lock request to be added. + @param mdl_request The lock request to be added. */ -void mdl_request_add(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req) +void MDL_context::add_request(MDL_request *mdl_request) { - DBUG_ENTER("mdl_request_add"); - DBUG_ASSERT(lock_req->ticket == NULL); - context->requests.push_front(lock_req); + DBUG_ENTER("MDL_context::add_request"); + DBUG_ASSERT(mdl_request->ticket == NULL); + m_requests.push_front(mdl_request); DBUG_VOID_RETURN; } @@ -348,8 +404,7 @@ void mdl_request_add(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req) Disassociates a lock request from the given context. - @param context The MDL context to remove the lock from. - @param lock_req The lock request to be removed. + @param mdl_request The lock request to be removed. @pre The lock request being removed should correspond to a ticket that was released or was not acquired. @@ -358,13 +413,13 @@ void mdl_request_add(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req) (i.e. sets type to MDL_SHARED). */ -void mdl_request_remove(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req) +void MDL_context::remove_request(MDL_request *mdl_request) { - DBUG_ENTER("mdl_request_remove"); + DBUG_ENTER("MDL_context::remove_request"); /* Reset lock request back to its initial state. */ - lock_req->type= MDL_SHARED; - lock_req->ticket= NULL; - context->requests.remove(lock_req); + mdl_request->type= MDL_SHARED; + mdl_request->ticket= NULL; + m_requests.remove(mdl_request); DBUG_VOID_RETURN; } @@ -374,76 +429,62 @@ void mdl_request_remove(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req) Disassociates lock requests from the context. Also resets lock requests back to their initial state (i.e. MDL_SHARED). - - @param context Context to be cleared. */ -void mdl_request_remove_all(MDL_CONTEXT *context) +void MDL_context::remove_all_requests() { - MDL_LOCK_REQUEST *lock_req; - MDL_CONTEXT::Request_iterator it(context->requests); - while ((lock_req= it++)) + MDL_request *mdl_request; + Request_iterator it(m_requests); + while ((mdl_request= it++)) { /* Reset lock request back to its initial state. */ - lock_req->type= MDL_SHARED; - lock_req->ticket= NULL; + mdl_request->type= MDL_SHARED; + mdl_request->ticket= NULL; } - context->requests.empty(); + m_requests.empty(); } /** - Auxiliary functions needed for creation/destruction of MDL_LOCK objects. + Auxiliary functions needed for creation/destruction of MDL_lock objects. @todo This naive implementation should be replaced with one that saves on memory allocation by reusing released objects. */ -static MDL_LOCK* alloc_lock_object(const MDL_KEY *mdl_key) +inline MDL_lock *MDL_lock::create(const MDL_key *mdl_key) { - return new MDL_LOCK(mdl_key); + return new MDL_lock(mdl_key); } -static void free_lock_object(MDL_LOCK *lock) +void MDL_lock::destroy(MDL_lock *lock) { delete lock; } /** - Auxiliary functions needed for creation/destruction of MDL_LOCK_TICKET + Auxiliary functions needed for creation/destruction of MDL_ticket objects. @todo This naive implementation should be replaced with one that saves on memory allocation by reusing released objects. */ -static MDL_LOCK_TICKET* alloc_ticket_object(MDL_CONTEXT *context) +MDL_ticket *MDL_ticket::create(MDL_context *ctx_arg, enum_mdl_type type_arg) { - MDL_LOCK_TICKET *ticket= new MDL_LOCK_TICKET; - return ticket; + return new MDL_ticket(ctx_arg, type_arg); } -static void free_ticket_object(MDL_LOCK_TICKET *ticket) +void MDL_ticket::destroy(MDL_ticket *ticket) { delete ticket; } /** - Helper functions which simplifies writing various checks and asserts. -*/ - -template <typename T> -static inline bool is_shared(T *lock_data) -{ - return (lock_data->type < MDL_EXCLUSIVE); -} - - -/** Helper functions and macros to be used for killable waiting in metadata locking subsystem. @@ -458,7 +499,7 @@ static inline bool is_shared(T *lock_data) #define MDL_ENTER_COND(A, B) mdl_enter_cond(A, B, __func__, __FILE__, __LINE__) -static inline const char* mdl_enter_cond(MDL_CONTEXT *context, +static inline const char *mdl_enter_cond(THD *thd, st_my_thread_var *mysys_var, const char *calling_func, const char *calling_file, @@ -469,13 +510,15 @@ static inline const char* mdl_enter_cond(MDL_CONTEXT *context, mysys_var->current_mutex= &LOCK_mdl; mysys_var->current_cond= &COND_mdl; - return set_thd_proc_info(context->thd, "Waiting for table", + DEBUG_SYNC(thd, "mdl_enter_cond"); + + return set_thd_proc_info(thd, "Waiting for table", calling_func, calling_file, calling_line); } #define MDL_EXIT_COND(A, B, C) mdl_exit_cond(A, B, C, __func__, __FILE__, __LINE__) -static inline void mdl_exit_cond(MDL_CONTEXT *context, +static inline void mdl_exit_cond(THD *thd, st_my_thread_var *mysys_var, const char* old_msg, const char *calling_func, @@ -490,7 +533,9 @@ static inline void mdl_exit_cond(MDL_CONTEXT *context, mysys_var->current_cond= 0; pthread_mutex_unlock(&mysys_var->mutex); - (void) set_thd_proc_info(context->thd, old_msg, calling_func, + DEBUG_SYNC(thd, "mdl_exit_cond"); + + (void) set_thd_proc_info(thd, old_msg, calling_func, calling_file, calling_line); } @@ -503,8 +548,8 @@ static inline void mdl_exit_cond(MDL_CONTEXT *context, request, implying a form of lock on the global metadata, is compatible with the current state of the global metadata lock. - @param lock_req Request for lock on an individual object, implying a - certain kind of global metadata lock. + @param mdl_request Request for lock on an individual object, implying a + certain kind of global metadata lock. @retval TRUE - Lock request can be satisfied @retval FALSE - There is some conflicting lock @@ -533,7 +578,9 @@ static inline void mdl_exit_cond(MDL_CONTEXT *context, type of locks we don't even have any accounting for them. */ -static bool can_grant_global_lock(enum_mdl_type type, bool is_upgrade) +bool +MDL_global_lock::is_lock_type_compatible(enum_mdl_type type, + bool is_upgrade) const { switch (type) { @@ -542,7 +589,7 @@ static bool can_grant_global_lock(enum_mdl_type type, bool is_upgrade) return TRUE; break; case MDL_SHARED_UPGRADABLE: - if (global_lock.active_shared || global_lock.waiting_shared) + if (active_shared || waiting_shared) { /* We are going to obtain intention exclusive global lock and @@ -562,13 +609,12 @@ static bool can_grant_global_lock(enum_mdl_type type, bool is_upgrade) There should be no conflicting global locks since for each upgradable shared lock we obtain intention exclusive global lock first. */ - DBUG_ASSERT(global_lock.active_shared == 0 && - global_lock.active_intention_exclusive); + DBUG_ASSERT(active_shared == 0 && active_intention_exclusive); return TRUE; } else { - if (global_lock.active_shared || global_lock.waiting_shared) + if (active_shared || waiting_shared) { /* We are going to obtain intention exclusive global lock and @@ -590,15 +636,15 @@ static bool can_grant_global_lock(enum_mdl_type type, bool is_upgrade) /** Check if request for the lock can be satisfied given current state of lock. - @param lock Lock. - @param lock_req Request for lock. + @param lock Lock. + @param mdl_request Request for lock. @retval TRUE Lock request can be satisfied @retval FALSE There is some conflicting lock. This function defines the following compatibility matrix for metadata locks: - | Satisfied or pending requests which we have in MDL_LOCK + | Satisfied or pending requests which we have in MDL_lock ----------------+--------------------------------------------------------- Current request | Active-S Pending-X Active-X Act-S-pend-upgrade-to-X ----------------+--------------------------------------------------------- @@ -617,22 +663,23 @@ static bool can_grant_global_lock(enum_mdl_type type, bool is_upgrade) being upgraded. */ -static bool can_grant_lock(MDL_CONTEXT *ctx, MDL_LOCK *lock, - enum_mdl_type type, bool is_upgrade) +bool +MDL_lock::can_grant_lock(const MDL_context *requestor_ctx, enum_mdl_type type_arg, + bool is_upgrade) { bool can_grant= FALSE; - switch (type) { + switch (type_arg) { case MDL_SHARED: case MDL_SHARED_UPGRADABLE: case MDL_SHARED_HIGH_PRIO: - if (lock->type == MDL_LOCK::SHARED) + if (type == MDL_lock::SHARED) { /* Pending exclusive locks have higher priority over shared locks. */ - if (lock->waiting.is_empty() || type == MDL_SHARED_HIGH_PRIO) + if (waiting.is_empty() || type_arg == MDL_SHARED_HIGH_PRIO) can_grant= TRUE; } - else if (lock->granted.head()->ctx == ctx) + else if (granted.head()->get_ctx() == requestor_ctx) { /* When exclusive lock comes from the same context we can satisfy our @@ -646,33 +693,33 @@ static bool can_grant_lock(MDL_CONTEXT *ctx, MDL_LOCK *lock, if (is_upgrade) { /* We are upgrading MDL_SHARED to MDL_EXCLUSIVE. */ - MDL_LOCK_TICKET *conf_lock_ticket; - MDL_LOCK::Ticket_iterator it(lock->granted); + MDL_ticket *conflicting_ticket; + MDL_lock::Ticket_iterator it(granted); /* There should be no active exclusive locks since we own shared lock on the object. */ - DBUG_ASSERT(lock->type == MDL_LOCK::SHARED); + DBUG_ASSERT(type == MDL_lock::SHARED); - while ((conf_lock_ticket= it++)) + while ((conflicting_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 (conf_lock_ticket->ctx != ctx) + if (conflicting_ticket->get_ctx() != requestor_ctx) break; } /* Grant lock if there are no conflicting shared locks. */ - if (conf_lock_ticket == NULL) + if (conflicting_ticket == NULL) can_grant= TRUE; break; } - else if (lock->type == MDL_LOCK::SHARED) + else if (type == MDL_lock::SHARED) { - can_grant= lock->granted.is_empty(); + can_grant= granted.is_empty(); } break; default: @@ -686,23 +733,23 @@ static bool can_grant_lock(MDL_CONTEXT *ctx, MDL_LOCK *lock, Check whether the context already holds a compatible lock ticket on a object. Only shared locks can be recursive. - @param lock_req Lock request object for lock to be acquired + @param mdl_request Lock request object for lock to be acquired @return A pointer to the lock ticket for the object or NULL otherwise. */ -static MDL_LOCK_TICKET * -mdl_context_find_ticket(MDL_CONTEXT *ctx, MDL_LOCK_REQUEST *lock_req) +MDL_ticket * +MDL_context::find_ticket(MDL_request *mdl_request) { - MDL_LOCK_TICKET *ticket; - MDL_CONTEXT::Ticket_iterator it(ctx->tickets); + MDL_ticket *ticket; + Ticket_iterator it(m_tickets); - DBUG_ASSERT(is_shared(lock_req)); + DBUG_ASSERT(mdl_request->is_shared()); while ((ticket= it++)) { - if (lock_req->type == ticket->type && - lock_req->key.is_equal(&ticket->lock->key)) + if (mdl_request->type == ticket->m_type && + mdl_request->key.is_equal(&ticket->m_lock->key)) break; } @@ -715,7 +762,7 @@ mdl_context_find_ticket(MDL_CONTEXT *ctx, MDL_LOCK_REQUEST *lock_req) 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_acquire_shared_lock() + the new locking API to the system. MDL_context::acquire_shared_lock() is currently used from open_table(), and there we have only one table to work with. @@ -723,32 +770,31 @@ mdl_context_find_ticket(MDL_CONTEXT *ctx, MDL_LOCK_REQUEST *lock_req) This function must be called after the lock is added to a context. - @param context [in] Context containing request for lock - @param lock_req [in] Lock request object for lock to be acquired + @param mdl_request [in] Lock request object for lock to be acquired @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 - (using mdl_wait_for_locks()). + (using MDL_context::wait_for_locks()). @retval FALSE Success. - @retval TRUE Failure. Either error occured or conflicting lock exists. + @retval TRUE Failure. Either error occurred or conflicting lock exists. In the latter case "retry" parameter is set to TRUE. */ -bool mdl_acquire_shared_lock(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req, - bool *retry) +bool +MDL_context::acquire_shared_lock(MDL_request *mdl_request, bool *retry) { - MDL_LOCK *lock; - MDL_KEY *key= &lock_req->key; - MDL_LOCK_TICKET *ticket; + MDL_lock *lock; + MDL_key *key= &mdl_request->key; + MDL_ticket *ticket; *retry= FALSE; - DBUG_ASSERT(is_shared(lock_req) && lock_req->ticket == NULL); + DBUG_ASSERT(mdl_request->is_shared() && mdl_request->ticket == NULL); safe_mutex_assert_not_owner(&LOCK_open); - if (context->has_global_shared_lock && - lock_req->type == MDL_SHARED_UPGRADABLE) + if (m_has_global_shared_lock && + mdl_request->type == MDL_SHARED_UPGRADABLE) { my_error(ER_CANT_UPDATE_WITH_READLOCK, MYF(0)); return TRUE; @@ -758,52 +804,50 @@ bool mdl_acquire_shared_lock(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req, Check whether the context already holds a shared lock on the object, and if so, grant the request. */ - if ((ticket= mdl_context_find_ticket(context, lock_req))) + if ((ticket= find_ticket(mdl_request))) { - DBUG_ASSERT(ticket->state == MDL_ACQUIRED); - lock_req->ticket= ticket; + DBUG_ASSERT(ticket->m_state == MDL_ACQUIRED); + mdl_request->ticket= ticket; return FALSE; } pthread_mutex_lock(&LOCK_mdl); - if (!can_grant_global_lock(lock_req->type, FALSE)) + if (!global_lock.is_lock_type_compatible(mdl_request->type, FALSE)) { pthread_mutex_unlock(&LOCK_mdl); *retry= TRUE; return TRUE; } - if (!(ticket= alloc_ticket_object(context))) + if (!(ticket= MDL_ticket::create(this, mdl_request->type))) { pthread_mutex_unlock(&LOCK_mdl); return TRUE; } - if (!(lock= (MDL_LOCK*) my_hash_search(&mdl_locks, + if (!(lock= (MDL_lock*) my_hash_search(&mdl_locks, key->ptr(), key->length()))) { - /* Default lock type is MDL_LOCK::SHARED */ - lock= alloc_lock_object(key); + /* Default lock type is MDL_lock::SHARED */ + lock= MDL_lock::create(key); if (!lock || my_hash_insert(&mdl_locks, (uchar*)lock)) { - free_lock_object(lock); - free_ticket_object(ticket); + MDL_lock::destroy(lock); + MDL_ticket::destroy(ticket); pthread_mutex_unlock(&LOCK_mdl); return TRUE; } } - if (can_grant_lock(context, lock, lock_req->type, FALSE)) + if (lock->can_grant_lock(this, mdl_request->type, FALSE)) { + mdl_request->ticket= ticket; lock->granted.push_front(ticket); - context->tickets.push_front(ticket); - ticket->state= MDL_ACQUIRED; - lock_req->ticket= ticket; - ticket->lock= lock; - ticket->type= lock_req->type; - ticket->ctx= context; - if (lock_req->type == MDL_SHARED_UPGRADABLE) + m_tickets.push_front(ticket); + ticket->m_state= MDL_ACQUIRED; + ticket->m_lock= lock; + if (mdl_request->type == MDL_SHARED_UPGRADABLE) global_lock.active_intention_exclusive++; } else @@ -811,7 +855,7 @@ bool mdl_acquire_shared_lock(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req, /* We can't get here if we allocated a new lock. */ DBUG_ASSERT(! lock->is_empty()); *retry= TRUE; - free_ticket_object(ticket); + MDL_ticket::destroy(ticket); } pthread_mutex_unlock(&LOCK_mdl); @@ -820,24 +864,25 @@ bool mdl_acquire_shared_lock(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req, } -static void release_ticket(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket); - - /** - Notify a thread holding a shared metadata lock of a pending exclusive lock. + Notify a thread holding a shared metadata lock which + conflicts with a pending exclusive lock. @param thd Current thread context - @param conf_lock_ticket Conflicting metadata lock + @param conflicting_ticket Conflicting metadata lock @retval TRUE A thread was woken up @retval FALSE Lock is not a shared one or no thread was woken up */ -static bool notify_shared_lock(THD *thd, MDL_LOCK_TICKET *conf_lock_ticket) +static bool notify_shared_lock(THD *thd, MDL_ticket *conflicting_ticket) { bool woke= FALSE; - if (conf_lock_ticket->type != MDL_EXCLUSIVE) - woke= mysql_notify_thread_having_shared_lock(thd, conf_lock_ticket->ctx->thd); + if (conflicting_ticket->is_shared()) + { + THD *conflicting_thd= conflicting_ticket->get_ctx()->get_thd(); + woke= mysql_notify_thread_having_shared_lock(thd, conflicting_thd); + } return woke; } @@ -850,26 +895,26 @@ static bool notify_shared_lock(THD *thd, MDL_LOCK_TICKET *conf_lock_ticket) This is a replacement of lock_table_names(). It is used in RENAME, DROP and other DDL SQL statements. - @param context A context containing requests for exclusive locks - The context may not have other lock requests. + @note The MDL context may not have non-exclusive lock requests + or acquired locks. @retval FALSE Success @retval TRUE Failure */ -bool mdl_acquire_exclusive_locks(MDL_CONTEXT *context) +bool MDL_context::acquire_exclusive_locks() { - MDL_LOCK *lock; + MDL_lock *lock; bool signalled= FALSE; const char *old_msg; - MDL_LOCK_REQUEST *lock_req; - MDL_LOCK_TICKET *ticket; + MDL_request *mdl_request; + MDL_ticket *ticket; st_my_thread_var *mysys_var= my_thread_var; - MDL_CONTEXT::Request_iterator it(context->requests); + Request_iterator it(m_requests); safe_mutex_assert_not_owner(&LOCK_open); - if (context->has_global_shared_lock) + if (m_has_global_shared_lock) { my_error(ER_CANT_UPDATE_WITH_READLOCK, MYF(0)); return TRUE; @@ -877,79 +922,76 @@ bool mdl_acquire_exclusive_locks(MDL_CONTEXT *context) pthread_mutex_lock(&LOCK_mdl); - old_msg= MDL_ENTER_COND(context, mysys_var); + old_msg= MDL_ENTER_COND(m_thd, mysys_var); - while ((lock_req= it++)) + while ((mdl_request= it++)) { - MDL_KEY *key= &lock_req->key; - DBUG_ASSERT(lock_req->type == MDL_EXCLUSIVE && - lock_req->ticket == NULL); + MDL_key *key= &mdl_request->key; + DBUG_ASSERT(mdl_request->type == MDL_EXCLUSIVE && + mdl_request->ticket == NULL); /* Early allocation: ticket is used as a shortcut to the lock. */ - if (!(ticket= alloc_ticket_object(context))) + if (!(ticket= MDL_ticket::create(this, mdl_request->type))) goto err; - if (!(lock= (MDL_LOCK*) my_hash_search(&mdl_locks, + if (!(lock= (MDL_lock*) my_hash_search(&mdl_locks, key->ptr(), key->length()))) { - lock= alloc_lock_object(key); + lock= MDL_lock::create(key); if (!lock || my_hash_insert(&mdl_locks, (uchar*)lock)) { - free_ticket_object(ticket); - free_lock_object(lock); + MDL_ticket::destroy(ticket); + MDL_lock::destroy(lock); goto err; } } - lock_req->ticket= ticket; - ticket->state= MDL_PENDING; - ticket->ctx= context; - ticket->lock= lock; - ticket->type= lock_req->type; + mdl_request->ticket= ticket; lock->waiting.push_front(ticket); + ticket->m_lock= lock; } while (1) { it.rewind(); - while ((lock_req= it++)) + while ((mdl_request= it++)) { - lock= lock_req->ticket->lock; + lock= mdl_request->ticket->m_lock; - if (!can_grant_global_lock(lock_req->type, FALSE)) + if (!global_lock.is_lock_type_compatible(mdl_request->type, FALSE)) { /* - There is an active or pending global shared lock so we have - to wait until it goes away. + Someone owns or wants to acquire the global shared lock so + we have to wait until he goes away. */ signalled= TRUE; break; } - else if (!can_grant_lock(context, lock, lock_req->type, FALSE)) + else if (!lock->can_grant_lock(this, mdl_request->type, FALSE)) { - MDL_LOCK_TICKET *conf_lock_ticket; - MDL_LOCK::Ticket_iterator it(lock->granted); + MDL_ticket *conflicting_ticket; + MDL_lock::Ticket_iterator it(lock->granted); - signalled= (lock->type == MDL_LOCK::EXCLUSIVE); + signalled= (lock->type == MDL_lock::EXCLUSIVE); - while ((conf_lock_ticket= it++)) - signalled|= notify_shared_lock(context->thd, conf_lock_ticket); + while ((conflicting_ticket= it++)) + signalled|= notify_shared_lock(m_thd, conflicting_ticket); break; } } - if (!lock_req) + if (!mdl_request) break; /* There is a shared or exclusive lock on the object. */ - DEBUG_SYNC(context->thd, "mdl_acquire_exclusive_locks_wait"); + DEBUG_SYNC(m_thd, "mdl_acquire_exclusive_locks_wait"); if (signalled) pthread_cond_wait(&COND_mdl, &LOCK_mdl); else { /* - Another thread obtained shared MDL-lock on some table but + Another thread obtained a shared MDL lock on some table but 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. @@ -962,22 +1004,22 @@ bool mdl_acquire_exclusive_locks(MDL_CONTEXT *context) goto err; } it.rewind(); - while ((lock_req= it++)) + while ((mdl_request= it++)) { global_lock.active_intention_exclusive++; - ticket= lock_req->ticket; - lock= ticket->lock; - lock->type= MDL_LOCK::EXCLUSIVE; + ticket= mdl_request->ticket; + lock= ticket->m_lock; + lock->type= MDL_lock::EXCLUSIVE; lock->waiting.remove(ticket); lock->granted.push_front(ticket); - context->tickets.push_front(ticket); - ticket->state= MDL_ACQUIRED; + m_tickets.push_front(ticket); + ticket->m_state= MDL_ACQUIRED; if (lock->cached_object) (*lock->cached_object_release_hook)(lock->cached_object); lock->cached_object= NULL; } /* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */ - MDL_EXIT_COND(context, mysys_var, old_msg); + MDL_EXIT_COND(m_thd, mysys_var, old_msg); return FALSE; err: @@ -986,24 +1028,24 @@ err: Ignore those lock requests which were not made MDL_PENDING. */ it.rewind(); - while ((lock_req= it++) && lock_req->ticket) + while ((mdl_request= it++) && mdl_request->ticket) { - ticket= lock_req->ticket; - DBUG_ASSERT(ticket->state == MDL_PENDING); - lock= ticket->lock; - free_ticket_object(ticket); + ticket= mdl_request->ticket; + DBUG_ASSERT(ticket->m_state == MDL_PENDING); + lock= ticket->m_lock; lock->waiting.remove(ticket); + MDL_ticket::destroy(ticket); /* Reset lock request back to its initial state. */ - lock_req->ticket= NULL; + mdl_request->ticket= NULL; if (lock->is_empty()) { my_hash_delete(&mdl_locks, (uchar *)lock); - free_lock_object(lock); + MDL_lock::destroy(lock); } } /* May be some pending requests for shared locks can be satisfied now. */ pthread_cond_broadcast(&COND_mdl); - MDL_EXIT_COND(context, mysys_var, old_msg); + MDL_EXIT_COND(m_thd, mysys_var, old_msg); return TRUE; } @@ -1014,9 +1056,6 @@ err: Used in ALTER TABLE, when a copy of the table with the new definition has been constructed. - @param context Context to which shared lock belongs - @param ticket Ticket for shared lock to be upgraded - @note In case of failure to upgrade lock (e.g. because upgrader was killed) leaves lock in its original state (locked in shared mode). @@ -1030,46 +1069,46 @@ err: @retval TRUE Failure (thread was killed) */ -bool mdl_upgrade_shared_lock_to_exclusive(MDL_CONTEXT *context, - MDL_LOCK_TICKET *ticket) +bool +MDL_ticket::upgrade_shared_lock_to_exclusive() { - MDL_LOCK *lock= ticket->lock; const char *old_msg; st_my_thread_var *mysys_var= my_thread_var; + THD *thd= m_ctx->get_thd(); - DBUG_ENTER("mdl_upgrade_shared_lock_to_exclusive"); - DEBUG_SYNC(context->thd, "mdl_upgrade_shared_lock_to_exclusive"); + DBUG_ENTER("MDL_ticket::upgrade_shared_lock_to_exclusive"); + DEBUG_SYNC(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 (ticket->type == MDL_EXCLUSIVE) + if (m_type == MDL_EXCLUSIVE) DBUG_RETURN(FALSE); pthread_mutex_lock(&LOCK_mdl); - old_msg= MDL_ENTER_COND(context, mysys_var); + old_msg= MDL_ENTER_COND(thd, mysys_var); /* Since we should have already acquired an intention exclusive global lock this call is only enforcing asserts. */ - DBUG_ASSERT(can_grant_global_lock(MDL_EXCLUSIVE, TRUE)); + DBUG_ASSERT(global_lock.is_lock_type_compatible(MDL_EXCLUSIVE, TRUE)); while (1) { - if (can_grant_lock(context, lock, MDL_EXCLUSIVE, TRUE)) + if (m_lock->can_grant_lock(m_ctx, MDL_EXCLUSIVE, TRUE)) break; bool signalled= FALSE; - MDL_LOCK_TICKET *conf_lock_ticket; - MDL_LOCK::Ticket_iterator it(lock->granted); + MDL_ticket *conflicting_ticket; + MDL_lock::Ticket_iterator it(m_lock->granted); - while ((conf_lock_ticket= it++)) + while ((conflicting_ticket= it++)) { - if (conf_lock_ticket->ctx != context) - signalled|= notify_shared_lock(context->thd, conf_lock_ticket); + if (conflicting_ticket->m_ctx != m_ctx) + signalled|= notify_shared_lock(thd, conflicting_ticket); } if (signalled) @@ -1077,7 +1116,7 @@ bool mdl_upgrade_shared_lock_to_exclusive(MDL_CONTEXT *context, else { /* - Another thread obtained shared MDL-lock on some table but + Another thread obtained a shared MDL lock on some table but 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. @@ -1091,20 +1130,20 @@ bool mdl_upgrade_shared_lock_to_exclusive(MDL_CONTEXT *context, { /* Pending requests for shared locks can be satisfied now. */ pthread_cond_broadcast(&COND_mdl); - MDL_EXIT_COND(context, mysys_var, old_msg); + MDL_EXIT_COND(thd, mysys_var, old_msg); DBUG_RETURN(TRUE); } } - lock->type= MDL_LOCK::EXCLUSIVE; + m_lock->type= MDL_lock::EXCLUSIVE; /* Set the new type of lock in the ticket. */ - ticket->type= MDL_EXCLUSIVE; - if (lock->cached_object) - (*lock->cached_object_release_hook)(lock->cached_object); - lock->cached_object= 0; + m_type= MDL_EXCLUSIVE; + if (m_lock->cached_object) + (*m_lock->cached_object_release_hook)(m_lock->cached_object); + m_lock->cached_object= 0; /* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */ - MDL_EXIT_COND(context, mysys_var, old_msg); + MDL_EXIT_COND(thd, mysys_var, old_msg); DBUG_RETURN(FALSE); } @@ -1121,28 +1160,27 @@ bool mdl_upgrade_shared_lock_to_exclusive(MDL_CONTEXT *context, 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 context [in] The context containing the lock request - @param lock_req [in] The lock request - @param conflict [out] Indicates that conflicting lock exists + @param mdl_request [in] The lock request + @param conflict [out] Indicates that conflicting lock exists @retval TRUE Failure either conflicting lock exists or some error occured (probably OOM). @retval FALSE Success, lock was acquired. FIXME: Compared to lock_table_name_if_not_cached() - it gives sligthly more false negatives. + it gives slightly more false negatives. */ -bool mdl_try_acquire_exclusive_lock(MDL_CONTEXT *context, - MDL_LOCK_REQUEST *lock_req, - bool *conflict) +bool +MDL_context::try_acquire_exclusive_lock(MDL_request *mdl_request, + bool *conflict) { - MDL_LOCK *lock; - MDL_LOCK_TICKET *ticket; - MDL_KEY *key= &lock_req->key; + MDL_lock *lock; + MDL_ticket *ticket; + MDL_key *key= &mdl_request->key; - DBUG_ASSERT(lock_req->type == MDL_EXCLUSIVE && - lock_req->ticket == NULL); + DBUG_ASSERT(mdl_request->type == MDL_EXCLUSIVE && + mdl_request->ticket == NULL); safe_mutex_assert_not_owner(&LOCK_open); @@ -1150,25 +1188,23 @@ bool mdl_try_acquire_exclusive_lock(MDL_CONTEXT *context, pthread_mutex_lock(&LOCK_mdl); - if (!(lock= (MDL_LOCK*) my_hash_search(&mdl_locks, + if (!(lock= (MDL_lock*) my_hash_search(&mdl_locks, key->ptr(), key->length()))) { - ticket= alloc_ticket_object(context); - lock= alloc_lock_object(key); + ticket= MDL_ticket::create(this, mdl_request->type); + lock= MDL_lock::create(key); if (!ticket || !lock || my_hash_insert(&mdl_locks, (uchar*)lock)) { - free_ticket_object(ticket); - free_lock_object(lock); + MDL_ticket::destroy(ticket); + MDL_lock::destroy(lock); goto err; } - lock->type= MDL_LOCK::EXCLUSIVE; + mdl_request->ticket= ticket; + lock->type= MDL_lock::EXCLUSIVE; lock->granted.push_front(ticket); - context->tickets.push_front(ticket); - ticket->state= MDL_ACQUIRED; - lock_req->ticket= ticket; - ticket->ctx= context; - ticket->lock= lock; - ticket->type= lock_req->type; + m_tickets.push_front(ticket); + ticket->m_state= MDL_ACQUIRED; + ticket->m_lock= lock; global_lock.active_intention_exclusive++; pthread_mutex_unlock(&LOCK_mdl); return FALSE; @@ -1184,29 +1220,27 @@ err: /** - Acquire global shared metadata lock. + 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. - @param context Current metadata locking context. - @retval FALSE Success -- the lock was granted. @retval TRUE Failure -- our thread was killed. */ -bool mdl_acquire_global_shared_lock(MDL_CONTEXT *context) +bool MDL_context::acquire_global_shared_lock() { st_my_thread_var *mysys_var= my_thread_var; const char *old_msg; safe_mutex_assert_not_owner(&LOCK_open); - DBUG_ASSERT(!context->has_global_shared_lock); + DBUG_ASSERT(!m_has_global_shared_lock); pthread_mutex_lock(&LOCK_mdl); global_lock.waiting_shared++; - old_msg= MDL_ENTER_COND(context, mysys_var); + old_msg= MDL_ENTER_COND(m_thd, mysys_var); while (!mysys_var->abort && global_lock.active_intention_exclusive) pthread_cond_wait(&COND_mdl, &LOCK_mdl); @@ -1215,13 +1249,13 @@ bool mdl_acquire_global_shared_lock(MDL_CONTEXT *context) if (mysys_var->abort) { /* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */ - MDL_EXIT_COND(context, mysys_var, old_msg); + MDL_EXIT_COND(m_thd, mysys_var, old_msg); return TRUE; } global_lock.active_shared++; - context->has_global_shared_lock= TRUE; + m_has_global_shared_lock= TRUE; /* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */ - MDL_EXIT_COND(context, mysys_var, old_msg); + MDL_EXIT_COND(m_thd, mysys_var, old_msg); return FALSE; } @@ -1235,17 +1269,16 @@ bool mdl_acquire_global_shared_lock(MDL_CONTEXT *context) Does not acquire the locks! - @param context Context with which lock requests are associated. - @retval FALSE Success. One can try to obtain metadata locks. @retval TRUE Failure (thread was killed) */ -bool mdl_wait_for_locks(MDL_CONTEXT *context) +bool +MDL_context::wait_for_locks() { - MDL_LOCK *lock; - MDL_LOCK_REQUEST *lock_req; - MDL_CONTEXT::Request_iterator it(context->requests); + MDL_lock *lock; + MDL_request *mdl_request; + Request_iterator it(m_requests); const char *old_msg; st_my_thread_var *mysys_var= my_thread_var; @@ -1263,34 +1296,34 @@ bool mdl_wait_for_locks(MDL_CONTEXT *context) TODO: investigate situations in which we need to broadcast on COND_mdl because of above scenario. */ - mysql_ha_flush(context->thd); + mysql_ha_flush(m_thd); pthread_mutex_lock(&LOCK_mdl); - old_msg= MDL_ENTER_COND(context, mysys_var); + old_msg= MDL_ENTER_COND(m_thd, mysys_var); it.rewind(); - while ((lock_req= it++)) + while ((mdl_request= it++)) { - MDL_KEY *key= &lock_req->key; - DBUG_ASSERT(lock_req->ticket == NULL); - if (!can_grant_global_lock(lock_req->type, FALSE)) + MDL_key *key= &mdl_request->key; + DBUG_ASSERT(mdl_request->ticket == NULL); + if (!global_lock.is_lock_type_compatible(mdl_request->type, FALSE)) break; /* To avoid starvation we don't wait if we have a conflict against request for MDL_EXCLUSIVE lock. */ - if (is_shared(lock_req) && - (lock= (MDL_LOCK*) my_hash_search(&mdl_locks, key->ptr(), + if (mdl_request->is_shared() && + (lock= (MDL_lock*) my_hash_search(&mdl_locks, key->ptr(), key->length())) && - !can_grant_lock(context, lock, lock_req->type, FALSE)) + !lock->can_grant_lock(this, mdl_request->type, FALSE)) break; } - if (!lock_req) + if (!mdl_request) { pthread_mutex_unlock(&LOCK_mdl); break; } pthread_cond_wait(&COND_mdl, &LOCK_mdl); /* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */ - MDL_EXIT_COND(context, mysys_var, old_msg); + MDL_EXIT_COND(m_thd, mysys_var, old_msg); } return mysys_var->abort; } @@ -1301,18 +1334,18 @@ bool mdl_wait_for_locks(MDL_CONTEXT *context) ownership of which is represented by a lock ticket object. */ -static void release_ticket(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket) +void MDL_context::release_ticket(MDL_ticket *ticket) { - MDL_LOCK *lock= ticket->lock; + MDL_lock *lock= ticket->m_lock; DBUG_ENTER("release_ticket"); DBUG_PRINT("enter", ("db=%s name=%s", lock->key.db_name(), lock->key.table_name())); safe_mutex_assert_owner(&LOCK_mdl); - context->tickets.remove(ticket); + m_tickets.remove(ticket); - switch (ticket->type) + switch (ticket->m_type) { case MDL_SHARED_UPGRADABLE: global_lock.active_intention_exclusive--; @@ -1322,7 +1355,7 @@ static void release_ticket(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket) lock->granted.remove(ticket); break; case MDL_EXCLUSIVE: - lock->type= MDL_LOCK::SHARED; + lock->type= MDL_lock::SHARED; lock->granted.remove(ticket); global_lock.active_intention_exclusive--; break; @@ -1330,7 +1363,7 @@ static void release_ticket(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket) DBUG_ASSERT(0); } - free_ticket_object(ticket); + MDL_ticket::destroy(ticket); if (lock->is_empty()) { @@ -1339,7 +1372,7 @@ static void release_ticket(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket) lock->cached_object)); if (lock->cached_object) (*lock->cached_object_release_hook)(lock->cached_object); - free_lock_object(lock); + MDL_lock::destroy(lock); } DBUG_VOID_RETURN; @@ -1353,42 +1386,39 @@ static void release_ticket(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket) 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. - - @param context The context with which the locks to be released - are associated. */ -void mdl_ticket_release_all(MDL_CONTEXT *context) +void MDL_context::release_all_locks() { - MDL_LOCK_TICKET *ticket; - MDL_CONTEXT::Ticket_iterator it(context->tickets); - DBUG_ENTER("mdl_ticket_release_all"); + MDL_ticket *ticket; + Ticket_iterator it(m_tickets); + DBUG_ENTER("MDL_context::release_all_locks"); safe_mutex_assert_not_owner(&LOCK_open); /* Detach lock tickets from the requests for back off. */ { - MDL_LOCK_REQUEST *lock_req; - MDL_CONTEXT::Request_iterator it(context->requests); + MDL_request *mdl_request; + Request_iterator it(m_requests); - while ((lock_req= it++)) - lock_req->ticket= NULL; + while ((mdl_request= it++)) + mdl_request->ticket= NULL; } - if (context->tickets.is_empty()) + if (m_tickets.is_empty()) DBUG_VOID_RETURN; pthread_mutex_lock(&LOCK_mdl); while ((ticket= it++)) { DBUG_PRINT("info", ("found lock to release ticket=%p", ticket)); - release_ticket(context, ticket); + release_ticket(ticket); } /* Inefficient but will do for a while */ pthread_cond_broadcast(&COND_mdl); pthread_mutex_unlock(&LOCK_mdl); - context->tickets.empty(); + m_tickets.empty(); DBUG_VOID_RETURN; } @@ -1397,17 +1427,16 @@ void mdl_ticket_release_all(MDL_CONTEXT *context) /** Release a lock. - @param context Context containing lock in question @param ticket Lock to be released */ -void mdl_ticket_release(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket) +void MDL_context::release_lock(MDL_ticket *ticket) { - DBUG_ASSERT(context == ticket->ctx); + DBUG_ASSERT(this == ticket->m_ctx); safe_mutex_assert_not_owner(&LOCK_open); pthread_mutex_lock(&LOCK_mdl); - release_ticket(context, ticket); + release_ticket(ticket); pthread_cond_broadcast(&COND_mdl); pthread_mutex_unlock(&LOCK_mdl); } @@ -1417,91 +1446,76 @@ void mdl_ticket_release(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket) Release all locks in the context which correspond to the same name/ object as this lock request, remove lock requests from the context. - @param context Context containing locks in question @param ticket One of the locks for the name/object for which all locks should be released. */ -void mdl_ticket_release_all_for_name(MDL_CONTEXT *context, - MDL_LOCK_TICKET *ticket) +void MDL_context::release_all_locks_for_name(MDL_ticket *name) { - MDL_LOCK *lock; - - /* - We can use MDL_LOCK_TICKET::lock here to identify other locks for the same - object since even though MDL_LOCK object might be reused for different - lock after the first lock for this object have been released we can't - have references to this other MDL_LOCK object in this context. - */ - lock= ticket->lock; + /* Use MDL_ticket::lock to identify other locks for the same object. */ + MDL_lock *lock= name->m_lock; /* Remove matching lock requests from the context. */ - MDL_LOCK_REQUEST *lock_req; - MDL_CONTEXT::Request_iterator it_lock_req(context->requests); + MDL_request *mdl_request; + Request_iterator it_mdl_request(m_requests); - while ((lock_req= it_lock_req++)) + while ((mdl_request= it_mdl_request++)) { - DBUG_ASSERT(lock_req->ticket && lock_req->ticket->state == MDL_ACQUIRED); - if (lock_req->ticket->lock == lock) - mdl_request_remove(context, lock_req); + DBUG_ASSERT(mdl_request->ticket && + mdl_request->ticket->m_state == MDL_ACQUIRED); + + if (mdl_request->ticket->m_lock == lock) + remove_request(mdl_request); } /* Remove matching lock tickets from the context. */ - MDL_LOCK_TICKET *lock_tkt; - MDL_CONTEXT::Ticket_iterator it_lock_tkt(context->tickets); + MDL_ticket *ticket; + Ticket_iterator it_ticket(m_tickets); - while ((lock_tkt= it_lock_tkt++)) + while ((ticket= it_ticket++)) { - DBUG_ASSERT(lock_tkt->state == MDL_ACQUIRED); - if (lock_tkt->lock == lock) - mdl_ticket_release(context, lock_tkt); + 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); } } /** Downgrade an exclusive lock to shared metadata lock. - - @param context A context to which exclusive lock belongs - @param ticket Ticket for exclusive lock to be downgraded */ -void mdl_downgrade_exclusive_lock(MDL_CONTEXT *context, - MDL_LOCK_TICKET *ticket) +void MDL_ticket::downgrade_exclusive_lock() { - MDL_LOCK *lock; - - DBUG_ASSERT(context == ticket->ctx); - safe_mutex_assert_not_owner(&LOCK_open); - if (is_shared(ticket)) + if (is_shared()) return; - lock= ticket->lock; - pthread_mutex_lock(&LOCK_mdl); - lock->type= MDL_LOCK::SHARED; - ticket->type= MDL_SHARED_UPGRADABLE; + m_lock->type= MDL_lock::SHARED; + m_type= MDL_SHARED_UPGRADABLE; pthread_cond_broadcast(&COND_mdl); pthread_mutex_unlock(&LOCK_mdl); } /** - Release global shared metadata lock. - - @param context Current context + Release the global shared metadata lock. */ -void mdl_release_global_shared_lock(MDL_CONTEXT *context) +void MDL_context::release_global_shared_lock() { safe_mutex_assert_not_owner(&LOCK_open); - DBUG_ASSERT(context->has_global_shared_lock); + DBUG_ASSERT(m_has_global_shared_lock); pthread_mutex_lock(&LOCK_mdl); global_lock.active_shared--; - context->has_global_shared_lock= FALSE; + m_has_global_shared_lock= FALSE; pthread_cond_broadcast(&COND_mdl); pthread_mutex_unlock(&LOCK_mdl); } @@ -1511,7 +1525,6 @@ void mdl_release_global_shared_lock(MDL_CONTEXT *context) Auxiliary function which allows to check if we have exclusive lock on the object. - @param context Current context @param type Id of object type @param db Name of the database @param name Name of the object @@ -1520,19 +1533,18 @@ void mdl_release_global_shared_lock(MDL_CONTEXT *context) FALSE otherwise. */ -bool mdl_is_exclusive_lock_owner(MDL_CONTEXT *context, unsigned char type, - const char *db, const char *name) +bool +MDL_context::is_exclusive_lock_owner(unsigned char type, + const char *db, const char *name) { - MDL_KEY key; - MDL_LOCK_TICKET *ticket; - MDL_CONTEXT::Ticket_iterator it(context->tickets); - - key.mdl_key_init(type, db, name); + MDL_key key(type, db, name); + MDL_ticket *ticket; + MDL_context::Ticket_iterator it(m_tickets); while ((ticket= it++)) { - if (ticket->lock->type == MDL_LOCK::EXCLUSIVE && - ticket->lock->key.is_equal(&key)) + if (ticket->m_lock->type == MDL_lock::EXCLUSIVE && + ticket->m_lock->key.is_equal(&key)) break; } @@ -1544,7 +1556,6 @@ bool mdl_is_exclusive_lock_owner(MDL_CONTEXT *context, unsigned char type, Auxiliary function which allows to check if we have some kind of lock on a object. - @param context Current context @param type Id of object type @param db Name of the database @param name Name of the object @@ -1553,18 +1564,17 @@ bool mdl_is_exclusive_lock_owner(MDL_CONTEXT *context, unsigned char type, FALSE otherwise. */ -bool mdl_is_lock_owner(MDL_CONTEXT *context, unsigned char type, - const char *db, const char *name) +bool +MDL_context::is_lock_owner(unsigned char type, + const char *db, const char *name) { - MDL_KEY key; - MDL_LOCK_TICKET *ticket; - MDL_CONTEXT::Ticket_iterator it(context->tickets); - - key.mdl_key_init(type, db, name); + MDL_key key(type, db, name); + MDL_ticket *ticket; + MDL_context::Ticket_iterator it(m_tickets); while ((ticket= it++)) { - if (ticket->lock->key.is_equal(&key)) + if (ticket->m_lock->key.is_equal(&key)) break; } @@ -1576,20 +1586,22 @@ bool mdl_is_lock_owner(MDL_CONTEXT *context, unsigned char type, Check if we have any pending exclusive locks which conflict with existing shared lock. + @pre The ticket must match an acquired lock. + @param ticket Shared lock against which check should be performed. @return TRUE if there are any conflicting locks, FALSE otherwise. */ -bool mdl_has_pending_conflicting_lock(MDL_LOCK_TICKET *ticket) +bool MDL_ticket::has_pending_conflicting_lock() const { bool result; - DBUG_ASSERT(is_shared(ticket)); + DBUG_ASSERT(is_shared()); safe_mutex_assert_not_owner(&LOCK_open); pthread_mutex_lock(&LOCK_mdl); - result= !ticket->lock->waiting.is_empty(); + result= !m_lock->waiting.is_empty(); pthread_mutex_unlock(&LOCK_mdl); return result; } @@ -1598,8 +1610,6 @@ bool mdl_has_pending_conflicting_lock(MDL_LOCK_TICKET *ticket) /** Associate pointer to an opaque object with a lock. - @param ticket Lock ticket for the lock with which the object - should be associated. @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. @@ -1623,24 +1633,24 @@ bool mdl_has_pending_conflicting_lock(MDL_LOCK_TICKET *ticket) lock on this name is released. */ -void mdl_set_cached_object(MDL_LOCK_TICKET *ticket, void *cached_object, - mdl_cached_object_release_hook release_hook) +void +MDL_ticket::set_cached_object(void *cached_object, + mdl_cached_object_release_hook release_hook) { - MDL_LOCK *lock= ticket->lock; DBUG_ENTER("mdl_set_cached_object"); DBUG_PRINT("enter", ("db=%s name=%s cached_object=%p", - lock->key.db_name(), lock->key.table_name(), + m_lock->key.db_name(), m_lock->key.table_name(), cached_object)); /* - TODO: This assumption works now since we do mdl_get_cached_object() - and mdl_set_cached_object() in the same critical section. Once + TODO: This assumption works now since we do get_cached_object() + and set_cached_object() in the same critical section. Once this becomes false we will have to call release_hook here and use additional mutex protecting 'cached_object' member. */ - DBUG_ASSERT(!lock->cached_object); + DBUG_ASSERT(!m_lock->cached_object); - lock->cached_object= cached_object; - lock->cached_object_release_hook= release_hook; + m_lock->cached_object= cached_object; + m_lock->cached_object_release_hook= release_hook; DBUG_VOID_RETURN; } @@ -1654,13 +1664,12 @@ void mdl_set_cached_object(MDL_LOCK_TICKET *ticket, void *cached_object, @return Pointer to an opaque object associated with the lock. */ -void* mdl_get_cached_object(MDL_LOCK_TICKET *ticket) +void *MDL_ticket::get_cached_object() { - return ticket->lock->cached_object; + return m_lock->cached_object; } - /** Releases metadata locks that were acquired after a specific savepoint. @@ -1669,23 +1678,22 @@ void* mdl_get_cached_object(MDL_LOCK_TICKET *ticket) savepoint because other statements that take other special locks cause a implicit commit (ie LOCK TABLES). - @param thd Current thread - @param sv Savepoint + @param mdl_savepont The last acquired MDL lock when the + savepoint was set. */ -void mdl_rollback_to_savepoint(MDL_CONTEXT *ctx, - MDL_LOCK_TICKET *mdl_savepoint) +void MDL_context::rollback_to_savepoint(MDL_ticket *mdl_savepoint) { - MDL_LOCK_TICKET *mdl_lock_ticket; - MDL_CONTEXT::Ticket_iterator it(ctx->tickets); - DBUG_ENTER("mdl_rollback_to_savepoint"); + MDL_ticket *ticket; + Ticket_iterator it(m_tickets); + DBUG_ENTER("MDL_context::rollback_to_savepoint"); - while ((mdl_lock_ticket= it++)) + while ((ticket= it++)) { /* Stop when lock was acquired before this savepoint. */ - if (mdl_lock_ticket == mdl_savepoint) + if (ticket == mdl_savepoint) break; - mdl_ticket_release(ctx, mdl_lock_ticket); + release_lock(ticket); } DBUG_VOID_RETURN; diff --git a/sql/mdl.h b/sql/mdl.h index b5d05b06144..cfa3e671706 100644 --- a/sql/mdl.h +++ b/sql/mdl.h @@ -23,10 +23,9 @@ class THD; -struct MDL_LOCK_REQUEST; -struct MDL_LOCK_TICKET; -struct MDL_LOCK; -struct MDL_CONTEXT; +class MDL_context; +class MDL_lock; +class MDL_ticket; /** Type of metadata lock request. @@ -62,7 +61,7 @@ enum enum_mdl_state { MDL_PENDING, MDL_ACQUIRED }; or "name". */ -class MDL_KEY +class MDL_key { public: const uchar *ptr() const { return (uchar*) m_ptr; } @@ -90,21 +89,34 @@ public: m_db_name_length= (uint) (strmov(m_ptr + 1, db) - m_ptr - 1); m_length= (uint) (strmov(m_ptr + m_db_name_length + 2, name) - m_ptr + 1); } - void mdl_key_init(const MDL_KEY *rhs) + void mdl_key_init(const MDL_key *rhs) { memcpy(m_ptr, rhs->m_ptr, rhs->m_length); m_length= rhs->m_length; m_db_name_length= rhs->m_db_name_length; } - bool is_equal(const MDL_KEY *rhs) const + bool is_equal(const MDL_key *rhs) const { return (m_length == rhs->m_length && memcmp(m_ptr, rhs->m_ptr, m_length) == 0); } + MDL_key(const MDL_key *rhs) + { + mdl_key_init(rhs); + } + MDL_key(char type_arg, const char *db_arg, const char *name_arg) + { + mdl_key_init(type_arg, db_arg, name_arg); + } + MDL_key() {} /* To use when part of MDL_request. */ + private: char m_ptr[MAX_MDLKEY_LENGTH]; uint m_length; uint m_db_name_length; +private: + MDL_key(const MDL_key &); /* not implemented */ + MDL_key &operator=(const MDL_key &); /* not implemented */ }; @@ -125,14 +137,18 @@ struct I_P_List_adapter /** A pending metadata lock request. - A pending lock request or a granted metadata lock share the same abstract - base but are presented individually because they have different allocation + + A lock request and a granted metadata lock are represented by + different classes because they have different allocation sites and hence different lifetimes. The allocation of lock requests is controlled from outside of the MDL subsystem, while allocation of granted locks (tickets) is controlled within the MDL subsystem. + + MDL_request is a C structure, you don't need to call a constructor + or destructor for it. */ -struct MDL_LOCK_REQUEST +struct MDL_request { /** Type of metadata lock. */ enum enum_mdl_type type; @@ -140,51 +156,94 @@ struct MDL_LOCK_REQUEST /** Pointers for participating in the list of lock requests for this context. */ - MDL_LOCK_REQUEST *next_in_context; - MDL_LOCK_REQUEST **prev_in_context; + MDL_request *next_in_context; + MDL_request **prev_in_context; /** A lock is requested based on a fully qualified name and type. */ - MDL_KEY key; + MDL_key key; + + void init(unsigned char type_arg, const char *db_arg, const char *name_arg); + /** Set type of lock request. Can be only applied to pending locks. */ + inline void set_type(enum_mdl_type type_arg) + { + DBUG_ASSERT(ticket == NULL); + type= type_arg; + } + bool is_shared() const { return type < MDL_EXCLUSIVE; } /** Pointer to the lock ticket object for this lock request. Valid only if this lock request is satisfied. */ - MDL_LOCK_TICKET *ticket; + MDL_ticket *ticket; + + static MDL_request *create(unsigned char type, const char *db, + const char *name, MEM_ROOT *root); + }; +typedef void (*mdl_cached_object_release_hook)(void *); + /** A granted metadata lock. - @warning MDL_LOCK_TICKET members are private to the MDL subsystem. + @warning MDL_ticket members are private to the MDL subsystem. @note Multiple shared locks on a same object are represented by a single ticket. The same does not apply for other lock types. */ -struct MDL_LOCK_TICKET +class MDL_ticket { - /** Type of metadata lock. */ - enum enum_mdl_type type; - /** State of the metadata lock ticket. */ - enum enum_mdl_state state; - +public: /** Pointers for participating in the list of lock requests for this context. */ - MDL_LOCK_TICKET *next_in_context; - MDL_LOCK_TICKET **prev_in_context; + MDL_ticket *next_in_context; + MDL_ticket **prev_in_context; /** Pointers for participating in the list of satisfied/pending requests for the lock. */ - MDL_LOCK_TICKET *next_in_lock; - MDL_LOCK_TICKET **prev_in_lock; + MDL_ticket *next_in_lock; + MDL_ticket **prev_in_lock; +public: + bool has_pending_conflicting_lock() const; + + void *get_cached_object(); + void set_cached_object(void *cached_object, + mdl_cached_object_release_hook release_hook); + const MDL_context *get_ctx() const { return m_ctx; } + bool is_shared() const { return m_type < MDL_EXCLUSIVE; } + bool upgrade_shared_lock_to_exclusive(); + void downgrade_exclusive_lock(); +private: + friend class MDL_context; + + MDL_ticket(MDL_context *ctx_arg, enum_mdl_type type_arg) + : m_type(type_arg), + m_state(MDL_PENDING), + m_ctx(ctx_arg), + m_lock(NULL) + {} + + + static MDL_ticket *create(MDL_context *ctx_arg, enum_mdl_type type_arg); + static void destroy(MDL_ticket *ticket); +private: + /** Type of metadata lock. */ + enum enum_mdl_type m_type; + /** State of the metadata lock ticket. */ + enum enum_mdl_state m_state; + /** Context of the owner of the metadata lock ticket. */ - MDL_CONTEXT *ctx; + MDL_context *m_ctx; /** Pointer to the lock object for this lock ticket. */ - MDL_LOCK *lock; + MDL_lock *m_lock; +private: + MDL_ticket(const MDL_ticket &); /* not implemented */ + MDL_ticket &operator=(const MDL_ticket &); /* not implemented */ }; @@ -193,115 +252,86 @@ struct MDL_LOCK_TICKET connection has such a context. */ -struct MDL_CONTEXT +class MDL_context { - typedef I_P_List<MDL_LOCK_REQUEST, - I_P_List_adapter<MDL_LOCK_REQUEST, - &MDL_LOCK_REQUEST::next_in_context, - &MDL_LOCK_REQUEST::prev_in_context> > +public: + typedef I_P_List<MDL_request, + I_P_List_adapter<MDL_request, + &MDL_request::next_in_context, + &MDL_request::prev_in_context> > Request_list; typedef Request_list::Iterator Request_iterator; - typedef I_P_List<MDL_LOCK_TICKET, - I_P_List_adapter<MDL_LOCK_TICKET, - &MDL_LOCK_TICKET::next_in_context, - &MDL_LOCK_TICKET::prev_in_context> > + typedef I_P_List<MDL_ticket, + I_P_List_adapter<MDL_ticket, + &MDL_ticket::next_in_context, + &MDL_ticket::prev_in_context> > Ticket_list; typedef Ticket_list::Iterator Ticket_iterator; - Request_list requests; - Ticket_list tickets; - bool has_global_shared_lock; - THD *thd; -}; + void init(THD *thd); + void destroy(); + void backup_and_reset(MDL_context *backup); + void restore_from_backup(MDL_context *backup); + void merge(MDL_context *source); -void mdl_init(); -void mdl_destroy(); + void add_request(MDL_request *mdl_request); + void remove_request(MDL_request *mdl_request); + void remove_all_requests(); -void mdl_context_init(MDL_CONTEXT *context, THD *thd); -void mdl_context_destroy(MDL_CONTEXT *context); -void mdl_context_backup_and_reset(MDL_CONTEXT *ctx, MDL_CONTEXT *backup); -void mdl_context_restore(MDL_CONTEXT *ctx, MDL_CONTEXT *backup); -void mdl_context_merge(MDL_CONTEXT *target, MDL_CONTEXT *source); + bool acquire_shared_lock(MDL_request *mdl_request, bool *retry); + bool acquire_exclusive_locks(); + bool try_acquire_exclusive_lock(MDL_request *mdl_request, bool *conflict); + bool acquire_global_shared_lock(); -void mdl_request_init(MDL_LOCK_REQUEST *lock_req, unsigned char type, - const char *db, const char *name); -MDL_LOCK_REQUEST *mdl_request_alloc(unsigned char type, const char *db, - const char *name, MEM_ROOT *root); -void mdl_request_add(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req); -void mdl_request_remove(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req); -void mdl_request_remove_all(MDL_CONTEXT *context); + bool wait_for_locks(); -/** - Set type of lock request. Can be only applied to pending locks. -*/ + void release_all_locks(); + void release_all_locks_for_name(MDL_ticket *ticket); + void release_lock(MDL_ticket *ticket); + void release_global_shared_lock(); -inline void mdl_request_set_type(MDL_LOCK_REQUEST *lock_req, enum_mdl_type lock_type) -{ - DBUG_ASSERT(lock_req->ticket == NULL); - lock_req->type= lock_type; -} - -bool mdl_acquire_shared_lock(MDL_CONTEXT *context, MDL_LOCK_REQUEST *lock_req, - bool *retry); -bool mdl_acquire_exclusive_locks(MDL_CONTEXT *context); -bool mdl_upgrade_shared_lock_to_exclusive(MDL_CONTEXT *context, - MDL_LOCK_TICKET *ticket); -bool mdl_try_acquire_exclusive_lock(MDL_CONTEXT *context, - MDL_LOCK_REQUEST *lock_req, - bool *conflict); -bool mdl_acquire_global_shared_lock(MDL_CONTEXT *context); - -bool mdl_wait_for_locks(MDL_CONTEXT *context); - -void mdl_ticket_release_all(MDL_CONTEXT *context); -void mdl_ticket_release_all_for_name(MDL_CONTEXT *context, - MDL_LOCK_TICKET *ticket); -void mdl_ticket_release(MDL_CONTEXT *context, MDL_LOCK_TICKET *ticket); -void mdl_downgrade_exclusive_lock(MDL_CONTEXT *context, - MDL_LOCK_TICKET *ticket); -void mdl_release_global_shared_lock(MDL_CONTEXT *context); - -bool mdl_is_exclusive_lock_owner(MDL_CONTEXT *context, unsigned char type, - const char *db, const char *name); -bool mdl_is_lock_owner(MDL_CONTEXT *context, unsigned char type, - const char *db, const char *name); - -bool mdl_has_pending_conflicting_lock(MDL_LOCK_TICKET *ticket); - -inline bool mdl_has_locks(MDL_CONTEXT *context) -{ - return !context->tickets.is_empty(); -} + bool is_exclusive_lock_owner(unsigned char type, + const char *db, + const char *name); + bool is_lock_owner(unsigned char type, const char *db, const char *name); -inline MDL_LOCK_TICKET *mdl_savepoint(MDL_CONTEXT *ctx) -{ - return ctx->tickets.head(); -} + inline bool has_locks() const + { + return !m_tickets.is_empty(); + } -void mdl_rollback_to_savepoint(MDL_CONTEXT *ctx, - MDL_LOCK_TICKET *mdl_savepoint); + inline MDL_ticket *mdl_savepoint() + { + return m_tickets.head(); + } -/** - Get iterator for walking through all lock requests in the context. -*/ + void rollback_to_savepoint(MDL_ticket *mdl_savepoint); -inline MDL_CONTEXT::Request_iterator -mdl_get_requests(MDL_CONTEXT *ctx) -{ - MDL_CONTEXT::Request_iterator result(ctx->requests); - return result; -} + /** + Get iterator for walking through all lock requests in the context. + */ + inline Request_iterator get_requests() + { + return Request_iterator(m_requests); + } + inline THD *get_thd() const { return m_thd; } +private: + Request_list m_requests; + Ticket_list m_tickets; + bool m_has_global_shared_lock; + THD *m_thd; +private: + void release_ticket(MDL_ticket *ticket); + MDL_ticket *find_ticket(MDL_request *mdl_req); +}; -void mdl_get_tdc_key(MDL_LOCK_TICKET *ticket, LEX_STRING *key); -typedef void (* mdl_cached_object_release_hook)(void *); -void *mdl_get_cached_object(MDL_LOCK_TICKET *ticket); -void mdl_set_cached_object(MDL_LOCK_TICKET *ticket, void *cached_object, - mdl_cached_object_release_hook release_hook); +void mdl_init(); +void mdl_destroy(); /* diff --git a/sql/rpl_rli.cc b/sql/rpl_rli.cc index 0a703475aa7..695b160fc01 100644 --- a/sql/rpl_rli.cc +++ b/sql/rpl_rli.cc @@ -1205,7 +1205,7 @@ void Relay_log_info::clear_tables_to_lock() meta-data locks are stored. So we want to be sure that we don't have any references to this memory left. */ - DBUG_ASSERT(!mdl_has_locks(&(current_thd->mdl_context))); + DBUG_ASSERT(!current_thd->mdl_context.has_locks()); while (tables_to_lock) { diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 5732d7b7079..92019299f6c 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -3981,10 +3981,10 @@ sp_head::add_used_tables_to_table_list(THD *thd, table->prelocking_placeholder= 1; table->belong_to_view= belong_to_view; table->trg_event_map= stab->trg_event_map; - table->mdl_lock_request= mdl_request_alloc(0, table->db, table->table_name, - thd->locked_tables_root ? - thd->locked_tables_root : - thd->mem_root); + table->mdl_request= MDL_request::create(0, table->db, table->table_name, + thd->locked_tables_root ? + thd->locked_tables_root : + thd->mem_root); /* Everyting else should be zeroed */ @@ -4026,10 +4026,10 @@ sp_add_to_query_tables(THD *thd, LEX *lex, table->lock_type= locktype; table->select_lex= lex->current_select; table->cacheable_table= 1; - table->mdl_lock_request= mdl_request_alloc(0, table->db, table->table_name, - thd->locked_tables_root ? - thd->locked_tables_root : - thd->mem_root); + table->mdl_request= MDL_request::create(0, table->db, table->table_name, + thd->locked_tables_root ? + thd->locked_tables_root : + thd->mem_root); lex->add_to_query_tables(table); return table; diff --git a/sql/sql_base.cc b/sql/sql_base.cc index f4f07ad08f7..0441458510f 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -124,7 +124,7 @@ static bool open_new_frm(THD *thd, TABLE_SHARE *share, const char *alias, uint db_stat, uint prgflag, uint ha_open_flags, TABLE *outparam, TABLE_LIST *table_desc, MEM_ROOT *mem_root); -static bool tdc_wait_for_old_versions(THD *thd, MDL_CONTEXT *context); +static bool tdc_wait_for_old_versions(THD *thd, MDL_context *context); static bool has_write_table_with_auto_increment(TABLE_LIST *tables); @@ -449,8 +449,8 @@ TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list, char *key, To be able perform any operation on table we should own some kind of metadata lock on it. */ - DBUG_ASSERT(mdl_is_lock_owner(&thd->mdl_context, 0, table_list->db, - table_list->table_name)); + DBUG_ASSERT(thd->mdl_context.is_lock_owner(0, table_list->db, + table_list->table_name)); /* Read table definition from cache */ if ((share= (TABLE_SHARE*) my_hash_search(&table_def_cache,(uchar*) key, @@ -1050,12 +1050,12 @@ err_with_reopen: */ thd->locked_tables_list.reopen_tables(thd); /* - Since mdl_downgrade_exclusive_lock() won't do anything with shared - metadata lock it is much simplier to go through all open tables rather + Since downgrade_exclusive_lock() won't do anything with shared + metadata lock it is much simpler to go through all open tables rather than picking only those tables that were flushed. */ for (TABLE *tab= thd->open_tables; tab; tab= tab->next) - mdl_downgrade_exclusive_lock(&thd->mdl_context, tab->mdl_lock_ticket); + tab->mdl_ticket->downgrade_exclusive_lock(); } DBUG_RETURN(result); } @@ -1336,7 +1336,7 @@ close_all_tables_for_name(THD *thd, TABLE_SHARE *share, */ void close_thread_tables(THD *thd, - bool skip_mdl) + bool is_back_off) { TABLE *table; DBUG_ENTER("close_thread_tables"); @@ -1478,10 +1478,10 @@ void close_thread_tables(THD *thd, if (thd->open_tables) close_open_tables(thd); - mdl_ticket_release_all(&thd->mdl_context); - if (!skip_mdl) + thd->mdl_context.release_all_locks(); + if (!is_back_off) { - mdl_request_remove_all(&thd->mdl_context); + thd->mdl_context.remove_all_requests(); } DBUG_VOID_RETURN; } @@ -1500,7 +1500,7 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) *table_ptr=table->next; - table->mdl_lock_ticket= NULL; + table->mdl_ticket= NULL; if (table->needs_reopen() || thd->version != refresh_version || !table->db_stat) { @@ -2095,8 +2095,7 @@ bool wait_while_table_is_used(THD *thd, TABLE *table, old_lock_type= table->reginfo.lock_type; mysql_lock_abort(thd, table, TRUE); /* end threads waiting on lock */ - if (mdl_upgrade_shared_lock_to_exclusive(&thd->mdl_context, - table->mdl_lock_ticket)) + if (table->mdl_ticket->upgrade_shared_lock_to_exclusive()) { mysql_lock_downgrade_write(thd, table, old_lock_type); DBUG_RETURN(TRUE); @@ -2279,11 +2278,11 @@ void table_share_release_hook(void *share) static bool open_table_get_mdl_lock(THD *thd, TABLE_LIST *table_list, - MDL_LOCK_REQUEST *mdl_lock_request, + MDL_request *mdl_request, uint flags, enum_open_table_action *action) { - mdl_request_add(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.add_request(mdl_request); if (table_list->open_type) { @@ -2296,10 +2295,10 @@ open_table_get_mdl_lock(THD *thd, TABLE_LIST *table_list, shared locks. This invariant is preserved here and is also enforced by asserts in metadata locking subsystem. */ - mdl_request_set_type(mdl_lock_request, MDL_EXCLUSIVE); - if (mdl_acquire_exclusive_locks(&thd->mdl_context)) + mdl_request->set_type(MDL_EXCLUSIVE); + if (thd->mdl_context.acquire_exclusive_locks()) { - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.remove_request(mdl_request); return 1; } } @@ -2316,16 +2315,16 @@ open_table_get_mdl_lock(THD *thd, TABLE_LIST *table_list, if (flags & MYSQL_OPEN_TAKE_UPGRADABLE_MDL && table_list->lock_type >= TL_WRITE_ALLOW_WRITE) - mdl_request_set_type(mdl_lock_request, MDL_SHARED_UPGRADABLE); + mdl_request->set_type(MDL_SHARED_UPGRADABLE); if (flags & MYSQL_LOCK_IGNORE_FLUSH) - mdl_request_set_type(mdl_lock_request, MDL_SHARED_HIGH_PRIO); + mdl_request->set_type(MDL_SHARED_HIGH_PRIO); - if (mdl_acquire_shared_lock(&thd->mdl_context, mdl_lock_request, &retry)) + if (thd->mdl_context.acquire_shared_lock(mdl_request, &retry)) { if (retry) *action= OT_BACK_OFF_AND_RETRY; else - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.remove_request(mdl_request); return 1; } } @@ -2380,8 +2379,8 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, char key[MAX_DBKEY_LENGTH]; uint key_length; char *alias= table_list->alias; - MDL_LOCK_REQUEST *mdl_lock_request; - MDL_LOCK_TICKET *mdl_lock_ticket; + MDL_request *mdl_request; + MDL_ticket *mdl_ticket; int error; TABLE_SHARE *share; DBUG_ENTER("open_table"); @@ -2517,8 +2516,8 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, TABLES breaks metadata locking protocol (potentially can lead to deadlocks) it should be disallowed. */ - if (mdl_is_lock_owner(&thd->mdl_context, 0, table_list->db, - table_list->table_name)) + if (thd->mdl_context.is_lock_owner(0, table_list->db, + table_list->table_name)) { char path[FN_REFLEN + 1]; enum legacy_db_type not_used; @@ -2560,10 +2559,10 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, This is the normal use case. */ - mdl_lock_request= table_list->mdl_lock_request; + mdl_request= table_list->mdl_request; if (! (flags & MYSQL_OPEN_HAS_MDL_LOCK)) { - if (open_table_get_mdl_lock(thd, table_list, mdl_lock_request, flags, + if (open_table_get_mdl_lock(thd, table_list, mdl_request, flags, action)) DBUG_RETURN(TRUE); } @@ -2573,7 +2572,7 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, open_table_get_mdl_lock as the lock on the table might have been acquired previously (MYSQL_OPEN_HAS_MDL_LOCK). */ - mdl_lock_ticket= mdl_lock_request->ticket; + mdl_ticket= mdl_request->ticket; pthread_mutex_lock(&LOCK_open); @@ -2616,7 +2615,7 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, DBUG_RETURN(FALSE); } - if (!(share= (TABLE_SHARE *)mdl_get_cached_object(mdl_lock_ticket))) + if (!(share= (TABLE_SHARE *) mdl_ticket->get_cached_object())) { if (!(share= get_table_share_with_create(thd, table_list, key, key_length, OPEN_VIEW, @@ -2687,7 +2686,7 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, so we need to increase reference counter; */ reference_table_share(share); - mdl_set_cached_object(mdl_lock_ticket, share, table_share_release_hook); + mdl_ticket->set_cached_object(share, table_share_release_hook); } else { @@ -2796,9 +2795,9 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, lock on this table to shared metadata lock. */ if (table_list->open_type == TABLE_LIST::OPEN_OR_CREATE) - mdl_downgrade_exclusive_lock(&thd->mdl_context, mdl_lock_ticket); + mdl_ticket->downgrade_exclusive_lock(); - table->mdl_lock_ticket= mdl_lock_ticket; + table->mdl_ticket= mdl_ticket; table->next=thd->open_tables; /* Link into simple list */ thd->open_tables=table; @@ -2850,8 +2849,8 @@ err_unlock2: pthread_mutex_unlock(&LOCK_open); if (! (flags & MYSQL_OPEN_HAS_MDL_LOCK)) { - mdl_ticket_release(&thd->mdl_context, mdl_lock_ticket); - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.release_lock(mdl_ticket); + thd->mdl_context.remove_request(mdl_request); } DBUG_RETURN(TRUE); } @@ -2969,7 +2968,7 @@ Locked_tables_list::init_locked_tables(THD *thd) dst_table_list->init_one_table(db, db_len, table_name, table_name_len, alias, src_table_list->table->reginfo.lock_type); - dst_table_list->mdl_lock_request= src_table_list->mdl_lock_request; + dst_table_list->mdl_request= src_table_list->mdl_request; dst_table_list->table= table; memcpy(db, src_table_list->db, db_len + 1); memcpy(table_name, src_table_list->table_name, table_name_len + 1); @@ -3020,8 +3019,6 @@ Locked_tables_list::unlock_locked_tables(THD *thd) thd->locked_tables_mode= LTM_NONE; close_thread_tables(thd); - - mdl_ticket_release_all(&thd->mdl_context); } /* After closing tables we can free memory used for storing lock @@ -3506,21 +3503,21 @@ recover_from_failed_open_table_attempt(THD *thd, TABLE_LIST *table, enum_open_table_action action) { bool result= FALSE; - MDL_LOCK_REQUEST *mdl_lock_request= table->mdl_lock_request; + MDL_request *mdl_request= table->mdl_request; switch (action) { case OT_BACK_OFF_AND_RETRY: - result= (mdl_wait_for_locks(&thd->mdl_context) || + result= (thd->mdl_context.wait_for_locks() || tdc_wait_for_old_versions(thd, &thd->mdl_context)); - mdl_request_remove_all(&thd->mdl_context); + thd->mdl_context.remove_all_requests(); break; case OT_DISCOVER: - mdl_request_set_type(mdl_lock_request, MDL_EXCLUSIVE); - mdl_request_add(&thd->mdl_context, mdl_lock_request); - if (mdl_acquire_exclusive_locks(&thd->mdl_context)) + mdl_request->set_type(MDL_EXCLUSIVE); + thd->mdl_context.add_request(mdl_request); + if (thd->mdl_context.acquire_exclusive_locks()) { - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.remove_request(mdl_request); return TRUE; } pthread_mutex_lock(&LOCK_open); @@ -3530,15 +3527,15 @@ recover_from_failed_open_table_attempt(THD *thd, TABLE_LIST *table, thd->warning_info->clear_warning_info(thd->query_id); thd->clear_error(); // Clear error message - mdl_ticket_release(&thd->mdl_context, mdl_lock_request->ticket); - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.release_lock(mdl_request->ticket); + thd->mdl_context.remove_request(mdl_request); break; case OT_REPAIR: - mdl_request_set_type(mdl_lock_request, MDL_EXCLUSIVE); - mdl_request_add(&thd->mdl_context, mdl_lock_request); - if (mdl_acquire_exclusive_locks(&thd->mdl_context)) + mdl_request->set_type(MDL_EXCLUSIVE); + thd->mdl_context.add_request(mdl_request); + if (thd->mdl_context.acquire_exclusive_locks()) { - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.remove_request(mdl_request); return TRUE; } pthread_mutex_lock(&LOCK_open); @@ -3546,8 +3543,8 @@ recover_from_failed_open_table_attempt(THD *thd, TABLE_LIST *table, pthread_mutex_unlock(&LOCK_open); result= auto_repair_table(thd, table); - mdl_ticket_release(&thd->mdl_context, mdl_lock_request->ticket); - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.release_lock(mdl_request->ticket); + thd->mdl_context.remove_request(mdl_request); break; default: DBUG_ASSERT(0); @@ -4652,7 +4649,7 @@ int lock_tables(THD *thd, TABLE_LIST *tables, uint count, */ -void close_tables_for_reopen(THD *thd, TABLE_LIST **tables, bool skip_mdl) +void close_tables_for_reopen(THD *thd, TABLE_LIST **tables, bool is_back_off) { /* If table list consists only from tables from prelocking set, table list @@ -4664,7 +4661,7 @@ void close_tables_for_reopen(THD *thd, TABLE_LIST **tables, bool skip_mdl) sp_remove_not_own_routines(thd->lex); for (TABLE_LIST *tmp= *tables; tmp; tmp= tmp->next_global) tmp->table= 0; - close_thread_tables(thd, skip_mdl); + close_thread_tables(thd, is_back_off); } @@ -7691,8 +7688,7 @@ void tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type, safe_mutex_assert_owner(&LOCK_open); DBUG_ASSERT(remove_type == TDC_RT_REMOVE_UNUSED || - mdl_is_exclusive_lock_owner(&thd->mdl_context, 0, - db, table_name)); + thd->mdl_context.is_exclusive_lock_owner(0, db, table_name)); key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1; @@ -7739,11 +7735,11 @@ void tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type, @param context Metadata locking context with locks. */ -static bool tdc_wait_for_old_versions(THD *thd, MDL_CONTEXT *context) +static bool tdc_wait_for_old_versions(THD *thd, MDL_context *mdl_context) { TABLE_SHARE *share; const char *old_msg; - MDL_LOCK_REQUEST *lock_req; + MDL_request *mdl_request; while (!thd->killed) { @@ -7756,16 +7752,16 @@ static bool tdc_wait_for_old_versions(THD *thd, MDL_CONTEXT *context) mysql_ha_flush(thd); pthread_mutex_lock(&LOCK_open); - MDL_CONTEXT::Request_iterator it= mdl_get_requests(context); - while ((lock_req= it++)) + MDL_context::Request_iterator it= mdl_context->get_requests(); + while ((mdl_request= it++)) { - if ((share= get_cached_table_share(lock_req->key.db_name(), - lock_req->key.table_name())) && + if ((share= get_cached_table_share(mdl_request->key.db_name(), + mdl_request->key.table_name())) && share->version != refresh_version && !share->used_tables.is_empty()) break; } - if (!lock_req) + if (!mdl_request) { pthread_mutex_unlock(&LOCK_open); break; @@ -8169,8 +8165,8 @@ void close_performance_schema_table(THD *thd, Open_tables_state *backup) pthread_mutex_unlock(&LOCK_open); - mdl_ticket_release_all(&thd->mdl_context); - mdl_request_remove_all(&thd->mdl_context); + thd->mdl_context.release_all_locks(); + thd->mdl_context.remove_all_requests(); thd->restore_backup_open_tables_state(backup); } diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 4f1777c152c..654d7ad718d 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -1050,8 +1050,8 @@ THD::~THD() if (!cleanup_done) cleanup(); - mdl_context_destroy(&mdl_context); - mdl_context_destroy(&handler_mdl_context); + mdl_context.destroy(); + handler_mdl_context.destroy(); ha_close_connection(this); plugin_thdvar_cleanup(this); @@ -3033,8 +3033,8 @@ void THD::restore_backup_open_tables_state(Open_tables_state *backup) lock == 0 && locked_tables_mode == LTM_NONE && m_reprepare_observer == NULL); - mdl_context_destroy(&mdl_context); - mdl_context_destroy(&handler_mdl_context); + mdl_context.destroy(); + handler_mdl_context.destroy(); set_open_tables_state(backup); DBUG_VOID_RETURN; diff --git a/sql/sql_class.h b/sql/sql_class.h index 80817fa4c12..92b9f9f4611 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -780,7 +780,7 @@ struct st_savepoint { uint length; Ha_trx_info *ha_list; /** Last acquired lock before this savepoint was set. */ - MDL_LOCK_TICKET *mdl_savepoint; + MDL_ticket *mdl_savepoint; }; enum xa_states {XA_NOTR=0, XA_ACTIVE, XA_IDLE, XA_PREPARED, XA_ROLLBACK_ONLY}; @@ -983,8 +983,8 @@ public: */ uint state_flags; - MDL_CONTEXT mdl_context; - MDL_CONTEXT handler_mdl_context; + MDL_context mdl_context; + MDL_context handler_mdl_context; /** This constructor initializes Open_tables_state instance which can only @@ -1015,8 +1015,8 @@ public: locked_tables_mode= LTM_NONE; state_flags= 0U; m_reprepare_observer= NULL; - mdl_context_init(&mdl_context, thd); - mdl_context_init(&handler_mdl_context, thd); + mdl_context.init(thd); + handler_mdl_context.init(thd); } }; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index c96594a8032..fc92dbee0a1 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -1100,7 +1100,7 @@ bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok) TABLE *table; bool error; uint path_length; - MDL_LOCK_REQUEST *mdl_lock_request= NULL; + MDL_request *mdl_request= NULL; DBUG_ENTER("mysql_truncate"); bzero((char*) &create_info,sizeof(create_info)); @@ -1175,13 +1175,13 @@ bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok) tries to get table enging and therefore accesses table in some way without holding any kind of meta-data lock. */ - mdl_lock_request= mdl_request_alloc(0, table_list->db, - table_list->table_name, thd->mem_root); - mdl_request_set_type(mdl_lock_request, MDL_EXCLUSIVE); - mdl_request_add(&thd->mdl_context, mdl_lock_request); - if (mdl_acquire_exclusive_locks(&thd->mdl_context)) + mdl_request= MDL_request::create(0, table_list->db, + table_list->table_name, thd->mem_root); + mdl_request->set_type(MDL_EXCLUSIVE); + thd->mdl_context.add_request(mdl_request); + if (thd->mdl_context.acquire_exclusive_locks()) { - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.remove_request(mdl_request); DBUG_RETURN(TRUE); } pthread_mutex_lock(&LOCK_open); @@ -1212,18 +1212,18 @@ end: write_bin_log(thd, TRUE, thd->query(), thd->query_length()); my_ok(thd); // This should return record count } - if (mdl_lock_request) + if (mdl_request) { - mdl_ticket_release(&thd->mdl_context, mdl_lock_request->ticket); - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.release_lock(mdl_request->ticket); + thd->mdl_context.remove_request(mdl_request); } } else if (error) { - if (mdl_lock_request) + if (mdl_request) { - mdl_ticket_release(&thd->mdl_context, mdl_lock_request->ticket); - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.release_lock(mdl_request->ticket); + thd->mdl_context.remove_request(mdl_request); } } DBUG_RETURN(error); diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc index 83c5c60dc01..4b07a00c779 100644 --- a/sql/sql_handler.cc +++ b/sql/sql_handler.cc @@ -125,7 +125,7 @@ static void mysql_ha_hash_free(TABLE_LIST *tables) static void mysql_ha_close_table(THD *thd, TABLE_LIST *tables) { TABLE **table_ptr; - MDL_LOCK_TICKET *mdl_lock_ticket; + MDL_ticket *mdl_ticket; /* Though we could take the table pointer from hash_tables->table, @@ -141,7 +141,7 @@ static void mysql_ha_close_table(THD *thd, TABLE_LIST *tables) if (*table_ptr) { (*table_ptr)->file->ha_index_or_rnd_end(); - mdl_lock_ticket= (*table_ptr)->mdl_lock_ticket; + mdl_ticket= (*table_ptr)->mdl_ticket; pthread_mutex_lock(&LOCK_open); if (close_thread_table(thd, table_ptr)) { @@ -149,8 +149,8 @@ static void mysql_ha_close_table(THD *thd, TABLE_LIST *tables) broadcast_refresh(); } pthread_mutex_unlock(&LOCK_open); - mdl_ticket_release(&thd->handler_mdl_context, mdl_lock_ticket); - mdl_request_remove(&thd->handler_mdl_context, tables->mdl_lock_request); + thd->handler_mdl_context.release_lock(mdl_ticket); + thd->handler_mdl_context.remove_request(tables->mdl_request); } else if (tables->table) { @@ -194,8 +194,8 @@ bool mysql_ha_open(THD *thd, TABLE_LIST *tables, bool reopen) uint dblen, namelen, aliaslen, counter; int error; TABLE *backup_open_tables; - MDL_CONTEXT backup_mdl_context; - MDL_LOCK_REQUEST *mdl_lock_request; + MDL_context backup_mdl_context; + MDL_request *mdl_request; DBUG_ENTER("mysql_ha_open"); DBUG_PRINT("enter",("'%s'.'%s' as '%s' reopen: %d", tables->db, tables->table_name, tables->alias, @@ -246,7 +246,7 @@ bool mysql_ha_open(THD *thd, TABLE_LIST *tables, bool reopen) &db, (uint) dblen, &name, (uint) namelen, &alias, (uint) aliaslen, - &mdl_lock_request, sizeof(MDL_LOCK_REQUEST), + &mdl_request, sizeof(MDL_request), NullS))) { DBUG_PRINT("exit",("ERROR")); @@ -260,8 +260,8 @@ bool mysql_ha_open(THD *thd, TABLE_LIST *tables, bool reopen) memcpy(hash_tables->db, tables->db, dblen); memcpy(hash_tables->table_name, tables->table_name, namelen); memcpy(hash_tables->alias, tables->alias, aliaslen); - mdl_request_init(mdl_lock_request, 0, db, name); - hash_tables->mdl_lock_request= mdl_lock_request; + mdl_request->init(0, db, name); + hash_tables->mdl_request= mdl_request; /* add to hash */ if (my_hash_insert(&thd->handler_tables_hash, (uchar*) hash_tables)) @@ -289,7 +289,7 @@ bool mysql_ha_open(THD *thd, TABLE_LIST *tables, bool reopen) */ backup_open_tables= thd->open_tables; thd->open_tables= NULL; - mdl_context_backup_and_reset(&thd->mdl_context, &backup_mdl_context); + thd->mdl_context.backup_and_reset(&backup_mdl_context); /* open_tables() will set 'hash_tables->table' if successful. @@ -328,10 +328,10 @@ bool mysql_ha_open(THD *thd, TABLE_LIST *tables, bool reopen) thd->handler_tables= thd->open_tables; } } - mdl_context_merge(&thd->handler_mdl_context, &thd->mdl_context); + thd->handler_mdl_context.merge(&thd->mdl_context); thd->open_tables= backup_open_tables; - mdl_context_restore(&thd->mdl_context, &backup_mdl_context); + thd->mdl_context.restore_from_backup(&backup_mdl_context); if (error) goto err; @@ -800,11 +800,11 @@ void mysql_ha_flush(THD *thd) { hash_tables= (TABLE_LIST*) my_hash_element(&thd->handler_tables_hash, i); /* - TABLE::mdl_lock_ticket is 0 for temporary tables so we need extra check. + TABLE::mdl_ticket is 0 for temporary tables so we need extra check. */ if (hash_tables->table && - (hash_tables->table->mdl_lock_ticket && - mdl_has_pending_conflicting_lock(hash_tables->table->mdl_lock_ticket) || + (hash_tables->table->mdl_ticket && + hash_tables->table->mdl_ticket->has_pending_conflicting_lock() || hash_tables->table->needs_reopen())) mysql_ha_close_table(thd, hash_tables); } diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 4166267c5a9..579fbcea4ea 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -5977,9 +5977,9 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd, ptr->next_name_resolution_table= NULL; /* Link table in global list (all used tables) */ lex->add_to_query_tables(ptr); - ptr->mdl_lock_request= - mdl_request_alloc(0, ptr->db, ptr->table_name, thd->locked_tables_root ? - thd->locked_tables_root : thd->mem_root); + ptr->mdl_request= + MDL_request::create(0, ptr->db, ptr->table_name, thd->locked_tables_root ? + thd->locked_tables_root : thd->mem_root); DBUG_RETURN(ptr); } diff --git a/sql/sql_plist.h b/sql/sql_plist.h index 91fa9ef52bb..b0a0bb016d0 100644 --- a/sql/sql_plist.h +++ b/sql/sql_plist.h @@ -79,6 +79,7 @@ public: **B::prev_ptr(a)= next; } inline T* head() { return first; } + inline const T *head() const { return first; } void swap(I_P_List<T,B> &rhs) { swap_variables(T *, first, rhs.first); diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 440efbf7c6d..9727498cb2e 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -3050,7 +3050,7 @@ uint get_table_open_method(TABLE_LIST *tables, Acquire high priority share metadata lock on a table. @param thd Thread context. - @param mdl_lock_req Pointer to memory to be used for MDL_LOCK_REQUEST + @param mdl_request Pointer to memory to be used for MDL_request object for a lock request. @param table Table list element for the table @@ -3065,23 +3065,23 @@ uint get_table_open_method(TABLE_LIST *tables, */ static bool -acquire_high_prio_shared_mdl_lock(THD *thd, MDL_LOCK_REQUEST *mdl_lock_req, +acquire_high_prio_shared_mdl_lock(THD *thd, MDL_request *mdl_request, TABLE_LIST *table) { bool retry; - mdl_request_init(mdl_lock_req, 0, table->db, table->table_name); - table->mdl_lock_request= mdl_lock_req; - mdl_request_add(&thd->mdl_context, mdl_lock_req); - mdl_request_set_type(mdl_lock_req, MDL_SHARED_HIGH_PRIO); + mdl_request->init(0, table->db, table->table_name); + table->mdl_request= mdl_request; + thd->mdl_context.add_request(mdl_request); + mdl_request->set_type(MDL_SHARED_HIGH_PRIO); while (1) { - if (mdl_acquire_shared_lock(&thd->mdl_context, mdl_lock_req, &retry)) + if (thd->mdl_context.acquire_shared_lock(mdl_request, &retry)) { - if (!retry || mdl_wait_for_locks(&thd->mdl_context)) + if (!retry || thd->mdl_context.wait_for_locks()) { - mdl_request_remove_all(&thd->mdl_context); + thd->mdl_context.remove_all_requests(); return TRUE; } continue; @@ -3123,7 +3123,7 @@ static int fill_schema_table_from_frm(THD *thd,TABLE *table, char key[MAX_DBKEY_LENGTH]; uint key_length; char db_name_buff[NAME_LEN + 1], table_name_buff[NAME_LEN + 1]; - MDL_LOCK_REQUEST mdl_lock_request; + MDL_request mdl_request; bzero((char*) &table_list, sizeof(TABLE_LIST)); bzero((char*) &tbl, sizeof(TABLE)); @@ -3153,7 +3153,7 @@ static int fill_schema_table_from_frm(THD *thd,TABLE *table, simply obtaining internal lock of data-dictionary (ATM it is LOCK_open) instead of obtaning full-blown metadata lock. */ - if (acquire_high_prio_shared_mdl_lock(thd, &mdl_lock_request, &table_list)) + if (acquire_high_prio_shared_mdl_lock(thd, &mdl_request, &table_list)) { /* Some error occured (most probably we have been killed while @@ -3213,9 +3213,8 @@ err_share: err_unlock: pthread_mutex_unlock(&LOCK_open); -err: - mdl_ticket_release(&thd->mdl_context, mdl_lock_request.ticket); - mdl_request_remove(&thd->mdl_context, &mdl_lock_request); + thd->mdl_context.release_lock(mdl_request.ticket); + thd->mdl_context.remove_request(&mdl_request); thd->clear_error(); return res; } diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 54f06935d3b..42ae0a89458 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -1910,7 +1910,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, Since we don't acquire metadata lock if we have found temporary table, we should do something to avoid releasing it at the end. */ - table->mdl_lock_request= NULL; + table->mdl_request= NULL; } else { @@ -1923,7 +1923,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, table->table_name); if (!table->table) DBUG_RETURN(1); - table->mdl_lock_request->ticket= table->table->mdl_lock_ticket; + table->mdl_request->ticket= table->table->mdl_ticket; } } } @@ -2202,15 +2202,14 @@ err: } for (table= tables; table; table= table->next_local) { - if (table->mdl_lock_request) + if (table->mdl_request) { /* Under LOCK TABLES we may have several instances of table open and locked and therefore have to remove several metadata lock requests associated with them. */ - mdl_ticket_release_all_for_name(&thd->mdl_context, - table->mdl_lock_request->ticket); + thd->mdl_context.release_all_locks_for_name(table->mdl_request->ticket); } } } @@ -4108,28 +4107,28 @@ warn: static bool lock_table_name_if_not_cached(THD *thd, const char *db, const char *table_name, - MDL_LOCK_REQUEST **lock_req) + MDL_request **mdl_request) { bool conflict; - if (!(*lock_req= mdl_request_alloc(0, db, table_name, thd->mem_root))) + if (!(*mdl_request= MDL_request::create(0, db, table_name, thd->mem_root))) return TRUE; - mdl_request_set_type(*lock_req, MDL_EXCLUSIVE); - mdl_request_add(&thd->mdl_context, *lock_req); - if (mdl_try_acquire_exclusive_lock(&thd->mdl_context, *lock_req, &conflict)) + (*mdl_request)->set_type(MDL_EXCLUSIVE); + thd->mdl_context.add_request(*mdl_request); + if (thd->mdl_context.try_acquire_exclusive_lock(*mdl_request, &conflict)) { /* To simplify our life under LOCK TABLES we remove unsatisfied lock request from the context. */ - mdl_request_remove(&thd->mdl_context, *lock_req); + thd->mdl_context.remove_request(*mdl_request); if (!conflict) { /* Probably OOM. */ return TRUE; } else - *lock_req= NULL; + *mdl_request= NULL; } return FALSE; } @@ -4145,7 +4144,7 @@ bool mysql_create_table(THD *thd, const char *db, const char *table_name, bool internal_tmp_table, uint select_field_count) { - MDL_LOCK_REQUEST *target_lock_req= NULL; + MDL_request *target_mdl_request= NULL; bool result; DBUG_ENTER("mysql_create_table"); @@ -4168,12 +4167,12 @@ bool mysql_create_table(THD *thd, const char *db, const char *table_name, if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE)) { - if (lock_table_name_if_not_cached(thd, db, table_name, &target_lock_req)) + if (lock_table_name_if_not_cached(thd, db, table_name, &target_mdl_request)) { result= TRUE; goto unlock; } - if (!target_lock_req) + if (!target_mdl_request) { if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) { @@ -4199,10 +4198,10 @@ bool mysql_create_table(THD *thd, const char *db, const char *table_name, select_field_count); unlock: - if (target_lock_req) + if (target_mdl_request) { - mdl_ticket_release(&thd->mdl_context, target_lock_req->ticket); - mdl_request_remove(&thd->mdl_context, target_lock_req); + thd->mdl_context.release_lock(target_mdl_request->ticket); + thd->mdl_context.remove_request(target_mdl_request); } pthread_mutex_lock(&LOCK_lock_db); if (!--creating_table && creating_database) @@ -4367,7 +4366,7 @@ static int prepare_for_repair(THD *thd, TABLE_LIST *table_list, char from[FN_REFLEN],tmp[FN_REFLEN+32]; const char **ext; MY_STAT stat_info; - MDL_LOCK_REQUEST *mdl_lock_request= NULL; + MDL_request *mdl_request= NULL; enum enum_open_table_action ot_action_unused; DBUG_ENTER("prepare_for_repair"); uint reopen_for_repair_flags= (MYSQL_LOCK_IGNORE_FLUSH | @@ -4386,13 +4385,13 @@ static int prepare_for_repair(THD *thd, TABLE_LIST *table_list, uint key_length; key_length= create_table_def_key(thd, key, table_list, 0); - mdl_lock_request= mdl_request_alloc(0, table_list->db, - table_list->table_name, thd->mem_root); - mdl_request_set_type(mdl_lock_request, MDL_EXCLUSIVE); - mdl_request_add(&thd->mdl_context, mdl_lock_request); - if (mdl_acquire_exclusive_locks(&thd->mdl_context)) + mdl_request= MDL_request::create(0, table_list->db, + table_list->table_name, thd->mem_root); + mdl_request->set_type(MDL_EXCLUSIVE); + thd->mdl_context.add_request(mdl_request); + if (thd->mdl_context.acquire_exclusive_locks()) { - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.remove_request(mdl_request); DBUG_RETURN(0); } @@ -4412,7 +4411,7 @@ static int prepare_for_repair(THD *thd, TABLE_LIST *table_list, } pthread_mutex_unlock(&LOCK_open); table= &tmp_table; - table_list->mdl_lock_request= mdl_lock_request; + table_list->mdl_request= mdl_request; } /* A MERGE table must not come here. */ @@ -4523,10 +4522,10 @@ end: pthread_mutex_unlock(&LOCK_open); } /* In case of a temporary table there will be no metadata lock. */ - if (error && mdl_lock_request) + if (error && mdl_request) { - mdl_ticket_release(&thd->mdl_context, mdl_lock_request->ticket); - mdl_request_remove(&thd->mdl_context, mdl_lock_request); + thd->mdl_context.release_lock(mdl_request->ticket); + thd->mdl_context.remove_request(mdl_request); } DBUG_RETURN(error); } @@ -5229,7 +5228,7 @@ bool mysql_create_like_schema_frm(THD* thd, TABLE_LIST* schema_table, bool mysql_create_like_table(THD* thd, TABLE_LIST* table, TABLE_LIST* src_table, HA_CREATE_INFO *create_info) { - MDL_LOCK_REQUEST *target_lock_req= NULL; + MDL_request *target_mdl_request= NULL; char src_path[FN_REFLEN], dst_path[FN_REFLEN + 1]; uint dst_path_length; char *db= table->db; @@ -5286,9 +5285,9 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, TABLE_LIST* src_table, } else { - if (lock_table_name_if_not_cached(thd, db, table_name, &target_lock_req)) + if (lock_table_name_if_not_cached(thd, db, table_name, &target_mdl_request)) goto err; - if (!target_lock_req) + if (!target_mdl_request) goto table_exists; dst_path_length= build_table_filename(dst_path, sizeof(dst_path) - 1, db, table_name, reg_ext, 0); @@ -5298,7 +5297,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, TABLE_LIST* src_table, Make the metadata lock available to open_table() called to reopen the table down the road. */ - table->mdl_lock_request= target_lock_req; + table->mdl_request= target_mdl_request; } DBUG_EXECUTE_IF("sleep_create_like_before_copy", my_sleep(6000000);); @@ -5469,10 +5468,10 @@ binlog: res= FALSE; err: - if (target_lock_req) + if (target_mdl_request) { - mdl_ticket_release(&thd->mdl_context, target_lock_req->ticket); - mdl_request_remove(&thd->mdl_context, target_lock_req); + thd->mdl_context.release_lock(target_mdl_request->ticket); + thd->mdl_context.remove_request(target_mdl_request); } DBUG_RETURN(res); } @@ -6411,8 +6410,8 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, uint order_num, ORDER *order, bool ignore) { TABLE *table, *new_table= 0; - MDL_LOCK_TICKET *mdl_lock_ticket; - MDL_LOCK_REQUEST *target_lock_req= NULL; + MDL_ticket *mdl_ticket; + MDL_request *target_mdl_request= NULL; int error= 0; char tmp_name[80],old_name[32],new_name_buff[FN_REFLEN + 1]; char new_alias_buff[FN_REFLEN], *table_name, *db, *new_alias, *alias; @@ -6583,7 +6582,7 @@ view_err: MYSQL_OPEN_TAKE_UPGRADABLE_MDL))) DBUG_RETURN(TRUE); table->use_all_columns(); - mdl_lock_ticket= table->mdl_lock_ticket; + mdl_ticket= table->mdl_ticket; /* Prohibit changing of the UNION list of a non-temporary MERGE table @@ -6636,9 +6635,9 @@ view_err: else { if (lock_table_name_if_not_cached(thd, new_db, new_name, - &target_lock_req)) + &target_mdl_request)) DBUG_RETURN(TRUE); - if (!target_lock_req) + if (!target_mdl_request) { my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); DBUG_RETURN(TRUE); @@ -6831,12 +6830,12 @@ view_err: */ if (new_name != table_name || new_db != db) { - mdl_ticket_release(&thd->mdl_context, target_lock_req->ticket); - mdl_request_remove(&thd->mdl_context, target_lock_req); - mdl_ticket_release_all_for_name(&thd->mdl_context, mdl_lock_ticket); + thd->mdl_context.release_lock(target_mdl_request->ticket); + thd->mdl_context.remove_request(target_mdl_request); + thd->mdl_context.release_all_locks_for_name(mdl_ticket); } else - mdl_downgrade_exclusive_lock(&thd->mdl_context, mdl_lock_ticket); + mdl_ticket->downgrade_exclusive_lock(); } DBUG_RETURN(error); } @@ -7069,7 +7068,7 @@ view_err: #ifdef WITH_PARTITION_STORAGE_ENGINE if (fast_alter_partition) { - DBUG_ASSERT(!target_lock_req); + DBUG_ASSERT(!target_mdl_request); DBUG_RETURN(fast_alter_partition_table(thd, table, alter_info, create_info, table_list, db, table_name, @@ -7436,7 +7435,7 @@ view_err: table_list->table_name_length= strlen(new_name); table_list->db= new_db; table_list->db_length= strlen(new_db); - table_list->mdl_lock_request= target_lock_req; + table_list->mdl_request= target_mdl_request; } else { @@ -7445,7 +7444,7 @@ view_err: points to a different instance than the one set initially to request the lock. */ - table_list->mdl_lock_request->ticket= mdl_lock_ticket; + table_list->mdl_request->ticket= mdl_ticket; } if (open_table(thd, table_list, thd->mem_root, &ot_action_unused, MYSQL_OPEN_REOPEN)) @@ -7511,12 +7510,12 @@ view_err: { if ((new_name != table_name || new_db != db)) { - mdl_ticket_release(&thd->mdl_context, target_lock_req->ticket); - mdl_request_remove(&thd->mdl_context, target_lock_req); - mdl_ticket_release_all_for_name(&thd->mdl_context, mdl_lock_ticket); + thd->mdl_context.release_lock(target_mdl_request->ticket); + thd->mdl_context.remove_request(target_mdl_request); + thd->mdl_context.release_all_locks_for_name(mdl_ticket); } else - mdl_downgrade_exclusive_lock(&thd->mdl_context, mdl_lock_ticket); + mdl_ticket->downgrade_exclusive_lock(); } end_temporary: @@ -7571,10 +7570,10 @@ err: alter_info->datetime_field->field_name); thd->abort_on_warning= save_abort_on_warning; } - if (target_lock_req) + if (target_mdl_request) { - mdl_ticket_release(&thd->mdl_context, target_lock_req->ticket); - mdl_request_remove(&thd->mdl_context, target_lock_req); + thd->mdl_context.release_lock(target_mdl_request->ticket); + thd->mdl_context.remove_request(target_mdl_request); } DBUG_RETURN(TRUE); @@ -7586,12 +7585,12 @@ err_with_mdl: tables and release the exclusive metadata lock. */ thd->locked_tables_list.unlink_all_closed_tables(); - if (target_lock_req) + if (target_mdl_request) { - mdl_ticket_release(&thd->mdl_context, target_lock_req->ticket); - mdl_request_remove(&thd->mdl_context, target_lock_req); + thd->mdl_context.release_lock(target_mdl_request->ticket); + thd->mdl_context.remove_request(target_mdl_request); } - mdl_ticket_release_all_for_name(&thd->mdl_context, mdl_lock_ticket); + thd->mdl_context.release_all_locks_for_name(mdl_ticket); DBUG_RETURN(TRUE); } /* mysql_alter_table */ diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index 894204fec58..93b89b869d6 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -329,7 +329,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) String stmt_query; bool need_start_waiting= FALSE; bool lock_upgrade_done= FALSE; - MDL_LOCK_TICKET *mdl_lock_ticket= NULL; + MDL_ticket *mdl_ticket= NULL; DBUG_ENTER("mysql_create_or_drop_trigger"); @@ -465,7 +465,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) table= tables->table; /* Later on we will need it to downgrade the lock */ - mdl_lock_ticket= table->mdl_lock_ticket; + mdl_ticket= table->mdl_ticket; if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN)) goto end; @@ -513,7 +513,7 @@ end: TABLE instance created by open_n_lock_single_table() and metadata lock. */ if (thd->locked_tables_mode && tables && lock_upgrade_done) - mdl_downgrade_exclusive_lock(&thd->mdl_context, mdl_lock_ticket); + mdl_ticket->downgrade_exclusive_lock(); if (need_start_waiting) start_waiting_global_read_lock(thd); @@ -1882,7 +1882,7 @@ bool Table_triggers_list::change_table_name(THD *thd, const char *db, In the future, only an exclusive metadata lock will be enough. */ #ifndef DBUG_OFF - if (mdl_is_exclusive_lock_owner(&thd->mdl_context, 0, db, old_table)) + if (thd->mdl_context.is_exclusive_lock_owner(0, db, old_table)) safe_mutex_assert_owner(&LOCK_open); #endif diff --git a/sql/table.cc b/sql/table.cc index b1988faf722..8d28514e912 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -4814,8 +4814,8 @@ size_t max_row_length(TABLE *table, const uchar *data) void alloc_mdl_requests(TABLE_LIST *table_list, MEM_ROOT *root) { for ( ; table_list ; table_list= table_list->next_global) - table_list->mdl_lock_request= - mdl_request_alloc(0, table_list->db, table_list->table_name, root); + table_list->mdl_request= + MDL_request::create(0, table_list->db, table_list->table_name, root); } diff --git a/sql/table.h b/sql/table.h index 3b125217338..02492ea4e81 100644 --- a/sql/table.h +++ b/sql/table.h @@ -30,8 +30,8 @@ class st_select_lex; class partition_info; class COND_EQUAL; class Security_context; -struct MDL_LOCK_REQUEST; -struct MDL_LOCK_TICKET; +struct MDL_request; +struct MDL_ticket; /*************************************************************************/ @@ -814,7 +814,7 @@ public: partition_info *part_info; /* Partition related information */ bool no_partitions_used; /* If true, all partitions have been pruned away */ #endif - MDL_LOCK_TICKET *mdl_lock_ticket; + MDL_ticket *mdl_ticket; bool fill_item_list(List<Item> *item_list) const; void reset_item_list(List<Item> *item_list) const; @@ -1417,7 +1417,7 @@ struct TABLE_LIST uint table_open_method; enum enum_schema_table_state schema_table_state; - MDL_LOCK_REQUEST *mdl_lock_request; + MDL_request *mdl_request; void calc_md5(char *buffer); void set_underlying_merge(); |