Class: Aerospike::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/aerospike/cluster.rb,
lib/aerospike/cluster/find_node.rb,
lib/aerospike/cluster/create_connection.rb,
lib/aerospike/cluster/find_nodes_to_remove.rb

Defined Under Namespace

Modules: CreateConnection, FindNode, FindNodesToRemove

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy, hosts) ⇒ Cluster

Returns a new instance of Cluster.



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
# File 'lib/aerospike/cluster.rb', line 32

def initialize(policy, hosts)
  @cluster_seeds = hosts
  @fail_if_not_connected = policy.fail_if_not_connected
  @connection_queue_size = policy.connection_queue_size
  @connection_timeout = policy.timeout
  @tend_interval = policy.tend_interval
  @cluster_name = policy.cluster_name
  @tls_options = policy.tls

  @aliases = {}
  @cluster_nodes = []
  @partition_write_map = {}
  @node_index = Atomic.new(0)
  @features = Atomic.new(Set.new)
  @closed = Atomic.new(true)
  @mutex = Mutex.new
  @cluster_config_change_listeners = Atomic.new([])

  @old_node_count = 0

  # setup auth info for cluster
  if policy.requires_authentication
    @user = policy.user
    @password = AdminCommand.hash_password(policy.password)
  end

  initialize_tls_host_names(hosts) if tls_enabled?
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



29
30
31
# File 'lib/aerospike/cluster.rb', line 29

def aliases
  @aliases
end

#cluster_idObject (readonly)

Returns the value of attribute cluster_id.



29
30
31
# File 'lib/aerospike/cluster.rb', line 29

def cluster_id
  @cluster_id
end

#cluster_nameObject (readonly)

Returns the value of attribute cluster_name.



30
31
32
# File 'lib/aerospike/cluster.rb', line 30

def cluster_name
  @cluster_name
end

#connection_queue_sizeObject (readonly)

Returns the value of attribute connection_queue_size.



27
28
29
# File 'lib/aerospike/cluster.rb', line 27

def connection_queue_size
  @connection_queue_size
end

#connection_timeoutObject (readonly)

Returns the value of attribute connection_timeout.



27
28
29
# File 'lib/aerospike/cluster.rb', line 27

def connection_timeout
  @connection_timeout
end

#featuresObject (readonly)

Returns the value of attribute features.



28
29
30
# File 'lib/aerospike/cluster.rb', line 28

def features
  @features
end

#passwordObject (readonly)

Returns the value of attribute password.



27
28
29
# File 'lib/aerospike/cluster.rb', line 27

def password
  @password
end

#tls_optionsObject (readonly)

Returns the value of attribute tls_options.



28
29
30
# File 'lib/aerospike/cluster.rb', line 28

def tls_options
  @tls_options
end

#userObject (readonly)

Returns the value of attribute user.



27
28
29
# File 'lib/aerospike/cluster.rb', line 27

def user
  @user
end

Instance Method Details

#add_alias(host, node) ⇒ Object



413
414
415
416
417
418
419
# File 'lib/aerospike/cluster.rb', line 413

def add_alias(host, node)
  if host && node
    @mutex.synchronize do
      @aliases[host] = node
    end
  end
end

#add_aliases(node) ⇒ Object



459
460
461
462
463
464
465
# File 'lib/aerospike/cluster.rb', line 459

def add_aliases(node)
  # Add node's aliases to global alias set.
  # Aliases are only used in tend thread, so synchronization is not necessary.
  node.aliases.each do |aliass|
    @aliases[aliass] = node
  end
end

#add_cluster_config_change_listener(listener) ⇒ Object



199
200
201
202
203
# File 'lib/aerospike/cluster.rb', line 199

def add_cluster_config_change_listener(listener)
  @cluster_config_change_listeners.update do |listeners|
    listeners.push(listener)
  end
end

#add_nodes(nodes_to_add) ⇒ Object



450
451
452
453
454
455
456
457
# File 'lib/aerospike/cluster.rb', line 450

def add_nodes(nodes_to_add)
  # Add all nodes at once to avoid copying entire array multiple times.
  nodes_to_add.each do |node|
    add_aliases(node)
  end

  add_nodes_copy(nodes_to_add)
end

#add_nodes_copy(nodes_to_add) ⇒ Object



467
468
469
470
471
# File 'lib/aerospike/cluster.rb', line 467

def add_nodes_copy(nodes_to_add)
  @mutex.synchronize do
    @cluster_nodes.concat(nodes_to_add)
  end
end

#add_seeds(hosts) ⇒ Object



87
88
89
90
91
# File 'lib/aerospike/cluster.rb', line 87

def add_seeds(hosts)
  @mutex.synchronize do
    @cluster_seeds.concat(hosts)
  end
end

#change_password(user, password) ⇒ Object



194
195
196
197
# File 'lib/aerospike/cluster.rb', line 194

def change_password(user, password)
  # change password ONLY if the user is the same
  @password = password if @user == user
end

#closeObject

Closes all cached connections to the cluster nodes and stops the tend thread



155
156
157
158
159
160
161
162
# File 'lib/aerospike/cluster.rb', line 155

def close
  return if @closed.value
  # send close signal to maintenance channel
  @closed.value = true
  @tend_thread.kill

  nodes.each(&:close)
end

#connectObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aerospike/cluster.rb', line 61

def connect
  wait_till_stablized

  if @fail_if_not_connected && !connected?
    raise Aerospike::Exceptions::Aerospike, Aerospike::ResultCode::SERVER_NOT_AVAILABLE
  end

  launch_tend_thread

  Aerospike.logger.info('New cluster initialized and ready to be used...')
end

#connected?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/aerospike/cluster.rb', line 99

def connected?
  # Must copy array reference for copy on write semantics to work.
  node_array = nodes
  (node_array.length > 0) && !@closed.value
end

#create_connection(host) ⇒ Object



433
434
435
# File 'lib/aerospike/cluster.rb', line 433

def create_connection(host)
  ::Aerospike::Cluster::CreateConnection.(self, host)
end

#create_node(nv) ⇒ Object



429
430
431
# File 'lib/aerospike/cluster.rb', line 429

def create_node(nv)
  ::Aerospike::Node.new(self, nv)
end

#credentials_given?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/aerospike/cluster.rb', line 73

def credentials_given?
  !(@user.nil? || @user.empty?)
end

#find_alias(aliass) ⇒ Object



164
165
166
167
168
# File 'lib/aerospike/cluster.rb', line 164

def find_alias(aliass)
  @mutex.synchronize do
    @aliases[aliass]
  end
end

#find_node_by_name(node_name) ⇒ Object



535
536
537
# File 'lib/aerospike/cluster.rb', line 535

def find_node_by_name(node_name)
  nodes.detect{|node| node.name == node_name }
end

#find_node_in_partition_map(filter) ⇒ Object



441
442
443
444
445
446
447
448
# File 'lib/aerospike/cluster.rb', line 441

def find_node_in_partition_map(filter)
  partitions_list = partitions

  partitions_list.values.each do |node_array|
    return true if node_array.value.any? { |node| node == filter }
  end
  false
end

#find_node_name(list, name) ⇒ Object

Finds a node by name in a list of nodes



409
410
411
# File 'lib/aerospike/cluster.rb', line 409

def find_node_name(list, name)
  list.any? { |node| node.name == name }
end

#find_nodes_to_remove(refresh_count) ⇒ Object



437
438
439
# File 'lib/aerospike/cluster.rb', line 437

def find_nodes_to_remove(refresh_count)
  FindNodesToRemove.(self, refresh_count)
end

#get_node_by_name(node_name) ⇒ Object

Find a node by name and returns an error if not found



146
147
148
149
150
151
152
# File 'lib/aerospike/cluster.rb', line 146

def get_node_by_name(node_name)
  node = find_node_by_name(node_name)

  raise Aerospike::Exceptions::InvalidNode unless node

  node
end

#get_node_for_key(key) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/aerospike/cluster.rb', line 105

def get_node_for_key(key)
  partition = Partition.new_by_key(key)

  # Must copy hashmap reference for copy on write semantics to work.
  nmap = partitions
  if node_array = nmap[partition.namespace]
    node = node_array.value[partition.partition_id]

    return node if node && node.active?
  end

  random_node
end

#initialize_tls_host_names(hosts) ⇒ Object



81
82
83
84
85
# File 'lib/aerospike/cluster.rb', line 81

def initialize_tls_host_names(hosts)
  hosts.each do |host|
    host.tls_name ||= cluster_id.nil? ? host.name : cluster_id
  end
end

#inspectObject



211
212
213
# File 'lib/aerospike/cluster.rb', line 211

def inspect
  "#<Aerospike::Cluster @cluster_nodes=#{@cluster_nodes}>"
end

#launch_tend_threadObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/aerospike/cluster.rb', line 215

def launch_tend_thread
  @tend_thread = Thread.new do
    Thread.current.abort_on_exception = false
    loop do
      begin
        tend
        sleep(@tend_interval / 1000.0)
      rescue => e
        Aerospike.logger.error("Exception occured during tend: #{e}")
        Aerospike.logger.debug { e.backtrace.join("\n") }
      end
    end
  end
end

#log_tend_stats(nodes) ⇒ Object



302
303
304
305
306
307
# File 'lib/aerospike/cluster.rb', line 302

def log_tend_stats(nodes)
  diff = nodes.size - @old_node_count
  action = "#{diff.abs} #{diff.abs == 1 ? "node has" : "nodes have"} #{diff > 0 ? "joined" : "left"} the cluster."
  Aerospike.logger.info("Tend finished. #{action} Old node count: #{@old_node_count}, New node count: #{nodes.size}")
  @old_node_count = nodes.size
end

#node_exists(search, node_list) ⇒ Object



531
532
533
# File 'lib/aerospike/cluster.rb', line 531

def node_exists(search, node_list)
  node_list.any? {|node| node == search }
end

#nodesObject

Returns a list of all nodes in the cluster



138
139
140
141
142
143
# File 'lib/aerospike/cluster.rb', line 138

def nodes
  @mutex.synchronize do
    # Must copy array reference for copy on write semantics to work.
    @cluster_nodes.dup
  end
end

#notify_cluster_config_changedObject



347
348
349
350
351
352
# File 'lib/aerospike/cluster.rb', line 347

def notify_cluster_config_changed
  listeners = @cluster_config_change_listeners.get
  listeners.each do |listener|
    listener.send(:cluster_config_changed, self)
  end
end

#partitionsObject



360
361
362
363
364
365
366
367
# File 'lib/aerospike/cluster.rb', line 360

def partitions
  res = nil
  @mutex.synchronize do
    res = @partition_write_map
  end

  res
end

#random_nodeObject

Returns a random node on the cluster



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aerospike/cluster.rb', line 120

def random_node
  # Must copy array reference for copy on write semantics to work.
  node_array = nodes
  length = node_array.length
  i = 0
  while i < length
    # Must handle concurrency with other non-tending threads, so node_index is consistent.
    index = (@node_index.update{ |v| v+1 } % node_array.length).abs
    node = node_array[index]

    return node if node.active?

    i = i.succ
  end
  raise Aerospike::Exceptions::InvalidNode
end

#refresh_nodesObject

Refresh status of all nodes in cluster. Adds new nodes and/or removes unhealty ones



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/aerospike/cluster.rb', line 245

def refresh_nodes
  cluster_config_changed = false

  nodes = self.nodes
  if nodes.empty?
    seed_nodes
    cluster_config_changed = true
    nodes = self.nodes
  end

  peers = Peers.new

  # Clear node reference count
  nodes.each do |node|
    node.refresh_reset
  end

  peers.use_peers = supports_peers_protocol?

  # refresh all known nodes
  nodes.each do |node|
    node.refresh_info(peers)
  end

  # refresh peers when necessary
  if peers.generation_changed?
    # Refresh peers for all nodes that responded the first time even if only
    # one node's peers changed.
    peers.reset_refresh_count!

    nodes.each do |node|
      node.refresh_peers(peers)
    end
  end

  nodes.each do |node|
    node.refresh_partitions(peers) if node.partition_generation.changed?
  end

  if peers.generation_changed? || !peers.use_peers?
    nodes_to_remove = find_nodes_to_remove(peers.refresh_count)
    if nodes_to_remove.any?
      remove_nodes(nodes_to_remove)
      cluster_config_changed = true
    end
  end

  # Add any new nodes from peer refresh
  if peers.nodes.any?
    # peers.nodes is a Hash. Pass only values, ie. the array of nodes
    add_nodes(peers.nodes.values)
    cluster_config_changed = true
  end

  cluster_config_changed
end

#remove_alias(aliass) ⇒ Object



421
422
423
424
425
426
427
# File 'lib/aerospike/cluster.rb', line 421

def remove_alias(aliass)
  if aliass
    @mutex.synchronize do
      @aliases.delete(aliass)
    end
  end
end

#remove_cluster_config_change_listener(listener) ⇒ Object



205
206
207
208
209
# File 'lib/aerospike/cluster.rb', line 205

def remove_cluster_config_change_listener(listener)
  @cluster_config_change_listeners.update do |listeners|
    listeners.delete(listener)
  end
end

#remove_nodes(nodes_to_remove) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/aerospike/cluster.rb', line 473

def remove_nodes(nodes_to_remove)
  # There is no need to delete nodes from partition_write_map because the nodes
  # have already been set to inactive. Further connection requests will result
  # in an exception and a different node will be tried.

  # Cleanup node resources.
  nodes_to_remove.each do |node|
    # Remove node's aliases from cluster alias set.
    # Aliases are only used in tend thread, so synchronization is not necessary.
    node.aliases.each do |aliass|
      Aerospike.logger.debug("Removing alias #{aliass}")
      remove_alias(aliass)
    end

    node.close
  end

  # Remove all nodes at once to avoid copying entire array multiple times.
  remove_nodes_copy(nodes_to_remove)
end

#remove_nodes_copy(nodes_to_remove) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/aerospike/cluster.rb', line 501

def remove_nodes_copy(nodes_to_remove)
  # Create temporary nodes array.
  # Since nodes are only marked for deletion using node references in the nodes array,
  # and the tend thread is the only thread modifying nodes, we are guaranteed that nodes
  # in nodes_to_remove exist.  Therefore, we know the final array size.
  nodes_list = nodes
  node_array = []
  count = 0

  # Add nodes that are not in remove list.
  nodes_list.each do |node|
    if node_exists(node, nodes_to_remove)
      Aerospike.logger.info("Removed node `#{node}`")
    else
      node_array[count] = node
      count += 1
    end
  end

  # Do sanity check to make sure assumptions are correct.
  if count < node_array.length
    Aerospike.logger.warn("Node remove mismatch. Expected #{node_array.length}, Received #{count}")

    # Resize array.
    node_array = node_array.dup[0..count-1]
  end

  set_nodes(node_array)
end

#request_info(policy, *commands) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/aerospike/cluster.rb', line 178

def request_info(policy, *commands)
  node = random_node
  conn = node.get_connection(policy.timeout)
  Info.request(conn, *commands).tap do
    node.put_connection(conn)
  end
end

#seed_nodesObject



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/aerospike/cluster.rb', line 369

def seed_nodes
  seed_array = seeds

  Aerospike.logger.info("Seeding the cluster. Seeds count: #{seed_array.length}")

  list = []

  seed_array.each do |seed|
    begin
      seed_node_validator = NodeValidator.new(self, seed, @connection_timeout, @cluster_name, tls_options)
    rescue => e
      Aerospike.logger.error("Seed #{seed} failed: #{e}\n#{e.backtrace.join("\n")}")
      next
    end

    nv = nil
    # Seed host may have multiple aliases in the case of round-robin dns configurations.
    seed_node_validator.aliases.each do |aliass|
      if aliass == seed
        nv = seed_node_validator
      else
        begin
          nv = NodeValidator.new(self, aliass, @connection_timeout, @cluster_name, tls_options)
        rescue => e
          Aerospike.logger.error("Seed #{seed} failed: #{e}")
          next
        end
      end
      next if find_node_name(list, nv.name)

      node = create_node(nv)
      add_aliases(node)
      list << node
    end
  end

  add_nodes_copy(list) if list.length > 0
end

#seedsObject



93
94
95
96
97
# File 'lib/aerospike/cluster.rb', line 93

def seeds
  @mutex.synchronize do
    @cluster_seeds.dup
  end
end

#set_nodes(nodes) ⇒ Object



494
495
496
497
498
499
# File 'lib/aerospike/cluster.rb', line 494

def set_nodes(nodes)
  @mutex.synchronize do
    # Replace nodes with copy.
    @cluster_nodes = nodes
  end
end

#set_partitions(part_map) ⇒ Object



354
355
356
357
358
# File 'lib/aerospike/cluster.rb', line 354

def set_partitions(part_map)
  @mutex.synchronize do
    @partition_write_map = part_map
  end
end

#supports_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/aerospike/cluster.rb', line 186

def supports_feature?(feature)
  @features.get.include?(feature.to_s)
end

#supports_peers_protocol?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/aerospike/cluster.rb', line 190

def supports_peers_protocol?
  nodes.all? { |node| node.supports_feature?(Aerospike::Features::PEERS) }
end

#tendObject

Check health of all nodes in cluster



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/aerospike/cluster.rb', line 231

def tend
  was_changed = refresh_nodes

  return unless was_changed

  update_cluster_features
  notify_cluster_config_changed
  # only log the tend finish IF the number of nodes has been changed.
  # This prevents spamming the log on every tend interval
  log_tend_stats(nodes)
end

#tls_enabled?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/aerospike/cluster.rb', line 77

def tls_enabled?
  !tls_options.nil? && tls_options[:enable] != false
end

#update_cluster_featuresObject



339
340
341
342
343
344
345
# File 'lib/aerospike/cluster.rb', line 339

def update_cluster_features
  # Cluster supports features that are supported by all nodes
  @features.update do
    node_features = nodes.map(&:features)
    node_features.reduce(&:intersection) || Set.new
  end
end

#update_partitions(tokens, node) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/aerospike/cluster.rb', line 170

def update_partitions(tokens, node)
  nmap = tokens.update_partition(partitions, node)
  # update partition write map
  set_partitions(nmap) if nmap

  Aerospike.logger.info("Partitions for node #{node.name} updated")
end

#wait_till_stablizedObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/aerospike/cluster.rb', line 309

def wait_till_stablized
  count = -1

  # will run until the cluster is stablized
  thr = Thread.new do
    loop do
      tend

      # Check to see if cluster has changed since the last Tend.
      # If not, assume cluster has stabilized and return.
      break if count == nodes.length

      sleep(0.001) # sleep for a miliseconds

      count = nodes.length
    end
  end

  # wait for the thread to finish or timeout
  begin
    Timeout.timeout(@connection_timeout) do
      thr.join
    end
  rescue Timeout::Error
    thr.kill if thr.alive?
  end

  @closed.value = false if @cluster_nodes.length > 0
end