From 53a7538a84ea2612cd86e6e98887cab7723dcbb2 Mon Sep 17 00:00:00 2001 From: David Scannell Date: Wed, 13 Feb 2013 11:18:04 -0500 Subject: Add select_hosts to scheduler manager rpc This adds a select_hosts(...) method to the scheduler manager rpc that allows other services to query it for the best suited host instead of relying on it to proxy its messages. This can be used by extensions that need the scheduler to determine the best-fit host but want to prepare and send their own messages to that host. Change-Id: I5b4760114dfcdb0464fac8ea81f46f7532ac0580 --- nova/tests/scheduler/test_chance_scheduler.py | 30 ++++++++++++++++++++ nova/tests/scheduler/test_filter_scheduler.py | 40 +++++++++++++++++++++++++++ nova/tests/scheduler/test_rpcapi.py | 6 ++++ 3 files changed, 76 insertions(+) (limited to 'nova/tests') diff --git a/nova/tests/scheduler/test_chance_scheduler.py b/nova/tests/scheduler/test_chance_scheduler.py index dcbe86f75..0ee617044 100644 --- a/nova/tests/scheduler/test_chance_scheduler.py +++ b/nova/tests/scheduler/test_chance_scheduler.py @@ -165,3 +165,33 @@ class ChanceSchedulerTestCase(test_scheduler.SchedulerTestCase): self.driver.schedule_prep_resize(fake_context, {}, {}, {}, instance, {}, None) self.assertEqual(info['called'], 0) + + def test_select_hosts(self): + ctxt = context.RequestContext('fake', 'fake', False) + ctxt_elevated = 'fake-context-elevated' + fake_args = (1, 2, 3) + instance_opts = {'fake_opt1': 'meow', 'launch_index': -1} + instance1 = {'uuid': 'fake-uuid1'} + instance2 = {'uuid': 'fake-uuid2'} + request_spec = {'instance_uuids': ['fake-uuid1', 'fake-uuid2'], + 'instance_properties': instance_opts} + + self.mox.StubOutWithMock(ctxt, 'elevated') + self.mox.StubOutWithMock(self.driver, 'hosts_up') + self.mox.StubOutWithMock(random, 'random') + + ctxt.elevated().AndReturn(ctxt_elevated) + # instance 1 + self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn( + ['host1', 'host2', 'host3', 'host4']) + random.random().AndReturn(.5) + + # instance 2 + ctxt.elevated().AndReturn(ctxt_elevated) + self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn( + ['host1', 'host2', 'host3', 'host4']) + random.random().AndReturn(.2) + + self.mox.ReplayAll() + hosts = self.driver.select_hosts(ctxt, request_spec, {}) + self.assertEquals(['host3', 'host1'], hosts) diff --git a/nova/tests/scheduler/test_filter_scheduler.py b/nova/tests/scheduler/test_filter_scheduler.py index 4b07581fb..849f63f5d 100644 --- a/nova/tests/scheduler/test_filter_scheduler.py +++ b/nova/tests/scheduler/test_filter_scheduler.py @@ -656,3 +656,43 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): self.assertEquals(1, len(hosts)) self.assertEquals(50, hosts[0].weight) + + def test_select_hosts_happy_day(self): + """select_hosts is basically a wrapper around the _select() method. + Similar to the _select tests, this just does a happy path test to + ensure there is nothing glaringly wrong.""" + + self.next_weight = 1.0 + + selected_hosts = [] + + def _fake_weigh_objects(_self, functions, hosts, options): + self.next_weight += 2.0 + host_state = hosts[0] + selected_hosts.append(host_state.host) + return [weights.WeighedHost(host_state, self.next_weight)] + + sched = fakes.FakeFilterScheduler() + fake_context = context.RequestContext('user', 'project', + is_admin=True) + + self.stubs.Set(sched.host_manager, 'get_filtered_hosts', + fake_get_filtered_hosts) + self.stubs.Set(weights.HostWeightHandler, + 'get_weighed_objects', _fake_weigh_objects) + fakes.mox_host_manager_db_calls(self.mox, fake_context) + + request_spec = {'num_instances': 10, + 'instance_type': {'memory_mb': 512, 'root_gb': 512, + 'ephemeral_gb': 0, + 'vcpus': 1}, + 'instance_properties': {'project_id': 1, + 'root_gb': 512, + 'memory_mb': 512, + 'ephemeral_gb': 0, + 'vcpus': 1, + 'os_type': 'Linux'}} + self.mox.ReplayAll() + hosts = sched.select_hosts(fake_context, request_spec, {}) + self.assertEquals(len(hosts), 10) + self.assertEquals(hosts, selected_hosts) diff --git a/nova/tests/scheduler/test_rpcapi.py b/nova/tests/scheduler/test_rpcapi.py index e9a1680a8..af6a57ba6 100644 --- a/nova/tests/scheduler/test_rpcapi.py +++ b/nova/tests/scheduler/test_rpcapi.py @@ -90,3 +90,9 @@ class SchedulerRpcAPITestCase(test.TestCase): def test_get_backdoor_port(self): self._test_scheduler_api('get_backdoor_port', rpc_method='call', host='fake_host', version='2.5') + + def test_select_hosts(self): + self._test_scheduler_api('select_hosts', rpc_method='call', + request_spec='fake_request_spec', + filter_properties='fake_prop', + version='2.6') -- cgit