From 7af5bdd41dccd62ee19953b3616bd9d0863253f7 Mon Sep 17 00:00:00 2001 From: Arata Notsu Date: Thu, 8 Nov 2012 01:00:26 +0900 Subject: [PATCH] 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 --- nova/scheduler/filter_scheduler.py | 7 ++----- nova/scheduler/host_manager.py | 11 ++++------- nova/tests/scheduler/test_host_manager.py | 23 ++++++++++++----------- nova/tests/scheduler/test_least_cost.py | 2 +- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/nova/scheduler/filter_scheduler.py b/nova/scheduler/filter_scheduler.py index c43e488760eb..b2802931cbd4 100644 --- a/nova/scheduler/filter_scheduler.py +++ b/nova/scheduler/filter_scheduler.py @@ -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: diff --git a/nova/scheduler/host_manager.py b/nova/scheduler/host_manager.py index 91e16ad345ff..1f4b6d956f9f 100644 --- a/nova/scheduler/host_manager.py +++ b/nova/scheduler/host_manager.py @@ -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() diff --git a/nova/tests/scheduler/test_host_manager.py b/nova/tests/scheduler/test_host_manager.py index 74c24d56bf76..4d1e00852206 100644 --- a/nova/tests/scheduler/test_host_manager.py +++ b/nova/tests/scheduler/test_host_manager.py @@ -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): diff --git a/nova/tests/scheduler/test_least_cost.py b/nova/tests/scheduler/test_least_cost.py index df4e13244c15..64cda0b2a6e8 100644 --- a/nova/tests/scheduler/test_least_cost.py +++ b/nova/tests/scheduler/test_least_cost.py @@ -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