Module: Aerospike::Node::Refresh::Friends

Defined in:
lib/aerospike/node/refresh/friends.rb

Overview

Refresh peers/friends based on old service protocol

Class Method Summary collapse

Class Method Details

.call(node, peers, info_map) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aerospike/node/refresh/friends.rb', line 26

def call(node, peers, info_map)
  friend_string = info_map['services']
  cluster = node.cluster

  Aerospike.logger.debug("Refreshing friends for node #{node.name}: services=#{friend_string}")

  if friend_string.to_s.empty?
    node.peers_count.value = 0
    return
  end

  friend_names = friend_string.split(';')
  node.peers_count.value = friend_names.size

  friend_names.each do |friend|
    hostname, port = friend.split(':')
    host = Host.new(hostname, port.to_i)
    found_node = cluster.find_alias(host)

    if found_node
      found_node.increase_reference_count!
      Aerospike.logger.debug("Found existing node #{found_node.name} for host #{host}: Increased ref count to #{found_node.reference_count.value}")
    else
      unless peers.hosts.include?(host)
        prepare(cluster, peers, host)
      end
    end
  end
end

.prepare(cluster, peers, host) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aerospike/node/refresh/friends.rb', line 56

def prepare(cluster, peers, host)
  Aerospike.logger.debug("Preparing to add new node for host #{host}")
  nv = NodeValidator.new(
    cluster,
    host,
    cluster.connection_timeout,
    cluster.cluster_name,
    cluster.tls_options
  )

  node = peers.find_node_by_name(nv.name)

  unless node.nil?
    Aerospike.logger.debug("Found existing node #{node.name} among peers for host #{host}")
    peers.hosts << host
    node.aliases << host
    return true
  end

  node = cluster.find_node_by_name(nv.name)

  unless node.nil?
    Aerospike.logger.debug("Found existing node #{node.name} in cluster for host #{host}")
    peers.hosts << host
    node.aliases << host
    # Only increase reference count if found in cluster
    node.increase_reference_count!
    cluster.add_alias(host, node)
    return true
  end

  Aerospike.logger.debug("No existing node found - creating new node #{nv.name} for host #{host}")
  node = cluster.create_node(nv)
  peers.hosts << host
  peers.nodes[nv.name] = node
  true
rescue ::Aerospike::Exceptions::Aerospike => e
  ::Aerospike.logger.warn("Add node for host #{host} failed: #{e}")
  false
end