An API for Reading the MySQL Binary Log



Similar documents
URI and UUID. Identifying things on the Web.

MySQL and Hadoop: Big Data Integration. Shubhangi Garg & Neha Kumari MySQL Engineering

Using MySQL for Big Data Advantage Integrate for Insight Sastry Vedantam

Best Practices for Using MySQL in the Cloud

Zero Downtime Deployments with Database Migrations. Bob Feldbauer

Comp151. Definitions & Declarations

MySQL Security for Security Audits

Linas Virbalas Continuent, Inc.

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Architectures Haute-Dispo Joffrey MICHAÏE Consultant MySQL

DBA Tutorial Kai Voigt Senior MySQL Instructor Sun Microsystems Santa Clara, April 12, 2010

Network Programming. Writing network and internet applications.

MySQL Backup IEDR

An Incomplete C++ Primer. University of Wyoming MA 5310

How, What, and Where of Data Warehouses for MySQL

Using SQL Server Management Studio

Partitioning under the hood in MySQL 5.5

SQLITE C/C++ TUTORIAL

CSCI110: Examination information.

Oracle Database 10g Express

Database Administration with MySQL

Logging. Working with the POCO logging framework.

MySQL++ v3.2.2 User Manual

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

MySQL and Virtualization Guide

ns-3 Tutorial (Part IV) Wireless & Tracing System and Visualizing Results

Tushar Joshi Turtle Networks Ltd

Brent A. Perdue. July 15, 2009

MySQL High Availability Solutions. Lenz Grimmer OpenSQL Camp St. Augustin Germany

SQL Server An Overview

Migrate Topaz databases from One Server to Another

New Features in MySQL 5.0, 5.1, and Beyond

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

MySQL Replication. openark.org

MySQL Cluster New Features. Johan Andersson MySQL Cluster Consulting johan.andersson@sun.com

Database Replication with MySQL and PostgresSQL

Netscape Internet Service Broker for C++ Programmer's Guide. Contents

Constructing a Data Lake: Hadoop and Oracle Database United!

news from Tom Bacon about Monday's lecture

A Brief Introduction to MySQL

Architectural patterns for building real time applications with Apache HBase. Andrew Purtell Committer and PMC, Apache HBase

php tek 2006 in Orlando Florida Lukas Kahwe Smith

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

N3458: Simple Database Integration in C++11

Oracle 11g PL/SQL training

Configuring Apache Derby for Performance and Durability Olav Sandstå

Comparing MySQL and Postgres 9.0 Replication

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

MCTS Microsoft SQL Server 2005 Implementation & Maintenance

1 File Processing Systems

Python MySQL Replication Documentation

Getting Started with Attunity CloudBeam for Azure SQL Data Warehouse BYOL

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

How To Create A Table In Sql (Ahem)

Big data is like teenage sex: everyone talks about it, nobody really knows how to do it, everyone thinks everyone else is doing it, so everyone

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

MySQL for Beginners Ed 3

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer

CSE 333 SECTION 6. Networking and sockets

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

C++ Programming Language

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Micro Focus Database Connectors

Member Functions of the istream Class

MySQL backups: strategy, tools, recovery scenarios. Akshay Suryawanshi Roman Vynar

: provid.ir

Optional Lab: Data Backup and Recovery in Windows 7

Parallel Replication for MySQL in 5 Minutes or Less

Conventional Files versus the Database. Files versus Database. Pros and Cons of Conventional Files. Pros and Cons of Databases. Fields (continued)

Intro to Databases. ACM Webmonkeys 2011

Database Replication with MySQL and PostgreSQL

Object Oriented Software Design II

Real-time reporting at 10,000 inserts per second. Wesley Biggs CTO 25 October 2011 Percona Live

Bidirectional replication for InterBase and Firebird

CA DLP. Stored Data Integration Guide. Release rd Edition

MySQL: Cloud vs Bare Metal, Performance and Reliability

High Availability Solutions for the MariaDB and MySQL Database

How to evaluate which MySQL High Availability solution best suits you

CS 103 Lab Linux and Virtual Machines

Basics of I/O Streams and File I/O

A Case Study of ebay UTF-8 Database Migration

Geodatabase Programming with SQL

SPHOL207: Database Snapshots with SharePoint 2013

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Informatica Data Replication FAQs

C++ Object Persistence with ODB

TivaWare Utilities Library

Microsoft SQL Server Beginner course content (3-day)

Lab - Data Backup and Recovery in Windows 7

Oracle Tuxedo Systems and Application Monitor (TSAM)

Developing an ODBC C++ Client with MySQL Database

Distributed Database Guide Version: 00.01

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

Oracle CRM Foundation

C++FA 5.1 PRACTICE MID-TERM EXAM

Transcription:

<Insert Picture Here> An API for Reading the MySQL Binary Log Mats Kindahl Lead Software Engineer, MySQL Replication & Utilities Lars Thalmann Development Director, MySQL Replication, Backup & Connectors

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle s products remains at the sole discretion of Oracle. 3

Outline Replication Architecture Binary logs Binary log event Reading binary log Connecting to server Reading from files Reading events Queries Reading rows (row-based replication) Other events 4

Replication Architecture Master Slave(s) Clients Changes 5

Replication Architecture Master Slave(s) Session Session Client Dump Dump Dump Databa Databa se Databa se se Session I/O I/O I/O SQL SQL SQL Binary Log 6

Replication Architecture Master Slave(s) Session Session Client Dump Dump Dump Database Databa se se Session I/O I/O I/O SQL SQL SQL Binary Log Relay Log 7

Replication to other systems Master Slave(s) Database Session I/O SQL Session Client Session Dump Dump Dump Dump HBase Relay Log Data Mining SOLR Full-text indexing Binary Log? 8

Transforming events Subject of our presentation Server File API Transformer SOLR Change Data Capture 9

Binlog API Library to process replication events API is ready for use Goals: Simple Extensible Efficient 10

<Insert Picture Here> Binlog API The replication listener How to capture events 11

First example #include <cstdlib> #include <iostream> #include <binlog_api.h> <Insert Picture Here> int main(int argc, char *argv[]) { const char *url = mysql://root@127.0.0.1:3360 ; Binary_log binlog(create_transport(url)); binlog.connect(); Binary_log_event *event; while (true) { int result = binlog.wait_for_next_event(&event); if (result == ERR_EOF) break; cout << at << binlog.get_position() << event type << event.get_type_code() << endl; } return EXIT_SUCCESS; } 12

Create network transport #include <cstdlib> #include <iostream> #include <binlog_api.h> <Insert Picture Here> int main(int argc, char *argv[]) { const char *url = mysql://root@127.0.0.1:3360 ; Binary_log binlog(create_transport(url)); binlog.connect(); Binary_log_event *event; while (true) { int result = binlog.wait_for_next_event(&event); if (result == ERR_EOF) break; cout << at << binlog.get_position() << event type << event.get_type_code() << endl; } return EXIT_SUCCESS; } 13

or file transport #include <cstdlib> #include <iostream> #include <binlog_api.h> <Insert Picture Here> int main(int argc, char *argv[]) { const char *url = file:///tmp/binlog.0000001 ; Binary_log binlog(create_transport(url)); binlog.connect(); Binary_log_event *event; while (true) { int result = binlog.wait_for_next_event(&event); if (result == ERR_EOF) break; cout << at << binlog.get_position() << event type << event.get_type_code() << endl; } return EXIT_SUCCESS; } 14

Connect the transport #include <cstdlib> #include <iostream> #include <binlog_api.h> <Insert Picture Here> int main(int argc, char *argv[]) { const char *url = file:///tmp/binlog.0000001 ; Binary_log binlog(create_transport(url)); binlog.connect(); Binary_log_event *event; while (true) { int result = binlog.wait_for_next_event(&event); if (result == ERR_EOF) break; cout << at << binlog.get_position() << event type << event.get_type_code() << endl; } return EXIT_SUCCESS; } 15

Digression: set read position Default: start at beginning <Insert Picture Here> Set position explicitly: if (binlog.set_position(file, pos)) { /* Handle error */ } 16

Read events #include <cstdlib> #include <iostream> #include <binlog_api.h> <Insert Picture Here> int main(int argc, char *argv[]) { Get event const char *url = file:///tmp/binlog.0000001 ; Binary_log binlog(create_transport(url)); binlog.connect(); Binary_log_event *event; while (true) { int result = binlog.wait_for_next_event(&event); if (result == ERR_EOF) break; cout << at << binlog.get_position() << event type << event >get_type_code() << endl; } return EXIT_SUCCESS; } 17

Steps summary Create a transport create_transport Connect to server connect Set position set_position Start event loop wait_for_next_event 18

<Insert Picture Here> Binlog API The replication listener Reading information in events 19

Binlog Event Structure Common Header Post-header Variable Part Common header Generic data Fixed size Post-header Event-specific data Fixed size Variable part Event-specific data Variable size 20

Reading the header Read common header header() Access fields <Insert Picture Here> switch (event >header() >type_code) { case QUERY_EVENT: case USER_VAR_EVENT: case FORMAT_DESCRIPTION_EVENT: } 21

Binlog Event Common Header timestamp 4 bytes flags type_code server_id next_position event_length 19 Bytes Data common to all events Next Position One-after-end of event Timestamp Statement start time Flags Binlog-in-use Thread-specific Suppress use Artificial Relay-log event 22

Binlog Event Structure Common Header Post-header Variable Part Common header Generic data Fixed size Post-header Event-specific data Fixed size Variable part Event-specific data Variable size 23

Query Event thread_id exec_time Common Header query Most common event Used for statements Statement logged literally in almost all cases db_name error_code std::vector<uint8_t> variables Special case: need to be decoded 24

Reading event data Cast to correct event type Access fields <Insert Picture Here> switch (event >header() >type_code) { case QUERY_EVENT: Query_event *qev = static_cast<query_event*>(event); cout << qev >query << endl; break; case USER_VAR_EVENT: case FORMAT_DESCRIPTION_EVENT: } 25

Event-driven API <Insert Picture Here> 26

Event-driven API Content handlers <Insert Picture Here> wait_for_next_event 27

Saving user-defined variables class Save_handler : public Content_handler { }; Save_handler::Map vars; Save_handler save_vars(vars); binlog.content_handler_pipeline() >push_back(&save_vars); 28

User-defined variables class Save_handler : public Content_handler { public: typedef std::map<std::string, std::string> Map; Save_handler(Map &container) : m_var(container) { } Binary_log_event * process_event(user_var_event *event) { m_var[event >name] = event >value; return NULL; } private: Map &m_var; }; <Insert Picture Here> 29

Replace handler class Replace_vars : public Content_handler { Binary_log_event * process_event(query_log_event *event) { /* Code to replace variables */ } }; Full example: basic-2.cpp 30

<Insert Picture Here> Binlog API The replication listener Example two: How to capture live row changes 31

Row events in the binlog We'll cover this soon (trust me) Transaction Transaction Table map Write rows Write rows Write rows Table map Delete rows Header A bunch of rows Map table definition to table ID 32

Capturing row events class Row_event_handler : public Content_handler { public: Binary_log_event * process_event(row_event *event) { switch(ev >header() >type_code) { case WRITE_ROWS_EVENT: case UPDATE_ROWS_EVENT: case DELETE_ROWS_EVENT:... 33

Capturing row events The *_ROWS_EVENT Defined in the table map event uint64_t table_id; uint16_t flags; uint64_t columns_len; uint32_t null_bits_len; vector<uint8_t> columns_before_image; vector<uint8_t> used_columns; vector<uint8_t> row; Raw row data 34

Reading rows Wrap raw row data in Row_event_set Iterate over rows using iterator Row_event_set rows(row_event, table_map_event); Row_event_set::iterator it= rows.begin(); You need to have captured this before! 35

Reading fields of a row Row_of_fields to iterate fields of a row Turns row into row of fields sequence Row_event_set rows(row_event, table_map_event); for (Row_event_set::iterator it = rows.begin() ; it!= rows.end() ; ++it) table_delete(os.str(), Row_of_fields(*it)); 36

Reading fields of a row Iterate over fields in Row_of_fields void table_delete (..., const Row_of_fields& fields) { Row_of_fields::iterator it= fields.begin(); for (int id = 0 ; it =! fields.end() ; ++it, ++id) { std::string str; Converter().to(str, *it); std::cout << id << "= " << str << std::endl; } } 37

Decoding a field Iterate over fields in Row_of_fields void table_delete (..., const Row_of_fields& fields) { Row_of_fields::iterator it= fields.begin(); for (int id = 0 ; it =! fields.end() ; ++it, ++id) { std::string str; Converter().to(str, *it); std::cout << id << "= " << str << std::endl; } } 38

Summary what's it for? Replicate to other systems Hbase, SOLR, etc. Triggering on specific events Call the DBA when tables are dropped? Monitor objects in database Browsing binary logs Extract subset of changes (by table, by execution time, etc.) Statistics Component in building other solutions Point-in-time restore Sharding / Load-balancing 39

Summary what we've covered Reading events Creating content handlers Processing queries Processing rows Reading fields but there is a lot more 40

Available at labs http://labs.mysql.com/ Source code available at launchpad http://launchpad.net/mysql-replication-listener MySQL High Availability Get it as free ebook: http://oreilly.com/go/ebookrequest Valid this week, mention event MySQL Replication Update 41