Coder Social home page Coder Social logo

kodama's Introduction

Kodama

Kodama is a MySQL replication listener based on ruby-binlog. Kodama provides a simple DSL to easily write your own replication listener.

Features

  • Provides simple DSL for writing binlog event handlers
  • Automatically restarts from the saved binlog position
  • Attempts to reconnect to MySQL when the connection is somehow teminated

These features allow developers to focus on writing their own replication logic rather than having to spend time figuring things out.

Kodama Benefits

Kodama can be used to replicate MySQL updates to other data stores, arbitrary software or even a flat file. The sole purpose of Kodama is to provide a convenient way to reflect the database updates to other components in your system.

  • Replicate from MySQL to Postgres
  • Replicate between tables with different schema
  • Sync production data to development DB while masking privacy information
  • Realtime full text index update

Dependencies

This gem links against MySQL's libreplication C shared library. You need to first install the mysql-replication-listener package.

But official repository has some bugs. It is recommended to use winebarrel's patched version (There are rpm package and homebrew formula).

Installation

Add this line to your application's Gemfile:

gem 'kodama'

And then execute:

$ bundle

Or install it yourself as:

$ gem install kodama

binlog_format

It is recommended to set the mysqld binlog_format option to ROW. This is because the ROW format allows Kodama to pickup every single updates made to the database.

SET GLOBAL binlog_format = 'ROW';

Usage

Simple client

require 'kodama'

Kodama::Client.start(:host => '127.0.0.1', :username => 'user') do |c|
  c.binlog_position_file = 'position.log'
  c.log_level = :info # [:debug|:info|:warn|:error|:fatal]
  c.connection_retry_limit = 100 # times
  c.connection_retry_wait = 3 # second

  # Exit gracefully when kodama receives specified signals
  c.gracefully_stop_on :QUIT, :INT

  c.on_query_event do |event|
    p event.query
  end

  c.on_row_event do |event|
    p event.rows
  end
end

Replicate to redis

require 'rubygems'
require 'kodama'
require 'json'
require 'redis'

class Worker
  def initialize
    @redis = Redis.new
  end

  def perform(event)
    record_id = get_row(event)[0] # first column is id
    @redis.set "#{event.table_name}_#{record_id}", event.rows.to_json
  end

  def get_row(event)
    case event.event_type
    when /Write/, /Delete/
      event.rows[0]    # [row]
    when /Update/
      event.rows[0][1] # [[old_row, new_row]]
    end
  end
end


worker = Worker.new

Kodama::Client.start(:host => '127.0.0.1', :username => 'user') do |c|
  c.binlog_position_file = 'position.log'

  c.on_row_event do |event|
    worker.perform(event)
  end
end

Configuration

binlog_position_file

Sets the filename to save the binlog position. Kodama will read this file and resume listening from the stored position.

log_level

Set logger's log level. It accepts :debug, :info, :warn, :error, :fatal.

connection_retry_limit, connection_retry_wait

If for some reason the connection to MySQL is terminated, Kodama will attempt to reconnect connection_retry_limit times, while waiting connection_retry_wait seconds between attempts.

gracefully_stop_on

Kodama traps specified signals and stop gracefully. It accpets multiple signals like following.

Kodama::Client.start do |c|
  c.gracefully_stop_on :INT, :QUIT
end

Authors

Yusuke Mito, Genki Sugawara

Based On

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

kodama's People

Contributors

y310 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kodama's Issues

Kodama hangs when restarting with binlog_position_file

Hi,

when using binlog_position_file the programrm hangs on second startup.

This is my code:

Kodama::Client.start(:host => 'xxx.xxx.xxx.xxx', :username => 'root', :password => '') do |c|
  c.binlog_position_file = 'position.log'
  c.log_level = :info
  c.connection_retry_limit = 100
  c.connection_retry_wait = 3
  c.gracefully_stop_on :QUIT, :INT

  c.on_row_event do |event|
    if event.table_name == "orders" then
      eventHandler = Event.new(columns)
      eventHandler.handle(event)
    end
  end
end

On first startup everything works fine. But after stopping it with CTRL+C and trying to restart it, the programm won't startup correctly. Dtrussing it gives me than:

SYSCALL(args)        = return

I need to kill -9 the process to get my Terminal back.

Versions:

ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
ruby-binlog (0.1.9)
kodama (0.1.1)

Thanks in advance
Björn

client.set_position throwing "An unspecified error occurred"

Hello,

I've been trying to start up kodama using the simple client code from your README:

require 'kodama'
class KodamaService
  Kodama::Client.start(:host => '127.0.0.1', :username => 'root', :port => 3306) do |c|
    c.binlog_position_file = '/usr/local/var/mysql/mysql-binlog.index'
    c.log_level = :debug # [:debug|:info|:warn|:error|:fatal]
    c.connection_retry_limit = 3 # times
    c.connection_retry_wait = 1 # second

    # Exit gracefully when kodama receives specified signals
    c.gracefully_stop_on :QUIT, :INT

    c.on_query_event do |event|
      p event.query
    end

    c.on_row_event do |event|
      p event.rows
    end
  end
end

I get the following error:

D, [2014-02-24T15:35:26.623775 #60639] DEBUG -- : An unspecified error occurred (2) (Binlog::Error)
/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:108:in `set_position'
/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:108:in `start'
/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:20:in `start'
kodama-client.rb:3:in `<main>'
D, [2014-02-24T15:35:29.636154 #60639] DEBUG -- : An unspecified error occurred (2) (Binlog::Error)
/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:108:in `set_position'
/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:108:in `start'
/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:20:in `start'
kodama-client.rb:3:in `<main>'
/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:108:in `set_position': An unspecified error occurred (2) (Binlog::Error)
    from /usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:108:in `start'
    from /usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/kodama-0.1.1/lib/kodama/client.rb:20:in `start'
    from kodama-client.rb:3:in `<main>'

I am using winebarrel's mysql-replication-listener as you suggested. I've tried both versions 0.0.47-10 and 0.0.47-11. I started up mysql with the following commands:

mysqld --log-bin='mysql-binlog' --bind-address='127.0.0.1'

Once mysql is started, I ran the command

SET GLOBAL binlog_format = 'ROW';

The binlogs are written out to /usr/local/var/mysql by default. I tested out the actual replication part with mysql - when I update/write something to a table, the mysql-binlog.000001 file grows. I've been going through every line of code with a debugger, and here are some things I've found when I put a breakpoint in line 107 in lib/kodama/client.rb:

  • @binlog_info.filename = './mysql-binlog.000001'
  • @binlog_info.position = 0
    Both seem valid and it goes into the if @binlog_info.valid? block. NOW here the problem starts - it seems to throw errors during the ``client.set_position(@binlog_info.filename, @binlog_info.position)` method call.

Can you help? I've been bashing my head against this trying to get it to work to no avail :( Please let me know! Thanks!!

Have you seen this error by any chance?

symbol lookup error: /var/lib/gems/1.9.1/gems/ruby-binlog-0.1.8/lib/binlog.so: undefined symbol: rb_thread_blocking_region_begin

Thanks -- I'm looking forward to trying this out.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.