Class: Dcmgr::Models::HostNode

Inherits:
AccountResource show all
Defined in:
lib/dcmgr/models/host_node.rb

Constant Summary collapse

HYPERVISOR_XEN_34 =
'xen-3.4'
HYPERVISOR_XEN_40 =
'xen-4.0'
HYPERVISOR_KVM =
'kvm'
HYPERVISOR_LXC =
'lxc'
ARCH_X86 =
:x86.to_s
ARCH_X86_64 =
:x86_64.to_s
SUPPORTED_ARCH =
[ARCH_X86, ARCH_X86_64]
SUPPORTED_HYPERVISOR =
[HYPERVISOR_KVM, HYPERVISOR_LXC]

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AccountResource

#account

Methods inherited from BaseNew

Proxy, dataset, default_row_lock_mode=, install_data, install_data_hooks, lock!, unlock!, #with_timestamps?

Class Method Details

.check_domain_capacity?(cpu_cores, memory_size, num = 1) ⇒ Boolean

Check the free resource capacity across entire local VDC domain.

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dcmgr/models/host_node.rb', line 104

def self.check_domain_capacity?(cpu_cores, memory_size, num=1)
  alives_mem_size = Instance.dataset.lives.filter.sum(:memory_size).to_i
  stopped_mem_size = Instance.dataset.lives.filter(:state=>'stopped').sum(:memory_size).to_i
  alives_cpu_cores = Instance.dataset.lives.filter.sum(:cpu_cores).to_i
  stopped_cpu_cores = Instance.dataset.lives.filter(:state=>'stopped').sum(:cpu_cores).to_i
  # instance releases the resources during stopped state normally. however admins may
  # want to manage the reserved resource ratio for stopped
  # instances. "stopped_instance_usage_factor" conf parameter allows its control.
  # 
  # * stopped_instance_usage_factor == 1.0 means that 100% of
  # resources are reserved for stopped instances. all of them will
  # success to start up but utilization of host notes will be dropped.
  # * stopped_instance_usage_factor == 0.5 means that 50% of
  # resources for stopped instances are reserved and rest of 50%
  # may fail to start again.
  usage_factor = (Dcmgr.conf.stopped_instance_usage_factor || 1.0).to_f
  avail_mem_size = self.online_nodes.sum(:offering_memory_size).to_i - ((alives_mem_size - stopped_mem_size) + (stopped_mem_size * usage_factor).floor)
  avail_cpu_cores = self.online_nodes.sum(:offering_cpu_cores).to_i - ((alives_cpu_cores - stopped_cpu_cores) + (stopped_cpu_cores * usage_factor).floor)
  
  (avail_mem_size >= memory_size * num.to_i) && (avail_cpu_cores >= cpu_cores * num.to_i)
end

Instance Method Details

#available_cpu_coresObject

Returns available CPU cores.



94
95
96
# File 'lib/dcmgr/models/host_node.rb', line 94

def available_cpu_cores
  self.offering_cpu_cores - self.cpu_core_usage
end

#available_memory_sizeObject

Returns available memory size.



99
100
101
# File 'lib/dcmgr/models/host_node.rb', line 99

def available_memory_size
  self.offering_memory_size - self.memory_size_usage
end

#check_capacity(spec) ⇒ Object

Returns true/false if the host pool has enough capacity to run the spec.

Parameters:

Raises:

  • (TypeError)


68
69
70
71
72
73
74
# File 'lib/dcmgr/models/host_node.rb', line 68

def check_capacity(spec)
  raise TypeError unless spec.is_a?(InstanceSpec)
  inst_on_hp = self.instances_dataset.lives.all

  (self.offering_cpu_cores >= inst_on_hp.inject(0) {|t, i| t += i.cpu_cores } + spec.cpu_cores) &&
    (self.offering_memory_size >= inst_on_hp.inject(0) {|t, i| t += i.memory_size } + spec.memory_size)
end

#cpu_core_usageObject

Returns reserved CPU cores used by running/scheduled instances.



84
85
86
# File 'lib/dcmgr/models/host_node.rb', line 84

def cpu_core_usage
  instances_usage(:cpu_cores)
end

#depend_resources?boolean

Check if the resources exist depending on the HostNode.

Returns:

  • (boolean)


58
59
60
# File 'lib/dcmgr/models/host_node.rb', line 58

def depend_resources?
  !self.instances_dataset.runnings.empty?
end

#memory_size_usageObject

Returns reserved memory size used by running/scheduled instances.



89
90
91
# File 'lib/dcmgr/models/host_node.rb', line 89

def memory_size_usage
  instances_usage(:memory_size)
end

#statusObject



62
63
64
# File 'lib/dcmgr/models/host_node.rb', line 62

def status
  node.nil? ? :offline : node.state
end

#to_api_documentObject



76
77
78
79
80
81
# File 'lib/dcmgr/models/host_node.rb', line 76

def to_api_document
  h = super()
  h.merge!(:status=>self.status)
  h.delete(:node_id)
  h
end

#to_hashObject



52
53
54
# File 'lib/dcmgr/models/host_node.rb', line 52

def to_hash
  super.merge(:status=>self.status)
end

#validateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dcmgr/models/host_node.rb', line 29

def validate
  super
  # for compatibility: hva.xxx or hva-xxxx
  unless self.node_id =~ /^hva[-.]/
    errors.add(:node_id, "is invalid ID: #{self.node_id}")
  end

  if (h = self.class.filter(:node_id=>self.node_id).first) && h.id != self.id
    errors.add(:node_id, " #{self.node_id} is already been associated to #{h.canonical_uuid} ")
  end
  
  unless SUPPORTED_ARCH.member?(self.arch)
    errors.add(:arch, "unknown architecture type: #{self.arch}")
  end

  unless self.offering_cpu_cores > 0
    errors.add(:offering_cpu_cores, "it must have digit more than zero")
  end
  unless self.offering_memory_size > 0
    errors.add(:offering_memory_size, "it must have digit more than zero")
  end
end