test for too many instances work

This commit is contained in:
Vishvananda Ishaya
2010-09-02 15:15:39 -07:00
parent baef7fe167
commit 3cb63ee0ad
3 changed files with 48 additions and 61 deletions

View File

@@ -28,7 +28,7 @@ from nova import exception
from nova import flags
FLAGS = flags.FLAGS
flags.DEFINE_integer('daemon_down_time',
flags.DEFINE_integer('service_down_time',
60,
'seconds without heartbeat that determines a '
'compute node to be down')
@@ -43,20 +43,21 @@ class Scheduler(object):
"""
@staticmethod
def daemon_is_up(daemon):
def service_is_up(service):
"""
Given a daemon, return whether the deamon is considered 'up' by
Given a service, return whether the service is considered 'up' by
if it's sent a heartbeat recently
"""
elapsed = datetime.datetime.now() - daemon['updated_at']
return elapsed < datetime.timedelta(seconds=FLAGS.daemon_down_time)
last_heartbeat = service['updated_at'] or service['created_at']
elapsed = datetime.datetime.now() - last_heartbeat
return elapsed < datetime.timedelta(seconds=FLAGS.service_down_time)
def hosts_up(self, context, topic):
"""
Return the list of hosts that have a running daemon for topic
Return the list of hosts that have a running service for topic
"""
daemons = db.daemon_get_all_by_topic(context, topic)
return [daemon.host
for daemon in daemons
if self.daemon_is_up(daemon)]
services = db.service_get_all_by_topic(context, topic)
return [service.host
for service in services
if self.service_is_up(service)]

View File

@@ -43,14 +43,13 @@ class SimpleScheduler(driver.Scheduler):
Picks a host that is up and has the fewest running instances
"""
results = db.daemon_get_all_compute_sorted(context)
results = db.service_get_all_compute_sorted(context)
for result in results:
(daemon, instance_count) = result
print daemon.host, instance_count
(service, instance_count) = result
if instance_count >= FLAGS.max_instances:
raise driver.NoValidHost("All hosts have too many instances")
if self.daemon_is_up(daemon):
return daemon['host']
if self.service_is_up(service):
return service['host']
raise driver.NoValidHost("No hosts found")
def pick_volume_host(self, context, volume_id, **_kwargs):
@@ -58,13 +57,13 @@ class SimpleScheduler(driver.Scheduler):
Picks a host that is up and has the fewest volumes
"""
results = db.daemon_get_all_volume_sorted(context)
results = db.service_get_all_volume_sorted(context)
for result in results:
(daemon, instance_count) = result
(service, instance_count) = result
if instance_count >= FLAGS.max_volumes:
raise driver.NoValidHost("All hosts have too many volumes")
if self.daemon_is_up(daemon):
return daemon['host']
if self.service_is_up(service):
return service['host']
raise driver.NoValidHost("No hosts found")
def pick_network_host(self, context, network_id, **_kwargs):
@@ -72,11 +71,11 @@ class SimpleScheduler(driver.Scheduler):
Picks a host that is up and has the fewest networks
"""
results = db.daemon_get_all_network_sorted(context)
results = db.service_get_all_network_sorted(context)
for result in results:
(daemon, instance_count) = result
(service, instance_count) = result
if instance_count >= FLAGS.max_networks:
raise driver.NoValidHost("All hosts have too many networks")
if self.daemon_is_up(daemon):
return daemon['host']
if self.service_is_up(service):
return service['host']
raise driver.NoValidHost("No hosts found")

View File

@@ -18,9 +18,6 @@
"""
Tests For Scheduler
"""
import logging
from twisted.internet import defer
from nova import db
from nova import flags
@@ -36,10 +33,10 @@ FLAGS = flags.FLAGS
flags.DECLARE('max_instances', 'nova.scheduler.simple')
class SchedulerTestCase(test.TrialTestCase):
class SimpleSchedulerTestCase(test.TrialTestCase):
"""Test case for scheduler"""
def setUp(self): # pylint: disable-msg=C0103
super(SchedulerTestCase, self).setUp()
super(SimpleSchedulerTestCase, self).setUp()
self.flags(connection_type='fake',
max_instances=4,
scheduler_driver='nova.scheduler.simple.SimpleScheduler')
@@ -49,10 +46,20 @@ class SchedulerTestCase(test.TrialTestCase):
self.user = self.manager.create_user('fake', 'fake', 'fake')
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.context = None
self.service1 = service.Service('host1',
'nova-compute',
'compute',
FLAGS.compute_manager)
self.service2 = service.Service('host2',
'nova-compute',
'compute',
FLAGS.compute_manager)
def tearDown(self): # pylint: disable-msg=C0103
self.manager.delete_user(self.user)
self.manager.delete_project(self.project)
self.service1.kill()
self.service2.kill()
def _create_instance(self):
"""Create a test instance"""
@@ -70,53 +77,33 @@ class SchedulerTestCase(test.TrialTestCase):
def test_hosts_are_up(self):
# NOTE(vish): constructing service without create method
# because we are going to use it without queue
service1 = service.Service('host1',
'nova-compute',
'compute',
FLAGS.compute_manager)
service2 = service.Service('host2',
'nova-compute',
'compute',
FLAGS.compute_manager)
hosts = self.scheduler.driver.hosts_up(self.context, 'compute')
self.assertEqual(len(hosts), 0)
service1.report_state()
service2.report_state()
hosts = self.scheduler.driver.hosts_up(self.context, 'compute')
self.assertEqual(len(hosts), 2)
def test_least_busy_host_gets_instance(self):
service1 = service.Service('host1',
'nova-compute',
'compute',
FLAGS.compute_manager)
service2 = service.Service('host2',
'nova-compute',
'compute',
FLAGS.compute_manager)
service1.report_state()
service2.report_state()
instance_id = self._create_instance()
service1.run_instance(self.context, instance_id)
self.service1.run_instance(self.context, instance_id)
host = self.scheduler.driver.pick_compute_host(self.context,
instance_id)
self.assertEqual(host, 'host2')
service1.terminate_instance(self.context, instance_id)
self.service1.terminate_instance(self.context, instance_id)
def test_too_many_instances(self):
service1 = service.Service('host',
'nova-compute',
'compute',
FLAGS.compute_manager)
instance_ids = []
instance_ids1 = []
instance_ids2 = []
for index in xrange(FLAGS.max_instances):
instance_id = self._create_instance()
service1.run_instance(self.context, instance_id)
instance_ids.append(instance_id)
self.service1.run_instance(self.context, instance_id)
instance_ids1.append(instance_id)
instance_id = self._create_instance()
self.service2.run_instance(self.context, instance_id)
instance_ids2.append(instance_id)
instance_id = self._create_instance()
self.assertRaises(driver.NoValidHost,
self.scheduler.driver.pick_compute_host,
self.context,
instance_id)
for instance_id in instance_ids:
service1.terminate_instance(self.context, instance_id)
for instance_id in instance_ids1:
self.service1.terminate_instance(self.context, instance_id)
for instance_id in instance_ids2:
self.service2.terminate_instance(self.context, instance_id)