Class: ContentShard

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodesv, this_shardv) ⇒ ContentShard

Returns a new instance of ContentShard.



6
7
8
9
10
11
12
13
14
15
# File 'lib/adhd/models/content_shard.rb', line 6

def initialize(nodesv, this_shardv)
  @nodes = nodesv
  @this_shard = this_shardv

  # Work out the rest
  @our_node = nodesv.our_node
  @this_shard_db = nodesv.our_node.get_content_db(this_shardv.shard_db_name)

  @last_sync_seq = 0 # @this_shard_db.info['update_seq']
end

Instance Attribute Details

#nodesObject

Returns the value of attribute nodes.



4
5
6
# File 'lib/adhd/models/content_shard.rb', line 4

def nodes
  @nodes
end

#our_nodeObject

Returns the value of attribute our_node.



4
5
6
# File 'lib/adhd/models/content_shard.rb', line 4

def our_node
  @our_node
end

#this_shardObject

Returns the value of attribute this_shard.



4
5
6
# File 'lib/adhd/models/content_shard.rb', line 4

def this_shard
  @this_shard
end

#this_shard_dbObject

Returns the value of attribute this_shard_db.



4
5
6
# File 'lib/adhd/models/content_shard.rb', line 4

def this_shard_db
  @this_shard_db
end

Instance Method Details

#in_shard?(internal_id) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/adhd/models/content_shard.rb', line 17

def in_shard?(internal_id)
   internal_id >= this_shard.range_start && internal_id < this_shard.range_end
end

#syncObject



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
55
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
# File 'lib/adhd/models/content_shard.rb', line 29

def sync
  # A Shard only pushes with the master of the shard
  # or the node with the highest is_storage value alive
  # Shard masters ensure changes are pushed to all

  # NOTE: This method needs serious refactoring
  # No need to update
  return false if @this_shard_db.info['update_seq'] == @last_sync_seq

  # Are we the shard master?
  am_master = (our_node.name == this_shard.master_node)

  if !am_master
    master_node = Node.by_name(:key => this_shard.master_node).first
    remote_db = master_node.get_content_db(this_shard.shard_db_name)
    bool_to = @our_node.replicate_to(this_shard_db, master_node, remote_db)
    if bool_to
      @last_sync_seq = @this_shard_db.info['update_seq']
      return true
    end
  end

  # Either we are the master or the master has failed -- we replicate with
  # all nodes or the first available aside us and master
  all_good = true
  this_shard.node_list.each do |node_name|
    # Do not sync with ourselves
    next if (@our_node.name == node_name)
  
     # Push all changes to the other nodes
     remote_node = Node.by_name(:key =>  node_name).first
     remote_db = remote_node.get_content_db(this_shard.shard_db_name)
     bool_to = @our_node.replicate_to(this_shard_db, remote_node, remote_db)
     all_good &= bool_to
     if !am_master && bool_to
       # NOTE: How to build skynet, Note 2
       #       We are doing some "gonzo" replication, here. Our master is
       #       clearly down so we find the second best node; we push our
       #       changes to this node, and now also *replicate from*
       #       that node.
       @our_node.replicate_from(this_shard_db, remote_node, remote_db)
       @last_sync_seq = @this_shard_db.info['update_seq']
       break
     end
  end
  if all_good
    @last_sync_seq = @this_shard_db.info['update_seq']
    return true
  else
    return false
  end
end

#write_doc(content_doc) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/adhd/models/content_shard.rb', line 21

def write_doc(content_doc)
  # Write a content document to this shard
  # Make sure it is in this shard
  if in_shard? content_doc.internal_id
    this_shard_db.save_doc(content_doc)
  end
end