module Roma::Routing::RandomBalancer
Public Instance Methods
balance!(vn_replacement_list, repethost = false)
click to toggle source
# File lib/roma/routing/random_balancer.rb 68 def balance!(vn_replacement_list, repethost = false) 69 vn_replacement_list.each do |rep| 70 v_idx[rep[:vn]][rep[:idx]] = rep[:to] 71 end 72 end
get_balanced_vn_replacement_list(repethost = false)
click to toggle source
Returns a replacement list for balanced routing.
# File lib/roma/routing/random_balancer.rb 53 def get_balanced_vn_replacement_list(repethost = false) 54 rd = clone 55 ret = [] 56 @rn.times do |idx| # primary, secondary1, ... 57 loop do # until balanced 58 min, min_nid, max, max_nid = rd.get_min_max_histgram(idx) 59 break if max - min < 2 || min_nid == max_nid 60 vn = rd.randomly_change_nid!(idx, max_nid, min_nid, repethost) 61 return nil unless vn # error 62 ret << {:vn=>vn, :idx=>idx, :from=>max_nid, :to=>min_nid} 63 end 64 end 65 ret 66 end
get_min_max_histgram(idx)
click to toggle source
Returns min/max values and correspondent node-id of the histgram.
idx
-
As for 0 is primary, 1 or more are secondary.
# File lib/roma/routing/random_balancer.rb 34 def get_min_max_histgram(idx) 35 h = get_histgram 36 min_nid = max_nid = nil 37 min = v_idx.length 38 max = 0 39 h.each do |nid, v| 40 if v[idx] < min 41 min = v[idx] 42 min_nid = nid 43 end 44 if v[idx] > max 45 max = v[idx] 46 max_nid = nid 47 end 48 end 49 [min, min_nid, max, max_nid] 50 end
randomly_change_nid!(idx, from, to, repethost = false)
click to toggle source
The randomly selected from
's vertual-node changes to to
.
idx
-
As for 0 is primary, 1 or more are secondary.
# File lib/roma/routing/random_balancer.rb 7 def randomly_change_nid!(idx, from, to, repethost = false) 8 vns = [] 9 v_idx.each_pair do |vn, nids| 10 cnt = 0 11 nids.each_with_index do |nid, i| 12 if idx == i 13 cnt += 1 if nid == from 14 else 15 if repethost == true 16 cnt += 1 if nid != to 17 else 18 cnt += 1 if nid.split('_')[0] != to.split('_')[0] 19 end 20 end 21 end 22 vns << vn if cnt == nids.length 23 end 24 return nil if vns.length == 0 25 vn = vns[rand(vns.length)] 26 #puts "#{vn} #{v_idx[vn]}" 27 v_idx[vn][idx] = to 28 #puts "#{vn} #{v_idx[vn]}" 29 vn 30 end