drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
queue_producer.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2011 David Shrewsbury
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <config.h>
22
23#include <plugin/slave/queue_producer.h>
24#include <client/bulk_limits.h>
25#include <drizzled/errmsg_print.h>
26#include <drizzled/sql/result_set.h>
27#include <drizzled/execute.h>
28#include <drizzled/gettext.h>
29#include <drizzled/message/transaction.pb.h>
30#include <boost/lexical_cast.hpp>
31#include <google/protobuf/text_format.h>
32#include <limits>
33#include <string>
34#include <vector>
35
36using namespace std;
37using namespace drizzled;
38
39namespace slave
40{
41
42QueueProducer::~QueueProducer()
43{
44 if (_is_connected)
46}
47
49{
50 setIOState("", true);
51 return reconnect(true);
52}
53
55{
56 if (_saved_max_commit_id == 0)
57 {
58 if (not queryForMaxCommitId(&_saved_max_commit_id))
59 {
60 if (_last_return == DRIZZLE_RETURN_IO_ERROR ||
61 _last_return == DRIZZLE_RETURN_CLOSED)
62 {
63 if (reconnect(false))
64 {
65 return true; /* reconnect successful, try again */
66 }
67 else
68 {
69 _last_error_message= "Master offline";
70 return false; /* reconnect failed, shutdown the thread */
71 }
72 }
73 else
74 {
75 return false; /* unrecoverable error, shutdown the thread */
76 }
77 }
78 }
79
80 /* Keep getting events until caught up */
81 enum drizzled::error_t err;
82 while ((err= (queryForReplicationEvents(_saved_max_commit_id))) == EE_OK)
83 {}
84
85 if (err == ER_YES) /* We encountered an error */
86 {
87 if (_last_return == DRIZZLE_RETURN_IO_ERROR ||
88 _last_return == DRIZZLE_RETURN_CLOSED)
89 {
90 if (reconnect(false))
91 {
92 return true; /* reconnect successful, try again */
93 }
94 else
95 {
96 _last_error_message= "Master offline";
97 return false; /* reconnect failed, shutdown the thread */
98 }
99 }
100 else
101 {
102 return false; /* unrecoverable error, shutdown the thread */
103 }
104 }
105
106 return true;
107}
108
110{
111 setIOState(_last_error_message, false);
112 if (_is_connected)
114}
115
116bool QueueProducer::reconnect(bool initial_connection)
117{
118 if (not initial_connection)
119 {
120 errmsg_printf(error::ERROR, _("Lost connection to master. Reconnecting."));
121 }
122
123 _is_connected= false;
124 _last_return= DRIZZLE_RETURN_OK;
125 _last_error_message.clear();
126 boost::posix_time::seconds duration(_seconds_between_reconnects);
127
128 uint32_t attempts= 1;
129
130 setIOState("Connecting...", true);
131
132 while (not openConnection())
133 {
134 char buf[250];
135 snprintf(buf, sizeof(buf),_("Connection attempt %d of %d failed, sleeping for %d seconds and retrying. %s"), attempts, _max_reconnects, _seconds_between_reconnects, _last_error_message.c_str());
136
137 setIOState(buf, true);
138 if (attempts++ == _max_reconnects)
139 break;
140 boost::this_thread::sleep(duration);
141 }
142
143 setIOState(_is_connected ? _("Connected") : _("Disconnected"), true);
144
145 return _is_connected;
146}
147
149{
150 _is_connected= false;
151 _connection= nullptr;
152 _drizzle.reset(drizzle_create());
153 if (!_drizzle)
154 {
155 _last_return= DRIZZLE_RETURN_INTERNAL_ERROR;
156 _last_error_message= "Replication slave: " + string(drizzle_last_error());
157 errmsg_printf(error::ERROR, _("%s"), _last_error_message.c_str());
158 return false;
159 }
160
161 _connection= drizzle_con_create(_drizzle.get());
162 if (!_connection)
163 {
164 _last_return= DRIZZLE_RETURN_INTERNAL_ERROR;
165 _last_error_message= "Replication slave: " + string(drizzle_last_error());
166 errmsg_printf(error::ERROR, _("%s"), _last_error_message.c_str());
167 _drizzle.reset();
168 return false;
169 }
170
171 _last_return= drizzle_con_set_tcp(_connection,
172 drizzle_client::bytes(_master_host.data()), _master_host.size(), _master_port);
173 if (_last_return == DRIZZLE_RETURN_OK)
174 _last_return= drizzle_con_set_auth(_connection,
175 drizzle_client::bytes(_master_user.data()), _master_user.size(),
176 drizzle_client::bytes(_master_pass.data()), _master_pass.size());
177 if (_last_return == DRIZZLE_RETURN_OK)
178 _last_return= drizzle_con_set_timeouts(_connection, 10000, 0, 0);
179 if (_last_return == DRIZZLE_RETURN_OK)
180 _last_return= drizzle_client::bulk_limits(_connection);
181 if (_last_return == DRIZZLE_RETURN_OK)
182 _last_return= drizzle_con_connect(_connection);
183
184 if (_last_return != DRIZZLE_RETURN_OK)
185 {
186 _last_error_message= "Replication slave: " + drizzle_client::error(_connection);
187 errmsg_printf(error::ERROR, _("%s"), _last_error_message.c_str());
188 _connection= nullptr;
189 _drizzle.reset();
190 return false;
191 }
192
193 _is_connected= true;
194 return true;
195}
196
198{
199 _is_connected= false;
200 _last_return= _connection ? drizzle_con_quit(_connection) : DRIZZLE_RETURN_OK;
201 if (_last_return != DRIZZLE_RETURN_OK)
202 _last_error_message= "Replication slave: " + drizzle_client::error(_connection);
203 _connection= nullptr;
204 _drizzle.reset();
205 return _last_return == DRIZZLE_RETURN_OK;
206}
207
208bool QueueProducer::queryForMaxCommitId(uint64_t *max_commit_id)
209{
210 /*
211 * This SQL will get the maximum commit_id value we have pulled over from
212 * the master. We query two tables because either the queue will be empty,
213 * in which case the last_applied_commit_id will be the value we want, or
214 * we have yet to drain the queue, we get the maximum value still in
215 * the queue.
216 */
217 string sql("SELECT MAX(x.cid) FROM"
218 " (SELECT MAX(`commit_order`) AS cid FROM `sys_replication`.`queue`"
219 " WHERE `master_id` = "
220 + boost::lexical_cast<string>(masterId())
221 + " UNION ALL SELECT `last_applied_commit_id` AS cid"
222 + " FROM `sys_replication`.`applier_state` WHERE `master_id` = "
223 + boost::lexical_cast<string>(masterId())
224 + ") AS x");
225
226 sql::ResultSet result_set(1);
227 Execute execute(*(_session.get()), true);
228 execute.run(sql, result_set);
229 assert(result_set.getMetaData().getColumnCount() == 1);
230
231 /* Really should only be 1 returned row */
232 uint32_t found_rows= 0;
233 while (result_set.next())
234 {
235 string value= result_set.getString(0);
236
237 if ((value == "") || (found_rows == 1))
238 break;
239
240 assert(result_set.isNull(0) == false);
241 *max_commit_id= boost::lexical_cast<uint64_t>(value);
242 found_rows++;
243 }
244
245 if (found_rows == 0)
246 {
247 _last_error_message= "Could not determine last committed transaction.";
248 return false;
249 }
250
251 return true;
252}
253
254bool QueueProducer::queryForTrxIdList(uint64_t max_commit_id,
255 vector<uint64_t> &list)
256{
257 string sql("SELECT `id` FROM `data_dictionary`.`sys_replication_log`"
258 " WHERE `commit_id` > ");
259 sql.append(boost::lexical_cast<string>(max_commit_id));
260 sql.append(" AND `originating_server_uuid` != ");
261 sql.append("'", 1);
262 sql.append(_session.get()->getServerUUID());
263 sql.append("'", 1);
264 sql.append(" ORDER BY `commit_id` LIMIT 25");
265
266 std::unique_ptr<drizzle::result_c> result(
267 drizzle_client::query(_connection, sql.data(), sql.size(), &_last_return));
268 if (_last_return != DRIZZLE_RETURN_OK || !result)
269 {
270 _last_error_message= "Replication slave: " + drizzle_client::error(_connection);
271 errmsg_printf(error::ERROR, _("%s"), _last_error_message.c_str());
272 return false;
273 }
274 if (result->column_count() != 1)
275 {
276 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
277 _last_error_message= "Replication slave: Unexpected transaction ID columns";
278 return false;
279 }
280
281 while (char **row= result->row_next())
282 {
283 if (!row[0])
284 {
285 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
286 _last_error_message= "Replication slave: Unexpected NULL for trx id";
287 errmsg_printf(error::ERROR, _("%s"), _last_error_message.c_str());
288 return false;
289 }
290 try
291 {
292 list.push_back(boost::lexical_cast<uint64_t>(
293 string(row[0], result->row_field_sizes()[0])));
294 }
295 catch (const boost::bad_lexical_cast &)
296 {
297 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
298 _last_error_message= "Replication slave: Invalid transaction ID";
299 return false;
300 }
301 }
302 return true;
303}
304
305
306bool QueueProducer::queueInsert(const char *trx_id,
307 const char *seg_id,
308 const char *commit_id,
309 const char *originating_server_uuid,
310 const char *originating_commit_id,
311 const char *msg,
312 size_t msg_size)
313{
314 message::Transaction message;
315
316 if (msg_size > static_cast<size_t>(std::numeric_limits<int>::max()) ||
317 !message.ParseFromArray(msg, static_cast<int>(msg_size)))
318 {
319 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
320 _last_error_message= "Replication slave: Invalid transaction message";
321 return false;
322 }
323
324 /*
325 * The SQL to insert our results into the local queue.
326 */
327 string sql= "INSERT INTO `sys_replication`.`queue`"
328 " (`master_id`, `trx_id`, `seg_id`, `commit_order`,"
329 " `originating_server_uuid`, `originating_commit_id`, `msg`) VALUES (";
330 sql.append(boost::lexical_cast<string>(masterId()));
331 sql.append(", ", 2);
332 sql.append(trx_id);
333 sql.append(", ", 2);
334 sql.append(seg_id);
335 sql.append(", ", 2);
336 sql.append(commit_id);
337 sql.append(", '", 3);
338 sql.append(originating_server_uuid);
339 sql.append("' , ", 4);
340 sql.append(originating_commit_id);
341 sql.append(", '", 3);
342
343 /*
344 * Ideally we would store the Transaction message in binary form, as it
345 * it stored on the master and tranferred to the slave. However, we are
346 * inserting using drizzle::Execute which doesn't really handle binary
347 * data. Until that is changed, we store as plain text.
348 */
349 string message_text;
350 google::protobuf::TextFormat::PrintToString(message, &message_text);
351
352 /*
353 * Execution using drizzled::Execute requires some special escaping.
354 */
355 string::iterator it= message_text.begin();
356 for (; it != message_text.end(); ++it)
357 {
358 if (*it == '\"')
359 {
360 it= message_text.insert(it, '\\');
361 ++it;
362 }
363 else if (*it == '\'')
364 {
365 it= message_text.insert(it, '\\');
366 ++it;
367 it= message_text.insert(it, '\\');
368 ++it;
369 }
370 else if (*it == '\\')
371 {
372 it= message_text.insert(it, '\\');
373 ++it;
374 it= message_text.insert(it, '\\');
375 ++it;
376 it= message_text.insert(it, '\\');
377 ++it;
378 }
379 else if (*it == ';')
380 {
381 it= message_text.insert(it, '\\');
382 ++it; /* advance back to the semicolon */
383 }
384 }
385
386 sql.append(message_text);
387 sql.append("')", 2);
388
389 vector<string> statements;
390 statements.push_back(sql);
391
392 if (not executeSQL(statements))
393 {
394 markInErrorState();
395 return false;
396 }
397
398 uint64_t tmp_commit_id= boost::lexical_cast<uint64_t>(commit_id);
399 if (tmp_commit_id > _saved_max_commit_id)
400 _saved_max_commit_id= tmp_commit_id;
401
402 return true;
403}
404
405
406enum drizzled::error_t QueueProducer::queryForReplicationEvents(uint64_t max_commit_id)
407{
408 vector<uint64_t> trx_id_list;
409
410 if (not queryForTrxIdList(max_commit_id, trx_id_list))
411 return ER_YES;
412
413 if (trx_id_list.size() == 0) /* nothing to get from the master */
414 {
415 return ER_NO;
416 }
417
418 /*
419 * The SQL to pull everything we need from the master.
420 */
421 string sql= "SELECT `id`, `segid`, `commit_id`, `originating_server_uuid`,"
422 " `originating_commit_id`, `message`, `message_len` "
423 " FROM `data_dictionary`.`sys_replication_log` WHERE `id` IN (";
424
425 for (size_t x= 0; x < trx_id_list.size(); x++)
426 {
427 if (x > 0)
428 sql.append(", ", 2);
429 sql.append(boost::lexical_cast<string>(trx_id_list[x]));
430 }
431
432 sql.append(")", 1);
433 sql.append(" ORDER BY `commit_id` ASC");
434
435 std::unique_ptr<drizzle::result_c> result(
436 drizzle_client::query(_connection, sql.data(), sql.size(), &_last_return));
437 if (_last_return != DRIZZLE_RETURN_OK || !result)
438 {
439 _last_error_message= "Replication slave: " + drizzle_client::error(_connection);
440 errmsg_printf(error::ERROR, _("%s"), _last_error_message.c_str());
441 return ER_YES;
442 }
443 if (result->column_count() != 7)
444 {
445 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
446 _last_error_message= "Replication slave: Unexpected replication columns";
447 return ER_YES;
448 }
449
450 while (char **row= result->row_next())
451 {
452 for (size_t column= 0; column < 7; ++column)
453 {
454 if (!row[column])
455 {
456 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
457 _last_error_message= "Replication slave: Unexpected NULL replication field";
458 return ER_YES;
459 }
460 }
461 const size_t message_size= result->row_field_sizes()[5];
462 try
463 {
464 if (boost::lexical_cast<size_t>(string(row[6], result->row_field_sizes()[6])) != message_size)
465 {
466 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
467 _last_error_message= "Replication slave: Transaction message length mismatch";
468 return ER_YES;
469 }
470 }
471 catch (const boost::bad_lexical_cast &)
472 {
473 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
474 _last_error_message= "Replication slave: Invalid transaction message length";
475 return ER_YES;
476 }
477 if (!queueInsert(row[0], row[1], row[2], row[3], row[4], row[5], message_size))
478 {
479 errmsg_printf(error::ERROR,
480 _("Replication slave: Unable to insert into queue."));
481 return ER_YES;
482 }
483 }
484 return EE_OK;
485}
486
487
488void QueueProducer::setIOState(const string &err_msg, bool status)
489{
490 vector<string> statements;
491 string sql;
492 string msg(err_msg);
493
494 if (not status)
495 {
496 sql= "UPDATE `sys_replication`.`io_state` SET `status` = 'STOPPED'";
497 }
498 else
499 {
500 sql= "UPDATE `sys_replication`.`io_state` SET `status` = 'RUNNING'";
501 }
502
503 sql.append(", `error_msg` = '", 17);
504
505 /* Escape embedded quotes and statement terminators */
506 string::iterator it;
507 for (it= msg.begin(); it != msg.end(); ++it)
508 {
509 if (*it == '\'')
510 {
511 it= msg.insert(it, '\'');
512 ++it; /* advance back to the quote */
513 }
514 else if (*it == ';')
515 {
516 it= msg.insert(it, '\\');
517 ++it; /* advance back to the semicolon */
518 }
519 }
520
521 sql.append(msg);
522 sql.append("' WHERE `master_id` = ");
523 sql.append(boost::lexical_cast<string>(masterId()));
524
525 statements.push_back(sql);
526 executeSQL(statements);
527}
528
529} /* namespace slave */
bool reconnect(bool initial_connection)
bool queryForMaxCommitId(uint64_t *max_commit_id)
enum drizzled::error_t queryForReplicationEvents(uint64_t max_commit_id)
void setIOState(const std::string &err_msg, bool status)
bool executeSQL(std::vector< std::string > &sql)
TODO: Rename this file - func.h is stupid.
STL namespace.