from oslo.config import cfg
from nova.scheduler import weights
instancename_weight_opts = [
cfg.FloatOpt('instancename_weight_multiplier',
default=-100000.0,
help='Multiplier used for weighing instancename. Negative '
'numbers mean to stack vs spread.'),
]
CONF = cfg.CONF
CONF.register_opts(instancename_weight_opts)
<pre name="code" class="python">hosts = self.host_manager.get_all_host_states(elevated)
selected_hosts = []
if instance_uuids:
num_instances = len(instance_uuids)
else:
num_instances = request_spec.get('num_instances', 1)
for num in xrange(num_instances):
# Filter local hosts based on requirements ...
hosts = self.host_manager.get_filtered_hosts(hosts,
filter_properties, index=num)
if not hosts:
# Can't get any more locally.
break
LOG.debug(_("Filtered %(hosts)s"), {'hosts': hosts})
weighed_hosts = self.host_manager.get_weighed_hosts(hosts,
filter_properties)
LOG.debug(_("Weighed %(hosts)s"), {'hosts': weighed_hosts})
scheduler_host_subset_size = CONF.scheduler_host_subset_size
if scheduler_host_subset_size > len(weighed_hosts):
scheduler_host_subset_size = len(weighed_hosts)
if scheduler_host_subset_size < 1:
scheduler_host_subset_size = 1
chosen_host = random.choice(
weighed_hosts[0:scheduler_host_subset_size])
selected_hosts.append(chosen_host)
filter_properties["selected_hosts"]=selected_hosts
# Now consume the resources so the filter/weights
# will change for the next instance.
chosen_host.obj.consume_from_instance(instance_properties)
if update_group_hosts is True:
filter_properties['group_hosts'].append(chosen_host.obj.host)
filter_properties.pop("selected_hosts")
return selected_hosts
class InstancenameWeigher(weights.BaseHostWeigher):
def _weight_multiplier(self):
"""Override the weight multiplier."""
return CONF.instancename_weight_multiplier
def _weigh_object(self, host_state, weight_properties):
"""Higher weights win. We want spreading to be the default."""
"""calculate the count that the host to be weighted appears in selected_hosts"""
""""""
count=0
if weight_properties.get('selected_hosts') == None:
return count
for selected_host in weight_properties['selected_hosts']:
if selected_host.obj.host==host_state.host:
count+=1
return count
这个是之前写过的一个diffrent host的代码,现在用不到了,openstack创建多台虚拟机的时候,使用一个for循环,每台虚拟机创建时候,对每台主机,去使用指定目录下的所有称重函数,排序,然后选择,我做的事情是每次选择之后,维护一个列表,将每次选择的主机记录,这样,下一次称重的时候,从list里判断,这次称重的主机,在本次创建工作中已经被选择了几次,举例:一次创建10台虚拟机,共有8台主机可以选择,那创建到第5台的时候,list中记录了,之前4台虚拟机选择的主机是1,2,3,4,那么第五台虚机称重时,就会把1,2,3,4这四台主机的重量减去,1*配置文件中的称重因子,这样这四台主机的重量降低,排序的时候会放在下面,第五台虚机会在5,6,7,8四台主机上选择一台作为主机。第十台虚机调用称重主机的时候,list中主机列表可能如下:1,2,3,4,5,6,7,8,1。这时候就将主机1减去2*配置文件中的称重因子,其他主机减去1*因子,这样已经有了两台虚拟机在1上的时候,1就优先不被选择