SlideShare a Scribd company logo
MySQL Binary Log API Presentation - OSCON 2011
<Insert Picture Here>




An API for Reading the MySQL Binary Log
Mats Kindahl                   Lars Thalmann
Lead Software Engineer, MySQL Development Director, MySQL
Replication & Utilities       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
                                Databa
                                 Databa
                                  Databa
                                  se
Session
 Client        Dump
                Dump               se
                 Dump                se
Session
                        I/O     SQL
                         I/O     SQL
                          I/O     SQL




      Binary Log




                                           6
Replication Architecture
          Master           Slave(s)

Session
                                  Databa
                                 Database
                                   Databa
Session
 Client        Dump
                Dump                se
                 Dump                 se
Session
                        I/O       SQL
                         I/O       SQL
                          I/O       SQL




      Binary Log          Relay Log




                                            7
Replication to other systems       Slave(s)


                                         Database

          Master
                               I/O        SQL


Session
                                 Relay Log

Session
 Client        Dump
                Dump
                 Dump
                  Dump
                                                Data
Session                      HBase              Mining



                                                Full-text
                              SOLR              indexing

      Binary Log
                         ?



                                                      8
Transforming events
              Subject of our presentation
Se
   rv
    r e




            API
                  Transformer      SOLR
     le
   Fi




          “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
   Common Header      • Generic data
                      • Fixed size
                    • Post-header
    Post-header
                      • Event-specific data
                      • Fixed size
                    • Variable part
                      • Event-specific data
    Variable Part
                      • Variable size




                                              20
Reading the header

•Read common header                     <Insert Picture Here>


  – header()
•Access fields
switch (event­>header()­>type_code) {
case QUERY_EVENT:
  …
case USER_VAR_EVENT:
  …
case FORMAT_DESCRIPTION_EVENT:
  …
}




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




                                                                 22
Binlog Event Structure

                    • Common header
   Common Header      • Generic data
                      • Fixed size
                    • Post-header
    Post-header
                      • Event-specific data
                      • Fixed size
                    • Variable part
                      • Event-specific data
    Variable Part
                      • Variable size




                                              23
Query Event
thread_id        exec_time   •Most common event
      Common Header
                             •Used for statements
                             •Statement logged
                              literally
            query              – … in almost all cases

                         std::vector<uint8_t> variables          ed
                                                             ecod
                                                     to be d
            error_code
                                               ne ed
db_name                                 ca se:
                               Spe cial




                                                                  24
Reading event data

•Cast to correct event type             <Insert Picture Here>


•Access fields
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:
                                              <Insert Picture Here>
  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;
};




                                                                 29
Replace handler
class Replace_vars : 
  public Content_handler
{
  Binary_log_event *
  process_event(Query_log_event *event)
  {
    /* Code to replace variables */
  }                                p
                                 cp
};                            2.
                                       s ic-
                                  ba
                            le:
                          mp
                     e xa
              Full

                                               30
<Insert Picture Here>



                                               Binlog API
                                ●
                                  The replication listener

                                           Example two:
                        How to capture live row changes




                                                             31
We'll cover this
     Row events in the binlog                      soon (trust me)


              Table map
                                           Header
              Write rows
Transaction
              Write rows

              Write rows

              Table map
Transaction
              Delete rows                          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
MySQL Binary Log API Presentation - OSCON 2011

More Related Content

PDF
State of the art of MySQL replication and clustering
KEY
Perf Tuning Short
PDF
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
PPT
OSSCube MySQL Cluster Tutorial By Sonali At Osspac 09
PDF
MySQL 5.5&5.6 new features summary
PDF
Percona Live 2012PPT: introduction-to-mysql-replication
PDF
Methods of NoSQL database systems benchmarking
PDF
MySQL高可用
State of the art of MySQL replication and clustering
Perf Tuning Short
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
OSSCube MySQL Cluster Tutorial By Sonali At Osspac 09
MySQL 5.5&5.6 new features summary
Percona Live 2012PPT: introduction-to-mysql-replication
Methods of NoSQL database systems benchmarking
MySQL高可用

What's hot (20)

PDF
MySQL 5.7 in a Nutshell
PDF
What You Should Know About WebLogic Server 12c (12.2.1.2) #oow2015 #otntour2...
PDF
The Native NDB Engine for Memcached
PDF
Percona Live 2012PPT:mysql-security-privileges-and-user-management
PPTX
Easy MySQL Replication Setup and Troubleshooting
PPTX
Hadoop For Enterprises
PDF
Weblogic Administration Managed Server migration
PDF
MySQL Performance Best Practices
PPTX
2020 pre fosdem mysql clone
PDF
MySQL Performance Tuning Variables
PDF
MySQL Group Replication - Ready For Production? (2018-04)
PPTX
2015: Whats New in MySQL 5.7, At Oracle Open World, November 3rd, 2015
PDF
MySQL cluster 72 in the Cloud
PDF
Oracle Database SQL Tuning Concept
ODP
MySQL 5.7 - What's new and How to upgrade
ODP
Benchmarking MongoDB and CouchBase
PDF
5050 dev nation
PDF
12cR2 Single-Tenant: Multitenant Features for All Editions
PDF
Upcoming changes in MySQL 5.7
PDF
InnoDB Tablespace Encryption
MySQL 5.7 in a Nutshell
What You Should Know About WebLogic Server 12c (12.2.1.2) #oow2015 #otntour2...
The Native NDB Engine for Memcached
Percona Live 2012PPT:mysql-security-privileges-and-user-management
Easy MySQL Replication Setup and Troubleshooting
Hadoop For Enterprises
Weblogic Administration Managed Server migration
MySQL Performance Best Practices
2020 pre fosdem mysql clone
MySQL Performance Tuning Variables
MySQL Group Replication - Ready For Production? (2018-04)
2015: Whats New in MySQL 5.7, At Oracle Open World, November 3rd, 2015
MySQL cluster 72 in the Cloud
Oracle Database SQL Tuning Concept
MySQL 5.7 - What's new and How to upgrade
Benchmarking MongoDB and CouchBase
5050 dev nation
12cR2 Single-Tenant: Multitenant Features for All Editions
Upcoming changes in MySQL 5.7
InnoDB Tablespace Encryption
Ad

Similar to MySQL Binary Log API Presentation - OSCON 2011 (20)

PDF
MySQL Cluster Asynchronous replication (2014)
PDF
2012 replication
PDF
iguazio - nuclio overview to CNCF (Sep 25th 2017)
PDF
PostgreSQL Write-Ahead Log (Heikki Linnakangas)
PDF
(phpconftw2012) PHP as a Middleware in Embedded Systems
PDF
Dbvisit replicate: logical replication made easy
PDF
nuclio Overview October 2017
PPTX
Open Source LinkedIn Analytics Pipeline - BOSS 2016 (VLDB)
PDF
What's New In Apache Lenya 1.4
PDF
Splunk as a_big_data_platform_for_developers_spring_one2gx
PDF
[KubeCon EU 2020] containerd Deep Dive
PPTX
Road to sbt 1.0 paved with server
PDF
Replication features, technologies and 3rd party Extinction
PDF
Java one 2015 [con3339]
PDF
2012 scale replication
PDF
MySQL Replication
PDF
Creating Scalable JVM/Java Apps on Heroku
PDF
The Java Content Repository
PDF
Using MongoDB to Build a Fast and Scalable Content Repository
PDF
Playing in the Same Sandbox: MySQL and Oracle
MySQL Cluster Asynchronous replication (2014)
2012 replication
iguazio - nuclio overview to CNCF (Sep 25th 2017)
PostgreSQL Write-Ahead Log (Heikki Linnakangas)
(phpconftw2012) PHP as a Middleware in Embedded Systems
Dbvisit replicate: logical replication made easy
nuclio Overview October 2017
Open Source LinkedIn Analytics Pipeline - BOSS 2016 (VLDB)
What's New In Apache Lenya 1.4
Splunk as a_big_data_platform_for_developers_spring_one2gx
[KubeCon EU 2020] containerd Deep Dive
Road to sbt 1.0 paved with server
Replication features, technologies and 3rd party Extinction
Java one 2015 [con3339]
2012 scale replication
MySQL Replication
Creating Scalable JVM/Java Apps on Heroku
The Java Content Repository
Using MongoDB to Build a Fast and Scalable Content Repository
Playing in the Same Sandbox: MySQL and Oracle
Ad

More from Mats Kindahl (13)

PDF
Why rust?
PDF
Building Scalable High Availability Systems using MySQL Fabric
PDF
High-Availability using MySQL Fabric
PDF
Elastic Scalability in MySQL Fabric Using OpenStack
PDF
Sharding and Scale-out using MySQL Fabric
PDF
MySQL Fabric: Easy Management of MySQL Servers
PDF
MySQL Applier for Apache Hadoop: Real-Time Event Streaming to HDFS
PDF
MySQL Sharding: Tools and Best Practices for Horizontal Scaling
PDF
Replication Tips & Trick for SMUG
PDF
Sharding using MySQL and PHP
PDF
Replication Tips & Tricks
PDF
Python Utilities for Managing MySQL Databases
PDF
Mysteries of the binary log
Why rust?
Building Scalable High Availability Systems using MySQL Fabric
High-Availability using MySQL Fabric
Elastic Scalability in MySQL Fabric Using OpenStack
Sharding and Scale-out using MySQL Fabric
MySQL Fabric: Easy Management of MySQL Servers
MySQL Applier for Apache Hadoop: Real-Time Event Streaming to HDFS
MySQL Sharding: Tools and Best Practices for Horizontal Scaling
Replication Tips & Trick for SMUG
Sharding using MySQL and PHP
Replication Tips & Tricks
Python Utilities for Managing MySQL Databases
Mysteries of the binary log

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Cloud computing and distributed systems.
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Machine learning based COVID-19 study performance prediction
PPT
Teaching material agriculture food technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Weekly Chronicles - August'25-Week II
20250228 LYD VKU AI Blended-Learning.pptx
Spectroscopy.pptx food analysis technology
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Assigned Numbers - 2025 - Bluetooth® Document
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Cloud computing and distributed systems.
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Programs and apps: productivity, graphics, security and other tools
Machine learning based COVID-19 study performance prediction
Teaching material agriculture food technology
Per capita expenditure prediction using model stacking based on satellite ima...

MySQL Binary Log API Presentation - OSCON 2011

  • 2. <Insert Picture Here> An API for Reading the MySQL Binary Log Mats Kindahl Lars Thalmann Lead Software Engineer, MySQL Development Director, MySQL Replication & Utilities Replication, Backup & Connectors
  • 3. 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
  • 4. 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
  • 5. Replication Architecture Master Slave(s) Clients Changes 5
  • 6. Replication Architecture Master Slave(s) Session Databa Databa Databa se Session Client Dump Dump se Dump se Session I/O SQL I/O SQL I/O SQL Binary Log 6
  • 7. Replication Architecture Master Slave(s) Session Databa Database Databa Session Client Dump Dump se Dump se Session I/O SQL I/O SQL I/O SQL Binary Log Relay Log 7
  • 8. Replication to other systems Slave(s) Database Master I/O SQL Session Relay Log Session Client Dump Dump Dump Dump Data Session HBase Mining Full-text SOLR indexing Binary Log ? 8
  • 9. Transforming events Subject of our presentation Se rv r e API Transformer SOLR le Fi “Change Data Capture” 9
  • 10. Binlog API •Library to process replication events •API is ready for use •Goals: – Simple – Extensible – Efficient 10
  • 11. <Insert Picture Here> Binlog API ● The replication listener How to capture events 11
  • 12. 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
  • 13. 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
  • 14. … 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
  • 15. 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
  • 16. Digression: set read position •Default: start at beginning <Insert Picture Here> •Set position explicitly: if (binlog.set_position(file, pos)) {   /* Handle error */ } 16
  • 17. 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
  • 18. Steps summary •Create a transport – create_transport •Connect to server – connect •Set position – set_position •Start event loop – wait_for_next_event 18
  • 19. <Insert Picture Here> Binlog API ● The replication listener Reading information in events 19
  • 20. Binlog Event Structure • Common header Common Header • Generic data • Fixed size • Post-header Post-header • Event-specific data • Fixed size • Variable part • Event-specific data Variable Part • Variable size 20
  • 21. Reading the header •Read common header <Insert Picture Here> – header() •Access fields switch (event­>header()­>type_code) { case QUERY_EVENT:   … case USER_VAR_EVENT:   … case FORMAT_DESCRIPTION_EVENT:   … } 21
  • 22. Binlog Event Common Header type_code timestamp server_id • Data common to all events • Next Position 4 bytes – One-after-end of event • Timestamp – Statement start time • Flags – Binlog-in-use next_position – Thread-specific – Suppress “use” event_length – Artificial flags 19 Bytes – Relay-log event 22
  • 23. Binlog Event Structure • Common header Common Header • Generic data • Fixed size • Post-header Post-header • Event-specific data • Fixed size • Variable part • Event-specific data Variable Part • Variable size 23
  • 24. Query Event thread_id exec_time •Most common event Common Header •Used for statements •Statement logged literally query – … in almost all cases std::vector<uint8_t> variables ed ecod to be d error_code ne ed db_name ca se: Spe cial 24
  • 25. Reading event data •Cast to correct event type <Insert Picture Here> •Access fields 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
  • 26. Event-driven API <Insert Picture Here> 26
  • 27. Event-driven API • Content handlers <Insert Picture Here> wait_for_next_event 27
  • 29. User-defined variables class Save_handler : public Content_handler { public: <Insert Picture Here>   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; }; 29
  • 31. <Insert Picture Here> Binlog API ● The replication listener Example two: How to capture live row changes 31
  • 32. We'll cover this Row events in the binlog soon (trust me) Table map Header Write rows Transaction Write rows Write rows Table map Transaction Delete rows A bunch of rows Map table definition to table ID 32
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. Summary – what we've covered •Reading events •Creating content handlers •Processing queries •Processing rows •Reading fields •… but there is a lot more 40
  • 41. • 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