MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
mdl.h
Go to the documentation of this file.
1#ifndef MDL_H
2#define MDL_H
3/* Copyright (c) 2009, 2025, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <assert.h>
27#include <string.h>
28#include <sys/types.h>
29#include <algorithm>
30#include <functional>
31#include <new>
32#include <unordered_map>
33
34#include "my_alloc.h"
35#include "my_compiler.h"
36#include "strmake.h"
37
38#include "my_inttypes.h"
39#include "my_psi_config.h"
40#include "my_sys.h"
41#include "my_systime.h" // Timeout_type
48#include "mysql_com.h"
49#include "sql/sql_plist.h"
50#include "template_utils.h"
51
52class MDL_context;
53class MDL_lock;
54class MDL_ticket;
55class THD;
56struct LF_PINS;
57struct MDL_key;
58struct MEM_ROOT;
59namespace mdl_unittest {
61}
62/**
63 @def ENTER_COND(C, M, S, O)
64 Start a wait on a condition.
65 @param C the condition to wait on
66 @param M the associated mutex
67 @param S the new stage to enter
68 @param O the previous stage
69 @sa EXIT_COND().
70*/
71#define ENTER_COND(C, M, S, O) \
72 enter_cond(C, M, S, O, __func__, __FILE__, __LINE__)
73
74/**
75 @def EXIT_COND(S)
76 End a wait on a condition
77 @param S the new stage to enter
78*/
79#define EXIT_COND(S) exit_cond(S, __func__, __FILE__, __LINE__)
80
81/**
82 An interface to separate the MDL module from the THD, and the rest of the
83 server code.
84 */
85
87 public:
88 virtual ~MDL_context_owner() = default;
89
90 /**
91 Enter a condition wait.
92 For @c enter_cond() / @c exit_cond() to work the mutex must be held before
93 @c enter_cond(); this mutex must then be released before @c exit_cond().
94 Usage must be: lock mutex; enter_cond(); your code; unlock mutex;
95 exit_cond().
96 @param cond the condition to wait on
97 @param mutex the associated mutex
98 @param [in] stage the stage to enter, or NULL
99 @param [out] old_stage the previous stage, or NULL
100 @param src_function function name of the caller
101 @param src_file file name of the caller
102 @param src_line line number of the caller
103 @sa ENTER_COND(), THD::enter_cond()
104 @sa EXIT_COND(), THD::exit_cond()
105 */
106 virtual void enter_cond(mysql_cond_t *cond, mysql_mutex_t *mutex,
107 const PSI_stage_info *stage,
108 PSI_stage_info *old_stage, const char *src_function,
109 const char *src_file, int src_line) = 0;
110
111 /**
112 End a wait on a condition
113 @param [in] stage the new stage to enter
114 @param src_function function name of the caller
115 @param src_file file name of the caller
116 @param src_line line number of the caller
117 @sa ENTER_COND(), THD::enter_cond()
118 @sa EXIT_COND(), THD::exit_cond()
119 */
120 virtual void exit_cond(const PSI_stage_info *stage, const char *src_function,
121 const char *src_file, int src_line) = 0;
122 /**
123 Has the owner thread been killed?
124 */
125 virtual int is_killed() const = 0;
126
127 /**
128 Does the owner still have connection to the client?
129 */
130 virtual bool is_connected(bool = false) = 0;
131
132 /**
133 Indicates that owner thread might have some commit order (non-MDL) waits
134 for it which are still taken into account by MDL deadlock detection,
135 even in cases when it doesn't have any MDL locks acquired and therefore
136 can't have any MDL waiters.
137
138 @note It is important for this check to be non-racy and quick (perhaps
139 at expense of being pretty pessimistic), so it can be used to
140 make decisions reliably about whether we can skip deadlock
141 detection in some cases.
142 */
143 virtual bool might_have_commit_order_waiters() const = 0;
144
145 /**
146 Within MDL subsystem this one is only used for DEBUG_SYNC.
147 Do not use it to peek/poke into other parts of THD from MDL.
148 However it is OK to use this method in callbacks provided
149 by SQL-layer to MDL subsystem (since SQL-layer has full
150 access to THD anyway).
151
152 @warning For some derived classes implementation of this method
153 can return nullptr. Calling side must be ready to handle
154 this case.
155 */
156 virtual THD *get_thd() = 0;
157
158 /**
159 @see THD::notify_shared_lock()
160 */
162 bool needs_thr_lock_abort) = 0;
163
164 /**
165 Notify/get permission from interested storage engines before acquiring
166 exclusive lock for the key.
167
168 The returned argument 'victimized' specify reason for lock
169 not granted. If 'true', lock was refused in an attempt to
170 resolve a possible MDL->GSL deadlock. Locking may then be retried.
171
172 @return False if notification was successful and it is OK to acquire lock,
173 True if one of SEs asks to abort lock acquisition.
174 */
175 virtual bool notify_hton_pre_acquire_exclusive(const MDL_key *mdl_key,
176 bool *victimized) = 0;
177 /**
178 Notify interested storage engines that we have just released exclusive
179 lock for the key.
180 */
181 virtual void notify_hton_post_release_exclusive(const MDL_key *mdl_key) = 0;
182
183 /**
184 Get random seed specific to this THD to be used for initialization
185 of PRNG for the MDL_context.
186 */
187 virtual uint get_rand_seed() const = 0;
188};
189
190/**
191 Type of metadata lock request.
192
193 @sa Comments for MDL_object_lock::can_grant_lock() and
194 MDL_scoped_lock::can_grant_lock() for details.
195*/
196
198 /*
199 An intention exclusive metadata lock. Used only for scoped locks.
200 Owner of this type of lock can acquire upgradable exclusive locks on
201 individual objects.
202 This lock type is also used when doing lookups in the dictionary
203 cache. When acquiring objects in a schema, we lock the schema with IX
204 to prevent the schema from being deleted. This should conceptually
205 be an IS lock, but it would have the same behavior as the current IX.
206 Compatible with other IX locks, but is incompatible with scoped S and
207 X locks.
208 */
210 /*
211 A shared metadata lock.
212 To be used in cases when we are interested in object metadata only
213 and there is no intention to access object data (e.g. for stored
214 routines or during preparing prepared statements).
215 We also mis-use this type of lock for open HANDLERs, since lock
216 acquired by this statement has to be compatible with lock acquired
217 by LOCK TABLES ... WRITE statement, i.e. SNRW (We can't get by by
218 acquiring S lock at HANDLER ... OPEN time and upgrading it to SR
219 lock for HANDLER ... READ as it doesn't solve problem with need
220 to abort DML statements which wait on table level lock while having
221 open HANDLER in the same connection).
222 To avoid deadlock which may occur when SNRW lock is being upgraded to
223 X lock for table on which there is an active S lock which is owned by
224 thread which waits in its turn for table-level lock owned by thread
225 performing upgrade we have to use thr_abort_locks_for_thread()
226 facility in such situation.
227 This problem does not arise for locks on stored routines as we don't
228 use SNRW locks for them. It also does not arise when S locks are used
229 during PREPARE calls as table-level locks are not acquired in this
230 case.
231 */
233 /*
234 A high priority shared metadata lock.
235 Used for cases when there is no intention to access object data (i.e.
236 data in the table).
237 "High priority" means that, unlike other shared locks, it is granted
238 ignoring pending requests for exclusive locks. Intended for use in
239 cases when we only need to access metadata and not data, e.g. when
240 filling an INFORMATION_SCHEMA table.
241 Since SH lock is compatible with SNRW lock, the connection that
242 holds SH lock lock should not try to acquire any kind of table-level
243 or row-level lock, as this can lead to a deadlock. Moreover, after
244 acquiring SH lock, the connection should not wait for any other
245 resource, as it might cause starvation for X locks and a potential
246 deadlock during upgrade of SNW or SNRW to X lock (e.g. if the
247 upgrading connection holds the resource that is being waited for).
248 */
250 /*
251 A shared metadata lock for cases when there is an intention to read data
252 from table.
253 A connection holding this kind of lock can read table metadata and read
254 table data (after acquiring appropriate table and row-level locks).
255 This means that one can only acquire TL_READ, TL_READ_NO_INSERT, and
256 similar table-level locks on table if one holds SR MDL lock on it.
257 To be used for tables in SELECTs, subqueries, and LOCK TABLE ... READ
258 statements.
259 */
261 /*
262 A shared metadata lock for cases when there is an intention to modify
263 (and not just read) data in the table.
264 A connection holding SW lock can read table metadata and modify or read
265 table data (after acquiring appropriate table and row-level locks).
266 To be used for tables to be modified by INSERT, UPDATE, DELETE
267 statements, but not LOCK TABLE ... WRITE or DDL). Also taken by
268 SELECT ... FOR UPDATE.
269 */
271 /*
272 A version of MDL_SHARED_WRITE lock which has lower priority than
273 MDL_SHARED_READ_ONLY locks. Used by DML statements modifying
274 tables and using the LOW_PRIORITY clause.
275 */
277 /*
278 An upgradable shared metadata lock which allows concurrent updates and
279 reads of table data.
280 A connection holding this kind of lock can read table metadata and read
281 table data. It should not modify data as this lock is compatible with
282 SRO locks.
283 Can be upgraded to SNW, SNRW and X locks. Once SU lock is upgraded to X
284 or SNRW lock data modification can happen freely.
285 To be used for the first phase of ALTER TABLE.
286 */
288 /*
289 A shared metadata lock for cases when we need to read data from table
290 and block all concurrent modifications to it (for both data and metadata).
291 Used by LOCK TABLES READ statement.
292 */
294 /*
295 An upgradable shared metadata lock which blocks all attempts to update
296 table data, allowing reads.
297 A connection holding this kind of lock can read table metadata and read
298 table data.
299 Can be upgraded to X metadata lock.
300 Note, that since this type of lock is not compatible with SNRW or SW
301 lock types, acquiring appropriate engine-level locks for reading
302 (TL_READ* for MyISAM, shared row locks in InnoDB) should be
303 contention-free.
304 To be used for the first phase of ALTER TABLE, when copying data between
305 tables, to allow concurrent SELECTs from the table, but not UPDATEs.
306 */
308 /*
309 An upgradable shared metadata lock which allows other connections
310 to access table metadata, but not data.
311 It blocks all attempts to read or update table data, while allowing
312 INFORMATION_SCHEMA and SHOW queries.
313 A connection holding this kind of lock can read table metadata modify and
314 read table data.
315 Can be upgraded to X metadata lock.
316 To be used for LOCK TABLES WRITE statement.
317 Not compatible with any other lock type except S and SH.
318 */
320 /*
321 An exclusive metadata lock.
322 A connection holding this lock can modify both table's metadata and data.
323 No other type of metadata lock can be granted while this lock is held.
324 To be used for CREATE/DROP/RENAME TABLE statements and for execution of
325 certain phases of other DDL statements.
326 */
328 /* This should be the last !!! */
331
332/** Duration of metadata lock. */
333
335 /**
336 Locks with statement duration are automatically released at the end
337 of statement or transaction.
338 */
340 /**
341 Locks with transaction duration are automatically released at the end
342 of transaction.
343 */
345 /**
346 Locks with explicit duration survive the end of statement and transaction.
347 They have to be released explicitly by calling MDL_context::release_lock().
348 */
350 /* This should be the last ! */
353
354/** Maximal length of key for metadata locking subsystem. */
355#define MAX_MDLKEY_LENGTH (1 + NAME_LEN + 1 + NAME_LEN + 1)
356
357/**
358 Metadata lock object key.
359
360 A lock is requested or granted based on a fully qualified name and type.
361 E.g. They key for a table consists of @<0 (=table)@>+@<database@>+@<table
362 name@>. Elsewhere in the comments this triple will be referred to simply as
363 "key" or "name".
364*/
365
366struct MDL_key {
367 public:
368#ifdef HAVE_PSI_INTERFACE
369 static void init_psi_keys();
370#endif
371
372 /**
373 Object namespaces.
374 Sic: when adding a new member to this enum make sure to
375 update m_namespace_to_wait_state_name array in mdl.cc!
376
377 Different types of objects exist in different namespaces
378 - GLOBAL is used for the global read lock.
379 - BACKUP_LOCK is to block any operations that could cause
380 inconsistent backup. Such operations are most DDL statements,
381 and some administrative statements.
382 - TABLESPACE is for tablespaces.
383 - SCHEMA is for schemas (aka databases).
384 - TABLE is for tables and views.
385 - FUNCTION is for stored functions.
386 - PROCEDURE is for stored procedures.
387 - LIBRARY is for libraries.
388 - TRIGGER is for triggers.
389 - EVENT is for event scheduler events.
390 - COMMIT is for enabling the global read lock to block commits.
391 - USER_LEVEL_LOCK is for user-level locks.
392 - LOCKING_SERVICE is for the name plugin RW-lock service
393 - SRID is for spatial reference systems
394 - ACL_CACHE is for ACL caches
395 - COLUMN_STATISTICS is for column statistics, such as histograms
396 - RESOURCE_GROUPS is for resource groups.
397 - FOREIGN_KEY is for foreign key names.
398 - CHECK_CONSTRAINT is for check constraint names.
399 Note that requests waiting for user-level locks get special
400 treatment - waiting is aborted if connection to client is lost.
401 */
422 /* This should be the last ! */
424 };
425
426 const uchar *ptr() const { return pointer_cast<const uchar *>(m_ptr); }
427 uint length() const { return m_length; }
428
429 const char *db_name() const { return m_ptr + 1; }
430 uint db_name_length() const { return m_db_name_length; }
431
432 const char *name() const {
434 : m_ptr + m_db_name_length + 2);
435 }
436 uint name_length() const { return m_object_name_length; }
437
438 const char *col_name() const {
440
442 /* A column name was stored in the key buffer. */
444 }
445
446 /* No column name stored. */
447 return nullptr;
448 }
449
450 uint col_name_length() const {
452
454 /* A column name was stored in the key buffer. */
456 }
457
458 /* No column name stored. */
459 return 0;
460 }
461
463 return (enum_mdl_namespace)(m_ptr[0]);
464 }
465
466 /**
467 Construct a metadata lock key from a triplet (mdl_namespace,
468 database and name).
469
470 @remark The key for a table is @<mdl_namespace@>+@<database name@>+@<table
471 name@>
472
473 @param mdl_namespace Id of namespace of object to be locked
474 @param db Name of database to which the object belongs
475 @param name Name of of the object
476 */
478 const char *name) {
479 m_ptr[0] = (char)mdl_namespace;
480
482
483 /*
484 It is responsibility of caller to ensure that db and object names
485 are not longer than NAME_LEN. Still we play safe and try to avoid
486 buffer overruns.
487
488 Implicit tablespace names in InnoDB may be longer than NAME_LEN.
489 We will lock based on the first NAME_LEN characters.
490
491 TODO: The patch acquires metadata locks on the NAME_LEN
492 first bytest of the tablespace names. For long names,
493 the consequence of locking on this prefix is
494 that locking a single implicit tablespace might end up
495 effectively lock all implicit tablespaces in the same
496 schema. A possible fix is to lock on a prefix of length
497 NAME_LEN * 2, since this is the real buffer size of
498 the metadata lock key. Dependencies from the PFS
499 implementation, possibly relying on the key format,
500 must be investigated first, though.
501 */
502 assert(strlen(db) <= NAME_LEN);
503 assert((mdl_namespace == TABLESPACE) || (strlen(name) <= NAME_LEN));
505 static_cast<uint16>(strmake(m_ptr + 1, db, NAME_LEN) - m_ptr - 1);
506 m_object_name_length = static_cast<uint16>(
508 m_db_name_length - 2);
510 }
511
512 /**
513 Construct a metadata lock key from a quadruplet (mdl_namespace,
514 database, table and column name).
515
516 @remark The key for a column is
517 @<mdl_namespace@>+@<database name@>+@<table name@>+@<column name@>
518
519 @param mdl_namespace Id of namespace of object to be locked
520 @param db Name of database to which the object belongs
521 @param name Name of of the object
522 @param column_name Name of of the column
523 */
525 const char *name, const char *column_name) {
526 m_ptr[0] = (char)mdl_namespace;
527 char *start;
528 char *end;
529
531
532 assert(strlen(db) <= NAME_LEN);
533 start = m_ptr + 1;
534 end = strmake(start, db, NAME_LEN);
535 m_db_name_length = static_cast<uint16>(end - start);
536
537 assert(strlen(name) <= NAME_LEN);
538 start = end + 1;
540 m_object_name_length = static_cast<uint16>(end - start);
541
542 const size_t col_len = strlen(column_name);
543 assert(col_len <= NAME_LEN);
544 start = end + 1;
545 const size_t remaining =
547 uint16 extra_length = 0;
548
549 /*
550 In theory:
551 - schema name is up to NAME_LEN characters
552 - object name is up to NAME_LEN characters
553 - column name is up to NAME_LEN characters
554 - NAME_LEN is 64 characters
555 - 1 character is up to 3 bytes (UTF8MB3),
556 and when moving to UTF8MB4, up to 4 bytes.
557 - Storing a SCHEMA + OBJECT MDL key
558 can take up to 387 bytes
559 - Storing a SCHEMA + OBJECT + COLUMN MDL key
560 can take up to 580 bytes.
561
562 In practice:
563 - full storage is allocated for SCHEMA + OBJECT only,
564 storage for COLUMN is **NOT** reserved.
565 - SCHEMA and OBJECT names are typically shorter,
566 and are not using systematically multi-bytes characters
567 for each character, so that less space is required.
568 - MDL keys that are not COLUMN_STATISTICS
569 are stored in full, without truncation.
570
571 For the COLUMN_STATISTICS name space:
572 - either the full SCHEMA + OBJECT + COLUMN key fits
573 within 387 bytes, in which case the fully qualified
574 column name is stored,
575 leading to MDL locks per column (as intended)
576 - or the SCHEMA and OBJECT names are very long,
577 so that not enough room is left to store a column name,
578 in which case the MDL key is truncated to be
579 COLUMN_STATISTICS + SCHEMA + NAME.
580 In this case, MDL locks for columns col_X and col_Y
581 in table LONG_FOO.LONG_BAR will both share the same
582 key LONG_FOO.LONG_BAR, in effect providing a lock
583 granularity not per column but per table.
584 This is a degraded mode of operation,
585 which serializes MDL access to columns
586 (for tables with a very long fully qualified name),
587 to reduce the memory footprint for all MDL access.
588
589 To be revised if the MDL key buffer is allocated dynamically
590 instead.
591 */
592
593 static_assert(MAX_MDLKEY_LENGTH == 387, "UTF8MB3");
594
595 /*
596 Check if there is room to store the whole column name.
597 This code is not trying to store truncated column names,
598 to avoid cutting column_name in the middle of a
599 multi-byte character.
600 */
601 if (remaining >= col_len + 1) {
602 end = strmake(start, column_name, remaining);
603 extra_length = static_cast<uint16>(end - start) + 1; // With \0
604 }
605 m_length = m_db_name_length + m_object_name_length + 3 + extra_length;
606 assert(m_length <= MAX_MDLKEY_LENGTH);
607 }
608
609 /**
610 Construct a metadata lock key from a quadruplet (mdl_namespace, database,
611 normalized object name buffer and the object name).
612
613 @remark The key for a routine/event/resource group/trigger is
614 @<mdl_namespace@>+@<database name@>+@<normalized object name@>
615 additionally @<object name@> is stored in the same buffer for information
616 purpose if buffer has sufficient space.
617
618 Routine, Event and Resource group names are case sensitive and accent
619 sensitive. So normalized object name is used to form a MDL_key.
620
621 With the UTF8MB3 charset space reserved for the db name/object
622 name is 64 * 3 bytes. utf8mb3_general_ci collation is used for the
623 Routine, Event and Resource group names. With this collation, the
624 normalized object name uses just 2 bytes for each character (max
625 length = 64 * 2 bytes). MDL_key has still some space to store the
626 object names. If there is a sufficient space for the object name
627 in the MDL_key then it is stored in the MDL_key (similar to the
628 column names in the MDL_key). Actual object name is used by the
629 PFS. Not listing actual object name from the PFS should be OK
630 when there is no space to store it (instead of increasing the
631 MDL_key size). Object name is not used in the key comparisons. So
632 only (mdl_namespace + strlen(db) + 1 + normalized_name_len + 1)
633 value is stored in the m_length member.
634
635 @param mdl_namespace Id of namespace of object to be locked.
636 @param db Name of database to which the object belongs.
637 @param normalized_name Normalized name of the object.
638 @param normalized_name_len Length of the normalized object name.
639 @param name Name of the object.
640 */
642 const char *normalized_name, size_t normalized_name_len,
643 const char *name) {
644 m_ptr[0] = (char)mdl_namespace;
645
646 /*
647 FUNCTION, PROCEDURE, EVENT and RESOURCE_GROUPS names are case and accent
648 insensitive. For other objects key should not be formed from this method.
649 */
651
652 assert(strlen(db) <= NAME_LEN && strlen(name) <= NAME_LEN &&
653 normalized_name_len <= NAME_CHAR_LEN * 2);
654
655 // Database name.
657 static_cast<uint16>(strmake(m_ptr + 1, db, NAME_LEN) - m_ptr - 1);
658
659 // Normalized object name.
660 m_length = static_cast<uint16>(m_db_name_length + normalized_name_len + 3);
661 memcpy(m_ptr + m_db_name_length + 2, normalized_name, normalized_name_len);
662 *(m_ptr + m_length - 1) = 0;
663
664 /*
665 Copy name of the object if there is a sufficient space to store the name
666 in the MDL key. This code is not trying to store truncated object names,
667 to avoid cutting object_name in the middle of a multi-byte character.
668 */
669 if (strlen(name) < static_cast<size_t>(MAX_MDLKEY_LENGTH - m_length)) {
670 m_object_name_length = static_cast<uint16>(
672 m_ptr - m_length));
673 } else {
675 *(m_ptr + m_length) = 0;
676 }
677
679 }
680
681 /**
682 Construct a metadata lock key from namespace and partial key, which
683 contains info about object database and name.
684
685 @remark The partial key must be "<database>\0<name>\0".
686
687 @param mdl_namespace Id of namespace of object to be locked
688 @param part_key Partial key.
689 @param part_key_length Partial key length
690 @param db_length Database name length.
691 */
693 size_t part_key_length, size_t db_length) {
694 /*
695 Key suffix provided should be in compatible format and
696 its components should adhere to length restrictions.
697 */
698 assert(strlen(part_key) == db_length);
699 assert(db_length + 1 + strlen(part_key + db_length + 1) + 1 ==
700 part_key_length);
701 assert(db_length <= NAME_LEN);
702 assert(part_key_length <= NAME_LEN + 1 + NAME_LEN + 1);
703
704 m_ptr[0] = (char)mdl_namespace;
705 /*
706 Partial key of objects with normalized object name can not be used to
707 initialize MDL key.
708 */
710
711 memcpy(m_ptr + 1, part_key, part_key_length);
712 m_length = static_cast<uint16>(part_key_length + 1);
713 m_db_name_length = static_cast<uint16>(db_length);
715 }
716 void mdl_key_init(const MDL_key *rhs) {
717 const uint16 copy_length =
719 ? rhs->m_length + rhs->m_object_name_length + 1
720 : rhs->m_length;
721 memcpy(m_ptr, rhs->m_ptr, copy_length);
722 m_length = rhs->m_length;
725 }
726 void reset() {
727 m_ptr[0] = NAMESPACE_END;
730 m_length = 0;
731 }
732 bool is_equal(const MDL_key *rhs) const {
733 return (m_length == rhs->m_length &&
734 memcmp(m_ptr, rhs->m_ptr, m_length) == 0);
735 }
736 /**
737 Compare two MDL keys lexicographically.
738 */
739 int cmp(const MDL_key *rhs) const {
740 /*
741 For the keys with the normalized names, there is a possibility of getting
742 '\0' in its middle. So only key content comparison would yield incorrect
743 result. Hence comparing key length too when keys are equal.
744 For other keys, key buffer is always '\0'-terminated. Since key character
745 set is utf-8, we can safely assume that no character starts with a zero
746 byte.
747 */
748 int res = memcmp(m_ptr, rhs->m_ptr, std::min(m_length, rhs->m_length));
749 if (res == 0) res = m_length - rhs->m_length;
750 return res;
751 }
752
753 MDL_key(const MDL_key &rhs) { mdl_key_init(&rhs); }
754
755 MDL_key &operator=(const MDL_key &rhs) {
756 mdl_key_init(&rhs);
757 return *this;
758 }
759
760 MDL_key(enum_mdl_namespace namespace_arg, const char *db_arg,
761 const char *name_arg) {
762 mdl_key_init(namespace_arg, db_arg, name_arg);
763 }
764 MDL_key() = default; /* To use when part of MDL_request. */
765
766 /**
767 Get thread state name to be used in case when we have to
768 wait on resource identified by key.
769 */
772 }
773
774 private:
775 /**
776 Check if normalized object name should be used.
777
778 @return true if normlized object name should be used, false
779 otherwise.
780 */
782 return (mdl_namespace() == FUNCTION || mdl_namespace() == PROCEDURE ||
785 }
786
787 private:
793};
794
795/**
796 A pending metadata lock request.
797
798 A lock request and a granted metadata lock are represented by
799 different classes because they have different allocation
800 sites and hence different lifetimes. The allocation of lock requests is
801 controlled from outside of the MDL subsystem, while allocation of granted
802 locks (tickets) is controlled within the MDL subsystem.
803*/
804
806 public:
807 /** Type of metadata lock. */
809 /** Duration for requested lock. */
811
812 /**
813 Pointers for participating in the list of lock requests for this context.
814 */
817 /**
818 Pointer to the lock ticket object for this lock request.
819 Valid only if this lock request is satisfied.
820 */
822
823 /** A lock is requested based on a fully qualified name and type. */
825
826 const char *m_src_file{nullptr};
827 uint m_src_line{0};
828
829 public:
830 static void *operator new(size_t size, MEM_ROOT *mem_root,
831 const std::nothrow_t &arg
832 [[maybe_unused]] = std::nothrow) noexcept {
833 return mem_root->Alloc(size);
834 }
835
836 static void operator delete(void *, MEM_ROOT *,
837 const std::nothrow_t &) noexcept {}
838
840 const char *db_arg, const char *name_arg,
841 enum_mdl_type mdl_type_arg,
842 enum_mdl_duration mdl_duration_arg,
843 const char *src_file, uint src_line);
844 void init_by_key_with_source(const MDL_key *key_arg,
845 enum_mdl_type mdl_type_arg,
846 enum_mdl_duration mdl_duration_arg,
847 const char *src_file, uint src_line);
849 const char *part_key_arg,
850 size_t part_key_length_arg,
851 size_t db_length_arg,
852 enum_mdl_type mdl_type_arg,
853 enum_mdl_duration mdl_duration_arg,
854 const char *src_file, uint src_line);
855 /** Set type of lock request. Can be only applied to pending locks. */
856 inline void set_type(enum_mdl_type type_arg) {
857 assert(ticket == nullptr);
858 type = type_arg;
859 }
860
861 /**
862 Is this a request for a lock which allow data to be updated?
863
864 @note This method returns true for MDL_SHARED_UPGRADABLE type of
865 lock. Even though this type of lock doesn't allow updates
866 it will always be upgraded to one that does.
867 */
870 }
871
872 /** Is this a request for a strong, DDL/LOCK TABLES-type, of lock? */
874 return type >= MDL_SHARED_UPGRADABLE;
875 }
876
877 /**
878 This constructor exists for two reasons:
879
880 - Table_ref objects are sometimes default-constructed. We plan to
881 remove this as there is no practical reason, the call to the default
882 constructor is always followed by either a call to
883 Table_ref::operator= or memberwise assignments.
884
885 - In some legacy cases Table_ref objects are copy-assigned without
886 intention to copy the Table_ref::mdl_request member. In this cases
887 they are overwritten with an uninitialized MDL_request object. The cases
888 are:
889
890 - Sql_cmd_handler_open::execute()
891 - mysql_execute_command()
892 - Query_expression::prepare()
893 - fill_defined_view_parts()
894
895 No new cases are expected. In all other cases, so far only
896 Locked_tables_list::rename_locked_table(), a move assignment is actually
897 what is intended.
898 */
899 MDL_request() = default;
900
902 : type(rhs.type), duration(rhs.duration), ticket(nullptr), key(rhs.key) {}
903
905
907};
908
909#define MDL_REQUEST_INIT(R, P1, P2, P3, P4, P5) \
910 (*R).init_with_source(P1, P2, P3, P4, P5, __FILE__, __LINE__)
911
912#define MDL_REQUEST_INIT_BY_KEY(R, P1, P2, P3) \
913 (*R).init_by_key_with_source(P1, P2, P3, __FILE__, __LINE__)
914
915#define MDL_REQUEST_INIT_BY_PART_KEY(R, P1, P2, P3, P4, P5, P6) \
916 (*R).init_by_part_key_with_source(P1, P2, P3, P4, P5, P6, __FILE__, __LINE__)
917
918/**
919 An abstract class for inspection of a connected
920 subgraph of the wait-for graph.
921*/
922
924 public:
925 virtual bool enter_node(MDL_context *node) = 0;
926 virtual void leave_node(MDL_context *node) = 0;
927
928 virtual bool inspect_edge(MDL_context *dest) = 0;
931
932 public:
933 /**
934 XXX, hack: During deadlock search, we may need to
935 inspect TABLE_SHAREs and acquire LOCK_open. Since
936 LOCK_open is not a recursive mutex, count here how many
937 times we "took" it (but only take and release once).
938 Not using a native recursive mutex or rwlock in 5.5 for
939 LOCK_open since it has significant performance impacts.
940 */
942};
943
944/**
945 Abstract class representing an edge in the waiters graph
946 to be traversed by deadlock detection algorithm.
947*/
948
950 public:
952
953 /**
954 Accept a wait-for graph visitor to inspect the node
955 this edge is leading to.
956 */
957 virtual bool accept_visitor(MDL_wait_for_graph_visitor *gvisitor) = 0;
958
959 static const uint DEADLOCK_WEIGHT_CO = 0;
960 static const uint DEADLOCK_WEIGHT_DML = 25;
961 static const uint DEADLOCK_WEIGHT_ULL = 50;
962 static const uint DEADLOCK_WEIGHT_DDL = 100;
963
964 /* A helper used to determine which lock request should be aborted. */
965 virtual uint get_deadlock_weight() const = 0;
966};
967
968/**
969 A granted metadata lock.
970
971 @warning MDL_ticket members are private to the MDL subsystem.
972
973 @note Multiple shared locks on a same object are represented by a
974 single ticket. The same does not apply for other lock types.
975
976 @note There are two groups of MDL_ticket members:
977 - "Externally accessible". These members can be accessed from
978 threads/contexts different than ticket owner in cases when
979 ticket participates in some list of granted or waiting tickets
980 for a lock. Therefore one should change these members before
981 including then to waiting/granted lists or while holding lock
982 protecting those lists.
983 - "Context private". Such members are private to thread/context
984 owning this ticket. I.e. they should not be accessed from other
985 threads/contexts.
986*/
987
989 public:
990 /**
991 Pointers for participating in the list of lock requests for this context.
992 Context private.
993 */
996
997 /**
998 Pointers for participating in the list of satisfied/pending requests
999 for the lock. Externally accessible.
1000 */
1003
1004 public:
1005 bool has_pending_conflicting_lock() const;
1006
1007 MDL_context *get_ctx() const { return m_ctx; }
1011 }
1012 enum_mdl_type get_type() const { return m_type; }
1013 MDL_lock *get_lock() const { return m_lock; }
1014 const MDL_key *get_key() const;
1016
1018
1021
1022 /** Implement MDL_wait_for_subgraph interface. */
1023 bool accept_visitor(MDL_wait_for_graph_visitor *dvisitor) override;
1024 uint get_deadlock_weight() const override;
1025
1026#ifndef NDEBUG
1029#endif
1030
1031 public:
1032 /**
1033 Status of lock request represented by the ticket as reflected in P_S.
1034 */
1041
1042 private:
1043 friend class MDL_context;
1044
1046#ifndef NDEBUG
1047 ,
1048 enum_mdl_duration duration_arg
1049#endif
1050 )
1051 : m_type(type_arg),
1052#ifndef NDEBUG
1053 m_duration(duration_arg),
1054#endif
1055 m_ctx(ctx_arg),
1056 m_lock(nullptr),
1057 m_is_fast_path(false),
1058 m_hton_notified(false),
1059 m_psi(nullptr) {
1060 }
1061
1062 ~MDL_ticket() override { assert(m_psi == nullptr); }
1063
1064 static MDL_ticket *create(MDL_context *ctx_arg, enum_mdl_type type_arg
1065#ifndef NDEBUG
1066 ,
1067 enum_mdl_duration duration_arg
1068#endif
1069 );
1070 static void destroy(MDL_ticket *ticket);
1071
1072 private:
1073 /** Type of metadata lock. Externally accessible. */
1075#ifndef NDEBUG
1076 /**
1077 Duration of lock represented by this ticket.
1078 Context private. Debug-only.
1079 */
1081#endif
1082 /**
1083 Context of the owner of the metadata lock ticket. Externally accessible.
1084 */
1086
1087 /**
1088 Pointer to the lock object for this lock ticket. Externally accessible.
1089 */
1091
1092 /**
1093 Indicates that ticket corresponds to lock acquired using "fast path"
1094 algorithm. Particularly this means that it was not included into
1095 MDL_lock::m_granted bitmap/list and instead is accounted for by
1096 MDL_lock::m_fast_path_locks_granted_counter
1097 */
1099
1100 /**
1101 Indicates that ticket corresponds to lock request which required
1102 storage engine notification during its acquisition and requires
1103 storage engine notification after its release.
1104 */
1106
1108
1109 private:
1110 MDL_ticket(const MDL_ticket &); /* not implemented */
1111 MDL_ticket &operator=(const MDL_ticket &); /* not implemented */
1112};
1113
1114/**
1115 Keep track of MDL_ticket for different durations. Maintains a
1116 hash-based secondary index into the linked lists, to speed up access
1117 by MDL_key.
1118 */
1120 public:
1121 /**
1122 Utility struct for representing a ticket pointer and its duration.
1123 */
1127
1130 : m_dur{d}, m_ticket{t} {}
1131 };
1132
1133 private:
1138
1139 struct Duration {
1141 /**
1142 m_mat_front tracks what was the front of m_ticket_list, the last
1143 time MDL_context::materialize_fast_path_locks() was called. This
1144 just an optimization which allows
1145 MDL_context::materialize_fast_path_locks() only to consider the
1146 locks added since the last time it ran. Consequently, it can be
1147 assumed that every ticket after m_mat_front is materialized, but
1148 the converse is not necessarily true as new, already
1149 materialized, locks may have been added since the last time
1150 materialize_fast_path_locks() ran.
1151 */
1153 };
1154
1156
1157 struct Hash {
1158 size_t operator()(const MDL_key *k) const;
1159 };
1160
1161 struct Key_equal {
1162 bool operator()(const MDL_key *a, const MDL_key *b) const {
1163 return a->is_equal(b);
1164 }
1165 };
1166
1167 using Ticket_map = std::unordered_multimap<const MDL_key *, MDL_ticket_handle,
1168 Hash, Key_equal>;
1169
1170 /**
1171 If the number of tickets in the ticket store (in all durations) is equal
1172 to, or exceeds this constant the hash index (in the form of an
1173 unordered_multi_map) will be maintained and used for lookups.
1174
1175 The value 256 is chosen as it has worked well in benchmarks.
1176 */
1177 const size_t THRESHOLD = 256;
1178
1179 /**
1180 Initial number of buckets in the hash index. THRESHOLD is chosen
1181 to get a fill-factor of 50% when reaching the threshold value.
1182 */
1184 size_t m_count = 0;
1185
1186 std::unique_ptr<Ticket_map> m_map;
1187
1189 MDL_ticket_handle find_in_hash(const MDL_request &req) const;
1190
1191 public:
1192 /**
1193 Public alias.
1194 */
1196
1197 /**
1198 Constructs store. The hash index is initially empty. Filled on demand.
1199 */
1201 : // Comment in to test threshold values in unit test micro benchmark
1202 // THRESHOLD{read_from_env("TS_THRESHOLD", 500)},
1203 m_map{nullptr} {}
1204
1205 /**
1206 Calls the closure provided as argument for each of the MDL_tickets
1207 in the given duration.
1208 @param dur duration list to iterate over
1209 @param clos closure to invoke for each ticket in the list
1210 */
1211 template <typename CLOS>
1213 List_iterator it(m_durations[dur].m_ticket_list);
1214 for (MDL_ticket *t = it++; t != nullptr; t = it++) {
1215 clos(t, dur);
1216 }
1217 }
1218
1219 /**
1220 Calls the closure provided as argument for each of the MDL_tickets
1221 in the store.
1222 @param clos closure to invoke for each ticket in the store
1223 */
1224 template <typename CLOS>
1226 for_each_ticket_in_duration_list(MDL_STATEMENT, std::forward<CLOS>(clos));
1227 for_each_ticket_in_duration_list(MDL_TRANSACTION, std::forward<CLOS>(clos));
1228 for_each_ticket_in_duration_list(MDL_EXPLICIT, std::forward<CLOS>(clos));
1229 }
1230
1231 /**
1232 Predicate for the emptiness of the store.
1233 @return true if there are no tickets in the store
1234 */
1235 bool is_empty() const;
1236
1237 /**
1238 Predicate for the emptiness of a given duration list.
1239 @param di the duration to check
1240 @return true if there are no tickets with the given duration
1241 */
1242 bool is_empty(int di) const;
1243
1244 /**
1245 Return the first MDL_ticket for the given duration.
1246
1247 @param di duration to get first ticket for
1248
1249 @return first ticket in the given duration or nullptr if no such
1250 tickets exist
1251 */
1252 MDL_ticket *front(int di);
1253
1254 /**
1255 Push a ticket onto the list for a given duration.
1256 @param dur duration list to push into
1257 @param ticket to push
1258 */
1259 void push_front(enum_mdl_duration dur, MDL_ticket *ticket);
1260
1261 /**
1262 Remove a ticket from a duration list. Note that since the
1263 underlying list is an intrusive linked list there is no guarantee
1264 that the ticket is actually in the duration list. It will be
1265 removed from which ever list it is in.
1266 */
1267 void remove(enum_mdl_duration dur, MDL_ticket *ticket);
1268
1269 /**
1270 Return a P-list iterator to the given duration.
1271 @param di duration list index
1272 @return P-list iterator to tickets with given duration
1273 */
1276 }
1277
1278 /**
1279 Move all tickets to the explicit duration list.
1280 */
1282
1283 /**
1284 Move all tickets to the transaction duration list.
1285 */
1287
1288 /**
1289 Look up a ticket based on its MDL_key.
1290 @param req request to locate ticket for
1291 @return MDL_ticket_handle with ticket pointer and found duration
1292 (or nullptr and MDL_DURATION_END if not found
1293 */
1294 MDL_ticket_handle find(const MDL_request &req) const;
1295
1296 /**
1297 Mark boundary for tickets with fast_path=false, so that later
1298 calls to materialize_fast_path_locks() do not have to traverse the
1299 whole set of tickets.
1300 */
1301 void set_materialized();
1302
1303 /**
1304 Return the first ticket for which materialize_fast_path_locks
1305 already has been called for the given duration.
1306
1307 @param di duration list index
1308 @return first materialized ticket for the given duration
1309 */
1311};
1312
1313/**
1314 Savepoint for MDL context.
1315
1316 Doesn't include metadata locks with explicit duration as
1317 they are not released during rollback to savepoint.
1318*/
1319
1321 public:
1322 MDL_savepoint() = default;
1323
1324 private:
1325 MDL_savepoint(MDL_ticket *stmt_ticket, MDL_ticket *trans_ticket)
1326 : m_stmt_ticket(stmt_ticket), m_trans_ticket(trans_ticket) {}
1327
1328 friend class MDL_context;
1329
1330 private:
1331 /**
1332 Pointer to last lock with statement duration which was taken
1333 before creation of savepoint.
1334 */
1336 /**
1337 Pointer to last lock with transaction duration which was taken
1338 before creation of savepoint.
1339 */
1341};
1342
1343/**
1344 A reliable way to wait on an MDL lock.
1345*/
1346
1348 public:
1349 MDL_wait();
1350 ~MDL_wait();
1351
1352 // WS_EMPTY since EMPTY conflicts with #define in system headers on some
1353 // platforms.
1355
1356 bool set_status(enum_wait_status result_arg);
1358 void reset_status();
1360 struct timespec *abs_timeout, bool signal_timeout,
1361 const PSI_stage_info *wait_state_name);
1362
1363 private:
1364 /**
1365 Condvar which is used for waiting until this context's pending
1366 request can be satisfied or this thread has to perform actions
1367 to resolve a potential deadlock (we subscribe to such
1368 notification by adding a ticket corresponding to the request
1369 to an appropriate queue of waiters).
1370 */
1374};
1375
1376/**
1377 Base class to find out if the lock represented by a given ticket
1378 should be released. Users of release_locks() need to subclass
1379 this and specify an implementation of release(). Only for locks
1380 with explicit duration.
1381*/
1382
1384 public:
1385 virtual ~MDL_release_locks_visitor() = default;
1386 /**
1387 Check if the given ticket represents a lock that should be released.
1388
1389 @retval true if the lock should be released, false otherwise.
1390 */
1391 virtual bool release(MDL_ticket *ticket) = 0;
1392};
1393
1394/**
1395 Abstract visitor class for inspecting MDL_context.
1396*/
1397
1399 public:
1400 virtual ~MDL_context_visitor() = default;
1401 virtual void visit_context(const MDL_context *ctx) = 0;
1402};
1403
1404typedef I_P_List<MDL_request,
1409
1410/**
1411 Context of the owner of metadata locks. I.e. each server
1412 connection has such a context.
1413*/
1414
1416 public:
1417 typedef I_P_List<MDL_ticket,
1421
1423
1424 MDL_context();
1425 void destroy();
1426
1428 bool acquire_lock(MDL_request *mdl_request, Timeout_type lock_wait_timeout);
1429 bool acquire_locks(MDL_request_list *requests,
1430 Timeout_type lock_wait_timeout);
1431 bool upgrade_shared_lock(MDL_ticket *mdl_ticket, enum_mdl_type new_type,
1432 Timeout_type lock_wait_timeout);
1433
1435
1436 /**
1437 Create copy of all granted tickets of particular duration from given
1438 context to current context.
1439 Used by XA for preserving locks during client disconnect.
1440
1441 @param ticket_owner Owner of tickets to be cloned
1442 @param duration MDL lock duration for that tickets are to be cloned
1443
1444 @retval true Out of memory or deadlock happened or
1445 lock request was refused by storage engine.
1446 @retval false Success.
1447 */
1448
1449 bool clone_tickets(const MDL_context *ticket_owner,
1450 enum_mdl_duration duration);
1451
1454 void release_lock(MDL_ticket *ticket);
1455
1456 bool owns_equal_or_stronger_lock(const MDL_key *mdl_key,
1457 enum_mdl_type mdl_type);
1458
1460 const char *db, const char *name,
1461 enum_mdl_type mdl_type);
1462
1463 bool find_lock_owner(const MDL_key *mdl_key, MDL_context_visitor *visitor);
1464
1465 bool has_lock(const MDL_savepoint &mdl_savepoint, MDL_ticket *mdl_ticket);
1466
1467 inline bool has_locks() const { return !m_ticket_store.is_empty(); }
1468
1469 bool has_locks(MDL_key::enum_mdl_namespace mdl_namespace) const;
1470
1471 bool has_locks_waited_for() const;
1472
1473#ifndef NDEBUG
1475 return !m_ticket_store.is_empty(duration);
1476 }
1477#endif
1478
1482 }
1483
1486 void set_lock_duration(MDL_ticket *mdl_ticket, enum_mdl_duration duration);
1487
1491
1493
1494 /** @pre Only valid if we started waiting for lock. */
1495 inline uint get_deadlock_weight() const {
1499 }
1500
1501 void init(MDL_context_owner *arg) { m_owner = arg; }
1502
1503 void set_needs_thr_lock_abort(bool needs_thr_lock_abort) {
1504 /*
1505 @note In theory, this member should be modified under protection
1506 of some lock since it can be accessed from different threads.
1507 In practice, this is not necessary as code which reads this
1508 value and so might miss the fact that value was changed will
1509 always re-try reading it after small timeout and therefore
1510 will see the new value eventually.
1511 */
1512 m_needs_thr_lock_abort = needs_thr_lock_abort;
1513
1515 /*
1516 For MDL_object_lock::notify_conflicting_locks() to work properly
1517 all context requiring thr_lock aborts should not have any "fast
1518 path" locks.
1519 */
1521 }
1522 }
1524
1525 void set_force_dml_deadlock_weight(bool force_dml_deadlock_weight) {
1526 m_force_dml_deadlock_weight = force_dml_deadlock_weight;
1527 }
1528
1529 /**
1530 Get pseudo random value in [0 .. 2^31-1] range.
1531
1532 @note We use Linear Congruential Generator with venerable constant
1533 parameters for this.
1534 It is known to have problems with its lower bits are not being
1535 very random so probably is not good enough for generic use.
1536 However, we only use it to do random dives into MDL_lock objects
1537 hash when searching for unused objects to be freed, and for this
1538 purposes it is sufficient.
1539 We rely on values of "get_random() % 2^k" expression having "2^k"
1540 as a period to ensure that random dives eventually cover all hash
1541 (the former can be proven to be true). This also means that there
1542 is no bias towards any specific objects to be expelled (as hash
1543 values don't repeat), which is nice for performance.
1544 */
1545 uint get_random() {
1546 if (m_rand_state > INT_MAX32) {
1547 /*
1548 Perform lazy initialization of LCG. We can't initialize it at the
1549 point when MDL_context is created since THD represented through
1550 MDL_context_owner interface is not fully initialized at this point
1551 itself.
1552 */
1554 }
1555 m_rand_state = (m_rand_state * 1103515245 + 12345) & INT_MAX32;
1556 return m_rand_state;
1557 }
1558
1559 /**
1560 Within MDL subsystem this one is only used for DEBUG_SYNC.
1561 Do not use it to peek/poke into other parts of THD from MDL.
1562 @sa MDL_context_owner::get_thd().
1563 */
1564 THD *get_thd() const { return m_owner->get_thd(); }
1565
1566 public:
1567 /**
1568 If our request for a lock is scheduled, or aborted by the deadlock
1569 detector, the result is recorded in this class.
1570 */
1572
1573 private:
1574 /**
1575 Lists of all MDL tickets acquired by this connection.
1576
1577 Lists of MDL tickets:
1578 ---------------------
1579 The entire set of locks acquired by a connection can be separated
1580 in three subsets according to their duration: locks released at
1581 the end of statement, at the end of transaction and locks are
1582 released explicitly.
1583
1584 Statement and transactional locks are locks with automatic scope.
1585 They are accumulated in the course of a transaction, and released
1586 either at the end of uppermost statement (for statement locks) or
1587 on COMMIT, ROLLBACK or ROLLBACK TO SAVEPOINT (for transactional
1588 locks). They must not be (and never are) released manually,
1589 i.e. with release_lock() call.
1590
1591 Tickets with explicit duration are taken for locks that span
1592 multiple transactions or savepoints.
1593 These are: HANDLER SQL locks (HANDLER SQL is
1594 transaction-agnostic), LOCK TABLES locks (you can COMMIT/etc
1595 under LOCK TABLES, and the locked tables stay locked), user level
1596 locks (GET_LOCK()/RELEASE_LOCK() functions) and
1597 locks implementing "global read lock".
1598
1599 Statement/transactional locks are always prepended to the
1600 beginning of the appropriate list. In other words, they are
1601 stored in reverse temporal order. Thus, when we rollback to
1602 a savepoint, we start popping and releasing tickets from the
1603 front until we reach the last ticket acquired after the savepoint.
1604
1605 Locks with explicit duration are not stored in any
1606 particular order, and among each other can be split into
1607 four sets:
1608 - LOCK TABLES locks
1609 - User-level locks
1610 - HANDLER locks
1611 - GLOBAL READ LOCK locks
1612 */
1614
1616 /**
1617 true - if for this context we will break protocol and try to
1618 acquire table-level locks while having only S lock on
1619 some table.
1620 To avoid deadlocks which might occur during concurrent
1621 upgrade of SNRW lock on such object to X lock we have to
1622 abort waits for table-level locks for such connections.
1623 false - Otherwise.
1624 */
1626
1627 /**
1628 Indicates that we need to use DEADLOCK_WEIGHT_DML deadlock
1629 weight for this context and ignore the deadlock weight provided
1630 by the MDL_wait_for_subgraph object which we are waiting for.
1631
1632 @note Can be changed only when there is a guarantee that this
1633 MDL_context is not waiting for a metadata lock or table
1634 definition entry.
1635 */
1637
1638 /**
1639 Read-write lock protecting m_waiting_for member.
1640
1641 @note The fact that this read-write lock prefers readers is
1642 important as deadlock detector won't work correctly
1643 otherwise. @sa Comment for MDL_lock::m_rwlock.
1644 */
1646 /**
1647 Tell the deadlock detector what metadata lock or table
1648 definition cache entry this session is waiting for.
1649 In principle, this is redundant, as information can be found
1650 by inspecting waiting queues, but we'd very much like it to be
1651 readily available to the wait-for graph iterator.
1652 */
1654 /**
1655 Thread's pins (a.k.a. hazard pointers) to be used by lock-free
1656 implementation of MDL_map::m_locks container. NULL if pins are
1657 not yet allocated from container's pinbox.
1658 */
1660 /**
1661 State for pseudo random numbers generator (PRNG) which output
1662 is used to perform random dives into MDL_lock objects hash
1663 when searching for unused objects to free.
1664 */
1666
1667 private:
1670 MDL_ticket *sentinel);
1671 void release_lock(enum_mdl_duration duration, MDL_ticket *ticket);
1674
1676 bool fix_pins();
1677
1678 public:
1679 void find_deadlock();
1680
1682
1683 /** Inform the deadlock detector there is an edge in the wait-for graph. */
1684 void will_wait_for(MDL_wait_for_subgraph *waiting_for_arg) {
1685 /*
1686 Before starting wait for any resource we need to materialize
1687 all "fast path" tickets belonging to this thread. Otherwise
1688 locks acquired which are represented by these tickets won't
1689 be present in wait-for graph and could cause missed deadlocks.
1690
1691 It is OK for context which doesn't wait for any resource to
1692 have "fast path" tickets, as such context can't participate
1693 in any deadlock.
1694 */
1696
1698 m_waiting_for = waiting_for_arg;
1700 }
1701
1702 /** Remove the wait-for edge from the graph after we're done waiting. */
1705 m_waiting_for = nullptr;
1707 }
1710
1711 private:
1712 MDL_context(const MDL_context &rhs); /* not implemented */
1713 MDL_context &operator=(MDL_context &rhs); /* not implemented */
1714};
1715
1716void mdl_init();
1717void mdl_destroy();
1718
1719#ifndef NDEBUG
1721#endif
1722
1723/*
1724 Metadata locking subsystem tries not to grant more than
1725 max_write_lock_count high priority, strong locks successively,
1726 to avoid starving out weak, lower priority locks.
1727*/
1728extern ulong max_write_lock_count;
1729
1731
1732/**
1733 Default value for threshold for number of unused MDL_lock objects after
1734 exceeding which we start considering freeing them. Only unit tests use
1735 different threshold value.
1736*/
1738
1739/**
1740 Ratio of unused/total MDL_lock objects after exceeding which we
1741 start trying to free unused MDL_lock objects (assuming that
1742 mdl_locks_unused_locks_low_water threshold is passed as well).
1743 Note that this value should be high enough for our algorithm
1744 using random dives into hash to work well.
1745*/
1747
1749
1750#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
Element counting policy class for I_P_List which provides basic element counting.
Definition: sql_plist.h:222
Iterator for I_P_List.
Definition: sql_plist.h:168
An interface to separate the MDL module from the THD, and the rest of the server code.
Definition: mdl.h:86
virtual void notify_shared_lock(MDL_context_owner *in_use, bool needs_thr_lock_abort)=0
virtual bool notify_hton_pre_acquire_exclusive(const MDL_key *mdl_key, bool *victimized)=0
Notify/get permission from interested storage engines before acquiring exclusive lock for the key.
virtual ~MDL_context_owner()=default
virtual void exit_cond(const PSI_stage_info *stage, const char *src_function, const char *src_file, int src_line)=0
End a wait on a condition.
virtual void notify_hton_post_release_exclusive(const MDL_key *mdl_key)=0
Notify interested storage engines that we have just released exclusive lock for the key.
virtual bool might_have_commit_order_waiters() const =0
Indicates that owner thread might have some commit order (non-MDL) waits for it which are still taken...
virtual int is_killed() const =0
Has the owner thread been killed?
virtual uint get_rand_seed() const =0
Get random seed specific to this THD to be used for initialization of PRNG for the MDL_context.
virtual void enter_cond(mysql_cond_t *cond, mysql_mutex_t *mutex, const PSI_stage_info *stage, PSI_stage_info *old_stage, const char *src_function, const char *src_file, int src_line)=0
Enter a condition wait.
virtual bool is_connected(bool=false)=0
Does the owner still have connection to the client?
virtual THD * get_thd()=0
Within MDL subsystem this one is only used for DEBUG_SYNC.
Abstract visitor class for inspecting MDL_context.
Definition: mdl.h:1398
virtual ~MDL_context_visitor()=default
virtual void visit_context(const MDL_context *ctx)=0
Context of the owner of metadata locks.
Definition: mdl.h:1415
Ticket_list::Iterator Ticket_iterator
Definition: mdl.h:1422
MDL_context_owner * get_owner() const
Definition: mdl.h:1492
bool acquire_locks(MDL_request_list *requests, Timeout_type lock_wait_timeout)
Acquire exclusive locks.
Definition: mdl.cc:3643
bool owns_equal_or_stronger_lock(const MDL_key *mdl_key, enum_mdl_type mdl_type)
Auxiliary function which allows to check if we have some kind of lock on a object.
Definition: mdl.cc:4382
bool clone_tickets(const MDL_context *ticket_owner, enum_mdl_duration duration)
Create copy of all granted tickets of particular duration from given context to current context.
Definition: mdl.cc:3697
MDL_savepoint mdl_savepoint()
Definition: mdl.h:1479
MDL_context()
Initialize a metadata locking context.
Definition: mdl.cc:1439
bool has_locks(enum_mdl_duration duration)
Definition: mdl.h:1474
bool acquire_lock(MDL_request *mdl_request, Timeout_type lock_wait_timeout)
Acquire one lock with waiting for conflicting locks to go away if needed.
Definition: mdl.cc:3364
uint get_deadlock_weight() const
Definition: mdl.h:1495
void set_explicit_duration_for_all_locks()
Set explicit duration for all locks in the context.
Definition: mdl.cc:4637
void set_force_dml_deadlock_weight(bool force_dml_deadlock_weight)
Definition: mdl.h:1525
void init(MDL_context_owner *arg)
Definition: mdl.h:1501
MDL_context_owner * m_owner
Definition: mdl.h:1615
void release_statement_locks()
Definition: mdl.cc:4516
void set_lock_duration(MDL_ticket *mdl_ticket, enum_mdl_duration duration)
Change lock duration for transactional lock.
Definition: mdl.cc:4613
void release_lock(MDL_ticket *ticket)
Release lock with explicit duration.
Definition: mdl.cc:4218
uint get_random()
Get pseudo random value in [0 .
Definition: mdl.h:1545
mysql_prlock_t m_LOCK_waiting_for
Read-write lock protecting m_waiting_for member.
Definition: mdl.h:1645
void release_locks(MDL_release_locks_visitor *visitor)
Release all explicit locks in the context for which the release() method of the provided visitor eval...
Definition: mdl.cc:4283
void release_all_locks_for_name(MDL_ticket *ticket)
Release all explicit locks in the context which correspond to the same name/object as this lock reque...
Definition: mdl.cc:4261
void set_needs_thr_lock_abort(bool needs_thr_lock_abort)
Definition: mdl.h:1503
bool clone_ticket(MDL_request *mdl_request)
Create a copy of a granted ticket.
Definition: mdl.cc:3177
void release_locks_stored_before(enum_mdl_duration duration, MDL_ticket *sentinel)
Release all locks associated with the context.
Definition: mdl.cc:4237
bool find_lock_owner(const MDL_key *mdl_key, MDL_context_visitor *visitor)
Find the first context which owns the lock and inspect it by calling MDL_context_visitor::visit_conte...
Definition: mdl.cc:4435
void rollback_to_savepoint(const MDL_savepoint &mdl_savepoint)
Releases metadata locks that were acquired after a specific savepoint.
Definition: mdl.cc:4493
uint m_rand_state
State for pseudo random numbers generator (PRNG) which output is used to perform random dives into MD...
Definition: mdl.h:1665
bool try_acquire_lock_impl(MDL_request *mdl_request, MDL_ticket **out_ticket)
Auxiliary method for acquiring lock without waiting.
Definition: mdl.cc:2813
MDL_context(const MDL_context &rhs)
void find_deadlock()
Try to find a deadlock.
Definition: mdl.cc:4049
bool m_force_dml_deadlock_weight
Indicates that we need to use DEADLOCK_WEIGHT_DML deadlock weight for this context and ignore the dea...
Definition: mdl.h:1636
void lock_deadlock_victim()
Definition: mdl.h:1708
I_P_List< MDL_ticket, I_P_List_adapter< MDL_ticket, &MDL_ticket::next_in_context, &MDL_ticket::prev_in_context > > Ticket_list
Definition: mdl.h:1420
LF_PINS * m_pins
Thread's pins (a.k.a.
Definition: mdl.h:1659
void materialize_fast_path_locks()
"Materialize" requests for locks which were satisfied using "fast path" by properly including them in...
Definition: mdl.cc:2764
bool try_acquire_lock(MDL_request *mdl_request)
Try to acquire one lock.
Definition: mdl.cc:2690
bool visit_subgraph(MDL_wait_for_graph_visitor *dvisitor)
A fragment of recursive traversal of the wait-for graph of MDL contexts in the server in search for d...
Definition: mdl.cc:4029
bool has_locks_waited_for() const
Do we hold any locks which are possibly being waited for by another MDL_context?
Definition: mdl.cc:4583
bool m_needs_thr_lock_abort
true - if for this context we will break protocol and try to acquire table-level locks while having o...
Definition: mdl.h:1625
bool upgrade_shared_lock(MDL_ticket *mdl_ticket, enum_mdl_type new_type, Timeout_type lock_wait_timeout)
Upgrade a shared metadata lock.
Definition: mdl.cc:3750
MDL_wait_for_subgraph * m_waiting_for
Tell the deadlock detector what metadata lock or table definition cache entry this session is waiting...
Definition: mdl.h:1653
void set_transaction_duration_for_all_locks()
Set transactional duration for all locks in the context.
Definition: mdl.cc:4645
MDL_ticket * find_ticket(MDL_request *mdl_req, enum_mdl_duration *duration)
Check whether the context already holds a compatible lock ticket on an object.
Definition: mdl.cc:2660
void done_waiting_for()
Remove the wait-for edge from the graph after we're done waiting.
Definition: mdl.h:1703
THD * get_thd() const
Within MDL subsystem this one is only used for DEBUG_SYNC.
Definition: mdl.h:1564
bool get_needs_thr_lock_abort() const
Definition: mdl.h:1523
bool has_lock(const MDL_savepoint &mdl_savepoint, MDL_ticket *mdl_ticket)
Does this savepoint have this lock?
Definition: mdl.cc:4531
bool has_locks() const
Definition: mdl.h:1467
MDL_ticket_store m_ticket_store
Lists of all MDL tickets acquired by this connection.
Definition: mdl.h:1613
MDL_wait m_wait
If our request for a lock is scheduled, or aborted by the deadlock detector, the result is recorded i...
Definition: mdl.h:1571
void destroy()
Destroy metadata locking context.
Definition: mdl.cc:1461
bool fix_pins()
Allocate pins which are necessary to work with MDL_map container if they are not allocated already.
Definition: mdl.cc:1475
void unlock_deadlock_victim()
Definition: mdl.h:1709
MDL_context & operator=(MDL_context &rhs)
void release_transactional_locks()
Release locks acquired by normal statements (SELECT, UPDATE, DELETE, etc) in the course of a transact...
Definition: mdl.cc:4510
void will_wait_for(MDL_wait_for_subgraph *waiting_for_arg)
Inform the deadlock detector there is an edge in the wait-for graph.
Definition: mdl.h:1684
The lock context.
Definition: mdl.cc:430
Base class to find out if the lock represented by a given ticket should be released.
Definition: mdl.h:1383
virtual bool release(MDL_ticket *ticket)=0
Check if the given ticket represents a lock that should be released.
virtual ~MDL_release_locks_visitor()=default
A pending metadata lock request.
Definition: mdl.h:805
bool is_ddl_or_lock_tables_lock_request() const
Is this a request for a strong, DDL/LOCK TABLES-type, of lock?
Definition: mdl.h:873
bool is_write_lock_request() const
Is this a request for a lock which allow data to be updated?
Definition: mdl.h:868
MDL_request(MDL_request &&)=default
enum_mdl_type type
Type of metadata lock.
Definition: mdl.h:808
void init_by_key_with_source(const MDL_key *key_arg, enum_mdl_type mdl_type_arg, enum_mdl_duration mdl_duration_arg, const char *src_file, uint src_line)
Initialize a lock request using pre-built MDL_key.
Definition: mdl.cc:1541
void init_by_part_key_with_source(MDL_key::enum_mdl_namespace namespace_arg, const char *part_key_arg, size_t part_key_length_arg, size_t db_length_arg, enum_mdl_type mdl_type_arg, enum_mdl_duration mdl_duration_arg, const char *src_file, uint src_line)
Initialize a lock request using partial MDL key.
Definition: mdl.cc:1570
void set_type(enum_mdl_type type_arg)
Set type of lock request.
Definition: mdl.h:856
MDL_request & operator=(MDL_request &&)=default
MDL_ticket * ticket
Pointer to the lock ticket object for this lock request.
Definition: mdl.h:821
MDL_request ** prev_in_list
Definition: mdl.h:816
const char * m_src_file
Definition: mdl.h:826
void init_with_source(MDL_key::enum_mdl_namespace namespace_arg, const char *db_arg, const char *name_arg, enum_mdl_type mdl_type_arg, enum_mdl_duration mdl_duration_arg, const char *src_file, uint src_line)
Initialize a lock request.
Definition: mdl.cc:1507
MDL_key key
A lock is requested based on a fully qualified name and type.
Definition: mdl.h:824
enum_mdl_duration duration
Duration for requested lock.
Definition: mdl.h:810
MDL_request()=default
This constructor exists for two reasons:
MDL_request * next_in_list
Pointers for participating in the list of lock requests for this context.
Definition: mdl.h:815
uint m_src_line
Definition: mdl.h:827
MDL_request(const MDL_request &rhs)
Definition: mdl.h:901
Savepoint for MDL context.
Definition: mdl.h:1320
MDL_savepoint()=default
MDL_ticket * m_trans_ticket
Pointer to last lock with transaction duration which was taken before creation of savepoint.
Definition: mdl.h:1340
MDL_ticket * m_stmt_ticket
Pointer to last lock with statement duration which was taken before creation of savepoint.
Definition: mdl.h:1335
MDL_savepoint(MDL_ticket *stmt_ticket, MDL_ticket *trans_ticket)
Definition: mdl.h:1325
Keep track of MDL_ticket for different durations.
Definition: mdl.h:1119
void for_each_ticket_in_ticket_lists(CLOS &&clos)
Calls the closure provided as argument for each of the MDL_tickets in the store.
Definition: mdl.h:1225
List_iterator list_iterator(int di) const
Return a P-list iterator to the given duration.
Definition: mdl.h:1274
std::unique_ptr< Ticket_map > m_map
Definition: mdl.h:1186
MDL_ticket_handle find(const MDL_request &req) const
Look up a ticket based on its MDL_key.
Definition: mdl.cc:4898
size_t m_count
Definition: mdl.h:1184
void set_materialized()
Mark boundary for tickets with fast_path=false, so that later calls to materialize_fast_path_locks() ...
Definition: mdl.cc:4912
const size_t INITIAL_BUCKET_COUNT
Initial number of buckets in the hash index.
Definition: mdl.h:1183
MDL_ticket_handle find_in_hash(const MDL_request &req) const
Definition: mdl.cc:4670
void remove(enum_mdl_duration dur, MDL_ticket *ticket)
Remove a ticket from a duration list.
Definition: mdl.cc:4752
void move_all_to_explicit_duration()
Move all tickets to the explicit duration list.
Definition: mdl.cc:4788
void move_explicit_to_transaction_duration()
Move all tickets to the transaction duration list.
Definition: mdl.cc:4847
MDL_ticket * materialized_front(int di)
Return the first ticket for which materialize_fast_path_locks already has been called for the given d...
Definition: mdl.cc:4918
const size_t THRESHOLD
If the number of tickets in the ticket store (in all durations) is equal to, or exceeds this constant...
Definition: mdl.h:1177
MDL_ticket_store()
Constructs store.
Definition: mdl.h:1200
void push_front(enum_mdl_duration dur, MDL_ticket *ticket)
Push a ticket onto the list for a given duration.
Definition: mdl.cc:4708
Duration m_durations[MDL_DURATION_END]
Definition: mdl.h:1155
MDL_ticket * front(int di)
Return the first MDL_ticket for the given duration.
Definition: mdl.cc:4704
MDL_ticket_handle find_in_lists(const MDL_request &req) const
Definition: mdl.cc:4653
void for_each_ticket_in_duration_list(enum_mdl_duration dur, CLOS &&clos)
Calls the closure provided as argument for each of the MDL_tickets in the given duration.
Definition: mdl.h:1212
bool is_empty() const
Predicate for the emptiness of the store.
Definition: mdl.cc:4692
std::unordered_multimap< const MDL_key *, MDL_ticket_handle, Hash, Key_equal > Ticket_map
Definition: mdl.h:1168
A granted metadata lock.
Definition: mdl.h:988
enum enum_mdl_type m_type
Type of metadata lock.
Definition: mdl.h:1074
bool m_is_fast_path
Indicates that ticket corresponds to lock acquired using "fast path" algorithm.
Definition: mdl.h:1098
MDL_ticket(const MDL_ticket &)
bool has_stronger_or_equal_type(enum_mdl_type type) const
Check if ticket represents metadata lock of "stronger" or equal type than specified one.
Definition: mdl.cc:2602
bool has_pending_conflicting_lock() const
Check if we have any pending locks which conflict with existing shared lock.
Definition: mdl.cc:4476
MDL_context * get_ctx() const
Definition: mdl.h:1007
void downgrade_lock(enum_mdl_type type)
Downgrade an EXCLUSIVE or SHARED_NO_WRITE lock to shared metadata lock.
Definition: mdl.cc:4301
~MDL_ticket() override
Definition: mdl.h:1062
MDL_ticket(MDL_context *ctx_arg, enum_mdl_type type_arg, enum_mdl_duration duration_arg)
Definition: mdl.h:1045
MDL_ticket & operator=(const MDL_ticket &)
static void destroy(MDL_ticket *ticket)
Definition: mdl.cc:1684
bool accept_visitor(MDL_wait_for_graph_visitor *dvisitor) override
Implement MDL_wait_for_subgraph interface.
Definition: mdl.cc:4009
bool is_incompatible_when_waiting(enum_mdl_type type) const
Definition: mdl.cc:2613
MDL_ticket * next_in_context
Pointers for participating in the list of lock requests for this context.
Definition: mdl.h:994
bool is_incompatible_when_granted(enum_mdl_type type) const
Definition: mdl.cc:2609
PSI_metadata_lock * m_psi
Definition: mdl.h:1107
MDL_ticket ** prev_in_lock
Definition: mdl.h:1002
MDL_ticket ** prev_in_context
Definition: mdl.h:995
uint get_deadlock_weight() const override
Return the 'weight' of this ticket for the victim selection algorithm.
Definition: mdl.cc:1701
enum_mdl_duration get_duration() const
Definition: mdl.h:1027
bool is_upgradable_or_exclusive() const
Definition: mdl.h:1008
bool m_hton_notified
Indicates that ticket corresponds to lock request which required storage engine notification during i...
Definition: mdl.h:1105
enum_mdl_duration m_duration
Duration of lock represented by this ticket.
Definition: mdl.h:1080
enum_psi_status
Status of lock request represented by the ticket as reflected in P_S.
Definition: mdl.h:1035
@ GRANTED
Definition: mdl.h:1037
@ POST_RELEASE_NOTIFY
Definition: mdl.h:1039
@ PRE_ACQUIRE_NOTIFY
Definition: mdl.h:1038
@ PENDING
Definition: mdl.h:1036
MDL_lock * m_lock
Pointer to the lock object for this lock ticket.
Definition: mdl.h:1090
void set_duration(enum_mdl_duration dur)
Definition: mdl.h:1028
enum_mdl_type get_type() const
Definition: mdl.h:1012
MDL_ticket * next_in_lock
Pointers for participating in the list of satisfied/pending requests for the lock.
Definition: mdl.h:1001
const MDL_key * get_key() const
Return a key identifying this lock.
Definition: mdl.cc:4482
MDL_context * m_ctx
Context of the owner of the metadata lock ticket.
Definition: mdl.h:1085
MDL_lock * get_lock() const
Definition: mdl.h:1013
static MDL_ticket * create(MDL_context *ctx_arg, enum_mdl_type type_arg, enum_mdl_duration duration_arg)
Auxiliary functions needed for creation/destruction of MDL_ticket objects.
Definition: mdl.cc:1670
An abstract class for inspection of a connected subgraph of the wait-for graph.
Definition: mdl.h:923
MDL_wait_for_graph_visitor()
Definition: mdl.h:930
virtual bool enter_node(MDL_context *node)=0
virtual bool inspect_edge(MDL_context *dest)=0
virtual ~MDL_wait_for_graph_visitor()
uint m_lock_open_count
XXX, hack: During deadlock search, we may need to inspect TABLE_SHAREs and acquire LOCK_open.
Definition: mdl.h:941
virtual void leave_node(MDL_context *node)=0
Abstract class representing an edge in the waiters graph to be traversed by deadlock detection algori...
Definition: mdl.h:949
static const uint DEADLOCK_WEIGHT_CO
Definition: mdl.h:959
static const uint DEADLOCK_WEIGHT_ULL
Definition: mdl.h:961
static const uint DEADLOCK_WEIGHT_DML
Definition: mdl.h:960
virtual ~MDL_wait_for_subgraph()
static const uint DEADLOCK_WEIGHT_DDL
Definition: mdl.h:962
virtual bool accept_visitor(MDL_wait_for_graph_visitor *gvisitor)=0
Accept a wait-for graph visitor to inspect the node this edge is leading to.
virtual uint get_deadlock_weight() const =0
A reliable way to wait on an MDL lock.
Definition: mdl.h:1347
enum_wait_status m_wait_status
Definition: mdl.h:1373
bool set_status(enum_wait_status result_arg)
Set the status unless it's already set.
Definition: mdl.cc:1766
enum_wait_status
Definition: mdl.h:1354
@ KILLED
Definition: mdl.h:1354
@ WS_EMPTY
Definition: mdl.h:1354
@ TIMEOUT
Definition: mdl.h:1354
@ GRANTED
Definition: mdl.h:1354
@ VICTIM
Definition: mdl.h:1354
MDL_wait()
Construct an empty wait slot.
Definition: mdl.cc:1749
void reset_status()
Clear the current value of the wait slot.
Definition: mdl.cc:1790
mysql_cond_t m_COND_wait_status
Definition: mdl.h:1372
~MDL_wait()
Destroy system resources.
Definition: mdl.cc:1756
enum_wait_status timed_wait(MDL_context_owner *owner, struct timespec *abs_timeout, bool signal_timeout, const PSI_stage_info *wait_state_name)
Wait for the status to be assigned to this wait slot.
Definition: mdl.cc:1810
enum_wait_status get_status()
Query the current value of the wait slot.
Definition: mdl.cc:1780
mysql_mutex_t m_LOCK_wait_status
Condvar which is used for waiting until this context's pending request can be satisfied or this threa...
Definition: mdl.h:1371
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
#define mysql_prlock_wrlock(T)
Definition: mysql_rwlock.h:76
#define mysql_prlock_rdlock(T)
Definition: mysql_rwlock.h:66
#define mysql_prlock_unlock(T)
Definition: mysql_rwlock.h:96
mysql_mutex_t LOCK_open
LOCK_open protects the following variables/objects:
Definition: sql_base.cc:275
struct PSI_metadata_lock PSI_metadata_lock
Definition: psi_mdl_bits.h:52
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
void mdl_init()
Initialize the metadata locking subsystem.
Definition: mdl.cc:1075
I_P_List< MDL_request, I_P_List_adapter< MDL_request, &MDL_request::next_in_list, &MDL_request::prev_in_list >, I_P_List_counter > MDL_request_list
Definition: mdl.h:1408
#define MAX_MDLKEY_LENGTH
Maximal length of key for metadata locking subsystem.
Definition: mdl.h:355
int32 mdl_locks_unused_locks_low_water
Threshold for number of unused MDL_lock objects.
Definition: mdl.cc:286
const int32 MDL_LOCKS_UNUSED_LOCKS_LOW_WATER_DEFAULT
Default value for threshold for number of unused MDL_lock objects after exceeding which we start cons...
Definition: mdl.h:1737
int32 mdl_get_unused_locks_count()
Get number of unused MDL_lock objects in MDL_map cache.
Definition: mdl.cc:1105
enum_mdl_duration
Duration of metadata lock.
Definition: mdl.h:334
@ MDL_TRANSACTION
Locks with transaction duration are automatically released at the end of transaction.
Definition: mdl.h:344
@ MDL_STATEMENT
Locks with statement duration are automatically released at the end of statement or transaction.
Definition: mdl.h:339
@ MDL_EXPLICIT
Locks with explicit duration survive the end of statement and transaction.
Definition: mdl.h:349
@ MDL_DURATION_END
Definition: mdl.h:351
@ MDL_SHARED_WRITE
Definition: mdl.h:270
@ MDL_SHARED
Definition: mdl.h:232
@ MDL_SHARED_UPGRADABLE
Definition: mdl.h:287
@ MDL_SHARED_READ_ONLY
Definition: mdl.h:293
@ MDL_SHARED_HIGH_PRIO
Definition: mdl.h:249
@ MDL_SHARED_NO_WRITE
Definition: mdl.h:307
@ MDL_SHARED_NO_READ_WRITE
Definition: mdl.h:319
@ MDL_SHARED_WRITE_LOW_PRIO
Definition: mdl.h:276
@ MDL_SHARED_READ
Definition: mdl.h:260
@ MDL_TYPE_END
Definition: mdl.h:329
@ MDL_EXCLUSIVE
Definition: mdl.h:327
@ MDL_INTENTION_EXCLUSIVE
Definition: mdl.h:209
const double MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO
Ratio of unused/total MDL_lock objects after exceeding which we start trying to free unused MDL_lock ...
Definition: mdl.h:1746
void mdl_destroy()
Release resources of metadata locking subsystem.
Definition: mdl.cc:1093
ulong max_write_lock_count
Definition: thr_lock.cc:124
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
#define INT_MAX32
Definition: my_inttypes.h:78
int32_t int32
Definition: my_inttypes.h:66
uint16_t uint16
Definition: my_inttypes.h:65
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Common header for many mysys elements.
Defines for getting and processing the current system type programmatically.
std::uint64_t Timeout_type
Type alias to reduce chance of conversion errors on timeout values.
Definition: my_systime.h:127
Common definition between mysql server & client.
#define NAME_LEN
Definition: mysql_com.h:67
#define NAME_CHAR_LEN
Field/table name length.
Definition: mysql_com.h:60
Instrumentation helpers for conditions.
ABI for instrumented mutexes.
Instrumentation helpers for rwlock.
MDL_request * mdl_request(const Import_target &t, MEM_ROOT *mem_root)
Creates an MDL_request for exclusive MDL on the table being imported.
Definition: sdi_api.cc:247
MDL_request * mdl_req(THD *thd, const Tablespace_table_ref &tref, enum enum_mdl_type mdl_type)
Create am MDL_request for a the table identified by a Tablespace_table_ref.
Definition: tablespace_impl.cc:534
Definition: mdl.h:59
bool test_drive_fix_pins(MDL_context *)
size_t size(const char *const c)
Definition: base64.h:46
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Instrumentation helpers for rwlock.
Performance schema instrumentation interface.
Performance schema instrumentation interface.
required string type
Definition: replication_group_member_actions.proto:34
enum_mdl_type
Type of metadata lock request.
Definition: sql_lexer_yacc_state.h:106
case opt name
Definition: sslopt-case.h:29
char * strmake(char *dst, const char *src, size_t length)
Definition: strmake.cc:42
Hook class which via its methods specifies which members of T should be used for participating in a i...
Definition: sql_plist.h:198
Definition: lf.h:86
Metadata lock object key.
Definition: mdl.h:366
uint16 m_db_name_length
Definition: mdl.h:789
int cmp(const MDL_key *rhs) const
Compare two MDL keys lexicographically.
Definition: mdl.h:739
const char * db_name() const
Definition: mdl.h:429
static void init_psi_keys()
Definition: mdl.cc:139
uint db_name_length() const
Definition: mdl.h:430
MDL_key(const MDL_key &rhs)
Definition: mdl.h:753
uint name_length() const
Definition: mdl.h:436
bool is_equal(const MDL_key *rhs) const
Definition: mdl.h:732
const char * name() const
Definition: mdl.h:432
void mdl_key_init(enum_mdl_namespace mdl_namespace, const char *db, const char *name)
Construct a metadata lock key from a triplet (mdl_namespace, database and name).
Definition: mdl.h:477
enum_mdl_namespace
Object namespaces.
Definition: mdl.h:402
@ BACKUP_LOCK
Definition: mdl.h:404
@ EVENT
Definition: mdl.h:411
@ GLOBAL
Definition: mdl.h:403
@ LOCKING_SERVICE
Definition: mdl.h:414
@ USER_LEVEL_LOCK
Definition: mdl.h:413
@ COLUMN_STATISTICS
Definition: mdl.h:417
@ COMMIT
Definition: mdl.h:412
@ RESOURCE_GROUPS
Definition: mdl.h:418
@ SCHEMA
Definition: mdl.h:406
@ NAMESPACE_END
Definition: mdl.h:423
@ FUNCTION
Definition: mdl.h:408
@ TRIGGER
Definition: mdl.h:410
@ ACL_CACHE
Definition: mdl.h:416
@ FOREIGN_KEY
Definition: mdl.h:419
@ SRID
Definition: mdl.h:415
@ PROCEDURE
Definition: mdl.h:409
@ LIBRARY
Definition: mdl.h:421
@ TABLESPACE
Definition: mdl.h:405
@ TABLE
Definition: mdl.h:407
@ CHECK_CONSTRAINT
Definition: mdl.h:420
enum_mdl_namespace mdl_namespace() const
Definition: mdl.h:462
void mdl_key_init(const MDL_key *rhs)
Definition: mdl.h:716
void mdl_key_init(enum_mdl_namespace mdl_namespace, const char *db, const char *normalized_name, size_t normalized_name_len, const char *name)
Construct a metadata lock key from a quadruplet (mdl_namespace, database, normalized object name buff...
Definition: mdl.h:641
uint16 m_length
Definition: mdl.h:788
MDL_key()=default
uint length() const
Definition: mdl.h:427
char m_ptr[MAX_MDLKEY_LENGTH]
Definition: mdl.h:791
const uchar * ptr() const
Definition: mdl.h:426
const char * col_name() const
Definition: mdl.h:438
MDL_key & operator=(const MDL_key &rhs)
Definition: mdl.h:755
uint col_name_length() const
Definition: mdl.h:450
void mdl_key_init(enum_mdl_namespace mdl_namespace, const char *part_key, size_t part_key_length, size_t db_length)
Construct a metadata lock key from namespace and partial key, which contains info about object databa...
Definition: mdl.h:692
void reset()
Definition: mdl.h:726
MDL_key(enum_mdl_namespace namespace_arg, const char *db_arg, const char *name_arg)
Definition: mdl.h:760
uint16 m_object_name_length
Definition: mdl.h:790
void mdl_key_init(enum_mdl_namespace mdl_namespace, const char *db, const char *name, const char *column_name)
Construct a metadata lock key from a quadruplet (mdl_namespace, database, table and column name).
Definition: mdl.h:524
bool use_normalized_object_name() const
Check if normalized object name should be used.
Definition: mdl.h:781
const PSI_stage_info * get_wait_state_name() const
Get thread state name to be used in case when we have to wait on resource identified by key.
Definition: mdl.h:770
static PSI_stage_info m_namespace_to_wait_state_name[NAMESPACE_END]
Thread state names to be used in case when we have to wait on resource belonging to certain namespace...
Definition: mdl.h:792
Definition: mdl.h:1139
MDL_ticket * m_mat_front
m_mat_front tracks what was the front of m_ticket_list, the last time MDL_context::materialize_fast_p...
Definition: mdl.h:1152
Ticket_p_list m_ticket_list
Definition: mdl.h:1140
Definition: mdl.h:1157
size_t operator()(const MDL_key *k) const
Definition: mdl.cc:4649
Definition: mdl.h:1161
bool operator()(const MDL_key *a, const MDL_key *b) const
Definition: mdl.h:1162
Utility struct for representing a ticket pointer and its duration.
Definition: mdl.h:1124
enum_mdl_duration m_dur
Definition: mdl.h:1125
MDL_ticket_handle(MDL_ticket *t, enum_mdl_duration d)
Definition: mdl.h:1129
MDL_ticket * m_ticket
Definition: mdl.h:1126
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:145
Stage instrument information.
Definition: psi_stage_bits.h:74
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
An instrumented prlock structure.
Definition: mysql_rwlock_bits.h:72