Class: Bosh::Director::InstanceReuser

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/instance_reuser.rb

Overview

A class for maintaining Instance objects, making reusing Instances easier.

Instance Method Summary collapse

Constructor Details

#initializeInstanceReuser

Returns a new instance of InstanceReuser.



6
7
8
9
10
# File 'lib/bosh/director/instance_reuser.rb', line 6

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

Adds an instance’s information to the pool of VMs that can be reused.

Parameters:

Raises:



14
15
16
17
18
19
20
# File 'lib/bosh/director/instance_reuser.rb', line 14

def add_in_use_instance(instance, stemcell)
  raise NilInstanceError if instance.nil?
  @mutex.synchronize do
    @in_use_instances_by_stemcell[stemcell] ||= []
    @in_use_instances_by_stemcell[stemcell] << instance
  end
end

#each {|DeploymentPlan::Instance| ... } ⇒ Object

An iterator for all compilation VMs on all stemcells.

Yields:



68
69
70
71
72
73
# File 'lib/bosh/director/instance_reuser.rb', line 68

def each
  all_vms = (@idle_instances_by_stemcell.values + @in_use_instances_by_stemcell.values).flatten
  all_vms.each do |vm|
    yield vm
  end
end

#get_instance(stemcell) ⇒ DeploymentPlan::Instance

Returns the instance of a VM that is not in use and can be reused.

Parameters:

Returns:



25
26
27
28
29
30
31
32
33
34
# File 'lib/bosh/director/instance_reuser.rb', line 25

def get_instance(stemcell)
  @mutex.synchronize do
    return nil if @idle_instances_by_stemcell[stemcell].nil?
    instance = @idle_instances_by_stemcell[stemcell].pop
    return nil if instance.nil?
    @in_use_instances_by_stemcell[stemcell] ||= []
    @in_use_instances_by_stemcell[stemcell] << instance
    return instance
  end
end

#get_num_instances(stemcell) ⇒ Integer

Gets the total number of compilation instances created with a given stemcell.

Parameters:

Returns:

  • (Integer)

    The number of instances running a given stemcell.



58
59
60
61
62
63
64
# File 'lib/bosh/director/instance_reuser.rb', line 58

def get_num_instances(stemcell)
  @mutex.synchronize do
    idle_count = @idle_instances_by_stemcell[stemcell].nil? ? 0 : @idle_instances_by_stemcell[stemcell].size
    in_use_count = @in_use_instances_by_stemcell[stemcell].nil? ? 0 : @in_use_instances_by_stemcell[stemcell].size
    idle_count + in_use_count
  end
end

#release_instance(instance) ⇒ Object

Raises:



36
37
38
39
40
41
# File 'lib/bosh/director/instance_reuser.rb', line 36

def release_instance(instance)
  raise NilInstanceError if instance.nil?
  @mutex.synchronize do
    release_without_lock(instance)
  end
end

#remove_instance(instance) ⇒ Object

Raises:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bosh/director/instance_reuser.rb', line 43

def remove_instance(instance)
  raise NilInstanceError if instance.nil?
  @mutex.synchronize do
    release_without_lock(instance)
    @idle_instances_by_stemcell.each_value do |vms|
      vms.each do |v|
        vms.delete(v) if instance == v
      end
    end
  end
end