Class: Aerospike::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/aerospike/node.rb,
lib/aerospike/node/generation.rb,
lib/aerospike/node/verify/name.rb,
lib/aerospike/node/refresh/info.rb,
lib/aerospike/node/refresh/peers.rb,
lib/aerospike/node/refresh/reset.rb,
lib/aerospike/node/refresh/failed.rb,
lib/aerospike/node/refresh/friends.rb,
lib/aerospike/node/refresh/partitions.rb,
lib/aerospike/node/verify/cluster_name.rb,
lib/aerospike/node/verify/peers_generation.rb,
lib/aerospike/node/verify/partition_generation.rb

Defined Under Namespace

Modules: Refresh, Verify Classes: Generation

Constant Summary collapse

PARTITIONS =
4096
FULL_HEALTH =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster, nv) ⇒ Node

Initialize server node with connection parameters.



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

def initialize(cluster, nv)
  @cluster = cluster
  @name = nv.name
  @aliases = Atomic.new(nv.aliases)
  @host = nv.host
  @use_new_info = Atomic.new(nv.use_new_info)
  @features = nv.features
  @cluster_name = nv.cluster_name

  # TODO: Re-use connection from node validator
  @tend_connection = nil

  # Assign host to first IP alias because the server identifies nodes
  # by IP address (not hostname).
  @host = nv.aliases[0]
  @health = Atomic.new(FULL_HEALTH)
  @peers_count = Atomic.new(0)
  @peers_generation = ::Aerospike::Node::Generation.new
  @partition_generation = ::Aerospike::Node::Generation.new
  @reference_count = Atomic.new(0)
  @responded = Atomic.new(false)
  @active = Atomic.new(true)
  @failures = Atomic.new(0)

  @connections = Pool.new(@cluster.connection_queue_size)

  # TODO: put in separate methods
  @connections.create_block = Proc.new do
    conn = nil
    loop do
      conn = Cluster::CreateConnection.(cluster, host)
      break if conn.connected?
    end
    conn
  end

  @connections.cleanup_block = Proc.new { |conn| conn.close if conn }
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def cluster
  @cluster
end

#cluster_nameObject (readonly)

Returns the value of attribute cluster_name.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def cluster_name
  @cluster_name
end

#failuresObject (readonly)

Returns the value of attribute failures.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def failures
  @failures
end

#featuresObject (readonly)

Returns the value of attribute features.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def features
  @features
end

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def name
  @name
end

#partition_generationObject (readonly)

Returns the value of attribute partition_generation.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def partition_generation
  @partition_generation
end

#peers_countObject (readonly)

Returns the value of attribute peers_count.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def peers_count
  @peers_count
end

#peers_generationObject (readonly)

Returns the value of attribute peers_generation.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def peers_generation
  @peers_generation
end

#reference_countObject (readonly)

Returns the value of attribute reference_count.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def reference_count
  @reference_count
end

#respondedObject (readonly)

Returns the value of attribute responded.



25
26
27
# File 'lib/aerospike/node.rb', line 25

def responded
  @responded
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



184
185
186
# File 'lib/aerospike/node.rb', line 184

def ==(other)
  other && other.is_a?(Node) && (@name == other.name)
end

#active!Object

Sets node as active



120
121
122
# File 'lib/aerospike/node.rb', line 120

def active!
  @active.update { |_| true }
end

#active?Boolean

Checks if the node is active

Returns:

  • (Boolean)


130
131
132
# File 'lib/aerospike/node.rb', line 130

def active?
  @active.value
end

#aliasesObject



170
171
172
# File 'lib/aerospike/node.rb', line 170

def aliases
  @aliases.value
end

#closeObject

Marks node as inactice and closes all cached connections



175
176
177
178
# File 'lib/aerospike/node.rb', line 175

def close
  inactive!
  close_connections
end

#decrease_healthObject

Decrease node Health as a result of bad connection or communication



105
106
107
# File 'lib/aerospike/node.rb', line 105

def decrease_health
  @health.update { |v| v - 1 }
end

#failed!Object



162
163
164
# File 'lib/aerospike/node.rb', line 162

def failed!
  @failures.update { |v| v + 1 }
end

#failed?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/aerospike/node.rb', line 158

def failed?
  @failures.value > 0
end

#get_connection(timeout) ⇒ Object

Get a connection to the node. If no cached connection is not available, a new connection will be created



72
73
74
75
76
77
78
79
80
# File 'lib/aerospike/node.rb', line 72

def get_connection(timeout)
  loop do
    conn = @connections.poll
    if conn.connected?
      conn.timeout = timeout.to_f
      return conn
    end
  end
end

#get_hostObject

Retrieves host for the node



115
116
117
# File 'lib/aerospike/node.rb', line 115

def get_host
  @host
end

#has_peers?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/aerospike/node.rb', line 154

def has_peers?
  @peers_count.value > 0
end

#hashObject



193
194
195
# File 'lib/aerospike/node.rb', line 193

def hash
  @name.hash
end

#inactive!Object

Sets node as inactive



125
126
127
# File 'lib/aerospike/node.rb', line 125

def inactive!
  @active.update { |_| false }
end

#increase_reference_count!Object



134
135
136
# File 'lib/aerospike/node.rb', line 134

def increase_reference_count!
  @reference_count.update { |v| v + 1 }
end

#inspectObject



197
198
199
# File 'lib/aerospike/node.rb', line 197

def inspect
  "#<Aerospike::Node: @name=#{@name}, @host=#{@host}>"
end

#put_connection(conn) ⇒ Object

Put back a connection to the cache. If cache is full, the connection will be closed and discarded



84
85
86
87
# File 'lib/aerospike/node.rb', line 84

def put_connection(conn)
  conn.close if !active?
  @connections.offer(conn)
end

#refresh_info(peers) ⇒ Object

Convenience wrappers for applying refresh operations to a node



205
206
207
# File 'lib/aerospike/node.rb', line 205

def refresh_info(peers)
  Node::Refresh::Info.(self, peers)
end

#refresh_partitions(peers) ⇒ Object



209
210
211
# File 'lib/aerospike/node.rb', line 209

def refresh_partitions(peers)
  Node::Refresh::Partitions.(self, peers)
end

#refresh_peers(peers) ⇒ Object



213
214
215
# File 'lib/aerospike/node.rb', line 213

def refresh_peers(peers)
  Node::Refresh::Peers.(self, peers)
end

#refresh_resetObject



217
218
219
# File 'lib/aerospike/node.rb', line 217

def refresh_reset
  Node::Refresh::Reset.(self)
end

#reset_failures!Object



166
167
168
# File 'lib/aerospike/node.rb', line 166

def reset_failures!
  @failures.value = 0
end

#reset_reference_count!Object



138
139
140
# File 'lib/aerospike/node.rb', line 138

def reset_reference_count!
  @reference_count.value = 0
end

#reset_responded!Object



150
151
152
# File 'lib/aerospike/node.rb', line 150

def reset_responded!
  @responded.value = false
end

#responded!Object



142
143
144
# File 'lib/aerospike/node.rb', line 142

def responded!
  @responded.value = true
end

#responded?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/aerospike/node.rb', line 146

def responded?
  @responded.value == true
end

#restore_healthObject

Mark the node as healthy



98
99
100
101
102
# File 'lib/aerospike/node.rb', line 98

def restore_health
  # There can be cases where health is full, but active is false.
  # Once a node has been marked inactive, it stays inactive.
  @health.value = FULL_HEALTH
end

#supports_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/aerospike/node.rb', line 180

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

#tend_connectionObject

Separate connection for refreshing



90
91
92
93
94
95
# File 'lib/aerospike/node.rb', line 90

def tend_connection
  if @tend_connection.nil? || @tend_connection.closed?
    @tend_connection = Cluster::CreateConnection.(cluster, host)
  end
  @tend_connection
end

#unhealthy?Boolean

Check if the node is unhealthy

Returns:

  • (Boolean)


110
111
112
# File 'lib/aerospike/node.rb', line 110

def unhealthy?
  @health.value <= 0
end

#use_new_info?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/aerospike/node.rb', line 189

def use_new_info?
  @use_new_info.value
end