Class: NodeDB

Inherits:
Object show all
Defined in:
lib/adhd/models/node_db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(our_nodev) ⇒ NodeDB

Returns a new instance of NodeDB.



5
6
7
8
9
10
# File 'lib/adhd/models/node_db.rb', line 5

def initialize(our_nodev)
  @our_node = our_nodev

  # Get the address of the CDB from the node
  @local_node_db = our_nodev.get_node_db
end

Instance Attribute Details

#local_node_dbObject

Returns the value of attribute local_node_db.



3
4
5
# File 'lib/adhd/models/node_db.rb', line 3

def local_node_db
  @local_node_db
end

#our_nodeObject

Returns the value of attribute our_node.



3
4
5
# File 'lib/adhd/models/node_db.rb', line 3

def our_node
  @our_node
end

Instance Method Details

#available_node_listObject

Returns all nodes marked as available



46
47
48
49
# File 'lib/adhd/models/node_db.rb', line 46

def available_node_list
  all_nodes = Node.by_name
  return all_nodes.select {|node| node.status == "RUNNING"}
end

#head_management_nodeObject

Returns the first RUNNING management node. There is no real dependency on any specific node, this is just a way for all nodes to agree on the same node to do the job of the head management node.



55
56
57
58
59
# File 'lib/adhd/models/node_db.rb', line 55

def head_management_node
  management_nodes = Node.by_is_management.reverse
  hmn = management_nodes.find {|node| node.status == "RUNNING"}
  return hmn
end

#syncObject

Syncs this management node with other existing management nodes by looping through all known management nodes.

If replication to or from any management node fails, the method breaks and continues replicating to other management nodes until all management nodes have been tried.

NOTE: randomize the order for load balancing here

NOTE2: How to build skynet (TODO)

-------------------
If length of management is zero, then chose 3 different random
nodes at each sync, and sync with them in node_name order.
This guarantees that any updates on nodes are communicated in
O(log N) ephocs, at the cost of O(3 * N) connections per epoch.
It also guarantees any new management servers are discovered in
this O(log N) time, creating "jelly fish" or "partition proof"
availability.


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/adhd/models/node_db.rb', line 30

def sync
  # We replicate our state to the management node(s)
  management_nodes = Node.by_is_management.reverse

  management_nodes.each do |mng_node|
    remote_db = mng_node.get_node_db
    from_success = @our_node.replicate_from(local_node_db, mng_node, remote_db)
    to_success = @our_node.replicate_to(local_node_db, mng_node, remote_db)
    if from_success && to_success && !our_node.is_management
       break
    end
  end
end