module Roma::Routing::RandomPartitioner

Public Instance Methods

exclude_nodes(ap_str, rep_host) click to toggle source
   # File lib/roma/routing/random_partitioner.rb
 4 def exclude_nodes(ap_str, rep_host)
 5   exclude_nodes = nodes
 6   if rep_host
 7     exclude_nodes = [ap_str]
 8   else
 9     myhost = ap_str.split(/[:_]/)[0]
10     exclude_nodes.delete_if { |nid| nid.split(/[:_]/)[0] != myhost }
11   end
12   exclude_nodes
13 end
exclude_nodes_for_balance(ap_str, rep_host)
Alias for: exclude_nodes
exclude_nodes_for_join(ap_str, _rep_host) click to toggle source
   # File lib/roma/routing/random_partitioner.rb
15 def exclude_nodes_for_join(ap_str, _rep_host)
16   [ap_str]
17 end
exclude_nodes_for_recover(ap_str, rep_host)
Alias for: exclude_nodes
select_node_for_release(ap_str, rep_host, nids) click to toggle source
    # File lib/roma/routing/random_partitioner.rb
 89 def select_node_for_release(ap_str, rep_host, nids)
 90   buf = nodes
 91 
 92   unless rep_host
 93     deny_hosts = []
 94     nids.each do |nid|
 95       host = nid.split(/[:_]/)[0]
 96       deny_hosts << host if host != ap_str.split(/[:_]/)[0]
 97     end
 98     buf.delete_if { |nid| deny_hosts.include?(nid.split(/[:_]/)[0]) }
 99   else
100     nids.each { |nid| buf.delete(nid) }
101   end
102 
103   buf.delete_if { |instance| instance == ap_str }
104   to_nid = buf.sample
105   new_nids = nids.map { |n| n == ap_str ? to_nid : n }
106   [to_nid, new_nids]
107 end
select_vn_for_balance(exclude_nodes, _rep_host) click to toggle source

vnode sampling exclude exclude_nodes

    # File lib/roma/routing/random_partitioner.rb
110 def select_vn_for_balance(exclude_nodes, _rep_host)
111   short_idx = {}
112   idx = {}
113   @rd.v_idx.each_pair do |vn, nids|
114     unless list_include?(nids, exclude_nodes)
115       idx[vn] = nids
116       short_idx[vn] = nids if nids.length < @rd.rn
117     end
118   end
119   idx = short_idx if short_idx.length > 0
120 
121   ks = idx.keys
122   return nil if ks.length == 0
123   vn = ks[rand(ks.length)]
124   nids = idx[vn]
125   [vn, nids, rand(@rd.rn) == 0]
126 end
select_vn_for_join(exclude_nodes, rep_host) click to toggle source

vnode sampling exclude exclude_nodes

   # File lib/roma/routing/random_partitioner.rb
31 def select_vn_for_join(exclude_nodes, rep_host)
32   short_idx = {}
33   myhost_idx = {}
34   idx = {}
35   myhost = exclude_nodes[0].split(/[:_]/)[0]
36 
37   @rd.v_idx.each_pair do |vn, nids|
38     unless list_include?(nids, exclude_nodes)
39       if myhost_include?(nids, myhost)
40         myhost_idx[vn] = nids
41       else
42         idx[vn] = nids # other hosts
43       end
44       short_idx[vn] = nids if nids.length < @rd.rn
45     end
46   end
47 
48   # vnodes sampling priority:
49   # 1. Short vnodes
50   # 2. Other hosts vnodes
51   # 3. My host vnodes
52   if short_idx.length > 0
53     idx = short_idx
54   elsif idx.length == 0
55     idx = myhost_idx
56   end
57 
58   return nil if idx.length == 0
59 
60   ks = idx.keys
61   vn = ks[rand(ks.length)]
62   nids = idx[vn]
63 
64   if !rep_host && nids[0].split(/[:_]/)[0] == myhost
65     is_primary = true
66   else
67     is_primary = rand(@rd.rn) == 0
68   end
69 
70   [vn, nids, is_primary]
71 end
select_vn_for_recover(exclude_nodes, _rep_host) click to toggle source

select a vnodes where short of redundancy.

   # File lib/roma/routing/random_partitioner.rb
74 def select_vn_for_recover(exclude_nodes, _rep_host)
75   ret = []
76   @rd.v_idx.each_pair do |vn, nids|
77     if nids.length < @rd.rn && list_include?(nids, exclude_nodes) == false
78       ret << [vn, nids]
79     end
80   end
81   if ret.length == 0
82     nil
83   else
84     n = rand(ret.length)
85     [ret[n][0], ret[n][1], (rand(@rd.rn) == 0)]
86   end
87 end

Private Instance Methods

myhost_include?(nodes, myhost) click to toggle source
   # File lib/roma/routing/random_partitioner.rb
22 def myhost_include?(nodes, myhost)
23   nodes.each do |nid|
24     return true if nid.split(/[:_]/)[0] == myhost
25   end
26   false
27 end