MySQL  8.0.18
Source Code Documentation
dd_kill_immunizer.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
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 
29 namespace 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.
66  thd->killed = THD::NOT_KILLED;
67  }
68 
70  MUTEX_LOCK(thd_data_lock, &m_thd->LOCK_thd_data);
71 
72  /*
73  Current instance is of top level kill immunizer, set kill immune mode to
74  inactive(or exiting).
75  */
77  m_is_active = false;
78  else
79  // Set kill_immunizer of THD to parent. We must do it before calling awake
80  // to transfer the kill state to parent kill_immunizer.
82 
83  // If there were any concurrent kill operations in kill immune mode, call
84  // THD::awake() with the m_killed_state. This will either propagate the
85  // m_killed_state to the parent kill_immunizer and return or assign state to
86  // the THD::killed and complete the THD::awake(). Otherwise, if it is a top
87  // level kill immunizer just reset THD::killed state else we need not have
88  // to do anything.
89  if (m_killed_state)
91  else if (m_saved_kill_immunizer == NULL)
93 
94  // Reset kill_immunizer of THD.
96  }
97 
98  /*
99  Check if thread is in the kill immune mode.
100  */
101  bool is_active() { return m_is_active; }
102 
103  // Save kill state set while kill immune mode is active.
106 
108  }
109 
110  private:
112 
113  // When kill_immunizer is set(i.e. operation on DD tables is in progress) to
114  // THD then there might be some concurrent KILL operations. The KILL state
115  // from those operations is stored in the m_killed_state. While exiting from
116  // the kill immune mode THD::awake() is called with this value.
118 
119  // In case of nested Transaction_ro, m_saved_kill_immunizer is used to refer
120  // the parent Transaction_ro's kill_immunizer. This is used to propogate the
121  // m_killed_state to the parent kill_immunizer.
123 
124  // THD::killed value is saved before entering the kill immune mode.
125  // If kill_immunizer is of some Transaction_ro in the nested Transaction_ro
126  // then parent kill_immunizer's m_killed_state is saved in the
127  // m_saved_killed_state for reference.
129 
130  /*
131  kill immunizer state (active or exiting). The state is set to active when
132  kill immunizer is invoked. Kill_immunizer state is set to inactive(or
133  exiting) while exiting from the kill immune mode (current instance is of top
134  level kill immunizer at this point).
135  */
137 };
138 
139 } // namespace dd
140 #endif // DD_KILL_IMMUNIZER_INCLUDED
~DD_kill_immunizer()
Definition: dd_kill_immunizer.h:69
DD_kill_immunizer * m_saved_kill_immunizer
Definition: dd_kill_immunizer.h:122
bool m_is_active
Definition: dd_kill_immunizer.h:136
THD::killed_state m_killed_state
Definition: dd_kill_immunizer.h:117
std::atomic< killed_state > killed
Definition: sql_class.h:2296
THD::killed_state m_saved_killed_state
Definition: dd_kill_immunizer.h:128
void awake(THD::killed_state state_to_set)
Awake a thread.
Definition: sql_class.cc:1130
#define MUTEX_LOCK(NAME, X)
Definition: mutex_lock.h:60
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:2305
killed_state
Definition: sql_class.h:2289
#define true
Definition: config_static.h:44
void save_killed_state(THD::killed_state state)
Definition: dd_kill_immunizer.h:104
Definition: sql_class.h:2290
#define mysql_mutex_assert_owner(M)
Wrapper, to use safe_mutex_assert_owner with instrumented mutexes.
Definition: mysql_mutex.h:109
#define NULL
Definition: types.h:55
THD * m_thd
Definition: dd_kill_immunizer.h:111
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
mysql_mutex_t LOCK_thd_data
Protects THD data accessed from other threads.
Definition: sql_class.h:1006
DD_kill_immunizer(THD *thd)
Definition: dd_kill_immunizer.h:47
bool is_active()
Definition: dd_kill_immunizer.h:101
RAII class for immunizing the THD from kill operations.
Definition: dd_kill_immunizer.h:45
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778