-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathrpl_transaction_write_set_ctx.cc
322 lines (264 loc) · 10.2 KB
/
rpl_transaction_write_set_ctx.cc
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Copyright (c) 2014, 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 */
#include "sql/rpl_transaction_write_set_ctx.h"
#include <stddef.h>
#include <utility>
#include "my_dbug.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/service_rpl_transaction_write_set.h" // Transaction_write_set
#include "sql/binlog.h"
#include "sql/current_thd.h" // current_thd
#include "sql/debug_sync.h" // debug_sync_set_action
#include "sql/mysqld_thd_manager.h" // Global_THD_manager
#include "sql/psi_memory_key.h"
#include "sql/sql_class.h" // THD
#include "sql/transaction_info.h"
#include "string_with_len.h"
std::atomic<bool>
Rpl_transaction_write_set_ctx::m_global_component_requires_write_sets(
false);
std::atomic<uint64>
Rpl_transaction_write_set_ctx::m_global_write_set_memory_size_limit(0);
Rpl_transaction_write_set_ctx::Rpl_transaction_write_set_ctx()
: m_has_missing_keys(false),
m_has_related_foreign_keys(false),
m_ignore_write_set_memory_limit(false),
m_local_allow_drop_write_set(false),
m_local_has_reached_write_set_limit(false) {
DBUG_TRACE;
/*
In order to speed-up small transactions write-set extraction,
we preallocate 12 elements.
12 is a sufficient number to hold write-sets for single
statement transactions, even on tables with foreign keys.
*/
write_set.reserve(12);
}
bool Rpl_transaction_write_set_ctx::add_write_set(uint64 hash) {
DBUG_TRACE;
DBUG_EXECUTE_IF("add_write_set_no_memory", throw std::bad_alloc(););
if (!m_local_has_reached_write_set_limit) {
ulong binlog_trx_dependency_history_size =
mysql_bin_log.m_dependency_tracker.get_writeset()
->m_opt_max_history_size;
bool is_full_writeset_required =
m_global_component_requires_write_sets && !m_local_allow_drop_write_set;
if (!is_full_writeset_required) {
if (write_set.size() >= binlog_trx_dependency_history_size) {
m_local_has_reached_write_set_limit = true;
clear_write_set();
return false;
}
}
uint64 mem_limit = m_global_write_set_memory_size_limit;
if (mem_limit && !m_ignore_write_set_memory_limit) {
// Check if adding a new element goes over the limit
if (sizeof(uint64) + write_set_memory_size() > mem_limit) {
my_error(ER_WRITE_SET_EXCEEDS_LIMIT, MYF(0));
return true;
}
}
write_set.push_back(hash);
}
return false;
}
std::vector<uint64> *Rpl_transaction_write_set_ctx::get_write_set() {
DBUG_TRACE;
return &write_set;
}
void Rpl_transaction_write_set_ctx::reset_state() {
DBUG_TRACE;
clear_write_set();
m_has_missing_keys = m_has_related_foreign_keys = false;
m_local_has_reached_write_set_limit = false;
}
void Rpl_transaction_write_set_ctx::clear_write_set() {
DBUG_TRACE;
write_set.clear();
savepoint.clear();
savepoint_list.clear();
}
void Rpl_transaction_write_set_ctx::set_has_missing_keys() {
DBUG_TRACE;
m_has_missing_keys = true;
}
bool Rpl_transaction_write_set_ctx::get_has_missing_keys() {
DBUG_TRACE;
return m_has_missing_keys;
}
void Rpl_transaction_write_set_ctx::set_has_related_foreign_keys() {
DBUG_TRACE;
m_has_related_foreign_keys = true;
}
bool Rpl_transaction_write_set_ctx::get_has_related_foreign_keys() {
DBUG_TRACE;
return m_has_related_foreign_keys;
}
bool Rpl_transaction_write_set_ctx::was_write_set_limit_reached() {
DBUG_TRACE;
return m_local_has_reached_write_set_limit;
}
size_t Rpl_transaction_write_set_ctx::write_set_memory_size() {
DBUG_TRACE;
return sizeof(uint64) * write_set.size();
}
void Rpl_transaction_write_set_ctx::set_global_require_full_write_set(
bool requires_ws) {
assert(!requires_ws || !m_global_component_requires_write_sets);
m_global_component_requires_write_sets = requires_ws;
}
void require_full_write_set(bool requires_ws) {
Rpl_transaction_write_set_ctx::set_global_require_full_write_set(requires_ws);
}
void Rpl_transaction_write_set_ctx::set_global_write_set_memory_size_limit(
uint64 limit) {
assert(m_global_write_set_memory_size_limit == 0);
m_global_write_set_memory_size_limit = limit;
}
void Rpl_transaction_write_set_ctx::update_global_write_set_memory_size_limit(
uint64 limit) {
m_global_write_set_memory_size_limit = limit;
}
void set_write_set_memory_size_limit(uint64 size_limit) {
Rpl_transaction_write_set_ctx::set_global_write_set_memory_size_limit(
size_limit);
}
void update_write_set_memory_size_limit(uint64 size_limit) {
Rpl_transaction_write_set_ctx::update_global_write_set_memory_size_limit(
size_limit);
}
void Rpl_transaction_write_set_ctx::set_local_ignore_write_set_memory_limit(
bool ignore_limit) {
m_ignore_write_set_memory_limit = ignore_limit;
}
void Rpl_transaction_write_set_ctx::set_local_allow_drop_write_set(
bool allow_drop_write_set) {
m_local_allow_drop_write_set = allow_drop_write_set;
}
/**
Implementation of service_rpl_transaction_write_set, see
@file include/mysql/service_rpl_transaction_write_set.h
*/
Transaction_write_set *get_transaction_write_set(unsigned long m_thread_id) {
DBUG_TRACE;
Transaction_write_set *result_set = nullptr;
Find_thd_with_id find_thd_with_id(m_thread_id);
THD_ptr thd_ptr =
Global_THD_manager::get_instance()->find_thd(&find_thd_with_id);
if (thd_ptr) {
Rpl_transaction_write_set_ctx *transaction_write_set_ctx =
thd_ptr->get_transaction()->get_transaction_write_set_ctx();
int write_set_size = transaction_write_set_ctx->get_write_set()->size();
if (write_set_size == 0) return nullptr;
result_set = (Transaction_write_set *)my_malloc(
key_memory_write_set_extraction, sizeof(Transaction_write_set), MYF(0));
result_set->write_set_size = write_set_size;
result_set->write_set = (unsigned long long *)my_malloc(
key_memory_write_set_extraction,
write_set_size * sizeof(unsigned long long), MYF(0));
int result_set_index = 0;
for (std::vector<uint64>::iterator it =
transaction_write_set_ctx->get_write_set()->begin();
it != transaction_write_set_ctx->get_write_set()->end(); ++it) {
uint64 temp = *it;
result_set->write_set[result_set_index++] = temp;
}
}
return result_set;
}
void Rpl_transaction_write_set_ctx::add_savepoint(char *name) {
DBUG_TRACE;
std::string identifier(name);
DBUG_EXECUTE_IF("transaction_write_set_savepoint_clear_on_commit_rollback", {
assert(savepoint.size() == 0);
assert(write_set.size() == 0);
assert(savepoint_list.size() == 0);
});
DBUG_EXECUTE_IF("transaction_write_set_savepoint_level",
assert(savepoint.size() == 0););
std::map<std::string, size_t>::iterator it;
/*
Savepoint with the same name, the old savepoint is deleted and a new one
is set
*/
if ((it = savepoint.find(name)) != savepoint.end()) savepoint.erase(it);
savepoint.insert(
std::pair<std::string, size_t>(identifier, write_set.size()));
DBUG_EXECUTE_IF(
"transaction_write_set_savepoint_add_savepoint",
assert(savepoint.find(identifier)->second == write_set.size()););
}
void Rpl_transaction_write_set_ctx::del_savepoint(char *name) {
DBUG_TRACE;
std::string identifier(name);
DBUG_EXECUTE_IF("transaction_write_set_savepoint_block_before_release", {
const char act[] = "now wait_for signal.unblock_release";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
});
savepoint.erase(identifier);
}
void Rpl_transaction_write_set_ctx::rollback_to_savepoint(char *name) {
DBUG_TRACE;
size_t position = 0;
std::string identifier(name);
std::map<std::string, size_t>::iterator elem;
if ((elem = savepoint.find(identifier)) != savepoint.end()) {
assert(elem->second <= write_set.size());
DBUG_EXECUTE_IF("transaction_write_set_savepoint_block_before_rollback", {
const char act[] = "now wait_for signal.unblock_rollback";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
});
position = elem->second;
// Remove all savepoints created after the savepoint identifier given as
// parameter
std::map<std::string, size_t>::iterator it = savepoint.begin();
while (it != savepoint.end()) {
if (it->second > position)
savepoint.erase(it++);
else
++it;
}
/*
We need to check that:
- starting index of the range we want to erase does exist.
- write_set size have elements to be removed
*/
if (write_set.size() > 0 && position < write_set.size()) {
// Clear all elements after savepoint
write_set.erase(write_set.begin() + position, write_set.end());
}
DBUG_EXECUTE_IF("transaction_write_set_savepoint_add_savepoint",
assert(write_set.size() == 1););
DBUG_EXECUTE_IF("transaction_write_set_size_2",
assert(write_set.size() == 2););
}
}
void Rpl_transaction_write_set_ctx::reset_savepoint_list() {
DBUG_TRACE;
savepoint_list.push_back(savepoint);
savepoint.clear();
}
void Rpl_transaction_write_set_ctx::restore_savepoint_list() {
DBUG_TRACE;
if (!savepoint_list.empty()) {
savepoint = savepoint_list.back();
savepoint_list.pop_back();
}
}