Class: Bosh::Director::InstanceReuser
- Defined in:
- lib/bosh/director/instance_reuser.rb
Instance Method Summary collapse
- #add_in_use_instance(instance, stemcell) ⇒ Object
- #each ⇒ Object
- #get_instance(stemcell) ⇒ Object
- #get_num_instances(stemcell) ⇒ Object
-
#initialize ⇒ InstanceReuser
constructor
A new instance of InstanceReuser.
- #release_instance(instance) ⇒ Object
- #remove_idle_instance_not_matching_stemcell(stemcell) ⇒ Object
- #remove_instance(instance) ⇒ Object
- #total_instance_count ⇒ Object
Constructor Details
#initialize ⇒ InstanceReuser
Returns a new instance of InstanceReuser.
5 6 7 8 9 |
# File 'lib/bosh/director/instance_reuser.rb', line 5 def initialize @idle_instances_by_stemcell = {} @in_use_instances_by_stemcell = {} @mutex = Mutex.new end |
Instance Method Details
#add_in_use_instance(instance, stemcell) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/bosh/director/instance_reuser.rb', line 11 def add_in_use_instance(instance, stemcell) raise NilInstanceError if instance.nil? canonical_stemcell = canonical(stemcell) @mutex.synchronize do @in_use_instances_by_stemcell[canonical_stemcell] ||= [] @in_use_instances_by_stemcell[canonical_stemcell] << instance end end |
#each ⇒ Object
80 81 82 83 84 85 |
# File 'lib/bosh/director/instance_reuser.rb', line 80 def each all_instances = (@idle_instances_by_stemcell.values + @in_use_instances_by_stemcell.values).flatten all_instances.each do |instance| yield instance end end |
#get_instance(stemcell) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bosh/director/instance_reuser.rb', line 20 def get_instance(stemcell) canonical_stemcell = canonical(stemcell) @mutex.synchronize do return nil if @idle_instances_by_stemcell[canonical_stemcell].nil? instance = @idle_instances_by_stemcell[canonical_stemcell].pop return nil if instance.nil? @in_use_instances_by_stemcell[canonical_stemcell] ||= [] @in_use_instances_by_stemcell[canonical_stemcell] << instance return instance end end |
#get_num_instances(stemcell) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/bosh/director/instance_reuser.rb', line 62 def get_num_instances(stemcell) canonical_stemcell = canonical(stemcell) @mutex.synchronize do idle_count = @idle_instances_by_stemcell.fetch(canonical_stemcell, []).size in_use_count = @in_use_instances_by_stemcell.fetch(canonical_stemcell, []).size idle_count + in_use_count end end |
#release_instance(instance) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/bosh/director/instance_reuser.rb', line 32 def release_instance(instance) raise NilInstanceError if instance.nil? @mutex.synchronize do release_without_lock(instance) end end |
#remove_idle_instance_not_matching_stemcell(stemcell) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bosh/director/instance_reuser.rb', line 51 def remove_idle_instance_not_matching_stemcell(stemcell) canonical_stemcell = canonical(stemcell) @mutex.synchronize do @idle_instances_by_stemcell.each do |stemcell, idle_instances| if stemcell != canonical_stemcell && !idle_instances.empty? return idle_instances.pop end end end end |
#remove_instance(instance) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bosh/director/instance_reuser.rb', line 39 def remove_instance(instance) raise NilInstanceError if instance.nil? @mutex.synchronize do release_without_lock(instance) @idle_instances_by_stemcell.each_value do |idle_instances| idle_instances.each do |idle_instance| idle_instances.delete(idle_instance) if instance == idle_instance end end end end |
#total_instance_count ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/bosh/director/instance_reuser.rb', line 71 def total_instance_count @mutex.synchronize do sum = 0 @idle_instances_by_stemcell.values.each{|instances| sum += instances.to_a.count } @in_use_instances_by_stemcell.values.each{|instances| sum += instances.to_a.count } sum end end |