zkruby

DESCRIPTION:

Pure ruby client for ZooKeeper (zookeeper.apache.org)

FEATURES

Supports full ZooKeeper API, synchronous or asynchronous style, watches etc.. with implementations over EventMachine or plain old Ruby IO/Threads

Advantages:

  • Rubyist API - with block is asynchronous, without block is synchronous

  • Avoids conflicts between various Ruby threading models and the C/Java apis

  • Same code for JRuby or MRI

  • Connection bindings for pure ruby IO and eventmachine, or roll your own

Disadvantages:

  • Duplicated code from Java/C libraries, particularly around herd effect protection

  • Maintain in parallel with breaking changes in protocol which are possibly more likely than breaking changes in the client API

  • Probably not as optimised in terms of performance (but your client code is ruby anyway)

  • Not production tested (yet- do you want to be the first?)

SYNOPSIS:

  1. Configure Slf4R Logging (rubygems.org/gems/slf4r)

  2. Get a connection ZooKeeper.connect

  3. Make requests on the returned ZooKeeper::Client

require 'slf4r/ruby_logging' 
require 'zkruby'

# Using ruby threads and sockets (default)
zk = ZooKeeper.connect("localhost:2181")
# Synchronous
stat = zk.exists("/aPath")

# Asynchronous
zk.exists("/aPath) { |stat| puts stat.inspect }

# Watches
watch = lambda { |state,path,event| puts "Watch fired #{state} #{path} #{event}" }

stat,data = zk.get("/aPath",watch)

# OR with EventMachine
require 'em_zkruby'
EventMachine.run do
   # Magic of Fibers (via Strands) lets us code normally and execute asynchronously
   Strand.new() do
      begin
          zk = ZooKeeper.connect("localhost:2181")
          #Sync
          path = zk.create("/aPath/mynode",ZK::ACL_ANYONE_UNSAFE,:ephemeral,:sequential) 

          #Async
          zk.get(path) do |stat,data|
             puts "#{stat.inspect} #{data}" 
          end

      rescue ZooKeeper::Error => zkex
          puts zkex.message
      end
   end
end

REQUIREMENTS:

  • A ZooKeeper cluster to connect to

  • Ruby 1.9 (but a backport should be straightforward)

INSTALL:

$ (sudo) gem install zkruby

DEVELOPERS:

Checkout the zookeeper source from zookeeper.apache.org

Checkout the zkruby source into the contrib directory

Install hoe

$ (sudo) gem install hoe

Install other dependencies

$ rake check_extra_deps

Generate docs and run the tests/specs

$ rake newb

LICENSE:

(The MIT License)

Copyright © 2011

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.