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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
Copyright (c) 2023, MariaDB plc
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
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 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-1335 USA
*/
#include "my_global.h"
#include "handler.h"
#include "sql_class.h"
#include "log_cache.h"
static handlerton *online_alter_hton;
typedef void *sv_id_t;
struct Online_alter_cache_list: ilist<online_alter_cache_data>
{
sv_id_t savepoint_id= 0;
};
struct table_savepoint: ilist_node<>
{
sv_id_t id;
my_off_t log_prev_pos;
table_savepoint(sv_id_t id, my_off_t pos): id(id), log_prev_pos(pos){}
};
class online_alter_cache_data: public ilist_node<>,
public binlog_cache_data
{
public:
online_alter_cache_data() : binlog_cache_data(false),
hton(nullptr), sink_log(nullptr) { }
void store_prev_position()
{
before_stmt_pos= my_b_write_tell(&cache_log);
}
handlerton *hton;
Cache_flip_event_log *sink_log;
ilist<table_savepoint> sv_list;
/**
Finds savepoint with specified id and returns its associated data.
Cleans up all the savepoints up to the found one.
*/
my_off_t pop_sv_until(sv_id_t id)
{
my_off_t pos= 0;
auto it= sv_list.begin();
auto sentinel= it->prev;
while (pos == 0 && it != sv_list.end())
{
table_savepoint &sv= *it;
++it;
if (sv.id == id)
pos= sv.log_prev_pos;
delete &sv;
}
sentinel->next= &*it; // drop the range from the list
it->prev= sentinel;
return pos;
}
void cleanup_sv()
{
pop_sv_until((sv_id_t)1); // Erase the whole list
}
};
static
online_alter_cache_data *setup_cache_data(MEM_ROOT *root, TABLE_SHARE *share)
{
static ulong online_alter_cache_use= 0, online_alter_cache_disk_use= 0;
auto cache= new online_alter_cache_data();
if (!cache || open_cached_file(&cache->cache_log, mysql_tmpdir, LOG_PREFIX,
(size_t)binlog_cache_size, MYF(MY_WME)))
{
delete cache;
return NULL;
}
share->online_alter_binlog->acquire();
cache->hton= share->db_type();
cache->sink_log= share->online_alter_binlog;
my_off_t binlog_max_size= SIZE_T_MAX; // maximum possible cache size
DBUG_EXECUTE_IF("online_alter_small_cache", binlog_max_size= 4096;);
cache->set_binlog_cache_info(binlog_max_size,
&online_alter_cache_use,
&online_alter_cache_disk_use);
cache->store_prev_position();
return cache;
}
static Online_alter_cache_list &get_cache_list(handlerton *ht, THD *thd)
{
void *data= thd_get_ha_data(thd, ht);
DBUG_ASSERT(data);
return *(Online_alter_cache_list*)data;
}
static Online_alter_cache_list &get_or_create_cache_list(THD *thd)
{
void *data= thd_get_ha_data(thd, online_alter_hton);
if (!data)
{
data= new Online_alter_cache_list();
thd_set_ha_data(thd, online_alter_hton, data);
}
return *(Online_alter_cache_list*)data;
}
static online_alter_cache_data* get_cache_data(THD *thd, TABLE *table)
{
auto &cache_list= get_or_create_cache_list(thd);
/* we assume it's very rare to have more than one online ALTER running */
for (auto &cache: cache_list)
{
if (cache.sink_log == table->s->online_alter_binlog)
return &cache;
}
MEM_ROOT *root= &thd->transaction->mem_root;
auto *new_cache_data= setup_cache_data(root, table->s);
cache_list.push_back(*new_cache_data);
return new_cache_data;
}
int online_alter_log_row(TABLE* table, const uchar *before_record,
const uchar *after_record, Log_func *log_func)
{
THD *thd= table->in_use;
if (!table->online_alter_cache)
{
table->online_alter_cache= get_cache_data(thd, table);
DBUG_ASSERT(table->online_alter_cache->cache_log.type == WRITE_CACHE);
trans_register_ha(thd, false, online_alter_hton, 0);
if (thd->in_multi_stmt_transaction_mode())
trans_register_ha(thd, true, online_alter_hton, 0);
}
// We need to log all columns for the case if alter table changes primary key
DBUG_ASSERT(!before_record || bitmap_is_set_all(table->read_set));
MY_BITMAP *old_rpl_write_set= table->rpl_write_set;
table->rpl_write_set= &table->s->all_set;
table->online_alter_cache->store_prev_position();
int error= (*log_func)(thd, table, table->s->online_alter_binlog,
table->online_alter_cache,
table->file->has_transactions_and_rollback(),
BINLOG_ROW_IMAGE_FULL,
before_record, after_record);
table->rpl_write_set= old_rpl_write_set;
if (unlikely(error))
{
table->online_alter_cache->restore_prev_position();
return HA_ERR_RBR_LOGGING_FAILED;
}
return 0;
}
static void cleanup_cache_list(ilist<online_alter_cache_data> &list)
{
auto it= list.begin();
while (it != list.end())
{
auto &cache= *it++;
cache.sink_log->release();
cache.reset();
cache.cleanup_sv();
delete &cache;
}
list.clear();
DBUG_ASSERT(list.empty());
}
static
int online_alter_end_trans(Online_alter_cache_list &cache_list, THD *thd,
bool is_ending_transaction, bool commit)
{
DBUG_ENTER("online_alter_end_trans");
int error= 0;
if (cache_list.empty())
DBUG_RETURN(0);
for (auto &cache: cache_list)
{
auto *binlog= cache.sink_log;
DBUG_ASSERT(binlog);
bool non_trans= cache.hton->flags & HTON_NO_ROLLBACK // Aria
|| !cache.hton->rollback;
bool do_commit= (commit && is_ending_transaction) || non_trans;
if (commit || non_trans)
{
// Do not set STMT_END for last event to leave table open in altering thd
error= binlog_flush_pending_rows_event(thd, false, true, binlog, &cache);
}
if (do_commit)
{
/*
If the cache wasn't reinited to write, then it remains empty after
the last write.
*/
if (my_b_bytes_in_cache(&cache.cache_log) && likely(!error))
{
DBUG_ASSERT(cache.cache_log.type != READ_CACHE);
mysql_mutex_lock(binlog->get_log_lock());
error= binlog->write_cache_raw(thd, &cache.cache_log);
mysql_mutex_unlock(binlog->get_log_lock());
if (!is_ending_transaction)
cache.reset();
}
}
else if (!commit) // rollback
{
DBUG_ASSERT(!non_trans);
cache.restore_prev_position();
}
else
{
DBUG_ASSERT(!is_ending_transaction);
cache.store_prev_position();
}
if (error)
{
my_error(ER_ERROR_ON_WRITE, MYF(ME_ERROR_LOG),
binlog->get_name(), errno);
break;
}
}
if (is_ending_transaction)
cleanup_cache_list(cache_list);
DBUG_RETURN(error);
}
void cleanup_tables(THD *thd)
{
for (TABLE *table= thd->open_tables; table; table= table->next)
table->online_alter_cache= NULL;
}
static
int online_alter_savepoint_set(handlerton *hton, THD *thd, sv_id_t sv_id)
{
DBUG_ENTER("binlog_online_alter_savepoint");
auto &cache_list= get_cache_list(hton, thd);
if (cache_list.empty())
DBUG_RETURN(0);
for (auto &cache: cache_list)
{
if (cache.hton->savepoint_set == NULL)
continue;
auto *sv= new table_savepoint(sv_id, cache.get_byte_position());
if(unlikely(sv == NULL))
DBUG_RETURN(1);
cache.sv_list.push_front(*sv);
}
DBUG_RETURN(0);
}
static
int online_alter_savepoint_rollback(handlerton *hton, THD *thd, sv_id_t sv_id)
{
DBUG_ENTER("online_alter_savepoint_rollback");
auto &cache_list= get_cache_list(hton, thd);
for (auto &cache: cache_list)
{
if (cache.hton->savepoint_set == NULL)
continue;
// There's no savepoint if it was set up before online table was modified.
// In that case, restore to 0.
my_off_t pos= cache.pop_sv_until(sv_id);
cache.restore_savepoint(pos);
}
DBUG_RETURN(0);
}
static int online_alter_commit(handlerton *hton, THD *thd, bool all)
{
int res;
bool is_ending_transaction= ending_trans(thd, all);
if (is_ending_transaction
&& thd->transaction->xid_state.get_state_code() == XA_PREPARED)
{
res= hton->commit_by_xid(hton, thd->transaction->xid_state.get_xid());
// cleanup was already done by prepare()
}
else
{
res= online_alter_end_trans(get_cache_list(hton, thd), thd,
is_ending_transaction, true);
cleanup_tables(thd);
}
return res;
};
static int online_alter_rollback(handlerton *hton, THD *thd, bool all)
{
int res;
bool is_ending_transaction= ending_trans(thd, all);
if (is_ending_transaction
&& thd->transaction->xid_state.get_state_code() == XA_PREPARED)
{
res= hton->rollback_by_xid(hton, thd->transaction->xid_state.get_xid());
// cleanup was already done by prepare()
}
else
{
res= online_alter_end_trans(get_cache_list(hton, thd), thd,
is_ending_transaction, false);
cleanup_tables(thd);
}
return res;
};
static int online_alter_prepare(handlerton *hton, THD *thd, bool all)
{
auto &cache_list= get_cache_list(hton, thd);
int res= 0;
if (ending_trans(thd, all))
{
thd->transaction->xid_state.set_online_alter_cache(&cache_list);
thd_set_ha_data(thd, hton, NULL);
}
else
{
res= online_alter_end_trans(cache_list, thd, false, true);
}
cleanup_tables(thd);
return res;
};
static int online_alter_commit_by_xid(handlerton *hton, XID *x)
{
auto *xid= static_cast<XA_data*>(x);
if (likely(xid->online_alter_cache == NULL))
return 1;
int res= online_alter_end_trans(*xid->online_alter_cache, current_thd,
true, true);
delete xid->online_alter_cache;
xid->online_alter_cache= NULL;
return res;
};
static int online_alter_rollback_by_xid(handlerton *hton, XID *x)
{
auto *xid= static_cast<XA_data*>(x);
if (likely(xid->online_alter_cache == NULL))
return 1;
int res= online_alter_end_trans(*xid->online_alter_cache, current_thd,
true, false);
delete xid->online_alter_cache;
xid->online_alter_cache= NULL;
return res;
};
static int online_alter_close_connection(handlerton *hton, THD *thd)
{
auto *cache_list= (Online_alter_cache_list*)thd_get_ha_data(thd, hton);
DBUG_ASSERT(!cache_list || cache_list->empty());
delete cache_list;
thd_set_ha_data(thd, hton, NULL);
return 0;
}
static int online_alter_log_init(void *p)
{
online_alter_hton= (handlerton *)p;
online_alter_hton->db_type= DB_TYPE_ONLINE_ALTER;
online_alter_hton->savepoint_offset= 0;
online_alter_hton->close_connection= online_alter_close_connection;
online_alter_hton->savepoint_set= online_alter_savepoint_set;
online_alter_hton->savepoint_rollback= online_alter_savepoint_rollback;
online_alter_hton->savepoint_rollback_can_release_mdl=
[](handlerton *hton, THD *thd){ return true; };
online_alter_hton->commit= online_alter_commit;
online_alter_hton->rollback= online_alter_rollback;
online_alter_hton->recover= [](handlerton*, XID*, uint){ return 0; };
online_alter_hton->prepare= online_alter_prepare;
online_alter_hton->commit_by_xid= online_alter_commit_by_xid;
online_alter_hton->rollback_by_xid= online_alter_rollback_by_xid;
online_alter_hton->drop_table= [](handlerton *, const char*) { return -1; };
online_alter_hton->flags= HTON_NOT_USER_SELECTABLE | HTON_HIDDEN
| HTON_NO_ROLLBACK;
return 0;
}
struct st_mysql_storage_engine online_alter_storage_engine=
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
maria_declare_plugin(online_alter_log)
{
MYSQL_STORAGE_ENGINE_PLUGIN,
&online_alter_storage_engine,
"online_alter_log",
"MariaDB PLC",
"A pseudo storage engine for the online alter log",
PLUGIN_LICENSE_GPL,
online_alter_log_init,
NULL,
0x0100, // 1.0
NULL, // no status vars
NULL, // no sysvars
"1.0",
MariaDB_PLUGIN_MATURITY_STABLE
}
maria_declare_plugin_end;
|