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
Constant Summary
Instance Attribute Summary (collapse)
-
- (Object) event_handler
readonly
The Eventhandler is used by client code to register callbacks to handle events triggered for given paths.
Instance Method Summary (collapse)
-
- (Object) children(path, opts = {})
Return the list of the children of the node of the given path.
-
- (Object) close
close the underlying connection, but do not reset callbacks registered via the
registermethod. -
- (Object) close!
close the underlying connection and clear all pending events.
-
- (Boolean) closed?
returns true if the connection has been closed.
-
- (Object) connect(opts = {})
Connect to the server/cluster.
-
- (String?) create(path, *args)
Create a node with the given path.
-
- (Object) delete(path, opts = {})
Delete the node with the given path.
-
- (Boolean) event_dispatch_thread?
returns true if the caller is calling from the event dispatch thread.
-
- (Boolean) exists?(path, opts = {})
sugar around stat.
-
- (Array) get(path, opts = {})
Return the data and stat of the node of the given path.
-
- (Object) get_acl(path, opts = {})
Return the ACL and stat of the node of the given path.
-
- (Base) initialize(host, opts = {})
constructor
abstract
Create a new client and connect to the zookeeper server.
-
- (EventHandlerSubscription) register(path, opts = {}, &block) {|event| ... }
Register a block that should be delivered events for a given path.
-
- (Symbol) reopen(timeout = nil)
reopen the underlying connection.
-
- (Fixnum) session_id
The session_id of the underlying connection.
-
- (String) session_passwd
The session_passwd of the underlying connection.
-
- (Stat?) set(path, data, opts = {})
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).
-
- (Object) set_acl(path, acls, opts = {})
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.
-
- (Zookeeper::Stat) stat(path, opts = {})
Return the stat of the node of the given path.
-
- (Object) watcher
deprecated
Deprecated.
for backwards compatibility only
Constructor Details
- (Base) initialize(host, opts = {})
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
- (Object) event_handler (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
- (Object) children(path, opts = {})
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 |
- (Object) close
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 |
- (Object) close!
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 |
- (Boolean) closed?
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 |
- (Object) connect(opts = {})
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 |
- (String?) create(path, opts = {}) - (String?) create(path, data, opts = {})
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 creaed 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 |
- (Object) delete(path, opts = {})
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 |
- (Boolean) event_dispatch_thread?
returns true if the caller is calling from the event dispatch thread
1023 1024 1025 |
# File 'lib/zk/client/base.rb', line 1023 def event_dispatch_thread? cnx.event_dispatch_thread? end |
- (Boolean) exists?(path, opts = {})
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 |
- (Array) get(path, opts = {})
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 |
- (Object) get_acl(path, opts = {})
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 |
- (EventHandlerSubscription) register(path, interests = nil, &block) - (EventHandlerSubscription) register(path, opts = {}, &block)
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.
1018 1019 1020 |
# File 'lib/zk/client/base.rb', line 1018 def register(path, opts={}, &block) event_handler.register(path, opts, &block) end |
- (Symbol) reopen(timeout = nil)
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 |
- (Fixnum) session_id
The session_id of the underlying connection
899 900 901 |
# File 'lib/zk/client/base.rb', line 899 def session_id cnx.session_id end |
- (String) session_passwd
The session_passwd of the underlying connection
904 905 906 |
# File 'lib/zk/client/base.rb', line 904 def session_passwd cnx.session_passwd end |
- (Stat?) set(path, data, opts = {})
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 |
- (Object) set_acl(path, acls, opts = {})
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 |
- (Zookeeper::Stat) stat(path, opts = {})
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 |
- (Object) watcher
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 |