class Cassandra::Cluster::Registry

@private

Public Class Methods

new(logger) click to toggle source
   # File lib/cassandra/cluster/registry.rb
25 def initialize(logger)
26   @logger    = logger
27   @hosts     = ::Hash.new
28   @listeners = ::Array.new
29 
30   mon_initialize
31 end

Public Instance Methods

add_listener(listener) click to toggle source
   # File lib/cassandra/cluster/registry.rb
33 def add_listener(listener)
34   synchronize do
35     listeners = @listeners.dup
36     listeners.push(listener)
37     @listeners = listeners
38   end
39 
40   self
41 end
each_host(&block) click to toggle source
   # File lib/cassandra/cluster/registry.rb
53 def each_host(&block)
54   if block_given?
55     @hosts.each_value(&block)
56     self
57   else
58     @hosts.values
59   end
60 end
Also aliased as: hosts
has_host?(address) click to toggle source

@param address [IPAddr] resolved address of the node

   # File lib/cassandra/cluster/registry.rb
69 def has_host?(address)
70   @hosts.key?(address.to_s)
71 end
host(address) click to toggle source

@param address [IPAddr] resolved address of the node

   # File lib/cassandra/cluster/registry.rb
64 def host(address)
65   @hosts[address.to_s]
66 end
host_down(address) click to toggle source

@param address [IPAddr] resolved address of the node

    # File lib/cassandra/cluster/registry.rb
116 def host_down(address)
117   ip   = address.to_s
118   host = @hosts[ip]
119 
120   return self unless host && !host.down?
121 
122   host = toggle_down(host)
123 
124   synchronize do
125     hosts     = @hosts.dup
126     hosts[ip] = host
127     @hosts    = hosts
128   end
129 
130   self
131 end
host_found(address, data = {}) click to toggle source

@param address [IPAddr] resolved address of the node @param data [Hash<String, String>] attributes of the host, typically a row from system.local or system.peers.

    # File lib/cassandra/cluster/registry.rb
 75 def host_found(address, data = {})
 76   ip   = address.to_s
 77   host = @hosts[ip]
 78 
 79   if host
 80     if host.id              == data['host_id']         &&
 81        host.release_version == data['release_version'] &&
 82        host.rack            == data['rack']            &&
 83        host.datacenter      == data['data_center']     &&
 84        host.broadcast_address == data['broadcast_address'] &&
 85        host.listen_address == data['listen_address']
 86 
 87       return self if host.up?
 88 
 89       host = toggle_up(host)
 90     else
 91       @logger.debug("Host #{host.ip} metadata has been updated, it will be " \
 92           'considered lost and found')
 93 
 94       notify_lost(host)
 95 
 96       host = create_host(address, data)
 97 
 98       notify_found(host)
 99     end
100   else
101     host = create_host(address, data)
102 
103     notify_found(host)
104   end
105 
106   synchronize do
107     hosts     = @hosts.dup
108     hosts[ip] = host
109     @hosts    = hosts
110   end
111 
112   self
113 end
host_lost(address) click to toggle source

@param address [IPAddr] resolved address of the node

    # File lib/cassandra/cluster/registry.rb
152 def host_lost(address)
153   ip   = address.to_s
154   host = nil
155 
156   return self unless @hosts.key?(ip)
157 
158   synchronize do
159     hosts  = @hosts.dup
160     host   = hosts.delete(ip)
161     @hosts = hosts
162   end
163 
164   notify_lost(host)
165 
166   host
167 end
host_up(address) click to toggle source

@param address [IPAddr] resolved address of the node

    # File lib/cassandra/cluster/registry.rb
134 def host_up(address)
135   ip   = address.to_s
136   host = @hosts[ip]
137 
138   return self unless host && !host.up?
139 
140   host = toggle_up(host)
141 
142   synchronize do
143     hosts     = @hosts.dup
144     hosts[ip] = host
145     @hosts    = hosts
146   end
147 
148   self
149 end
hosts(&block)
Alias for: each_host
remove_listener(listener) click to toggle source
   # File lib/cassandra/cluster/registry.rb
43 def remove_listener(listener)
44   synchronize do
45     listeners = @listeners.dup
46     listeners.delete(listener)
47     @listeners = listeners
48   end
49 
50   self
51 end

Private Instance Methods

create_host(ip, data) click to toggle source

@param ip [String] resolved address of the node @param data [Hash<String, String>] attributes of the host, typically a row from system.local or system.peers.

    # File lib/cassandra/cluster/registry.rb
173 def create_host(ip, data)
174   Host.new(ip,
175            data['host_id'],
176            data['rack'],
177            data['data_center'],
178            data['release_version'],
179            Array(data['tokens']).freeze,
180            :up,
181            data['broadcast_address'],
182            data['listen_address'])
183 end
notify_found(host) click to toggle source

@param host [Host] host that is found.

    # File lib/cassandra/cluster/registry.rb
267 def notify_found(host)
268   @logger.debug("Host #{host.ip} is found and up")
269   @listeners.each do |listener|
270     begin
271       listener.host_found(host)
272     rescue
273       nil
274     end
275     begin
276       listener.host_up(host)
277     rescue
278       nil
279     end
280   end
281 end
notify_lost(host) click to toggle source

@param host [Host] host that is lost.

    # File lib/cassandra/cluster/registry.rb
230 def notify_lost(host)
231   if host.up?
232     @logger.debug("Host #{host.ip} is down and lost")
233     host = Host.new(host.ip,
234                     host.id,
235                     host.rack,
236                     host.datacenter,
237                     host.release_version,
238                     host.tokens,
239                     :down,
240                     host.broadcast_address,
241                     host.listen_address)
242     @listeners.reverse_each do |listener|
243       begin
244         listener.host_down(host)
245       rescue
246         nil
247       end
248       begin
249         listener.host_lost(host)
250       rescue
251         nil
252       end
253     end
254   else
255     @logger.debug("Host #{host.ip} is lost")
256     @listeners.reverse_each do |listener|
257       begin
258         listener.host_lost(host)
259       rescue
260         nil
261       end
262     end
263   end
264 end
toggle_down(host) click to toggle source

@param host [Host] host that is found to be down.

    # File lib/cassandra/cluster/registry.rb
208 def toggle_down(host)
209   host = Host.new(host.ip,
210                   host.id,
211                   host.rack,
212                   host.datacenter,
213                   host.release_version,
214                   host.tokens,
215                   :down,
216                   host.broadcast_address,
217                   host.listen_address)
218   @logger.debug("Host #{host.ip} is down")
219   @listeners.reverse_each do |listener|
220     begin
221       listener.host_down(host)
222     rescue
223       nil
224     end
225   end
226   host
227 end
toggle_up(host) click to toggle source

@param host [Host] host that is found to be up.

    # File lib/cassandra/cluster/registry.rb
186 def toggle_up(host)
187   host = Host.new(host.ip,
188                   host.id,
189                   host.rack,
190                   host.datacenter,
191                   host.release_version,
192                   host.tokens,
193                   :up,
194                   host.broadcast_address,
195                   host.listen_address)
196   @logger.debug("Host #{host.ip} is up")
197   @listeners.each do |listener|
198     begin
199       listener.host_up(host)
200     rescue
201       nil
202     end
203   end
204   host
205 end