class Roma::Routing::RoutingTable

Attributes

div_bits[R]
fail_cnt[R]
fail_cnt_gap[RW]
fail_cnt_threshold[RW]
hbits[R]
mtree[R]
rd[R]
rn[R]
search_mask[R]
sub_nid[RW]

Public Class Methods

new(rd) click to toggle source
   # File lib/roma/routing/rttable.rb
23 def initialize(rd)
24   @log = Roma::Logging::RLogger.instance
25   @rd = rd
26   @rn = @rd.rn
27   @div_bits=@rd.div_bits
28   @hbits = 2**@rd.dgst_bits
29   @search_mask = @rd.search_mask
30   @fail_cnt = Hash.new(0)
31   @fail_cnt_threshold = 5
32   @fail_cnt_gap = 0
33   @fail_time = Time.now
34   @sub_nid = {}
35   init_mtree
36 end

Public Instance Methods

check_repetition_in_routing() click to toggle source
    # File lib/roma/routing/rttable.rb
136 def check_repetition_in_routing
137   @rd.v_idx.each_value{|value|
138     host = []
139     value.each{|instance|
140       host << instance.split("_")[0]
141     }
142     if host.uniq!
143       return true
144     end
145   }
146 
147   false
148 end
create_nodes_from_v_idx() click to toggle source

Reconstruct vnodes from v_idx

    # File lib/roma/routing/rttable.rb
166 def create_nodes_from_v_idx
167   @rd.create_nodes_from_v_idx
168 end
dump() click to toggle source
    # File lib/roma/routing/rttable.rb
118 def dump
119   Marshal.dump(@rd)
120 end
dump_binary() click to toggle source
    # File lib/roma/routing/rttable.rb
132 def dump_binary
133   @rd.dump_binary
134 end
dump_json() click to toggle source
    # File lib/roma/routing/rttable.rb
126 def dump_json
127   JSON.generate(
128                 [{:dgst_bits=>@rd.dgst_bits,:div_bits=>@rd.div_bits,:rn=>@rd.rn},
129                  @rd.nodes,@rd.v_idx])
130 end
dump_yaml() click to toggle source
    # File lib/roma/routing/rttable.rb
122 def dump_yaml
123   YAML.dump(@rd)
124 end
get_stat(ap) click to toggle source
   # File lib/roma/routing/rttable.rb
56 def get_stat(ap)
57   pn, sn, short, lost = num_of_vn(ap)
58   ret = {}
59   ret['routing.redundant'] = @rn
60   ret['routing.nodes.length'] = nodes.length
61   ret['routing.nodes'] = nodes.inspect
62   ret['routing.dgst_bits'] = @rd.dgst_bits
63   ret['routing.div_bits'] = @div_bits
64   ret['routing.vnodes.length'] = vnodes.length
65   ret['routing.primary'] = pn
66   (@rn-1).times{|i| ret["routing.secondary#{i+1}"] = sn[i]}
67   ret['routing.short_vnodes'] = short
68   ret['routing.lost_vnodes'] = lost
69   ret['routing.fail_cnt_threshold'] = @fail_cnt_threshold
70   ret['routing.fail_cnt_gap'] = @fail_cnt_gap
71   ret['routing.sub_nid'] = @sub_nid.inspect
72   ret
73 end
get_vnode_id(d) click to toggle source

get vnode id from hash value

   # File lib/roma/routing/rttable.rb
91 def get_vnode_id(d)
92   d & @search_mask
93 end
init_mtree() click to toggle source
   # File lib/roma/routing/rttable.rb
75 def init_mtree
76   @mtree = MerkleTree.new(@rd.dgst_bits,@rd.div_bits)
77   @rd.v_idx.each_pair{ |vn, nids|
78     @mtree.set(vn,nids)
79   }
80 end
leave(nid) click to toggle source

delete dropping node from list nid: dropping node

    # File lib/roma/routing/rttable.rb
105 def leave(nid)
106   @rd.nodes.delete(nid)
107   # delet nid from list
108   @rd.v_idx.each_pair{ |vn, nids|
109     nids.delete_if{ |nid2| nid2 == nid}
110     if nids.length == 0
111       @log.error("Vnode data is lost.(Vnode=#{vn})")
112     end
113     @mtree.set(vn,nids)
114   }
115   @fail_cnt.delete(nid)
116 end
nodes() click to toggle source
   # File lib/roma/routing/rttable.rb
82 def nodes
83   @rd.nodes.clone
84 end
num_of_vn(ap) click to toggle source
   # File lib/roma/routing/rttable.rb
38 def num_of_vn(ap)
39   pn = short = lost = 0
40   sn = Array.new(@rd.rn - 1, 0)
41   @rd.v_idx.each_pair do |vn, nids|
42     if nids == nil || nids.length == 0
43       lost += 1
44       next
45     elsif nids[0] == ap
46       pn += 1
47     elsif nids.include?(ap)
48       i = nids.index(ap) - 1
49       sn[i] += 1
50     end
51     short += 1 if nids.length < @rd.rn
52   end
53   [pn, sn, short, lost]
54 end
proc_failed(nid) click to toggle source
    # File lib/roma/routing/rttable.rb
150 def proc_failed(nid)
151   t = Time.now
152   if t - @fail_time > @fail_cnt_gap
153     @fail_cnt[nid] += 1
154     if @fail_cnt[nid] >= @fail_cnt_threshold
155       leave(nid)
156     end
157   end
158   @fail_time = t
159 end
proc_succeed(nid) click to toggle source
    # File lib/roma/routing/rttable.rb
161 def proc_succeed(nid)
162   @fail_cnt.delete(nid)
163 end
search_nodes(vn) click to toggle source

get array of node ID which have vnode vn: vnode id

    # File lib/roma/routing/rttable.rb
 97 def search_nodes(vn)
 98   @rd.v_idx[vn].clone
 99 rescue
100   nil
101 end
sub_nid_rd(addr) click to toggle source

Returns a new RoutingData object which replaced host name by the sub_nid attribute.

    # File lib/roma/routing/rttable.rb
171 def sub_nid_rd(addr)
172   sub_nid.each do |mask, sub|
173     if check_netmask?(addr, mask)
174       return get_replaced_rd(sub[:regexp], sub[:replace])
175     end
176   end
177   nil
178 end
vnodes() click to toggle source
   # File lib/roma/routing/rttable.rb
86 def vnodes
87   @rd.v_idx.keys
88 end

Private Instance Methods

check_netmask?(addr, mask) click to toggle source
    # File lib/roma/routing/rttable.rb
197 def check_netmask?(addr, mask)
198   if addr =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/
199     iaddr = ($1.to_i  << 24) + ($2.to_i << 16) + ($3.to_i << 8) + $4.to_i
200   else
201     @log.error("#{__method__}:Illigal format addr #{addr}")
202     return false
203   end
204 
205   if mask =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)/
206     imask_addr = ($1.to_i  << 24) + ($2.to_i << 16) + ($3.to_i << 8) + $4.to_i
207     imask = (2 ** $5.to_i - 1) << (32 - $5.to_i)
208   else
209     @log.error("#{__method__}:Illigal format mask #{mask}")
210     return false
211   end
212   (iaddr & imask) == (imask_addr & imask)
213 end
get_replaced_rd(regxp, replace) click to toggle source
    # File lib/roma/routing/rttable.rb
182 def get_replaced_rd(regxp, replace)
183   rd = Marshal.load(dump)
184 
185   rd.nodes.map! do |nid|
186     nid.sub(regxp, replace)
187   end
188 
189   rd.v_idx.each_value do |nids|
190     nids.map! do |nid|
191       nid.sub(regxp, replace)
192     end
193   end
194   rd
195 end