X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fmanagers%2Fslice_manager.py;h=be6cc7e291aee4559e7eb0d7a7f15b59a598913d;hb=b6de9285a970eccb9658a0078c49f76fb11af7cd;hp=50edb9a3df2a9c78fce0b4e1f4856a45af697026;hpb=29df906cd2d154a3b90fcb1b7025f3fc1bb577fe;p=sfa.git diff --git a/sfa/managers/slice_manager.py b/sfa/managers/slice_manager.py index 50edb9a3..be6cc7e2 100644 --- a/sfa/managers/slice_manager.py +++ b/sfa/managers/slice_manager.py @@ -12,50 +12,32 @@ from sfa.util.sfalogging import logger from sfa.util.xrn import Xrn, urn_to_hrn from sfa.util.version import version_core from sfa.util.callids import Callids +from sfa.util.cache import Cache from sfa.server.threadmanager import ThreadManager from sfa.rspecs.rspec_converter import RSpecConverter from sfa.rspecs.version_manager import VersionManager from sfa.rspecs.rspec import RSpec + from sfa.client.client_helper import sfa_to_pg_users_arg +from sfa.client.return_value import ReturnValue class SliceManager: - def __init__ (self): - # self.caching=False - self.caching=True + + # the cache instance is a class member so it survives across incoming requests + cache = None + + def __init__ (self, config): + self.cache=None + if config.SFA_SM_CACHING: + if SliceManager.cache is None: + SliceManager.cache = Cache() + self.cache = SliceManager.cache - - def _call_id_supported(self, api, server): - """ - Returns true if server support the optional call_id arg, false otherwise. - """ - server_version = api.get_cached_server_version(server) - - if 'sfa' in server_version: - code_tag = server_version['code_tag'] - code_tag_parts = code_tag.split("-") - - version_parts = code_tag_parts[0].split(".") - major, minor = version_parts[0:2] - rev = code_tag_parts[1] - if int(major) > 1: - if int(minor) > 0 or int(rev) > 20: - return True - return False - - # we have specialized xmlrpclib.ServerProxy to remember the input url - # OTOH it's not clear if we're only dealing with XMLRPCServerProxy instances - def get_serverproxy_url (self, server): - try: - return server.get_url() - except: - logger.warning("GetVersion, falling back to xmlrpclib.ServerProxy internals") - return server._ServerProxy__host + server._ServerProxy__handler - - def GetVersion(self, api): + def GetVersion(self, api, options): # peers explicitly in aggregates.xml - peers =dict ([ (peername,self.get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems() + peers =dict ([ (peername,interface.get_url()) for (peername,interface) in api.aggregates.iteritems() if peername != api.hrn]) version_manager = VersionManager() ad_rspec_versions = [] @@ -65,20 +47,20 @@ class SliceManager: ad_rspec_versions.append(rspec_version.to_dict()) if rspec_version.content_type in ['*', 'request']: request_rspec_versions.append(rspec_version.to_dict()) - default_rspec_version = version_manager.get_version("sfa 1").to_dict() xrn=Xrn(api.hrn, 'authority+sa') version_more = {'interface':'slicemgr', + 'sfa': 2, + 'geni_api': 2, 'hrn' : xrn.get_hrn(), 'urn' : xrn.get_urn(), 'peers': peers, - 'request_rspec_versions': request_rspec_versions, - 'ad_rspec_versions': ad_rspec_versions, - 'default_ad_rspec': default_rspec_version + 'geni_request_rspec_versions': request_rspec_versions, + 'geni_ad_rspec_versions': ad_rspec_versions, } sm_version=version_core(version_more) # local aggregate if present needs to have localhost resolved if api.hrn in api.aggregates: - local_am_url=self.get_serverproxy_url(api.aggregates[api.hrn]) + local_am_url=api.aggregates[api.hrn].get_url() sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname']) return sm_version @@ -98,7 +80,8 @@ class SliceManager: else: stats_tag = rspec.xml.root.add_element("statistics", call=callname) - stat_tag = stats_tag.add_element("aggregate", name=str(aggname), elapsed=str(elapsed), status=str(status)) + stat_tag = stats_tag.add_element("aggregate", name=str(aggname), + elapsed=str(elapsed), status=str(status)) if exc_info: exc_tag = stat_tag.add_element("exc_info", name=str(exc_info[1])) @@ -109,33 +92,34 @@ class SliceManager: # formats the traceback as a set of xml elements tb = traceback.extract_tb(exc_info[2]) for item in tb: - exc_frame = exc_tag.add_element("tb_frame", filename=str(item[0]), line=str(item[1]), func=str(item[2]), code=str(item[3])) + exc_frame = exc_tag.add_element("tb_frame", filename=str(item[0]), + line=str(item[1]), func=str(item[2]), code=str(item[3])) except Exception, e: logger.warn("add_slicemgr_stat failed on %s: %s" %(aggname, str(e))) - def ListResources(self, api, creds, options, call_id): + def ListResources(self, api, creds, options): + call_id = options.get('call_id') + if Callids().already_handled(call_id): return "" + version_manager = VersionManager() - def _ListResources(aggregate, server, credential, opts, call_id): - - my_opts = copy(opts) - args = [credential, my_opts] + + def _ListResources(aggregate, server, credential, options): + forward_options = copy(options) tStart = time.time() try: - if self._call_id_supported(api, server): - args.append(call_id) version = api.get_cached_server_version(server) # force ProtoGENI aggregates to give us a v2 RSpec - if 'sfa' not in version.keys(): - my_opts['rspec_version'] = version_manager.get_version('ProtoGENI 2').to_dict() - rspec = server.ListResources(*args) + if 'sfa' in version.keys(): + forward_options['rspec_version'] = version_manager.get_version('SFA 1').to_dict() + else: + forward_options['rspec_version'] = version_manager.get_version('ProtoGENI 2').to_dict() + rspec = server.ListResources(credential, forward_options) return {"aggregate": aggregate, "rspec": rspec, "elapsed": time.time()-tStart, "status": "success"} except Exception, e: api.logger.log_exc("ListResources failed at %s" %(server.url)) return {"aggregate": aggregate, "elapsed": time.time()-tStart, "status": "exception", "exc_info": sys.exc_info()} - if Callids().already_handled(call_id): return "" - # get slice's hrn from options xrn = options.get('geni_slice_urn', '') (hrn, type) = urn_to_hrn(xrn) @@ -143,13 +127,14 @@ class SliceManager: del(options['geni_compressed']) # get the rspec's return format from options - rspec_version = version_manager.get_version(options.get('rspec_version')) - version_string = "rspec_%s" % (rspec_version.to_string()) + rspec_version = version_manager.get_version(options.get('geni_rspec_version')) + version_string = "rspec_%s" % (rspec_version) # look in cache first - if self.caching and api.cache and not xrn: - rspec = api.cache.get(version_string) + if self.cache and not xrn: + rspec = self.cache.get(version_string) if rspec: + api.logger.debug("SliceManager.ListResources returns cached advertisement") return rspec # get the callers hrn @@ -170,35 +155,39 @@ class SliceManager: # get the rspec from the aggregate interface = api.aggregates[aggregate] server = api.server_proxy(interface, cred) - threads.run(_ListResources, aggregate, server, [cred], options, call_id) + threads.run(_ListResources, aggregate, server, [cred], options) results = threads.get_results() - rspec_version = version_manager.get_version(options.get('rspec_version')) + rspec_version = version_manager.get_version(options.get('geni_rspec_version')) if xrn: result_version = version_manager._get_version(rspec_version.type, rspec_version.version, 'manifest') else: result_version = version_manager._get_version(rspec_version.type, rspec_version.version, 'ad') rspec = RSpec(version=result_version) for result in results: - self.add_slicemgr_stat(rspec, "ListResources", result["aggregate"], result["elapsed"], result["status"], result.get("exc_info",None)) + self.add_slicemgr_stat(rspec, "ListResources", result["aggregate"], result["elapsed"], + result["status"], result.get("exc_info",None)) if result["status"]=="success": try: - rspec.version.merge(result["rspec"]) + rspec.version.merge(ReturnValue.get_value(result["rspec"])) except: api.logger.log_exc("SM.ListResources: Failed to merge aggregate rspec") # cache the result - if self.caching and api.cache and not xrn: - api.cache.add(version_string, rspec.toxml()) + if self.cache and not xrn: + api.logger.debug("SliceManager.ListResources caches advertisement") + self.cache.add(version_string, rspec.toxml()) return rspec.toxml() - - - def CreateSliver(self, api, xrn, creds, rspec_str, users, call_id): + + + def CreateSliver(self, api, xrn, creds, rspec_str, users, options): + call_id = options.get('call_id') + if Callids().already_handled(call_id): return "" version_manager = VersionManager() - def _CreateSliver(aggregate, server, xrn, credential, rspec, users, call_id): + def _CreateSliver(aggregate, server, xrn, credential, rspec, users, options): tStart = time.time() try: # Need to call GetVersion at an aggregate to determine the supported @@ -213,16 +202,12 @@ class SliceManager: rspec.filter(filter) rspec = rspec.toxml() requested_users = sfa_to_pg_users_arg(users) - args = [xrn, credential, rspec, requested_users] - if self._call_id_supported(api, server): - args.append(call_id) - rspec = server.CreateSliver(*args) + rspec = server.CreateSliver(xrn, credential, rspec, requested_users, options) return {"aggregate": aggregate, "rspec": rspec, "elapsed": time.time()-tStart, "status": "success"} except: logger.log_exc('Something wrong in _CreateSliver with URL %s'%server.url) return {"aggregate": aggregate, "elapsed": time.time()-tStart, "status": "exception", "exc_info": sys.exc_info()} - - if Callids().already_handled(call_id): return "" + # Validate the RSpec against PlanetLab's schema --disabled for now # The schema used here needs to aggregate the PL and VINI schemas # schema = "/var/www/html/schemas/pl.rng" @@ -253,29 +238,27 @@ class SliceManager: interface = api.aggregates[aggregate] server = api.server_proxy(interface, cred) # Just send entire RSpec to each aggregate - threads.run(_CreateSliver, aggregate, server, xrn, [cred], rspec.toxml(), users, call_id) + threads.run(_CreateSliver, aggregate, server, xrn, [cred], rspec.toxml(), users, options) results = threads.get_results() manifest_version = version_manager._get_version(rspec.version.type, rspec.version.version, 'manifest') result_rspec = RSpec(version=manifest_version) for result in results: - self.add_slicemgr_stat(result_rspec, "CreateSliver", result["aggregate"], result["elapsed"], result["status"], result.get("exc_info",None)) + self.add_slicemgr_stat(result_rspec, "CreateSliver", result["aggregate"], result["elapsed"], + result["status"], result.get("exc_info",None)) if result["status"]=="success": try: - result_rspec.version.merge(result["rspec"]) + result_rspec.version.merge(ReturnValue.get_value(result["rspec"])) except: api.logger.log_exc("SM.CreateSliver: Failed to merge aggregate rspec") return result_rspec.toxml() - def RenewSliver(self, api, xrn, creds, expiration_time, call_id): - def _RenewSliver(server, xrn, creds, expiration_time, call_id): - server_version = api.get_cached_server_version(server) - args = [xrn, creds, expiration_time, call_id] - if self._call_id_supported(api, server): - args.append(call_id) - return server.RenewSliver(*args) - + def RenewSliver(self, api, xrn, creds, expiration_time, options): + call_id = options.get('call_id') if Callids().already_handled(call_id): return True + + def _RenewSliver(server, xrn, creds, expiration_time, options): + return server.RenewSliver(xrn, creds, expiration_time, options) (hrn, type) = urn_to_hrn(xrn) # get the callers hrn @@ -294,19 +277,18 @@ class SliceManager: continue interface = api.aggregates[aggregate] server = api.server_proxy(interface, cred) - threads.run(_RenewSliver, server, xrn, [cred], expiration_time, call_id) + threads.run(_RenewSliver, server, xrn, [cred], expiration_time, options) # 'and' the results - return reduce (lambda x,y: x and y, threads.get_results() , True) - - def DeleteSliver(self, api, xrn, creds, call_id): - def _DeleteSliver(server, xrn, creds, call_id): - server_version = api.get_cached_server_version(server) - args = [xrn, creds] - if self._call_id_supported(api, server): - args.append(call_id) - return server.DeleteSliver(*args) + results = [ReturnValue.get_value(result) for result in threads.get_results()] + return reduce (lambda x,y: x and y, results , True) + def DeleteSliver(self, api, xrn, creds, options): + call_id = options.get('call_id') if Callids().already_handled(call_id): return "" + + def _DeleteSliver(server, xrn, creds, options): + return server.DeleteSliver(xrn, creds, options) + (hrn, type) = urn_to_hrn(xrn) # get the callers hrn valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0] @@ -324,20 +306,17 @@ class SliceManager: continue interface = api.aggregates[aggregate] server = api.server_proxy(interface, cred) - threads.run(_DeleteSliver, server, xrn, [cred], call_id) + threads.run(_DeleteSliver, server, xrn, [cred], options) threads.get_results() return 1 # first draft at a merging SliverStatus - def SliverStatus(self, api, slice_xrn, creds, call_id): - def _SliverStatus(server, xrn, creds, call_id): - server_version = api.get_cached_server_version(server) - args = [xrn, creds] - if self._call_id_supported(api, server): - args.append(call_id) - return server.SliverStatus(*args) - + def SliverStatus(self, api, slice_xrn, creds, options): + def _SliverStatus(server, xrn, creds, options): + return server.SliverStatus(xrn, creds, options) + + call_id = options.get('call_id') if Callids().already_handled(call_id): return {} # attempt to use delegated credential first cred = api.getDelegatedCredential(creds) @@ -347,10 +326,10 @@ class SliceManager: for aggregate in api.aggregates: interface = api.aggregates[aggregate] server = api.server_proxy(interface, cred) - threads.run (_SliverStatus, server, slice_xrn, [cred], call_id) - results = threads.get_results() + threads.run (_SliverStatus, server, slice_xrn, [cred], options) + results = [ReturnValue.get_value(result) for result in threads.get_results()] - # get rid of any void result - e.g. when call_id was hit where by convention we return {} + # get rid of any void result - e.g. when call_id was hit, where by convention we return {} results = [ result for result in results if result and result['geni_resources']] # do not try to combine if there's no result @@ -371,20 +350,19 @@ class SliceManager: return overall - def ListSlices(self, api, creds, call_id): - def _ListSlices(server, creds, call_id): - server_version = api.get_cached_server_version(server) - args = [creds] - if self._call_id_supported(api, server): - args.append(call_id) - return server.ListSlices(*args) - + def ListSlices(self, api, creds, options): + call_id = options.get('call_id') if Callids().already_handled(call_id): return [] + def _ListSlices(server, creds, options): + return server.ListSlices(creds, options) + # look in cache first - if self.caching and api.cache: - slices = api.cache.get('slices') + # xxx is this really frequent enough that it is worth being cached ? + if self.cache: + slices = self.cache.get('slices') if slices: + api.logger.debug("SliceManager.ListSlices returns from cache") return slices # get the callers hrn @@ -404,22 +382,23 @@ class SliceManager: continue interface = api.aggregates[aggregate] server = api.server_proxy(interface, cred) - threads.run(_ListSlices, server, [cred], call_id) + threads.run(_ListSlices, server, [cred], options) # combime results - results = threads.get_results() + results = [ReturnValue.get_value(result) for result in threads.get_results()] slices = [] for result in results: slices.extend(result) # cache the result - if self.caching and api.cache: - api.cache.add('slices', slices) + if self.cache: + api.logger.debug("SliceManager.ListSlices caches value") + self.cache.add('slices', slices) return slices - def GetTicket(self, api, xrn, creds, rspec, users): + def GetTicket(self, api, xrn, creds, rspec, users, options): slice_hrn, type = urn_to_hrn(xrn) # get the netspecs contained within the clients rspec aggregate_rspecs = {} @@ -439,6 +418,7 @@ class SliceManager: cred = api.getCredential() threads = ThreadManager() for (aggregate, aggregate_rspec) in aggregate_rspecs.iteritems(): + # xxx sounds like using call_id here would be safer # prevent infinite loop. Dont send request back to caller # unless the caller is the aggregate's SM if caller_hrn == aggregate and aggregate != api.hrn: @@ -446,7 +426,7 @@ class SliceManager: interface = api.aggregates[aggregate] server = api.server_proxy(interface, cred) - threads.run(server.GetTicket, xrn, [cred], aggregate_rspec, users) + threads.run(server.GetTicket, xrn, [cred], aggregate_rspec, users, options) results = threads.get_results()