Class: Roma::Routing::RoutingData

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

Defined Under Namespace

Classes: RandomNodeListMaker

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dgst_bits, div_bits, rn) ⇒ RoutingData

Returns a new instance of RoutingData.



14
15
16
17
18
19
20
21
# File 'lib/roma/client/routing/routing_data.rb', line 14

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.



7
8
9
# File 'lib/roma/client/routing/routing_data.rb', line 7

def dgst_bits
  @dgst_bits
end

#div_bitsObject

Returns the value of attribute div_bits.



8
9
10
# File 'lib/roma/client/routing/routing_data.rb', line 8

def div_bits
  @div_bits
end

#nodesObject

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#rnObject

Returns the value of attribute rn.



9
10
11
# File 'lib/roma/client/routing/routing_data.rb', line 9

def rn
  @rn
end

#v_clkObject

Returns the value of attribute v_clk.



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

def v_clk
  @v_clk
end

#v_idxObject

Returns the value of attribute v_idx.



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

def v_idx
  @v_idx
end

Class Method Details

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



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

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{|i|
    vn=i<<(dgst_bits-div_bits)
    ret.v_clk[vn]=0
    ret.v_idx[vn]=rnlm.list(rn)
  }
  ret
end

.load(fname) ⇒ Object



30
31
32
33
34
# File 'lib/roma/client/routing/routing_data.rb', line 30

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

.load_snapshot(fname) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/roma/client/routing/routing_data.rb', line 36

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

.snapshot(fname) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/roma/client/routing/routing_data.rb', line 44

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

#create_nodes_from_v_idxObject



123
124
125
126
127
128
129
# File 'lib/roma/client/routing/routing_data.rb', line 123

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

#each_log_all(fname) ⇒ Object



60
61
62
63
64
65
# File 'lib/roma/client/routing/routing_data.rb', line 60

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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/roma/client/routing/routing_data.rb', line 67

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


158
159
160
161
162
163
164
165
166
167
168
# File 'lib/roma/client/routing/routing_data.rb', line 158

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



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/roma/client/routing/routing_data.rb', line 170

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.



132
133
134
135
136
137
138
# File 'lib/roma/client/routing/routing_data.rb', line 132

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

#load_log_all(fname) ⇒ Object



79
80
81
82
83
84
# File 'lib/roma/client/routing/routing_data.rb', line 79

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

#load_log_one(fname) ⇒ Object



86
87
88
89
90
91
# File 'lib/roma/client/routing/routing_data.rb', line 86

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

#next_vnode(vn) ⇒ Object



117
118
119
120
121
# File 'lib/roma/client/routing/routing_data.rb', line 117

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/roma/client/routing/routing_data.rb', line 93

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



23
24
25
26
27
28
# File 'lib/roma/client/routing/routing_data.rb', line 23

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

#search_maskObject



113
114
115
# File 'lib/roma/client/routing/routing_data.rb', line 113

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