From 4cf98bbb9f5e5eb98e5f97c90c0d74ea74fb21f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lindstr=C3=B6m?= Date: Thu, 8 Aug 2013 08:28:22 +0300 Subject: [PATCH] Added more comments and test file. --- replication_listener/CMakeLists.txt | 5 +- replication_listener/COPYING.SkySQL | 30 +++ .../access_method_factory.cpp | 12 + replication_listener/access_method_factory.h | 7 +- .../basic_content_handler.cpp | 13 + replication_listener/basic_content_handler.h | 15 +- .../basic_transaction_parser.cpp | 172 ------------- replication_listener/binary_log.cpp | 17 +- replication_listener/binlog_api.h | 13 + replication_listener/binlog_driver.cpp | 17 +- replication_listener/binlog_driver.h | 13 + replication_listener/binlog_event.cpp | 12 + replication_listener/binlog_event.h | 20 +- replication_listener/gtid.h | 1 - replication_listener/listener_exception.h | 2 +- replication_listener/protocol.cpp | 12 + replication_listener/tcp_driver.cpp | 16 +- replication_listener/tcp_driver.h | 14 + replication_listener/tests/CMakeLists.txt | 35 +++ replication_listener/tests/event_dump.cpp | 243 ++++++++++++++++++ 20 files changed, 478 insertions(+), 191 deletions(-) create mode 100644 replication_listener/COPYING.SkySQL delete mode 100644 replication_listener/basic_transaction_parser.cpp create mode 100644 replication_listener/tests/CMakeLists.txt create mode 100644 replication_listener/tests/event_dump.cpp diff --git a/replication_listener/CMakeLists.txt b/replication_listener/CMakeLists.txt index 162697856..8c9a2c098 100644 --- a/replication_listener/CMakeLists.txt +++ b/replication_listener/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 2.6) # the library. set(replication_sources access_method_factory.cpp - binlog_driver.cpp tcp_driver.cpp + binlog_driver.cpp tcp_driver.cpp basic_content_handler.cpp binary_log.cpp protocol.cpp binlog_event.cpp gtid.cpp resultset_iterator.cpp value.cpp row_of_fields.cpp) @@ -20,7 +20,8 @@ SET(Boost_USE_STATIC_LIBS FALSE) SET(Boost_ADDITIONAL_VERSIONS "1.41" "1.41.0") FIND_PACKAGE(Boost REQUIRED system thread) -FIND_LIBRARY(LIB_CRYPTO crypto /opt/local/lib /opt/lib /usr/lib /usr/local/lib) +FIND_LIBRARY(LIB_CRYPTO NAMES libcrypto.a /opt/local/lib /opt/lib /usr/lib /usr/local/lib /usr/local/ssl/lib) +LINK_DIRECTORIES(${LIB_CRYPTO}) LINK_DIRECTORIES(${Boost_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR}) diff --git a/replication_listener/COPYING.SkySQL b/replication_listener/COPYING.SkySQL new file mode 100644 index 000000000..9845b0fe8 --- /dev/null +++ b/replication_listener/COPYING.SkySQL @@ -0,0 +1,30 @@ +Portions of this software contain modifications contributed by SkySQL, Ab. +These contributions are used with the following license: + +Copyright (c) 2013, SkySQL Ab. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials + provided with the distribution. + * Neither the name of the SkySQL Ab. nor the names of its + contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/replication_listener/access_method_factory.cpp b/replication_listener/access_method_factory.cpp index e02766bbe..2f6f6ca3b 100644 --- a/replication_listener/access_method_factory.cpp +++ b/replication_listener/access_method_factory.cpp @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,13 @@ 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 +*/ +/* +SkySQL change details: +- Removed unnecessary file driver + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #include "access_method_factory.h" #include "tcp_driver.h" diff --git a/replication_listener/access_method_factory.h b/replication_listener/access_method_factory.h index cc430f378..af245fa36 100644 --- a/replication_listener/access_method_factory.h +++ b/replication_listener/access_method_factory.h @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -28,8 +33,6 @@ namespace system { Binary_log_driver *create_transport(const char *url); Binary_log_driver *parse_mysql_url(char *url, const char *mysql_access_method); -Binary_log_driver *parse_file_url(char *url, const char - *file_access_method); } } diff --git a/replication_listener/basic_content_handler.cpp b/replication_listener/basic_content_handler.cpp index 9a051c8ba..7d24b9c0f 100644 --- a/replication_listener/basic_content_handler.cpp +++ b/replication_listener/basic_content_handler.cpp @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -17,6 +22,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/* +SkySQL change details: +- Added GTID event handler + +Author: Jan Lindström (jan.lindstrom@skysql.com + +*/ + #include "basic_content_handler.h" #include diff --git a/replication_listener/basic_content_handler.h b/replication_listener/basic_content_handler.h index 72804bab1..449d0f8d4 100644 --- a/replication_listener/basic_content_handler.h +++ b/replication_listener/basic_content_handler.h @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -15,8 +20,16 @@ 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-1301 USA +02110-1301 USA */ +/* +SkySQL change details: +- Added GTID event handler + +Author: Jan Lindström (jan.lindstrom@skysql.com + +*/ + #ifndef BASIC_CONTENT_HANDLER_H #define BASIC_CONTENT_HANDLER_H diff --git a/replication_listener/basic_transaction_parser.cpp b/replication_listener/basic_transaction_parser.cpp deleted file mode 100644 index 863bdc8ec..000000000 --- a/replication_listener/basic_transaction_parser.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/* -Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights -reserved. - -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-1301 USA -*/ - -#include "binlog_event.h" -#include "basic_transaction_parser.h" -#include "protocol.h" -#include "value.h" -#include -#include -#include -#include "field_iterator.h" - -namespace mysql { - -mysql::Binary_log_event *Basic_transaction_parser::process_event(mysql::Gtid_event *qev) -{ - m_transaction_state= STARTING; - - return process_transaction_state(qev); -} - - -mysql::Binary_log_event *Basic_transaction_parser::process_event(mysql::Query_event *qev) -{ - if (qev->query == "BEGIN") - { - //std::cout << "Transaction has started!" << std::endl; - m_transaction_state= STARTING; - } - else if (qev->query == "COMMIT") - { - m_transaction_state= COMMITTING; - } - - return process_transaction_state(qev); -} - -mysql::Binary_log_event *Basic_transaction_parser::process_event(mysql::Xid *ev) -{ - m_transaction_state= COMMITTING; - return process_transaction_state(ev); -} - -mysql::Binary_log_event *Basic_transaction_parser::process_event(mysql::Table_map_event *ev) -{ - if(m_transaction_state ==IN_PROGRESS) - { - m_event_stack.push_back(ev); - return 0; - } - return ev; -} - -mysql::Binary_log_event *Basic_transaction_parser::process_event(mysql::Row_event *ev) -{ - if(m_transaction_state ==IN_PROGRESS) - { - m_event_stack.push_back(ev); - return 0; - } - return ev; -} - -mysql::Binary_log_event *Basic_transaction_parser::process_transaction_state(mysql::Binary_log_event *incomming_event) -{ - switch(m_transaction_state) - { - case STARTING: - { - m_transaction_state= IN_PROGRESS; - m_start_time= incomming_event->header()->timestamp; - delete incomming_event; // drop the begin event - return 0; - } - case COMMITTING: - { - delete incomming_event; // drop the commit event - - /** - * Propagate the start time for the transaction to the newly created - * event. - */ - mysql::Transaction_log_event *trans= mysql::create_transaction_log_event(); - trans->header()->timestamp= m_start_time; - - //std::cout << "There are " << m_event_stack.size() << " events in the transaction: "; - while( m_event_stack.size() > 0) - { - mysql::Binary_log_event *event= m_event_stack.front(); - m_event_stack.pop_front(); - switch(event->get_event_type()) - { - case mysql::TABLE_MAP_EVENT: - { - /* - Index the table name with a table id to ease lookup later. - */ - mysql::Table_map_event *tm= static_cast(event); - //std::cout << "Indexing table " << tm->table_id << " " << tm->table_name << std::endl; - //std::cout.flush (); - trans->m_table_map.insert(mysql::Event_index_element(tm->table_id,tm)); - trans->m_events.push_back(event); - } - break; - case mysql::WRITE_ROWS_EVENT: - case mysql::DELETE_ROWS_EVENT: - case mysql::UPDATE_ROWS_EVENT: - { - trans->m_events.push_back(event); - /* - * Propagate last known next position - */ - trans->header()->next_position= event->header()->next_position; - } - break; - default: - delete event; - } - } // end while - m_transaction_state= NOT_IN_PROGRESS; - return(trans); - } - case NOT_IN_PROGRESS: - default: - return incomming_event; - } - -} - -Transaction_log_event *create_transaction_log_event(void) -{ - Transaction_log_event *trans= new Transaction_log_event(); - trans->header()->type_code= USER_DEFINED; - return trans; -}; - -Transaction_log_event::~Transaction_log_event() -{ - Int_to_Event_map::iterator it; - for(it = m_table_map.begin(); it != m_table_map.end();) - { - /* No need to delete the event here; it happens in the next iteration */ - m_table_map.erase(it++); - } - - while (m_events.size() > 0) - { - Binary_log_event *event= m_events.back(); - m_events.pop_back(); - delete(event); - } - -} - -} // end namespace diff --git a/replication_listener/binary_log.cpp b/replication_listener/binary_log.cpp index 382e60a29..f3100cbbf 100644 --- a/replication_listener/binary_log.cpp +++ b/replication_listener/binary_log.cpp @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,14 @@ 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 +*/ +/* +SkySQL change details: +- Added support for setting binlog position based on GTID +- Added support for MySQL and MariDB server types + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #include @@ -29,7 +42,7 @@ namespace mysql { /* -Return server type string. +Return server type string. */ const char *mysql_server_type_str(mysql_server_types server_type) @@ -41,7 +54,7 @@ const char *mysql_server_type_str(mysql_server_types server_type) } } - Binary_log::Binary_log(Binary_log_driver *drv) : m_binlog_position(4), m_binlog_file(""), m_uri("") +Binary_log::Binary_log(Binary_log_driver *drv) : m_binlog_position(4), m_binlog_file(""), m_uri("") { if (drv == NULL) { diff --git a/replication_listener/binlog_api.h b/replication_listener/binlog_api.h index b01da48d7..03b2f934f 100644 --- a/replication_listener/binlog_api.h +++ b/replication_listener/binlog_api.h @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,14 @@ 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 +*/ +/* +SkySQL change details: +- Added support for setting binlog position based on GTID +- Added support for MySQL and MariDB server types + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #ifndef _REPEVENT_H diff --git a/replication_listener/binlog_driver.cpp b/replication_listener/binlog_driver.cpp index 9756dd094..ef2512b5c 100644 --- a/replication_listener/binlog_driver.cpp +++ b/replication_listener/binlog_driver.cpp @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + Copyright (c) 2013, SkySQL Ab + + Portions of this file contain modifications contributed and copyrighted by + SkySQL, Ab. Those modifications are gratefully acknowledged and are described + briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -17,16 +22,18 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/* +SkySQL change details: +- Added support for GTID event handling for both MySQL and MariaDB + +Author: Jan Lindström (jan.lindstrom@skysql.com + +*/ #include "binlog_driver.h" namespace mysql { namespace system { -/* -Binary_log_event* Binary_log_driver::parse_event(boost::asio::streambuf - &sbuff, Log_event_header - *header) - */ Binary_log_event* Binary_log_driver::parse_event(std::istream &is, Log_event_header *header) diff --git a/replication_listener/binlog_driver.h b/replication_listener/binlog_driver.h index 0dca3b187..788ce8342 100644 --- a/replication_listener/binlog_driver.h +++ b/replication_listener/binlog_driver.h @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,14 @@ 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 +*/ +/* +SkySQL change details: +- Added support for GTID event handling for both MySQL and MariaDB +- Added support for setting binlog position based on GTID + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #ifndef _BINLOG_DRIVER_H diff --git a/replication_listener/binlog_event.cpp b/replication_listener/binlog_event.cpp index 672eebc45..77723e310 100644 --- a/replication_listener/binlog_event.cpp +++ b/replication_listener/binlog_event.cpp @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,13 @@ 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 +*/ +/* +SkySQL change details: +- Added support for GTID event handling for both MySQL and MariaDB + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #include "binlog_event.h" diff --git a/replication_listener/binlog_event.h b/replication_listener/binlog_event.h index cf699ac9d..2b0d650e2 100644 --- a/replication_listener/binlog_event.h +++ b/replication_listener/binlog_event.h @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,13 @@ 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 +*/ +/* +SkySQL change details: +- Added support for GTID event handling for both MySQL and MariaDB + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #ifndef _BINLOG_EVENT_H #define _BINLOG_EVENT_H @@ -87,10 +99,10 @@ enum Log_event_type */ INCIDENT_EVENT= 26, - /* - * A user defined event - */ - USER_DEFINED= 27, + /* + * A user defined event + */ + USER_DEFINED= 27, /* We have two different implementations of global transaction id */ GTID_EVENT_MYSQL=33, diff --git a/replication_listener/gtid.h b/replication_listener/gtid.h index cd9c48a8d..b0ca43548 100644 --- a/replication_listener/gtid.h +++ b/replication_listener/gtid.h @@ -1,7 +1,6 @@ /* Copyright (C) 2013, SkySQL Ab - This file is distributed as part of the SkySQL Gateway. It 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, diff --git a/replication_listener/listener_exception.h b/replication_listener/listener_exception.h index ead4ff44f..b9da4f0ae 100644 --- a/replication_listener/listener_exception.h +++ b/replication_listener/listener_exception.h @@ -50,7 +50,7 @@ class ListenerException : public std::runtime_error ListenerException(std::string message, const char *file, int line) : std::runtime_error(std::string("Exception: ") + message + std::string(" file: ") + std::string(file) + std::string(" line: ") + (to_string(line))) {} - + }; } diff --git a/replication_listener/protocol.cpp b/replication_listener/protocol.cpp index ce84f8b28..0bc58b458 100644 --- a/replication_listener/protocol.cpp +++ b/replication_listener/protocol.cpp @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,13 @@ 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 +*/ +/* +SkySQL change details: +- Added support for GTID event handling for both MySQL and MariaDB + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #include #include diff --git a/replication_listener/tcp_driver.cpp b/replication_listener/tcp_driver.cpp index e78aa00c6..325775ee3 100644 --- a/replication_listener/tcp_driver.cpp +++ b/replication_listener/tcp_driver.cpp @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,15 @@ 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 +*/ +/* +SkySQL change details: +- Added support for GTID event handling for both MySQL and MariaDB +- Added support for starting binlog dump from GTID position +- Added error handling using exceptions + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #include "binlog_api.h" #include @@ -260,7 +274,7 @@ int Binlog_tcp_driver::fetch_server_version(const std::string& user, { m_server_type = MYSQL_SERVER_TYPE_MARIADB; } else { - // Currently assuming MySQL + // Currently assuming MySQL m_server_type = MYSQL_SERVER_TYPE_MYSQL; } diff --git a/replication_listener/tcp_driver.h b/replication_listener/tcp_driver.h index ba0460461..2a5826bfd 100644 --- a/replication_listener/tcp_driver.h +++ b/replication_listener/tcp_driver.h @@ -1,6 +1,11 @@ /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. +Copyright (c) 2013, SkySQL Ab + +Portions of this file contain modifications contributed and copyrighted by +SkySQL, Ab. Those modifications are gratefully acknowledged and are described +briefly in the source code. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -16,6 +21,15 @@ 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 +*/ +/* +SkySQL change details: +- Added support for GTID event handling for both MySQL and MariaDB +- Added support for starting binlog dump from GTID position +- Added support for MariaDB server + +Author: Jan Lindström (jan.lindstrom@skysql.com + */ #ifndef _TCP_DRIVER_H diff --git a/replication_listener/tests/CMakeLists.txt b/replication_listener/tests/CMakeLists.txt new file mode 100644 index 000000000..114b66437 --- /dev/null +++ b/replication_listener/tests/CMakeLists.txt @@ -0,0 +1,35 @@ +project(tests) +cmake_minimum_required (VERSION 2.6) + +include_directories(../.) +include_directories(../table_replication_consistency) + +FIND_LIBRARY(CRYPTO NAMES libcrypto.a /opt/local/lib /opt/lib /usr/lib /usr/local/lib /usr/local/ssl/lib) +FIND_LIBRARY(SSL NAMES libssl.a /opt/local/lib /opt/lib /usr/lib /usr/local/lib /usr/local/ssl/lib) +FIND_LIBRARY(REPLICATION replication /opt/local/lib /opt/lib /usr/lib /usr/local/lib ../) + +# Find MySQL client library and header files +find_path(MySQL_INCLUDE_DIR mysql.h + /usr/local/include/mysql /usr/include/mysql /usr/local/mysql/include) +include_directories(${MySQL_INCLUDE_DIR}) +include_directories(../../table_replication_consistency) +include_directories(../../utils) + +# Find MySQL client library and header files +find_library(MySQL_LIBRARY NAMES libmysqld.a PATHS +/usr/lib64/mysql /usr/lib/mysql /usr/local/mysql/lib ${MARIADB_SRC_PATH}/lib) + +SET(Boost_DEBUG FALSE) +SET(Boost_FIND_REQUIRED TRUE) +SET(Boost_FIND_QUIETLY TRUE) +SET(Boost_USE_STATIC_LIBS FALSE) +SET(Boost_ADDITIONAL_VERSIONS "1.41" "1.41.0") +FIND_PACKAGE(Boost REQUIRED system thread) + +# Create build rules for all the simple examples that only require a +# single file. +foreach(prog event_dump) + ADD_EXECUTABLE(${prog} ${prog}.cpp /usr/local/mysql/lib/libmysqld.a) + TARGET_LINK_LIBRARIES(${prog} ${REPLICATION} boost_system boost_thread pthread aio ${SSL} ${CRYPTO} crypt z dl ${MySQL_LIBRARY}) +endforeach() + diff --git a/replication_listener/tests/event_dump.cpp b/replication_listener/tests/event_dump.cpp new file mode 100644 index 000000000..3fd33d1a7 --- /dev/null +++ b/replication_listener/tests/event_dump.cpp @@ -0,0 +1,243 @@ +/* +Copyright (C) 2013, SkySQL Ab + + +This file is distributed as part of the SkySQL Gateway. It 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. + +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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Author: Jan Lindström jan.lindstrom@skysql.com + +*/ + +#include "binlog_api.h" +#include "listener_exception.h" +#include "table_replication_consistency.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../gtid.h" + +using mysql::Binary_log; +using mysql::system::create_transport; +using namespace std; +using namespace mysql; +using namespace mysql::system; + +static char* server_options[] = { + (char *)"event_dump", + (char *)"--datadir=/tmp/", + (char *)"--skip-innodb", + (char *)"--default-storage-engine=myisam", + NULL +}; + +const int num_elements = (sizeof(server_options) / sizeof(char *)) - 1; + +static char* server_groups[] = { + (char *)"libmysqld_server", + (char *)"libmysqld_client", + (char *)"libmysqld_server", + (char *)"libmysqld_server", + NULL +}; + +void* binlog_reader(void * arg) +{ + replication_listener_t *rlt = (replication_listener_t*)arg; + char *uri = rlt->server_url; + map tid2tname; + map::iterator tb_it; + pthread_t id = pthread_self(); + string database_dot_table; + const char* server_type; + Gtid gtid = Gtid(); + + try { + Binary_log binlog(create_transport(uri)); + binlog.connect(); + + server_type = binlog.get_mysql_server_type_str(); + + cout << "Server " << uri << " type: " << server_type << endl; + + Binary_log_event *event; + + while (true) { + Log_event_header *lheader; + + int result = binlog.wait_for_next_event(&event); + + if (result == ERR_EOF) + break; + + lheader = event->header(); + + switch(event->get_event_type()) { + + case QUERY_EVENT: { + Query_event *qevent = dynamic_cast(event); + + std::cout << "Thread: " << id << " server_id " << lheader->server_id + << " position " << lheader->next_position << " : Found event of type " + << event->get_event_type() + << " txt " << get_event_type_str(event->get_event_type()) + << " query " << qevent->query << " db " << qevent->db_name + << std::endl; + break; + } + + case GTID_EVENT_MARIADB: + case GTID_EVENT_MYSQL: { + Gtid_event *gevent = dynamic_cast(event); + + std::cout << "Thread: " << id << " server_id " << lheader->server_id + << " position " << lheader->next_position << " : Found event of type " + << event->get_event_type() + << " txt " << get_event_type_str(event->get_event_type()) + << " GTID " << std::string((char *)gevent->m_gtid.get_gtid()) + << " GTID " << gevent->m_gtid.get_string() + << std::endl; + + break; + } + + case TABLE_MAP_EVENT: { + Table_map_event *table_map_event= dynamic_cast(event); + database_dot_table= table_map_event->db_name; + database_dot_table.append("."); + database_dot_table.append(table_map_event->table_name); + tid2tname[table_map_event->table_id]= database_dot_table; + break; + } + + case WRITE_ROWS_EVENT: + case UPDATE_ROWS_EVENT: + case DELETE_ROWS_EVENT: { + Row_event *revent = dynamic_cast(event); + tb_it= tid2tname.begin(); + tb_it= tid2tname.find(revent->table_id); + if (tb_it != tid2tname.end()) + { + database_dot_table= tb_it->second; + } + + std::cout << "Thread: " << id << " server_id " << lheader->server_id + << " position " << lheader->next_position << " : Found event of type " + << event->get_event_type() + << " txt " << get_event_type_str(event->get_event_type()) + << " table " << revent->table_id + << " tb " << database_dot_table + << std::endl; + break; + + } + default: + break; + } // switch + } // while + } // try + catch(ListenerException e) + { + std::cerr << "Listener exception: " << e.what() << std::endl; + } + catch(boost::system::error_code e) + { + std::cerr << "Listener system error: " << e.message() << std::endl; + } + // Try and catch all exceptions + catch(std::exception const& e) + { + std::cerr << "Listener other error: " << e.what() << std::endl; + } + // Rest of them + catch(...) + { + std::cerr << "Unknown exception: " << std::endl; + // Re-Throw this one. + // It was not handled so you want to make sure it is handled correctly by + // the OS. So just allow the exception to keep propagating. + throw; + } + + pthread_exit(NULL); + return NULL; + +} + +int main(int argc, char** argv) { + + int number_of_args = argc; + int i=0,k=0; + pthread_t *tid=NULL; + char *uri; + replication_listener_t *mrl; + int err=0; + + tid = (pthread_t*)malloc(sizeof(pthread_t) * argc); + mrl = (replication_listener_t*)calloc(argc, sizeof(replication_listener_t)); + + if (argc < 2) { + std::cerr << "Usage: event_dump " << std::endl; + exit(2); + } + + if (mysql_library_init(num_elements, server_options, server_groups)) { + std::cerr << "Failed to init MySQL server" << std::endl; + exit(1); + } + + argc =0; + while(argc != number_of_args) + { + uri= argv[argc++]; + + if ( strncmp("mysql://", uri, 8) == 0) { + + mrl[i].server_url = uri; + + if (argc == 1) { + mrl[i].is_master = 1; + } + + err = pthread_create(&(tid[i++]), NULL, &binlog_reader, (void *)&mrl[i]); + + if (err ) { + perror(NULL); + break; + } + + } + }//end of outer while loop + + for(k=0; k < i; k++) + { + err = pthread_join(tid[k], (void **)&(mrl[k])); + + if (err) { + perror(NULL); + } + } + + exit(0); + +}