forked from Altiscale/burstingsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.rb
2455 lines (2078 loc) · 89.7 KB
/
world.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'set'
require 'logger'
require 'csv'
require 'observer'
require_relative 'events/input_log_event'
require_relative 'events/synthetic_event'
require_relative 'state_updater'
require_relative 'modules/constants'
require_relative 'lru'
require_relative 'modules/errors'
require_relative 'yarn_scheduler'
# Every resource (cluster, host, colloc or anything else added in the future)
# is getting registered with the world
# at the time of the creation on its constructor.
# World is responsible to register the new resource with all the related
# entities on its "register" method.
# Similarly a job will register with the cluster and the cluster is
# responsible to register the job with the host.
# Jobs are not directly accessed through the world as they cannot exist
# without a cluster.
# methods with _signal postfix determine changes to internal state required
# by an external event that the object itself is not aware.
# For example finished jobs are determined by world and then signals are
# send to clusters, hosts and jobs
class World
include Observable
attr_accessor :clusters,
:hosts,
:collocs,
# <(cluster_name, host_name, job_id), Task<Status, Count>>
:tasks_map,
#:hosts_cluster_map, #Mappings of hosts to clusters
:last_seen_jobs_on_host,
:last_seen_jobs_on_cluster,
:last_seen_augmented_jobs_on_host,
:last_seen_augmented_jobs_on_cluster,
:finished_augm_jobs_signals_map,
:running_tasks_count,
:waiting_tasks_count,
:last_processed_timestamp,
:ticks,
:last_tick_change,
:granularity,
:inventory_hosts_count,
:events_received,
:events_received_this_timestamp,
:observers
def initialize(conf)
configure(conf)
@clusters = Hash.new(0)
@collocs = Hash.new(0)
@hosts = Hash.new(0)
@tasks_map = Hash.new(0)
@ticks = 0
@last_tick_change = 0
@granularity = Constants::Numeric::GRANULARITY
# For example ACQUIRED status for a particular job running on a host
@last_seen_task_statuses_of_job_on_host = Lru.new(-1, @granularity)
# Keep unlimited job keys that are not older than granularity seconds
# than the current timestamp
@last_seen_jobs_on_host = Lru.new(-1, @granularity)
@last_seen_jobs_on_cluster = Lru.new(-1, @granularity)
@last_seen_augmented_jobs_on_host = Lru.new(-1, @granularity)
# Keep separate track of augmented_jobs. When a job is finished on the augmented hosts
# we send signals to explicitely finish everything else still running
@last_seen_augmented_jobs_on_cluster = Lru.new(-1, @granularity)
# Keep track of the finished jobs signals so we can ignore updates for these jobs and also
# avoid re-sending finished signals for augmented jobs that can lead to inconsistencies
# finished_augm_jobs_signals_map <cluster_job_key, true> , true if signal already send
@finished_augm_jobs_signals_map = Hash.new
@observers = []
@events_received = 0
@events_received_this_timestamp = 0
@processed_clusters = {}
@processed_hosts = {}
@processed_jobs = {}
# job_metrics and hosts_metrics are imported from a file to estimate the max number of
# tasks that can fit to a provisioned host and the avg time needed to execute the task
@jobs_metrics = {} # <cluster_name, job_id, avg_task_size, avg_run_time>
@hosts_metrics = {} # <host_name, memory_size>
@logger = Logger.new(STDOUT)
@logger.level = Logger::INFO
end
def print_summary
puts '------------------ Summary -------------------'
puts "Clusters count: #{@clusters.size}"
puts "Hosts count: #{@hosts.size}"
puts "Collocs: #{get_collocs_names}"
puts "Clusters: #{get_clusters_names}"
@collocs.each { |_name, colloc| colloc.print_summary }
@clusters.each { |_name, cluster| cluster.print_summary }
end
def print_all
puts '------------- Collocs ------------------------'
@collocs.each { |_name, colloc| colloc.print_all }
puts "\n"
puts '------------- Clusters -----------------------'
@clusters.each do |_name, cluster|
# cluster.print_all
# cluster.print_summary
puts "\n"
cluster.print(true, false, false, false)
end
end
def create_stats(timestamp)
@collocs.each_value { |c| c.create_stats(timestamp) }
end
def print_stats
@collocs.each do |name, c|
puts "#{name}:"
c.print_stats
end
end
# Method called externally by the world_state object whenever the timestamp increases
def timestamp_changed_signal(timestamp)
timestamp_changed(timestamp)
if timestamp > @creation_time
if (@ticks % @granularity == 0) # Calculate every 1 hour
now = Time.now
@logger.info "Current time: #{timestamp} - #{Time.at(timestamp)}"
@logger.info "Hours processed: #{@ticks / @granularity} - events: #{@events_received}"
processing_time = 0
processing_time = (now - @last_tick_change).to_f unless @last_tick_change == 0
@logger.info "Last hour processed in (sec): #{processing_time}"
unless processing_time == 0
@logger.info "Events/sec (this hour): #{@events_received_this_timestamp/processing_time}"
end
print_stats
@last_tick_change = now
@events_received_this_timestamp = 0
end
@ticks += 1
# Send when all events of the timestamp are processed
send_finished_signals(timestamp)
send_timestamp_changed_signals(timestamp)
# Get values only when the timestamp changes to make sure all events
# of the same timestamp are processed
# print_offer_and_demand
create_stats(timestamp)
@logger.debug "*** Timestamp #{timestamp-60} processed!\n"
end
end
def send_timestamp_changed_signals(timestamp)
#@processed_jobs.timestamp_processed_signal(timestamp)
#@processed_hosts.timestamp_processed_signal(timestamp)
# Order is important! Hosts should update their status first so the can send the
# host_status_changed signal to the cluster before the cluster runs its own
# timestamp_changed method that includes sanity checks
send_timestamp_changed_to_processed_resource(timestamp, @processed_jobs)
send_timestamp_changed_to_processed_resource(timestamp, @processed_hosts)
send_timestamp_changed_to_processed_resource(timestamp, @processed_clusters)
@processed_clusters = {}
@processed_hosts = {}
@processed_jobs = {}
end
def update(state_updater, event)
@logger.level = Logger::INFO
@events_received += 1
@events_received_this_timestamp += 1
if event.is_a?(ControlEvent)
if (event.message == Constants::ControlMessages::SIM_IS_OVER)
@logger.info "Received '#{event.message}' event!"
return
elsif (event.message == Constants::ControlMessages::GAP)
@logger.debug "Received '#{event.message}' event with timestamp: #{event.timestamp}!"
return
end
elsif (event.is_a?(SchedulingEvent))
process_scheduling_event(event)
return #Do nothing at the moment
end
# OPTIMIZE: Instead of treating finished jobs differently I could create
# finished events and run update methods on all affected entities
# Sending finished signals should preceed the scheduling decisions
# so it cannot be an observer to the world_state like the scheduler is.
colloc_name = event.host_name.split('.')[1]
# Create objects if they don't already exist in the world
colloc, cluster, host, job = get_affected_resources(event.timestamp,
colloc_name,
event.cluster_name,
event.host_name,
event.job_id,
event.task_status)
@logger.debug 'Updating state with event:'
event.print if @logger.debug?
# Update tasks_map and get diff from previous state
cluster_job_key = ClusterJobKey.new(event.cluster_name, event.job_id)
host_job_key = HostJobKey.new(event.cluster_name, event.host_name, event.job_id)
# Have to track the statuses that appear on each minute, so they can be reseted to zero when
# they don't appear for a minute.
# There still will be 1min delay to update the status of a task that is going for example from
# acquired to running state on the same minute. We don't have TASK IDs on the input -
# so we cannot be sure about task status transitions.
host_job_status_key = HostJobStatusKey.new(event.cluster_name,
event.host_name,
event.job_id,
event.task_status)
# Ignore input events coming for a finished augmented job
# TODO: Create host_job_keys for all hosts of the cluster and delete. Without that
# the host_job_key has to appear before being able to delete
if (@finished_augm_jobs_signals_map.has_key?(cluster_job_key) &&
@finished_augm_jobs_signals_map[cluster_job_key])
@logger.debug "Ignoring..."
cluster_job_key.print if @logger.debug?
# delete_from_lru_cache(host_job_status_key, host_job_key, cluster_job_key)
return
end
# Add resources that are processed on this timestamp
# in order to send to all of them the timestamp_processed_signal
# that triggers them to update their status according to their logic
@processed_clusters[cluster.name] = cluster
@processed_hosts[host.name] = host unless event.task_status == Constants::TaskStatus::REQUESTED
@processed_jobs[job.id] = job
# Job on this host is already augmented by running tasks on a provisioned host
if(cluster.yarn_scheduler.augments_job_on_host(host_job_key) &&
event.task_status == Constants::TaskStatus::REQUESTED)
event.task_count = cluster.yarn_scheduler.calc_req_event_count(host_job_key, event.task_count)
@logger.debug "Remaining waiting tasks (after delegations): #{event.task_count}"
if (event.task_count == 0)
@logger.debug "Ignoring event..."
event.print if @logger.debug?
return
end
end
# For synthetic events the task_count is -1 until we give it the actual value at this point
# See comments on yarn_scheduler.generate_run_event to see why is this so
if (event.is_a?(SyntheticEvent))
event.task_count = cluster.yarn_scheduler.calc_run_event_count(event)
@logger.debug "Synthetic event:"
event.print if @logger.debug?
end
# Deal with waiting tasks when additional provisioned resources exist
if (event.task_status == Constants::TaskStatus::REQUESTED && \
cluster.provisioned_hosts_count > 0)
synthetic_events, wait_count = cluster.yarn_scheduler.delegate_event(event,
host_job_key)
if (synthetic_events.size > 0)
@logger.debug "Delegated tasks for #{job.id}:"
synthetic_events.each do |e|
e.print if @logger.debug?
end
job.augmented = true # Sufficient capacity on host to augment part of these job's tasks
end
event.task_count = wait_count # Update the event with the remaining waiting tasks
cluster.yarn_scheduler.enqueue_events(state_updater, synthetic_events)
# If there are no events left in REQUESTED status no need to process the event
if (wait_count == 0)
@logger.debug "All tasks for #{job.id} delegated!"
return
else
@logger.debug "Resources not sufficient to delegate all tasks! Remainder: #{wait_count}"
end
end
virtual = (job.augmented && !host.nil? && host.provisioned)? true : false
update_lru_cache(event.timestamp, host_job_status_key, host_job_key, cluster_job_key, virtual)
# Host changed! Make changes before updates happen
if host_changed_cluster?(event.host_name, event.cluster_name)
move_host_to_cluster(event.host_name, event.cluster_name)
end
# The following updates should happen only after the "update_finished_jobs" is called,
# as this will trigger external changes to object and then the object has
# to update their internal state to be consistent
# Gets the difference of tasks for the particular host and job based on this event
# and compared to the previous count.
# This works only for the statuses that still have at least one task running.
# The rest are treated with signals.
task_diff = set_tasks_count(host_job_key, event.task_status, event.task_count)
update_rest_of_world(event, cluster, host, job, task_diff)
end
def process_scheduling_event(scheduling_event)
@logger.debug "Processing scheduling event..."
scheduling_event.print if @logger.debug?
if scheduling_event.is_a?(EvictionEvent)
process_eviction_event(scheduling_event)
elsif scheduling_event.is_a?(ProvisionEvent)
process_provision_event(scheduling_event)
else
fail UnknownSchedulingEventTypeError,
"Scheduling type #{scheduling_event.class} not supported!"
end
end
def register_cluster(cluster, colloc_name)
@clusters[cluster.name] = cluster
register_cluster_with_colloc(cluster, colloc_name)
end
def register_colloc(colloc)
@collocs[colloc.name] = colloc
end
def register_host(host, colloc_name)
# OPTIMIZE: Host didn't have access to cluster and colloc object at the time this method
# was implemented. Consider making registration directly from host to cluster and colloc
@hosts[host.name] = host
# OPTIMIZE: Consider doing the registrations on host, cluster or colloc updates
register_host_with_colloc(host, colloc_name)
end
def get_full_clusters
@collocs.each_values do |col|
full_clusters.merge(col.full_clusters)
end
full_clusters
end
private
### Methods related to assigning waiting tasks to provisioned hosts through YarnScheduler ###
######
def configure(conf)
@inventory_hosts_count = conf['inventory_hosts_count']
end
def register_cluster_with_colloc(cluster, colloc_name)
@collocs[colloc_name].register_cluster(cluster)
end
def register_host_with_colloc(host, colloc_name)
@collocs[colloc_name].register_host(host)
end
def register_host_with_cluster(host, cluster_name)
@clusters[cluster_name].register_host(host)
end
def deregister_host_from_cluster(host_name, cluster_name)
@clusters[cluster_name].deregister_host(host_name)
end
def get_affected_resources(timestamp, colloc_name, cluster_name, host_name, job_id, task_status)
colloc = (collocs.key?(colloc_name)) ? collocs[colloc_name] : Colloc.new(self, colloc_name)
cluster = (clusters.key?(cluster_name)) ? clusters[cluster_name] : Cluster.new(self, cluster_name, colloc)
if hosts.key?(host_name)
host = hosts[host_name]
if(host.status == Constants::HostStatus::INVENTORY \
|| host.status == Constants::HostStatus::OFFLINE)
@logger.debug "#{timestamp} Re-registering #{host.name} with cluster #{cluster.name}"
# Will be re-registered with a cluster here
unless (task_status == Constants::TaskStatus::REQUESTED)
host.configure(timestamp, cluster, task_status)
end
end
else
unless (task_status == Constants::TaskStatus::REQUESTED)
host = Host.new(self, colloc, host_name)
host.configure(timestamp, cluster, task_status) # Will be registered with a cluster here
end
end
job = (cluster.has_job(job_id)) ? cluster.get_job(job_id) : Job.new(cluster, job_id)
[colloc, cluster, host, job]
end
def send_timestamp_changed_to_processed_resource(timestamp, resource)
# Informs resources (cluster, hosts, jobs) that the timestamp changed
return if resource.nil?
resource.each_value do |r|
r.timestamp_processed_signal(timestamp)
end
end
def send_finished_signals(timestamp)
# Dealing with Jobs and Task statuses that no longer appear on the updates. For example jobs
# that are finished on a host or they have completed their execution on the cluster or a task
# status that doesn't have any task to represent it on the current timestamp
# (for example 0 tasks on acquired status)
#
# NOTE: With the current algorithm jobs are found when clock ticks 2 min after the last time
# the job was seen running. For example if the last updated is on timestamp 1420099200
# the job will be marked as finished on the first event with timestamp 1420099320
# , since at this point all event updates with timestamps 1420099260 are done!
not_existing_task_statuses_of_job_on_host = @last_seen_task_statuses_of_job_on_host.expire(timestamp)
# Process all jobs that haven't appear for more than granularity seconds on a host
finished_jobs_on_host = @last_seen_jobs_on_host.expire(timestamp)
finished_augmented_jobs_on_host = @last_seen_augmented_jobs_on_host.expire(timestamp)
finished_jobs_on_cluster = @last_seen_jobs_on_cluster.expire(timestamp)
finished_augmented_jobs_on_cluster = @last_seen_augmented_jobs_on_cluster.expire(timestamp)
# Send signals for the jobs that no longer run on provisioned hosts
unless finished_augmented_jobs_on_host.nil?
if(finished_jobs_on_host.nil?)
finished_jobs_on_host = finished_augmented_jobs_on_host
else
finished_jobs_on_host.merge(finished_augmented_jobs_on_host)
end
end
unless finished_augmented_jobs_on_cluster.nil?
if(finished_jobs_on_cluster.nil?)
finished_jobs_on_cluster = finished_augmented_jobs_on_cluster
else
finished_jobs_on_cluster.merge(finished_augmented_jobs_on_cluster)
end
end
# Keep to the global set of finished augmented jobs
unless(finished_augmented_jobs_on_cluster.nil?)
finished_augmented_jobs_on_cluster.each do |cluster_job_key|
@finished_augm_jobs_signals_map[cluster_job_key] = false # Signal not yet sent
end
end
reset_task_statuses_count(timestamp, not_existing_task_statuses_of_job_on_host)
update_finished_jobs(timestamp, finished_jobs_on_cluster, finished_jobs_on_host)
end
# Required to reset the counters for all the statuses that stop appearing apart from the RUNNING
# statuses that will be treated separately with the job_finished_signals
def reset_task_statuses_count(timestamp, not_existing_task_statuses_of_job_on_host)
return if not_existing_task_statuses_of_job_on_host.nil? ||
not_existing_task_statuses_of_job_on_host.empty?
not_existing_task_statuses_of_job_on_host.each do |t|
host_job_key = HostJobKey.new(t.cluster_name, t.host_name, t.job_id)
cluster_job_key = ClusterJobKey.new(t.cluster_name, t.job_id)
@logger.level = Logger::INFO
# Necessary to avoid reducing counters that are already reseted to zero because jobs where
# forced to finish
if (@finished_augm_jobs_signals_map.has_key?(cluster_job_key) &&
@finished_augm_jobs_signals_map[cluster_job_key] == true)
@logger.debug "Skipping..."
cluster_job_key.print if @logger.debug?
# sleep 1
next # Don't send anything for jobs already done
end
# Diff is not directly set to 0 because we want to reduce from the task
# counters of the cluster and host, that might have way more tasks on this
# state from other jobs
# puts "#{diff} - #{host_job_key.job_id} - #{host_job_key.host_name}"
# RUNNING status will be treated with finished signals
@logger.debug "#{timestamp}: Resetting #{t.task_status} for:"
host_job_key.print if @logger.debug?
if t.task_status == Constants::TaskStatus::RUNNING
@logger.debug "Skipped!"
else
diff = 0 - get_tasks_count(host_job_key, t.task_status)
set_tasks_count(host_job_key, t.task_status, 0)
@clusters[t.cluster_name].task_status_update_signal(t.host_name, t.job_id, t.task_status, diff)
end
end
end
def set_tasks_count(key, task_status, task_count)
fail RangeError, "Tasks cannot be less than zero! (Found: #{task_count})" unless task_count >= 0
@tasks_map[key] = TaskStatusCounters.new('world', key) unless @tasks_map.key?(key)
diff = task_count - @tasks_map[key].get_count_of_status(task_status)
@tasks_map[key].set_count_of_status(task_status, task_count)
diff
end
def get_tasks_count(key, task_status)
get_tasks_map_with_key(key).get_count_of_status(task_status)
end
def update_finished_jobs(timestamp, finished_jobs_on_cluster, finished_jobs_on_host)
update_finished_jobs_on_cluster(timestamp, finished_jobs_on_cluster)
update_finished_jobs_on_host(timestamp, finished_jobs_on_host)
end
def update_finished_jobs_on_cluster(timestamp, finished_jobs_on_cluster)
# finished_jobs_on_cluster is a set of unique ClusterJobKeys <cluster_name, job_id>
return if finished_jobs_on_cluster.nil? || finished_jobs_on_cluster.size == 0
finished_jobs_on_cluster.each do |cluster_job_key|
if (@finished_augm_jobs_signals_map.has_key?(cluster_job_key) &&
@finished_augm_jobs_signals_map[cluster_job_key] == true)
next
end
job_id = cluster_job_key.job_id
cluster_name = cluster_job_key.cluster_name
@logger.debug "#{timestamp}:cluster job_finished_signal for #{job_id}"
@clusters[cluster_name].job_finished_signal(job_id)
# Send finished signals to other hosts running jobs for this cluster as the end of an
# augmented job on provisioned hosts dictates the finish of the job overall
# remaining tasks on the hosts are task already served on provisioned hosts
# XXX: These hosts should only be hosts that where contained on the transformed REQUESTED
# events. Other hosts its ok to keep on running tasks as their task run_time can be greater
# than the average time that we ran on provisioned hosts or additional tasks is possible that
# where added later
if(@clusters[cluster_name].jobs[job_id].augmented)
@logger.debug "Delegated job #{job_id} done!"
@finished_augm_jobs_signals_map[cluster_job_key] = true
forced_finished_task_statuses_of_job_on_host = Set.new
other_hosts_running_job = Set.new
@clusters[cluster_name].hosts.each_value do |host|
if(host.runs_job?(job_id))
@logger.debug "Sending force-finished signals to: #{cluster_name} - #{host.name} - #{job_id}"
forced_finished_task_statuses_of_job_on_host.add(HostJobStatusKey.new(cluster_name, host.name, job_id, Constants::TaskStatus::RUNNING))
other_hosts_running_job.add(HostJobKey.new(cluster_name, host.name, job_id))
end
end
reset_task_statuses_count(timestamp, forced_finished_task_statuses_of_job_on_host)
# The provisioned hosts will receive the finished_job_on_host signal twice but its ok
# because they will ignore duplicates
update_finished_jobs_on_host(timestamp, other_hosts_running_job)
# TODO: delete all necessary LRUs - after this point there should be no updates for this job!
# delete_from_lru_cache(host_job_status_key, host_job_key, cluster_job_key)
# @last_seen_jobs_on_cluster.delete(cluster_job_key)
end
# TODO: Runs something similar to below to stop tracking anything for jobs that got augmented
# and are now finished
# delete_from_lru_cache(host_job_status_key, host_job_key, cluster_job_key)
end
end
# This method is triggered from job_hosts keys that no longer appear on the input indicating that
# the job of this key stopped running on the host that this key indicates
# Signal sent initially to the cluster so the cluster can update its internal
# task_status_counters by reducing the tasks related to this job_host and then the cluster will
# also send the relevant signals to the related job and host so they can also update their counters
# and state
def update_finished_jobs_on_host(timestamp, finished_jobs_on_host)
# finished_jobs_on_host is a set of unique HostJobKeys <cluster_name, host_name, job_id>
return if finished_jobs_on_host.nil? || finished_jobs_on_host.size == 0
finished_jobs_on_host.each do |host_job_key|
job_on_host_task_status_counters = get_tasks_map_with_key(host_job_key)
# Without the check signals might be send to hosts that have jobs on "REQUESTED" state
# - With the check on inconsistencies appear (ex: Busy hosts = 4 and running job status
# counters=0) but they will be fixed when all object are updated at the end of the timestamp
@logger.debug "#{timestamp}: job_done_on_host signal
#{host_job_key.job_id}
on #{host_job_key.host_name}
of #{host_job_key.cluster_name}"
host_job_key.print if @logger.debug?
@logger.debug "counters:"
job_on_host_task_status_counters.print if @logger.debug?
if (@clusters[host_job_key.cluster_name].jobs[host_job_key.job_id].augmented)
@logger.debug "augmented job"
end
@clusters[host_job_key.cluster_name].job_done_on_host_signal( @last_processed_timestamp,
host_job_key.job_id,
host_job_key.host_name,
job_on_host_task_status_counters)
job_on_host_task_status_counters.reset_counters # Reset world counters for this key
end
end
def delete_from_lru_cache(host_job_status_key, host_job_key, cluster_job_key)
# TODO: Obsolete - Consider removing!
@last_seen_task_statuses_of_job_on_host.delete(host_job_status_key)
@last_seen_jobs_on_host.delete(host_job_key)
@last_seen_jobs_on_cluster.delete(cluster_job_key)
end
def update_lru_cache(timestamp, host_job_status_key, host_job_key, cluster_job_key, virtual = false)
@last_seen_task_statuses_of_job_on_host[host_job_status_key] = timestamp
@last_seen_jobs_on_host[host_job_key] = timestamp
@last_seen_jobs_on_cluster[cluster_job_key] = timestamp
if virtual
@last_seen_augmented_jobs_on_host[host_job_key] = timestamp
@last_seen_augmented_jobs_on_cluster[cluster_job_key] = timestamp
end
end
def host_changed_cluster?(host_name, event_cluster_name)
return false unless @hosts.has_key?(host_name)
(@hosts[host_name].cluster_name != event_cluster_name) ? true : false
end
def move_host_to_cluster(host_name, new_cluster_name)
old_cluster_name = @hosts[host_name].cluster_name
deregister_host_from_cluster(host_name, old_cluster_name)
register_host_with_cluster(@hosts[host_name], new_cluster_name)
@logger.info "Cluster for host #{host_name} changed from: #{old_cluster_name} to: #{new_cluster_name}"
end
def update_rest_of_world(event, cluster, host, job, task_diff)
# Correct update depends on the order of updates. Job update should run first.
job.update(event, task_diff, cluster)
#Hosts are not necessarily created the time they appear because the status can be REQUESTED
host.update(event, task_diff, cluster) unless host.nil?
cluster.update(event, task_diff)
#No need to update colloc --> will be updated with signals every time host status is updated
rescue => e
@logger.error e.message
e.backtrace.each { |line| @logger.error line }
raise
end
def print_offer_and_demand
# *Used only to create a graph - not part of the code flaw
logger = Logger.new('offer_and_demand-7days.csv')
logger.level = Logger::INFO
if @collocs.key?('sc1')
offer = @collocs['sc1'].free_hosts_count
demand = @collocs['sc1'].full_clusters_count
all_clusters = @collocs['sc1'].clusters_count
jobs_waiting = waiting_jobs_of_colloc('sc1')
tasks_waiting = waiting_tasks_of_colloc('sc1')
logger.<< "#{offer},#{demand},#{all_clusters},#{jobs_waiting},#{tasks_waiting}\n"
# sanity_check_on_colloc("sc1")
end
end
def sanity_check_on_colloc(colloc_name)
@collocs[colloc_name].full_clusters.each_value do |cluster|
if cluster.free_hosts_count != 0 || (cluster.busy_hosts_count != cluster.hosts_count)
fail InconsistentStateError, "#{@last_processed_timestamp}: #{cluster_name}
- #{cluster.free_hosts_count} - #{cluster.busy_hosts_count} - #{cluster.hosts_count}"
end
end
end
def waiting_jobs_of_colloc(colloc_name)
count = 0
@collocs[colloc_name].clusters.each do |cluster_name|
count += @clusters[cluster_name].waiting_jobs_count
end
count
end
def waiting_tasks_of_colloc(colloc_name)
count = 0
@collocs[colloc_name].clusters.each do |cluster_name|
count += @clusters[cluster_name].waiting_tasks_count
end
count
end
def timestamp_changed(timestamp)
if @last_processed_timestamp.nil?
@creation_time = timestamp - @granularity
@last_processed_timestamp = @creation_time
elsif (timestamp - @last_processed_timestamp) > @granularity
@last_processed_timestamp = timestamp - @granularity
end
end
def process_eviction_event(scheduling_event)
@hosts[scheduling_event.host_name].evicted_signal(scheduling_event.timestamp)
end
def process_provision_event(scheduling_event)
host = @hosts[scheduling_event.host_name]
cluster = @clusters[scheduling_event.cluster_name]
host.provisioned_signal(scheduling_event.timestamp, cluster)
end
def get_collocs_names
@collocs.keys.join(', ')
end
def get_clusters_names
@clusters.keys.join(', ')
end
def get_tasks_map_with_key(job_host_key)
@tasks_map[job_host_key]
end
def get_hosts_from_cluster(cluster_name, status)
@clusters[cluster_name].get_hosts(status)
end
def get_hosts_from_colloc(colloc_name, status)
@collocs[colloc_name].get_hosts(status)
end
def deregister_cluster(cluster)
@clusters.delete(cluster.name)
end
def deregister_colloc(colloc)
@collocs.delete(colloc.name)
end
def deregister_host_from_world(host)
@hosts.delete[host.name]
end
def hosts_count
@hosts.size
end
def increase_running_tasks(count)
@running_tasks_count += count
end
def decrease_running_tasks(count)
@running_tasks_count -= count
end
def increase_waiting_tasks(count)
@waiting_tasks_count += count
end
def decrease_waiting_tasks(count)
@waiting_tasks_count -= count
end
# Mimics Altiscale datacenters behavior owning a number of clusters with host assigned to them
# and also a number of inventory hosts. Scheduling shouldn't be done between different collocs
# so it also servers as the logical separation for scheduling.
# Keeps track of the free/busy hosts and clusters running on capacity along with stats
# related to their utilization
# Provides the interface to the scheduler so the scheduler can build its models by knowing at
# each time what are the available free hosts and what are the clusters that utilize
# all of their resources
class Colloc
attr_reader :full_clusters,
:inventory_hosts,
:offline_hosts,
:free_hosts,
:busy_hosts,
:inventory_hosts,
:transitioning_hosts
attr_accessor :name,
:clusters,
:avg_utilization,
:ticks
def initialize(world, name)
@name = name
@free_hosts = Hash.new
@busy_hosts = Hash.new
@full_clusters = Hash.new
@clusters = Hash.new
@total_utilization = 0.0
@ticks = 1 #The number of timestamps/minutes passed since colloc existance
world.register_colloc(self) ##Let the world know a new colloc exists!
@inventory_hosts = Hash.new
@inventory_hosts = create_inventory_hosts(world)
@offline_hosts = Hash.new
@transitioning_hosts = Hash.new
@logger = Logger.new(STDOUT)
@logger.level = Logger::ERROR
end
def create_stats(_timestamp)
return unless all_hosts_count > 0
@ticks += 1
@total_utilization += current_utilization
avg_utilization
end
def print_stats
puts "Hosts (free | busy | inventory | in_transit):\
#{free_hosts_count} | #{busy_hosts_count} | #{inventory_hosts_count} | #{transitioning_hosts_count}"
puts "Utilization (current - avg): \
#{current_utilization} - #{avg_utilization}"
# sleep 1
end
def avg_utilization
@total_utilization / @ticks
end
def current_utilization
(free_hosts_count + busy_hosts_count > 0) ? busy_hosts_count.to_f / (free_hosts_count + busy_hosts_count) : 'UNDEF'
end
def moving_from_inventory(timestamp, host_names)
return if host_names.nil? || host_names.empty?
host_names.each do |host_name|
@logger.debug "#{timestamp}: Moving #{host_name} from inventory!"
host = @inventory_hosts.delete(host_name)
host.in_transit_signal(timestamp)
@transitioning_hosts[host_name] = host
end
end
def moving_to_inventory(timestamp, host_names)
return if host_names.nil? || host_names.empty?
host_names.each do |host_name|
@logger.debug "Colloc received signal to move: #{host_name} to inventory"
host = @free_hosts.delete(host_name)
fail MovingBusyHostError, "Moving a busy host" if host.nil?
@clusters[host.cluster_name].deregister_host(host_name)
host.in_transit_signal(timestamp)
@transitioning_hosts[host_name] = host
end
end
def stdev_utilization
# TODO: Implement
end
def clusters_count
@clusters.size
end
def full_clusters_count
@full_clusters.size
end
def all_hosts_count
(free_hosts_count + busy_hosts_count)
end
def free_hosts_count
@free_hosts.size
end
def busy_hosts_count
@busy_hosts.size
end
def inventory_hosts_count
@inventory_hosts.size
end
def offline_hosts_count
@offline_hosts.size
end
def transitioning_hosts_count
@transitioning_hosts.size
end
def get_free_hosts
get_hosts(Constants::HostStatus::FREE)
end
def get_busy_hosts
get_hosts(Constants::HostStatus::BUSY)
end
def get_hosts(status)
if (status == Constants::HostStatus::FREE)
@free_hosts
elsif (status == Constants::HostStatus::BUSY)
@busy_hosts
elsif (status == Constants::HostStatus::INVENTORY)
@inventory_hosts
elsif (status == Constants::HostStatus::OFFLINE)
@offline_hosts
else
raise UnsupportedHostStatusError, "Host status unknown or unsupported!"
end
end
def cluster_status_changed_signal(cluster)
(cluster.full)? add_to_full_clusters(cluster) : remove_from_full_clusters(cluster)
end
def host_status_changed_signal(host, prev_status, current_status)
# Signal received from Host
@logger.debug "Signal on #{name}: #{host.name} - #{prev_status} --> #{current_status}"
host_status_updated(host, prev_status, current_status)
end
def print_summary
puts "--> Colloc: #{name}"
puts "- Hosts: Busy(#{busy_hosts_count}) - Free(#{free_hosts_count})- Inventory(#{inventory_hosts_count})"
end
def print_all
print_summary
puts 'Free hosts:'
print_hosts(@free_hosts)
puts "Busy hosts:"
print_hosts(@busy_hosts)
puts 'Inventory hosts:'
print_hosts(@inventory_hosts)
puts 'In transit hosts:'
print_hosts(@transitioning_hosts)
puts
print_stats
end
def register_cluster(cluster)
add_to_clusters(cluster)
add_to_full_clusters(cluster) if cluster.full
end
def register_host(host)
case host.status
when Constants::HostStatus::INVENTORY
add_to_inventory_hosts(host)
when Constants::HostStatus::FREE
add_to_free_hosts(host)
else
add_to_busy_hosts(host)
end
if is_on_free_hosts(host) && is_on_busy_hosts(host)
fail InconsistentStateError, "Registered host #{host.name} in both free and busy sets!"
end
end
# Host is tightly coupled with colloc (its part of the hostname)
# No need to provide a deregister method
################################
private # all methods that follow will be made private: not accessible for outside objects
def create_inventory_hosts(world)
return if world.nil? || world.inventory_hosts_count.nil? || world.inventory_hosts_count == 0
(1..world.inventory_hosts_count).each do |seq|
host_name = 'inv' + seq.to_s + '.' + @name + '.verticloud.com'
host = Host.new(world, self, host_name)
add_to_inventory_hosts(host)
end
@inventory_hosts
end
def host_status_updated(host, prev_status, current_status)
case prev_status
when Constants::HostStatus::OFFLINE
remove_from_hosts(host.name, @offline_hosts)
when Constants::HostStatus::INVENTORY
remove_from_hosts(host.name, @inventory_hosts)
when Constants::HostStatus::FREE
remove_from_hosts(host.name, @free_hosts)
when Constants::HostStatus::BUSY
remove_from_hosts(host.name, @busy_hosts)
when Constants::HostStatus::MOVING
remove_from_hosts(host.name, @transitioning_hosts)
else
fail UnsupportedHostStatusError,
"#{host.name} status #{prev_status} not supported on colloc!"
end
case current_status
when Constants::HostStatus::OFFLINE
add_to_hosts(host, @offline_hosts)
when Constants::HostStatus::INVENTORY
add_to_hosts(host, @inventory_hosts)
when Constants::HostStatus::FREE
add_to_hosts(host, @free_hosts)
when Constants::HostStatus::BUSY
add_to_hosts(host, @busy_hosts)
when Constants::HostStatus::MOVING
add_to_hosts(host, @transitioning_hosts)
else
fail UnsupportedHostStatusError,
"#{host.name} status #{prev_status} not supported on colloc!"
end