class Roma::Routing::RoutingData

Attributes

dgst_bits[RW]
div_bits[RW]
nodes[RW]
rn[RW]
v_clk[RW]
v_idx[RW]

Public Class Methods

create(dgst_bits,div_bits,rn,nodes,repethost=false) click to toggle source
    # File lib/roma/routing/routing_data.rb
216 def self.create(dgst_bits,div_bits,rn,nodes,repethost=false)
217   ret=RoutingData.new(dgst_bits,div_bits,rn)
218   ret.nodes=nodes.clone
219 
220   rnlm=RandomNodeListMaker.new(nodes,repethost)
221 
222   (2**div_bits).times do |i|
223     vn=i<<(dgst_bits-div_bits)
224     ret.v_clk[vn]=0
225     ret.v_idx[vn]=rnlm.list(rn)
226   end
227 
228   # vnode balanceing process
229   rlist = ret.get_balanced_vn_replacement_list(repethost)
230   ret.balance!(rlist, repethost) if rlist
231 
232   ret
233 end
decode_binary(bin) click to toggle source
   # File lib/roma/routing/routing_data.rb
64 def self.decode_binary(bin)
65   magic, ver, dgst_bits, div_bits, rn, nodeslen = bin.unpack('a2nCCCn')
66   raise 'Illegal format error' if magic != 'RT'
67   raise 'Unsupported version error' if ver != 1
68 
69   rd = RoutingData.new(dgst_bits, div_bits, rn)
70   
71   bin = bin[9..-1]
72   nodeslen.times{|i|
73     len, = bin.unpack('n')
74     bin = bin[2..-1]
75     nid, = bin.unpack("a#{len}")
76     bin = bin[len..-1]
77     nid.encode!("utf-8") if RUBY_VERSION >= "1.9.3"
78     rd.nodes << nid 
79   }
80   (2**div_bits).times{|i|
81     vn=i<<(dgst_bits-div_bits)
82     v_clk,len = bin.unpack('Nc')
83     rd.v_clk[vn] = v_clk
84     bin = bin[5..-1]
85     len.times{|i|
86       idx, = bin.unpack('n')
87       rd.v_idx[vn] = [] unless rd.v_idx[vn]
88       rd.v_idx[vn] << rd.nodes[idx]
89       bin = bin[2..-1]
90     }
91   }
92   rd
93 end
load(fname) click to toggle source
   # File lib/roma/routing/routing_data.rb
34 def self.load(fname)
35   rd=load_snapshot(fname)
36   rd.load_log_all(fname)
37   rd
38 end
load_snapshot(fname) click to toggle source
   # File lib/roma/routing/routing_data.rb
40 def self.load_snapshot(fname)
41   rd=nil
42   open(fname,'rb'){|io|
43     rd = YAML.load(io.read)
44   }
45   rd
46 end
new(dgst_bits,div_bits,rn) click to toggle source
   # File lib/roma/routing/routing_data.rb
18 def initialize(dgst_bits,div_bits,rn)
19   @dgst_bits=dgst_bits
20   @div_bits=div_bits
21   @rn=rn
22   @nodes=[]
23   @v_idx={}
24   @v_clk={}
25 end
snapshot(fname) click to toggle source
   # File lib/roma/routing/routing_data.rb
48 def self.snapshot(fname)
49   rd=load_snapshot(fname)
50   loglist=rd.get_file_list(fname)
51   if loglist.length<2
52     return false
53   end
54   loglist.delete(loglist.last)
55   loglist.each{|i,f|
56     rd.load_log_one(f)
57     File.rename(f,"#{f}~")
58   }
59   File.rename(fname,"#{fname}~")
60   rd.save(fname)
61   true
62 end

Public Instance Methods

clone() click to toggle source

for deep copy

   # File lib/roma/routing/routing_data.rb
96 def clone
97   Marshal.load(Marshal.dump(self))
98 end
create_nodes_from_v_idx() click to toggle source
    # File lib/roma/routing/routing_data.rb
199 def create_nodes_from_v_idx
200   buf_nodes={}
201   v_idx.each_value{|nids|
202     nids.each{|nid| buf_nodes[nid]=nid }
203   }
204   @nodes=buf_nodes.values.sort
205 end
dump_binary() click to toggle source

2 bytes('RT'):magic code unsigned short:format version unsigned char:dgst_bits unsigned char:div_bits unsigned char:rn unsigned short:number of nodes while number of nodes

unsigned short:length of node-id string
node-id string

while umber of vnodes

unsigned int32:v_clk
unsigned char:number of nodes
while umber of nodes
 unsigned short:index of nodes
    # File lib/roma/routing/routing_data.rb
114 def dump_binary
115   format_version = 1
116   # 9 bytes
117   ret = ['RT',format_version,dgst_bits,div_bits,rn,nodes.length].pack('a2nCCCn')
118   rev_hash = {}
119   nodes.each_with_index{|nid,idx|
120     rev_hash[nid] = idx
121     # 2 + nid.length bytes
122     ret += [nid.length,nid].pack('na*')
123   }
124   (2**div_bits).times{|i|
125     vn=i<<(dgst_bits-div_bits)
126     # 5 bytes
127     ret += [v_clk[vn],v_idx[vn].length].pack('Nc')
128     v_idx[vn].each{|nid|
129       # 2 bytes
130       ret += [rev_hash[nid]].pack('n')
131     }
132   }
133   ret
134 end
each_log_all(fname) { |t,l| ... } click to toggle source
    # File lib/roma/routing/routing_data.rb
136 def each_log_all(fname)
137   loglist=get_file_list(fname)
138   loglist.each{|i,f|
139     each_log_one(f){|t,l| yield t,l}
140   }
141 end
each_log_one(fname) { |mktime($1, $2, $3, $4, $5, $6), $7| ... } click to toggle source
    # File lib/roma/routing/routing_data.rb
143 def each_log_one(fname)
144   File.open(fname,"r"){|f|
145     while((line=f.gets)!=nil)
146       line.chomp!
147       next if line[0]=="#" || line.length==0
148       if line =~ /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d+\s(.+)/
149         yield Time.mktime($1, $2, $3, $4, $5, $6), $7 
150       end
151     end
152   }
153 end
get_file_list(fname) click to toggle source

Returns the log file list by old ordered.

fname

Prefix of a log file.(ex.roma0_3300.route)

One of the following example:

[[1, "roma0_3300.route.1"], [2, "roma0_3300.route.2"]]
    # File lib/roma/routing/routing_data.rb
239 def get_file_list(fname)
240   l={}
241   files=Dir.glob("#{fname}*")
242   files.each{ |file|
243     if /#{fname}\.(\d+)$/=~file
244       l[$1.to_i]=$&
245     end
246   }
247   # sorted by old order
248   l.to_a.sort{|a,b| a[0]<=>b[0]}
249 end
get_histgram() click to toggle source
    # File lib/roma/routing/routing_data.rb
251 def get_histgram
252   ret = {}
253   nodes.each{|nid|
254     ret[nid] = Array.new(rn,0)
255   }
256   v_idx.each_pair{|vn,nids|
257     nids.each_with_index{|nid,i|
258       ret[nid][i] += 1
259     }
260   }
261   ret
262 end
get_lost_vnodes() click to toggle source

Returns the losted vnode-id list.

    # File lib/roma/routing/routing_data.rb
208 def get_lost_vnodes
209   ret=[]
210   v_idx.each_pair{|vn,nids|
211     ret << vn if nids.length == 0
212   }
213   ret
214 end
load_log_all(fname) click to toggle source
    # File lib/roma/routing/routing_data.rb
155 def load_log_all(fname)
156   each_log_all(fname){|t,line|
157     parse_log(t,line)
158   }
159   @nodes.sort!
160 end
load_log_one(fname) click to toggle source
    # File lib/roma/routing/routing_data.rb
162 def load_log_one(fname)
163   each_log_one(fname){|t,line|
164     parse_log(t,line)
165   }
166   @nodes.sort!
167 end
next_vnode(vn) click to toggle source
    # File lib/roma/routing/routing_data.rb
193 def next_vnode(vn)
194   n = (vn >> (@dgst_bits-@div_bits)) + 1
195   n = 0 if n == (2**@div_bits)
196   n << (@dgst_bits-@div_bits)
197 end
parse_log(t,line) click to toggle source
    # File lib/roma/routing/routing_data.rb
169 def parse_log(t,line)
170   s=line.split(' ')
171   case s[0]
172   when 'setroute'
173     # setroute <vnode-id> <clock> <node-id> ...
174     nids=[]
175     s[3..-1].each{ |nid| nids << nid }
176     @v_idx[s[1].to_i]=nids
177     @v_clk[s[1].to_i]=s[2].to_i
178   when 'join'
179     # join <node-id>
180     @nodes << s[1] unless @nodes.include?(s[1])
181   when 'leave'
182     # leave <node-id>
183     @nodes.delete(s[1])
184   else
185     raise "RoutingData.parse_log:parse error #{line}"
186   end
187 end
save(fname) click to toggle source
   # File lib/roma/routing/routing_data.rb
27 def save(fname)
28   @nodes.sort!
29   open(fname,'wb'){|io|
30     io.write(YAML.dump(self))
31   }
32 end
search_mask() click to toggle source
    # File lib/roma/routing/routing_data.rb
189 def search_mask
190   2**@div_bits-1<<(@dgst_bits-@div_bits)
191 end