1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2010 Openstack, LLC.
4
# Copyright 2010 United States Government as represented by the
5
# Administrator of the National Aeronautics and Space Administration.
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
# not use this file except in compliance with the License. You may obtain
10
# a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
# License for the specific language governing permissions and limitations
27
from nova import flags
28
from nova.scheduler import driver
29
from nova.scheduler import chance
32
flags.DEFINE_integer("max_cores", 16,
33
"maximum number of instance cores to allow per host")
34
flags.DEFINE_integer("max_gigabytes", 10000,
35
"maximum number of volume gigabytes to allow per host")
36
flags.DEFINE_integer("max_networks", 1000,
37
"maximum number of networks to allow per host")
39
class SimpleScheduler(chance.ChanceScheduler):
40
"""Implements Naive Scheduler that tries to find least loaded host."""
42
def schedule_run_instance(self, context, instance_id, *_args, **_kwargs):
43
"""Picks a host that is up and has the fewest running instances."""
44
instance_ref = db.instance_get(context, instance_id)
45
results = db.service_get_all_compute_sorted(context)
46
for result in results:
47
(service, instance_cores) = result
48
if instance_cores + instance_ref['vcpus'] > FLAGS.max_cores:
49
raise driver.NoValidHost("All hosts have too many cores")
50
if self.service_is_up(service):
51
# NOTE(vish): this probably belongs in the manager, if we
52
# can generalize this somehow
53
now = datetime.datetime.utcnow()
54
db.instance_update(context,
56
{'host': service['host'],
58
return service['host']
59
raise driver.NoValidHost("No hosts found")
61
def schedule_create_volume(self, context, volume_id, *_args, **_kwargs):
62
"""Picks a host that is up and has the fewest volumes."""
63
volume_ref = db.volume_get(context, volume_id)
64
results = db.service_get_all_volume_sorted(context)
65
for result in results:
66
(service, volume_gigabytes) = result
67
if volume_gigabytes + volume_ref['size'] > FLAGS.max_gigabytes:
68
raise driver.NoValidHost("All hosts have too many gigabytes")
69
if self.service_is_up(service):
70
# NOTE(vish): this probably belongs in the manager, if we
71
# can generalize this somehow
72
now = datetime.datetime.utcnow()
73
db.volume_update(context,
75
{'host': service['host'],
77
return service['host']
78
raise driver.NoValidHost("No hosts found")
80
def schedule_set_network_host(self, context, *_args, **_kwargs):
81
"""Picks a host that is up and has the fewest networks."""
83
results = db.service_get_all_network_sorted(context)
84
for result in results:
85
(service, instance_count) = result
86
if instance_count >= FLAGS.max_networks:
87
raise driver.NoValidHost("All hosts have too many networks")
88
if self.service_is_up(service):
89
return service['host']
90
raise driver.NoValidHost("No hosts found")