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>
42QueueProducer::~QueueProducer()
56 if (_saved_max_commit_id == 0)
60 if (_last_return == DRIZZLE_RETURN_IO_ERROR ||
61 _last_return == DRIZZLE_RETURN_CLOSED)
69 _last_error_message=
"Master offline";
81 enum drizzled::error_t err;
87 if (_last_return == DRIZZLE_RETURN_IO_ERROR ||
88 _last_return == DRIZZLE_RETURN_CLOSED)
96 _last_error_message=
"Master offline";
118 if (not initial_connection)
120 errmsg_printf(error::ERROR, _(
"Lost connection to master. Reconnecting."));
123 _is_connected=
false;
124 _last_return= DRIZZLE_RETURN_OK;
125 _last_error_message.clear();
126 boost::posix_time::seconds duration(_seconds_between_reconnects);
128 uint32_t attempts= 1;
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());
138 if (attempts++ == _max_reconnects)
140 boost::this_thread::sleep(duration);
143 setIOState(_is_connected ? _(
"Connected") : _(
"Disconnected"),
true);
145 return _is_connected;
150 _is_connected=
false;
151 _connection=
nullptr;
152 _drizzle.reset(drizzle_create());
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());
161 _connection= drizzle_con_create(_drizzle.get());
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());
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);
184 if (_last_return != DRIZZLE_RETURN_OK)
186 _last_error_message=
"Replication slave: " + drizzle_client::error(_connection);
187 errmsg_printf(error::ERROR, _(
"%s"), _last_error_message.c_str());
188 _connection=
nullptr;
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;
205 return _last_return == DRIZZLE_RETURN_OK;
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())
227 Execute execute(*(_session.get()),
true);
228 execute.run(sql, result_set);
229 assert(result_set.getMetaData().getColumnCount() == 1);
232 uint32_t found_rows= 0;
233 while (result_set.next())
235 string value= result_set.getString(0);
237 if ((value ==
"") || (found_rows == 1))
240 assert(result_set.isNull(0) ==
false);
241 *max_commit_id= boost::lexical_cast<uint64_t>(value);
247 _last_error_message=
"Could not determine last committed transaction.";
254bool QueueProducer::queryForTrxIdList(uint64_t max_commit_id,
255 vector<uint64_t> &list)
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` != ");
262 sql.append(_session.get()->getServerUUID());
264 sql.append(
" ORDER BY `commit_id` LIMIT 25");
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)
270 _last_error_message=
"Replication slave: " + drizzle_client::error(_connection);
271 errmsg_printf(error::ERROR, _(
"%s"), _last_error_message.c_str());
274 if (result->column_count() != 1)
276 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
277 _last_error_message=
"Replication slave: Unexpected transaction ID columns";
281 while (
char **row= result->row_next())
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());
292 list.push_back(boost::lexical_cast<uint64_t>(
293 string(row[0], result->row_field_sizes()[0])));
295 catch (
const boost::bad_lexical_cast &)
297 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
298 _last_error_message=
"Replication slave: Invalid transaction ID";
306bool QueueProducer::queueInsert(
const char *trx_id,
308 const char *commit_id,
309 const char *originating_server_uuid,
310 const char *originating_commit_id,
314 message::Transaction message;
316 if (msg_size >
static_cast<size_t>(std::numeric_limits<int>::max()) ||
317 !message.ParseFromArray(msg,
static_cast<int>(msg_size)))
319 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
320 _last_error_message=
"Replication slave: Invalid transaction message";
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()));
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);
350 google::protobuf::TextFormat::PrintToString(message, &message_text);
355 string::iterator it= message_text.begin();
356 for (; it != message_text.end(); ++it)
360 it= message_text.insert(it,
'\\');
363 else if (*it ==
'\'')
365 it= message_text.insert(it,
'\\');
367 it= message_text.insert(it,
'\\');
370 else if (*it ==
'\\')
372 it= message_text.insert(it,
'\\');
374 it= message_text.insert(it,
'\\');
376 it= message_text.insert(it,
'\\');
381 it= message_text.insert(it,
'\\');
386 sql.append(message_text);
389 vector<string> statements;
390 statements.push_back(sql);
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;
408 vector<uint64_t> trx_id_list;
410 if (not queryForTrxIdList(max_commit_id, trx_id_list))
413 if (trx_id_list.size() == 0)
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 (";
425 for (
size_t x= 0; x < trx_id_list.size(); x++)
429 sql.append(boost::lexical_cast<string>(trx_id_list[x]));
433 sql.append(
" ORDER BY `commit_id` ASC");
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)
439 _last_error_message=
"Replication slave: " + drizzle_client::error(_connection);
440 errmsg_printf(error::ERROR, _(
"%s"), _last_error_message.c_str());
443 if (result->column_count() != 7)
445 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
446 _last_error_message=
"Replication slave: Unexpected replication columns";
450 while (
char **row= result->row_next())
452 for (
size_t column= 0; column < 7; ++column)
456 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
457 _last_error_message=
"Replication slave: Unexpected NULL replication field";
461 const size_t message_size= result->row_field_sizes()[5];
464 if (boost::lexical_cast<size_t>(
string(row[6], result->row_field_sizes()[6])) != message_size)
466 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
467 _last_error_message=
"Replication slave: Transaction message length mismatch";
471 catch (
const boost::bad_lexical_cast &)
473 _last_return= DRIZZLE_RETURN_PROTOCOL_ERROR;
474 _last_error_message=
"Replication slave: Invalid transaction message length";
477 if (!queueInsert(row[0], row[1], row[2], row[3], row[4], row[5], message_size))
479 errmsg_printf(error::ERROR,
480 _(
"Replication slave: Unable to insert into queue."));
490 vector<string> statements;
496 sql=
"UPDATE `sys_replication`.`io_state` SET `status` = 'STOPPED'";
500 sql=
"UPDATE `sys_replication`.`io_state` SET `status` = 'RUNNING'";
503 sql.append(
", `error_msg` = '", 17);
507 for (it= msg.begin(); it != msg.end(); ++it)
511 it= msg.insert(it,
'\'');
516 it= msg.insert(it,
'\\');
522 sql.append(
"' WHERE `master_id` = ");
523 sql.append(boost::lexical_cast<string>(masterId()));
525 statements.push_back(sql);
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.