1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include "read_replication.h"
24
#include "create_replication.h"
27
#include "dict0crea.ic"
32
#include "page0page.h"
33
#include "mach0data.h"
34
#include "dict0boot.h"
35
#include "dict0dict.h"
38
#include "row0mysql.h"
39
#include "pars0pars.h"
43
#include "row0merge.h"
44
#include "row0mysql.h"
46
UNIV_INTERN ulint dict_create_sys_replication_log(void)
52
mutex_enter(&(dict_sys->mutex));
54
table1 = dict_table_get_low("SYS_REPLICATION_LOG");
56
trx_sys_read_commit_id();
60
mutex_exit(&(dict_sys->mutex));
65
mutex_exit(&(dict_sys->mutex));
67
trx= trx_allocate_for_mysql();
69
trx->op_info= "creating replication sys table";
71
row_mysql_lock_data_dictionary(trx);
73
pars_info_t *info= pars_info_create();
76
error = que_eval_sql(info,
77
"PROCEDURE CREATE_SYS_REPLICATION_LOG_PROC () IS\n"
79
"CREATE TABLE SYS_REPLICATION_LOG(ID INT(8), SEGID INT, COMMIT_ID INT(8), END_TIMESTAMP INT(8), MESSAGE_LEN INT, MESSAGE BLOB);\n"
80
"CREATE UNIQUE CLUSTERED INDEX PRIMARY ON SYS_REPLICATION_LOG (ID, SEGID);\n"
81
"CREATE INDEX COMMIT_IDX ON SYS_REPLICATION_LOG (COMMIT_ID, ID);\n"
87
if (error != DB_SUCCESS)
89
fprintf(stderr, "InnoDB: error %lu in creation.\n", (ulong) error);
91
ut_a(error == DB_OUT_OF_FILE_SPACE || error == DB_TOO_MANY_CONCURRENT_TRXS);
94
"InnoDB: creation failed\n"
95
"InnoDB: tablespace is full\n"
96
"InnoDB: dropping incompletely created SYS_REPLICATION_LOG table.\n");
98
row_drop_table_for_mysql("SYS_REPLICATION_LOG", trx, TRUE);
100
error = DB_MUST_GET_MORE_FILE_SPACE;
103
trx_commit_for_mysql(trx);
105
row_mysql_unlock_data_dictionary(trx);
107
trx_free_for_mysql(trx);
112
UNIV_INTERN int read_replication_log_table_message(const char* table_name, drizzled::message::Table *table_message)
114
std::string search_string(table_name);
115
boost::algorithm::to_lower(search_string);
117
if (search_string.compare("sys_replication_log") != 0)
120
drizzled::message::Engine *engine= table_message->mutable_engine();
121
engine->set_name("InnoDB");
122
table_message->set_name("SYS_REPLICATION_LOG");
123
table_message->set_schema("DATA_DICTIONARY");
124
table_message->set_type(drizzled::message::Table::STANDARD);
125
table_message->set_creation_timestamp(0);
126
table_message->set_update_timestamp(0);
128
drizzled::message::Table::TableOptions *options= table_message->mutable_options();
129
options->set_collation_id(drizzled::my_charset_bin.number);
130
options->set_collation(drizzled::my_charset_bin.name);
132
drizzled::message::Table::Field *field= table_message->add_field();
133
field->set_name("ID");
134
field->set_type(drizzled::message::Table::Field::BIGINT);
136
field= table_message->add_field();
137
field->set_name("SEGID");
138
field->set_type(drizzled::message::Table::Field::INTEGER);
140
field= table_message->add_field();
141
field->set_name("COMMIT_ID");
142
field->set_type(drizzled::message::Table::Field::BIGINT);
144
field= table_message->add_field();
145
field->set_name("END_TIMESTAMP");
146
field->set_type(drizzled::message::Table::Field::BIGINT);
148
field= table_message->add_field();
149
field->set_name("MESSAGE_LEN");
150
field->set_type(drizzled::message::Table::Field::INTEGER);
152
field= table_message->add_field();
153
field->set_name("MESSAGE");
154
field->set_type(drizzled::message::Table::Field::BLOB);
155
drizzled::message::Table::Field::StringFieldOptions *stropt= field->mutable_string_options();
156
stropt->set_collation_id(drizzled::my_charset_bin.number);
157
stropt->set_collation(drizzled::my_charset_bin.name);
159
drizzled::message::Table::Index *index= table_message->add_indexes();
160
index->set_name("PRIMARY");
161
index->set_is_primary(true);
162
index->set_is_unique(true);
163
index->set_type(drizzled::message::Table::Index::BTREE);
164
index->set_key_length(8);
165
drizzled::message::Table::Index::IndexPart *part= index->add_index_part();
166
part->set_fieldnr(0);
167
part->set_compare_length(8);
168
part= index->add_index_part();
169
part->set_fieldnr(1);
170
part->set_compare_length(4);
172
index= table_message->add_indexes();
173
index->set_name("COMMIT_IDX");
174
index->set_is_primary(false);
175
index->set_is_unique(false);
176
index->set_type(drizzled::message::Table::Index::BTREE);
177
index->set_key_length(8);
178
part= index->add_index_part();
179
part->set_fieldnr(2);
180
part->set_compare_length(8);
181
part= index->add_index_part();
182
part->set_fieldnr(0);
183
part->set_compare_length(8);
188
extern dtuple_t* row_get_prebuilt_insert_row(row_prebuilt_t* prebuilt);
190
ulint insert_replication_message(const char *message, size_t size,
191
trx_t *trx, uint64_t trx_id,
192
uint64_t end_timestamp, bool is_end_segment,
196
row_prebuilt_t* prebuilt; /* For reading rows */
201
table = dict_table_get("SYS_REPLICATION_LOG",TRUE);
203
prebuilt = row_create_prebuilt(table);
205
if (prebuilt->trx != trx)
207
row_update_prebuilt_trx(prebuilt, trx);
210
/* DDL operations create table/drop table call
211
* innobase_commit_low() which will commit the trx
212
* that leaves the operation of committing to the
213
* log in a new trx. If that is the case we need
214
* to keep track and commit the trx later in this
217
bool is_started= true;
218
if (trx->conc_state == TRX_NOT_STARTED)
223
dtuple_t* dtuple= row_get_prebuilt_insert_row(prebuilt);
226
dfield = dtuple_get_nth_field(dtuple, 0);
227
data= static_cast<byte*>(mem_heap_alloc(prebuilt->heap, 8));
228
row_mysql_store_col_in_innobase_format(dfield, data, TRUE, (byte*)&trx_id, 8, dict_table_is_comp(prebuilt->table));
229
dfield_set_data(dfield, data, 8);
231
dfield = dtuple_get_nth_field(dtuple, 1);
233
data= static_cast<byte*>(mem_heap_alloc(prebuilt->heap, 4));
234
row_mysql_store_col_in_innobase_format(dfield, data, TRUE, (byte*)&seg_id, 4, dict_table_is_comp(prebuilt->table));
235
dfield_set_data(dfield, data, 4);
237
uint64_t commit_id= 0;
240
commit_id= trx_sys_commit_id.increment();
243
dfield = dtuple_get_nth_field(dtuple, 2);
244
data= static_cast<byte*>(mem_heap_alloc(prebuilt->heap, 8));
245
row_mysql_store_col_in_innobase_format(dfield, data, TRUE, (byte*)&commit_id, 8, dict_table_is_comp(prebuilt->table));
246
dfield_set_data(dfield, data, 8);
248
dfield = dtuple_get_nth_field(dtuple, 3);
249
data= static_cast<byte*>(mem_heap_alloc(prebuilt->heap, 8));
250
row_mysql_store_col_in_innobase_format(dfield, data, TRUE, (byte*)&end_timestamp, 8, dict_table_is_comp(prebuilt->table));
251
dfield_set_data(dfield, data, 8);
253
dfield = dtuple_get_nth_field(dtuple, 4);
254
data= static_cast<byte*>(mem_heap_alloc(prebuilt->heap, 4));
255
row_mysql_store_col_in_innobase_format(dfield, data, TRUE, (byte*)&size, 4, dict_table_is_comp(prebuilt->table));
256
dfield_set_data(dfield, data, 4);
258
dfield = dtuple_get_nth_field(dtuple, 5);
259
dfield_set_data(dfield, message, size);
261
ins_node_t* node = prebuilt->ins_node;
263
thr = que_fork_get_first_thr(prebuilt->ins_graph);
265
if (prebuilt->sql_stat_start) {
266
node->state = INS_NODE_SET_IX_LOCK;
267
prebuilt->sql_stat_start = FALSE;
269
node->state = INS_NODE_ALLOC_ROW_ID;
272
que_thr_move_to_run_state_for_mysql(thr, trx);
275
thr->run_node = node;
276
thr->prev_node = node;
280
error = trx->error_state;
282
que_thr_stop_for_mysql_no_error(thr, trx);
283
row_prebuilt_free(prebuilt, FALSE);
287
trx_commit_for_mysql(trx);
293
UNIV_INTERN read_replication_state_st *replication_read_init(void)
295
read_replication_state_st *state= new read_replication_state_st;
297
mutex_enter(&(dict_sys->mutex));
299
mtr_start(&state->mtr);
300
state->sys_tables= dict_table_get_low("SYS_REPLICATION_LOG");
301
state->sys_index= UT_LIST_GET_FIRST(state->sys_tables->indexes);
303
mutex_exit(&(dict_sys->mutex));
305
btr_pcur_open_at_index_side(TRUE, state->sys_index, BTR_SEARCH_LEAF, &state->pcur, TRUE, &state->mtr);
310
UNIV_INTERN void replication_read_deinit(struct read_replication_state_st *state)
312
btr_pcur_close(&state->pcur);
313
mtr_commit(&state->mtr);
317
UNIV_INTERN struct read_replication_return_st replication_read_next(struct read_replication_state_st *state)
319
struct read_replication_return_st ret;
322
btr_pcur_move_to_next_user_rec(&state->pcur, &state->mtr);
324
rec= btr_pcur_get_rec(&state->pcur);
326
while (btr_pcur_is_on_user_rec(&state->pcur))
331
// Is the row deleted? If so go fetch the next
332
if (rec_get_deleted_flag(rec, 0))
335
// Store transaction id
336
field = rec_get_nth_field_old(rec, 0, &len);
338
convert_to_mysql_format(idbyte, field, 8);
339
ret.id= *(uint64_t *)idbyte;
342
field = rec_get_nth_field_old(rec, 1, &len);
344
convert_to_mysql_format(segbyte, field, 4);
345
ret.seg_id= *(uint32_t *)segbyte;
347
field = rec_get_nth_field_old(rec, 4, &len);
349
convert_to_mysql_format(commitbyte, field, 8);
350
ret.commit_id= *(uint64_t *)commitbyte;
352
field = rec_get_nth_field_old(rec, 5, &len);
353
byte timestampbyte[8];
354
convert_to_mysql_format(timestampbyte, field, 8);
355
ret.end_timestamp= *(uint64_t *)timestampbyte;
358
field = rec_get_nth_field_old(rec, 7, &len);
359
ret.message= (char *)field;
360
ret.message_length= len;
362
// @todo double check that "field" will continue to be value past this
364
btr_pcur_store_position(&state->pcur, &state->mtr);
365
mtr_commit(&state->mtr);
367
mtr_start(&state->mtr);
369
btr_pcur_restore_position(BTR_SEARCH_LEAF, &state->pcur, &state->mtr);
375
memset(&ret, 0, sizeof(ret));
380
UNIV_INTERN void convert_to_mysql_format(byte* out, const byte* in, int len)
394
out[len - 1] = (byte) (out[len - 1] ^ 128);