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.



11
12
13
14
15
16
# File 'lib/dcell/node_manager.rb', line 11

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.



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

def heartbeat_rate
  @heartbeat_rate
end

#heartbeat_timeoutObject (readonly)

Returns the value of attribute heartbeat_timeout.



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

def heartbeat_timeout
  @heartbeat_timeout
end

Instance Method Details

#allObject

Return all available nodes in the cluster



19
20
21
22
23
# File 'lib/dcell/node_manager.rb', line 19

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

#eachObject

Iterate across all available nodes



26
27
28
29
30
# File 'lib/dcell/node_manager.rb', line 26

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



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

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)
    self.link node
  end

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

#node_died(node, reason) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/dcell/node_manager.rb', line 52

def node_died(node, reason)
  if reason.nil? # wtf?
    # this wtf error seems to come from node socket writes
    # when the socket is not reachable anymore
    Celluloid::logger.debug "wtf?"
    return
  end
  # Handle dead node???
end

#remove(id) ⇒ Object



72
73
74
75
76
77
# File 'lib/dcell/node_manager.rb', line 72

def remove(id)
  if @nodes[id]
    @nodes[id].terminate if @nodes[id].alive?
    @nodes.delete(id)
  end
end

#update(id) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/dcell/node_manager.rb', line 62

def update(id)
  addr = Directory[id]
  return unless addr
  if ( node = @nodes[id] ) and node.alive?
    node.update_client_address( addr )
  else
    @nodes[id] = Node.new( id, addr )
  end
end