Class: Zookeeper::CZookeeper

Inherits:
Object
  • Object
show all
Includes:
Constants, Exceptions, Forked
Defined in:
ext/c_zookeeper.rb,
ext/zookeeper_c.c

Defined Under Namespace

Classes: ClientId, GotNilEventException

Constant Summary collapse

DEFAULT_SESSION_TIMEOUT_MSEC =
10000

Constants included from Exceptions

Exceptions::ExpiredSession

Constants included from Constants

Zookeeper::Constants::CONNECTED_EVENT_VALUES, Zookeeper::Constants::EVENT_TYPE_NAMES, Zookeeper::Constants::STATE_NAMES, Zookeeper::Constants::ZAPIERROR, Zookeeper::Constants::ZAUTHFAILED, Zookeeper::Constants::ZBADARGUMENTS, Zookeeper::Constants::ZBADVERSION, Zookeeper::Constants::ZCLOSING, Zookeeper::Constants::ZCONNECTIONLOSS, Zookeeper::Constants::ZDATAINCONSISTENCY, Zookeeper::Constants::ZINVALIDACL, Zookeeper::Constants::ZINVALIDCALLBACK, Zookeeper::Constants::ZINVALIDSTATE, Zookeeper::Constants::ZKRB_GLOBAL_CB_REQ, Zookeeper::Constants::ZMARSHALLINGERROR, Zookeeper::Constants::ZNOAUTH, Zookeeper::Constants::ZNOCHILDRENFOREPHEMERALS, Zookeeper::Constants::ZNODEEXISTS, Zookeeper::Constants::ZNONODE, Zookeeper::Constants::ZNOTEMPTY, Zookeeper::Constants::ZNOTHING, Zookeeper::Constants::ZOK, Zookeeper::Constants::ZOO_ASSOCIATING_STATE, Zookeeper::Constants::ZOO_AUTH_FAILED_STATE, Zookeeper::Constants::ZOO_CHANGED_EVENT, Zookeeper::Constants::ZOO_CHILD_EVENT, Zookeeper::Constants::ZOO_CLOSED_STATE, Zookeeper::Constants::ZOO_CONNECTED_STATE, Zookeeper::Constants::ZOO_CONNECTING_STATE, Zookeeper::Constants::ZOO_CREATED_EVENT, Zookeeper::Constants::ZOO_DELETED_EVENT, Zookeeper::Constants::ZOO_EPHEMERAL, Zookeeper::Constants::ZOO_EXPIRED_SESSION_STATE, Zookeeper::Constants::ZOO_LOG_LEVEL_DEBUG, Zookeeper::Constants::ZOO_LOG_LEVEL_ERROR, Zookeeper::Constants::ZOO_LOG_LEVEL_INFO, Zookeeper::Constants::ZOO_LOG_LEVEL_WARN, Zookeeper::Constants::ZOO_NOTWATCHING_EVENT, Zookeeper::Constants::ZOO_SEQUENCE, Zookeeper::Constants::ZOO_SESSION_EVENT, Zookeeper::Constants::ZOPERATIONTIMEOUT, Zookeeper::Constants::ZRUNTIMEINCONSISTENCY, Zookeeper::Constants::ZSESSIONEXPIRED, Zookeeper::Constants::ZSESSIONMOVED, Zookeeper::Constants::ZSYSTEMERROR, Zookeeper::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 Exceptions

by_code, raise_on_error

Methods included from Constants

#event_by_value, #state_by_value

Methods included from Forked

#forked?, #update_pid!

Constructor Details

#initialize(host, event_queue, opts = {}) ⇒ CZookeeper

Returns a new instance of CZookeeper.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/c_zookeeper.rb', line 31

def initialize(host, event_queue, opts={})
  @host = host
  @event_queue = event_queue

  # keep track of the pid that created us
  update_pid!
  
  # used by the C layer. CZookeeper sets this to true when the init method
  # has completed. once this is set to true, it stays true.
  #
  # you should grab the @start_stop_mutex before messing with this flag
  @_running = nil

  # This is set to true after destroy_zkrb_instance has been called and all
  # CZookeeper state has been cleaned up
  @_closed = false  # also used by the C layer

  # set by the ruby side to indicate we are in shutdown mode used by method_get_next_event
  @_shutting_down = false

  # the actual C data is stashed in this ivar. never *ever* touch this
  @_data = nil

  @_session_timeout_msec = DEFAULT_SESSION_TIMEOUT_MSEC

  @start_stop_mutex = Monitor.new
  
  # used to signal that we're running
  @running_cond = @start_stop_mutex.new_cond

  # used to signal we've received the connected event
  @connected_cond = @start_stop_mutex.new_cond
  
  @event_thread = nil

  setup_event_thread!

  zkrb_init(@host)

  logger.debug { "init returned!" }
end

Instance Attribute Details

#original_pidObject

Returns the value of attribute original_pid.



19
20
21
# File 'ext/c_zookeeper.rb', line 19

def original_pid
  @original_pid
end

Class Method Details

.get_debug_levelObject

assume we’re at debug level



22
23
24
# File 'ext/c_zookeeper.rb', line 22

def self.get_debug_level
  @debug_level ||= ZOO_LOG_LEVEL_INFO
end

.set_debug_level(value) ⇒ Object



26
27
28
29
# File 'ext/c_zookeeper.rb', line 26

def self.set_debug_level(value)
  @debug_level = value
  set_zkrb_debug_level(value)
end

Instance Method Details

#associating?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'ext/c_zookeeper.rb', line 93

def associating?
  state == ZOO_ASSOCIATING_STATE
end

#closeObject



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

def close
  return if closed?

  fn_close = proc do
    if !@_closed and @_data
      logger.debug { "CALLING CLOSE HANDLE!!" }
      close_handle
    end
  end

  if forked?
    fn_close.call
  else
    shut_down!
    stop_event_thread!
    @start_stop_mutex.synchronize(&fn_close)
  end

  nil
end

#closed?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'ext/c_zookeeper.rb', line 73

def closed?
  @start_stop_mutex.synchronize { !!@_closed }
end

#connected?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'ext/c_zookeeper.rb', line 85

def connected?
  state == ZOO_CONNECTED_STATE
end

#connecting?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'ext/c_zookeeper.rb', line 89

def connecting?
  state == ZOO_CONNECTING_STATE
end

#running?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'ext/c_zookeeper.rb', line 77

def running?
  @start_stop_mutex.synchronize { !!@_running }
end

#shutting_down?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'ext/c_zookeeper.rb', line 81

def shutting_down?
  @start_stop_mutex.synchronize { !!@_shutting_down }
end

#stateObject



118
119
120
121
# File 'ext/c_zookeeper.rb', line 118

def state
  return ZOO_CLOSED_STATE if closed?
  zkrb_state
end

#wait_until_connected(timeout = 10) ⇒ Object

this implementation is gross, but i don’t really see another way of doing it without more grossness

returns true if we’re connected, false if we’re not

if timeout is nil, we never time out, and wait forever for CONNECTED state



130
131
132
133
134
135
136
137
# File 'ext/c_zookeeper.rb', line 130

def wait_until_connected(timeout=10)
  @start_stop_mutex.synchronize do
    wait_until_running(timeout)
    @connected_cond.wait(timeout) unless connected?
  end

  connected?
end