MySQL 8.0.40
Source Code Documentation
dd_kill_immunizer.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef DD_KILL_IMMUNIZER_INCLUDED
25#define DD_KILL_IMMUNIZER_INCLUDED
26
27#include "mutex_lock.h" // MUTEX_LOCK
28#include "sql/sql_class.h" // THD
29
30namespace dd {
31
32/**
33 RAII class for immunizing the THD from kill operations.
34
35 Interruptions to operations on new Data Dictionary tables due to KILL QUERY,
36 KILL CONNECTION or statement execution timeout would leave DD in inconsistent
37 state. So the operations on the New Data Dictionary tables are made immune to
38 these operations using DD_kill_immunizer.
39
40 Note:
41 DD operations are made immune to KILL operations till WL#7743 and WL#7016
42 are implemented. So as part of these WL's DD_kill_immunizer should be
43 removed.
44*/
45
47 public:
49 : m_thd(thd), m_killed_state(THD::NOT_KILLED), m_is_active(true) {
50 MUTEX_LOCK(thd_data_lock, &thd->LOCK_thd_data);
51
52 // If DD_kill_immunizer is initialized as part of nested Transaction_ro's
53 // then store reference to parent kill_immunizer else NULL value is saved in
54 // m_saved_kill_immunizer.
56
57 // Save either thd->killed value or parent kill_immunizer's m_killed_state
58 // value.
61 : thd->killed.load();
62
63 // Set current DD_kill_immunizer object to THD.
64 thd->kill_immunizer = this;
65
66 // Set killed state of THD as NOT_KILLED.
68 // No OOM error while immunizer is active.
70 }
71
73 MUTEX_LOCK(thd_data_lock, &m_thd->LOCK_thd_data);
74
75 /*
76 Current instance is of top level kill immunizer, set kill immune mode to
77 inactive(or exiting).
78 */
79 if (m_saved_kill_immunizer == nullptr) {
80 m_is_active = false;
82 } else
83 // Set kill_immunizer of THD to parent. We must do it before calling awake
84 // to transfer the kill state to parent kill_immunizer.
86
87 // If there were any concurrent kill operations in kill immune mode, call
88 // THD::awake() with the m_killed_state. This will either propagate the
89 // m_killed_state to the parent kill_immunizer and return or assign state to
90 // the THD::killed and complete the THD::awake(). Otherwise, if it is a top
91 // level kill immunizer just reset THD::killed state else we need not have
92 // to do anything.
95 else if (m_saved_kill_immunizer == nullptr)
97
98 // Reset kill_immunizer of THD.
100 }
101
102 /*
103 Check if thread is in the kill immune mode.
104 */
105 bool is_active() { return m_is_active; }
106
107 // Save kill state set while kill immune mode is active.
110
112 }
113
114 private:
116
117 // When kill_immunizer is set(i.e. operation on DD tables is in progress) to
118 // THD then there might be some concurrent KILL operations. The KILL state
119 // from those operations is stored in the m_killed_state. While exiting from
120 // the kill immune mode THD::awake() is called with this value.
122
123 // In case of nested Transaction_ro, m_saved_kill_immunizer is used to refer
124 // the parent Transaction_ro's kill_immunizer. This is used to propagate the
125 // m_killed_state to the parent kill_immunizer.
127
128 // THD::killed value is saved before entering the kill immune mode.
129 // If kill_immunizer is of some Transaction_ro in the nested Transaction_ro
130 // then parent kill_immunizer's m_killed_state is saved in the
131 // m_saved_killed_state for reference.
133
134 /*
135 kill immunizer state (active or exiting). The state is set to active when
136 kill immunizer is invoked. Kill_immunizer state is set to inactive(or
137 exiting) while exiting from the kill immune mode (current instance is of top
138 level kill immunizer at this point).
139 */
141};
142
143} // namespace dd
144#endif // DD_KILL_IMMUNIZER_INCLUDED
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
dd::DD_kill_immunizer * kill_immunizer
When operation on DD tables is in progress then THD is set to kill immune mode.
Definition: sql_class.h:2683
Thd_mem_cnt m_mem_cnt
Controlled memory stats for this session.
Definition: sql_class.h:937
void awake(THD::killed_state state_to_set)
Awake a thread.
Definition: sql_class.cc:1487
mysql_mutex_t LOCK_thd_data
Protects THD data accessed from other threads.
Definition: sql_class.h:1193
std::atomic< killed_state > killed
Definition: sql_class.h:2667
killed_state
Definition: sql_class.h:2660
@ NOT_KILLED
Definition: sql_class.h:2661
void restore_mode()
Restore original memory counter mode.
Definition: sql_class.h:279
void no_error_mode()
Set NO ERROR memory counter mode.
Definition: sql_class.h:283
RAII class for immunizing the THD from kill operations.
Definition: dd_kill_immunizer.h:46
THD::killed_state m_saved_killed_state
Definition: dd_kill_immunizer.h:132
~DD_kill_immunizer()
Definition: dd_kill_immunizer.h:72
bool is_active()
Definition: dd_kill_immunizer.h:105
THD::killed_state m_killed_state
Definition: dd_kill_immunizer.h:121
bool m_is_active
Definition: dd_kill_immunizer.h:140
THD * m_thd
Definition: dd_kill_immunizer.h:115
DD_kill_immunizer * m_saved_kill_immunizer
Definition: dd_kill_immunizer.h:126
void save_killed_state(THD::killed_state state)
Definition: dd_kill_immunizer.h:108
DD_kill_immunizer(THD *thd)
Definition: dd_kill_immunizer.h:48
#define mysql_mutex_assert_owner(M)
Wrapper, to use safe_mutex_assert_owner with instrumented mutexes.
Definition: mysql_mutex.h:112
#define MUTEX_LOCK(NAME, X)
Definition: mutex_lock.h:83
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43