forked from Altiscale/burstingsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld2.rb.bk
2036 lines (1713 loc) · 70.8 KB
/
world2.rb.bk
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 'state_updater'
require_relative 'modules/constants'
require_relative 'lru'
require_relative 'modules/errors'
# 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,
:running_tasks_count,
:waiting_tasks_count,
:last_processed_timestamp,
:time_created,
:ticks,
:last_tick_change,
:granularity,
:inventory_hosts_count,
: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)
@observers = []
@processed_clusters = []
@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 update_stats(timestamp)
@collocs.each_value { |c| c.update_stats(timestamp) }
end
def print_stats
@collocs.each do |name, c|
puts "#{name}:"
c.print_stats
end
end
def timestamp_changed_signal(timestamp)
timestamp_changed(timestamp)
if timestamp > @creation_time
now = Time.now
if (@ticks % @granularity == 0)
@logger.info "Hours processed: #{@ticks / @granularity} - \
Processing time: #{now - @last_tick_change} - \
Current timestamp: #{timestamp}"
print_stats
end
@ticks += 1
@last_tick_change = now
# 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
update_stats(timestamp)
end
end
def update(event)
if event.is_a?(ControlEvent) && event.message == Constants::ControlMessages::SIM_IS_OVER
puts "Received #{event.message} event!"
return
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(
colloc_name, event.cluster_name, event.host_name,
event.job_id, event.task_status)
# Tracking the affected resources for the whole timestamp
@processed_clusters << cluster
@processed_hosts << host
@processed_jobs << job
@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)
update_lru_cache(event.timestamp, host_job_status_key, host_job_key, cluster_job_key)
# 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 object have
# to update their internal state to be consistent
# Getting 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)
scheduling_event.print
puts "Processing event"
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 get_affected_resources(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]
else
host = Host.new(self, colloc, host_name)
host.configure(self, cluster, task_status) # Will be registered with a cluster here
end
job = (cluster.has_job(job_id)) ? cluster.get_job(job_id) : Job.new(cluster, job_id, cluster_name)
[colloc, cluster, host, job]
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 update_host_registration(host, cluster_name)
register_host_with_cluster(host, cluster_name)
end
def get_full_clusters
@collocs.each_values do |col|
full_clusters.merge(col.full_clusters)
end
full_clusters
end
private
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 send_timestamp_changed_signals(timestamp)
return if @processed_clusters.nil?
#TODO: Check order
#@processed_jobs.events_processed_signal(timestamp)
#@processed_hosts.events_processed_signal(timestamp)
send_timestamp_changed_to_processed_resource(timestamp, @processed_clusters)
#send_timestamp_changed_to_processed_resource(timestamp, @processed_hosts)
#send_timestamp_changed_to_processed_resource(timestamp, @processed_jobs)
@processed_clusters = []
@processed_hosts = []
@processed_jobs = []
end
def send_timestamp_changed_to_processed_resource(timestamp, resource)
return if resource.nil?
resource.each do |r|
r.events_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_jobs_on_cluster = @last_seen_jobs_on_cluster.expire(timestamp)
# Order of the following two is important! If task counters are updated first then job status
# will be changed before cluster gets the job_finished_signal and so cluster job counters will
# not be updated correctly
reset_task_statuses_count(not_existing_task_statuses_of_job_on_host)
update_finished_jobs(finished_jobs_on_cluster, finished_jobs_on_host)
end
def update_lru_cache(timestamp, host_job_status_key, host_job_key, cluster_job_key)
@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
end
def host_changed_cluster?(host_name, event_cluster_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)
host.update(event, task_diff, cluster)
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 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
return true
end
false
end
def process_eviction_event(scheduling_event)
puts "Eviction"
end
def process_provision_event(scheduling_event)
puts "Provision"
# Do this in appropriate order: Move from moving to free on colloc and register node with cluster
# Create "provisioned" hosts on cluster. This will be used to assign tasks on these free nodes
# Provisioned hosts can be on free or busy status
# Flag clusters that have provisioned hosts
# When receiving an event with task in requested status
# Add memory capacity to a cluster
# If a flagged cluster
# Create running events and assign to "provisioned" hosts
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
def reset_task_statuses_count(not_existing_task_statuses_of_job_on_host)
return unless !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)
# Diff and 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
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
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_on_cluster(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 { |j| @clusters[j.cluster_name].job_finished_signal(j.job_id) }
end
def update_finished_jobs_on_host(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 |j|
host_job_key = HostJobKey.new(j.cluster_name, j.host_name, j.job_id)
job_on_host_task_status_counters = get_tasks_map_with_key(host_job_key)
@clusters[j.cluster_name].job_done_on_host_signal(\
@last_processed_timestamp,\
j.job_id,\
j.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 update_finished_jobs(finished_jobs_on_cluster, finished_jobs_on_host)
update_finished_jobs_on_cluster(finished_jobs_on_cluster)
update_finished_jobs_on_host(finished_jobs_on_host)
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,
:free_hosts
attr_accessor :name,
:busy_hosts,
:clusters,
:avg_utilization,
:ticks
def initialize(world, name)
@name = name
@free_hosts = Hash.new
@busy_hosts = Hash.new
@full_clusters = Hash.new
@clusters = Set.new
@total_utilization = 0.0
@ticks = 0 #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)
@moving_hosts = Hash.new
@logger = Logger.new(STDOUT)
@logger.level = Logger::ERROR
end
def update_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): \
#{free_hosts_count} - #{busy_hosts_count} - #{inventory_hosts_count}"
puts "Utilization (current - avg): \
#{current_utilization} - #{avg_utilization} - Ticks: #{@ticks}"
# sleep 1
end
def avg_utilization
@total_utilization / @ticks
end
def current_utilization
(all_hosts_count > 0) ? busy_hosts_count.to_f / all_hosts_count : 'UNDEF'
end
def moving_from_inventory(host_names)
return if host_names.nil? || host_names.empty?
host_names.each do |host_name|
host = @inventory_hosts.delete(host_name)
host.in_transit_signal(Constants::HostStatus::MOVING)
@moving_hosts[host_name] = host
end
end
def moving_to_inventory(host_names)
return if host_names.nil? || host_names.empty?
host_names.each do |host_name|
host = @free_hosts.delete(host_name)
host.in_transit_signal(Constants::HostStatus::MOVING)
fail MovingBusyHostError, "Moving a busy host" if host.nil?
@moving_hosts[host_name] = host
end
end
def full_clusters_clone
@full_clusters.clone
end
def inventory_hosts_clone
@inventory_hosts.clone
end
def free_hosts_clone
@free_hosts.clone
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 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
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_name, prev_status, current_status)
# Signal received from Host
@logger.debug "#{host_name} - #{prev_status} --> #{current_status}"
host = case prev_status
when Constants::HostStatus::FREE
@free_hosts[host_name]
when Constants::HostStatus::BUSY
@busy_hosts[host_name]
when Constants::HostStatus::INVENTORY
@inventory_hosts[host_name]
else
fail UnsupportedHostStatusError, 'Host status unknown or unsupported!'
end
host_status_updated(host, current_status)
end
def print_summary
puts "--> Colloc: #{name}"
puts "- Hosts: Busy(#{busy_hosts_count}) - Free(#{free_hosts_count})"
end
def print_all
print_summary
puts 'Free hosts:'
if @free_hosts.empty?
puts 'None!'
else
@free_hosts.each_key { |k| puts k }
end
puts 'Busy hosts:'
if @busy_hosts.empty?
puts 'None!'
else
@busy_hosts.each_key { |k| puts k }
end
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)
(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, status)
case status
when Constants::HostStatus::INVENTORY
host_went_back_to_inventory(host)
when Constants::HostStatus::FREE
host_status_changed_to_free(host)
else
host_status_changed_to_busy(host)
end
end
def host_went_back_to_inventory(host)
add_to_inventory_hosts(host)
(is_on_free_hosts) ? remove_from_free_hosts(host) : remove_from_busy_hosts(host)
end
def host_status_changed_to_busy(host)
add_to_busy_hosts(host)
(is_on_free_hosts(host)) ? remove_from_free_hosts(host) : remove_from_inventory_hosts(host)
end
def host_status_changed_to_free(host)
add_to_free_hosts(host)
(is_on_busy_hosts(host)) ? remove_from_busy_hosts(host) : remove_from_inventory_hosts(host)
end
def add_to_clusters(cluster)
@clusters.add(cluster.name)
end
def add_to_full_clusters(cluster)
@full_clusters[cluster.name] = cluster
end
def remove_from_full_clusters(cluster)
@full_clusters.delete(cluster.name)
end
def add_to_inventory_hosts(host)
@inventory_hosts[host.name] = host
end
def remove_from_inventory_hosts(host)
@inventory_hosts.delete(host.name)
end
def add_to_free_hosts(host)
@free_hosts[host.name] = host
end
def remove_from_free_hosts(host)
@free_hosts.delete(host.name)
end
def add_to_busy_hosts(host)
@busy_hosts[host.name] = host
end
def remove_from_busy_hosts(host)
@busy_hosts.delete(host.name)
end
def is_on_free_hosts(host)
@free_hosts.include?(host.name)
end
def is_on_busy_hosts(host)
@busy_hosts.include?(host.name)
end
end
# Mimics YARN cluster behavior. Keeps track of the submitted jobs and their statuses,
# waiting/ running tasks, and free/busy hosts.
class Cluster
# Either keep free/busy as sets containing only host_names or completely remove hosts hash
attr_accessor :name,
:colloc_name,
:hosts,
:jobs,
:busy_hosts,
:free_hosts,
:task_status_counters,
:full # true if free_hosts is empty and cluster has at least one waiting task
def initialize(world, name, colloc)
@name = name
@colloc = colloc
@hosts = Hash.new(0)
@free_hosts = Hash.new(0)
@busy_hosts = Hash.new(0)
@jobs = Hash.new(0)
@job_status_counters = JobStatusCounters.new(@name) # ex: <RUNNING, 10>, <WAITING, 20>...
@task_status_counters = TaskStatusCounters.new('cluster', @name)
@full = is_full?
world.register_cluster(self, colloc.name) # Let the world know a new cluster was born!
@logger = Logger.new(STDOUT)
@logger.level = Logger::INFO
end
def update(event, task_diff)
fail ArgumentError, "Event not targeting this cluster (Found: #{event.cluster_name} \
- Expecting: #{@name})" unless event.cluster_name == @name
# No need to register job to host. Job will be be added in running jobs of host on host.update
task_status = event.task_status
begin
# Add diff calculated for the status of this specific Job to the total count for
# this status among all jobs on this cluster
@task_status_counters.add_value_to_status(task_status, task_diff)
rescue => e
@logger.error "Tasks on cluster cannot be less than zero! \
Found: #{@task_status_counters.get_count_of_status(task_status)} for cluster: #{@name}"
@logger.error 'Cluster object status:'
@logger.error "#{inspect}"
@logger.error e.message
e.backtrace.each { |line| @logger.error line }
@logger.error e.backtrace
raise
end
# Need jobs counters and free/busy hosts to be updated? NO...
# 1) job_status_counters are updated through job_status_changed_signal
# 2) host status is updated with host_status_changed_signal
#send_cluster_status_changed_signal if cluster_status_changed?
# Sanity checks
=begin
if (!@full and @task_status_counters.waiting_count > 0)
@logger.warn "#{event.timestamp}: #{name} has #{@task_status_counters.waiting_count} \
waiting tasks but not full!"
if @logger.warn
puts "free:"
@free_hosts.each_key { |h| puts h}
puts "busy:"
@busy_hosts.each_key { |h| puts h}
end
#fail InconsistentStateError, "Cluster has #{@task_status_counters.waiting_count} waiting tasks but not full!"
end
=end
end
def register_job(job)
fail StandardError, "Job with id: #{job.id} already exists!" if has_job(job.id)
add_job(job)
@job_status_counters.increase_count_of_status(job.status)
# Job will be registered with host on cluster update
end
def events_processed_signal(timestamp)
send_cluster_status_changed_signal if cluster_status_changed?
if busy_hosts_count > 0 && @job_status_counters.running_count == 0
@logger.error 'Busy Hosts:'
@busy_hosts.each_key { |k| puts k } if @logger.error?
@logger.error 'job counters'
@job_status_counters.print
@logger.error 'task counters'
@task_status_counters.print
fail InconsistentStateError, \
"Cluster #{@name} - timestamp: #{timestamp}: Busy hosts = #{busy_hosts_count} \
and running job status counters=#{@job_status_counters.running_count}"
end
if (@job_status_counters.running_count == 0 && busy_hosts_count > 0) \
|| (@job_status_counters.running_count > 0 && busy_hosts_count == 0)
fail InconsistentStateError, "Cluster #{@name} - timestamp: #{timestamp}: Running job counters: #{@job_status_counters.running_count} \
but busy hosts count: #{busy_hosts_count}"
end
fail InconsistentStateError, 'No Free hosts or Busy hosts on cluster!' \
unless free_hosts_count > 0 || busy_hosts_count > 0
if (!@full and @task_status_counters.waiting_count > 10)
@logger.warn " #{@timestamp} - #{@name} has #{@task_status_counters.waiting_count} \
waiting tasks but not full!"
if @logger.warn
puts "free:"
@free_hosts.each_key { |h| puts h}
puts "busy:"
@busy_hosts.each_key { |h| puts h}
end
#fail InconsistentStateError, "Cluster has #{@task_status_counters.waiting_count} waiting tasks but not full!"
end
end
def host_status_changed_signal(host_name, prev_status, current_status)
# Signal received from Host
@logger.debug "#{host_name} - #{prev_status} --> #{current_status}"
host_status_updated(host_name, current_status)
end
def task_status_update_signal(host_name, job_id, task_status, diff)
@task_status_counters.add_value_to_status(task_status, diff)
send_cluster_status_changed_signal if cluster_status_changed?
@jobs[job_id].task_status_update_signal(task_status, diff)
@hosts[host_name].task_status_update_signal(task_status, diff)
end
def job_done_on_host_signal(timestamp, job_id, host_name, job_on_host_task_status_counters)
# Signal received from World
puts "#{timestamp}: #{job_id} done on #{host_name}!" if @logger.debug?
if @logger.debug?
puts job_on_host_task_status_counters.map.inspect
end
update_task_status_counters(job_on_host_task_status_counters.map)
@jobs[job_id].done_on_host_signal(host_name, job_on_host_task_status_counters.map)
# Job not necessarily in running state on host. Host unaware of a job in "REQUESTED" state
@hosts[host_name].job_not_running_signal(timestamp, job_id, job_on_host_task_status_counters.map)
end
def job_finished_signal(job_id)
# Signal received from World
initial_value = @job_status_counters.get_count_of_status(Constants::JobStatus::FINISHED)
puts "#{job_id} finished!" if @logger.debug?
job = @jobs[job_id]
prev_status = job.status # Job status not updated yet - Cluster will trigger done_on_cluster_signal of job later
new_status = Constants::JobStatus::FINISHED
update_job_status_counters(prev_status, new_status)
update_task_status_counters(job.task_status_counters.map)
# end
@task_status_counters.reset_counters unless @job_status_counters.active_on_cluster_count > 0
send_cluster_status_changed_signal if cluster_status_changed?
job.done_on_cluster_signal # Send signal to affected job
final_value = @job_status_counters.get_count_of_status(Constants::JobStatus::FINISHED)
if(final_value != (initial_value + 1))
fail InconsistentStateError, "Expected: #{initial_value + 1} - Found: #{final_value}"
end
# No need to send signal to each host. Will be updated from job_not_running signals
end
def job_status_changed_signal(old_status, new_status)
# We deal with FINISHED jobs through job_finished_signal only!
return unless new_status != Constants::JobStatus::FINISHED
# Signal received from Job
update_job_status_counters(old_status, new_status)
end
def register_host(host)
if(host.status == Constants::HostStatus::INVENTORY)
# A host in inventory shouldn't be registered with a cluster
fail InconsistentStateError, "Host #{host.name} status is INVENTORY but tried to register with cluster #{name}"
end
(host.status == Constants::HostStatus::FREE) ? add_to_free_hosts(host) : add_to_busy_hosts(host)
@hosts[host.name] = host
send_cluster_status_changed_signal if cluster_status_changed?
if (is_on_free_hosts(host.name) && is_on_busy_hosts(host.name))
fail InconsistentStateError, "Registered host #{host.name} in both free and busy lists!"
end
end
def deregister_host(host_name)
begin
fail HostNotInCluster, "Host: #{host_name} doesn't exist on #{cluster_name}!" unless has_host(host_name)