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

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::ZKRB_GLOBAL_CB_REQ, 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, wrapped_logger, wrapped_logger=

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)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/zookeeper_base.rb', line 82

def initialize(host, timeout = 10, watcher=nil)
  # 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

  watcher ||= get_default_global_watcher

  @req_registry = RequestRegistry.new(watcher, :chroot_path => chroot_path)

  @dispatcher = @czk = nil

  update_pid!
  reopen_after_fork!
  
  yield self if block_given?

  reopen(timeout)
end

Instance Attribute Details

#event_queueObject (readonly)

Returns the value of attribute event_queue.



45
46
47
# File 'ext/zookeeper_base.rb', line 45

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



32
33
34
35
36
37
38
39
40
41
# File 'ext/zookeeper_base.rb', line 32

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



104
105
106
107
108
109
110
111
112
113
114
115
# File 'ext/zookeeper_base.rb', line 104

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/zookeeper_base.rb', line 127

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



120
121
122
123
# File 'ext/zookeeper_base.rb', line 120

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)


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

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.



153
154
155
156
157
# File 'ext/zookeeper_base.rb', line 153

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

#pause_before_fork_in_parentObject



193
194
195
196
197
198
199
200
201
202
203
# File 'ext/zookeeper_base.rb', line 193

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'ext/zookeeper_base.rb', line 63

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

  reopen_after_fork! if forked?

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

    # flushes all outstanding watcher reqs.
    @req_registry.clear_watchers!
    
    @czk.wait_until_connected(timeout)
  end

  setup_dispatch_thread!
  state
end

#resume_after_fork_in_parentObject



205
206
207
208
209
210
211
212
213
214
215
# File 'ext/zookeeper_base.rb', line 205

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



174
175
176
177
178
# File 'ext/zookeeper_base.rb', line 174

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

#session_passwdObject



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

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

#set_debug_level(int) ⇒ Object



159
160
161
162
# File 'ext/zookeeper_base.rb', line 159

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)



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

def set_default_global_watcher
  raise "NO! YOU CANNOT HAZ set_default_global_watcher"
end

#stateObject



169
170
171
172
# File 'ext/zookeeper_base.rb', line 169

def state
  return ZOO_CLOSED_STATE if closed?
  czk.state
end