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)


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

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.



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

def event_queue
  @event_queue
end

#original_pidObject

Returns the value of attribute original_pid.



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

def original_pid
  @original_pid
end

Class Method Details

.threadsafe_inquisitor(*syms) ⇒ Object



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

def self.threadsafe_inquisitor(*syms)
  syms.each do |sym|
    class_eval(<<-EOM, __FILE__, __LINE__+1)
      def #{sym}
        c = @mutex.synchronize { @czk }
        false|(c && c.#{sym})
      end
    EOM
  end
end

Instance Method Details

#assert_openObject

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



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

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

#closeObject

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



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

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



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

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

#closed?Boolean

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

Returns:

  • (Boolean)


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

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.



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

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



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

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



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

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



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

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



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

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

#session_passwdObject



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

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

#set_debug_level(int) ⇒ Object



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

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)



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

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



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

def state
  return ZOO_CLOSED_STATE if closed?
  czk.state
end