Module: ZooKeeper

Defined in:
lib/zkruby/zkruby.rb,
lib/zkruby/util.rb,
lib/zkruby/multi.rb,
lib/zkruby/client.rb,
lib/zkruby/bindata.rb,
lib/zkruby/session.rb,
lib/zkruby/protocol.rb,
lib/zkruby/eventmachine.rb

Overview

A pure ruby implementation of the zk client library

It implements the client side of the ZooKeeper TCP protocol directly rather than calling the zk client libraries

Defined Under Namespace

Modules: Data, EventMachine, Operations, Proto, Protocol, RubyIO Classes: AsyncOp, Client, ClosePacket, Error, ExistsPacket, KeeperState, Operation, Packet, Perms, ProtocolError, Session, Transaction, WatchEvent, Watcher, WrappedOp, ZKBoolean, ZKBuffer, ZKString

Constant Summary collapse

ANYONE_ID_UNSAFE =

The Anyone ID

Data::Identity.new(:scheme => "world", :identity => "anyone")
AUTH_IDS =

Represents the set of auth ids for the current connection

Data::Identity.new(:scheme => "auth")
OPEN_ACL_UNSAFE =
[ acl(ANYONE_ID_UNSAFE, Perms::ALL) ]
CREATOR_ALL_ACL =
[ acl(AUTH_IDS, Perms::ALL) ]
READ_ACL_UNSAFE =
[ acl(ANYONE_ID_UNSAFE, Perms::READ) ]
ID_ANYONE_UNSAFE =

The Anyone ID

ANYONE_ID_UNSAFE
ID_USE_AUTHS =

Represents the set of auth ids for the current connection

AUTH_IDS
ACL_OPEN_UNSAFE =
OPEN_ACL_UNSAFE
ACL_CREATOR_ALL =
CREATOR_ALL_ACL
ACL_READ_UNSAFE =
READ_ACL_UNSAFE
CURRENT =
:zookeeper_current
VERSION =

Major/Minor numbers track zookeeper itself, final digit is our build number

"3.4.3"

Class Method Summary collapse

Class Method Details

.acl(id, *perms) ⇒ Data::ACL

Convenience method to create a zk ACL

ZK.acl(ZK.id("world","anyone"), ZK::Perms.DELETE, ZL::Perms.WRITE)

Parameters:

Returns:

See Also:

  • #perms


73
74
75
# File 'lib/zkruby/client.rb', line 73

def self.acl(id,*perms)
    Data::ACL.new( :identity => id, :perms => self.perms(*perms) )
end

.add_binding(binding) ⇒ Object



10
11
12
# File 'lib/zkruby/zkruby.rb', line 10

def self.add_binding(binding)
    @bindings << binding unless @bindings.include?(binding)
end

.connect(addresses, options = {}) {|| ... } ⇒ Client

Main method for connecting to a client

Parameters:

  • addresses (Array<String>)

    list of host:port for the ZK cluster as Array or comma separated String

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :binding (Class)

    binding optional implementation class either ZooKeeper::EventMachine::Binding or ZooKeeper::RubyIO::Binding but normally autodiscovered

  • :chroot (String)

    chroot path. All client calls will be made relative to this path

  • :watch (Watcher)

    the default watcher

  • :scheme (String)

    the authentication scheme

  • :auth (String)

    the authentication credentials

Yield Parameters:

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/zkruby/client.rb', line 120

def self.connect(addresses,options={},&block)
    if options.has_key?(:binding)
        binding_type = options[:binding]
    else
        binding_type = @bindings.detect { |b| b.available? }
        raise ProtocolError,"No available binding" unless binding_type
    end
    binding = binding_type.new()
    session = Session.new(binding,addresses,options)
    client = Client.new(binding)
    binding.start(client,session)

    return client unless block_given?

    binding_type.context() do |storage|
        @binding_storage = storage
        storage.current[CURRENT] ||= []
        storage.current[CURRENT].push(client)
        begin
            block.call(client)
        ensure
            storage.current[CURRENT].pop
            client.close() unless session.closed?
        end
    end
end

.currentObject

within the block supplied to #connect this will return the current ZK client



149
150
151
152
# File 'lib/zkruby/client.rb', line 149

def self.current
    #We'd use if key? here if strand supported it
    @binding_storage.current[CURRENT].last if @binding_storage.current[CURRENT]
end

.id(scheme, id) ⇒ Data::Identity

Convenience method to create a zk Identity

Parameters:

  • scheme (String)
  • identity (String)

Returns:



61
62
63
# File 'lib/zkruby/client.rb', line 61

def self.id(scheme,id)
    Data::Identity.new(:scheme => scheme, :identity => id)
end

.path_to_seq(path) ⇒ Object



103
104
105
106
# File 'lib/zkruby/client.rb', line 103

def self.path_to_seq(path)
    matches = /^(.*)(\d{10})$/.match(path)
    matches ? [matches[1],matches[2].to_i] : [path,nil]
end

.perms(*perms) ⇒ Fixnum

Combine permissions constants

Parameters:

  • perms... (Perms)

    list of permissions to combine, can be Perms constants, symbols or ints

Returns:

  • (Fixnum)

    integer representing the combined permission



53
54
55
# File 'lib/zkruby/client.rb', line 53

def self.perms(*perms)
    perms.inject(0) { | result, perm | result = result | Perms.get(perm) }
end

.seq_to_path(path, id) ⇒ Object



99
100
101
# File 'lib/zkruby/client.rb', line 99

def self.seq_to_path(path,id)
    format("%s%010d",path,id)
end