38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
# File 'lib/sisfc/simulation.rb', line 38
def evaluate_allocation(vm_allocation)
@current_time = @start_time = @configuration.start_time
data_center_repository = Hash[
@configuration.data_centers.map do |k,v|
[ k, DataCenter.new(id: k, **v) ]
end
]
customer_repository = @configuration.customers
workflow_type_repository = @configuration.workflow_types
stats = Statistics.new
per_workflow_and_customer_stats = Hash[
workflow_type_repository.keys.map do |wft_id|
[
wft_id,
Hash[
customer_repository.keys.map do |c_id|
[ c_id, Statistics.new(@configuration.custom_stats.find{|x| x[:customer_id] == c_id && x[:workflow_type_id] == wft_id } || {}) ]
end
]
]
end
]
reqs_received_per_workflow_and_customer = Hash[
workflow_type_repository.keys.map do |wft_id|
[ wft_id, Hash[customer_repository.keys.map {|c_id| [ c_id, 0 ]}] ]
end
]
@vms = []
vmid = 0
vm_allocation.each do |opts|
stdist = @configuration.service_component_types[opts[:component_type]][:service_time_distribution]
opts[:vm_num].times do
vm = VM.new(vmid, opts[:dc_id], opts[:vm_size], stdist)
@vms << vm
unless data_center_repository[opts[:dc_id]].add_vm(vm, opts[:component_type])
$stderr.puts "====== Unfeasible allocation at data center #{dc_id} ======"
$stderr.flush
return Float::MAX
end
vmid += 1
end
end
@event_queue = SortedArray.new
rg = RequestGenerator.new(@configuration.request_generation)
req_attrs = rg.generate
new_event(Event::ET_REQUEST_GENERATION, req_attrs, req_attrs[:generation_time], nil)
unless @configuration.end_time.nil?
new_event(Event::ET_END_OF_SIMULATION, nil, @configuration.end_time, nil)
end
warmup_threshold = @configuration.start_time + @configuration.warmup_duration.to_i
requests_being_worked_on = 0
requests_forwarded_to_other_dcs = 0
current_event = 0
until @event_queue.empty?
e = @event_queue.shift
current_event += 1
if @current_time > e.time
raise "Error: simulation time inconsistency for event #{current_event} " +
"e.type=#{e.type} @current_time=#{@current_time}, e.time=#{e.time}"
end
@current_time = e.time
case e.type
when Event::ET_REQUEST_GENERATION
req_attrs = e.data
customer_location_id = customer_repository.dig(req_attrs[:customer_id], :location_id)
dc_at_customer_location = data_center_repository.values.find {|dc| dc.location_id == customer_location_id }
raise "No data center found at location id #{customer_location_id}!" unless dc_at_customer_location
workflow = workflow_type_repository[req_attrs[:workflow_type_id]]
first_component_name = workflow[:component_sequence][0][:name]
closest_dc = if dc_at_customer_location.has_vms_of_type?(first_component_name)
dc_at_customer_location
else
data_center_repository.values.select{|dc| dc.has_vms_of_type?(first_component_name) }&.sample
end
raise "Invalid configuration! No VMs of type #{first_component_name} found!" unless closest_dc
arrival_time = @current_time + @latency_manager.sample_latency_between(customer_location_id, closest_dc.location_id)
new_req = Request.new(req_attrs.merge!(initial_data_center_id: closest_dc.dcid,
arrival_time: arrival_time))
new_event(Event::ET_REQUEST_ARRIVAL, new_req, arrival_time, nil)
req_attrs = rg.generate
new_event(Event::ET_REQUEST_GENERATION, req_attrs, req_attrs[:generation_time], nil)
when Event::ET_REQUEST_ARRIVAL
req = e.data
data_center = data_center_repository[req.data_center_id]
reqs_received_per_workflow_and_customer[req.workflow_type_id][req.customer_id] += 1
workflow = workflow_type_repository[req.workflow_type_id]
next_component_name = workflow[:component_sequence][req.next_step][:name]
vm = data_center.get_random_vm(next_component_name)
new_event(Event::ET_REQUEST_FORWARDING, req, e.time, vm)
if req.arrival_time > warmup_threshold
requests_being_worked_on += 1
stats.request_received
per_workflow_and_customer_stats[req.workflow_type_id][req.customer_id].request_received
end
when Event::ET_REQUEST_FORWARDING
req = e.data
time = e.time
vm = e.destination
vm.new_request(self, req, time)
when Event::ET_WORKFLOW_STEP_COMPLETED
req = e.data
vm = e.destination
vm.request_finished(self, e.time)
data_center = data_center_repository[req.data_center_id]
workflow = workflow_type_repository[req.workflow_type_id]
if req.next_step < workflow[:component_sequence].size
next_component_name = workflow[:component_sequence][req.next_step][:name]
new_vm = data_center.get_random_vm(next_component_name)
forwarding_time = e.time
unless new_vm
other_dcs = data_center_repository.values.select{|x| x != data_center && x.has_vms_of_type?(next_component_name) }&.shuffle
other_dcs.each do |dc|
new_vm = dc.get_random_vm(next_component_name)
if new_vm
req.data_center_id = dc.dcid
transmission_time =
@latency_manager.sample_latency_between(data_center.location_id,
dc.location_id)
unless transmission_time >= 0.0
raise "Negative transmission time (#{transmission_time})!"
end
req.update_transfer_time(transmission_time)
forwarding_time += transmission_time
req.data_center_id = dc.dcid
requests_forwarded_to_other_dcs += 1
break
end
end
end
raise "Cannot find VM running a component of type " +
"#{next_component_name} in any data center!" unless new_vm
new_event(Event::ET_REQUEST_FORWARDING, req, forwarding_time, new_vm)
else transmission_time =
@latency_manager.sample_latency_between(
data_center_repository[req.data_center_id].location_id,
customer_repository.dig(req.customer_id, :location_id)
)
unless transmission_time >= 0.0
raise "Negative transmission time (#{transmission_time})!"
end
req.update_transfer_time(transmission_time)
new_event(Event::ET_REQUEST_CLOSURE, req, e.time + transmission_time, nil)
end
when Event::ET_REQUEST_CLOSURE
req = e.data
req.finished_processing(e.time)
if req.arrival_time > warmup_threshold
requests_being_worked_on -= 1
stats.record_request(req)
per_workflow_and_customer_stats[req.workflow_type_id][req.customer_id].record_request(req)
end
when Event::ET_END_OF_SIMULATION
break
end
end
costs = @evaluator.evaluate_business_impact(stats, per_workflow_and_customer_stats,
vm_allocation)
puts "====== Evaluating new allocation ======\n" +
"costs: #{costs}\n" +
"vm_allocation: #{vm_allocation.inspect}\n" +
"stats: #{stats.to_s}\n" +
"per_workflow_and_customer_stats: #{per_workflow_and_customer_stats.to_s}\n" +
"=======================================\n"
fitness = - costs.values.inject(0.0){|s,x| s += x }
end
|