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