From d926ec5b5daacb7a07f609861ece15cfbbf6e92a Mon Sep 17 00:00:00 2001 From: Joe Gordon Date: Tue, 1 Nov 2011 16:35:08 -0700 Subject: [PATCH] Exception cleanup in scheduler Fixes NoValidHost and willNotSchedule by moving them to exception.py Removed unneeded imports Change-Id: Ib9bb4b36d5e4c00667bd0b2d73137ba9eac5b3b0 --- nova/scheduler/distributed_scheduler.py | 4 +-- nova/scheduler/simple.py | 30 +++++++++---------- nova/scheduler/vsa.py | 5 ++-- .../scheduler/test_distributed_scheduler.py | 5 ++-- nova/tests/scheduler/test_vsa_scheduler.py | 13 ++++---- nova/tests/test_compute_utils.py | 4 --- 6 files changed, 28 insertions(+), 33 deletions(-) diff --git a/nova/scheduler/distributed_scheduler.py b/nova/scheduler/distributed_scheduler.py index 224b1940..b8fbd55f 100644 --- a/nova/scheduler/distributed_scheduler.py +++ b/nova/scheduler/distributed_scheduler.py @@ -71,7 +71,7 @@ class DistributedScheduler(driver.Scheduler): NOTE: We're only focused on compute instances right now, so this method will always raise NoValidHost().""" msg = _("No host selection for %s defined." % topic) - raise driver.NoValidHost(msg) + raise exception.NoValidHost(reason=msg) def schedule_run_instance(self, context, request_spec, *args, **kwargs): """This method is called from nova.compute.api to provision @@ -103,7 +103,7 @@ class DistributedScheduler(driver.Scheduler): *args, **kwargs) if not weighted_hosts: - raise driver.NoValidHost(_('No hosts were available')) + raise exception.NoValidHost(reason=_("")) instances = [] for num in xrange(num_instances): diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index cae4a5b7..8f993d9d 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -23,6 +23,7 @@ Simple Scheduler from nova import db from nova import flags +from nova import exception from nova.scheduler import driver from nova.scheduler import chance @@ -51,7 +52,7 @@ class SimpleScheduler(chance.ChanceScheduler): if host and context.is_admin: service = db.service_get_by_args(elevated, host, 'nova-compute') if not self.service_is_up(service): - raise driver.WillNotSchedule(_("Host %s is not alive") % host) + raise exception.WillNotSchedule(host=host) return host results = db.service_get_all_compute_sorted(elevated) @@ -61,12 +62,12 @@ class SimpleScheduler(chance.ChanceScheduler): for result in results: (service, instance_cores) = result if instance_cores + instance_opts['vcpus'] > FLAGS.max_cores: - raise driver.NoValidHost(_("All hosts have too many cores")) + msg = _("All hosts have too many cores") + raise exception.NoValidHost(reason=msg) if self.service_is_up(service): return service['host'] - raise driver.NoValidHost(_("Scheduler was unable to locate a host" - " for this request. Is the appropriate" - " service running?")) + msg = _("Is the appropriate service running?") + raise exception.NoValidHost(reason=msg) def schedule_run_instance(self, context, request_spec, *_args, **_kwargs): num_instances = request_spec.get('num_instances', 1) @@ -101,7 +102,7 @@ class SimpleScheduler(chance.ChanceScheduler): if host and context.is_admin: service = db.service_get_by_args(elevated, host, 'nova-volume') if not self.service_is_up(service): - raise driver.WillNotSchedule(_("Host %s not available") % host) + raise exception.WillNotSchedule(host=host) driver.cast_to_volume_host(context, host, 'create_volume', volume_id=volume_id, **_kwargs) return None @@ -113,15 +114,14 @@ class SimpleScheduler(chance.ChanceScheduler): for result in results: (service, volume_gigabytes) = result if volume_gigabytes + volume_ref['size'] > FLAGS.max_gigabytes: - raise driver.NoValidHost(_("All hosts have too many " - "gigabytes")) + msg = _("All hosts have too many gigabytes") + raise exception.NoValidHost(reason=msg) if self.service_is_up(service): driver.cast_to_volume_host(context, service['host'], 'create_volume', volume_id=volume_id, **_kwargs) return None - raise driver.NoValidHost(_("Scheduler was unable to locate a host" - " for this request. Is the appropriate" - " service running?")) + msg = _("Is the appropriate service running?") + raise exception.NoValidHost(reason=msg) def schedule_set_network_host(self, context, *_args, **_kwargs): """Picks a host that is up and has the fewest networks.""" @@ -131,11 +131,11 @@ class SimpleScheduler(chance.ChanceScheduler): for result in results: (service, instance_count) = result if instance_count >= FLAGS.max_networks: - raise driver.NoValidHost(_("All hosts have too many networks")) + msg = _("All hosts have too many networks") + raise exception.NoValidHost(reason=msg) if self.service_is_up(service): driver.cast_to_network_host(context, service['host'], 'set_network_host', **_kwargs) return None - raise driver.NoValidHost(_("Scheduler was unable to locate a host" - " for this request. Is the appropriate" - " service running?")) + msg = _("Is the appropriate service running?") + raise exception.NoValidHost(reason=msg) diff --git a/nova/scheduler/vsa.py b/nova/scheduler/vsa.py index da447500..7c167095 100644 --- a/nova/scheduler/vsa.py +++ b/nova/scheduler/vsa.py @@ -25,6 +25,7 @@ from nova import flags from nova import log as logging from nova import rpc from nova import utils +from nova import exception from nova.scheduler import driver from nova.scheduler import simple from nova.vsa.api import VsaState @@ -173,7 +174,7 @@ class VsaScheduler(simple.SimpleScheduler): selected_hosts, unique) if host is None: - raise driver.WillNotSchedule(_("No available hosts")) + raise exception.NoValidHost(reason=_("")) return (host, qos_cap) @@ -216,7 +217,7 @@ class VsaScheduler(simple.SimpleScheduler): service = db.service_get_by_args(context.elevated(), host, 'nova-volume') if not self.service_is_up(service): - raise driver.WillNotSchedule(_("Host %s not available") % host) + raise exception.WillNotSchedule(host=host) return host else: diff --git a/nova/tests/scheduler/test_distributed_scheduler.py b/nova/tests/scheduler/test_distributed_scheduler.py index 657232f7..ad2d1b1b 100644 --- a/nova/tests/scheduler/test_distributed_scheduler.py +++ b/nova/tests/scheduler/test_distributed_scheduler.py @@ -25,7 +25,6 @@ from nova import exception from nova import rpc from nova import test from nova.compute import api as compute_api -from nova.scheduler import driver from nova.scheduler import distributed_scheduler from nova.scheduler import least_cost from nova.scheduler import zone_manager @@ -120,7 +119,7 @@ class DistributedSchedulerTestCase(test.TestCase): fake_context = context.RequestContext('user', 'project') request_spec = dict(instance_type=dict(memory_mb=1, local_gb=1)) - self.assertRaises(driver.NoValidHost, sched.schedule_run_instance, + self.assertRaises(exception.NoValidHost, sched.schedule_run_instance, fake_context, request_spec) def test_run_instance_with_blob_hint(self): @@ -189,7 +188,7 @@ class DistributedSchedulerTestCase(test.TestCase): fake_context = context.RequestContext('user', 'project') - self.assertRaises(driver.NoValidHost, sched.schedule_run_instance, + self.assertRaises(exception.NoValidHost, sched.schedule_run_instance, fake_context, {}) self.assertTrue(self.was_admin) diff --git a/nova/tests/scheduler/test_vsa_scheduler.py b/nova/tests/scheduler/test_vsa_scheduler.py index 802946e1..9a74c994 100644 --- a/nova/tests/scheduler/test_vsa_scheduler.py +++ b/nova/tests/scheduler/test_vsa_scheduler.py @@ -28,7 +28,6 @@ from nova import utils from nova.volume import volume_types from nova.scheduler import vsa as vsa_sched -from nova.scheduler import driver FLAGS = flags.FLAGS LOG = logging.getLogger('nova.tests.scheduler.vsa') @@ -274,7 +273,7 @@ class VsaSchedulerTestCase(test.TestCase): drive_type_num=5, init_num_drives=1) request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=6) - self.assertRaises(driver.WillNotSchedule, + self.assertRaises(exception.NoValidHost, self.sched.schedule_create_volumes, self.context, request_spec, @@ -291,7 +290,7 @@ class VsaSchedulerTestCase(test.TestCase): prev = self._generate_default_service_states() request_spec = self._get_vol_creation_request(num_vols=3, drive_ix=0) - self.assertRaises(driver.WillNotSchedule, + self.assertRaises(exception.NoValidHost, self.sched.schedule_create_volumes, self.context, request_spec, @@ -314,7 +313,7 @@ class VsaSchedulerTestCase(test.TestCase): self.service_states = new_states request_spec = self._get_vol_creation_request(num_vols=1, drive_ix=0) - self.assertRaises(driver.WillNotSchedule, + self.assertRaises(exception.NoValidHost, self.sched.schedule_create_volumes, self.context, request_spec, @@ -365,7 +364,7 @@ class VsaSchedulerTestCase(test.TestCase): availability_zone=None) self._print_service_states() - self.assertRaises(driver.WillNotSchedule, + self.assertRaises(exception.NoValidHost, self.sched.schedule_create_volumes, self.context, new_request, @@ -393,7 +392,7 @@ class VsaSchedulerTestCase(test.TestCase): self.stubs.Set(self.sched, 'service_is_up', self._fake_service_is_up_False) - self.assertRaises(driver.WillNotSchedule, + self.assertRaises(exception.WillNotSchedule, self.sched.schedule_create_volumes, self.context, request_spec, @@ -483,7 +482,7 @@ class VsaSchedulerTestCase(test.TestCase): global_volume = {} global_volume['volume_type_id'] = None - self.assertRaises(driver.NoValidHost, + self.assertRaises(exception.NoValidHost, self.sched.schedule_create_volume, self.context, 123, diff --git a/nova/tests/test_compute_utils.py b/nova/tests/test_compute_utils.py index 5efb1016..12b98aa0 100644 --- a/nova/tests/test_compute_utils.py +++ b/nova/tests/test_compute_utils.py @@ -19,11 +19,7 @@ Tests For misc util methods used with compute. """ -from datetime import datetime -from datetime import timedelta - from nova import db -from nova import exception from nova import flags from nova import context from nova import test