Class: Roma::Routing::RoutingData

Inherits:
Object
  • Object
show all
Includes:
RandomBalancer
Defined in:
lib/roma/routing/routing_data.rb

Defined Under Namespace

Classes: RandomNodeListMaker

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RandomBalancer

#balance!, #get_balanced_vn_replacement_list, #get_min_max_histgram, #randomly_change_nid!

Constructor Details

#initialize(dgst_bits, div_bits, rn) ⇒ RoutingData

Returns a new instance of RoutingData.



18
19
20
21
22
23
24
25
# File 'lib/roma/routing/routing_data.rb', line 18

def initialize(dgst_bits,div_bits,rn)
  @dgst_bits=dgst_bits
  @div_bits=div_bits
  @rn=rn
  @nodes=[]
  @v_idx={}
  @v_clk={}
end

Instance Attribute Details

#dgst_bitsObject

Returns the value of attribute dgst_bits.



11
12
13
# File 'lib/roma/routing/routing_data.rb', line 11

def dgst_bits
  @dgst_bits
end

#div_bitsObject

Returns the value of attribute div_bits.



12
13
14
# File 'lib/roma/routing/routing_data.rb', line 12

def div_bits
  @div_bits
end

#nodesObject

Returns the value of attribute nodes.



14
15
16
# File 'lib/roma/routing/routing_data.rb', line 14

def nodes
  @nodes
end

#rnObject

Returns the value of attribute rn.



13
14
15
# File 'lib/roma/routing/routing_data.rb', line 13

def rn
  @rn
end

#v_clkObject

Returns the value of attribute v_clk.



16
17
18
# File 'lib/roma/routing/routing_data.rb', line 16

def v_clk
  @v_clk
end

#v_idxObject

Returns the value of attribute v_idx.



15
16
17
# File 'lib/roma/routing/routing_data.rb', line 15

def v_idx
  @v_idx
end

Class Method Details

.create(dgst_bits, div_bits, rn, nodes, repethost = false) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/roma/routing/routing_data.rb', line 216

def self.create(dgst_bits,div_bits,rn,nodes,repethost=false)
  ret=RoutingData.new(dgst_bits,div_bits,rn)
  ret.nodes=nodes.clone

  rnlm=RandomNodeListMaker.new(nodes,repethost)

  (2**div_bits).times do |i|
    vn=i<<(dgst_bits-div_bits)
    ret.v_clk[vn]=0
    ret.v_idx[vn]=rnlm.list(rn)
  end

  # vnode balanceing process
  rlist = ret.get_balanced_vn_replacement_list(repethost)
  ret.balance!(rlist, repethost) if rlist

  ret
end

.decode_binary(bin) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/roma/routing/routing_data.rb', line 64

def self.decode_binary(bin)
  magic, ver, dgst_bits, div_bits, rn, nodeslen = bin.unpack('a2nCCCn')
  raise 'Illegal format error' if magic != 'RT'
  raise 'Unsupported version error' if ver != 1

  rd = RoutingData.new(dgst_bits, div_bits, rn)
  
  bin = bin[9..-1]
  nodeslen.times{|i|
    len, = bin.unpack('n')
    bin = bin[2..-1]
    nid, = bin.unpack("a#{len}")
    bin = bin[len..-1]
    nid.encode!("utf-8") if RUBY_VERSION >= "1.9.3"
    rd.nodes << nid 
  }
  (2**div_bits).times{|i|
    vn=i<<(dgst_bits-div_bits)
    v_clk,len = bin.unpack('Nc')
    rd.v_clk[vn] = v_clk
    bin = bin[5..-1]
    len.times{|i|
      idx, = bin.unpack('n')
      rd.v_idx[vn] = [] unless rd.v_idx[vn]
      rd.v_idx[vn] << rd.nodes[idx]
      bin = bin[2..-1]
    }
  }
  rd
end

.load(fname) ⇒ Object



34
35
36
37
38
# File 'lib/roma/routing/routing_data.rb', line 34

def self.load(fname)
  rd=load_snapshot(fname)
  rd.load_log_all(fname)
  rd
end

.load_snapshot(fname) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/roma/routing/routing_data.rb', line 40

def self.load_snapshot(fname)
  rd=nil
  open(fname,'rb'){|io|
    rd = YAML.load(io.read)
  }
  rd
end

.snapshot(fname) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/roma/routing/routing_data.rb', line 48

def self.snapshot(fname)
  rd=load_snapshot(fname)
  loglist=rd.get_file_list(fname)
  if loglist.length<2
    return false
  end
  loglist.delete(loglist.last)
  loglist.each{|i,f|
    rd.load_log_one(f)
    File.rename(f,"#{f}~")
  }
  File.rename(fname,"#{fname}~")
  rd.save(fname)
  true
end

Instance Method Details

#cloneObject

for deep copy



96
97
98
# File 'lib/roma/routing/routing_data.rb', line 96

def clone
  Marshal.load(Marshal.dump(self))
end

#create_nodes_from_v_idxObject



199
200
201
202
203
204
205
# File 'lib/roma/routing/routing_data.rb', line 199

def create_nodes_from_v_idx
  buf_nodes={}
  v_idx.each_value{|nids|
    nids.each{|nid| buf_nodes[nid]=nid }
  }
  @nodes=buf_nodes.values.sort
end

#dump_binaryObject

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


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/roma/routing/routing_data.rb', line 114

def dump_binary
  format_version = 1
  # 9 bytes
  ret = ['RT',format_version,dgst_bits,div_bits,rn,nodes.length].pack('a2nCCCn')
  rev_hash = {}
  nodes.each_with_index{|nid,idx|
    rev_hash[nid] = idx
    # 2 + nid.length bytes
    ret += [nid.length,nid].pack('na*')
  }
  (2**div_bits).times{|i|
    vn=i<<(dgst_bits-div_bits)
    # 5 bytes
    ret += [v_clk[vn],v_idx[vn].length].pack('Nc')
    v_idx[vn].each{|nid|
      # 2 bytes
      ret += [rev_hash[nid]].pack('n')
    }
  }
  ret
end

#each_log_all(fname) ⇒ Object



136
137
138
139
140
141
# File 'lib/roma/routing/routing_data.rb', line 136

def each_log_all(fname)
  loglist=get_file_list(fname)
  loglist.each{|i,f|
    each_log_one(f){|t,l| yield t,l}
  }
end

#each_log_one(fname) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/roma/routing/routing_data.rb', line 143

def each_log_one(fname)
  File.open(fname,"r"){|f|
    while((line=f.gets)!=nil)
      line.chomp!
      next if line[0]=="#" || line.length==0
      if line =~ /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d+\s(.+)/
        yield Time.mktime($1, $2, $3, $4, $5, $6), $7 
      end
    end
  }
end

#get_file_list(fname) ⇒ Object

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"]]


239
240
241
242
243
244
245
246
247
248
249
# File 'lib/roma/routing/routing_data.rb', line 239

def get_file_list(fname)
  l={}
  files=Dir.glob("#{fname}*")
  files.each{ |file|
    if /#{fname}\.(\d+)$/=~file
      l[$1.to_i]=$&
    end
  }
  # sorted by old order
  l.to_a.sort{|a,b| a[0]<=>b[0]}
end

#get_histgramObject



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/roma/routing/routing_data.rb', line 251

def get_histgram
  ret = {}
  nodes.each{|nid|
    ret[nid] = Array.new(rn,0)
  }
  v_idx.each_pair{|vn,nids|
    nids.each_with_index{|nid,i|
      ret[nid][i] += 1
    }
  }
  ret
end

#get_lost_vnodesObject

Returns the losted vnode-id list.



208
209
210
211
212
213
214
# File 'lib/roma/routing/routing_data.rb', line 208

def get_lost_vnodes
  ret=[]
  v_idx.each_pair{|vn,nids|
    ret << vn if nids.length == 0
  }
  ret
end

#load_log_all(fname) ⇒ Object



155
156
157
158
159
160
# File 'lib/roma/routing/routing_data.rb', line 155

def load_log_all(fname)
  each_log_all(fname){|t,line|
    parse_log(t,line)
  }
  @nodes.sort!
end

#load_log_one(fname) ⇒ Object



162
163
164
165
166
167
# File 'lib/roma/routing/routing_data.rb', line 162

def load_log_one(fname)
  each_log_one(fname){|t,line|
    parse_log(t,line)
  }
  @nodes.sort!
end

#next_vnode(vn) ⇒ Object



193
194
195
196
197
# File 'lib/roma/routing/routing_data.rb', line 193

def next_vnode(vn)
  n = (vn >> (@dgst_bits-@div_bits)) + 1
  n = 0 if n == (2**@div_bits)
  n << (@dgst_bits-@div_bits)
end

#parse_log(t, line) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/roma/routing/routing_data.rb', line 169

def parse_log(t,line)
  s=line.split(' ')
  case s[0]
  when 'setroute'
    # setroute <vnode-id> <clock> <node-id> ...
    nids=[]
    s[3..-1].each{ |nid| nids << nid }
    @v_idx[s[1].to_i]=nids
    @v_clk[s[1].to_i]=s[2].to_i
  when 'join'
    # join <node-id>
    @nodes << s[1] unless @nodes.include?(s[1])
  when 'leave'
    # leave <node-id>
    @nodes.delete(s[1])
  else
    raise "RoutingData.parse_log:parse error #{line}"
  end
end

#save(fname) ⇒ Object



27
28
29
30
31
32
# File 'lib/roma/routing/routing_data.rb', line 27

def save(fname)
  @nodes.sort!
  open(fname,'wb'){|io|
    io.write(YAML.dump(self))
  }
end

#search_maskObject



189
190
191
# File 'lib/roma/routing/routing_data.rb', line 189

def search_mask
  2**@div_bits-1<<(@dgst_bits-@div_bits)
end