Class: DCell::NodeManager

Inherits:
Object
  • Object
show all
Includes:
Celluloid::ZMQ, Enumerable
Defined in:
lib/dcell/node_manager.rb

Overview

Manage nodes we’re connected to

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNodeManager

Returns a new instance of NodeManager.



9
10
11
12
13
14
# File 'lib/dcell/node_manager.rb', line 9

def initialize
  @nodes = {}

  @heartbeat_rate    = 5  # How often to send heartbeats in seconds
  @heartbeat_timeout = 10 # How soon until a lost heartbeat triggers a node partition
end

Instance Attribute Details

#heartbeat_rateObject (readonly)

Returns the value of attribute heartbeat_rate.



7
8
9
# File 'lib/dcell/node_manager.rb', line 7

def heartbeat_rate
  @heartbeat_rate
end

#heartbeat_timeoutObject (readonly)

Returns the value of attribute heartbeat_timeout.



7
8
9
# File 'lib/dcell/node_manager.rb', line 7

def heartbeat_timeout
  @heartbeat_timeout
end

Instance Method Details

#allObject

Return all available nodes in the cluster



17
18
19
20
21
# File 'lib/dcell/node_manager.rb', line 17

def all
  Directory.all.map do |node_id|
    find node_id
  end
end

#eachObject

Iterate across all available nodes



24
25
26
27
28
# File 'lib/dcell/node_manager.rb', line 24

def each
  Directory.all.each do |node_id|
    yield find node_id
  end
end

#find(id) ⇒ Object Also known as: []

Find a node by its node ID



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dcell/node_manager.rb', line 31

def find(id)
  node = @nodes[id]
  return node if node

  addr = Directory[id]
  return unless addr

  if id == DCell.id
    node = DCell.me
  else
    node = Node.new(id, addr)
  end

  @nodes[id] ||= node
  @nodes[id]
end