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