Class: Zookeeper

Inherits:
CZookeeper show all
Includes:
ZookeeperACLs, ZookeeperCallbacks, ZookeeperConstants, ZookeeperExceptions, ZookeeperStat
Defined in:
lib/zookeeper.rb

Constant Summary collapse

ZKRB_GLOBAL_CB_REQ =
-1
ZOO_LOG_LEVEL_ERROR =

debug levels

1
ZOO_LOG_LEVEL_WARN =
2
ZOO_LOG_LEVEL_INFO =
3
ZOO_LOG_LEVEL_DEBUG =
4

Constants included from ZookeeperACLs

ZookeeperACLs::ZOO_ANYONE_ID_UNSAFE, ZookeeperACLs::ZOO_AUTH_IDS, ZookeeperACLs::ZOO_CREATOR_ALL_ACL, ZookeeperACLs::ZOO_OPEN_ACL_UNSAFE, ZookeeperACLs::ZOO_PERM_ADMIN, ZookeeperACLs::ZOO_PERM_ALL, ZookeeperACLs::ZOO_PERM_CREATE, ZookeeperACLs::ZOO_PERM_DELETE, ZookeeperACLs::ZOO_PERM_READ, ZookeeperACLs::ZOO_PERM_WRITE, ZookeeperACLs::ZOO_READ_ACL_UNSAFE

Constants included from ZookeeperExceptions

ZookeeperExceptions::ZAPIERROR, ZookeeperExceptions::ZAUTHFAILED, ZookeeperExceptions::ZBADARGUMENTS, ZookeeperExceptions::ZBADVERSION, ZookeeperExceptions::ZCLOSING, ZookeeperExceptions::ZCONNECTIONLOSS, ZookeeperExceptions::ZDATAINCONSISTENCY, ZookeeperExceptions::ZINVALIDACL, ZookeeperExceptions::ZINVALIDCALLBACK, ZookeeperExceptions::ZINVALIDSTATE, ZookeeperExceptions::ZMARSHALLINGERROR, ZookeeperExceptions::ZNOAUTH, ZookeeperExceptions::ZNOCHILDRENFOREPHEMERALS, ZookeeperExceptions::ZNODEEXISTS, ZookeeperExceptions::ZNONODE, ZookeeperExceptions::ZNOTEMPTY, ZookeeperExceptions::ZNOTHING, ZookeeperExceptions::ZOK, ZookeeperExceptions::ZOPERATIONTIMEOUT, ZookeeperExceptions::ZRUNTIMEINCONSISTENCY, ZookeeperExceptions::ZSESSIONEXPIRED, ZookeeperExceptions::ZSESSIONMOVED, ZookeeperExceptions::ZSYSTEMERROR, ZookeeperExceptions::ZUNIMPLEMENTED

Constants included from ZookeeperConstants

ZookeeperConstants::ZOO_ASSOCIATING_STATE, ZookeeperConstants::ZOO_AUTH_FAILED_STATE, ZookeeperConstants::ZOO_CHANGED_EVENT, ZookeeperConstants::ZOO_CHILD_EVENT, ZookeeperConstants::ZOO_CONNECTED_STATE, ZookeeperConstants::ZOO_CONNECTING_STATE, ZookeeperConstants::ZOO_CREATED_EVENT, ZookeeperConstants::ZOO_DELETED_EVENT, ZookeeperConstants::ZOO_EPHEMERAL, ZookeeperConstants::ZOO_EXPIRED_SESSION_STATE, ZookeeperConstants::ZOO_NOTWATCHING_EVENT, ZookeeperConstants::ZOO_SEQUENCE, ZookeeperConstants::ZOO_SESSION_EVENT

Instance Method Summary collapse

Methods included from ZookeeperConstants

#event_by_value, #print_events, #print_states, #state_by_value

Constructor Details

#initialize(host, timeout = 10) ⇒ Zookeeper

Returns a new instance of Zookeeper.



41
42
43
44
45
46
47
48
# File 'lib/zookeeper.rb', line 41

def initialize(host, timeout = 10)
  @watcher_reqs = {}
  @completion_reqs = {}
  @req_mutex = Mutex.new
  @current_req_id = 1
  @host = host
  return nil if reopen(timeout) != Zookeeper::ZOO_CONNECTED_STATE
end

Instance Method Details

#closeObject

To close a Zk handle, first shutdown the dispatcher thread; this is done by signalling the waiting thread that there is a pending close. We then release the C-land Zk state.



157
158
159
160
161
# File 'lib/zookeeper.rb', line 157

def close
  signal_pending_close
  @dispatcher.join
  super
end

#create(options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/zookeeper.rb', line 100

def create(options = {})
  assert_open
  assert_supported_keys(options, [:path, :data, :acl, :ephemeral, :sequence, :callback, :callback_context])
  assert_required_keys(options, [:path])

  flags = 0
  flags |= ZOO_EPHEMERAL if options[:ephemeral]
  flags |= ZOO_SEQUENCE if options[:sequence]

  options[:acl] ||= ZOO_OPEN_ACL_UNSAFE

  req_id = setup_call(options)
  rc, newpath = super(req_id, options[:path], options[:data], options[:callback], options[:acl], flags)

  rv = { :req_id => req_id, :rc => rc }
  options[:callback] ? rv : rv.merge(:path => newpath)
end

#delete(options = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/zookeeper.rb', line 118

def delete(options = {})
  assert_open
  assert_supported_keys(options, [:path, :version, :callback, :callback_context])
  assert_required_keys(options, [:path])
  options[:version] ||= -1

  req_id = setup_call(options)
  rc = super(req_id, options[:path], options[:version], options[:callback])

  { :req_id => req_id, :rc => rc }
end

#get(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/zookeeper.rb', line 51

def get(options = {})
  assert_open
  assert_supported_keys(options, [:path, :watcher, :watcher_context, :callback, :callback_context])
  assert_required_keys(options, [:path])

  req_id = setup_call(options)
  rc, value, stat = super(req_id, options[:path], options[:callback], options[:watcher])

  rv = { :req_id => req_id, :rc => rc }
  options[:callback] ? rv : rv.merge(:data => value, :stat => Stat.new(stat))
end

#get_acl(options = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/zookeeper.rb', line 142

def get_acl(options = {})
  assert_open
  assert_supported_keys(options, [:path, :callback, :callback_context])
  assert_required_keys(options, [:path])

  req_id = setup_call(options)
  rc, acls, stat = super(req_id, options[:path], options[:callback])

  rv = { :req_id => req_id, :rc => rc }
  options[:callback] ? rv : rv.merge(:acl => acls, :stat => Stat.new(stat))
end

#get_children(options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/zookeeper.rb', line 76

def get_children(options = {})
  assert_open
  assert_supported_keys(options, [:path, :callback, :callback_context, :watcher, :watcher_context])
  assert_required_keys(options, [:path])

  req_id = setup_call(options)
  rc, children, stat = super(req_id, options[:path], options[:callback], options[:watcher])

  rv = { :req_id => req_id, :rc => rc }
  options[:callback] ? rv : rv.merge(:children => children, :stat => Stat.new(stat))
end

#reopen(timeout = 10) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/zookeeper.rb', line 26

def reopen(timeout = 10)
  init(@host)
  if timeout > 0
    time_to_stop = Time.now + timeout
    until state == Zookeeper::ZOO_CONNECTED_STATE
      break if Time.now > time_to_stop
      sleep 0.1
    end
  end
  # flushes all outstanding watcher reqs.
  @watcher_reqs = { ZKRB_GLOBAL_CB_REQ => { :watcher => get_default_global_watcher } }
  setup_dispatch_thread!
  state
end

#set(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zookeeper.rb', line 63

def set(options = {})
  assert_open
  assert_supported_keys(options, [:path, :data, :version, :callback, :callback_context])
  assert_required_keys(options, [:path])
  options[:version] ||= -1

  req_id = setup_call(options)
  rc, stat = super(req_id, options[:path], options[:data], options[:callback], options[:version])

  rv = { :req_id => req_id, :rc => rc }
  options[:callback] ? rv : rv.merge(:stat => Stat.new(stat))
end

#set_acl(options = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/zookeeper.rb', line 130

def set_acl(options = {})
  assert_open
  assert_supported_keys(options, [:path, :acl, :version, :callback, :callback_context])
  assert_required_keys(options, [:path, :acl])
  options[:version] ||= -1

  req_id = setup_call(options)
  rc = super(req_id, options[:path], options[:acl], options[:callback], options[:version])

  { :req_id => req_id, :rc => rc }
end

#stat(options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/zookeeper.rb', line 88

def stat(options = {})
  assert_open
  assert_supported_keys(options, [:path, :callback, :callback_context, :watcher, :watcher_context])
  assert_required_keys(options, [:path])

  req_id = setup_call(options)
  rc, stat = exists(req_id, options[:path], options[:callback], options[:watcher])

  rv = { :req_id => req_id, :rc => rc }
  options[:callback] ? rv : rv.merge(:stat => Stat.new(stat))
end

#unregister_watcher(req_id) ⇒ Object

TODO: Sanitize user mistakes by unregistering watchers from ops that don’t return ZOK (except wexists)? Make users clean up after themselves for now.



226
227
228
229
230
# File 'lib/zookeeper.rb', line 226

def unregister_watcher(req_id)
  @req_mutex.synchronize {
    @watcher_reqs.delete(req_id)
  }
end