Class: Zookeeper::ZookeeperBase

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ACLs, Callbacks, Common, Constants, Exceptions, Forked, Logger
Defined in:
ext/zookeeper_base.rb

Direct Known Subclasses

Client

Defined Under Namespace

Classes: ClientShutdownException

Constant Summary collapse

KILL_TOKEN =
Object.new
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 Exceptions

Exceptions::ExpiredSession

Constants included from Constants

Constants::CONNECTED_EVENT_VALUES, Constants::EVENT_TYPE_NAMES, Constants::STATE_NAMES, Constants::ZAPIERROR, Constants::ZAUTHFAILED, Constants::ZBADARGUMENTS, Constants::ZBADVERSION, Constants::ZCLOSING, Constants::ZCONNECTIONLOSS, Constants::ZDATAINCONSISTENCY, Constants::ZINVALIDACL, Constants::ZINVALIDCALLBACK, Constants::ZINVALIDSTATE, Constants::ZKRB_ASYNC_CONTN_ID, Constants::ZMARSHALLINGERROR, Constants::ZNOAUTH, Constants::ZNOCHILDRENFOREPHEMERALS, Constants::ZNODEEXISTS, Constants::ZNONODE, Constants::ZNOTEMPTY, Constants::ZNOTHING, Constants::ZOK, Constants::ZOO_ASSOCIATING_STATE, Constants::ZOO_AUTH_FAILED_STATE, Constants::ZOO_CHANGED_EVENT, Constants::ZOO_CHILD_EVENT, Constants::ZOO_CLOSED_STATE, Constants::ZOO_CONNECTED_STATE, Constants::ZOO_CONNECTING_STATE, Constants::ZOO_CREATED_EVENT, Constants::ZOO_DELETED_EVENT, Constants::ZOO_EPHEMERAL, Constants::ZOO_EXPIRED_SESSION_STATE, Constants::ZOO_NOTWATCHING_EVENT, Constants::ZOO_SEQUENCE, Constants::ZOO_SESSION_EVENT, Constants::ZOPERATIONTIMEOUT, Constants::ZRUNTIMEINCONSISTENCY, Constants::ZSESSIONEXPIRED, Constants::ZSESSIONMOVED, Constants::ZSYSTEMERROR, Constants::ZUNIMPLEMENTED

Constants included from ACLs::Constants

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

included, set_default

Methods included from Exceptions

by_code, raise_on_error

Methods included from Constants

#event_by_value, #state_by_value

Methods included from Common

#event_dispatch_thread?

Methods included from Forked

#forked?, #update_pid!

Constructor Details

#initialize(host, timeout = 10, watcher = nil) {|_self| ... } ⇒ ZookeeperBase

Returns a new instance of ZookeeperBase

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/zookeeper_base.rb', line 91

def initialize(host, timeout = 10, watcher=nil)
  @watcher_reqs = {}
  @completion_reqs = {}

  @current_req_id = 0

  @dispatcher = @czk = nil

  update_pid!
  reopen_after_fork!
  
  # approximate the java behavior of raising java.lang.IllegalArgumentException if the host
  # argument ends with '/'
  raise ArgumentError, "Host argument #{host.inspect} may not end with /" if host.end_with?('/')

  @host = host.dup

  @default_watcher = (watcher or get_default_global_watcher)

  yield self if block_given?

  reopen(timeout)
end

Instance Attribute Details

#event_queueObject (readonly)

Returns the value of attribute event_queue


50
51
52
# File 'ext/zookeeper_base.rb', line 50

def event_queue
  @event_queue
end

#original_pidObject

Returns the value of attribute original_pid


17
18
19
# File 'ext/zookeeper_base.rb', line 17

def original_pid
  @original_pid
end

Class Method Details

.threadsafe_inquisitor(*syms) ⇒ Object


37
38
39
40
41
42
43
44
45
46
# File 'ext/zookeeper_base.rb', line 37

def self.threadsafe_inquisitor(*syms)
  syms.each do |sym|
    class_eval("def \#{sym}\nc = @mutex.synchronize { @czk }\nfalse|(c && c.\#{sym})\nend\n", __FILE__, __LINE__+1)
  end
end

Instance Method Details

#assert_openObject

if either of these happen, the user will need to renegotiate a connection via reopen


116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/zookeeper_base.rb', line 116

def assert_open
  @mutex.synchronize do
    raise Exceptions::NotConnected if !@czk or @czk.closed?
    if forked?
      raise InheritedConnectionError, "You tried to use a connection inherited from another process\n(original pid: \#{original_pid}, your pid: \#{Process.pid})\nYou need to call reopen() after forking\n".gsub(/(?:^|\n)\s*/, ' ').strip
    end
  end
end

#closeObject

close the connection normally, stops the dispatch thread and closes the underlying connection cleanly


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ext/zookeeper_base.rb', line 139

def close
  sd_thread = nil

  @mutex.synchronize do
    return unless @czk
    inst, @czk = @czk, nil
  
    sd_thread = Thread.new(inst) do |_inst|
      stop_dispatch_thread!
      _inst.close
    end
  end

  # if we're on the event dispatch thread for some stupid reason, then don't join
  unless event_dispatch_thread?
    # hard-coded 30 second delay, don't hang forever
    if sd_thread.join(30) != sd_thread 
      logger.error { "timed out waiting for shutdown thread to exit" }
    end
  end

  nil
end

#close!Object

do not lock, do not mutex, just close the underlying handle this is potentially dangerous and should only be called after a fork() to close this instance


132
133
134
135
# File 'ext/zookeeper_base.rb', line 132

def close!
  inst, @czk = @czk, nil
  inst && inst.close
end

#closed?Boolean

we are closed if there is no @czk instance or @czk.closed?


204
205
206
207
208
# File 'ext/zookeeper_base.rb', line 204

def closed?
  czk.closed?
rescue Exceptions::NotConnected
  true
end

#create(*args) ⇒ Object

the C lib doesn't strip the chroot path off of returned path values, which is pretty damn annoying. this is used to clean things up.


165
166
167
168
169
# File 'ext/zookeeper_base.rb', line 165

def create(*args)
  # since we don't care about the inputs, just glob args
  rc, new_path = czk.create(*args)
  [rc, strip_chroot_from(new_path)]
end

#pause_before_fork_in_parentObject


210
211
212
213
214
215
216
217
218
219
220
# File 'ext/zookeeper_base.rb', line 210

def pause_before_fork_in_parent
  @mutex.synchronize do
    logger.debug { "ZookeeperBase#pause_before_fork_in_parent" }

    # XXX: add anal-retentive state checking 
    raise "EXPLODERATE! @czk was nil!" unless @czk

    @czk.pause_before_fork_in_parent
    stop_dispatch_thread!
  end
end

#reopen(timeout = 10, watcher = nil) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/zookeeper_base.rb', line 69

def reopen(timeout = 10, watcher=nil)
  if watcher and (watcher != @default_watcher)
    raise "You cannot set the watcher to a different value this way anymore!"
  end

  reopen_after_fork! if forked?

  @mutex.synchronize do
    @czk.close if @czk
    @czk = CZookeeper.new(@host, @event_queue)

    # flushes all outstanding watcher reqs.
    @watcher_reqs.clear
    set_default_global_watcher
    
    @czk.wait_until_connected(timeout)
  end

  setup_dispatch_thread!
  state
end

#resume_after_fork_in_parentObject


222
223
224
225
226
227
228
229
230
231
232
# File 'ext/zookeeper_base.rb', line 222

def resume_after_fork_in_parent
  @mutex.synchronize do
    logger.debug { "ZookeeperBase#resume_after_fork_in_parent" }

    raise "EXPLODERATE! @czk was nil!" unless @czk

    event_queue.open
    setup_dispatch_thread!
    @czk.resume_after_fork_in_parent
  end
end

#session_idObject


191
192
193
194
195
# File 'ext/zookeeper_base.rb', line 191

def session_id
  @mutex.synchronize do
    cid = client_id and cid.session_id
  end
end

#session_passwdObject


197
198
199
200
201
# File 'ext/zookeeper_base.rb', line 197

def session_passwd
  @mutex.synchronize do
    cid = client_id and cid.passwd
  end
end

#set_debug_level(int) ⇒ Object


171
172
173
174
# File 'ext/zookeeper_base.rb', line 171

def set_debug_level(int)
  warn "DEPRECATION WARNING: #{self.class.name}#set_debug_level, it has moved to the class level and will be removed in a future release"
  self.class.set_debug_level(int)
end

#set_default_global_watcherObject

set the watcher object/proc that will receive all global events (such as session/state events)


177
178
179
180
181
182
183
184
# File 'ext/zookeeper_base.rb', line 177

def set_default_global_watcher
  warn "DEPRECATION WARNING: #{self.class}#set_default_global_watcher ignores block" if block_given?

  @mutex.synchronize do
#       @default_watcher = block # save this here for reopen() to use
    @watcher_reqs[ZKRB_GLOBAL_CB_REQ] = { :watcher => @default_watcher, :watcher_context => nil }
  end
end

#stateObject


186
187
188
189
# File 'ext/zookeeper_base.rb', line 186

def state
  return ZOO_CLOSED_STATE if closed?
  czk.state
end