From 3e01acd4e70f9e850487c5ac4067ab2c2c1a18eb Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Mon, 9 Aug 2010 12:56:32 -0500 Subject: separated scheduler types into own modules --- nova/scheduler/base.py | 65 +++++++++++++++++++++++++++++++++ nova/scheduler/bestfit.py | 30 +++++++++++++++ nova/scheduler/chance.py | 33 +++++++++++++++++ nova/scheduler/scheduler.py | 89 --------------------------------------------- nova/scheduler/service.py | 10 ++--- 5 files changed, 133 insertions(+), 94 deletions(-) create mode 100644 nova/scheduler/base.py create mode 100644 nova/scheduler/bestfit.py create mode 100644 nova/scheduler/chance.py delete mode 100644 nova/scheduler/scheduler.py diff --git a/nova/scheduler/base.py b/nova/scheduler/base.py new file mode 100644 index 000000000..5c359943e --- /dev/null +++ b/nova/scheduler/base.py @@ -0,0 +1,65 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (c) 2010 Openstack, LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Scheduler base class that all Schedulers should inherit from +""" + +import time + +from nova import flags +from nova.datastore import Redis + +FLAGS = flags.FLAGS +flags.DEFINE_integer('node_down_time', + 60, + 'seconds without heartbeat that determines a ' + 'compute node to be down') + + +class Scheduler(object): + """ + The base class that all Scheduler clases should inherit from + """ + + @property + def compute_nodes(self): + return [identifier.split(':')[0] + for identifier in Redis.instance().smembers("daemons") + if (identifier.split(':')[1] == "nova-compute")] + + def compute_node_is_up(self, node): + time_str = Redis.instance().hget('%s:%s:%s' % + ('daemon', node, 'nova-compute'), + 'updated_at') + if not time_str: + return False + + # Would be a lot easier if we stored heartbeat time in epoch :) + + # The 'str()' here is to get rid of a pylint error + time_str = str(time_str).replace('Z', 'UTC') + time_split = time.strptime(time_str, '%Y-%m-%dT%H:%M:%S%Z') + epoch_time = int(time.mktime(time_split)) - time.timezone + return (time.time() - epoch_time) < FLAGS.node_down_time + + def compute_nodes_up(self): + return [node for node in self.compute_nodes + if self.compute_node_is_up(node)] + + def pick_node(self, instance_id, **_kwargs): + """You DEFINITELY want to define this in your subclass""" + raise NotImplementedError("Your subclass should define pick_node") diff --git a/nova/scheduler/bestfit.py b/nova/scheduler/bestfit.py new file mode 100644 index 000000000..1bd24456a --- /dev/null +++ b/nova/scheduler/bestfit.py @@ -0,0 +1,30 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (c) 2010 Openstack, LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Best Fit Scheduler +""" + +from nova.scheduler.base import Scheduler + + +class BestFitScheduler(Scheduler): + """ + Implements Scheduler as a best-fit node selector + """ + + def pick_node(self, instance_id, **_kwargs): + raise NotImplementedError("BestFitScheduler is not done yet") diff --git a/nova/scheduler/chance.py b/nova/scheduler/chance.py new file mode 100644 index 000000000..c57c346f5 --- /dev/null +++ b/nova/scheduler/chance.py @@ -0,0 +1,33 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (c) 2010 Openstack, LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Chance (Random) Scheduler implementation +""" + +import random + +from nova.scheduler.base import Scheduler + + +class ChanceScheduler(Scheduler): + """ + Implements Scheduler as a random node selector + """ + + def pick_node(self, instance_id, **_kwargs): + nodes = self.compute_nodes_up() + return nodes[int(random.random() * len(nodes))] diff --git a/nova/scheduler/scheduler.py b/nova/scheduler/scheduler.py deleted file mode 100644 index 49ef40f06..000000000 --- a/nova/scheduler/scheduler.py +++ /dev/null @@ -1,89 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2010 Openstack, LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -""" -Scheduler Classes -""" - -import random -import time - -from nova import flags -from nova.datastore import Redis - -FLAGS = flags.FLAGS -flags.DEFINE_integer('node_down_time', - 60, - 'seconds without heartbeat that determines a ' - 'compute node to be down') - - -class Scheduler(object): - """ - The base class that all Scheduler clases should inherit from - """ - - @property - def compute_nodes(self): - return [identifier.split(':')[0] - for identifier in Redis.instance().smembers("daemons") - if (identifier.split(':')[1] == "nova-compute")] - - def compute_node_is_up(self, node): - time_str = Redis.instance().hget('%s:%s:%s' % - ('daemon', node, 'nova-compute'), - 'updated_at') - if not time_str: - return False - - # Would be a lot easier if we stored heartbeat time in epoch :) - time_str = time_str.replace('Z', 'UTC') - time_split = time.strptime(time_str, '%Y-%m-%dT%H:%M:%S%Z') - epoch_time = int(time.mktime(time_split)) - time.timezone - return (time.time() - epoch_time) < FLAGS.node_down_time - - def compute_nodes_up(self): - return [node for node in self.compute_nodes - if self.compute_node_is_up(node)] - - def pick_node(self, instance_id, **_kwargs): - """You DEFINITELY want to define this in your subclass""" - raise NotImplementedError("Your subclass should define pick_node") - - -class RandomScheduler(Scheduler): - """ - Implements Scheduler as a random node selector - """ - - def __init__(self): - super(RandomScheduler, self).__init__() - - def pick_node(self, instance_id, **_kwargs): - nodes = self.compute_nodes_up() - return nodes[int(random.random() * len(nodes))] - - -class BestFitScheduler(Scheduler): - """ - Implements Scheduler as a best-fit node selector - """ - - def __init__(self): - super(BestFitScheduler, self).__init__() - - def pick_node(self, instance_id, **_kwargs): - raise NotImplementedError("BestFitScheduler is not done yet") diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 9d2d35f13..44b30ecb5 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -25,15 +25,16 @@ from nova import exception from nova import flags from nova import rpc from nova import service -from nova.scheduler import scheduler +from nova.scheduler import chance +from nova.scheduler import bestfit FLAGS = flags.FLAGS flags.DEFINE_string('scheduler_type', - 'random', + 'chance', 'the scheduler to use') -SCHEDULER_CLASSES = {'random': scheduler.RandomScheduler, - 'bestfit': scheduler.BestFitScheduler} +SCHEDULER_CLASSES = {'chance': chance.ChanceScheduler, + 'bestfit': bestfit.BestFitScheduler} class SchedulerService(service.Service): @@ -72,4 +73,3 @@ class SchedulerService(service.Service): "args": {"instance_id": instance_id}}) logging.debug("Casting to node %s for running instance %s", node, instance_id) - -- cgit