Refactored ZoneRedirect into ZoneChildHelper so ZoneManager can use this too.

This commit is contained in:
Sandy Walsh
2011-03-16 11:44:40 -07:00
parent 2f1676497f
commit 08bfa2e34b
2 changed files with 33 additions and 9 deletions

View File

@@ -23,6 +23,10 @@ from nova import flags
from nova import log as logging from nova import log as logging
from nova import rpc from nova import rpc
import novaclient.client as client
from eventlet import greenpool
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.scheduler.api') LOG = logging.getLogger('nova.scheduler.api')
@@ -76,6 +80,8 @@ class API(object):
@classmethod @classmethod
def get_instance_or_reroute(cls, context, instance_id): def get_instance_or_reroute(cls, context, instance_id):
"""Return an instance from the db or throw a ZoneRouteException
if not found."""
try: try:
instance = db.instance_get(context, instance_id) instance = db.instance_get(context, instance_id)
return instance return instance
@@ -88,12 +94,30 @@ class API(object):
zones = db.zone_get_all(context) zones = db.zone_get_all(context)
raise exception.ZoneRouteException(zones) raise exception.ZoneRouteException(zones)
@classmethod
def get_queue_for_instance(cls, context, service, instance_id):
instance = db.instance_get(context, instance_id)
zone = db.get_zone(instance.zone.id)
if cls._is_current_zone(zone):
return db.queue_get_for(context, service, instance['host'])
# Throw a reroute Exception for the middleware to pick up. def _wrap_method(function, self):
raise exception.ZoneRouteException(zone) def _wrap(*args, **kwargs):
return function(self, *args, **kwargs)
return _wrap
def _process(self, zone):
nova = client.OpenStackClient(zone.username, zone.password,
zone.api_url)
nova.authenticate()
return self.process(nova, zone)
class ChildZoneHelper(object):
"""Delegate a call to a set of Child Zones and wait for their
responses. Could be used for Zone Redirect or by the Scheduler
plug-ins to query the children."""
def start(self, zone_list):
self.green_pool = greenpool.GreenPool()
return [ result for result in self.green_pool.imap(
_wrap_method(_process, self), zone_list)]
def process(self, client, zone):
"""Derived class must override."""
pass

View File

@@ -104,7 +104,7 @@ class ZoneManager(object):
"""Keeps the zone states updated.""" """Keeps the zone states updated."""
def __init__(self): def __init__(self):
self.last_zone_db_check = datetime.min self.last_zone_db_check = datetime.min
self.zone_states = {} self.zone_states = {} # { <zone_id> : ZoneState }
self.service_states = {} # { <service> : { <host> : { cap k : v }}} self.service_states = {} # { <service> : { <host> : { cap k : v }}}
self.green_pool = greenpool.GreenPool() self.green_pool = greenpool.GreenPool()