Make HostManager.get_all_host_states() return an iterator

Change the method's returning from a dictionary {host: HostState}
to an iterator for HostState, because the only caller of the method
(in filter_scheduler.py) is not interested in its key.

Change-Id: I84f649795a422e392a4c0d555c6cb29a01fae2d9
This commit is contained in:
Arata Notsu 2012-11-08 01:00:26 +09:00
parent 066b3930cf
commit 7af5bdd41d
4 changed files with 19 additions and 24 deletions

View File

@ -257,14 +257,11 @@ class FilterScheduler(driver.Scheduler):
# host, we virtually consume resources on it so subsequent
# selections can adjust accordingly.
# unfiltered_hosts_dict is {host : ZoneManager.HostInfo()}
unfiltered_hosts_dict = self.host_manager.get_all_host_states(
elevated, topic)
# Note: remember, we are using an iterator here. So only
# traverse this list once. This can bite you if the hosts
# are being scanned in a filter or weighing function.
hosts = unfiltered_hosts_dict.itervalues()
hosts = self.host_manager.get_all_host_states(
elevated, topic)
selected_hosts = []
if instance_uuids:

View File

@ -336,12 +336,9 @@ class HostManager(object):
self.service_states[host] = service_caps
def get_all_host_states(self, context, topic):
"""Returns a dict of all the hosts the HostManager
knows about. Also, each of the consumable resources in HostState
are pre-populated and adjusted based on data in the db.
For example:
{'192.168.1.100': HostState(), ...}
"""Returns a list of HostStates that represents all the hosts
the HostManager knows about. Also, each of the consumable resources
in HostState are pre-populated and adjusted based on data in the db.
Note: this can be very slow with a lot of instances.
InstanceType table isn't required since a copy is stored
@ -372,4 +369,4 @@ class HostManager(object):
self.host_state_map[host] = host_state
host_state.update_from_compute_node(compute)
return self.host_state_map
return self.host_state_map.itervalues()

View File

@ -136,27 +136,28 @@ class HostManagerTestCase(test.TestCase):
host_manager.LOG.warn("No service for compute ID 5")
self.mox.ReplayAll()
host_states = self.host_manager.get_all_host_states(context, topic)
self.host_manager.get_all_host_states(context, topic)
host_states_map = self.host_manager.host_state_map
self.assertEqual(len(host_states), 4)
self.assertEqual(len(host_states_map), 4)
# Check that .service is set properly
for i in xrange(4):
compute_node = fakes.COMPUTE_NODES[i]
host = compute_node['service']['host']
self.assertEqual(host_states[host].service,
self.assertEqual(host_states_map[host].service,
compute_node['service'])
self.assertEqual(host_states['host1'].free_ram_mb, 512)
self.assertEqual(host_states_map['host1'].free_ram_mb, 512)
# 511GB
self.assertEqual(host_states['host1'].free_disk_mb, 524288)
self.assertEqual(host_states['host2'].free_ram_mb, 1024)
self.assertEqual(host_states_map['host1'].free_disk_mb, 524288)
self.assertEqual(host_states_map['host2'].free_ram_mb, 1024)
# 1023GB
self.assertEqual(host_states['host2'].free_disk_mb, 1048576)
self.assertEqual(host_states['host3'].free_ram_mb, 3072)
self.assertEqual(host_states_map['host2'].free_disk_mb, 1048576)
self.assertEqual(host_states_map['host3'].free_ram_mb, 3072)
# 3071GB
self.assertEqual(host_states['host3'].free_disk_mb, 3145728)
self.assertEqual(host_states['host4'].free_ram_mb, 8192)
self.assertEqual(host_states_map['host3'].free_disk_mb, 3145728)
self.assertEqual(host_states_map['host4'].free_ram_mb, 8192)
# 8191GB
self.assertEqual(host_states['host4'].free_disk_mb, 8388608)
self.assertEqual(host_states_map['host4'].free_disk_mb, 8388608)
class HostStateTestCase(test.TestCase):

View File

@ -41,7 +41,7 @@ class LeastCostTestCase(test.TestCase):
fakes.mox_host_manager_db_calls(self.mox, ctxt)
self.mox.ReplayAll()
host_states = self.host_manager.get_all_host_states(ctxt,
'compute').values()
'compute')
self.mox.VerifyAll()
self.mox.ResetAll()
return host_states