Class: ZK::Client::Base
- Inherits:
-
Object
- Object
- ZK::Client::Base
- Defined in:
- lib/zk/client/base.rb
Overview
There is a lot of functionality mixed into the subclasses of this class! You should take a look at Unixisms, Conveniences, and StateMixin for a lot of the higher-level functionality!
This class forms the base API for interacting with ZooKeeper. Most people will
want to create instances of the class ZK::Client::Threaded, and the most
convenient way of doing that is through the top-level method ZK.new
Direct Known Subclasses
Instance Attribute Summary collapse
-
#event_handler ⇒ Object
readonly
The Eventhandler is used by client code to register callbacks to handle events triggered for given paths.
Instance Method Summary collapse
-
#add_auth(*args) ⇒ Object
Send authentication.
-
#children(path, opts = {}) ⇒ Object
Return the list of the children of the node of the given path.
-
#close ⇒ Object
close the underlying connection, but do not reset callbacks registered via the
register
method. -
#close! ⇒ Object
close the underlying connection and clear all pending events.
-
#closed? ⇒ Boolean
returns true if the connection has been closed.
-
#connect(opts = {}) ⇒ Object
Connect to the server/cluster.
-
#create(path, *args) ⇒ String?
Create a node with the given path.
-
#delete(path, opts = {}) ⇒ Object
Delete the node with the given path.
-
#event_dispatch_thread? ⇒ Boolean
returns true if the caller is calling from the event dispatch thread.
-
#exists?(path, opts = {}) ⇒ Boolean
sugar around stat.
-
#get(path, opts = {}) ⇒ Array
Return the data and stat of the node of the given path.
-
#get_acl(path, opts = {}) ⇒ Object
Return the ACL and stat of the node of the given path.
-
#initialize(host, opts = {}) ⇒ Base
constructor
abstract
Create a new client and connect to the zookeeper server.
-
#register(path, opts = {}, &block) {|event| ... } ⇒ EventHandlerSubscription
Register a block that should be delivered events for a given path.
-
#reopen(timeout = nil) ⇒ Symbol
reopen the underlying connection.
-
#session_id ⇒ Fixnum
The session_id of the underlying connection.
-
#session_passwd ⇒ String
The session_passwd of the underlying connection.
-
#set(path, data, opts = {}) ⇒ Stat?
Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions).
-
#set_acl(path, acls, opts = {}) ⇒ Object
Set the ACL for the node of the given path if such a node exists and the given version matches the version of the node.
-
#stat(path, opts = {}) ⇒ Zookeeper::Stat
Return the stat of the node of the given path.
-
#watcher ⇒ Object
deprecated
Deprecated.
for backwards compatibility only
Constructor Details
#initialize(host, opts = {}) ⇒ Base
Overridden in subclasses
Create a new client and connect to the zookeeper server.
100 101 102 103 104 |
# File 'lib/zk/client/base.rb', line 100 def initialize(host, opts={}) # keep track of the process we were in when we started @host = host @pid = Process.pid end |
Instance Attribute Details
#event_handler ⇒ Object (readonly)
The Eventhandler is used by client code to register callbacks to handle events triggered for given paths.
49 50 51 |
# File 'lib/zk/client/base.rb', line 49 def event_handler @event_handler end |
Instance Method Details
#add_auth(*args) ⇒ Object
Send authentication
883 884 885 886 |
# File 'lib/zk/client/base.rb', line 883 def add_auth(*args) opts = args. call_and_check_rc(:add_auth, opts ) end |
#children(path, opts = {}) ⇒ Object
It is important to note that the list of children is not sorted. If you
need them to be ordered, you must call .sort
on the returned array
Return the list of the children of the node of the given path.
If the watch is true and the call is successful (no exception is thrown),
registered watchers of the children of the node will be enabled. The
watch will be triggered by a successful operation that deletes the node
of the given path or creates/delete a child under the node. See watcher
for documentation on how to register blocks to be called when a watch
event is fired.
712 713 714 715 716 717 718 719 720 721 |
# File 'lib/zk/client/base.rb', line 712 def children(path, opts={}) h = { :path => path }.merge(opts) rv = setup_watcher!(:child, h) do call_and_check_rc(:get_children, h) end opts[:callback] ? rv : rv[:children] end |
#close ⇒ Object
close the underlying connection, but do not reset callbacks registered
via the register
method. This is to be used when preparing to fork.
142 143 144 |
# File 'lib/zk/client/base.rb', line 142 def close wrap_state_closed_error { cnx.close if cnx && !cnx.closed? } end |
#close! ⇒ Object
close the underlying connection and clear all pending events.
135 136 137 138 |
# File 'lib/zk/client/base.rb', line 135 def close! event_handler.close close end |
#closed? ⇒ Boolean
returns true if the connection has been closed
74 75 76 77 78 79 |
# File 'lib/zk/client/base.rb', line 74 def closed? return true if cnx.nil? # XXX: should this be *our* idea of closed or ZOO_CLOSED_STATE ? defined?(::JRUBY_VERSION) ? jruby_closed? : mri_closed? end |
#connect(opts = {}) ⇒ Object
Connect to the server/cluster. This is called automatically by the constructor by default.
148 149 |
# File 'lib/zk/client/base.rb', line 148 def connect(opts={}) end |
#create(path, opts = {}) ⇒ String? #create(path, data, opts = {}) ⇒ String?
Document the asynchronous methods
Create a node with the given path. The node data will be the given data. The path is returned.
If the ephemeral option is given, the znode created will be removed by the server automatically when the session associated with the creation of the node expires. Note that ephemeral nodes cannot have children.
The sequence option, if true, will cause the server to create a sequential node. The actual path name of a sequential node will be the given path plus a suffix "_i" where i is the current sequential number of the node. Once such a node is created, the sequential number for the path will be incremented by one (i.e. the generated path will be unique across all clients).
Note that since a different actual path is used for each invocation of creating sequential node with the same path argument, the call will never throw a NodeExists exception.
If a node is created successfully, the ZooKeeper server will trigger the watches on the path left by exists calls, and the watches on the parent of the node by children calls.
350 351 352 353 354 |
# File 'lib/zk/client/base.rb', line 350 def create(path, *args) h = parse_create_args(path, *args) rv = call_and_check_rc(:create, h) h[:callback] ? rv : rv[:path] end |
#delete(path, opts = {}) ⇒ Object
Delete the node with the given path. The call will succeed if such a node exists, and the given version matches the node's version (if the given version is -1, it matches any node's versions), and the node has no children.
This operation, if successful, will trigger all the watches on the node of the given path left by exists API calls, and the watches on the parent node left by children API calls.
Can be called with just the path, otherwise a hash with the arguments set. Supports being executed asynchronousy by passing a callback object.
790 791 792 793 794 |
# File 'lib/zk/client/base.rb', line 790 def delete(path, opts={}) h = { :path => path, :version => -1 }.merge(opts) rv = call_and_check_rc(:delete, h) opts[:callback] ? rv : nil end |
#event_dispatch_thread? ⇒ Boolean
returns true if the caller is calling from the event dispatch thread
1038 1039 1040 |
# File 'lib/zk/client/base.rb', line 1038 def event_dispatch_thread? cnx.event_dispatch_thread? end |
#exists?(path, opts = {}) ⇒ Boolean
sugar around stat
only works for the synchronous version of stat. for async version, this method will act exactly like stat
646 647 648 649 650 |
# File 'lib/zk/client/base.rb', line 646 def exists?(path, opts={}) # XXX: this should use the underlying 'exists' call! rv = stat(path, opts) opts[:callback] ? rv : rv.exists? end |
#get(path, opts = {}) ⇒ Array
fix references to Watcher documentation
Return the data and stat of the node of the given path.
If :watch
is true and the call is successful (no exception is
raised), registered watchers on the node will be 'armed'. The watch
will be triggered by a successful operation that sets data on the node,
or deletes the node. See watcher
for documentation on how to register
blocks to be called when a watch event is fired.
Supports being executed asynchronousy by passing a callback object.
453 454 455 456 457 458 459 460 461 |
# File 'lib/zk/client/base.rb', line 453 def get(path, opts={}) h = { :path => path }.merge(opts) rv = setup_watcher!(:data, h) do call_and_check_rc(:get, h) end opts[:callback] ? rv : rv.values_at(:data, :stat) end |
#get_acl(path, opts = {}) ⇒ Object
this method is pretty much untested, YMMV
Return the ACL and stat of the node of the given path.
836 837 838 839 840 |
# File 'lib/zk/client/base.rb', line 836 def get_acl(path, opts={}) h = { :path => path }.merge(opts) rv = call_and_check_rc(:get_acl, h) opts[:callback] ? rv : rv.values_at(:children, :stat) end |
#register(path, interests = nil, &block) ⇒ EventHandlerSubscription #register(path, opts = {}, &block) ⇒ EventHandlerSubscription
All node watchers are one-shot handlers. After an event is delivered to your handler, you must re-watch the node to receive more events. This leads to a pattern you will find throughout ZK code that avoids races, see the example below "avoiding a race"
Register a block that should be delivered events for a given path. After
registering a block, you need to call #get, #stat, or #children with the
:watch => true
option for the block to receive the next event (see note).
#get and #stat will cause the block to receive events when the path is
created, deleted, or its data is changed. #children will cause the block to
receive events about its list of child nodes changing (i.e. being added
or deleted, but not their content changing).
This method will return an EventHandlerSubscription instance that can be used
to remove the block from further updates by calling its .unsubscribe
method.
You can specify a list of event types after the path that you wish to
receive in your block using the :only
option. This allows you to
register different blocks for different types of events. This sounds
more convenient, but there is a potential pitfall. The :only
option does filtering behind the scenes, so if you need a :created
event, but a :changed
event is delivered instead, and you don't have
a handler registered for the :changed
event which re-watches, then
you will most likely just miss it and blame the author. You should try
to stick to the style where you use a single block to test for the
different event types, re-registering as necessary. If you find that
block gets too out of hand, then use the :only
option and break the
logic up between handlers.
1033 1034 1035 |
# File 'lib/zk/client/base.rb', line 1033 def register(path, opts={}, &block) event_handler.register(path, opts, &block) end |
#reopen(timeout = nil) ⇒ Symbol
reopen the underlying connection
The timeout
param is here mainly for legacy support.
130 131 |
# File 'lib/zk/client/base.rb', line 130 def reopen(timeout=nil) end |
#session_id ⇒ Fixnum
Returns the session_id of the underlying connection.
914 915 916 |
# File 'lib/zk/client/base.rb', line 914 def session_id cnx.session_id end |
#session_passwd ⇒ String
Returns the session_passwd of the underlying connection.
919 920 921 |
# File 'lib/zk/client/base.rb', line 919 def session_passwd cnx.session_passwd end |
#set(path, data, opts = {}) ⇒ Stat?
Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions). Passing the version allows you to perform optimistic locking, in that if someone changes the node's data "behind your back", your update will fail. Since #create does not return a Zookeeper::Stat object, you should be aware that nodes are created with version == 0.
This operation, if successful, will trigger all the watches on the node of the given path left by get calls.
Called with a hash of arguments set. Supports being executed asynchronousy by passing a callback object.
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/zk/client/base.rb', line 555 def set(path, data, opts={}) h = { :path => path, :data => data }.merge(opts) rv = call_and_check_rc(:set, h) logger.debug { "rv: #{rv.inspect}" } # the reason we check the :rc here is: if the user set an :ignore which # has successfully squashed an error code from turning into an exception # we want to return nil. If the user was successful, we want to return # the Stat we got back from the server # # in the case of an async request, we want to return the result code of # the async operation (the submission) if opts[:callback] rv elsif (rv[:rc] == Zookeeper::ZOK) rv[:stat] else nil end end |
#set_acl(path, acls, opts = {}) ⇒ Object
Set the ACL for the node of the given path if such a node exists and the given version matches the version of the node. Return the stat of the node.
@todo: TBA - waiting on clarification of method use
867 868 869 870 871 |
# File 'lib/zk/client/base.rb', line 867 def set_acl(path, acls, opts={}) h = { :path => path, :acl => acls }.merge(opts) rv = call_and_check_rc(:set_acl, h) opts[:callback] ? rv : rv[:stat] end |
#stat(path, opts = {}) ⇒ Zookeeper::Stat
Return the stat of the node of the given path. Return nil if the node doesn't exist.
If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.
Can be called with just the path, otherwise a hash with the arguments set. Supports being executed asynchronousy by passing a callback object.
619 620 621 622 623 624 625 626 |
# File 'lib/zk/client/base.rb', line 619 def stat(path, opts={}) h = { :path => path }.merge(opts) setup_watcher!(:data, h) do rv = call_and_check_rc(:stat, h.merge(:ignore => :no_node)) opts[:callback] ? rv : rv.fetch(:stat) end end |
#watcher ⇒ Object
for backwards compatibility only
use ZK::Client::Base#event_handler instead
69 70 71 |
# File 'lib/zk/client/base.rb', line 69 def watcher event_handler end |