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

const_missing, included, new

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)


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

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.



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

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

some state methods need to be more paranoid about locking to ensure the correct state is returned



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

def self.threadsafe_inquisitor(*syms)
  syms.each do |sym|
    class_eval(<<-EOM, __FILE__, __LINE__+1)
      def #{sym}
        false|@mutex.synchronize { @czk and @czk.#{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



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

def assert_open
  @mutex.synchronize do
    raise Exceptions::SessionExpired if state == ZOO_EXPIRED_SESSION_STATE
    raise Exceptions::NotConnected   unless connected?
    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



141
142
143
144
145
146
147
148
149
150
# File 'ext/zookeeper_base.rb', line 141

def close
  shutdown_thread = Thread.new do
    @mutex.synchronize do
      stop_dispatch_thread!
      close!
    end
  end

  shutdown_thread.join unless event_dispatch_thread?
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



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

def close!
  @czk && @czk.close
end

#closed?Boolean

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

Returns:

  • (Boolean)


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

def closed?
  @mutex.synchronize { !@czk or @czk.closed? } 
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.



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

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

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



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

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

#session_idObject



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

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

#session_passwdObject



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

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

#set_debug_level(int) ⇒ Object



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

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)



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

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



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

def state
  return ZOO_CLOSED_STATE if closed?
  @mutex.synchronize { @czk.state }
end