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