def _call_from_coordaddr(self, *args, **kwargs): try: servers = self._easy_locator._get_server_from_coordaddr( self._other_coord, server_type, self._how_many) except LocatorErrors.NoServerFoundError: log.log( EasyLocator, log.level.Error, "Can't get %s servers! Error in get_server_from_coordaddr " % server_type) raise LocatorErrors.UnableToCompleteOperationError( "Couldn't connect to %s" % server_type) tested = 0 for server in servers: tested += 1 try: return getattr(server, method)(*args, **kwargs) except ProtocolErrors.RemoteError: log.log(EasyLocator, log.level.Warning, "%s failed in reserve_session" % server_type) log.log_exc(EasyLocator, log.level.Warning) self._easy_locator.inform_server_not_working( server, server_type, ()) log.log( EasyLocator, log.level.Error, "Can't get a %s server! Error in get_server after testing %s servers " % (server_type, tested)) raise LocatorErrors.UnableToCompleteOperationError( "Couldn't connect to any %s" % server_type)
def _retrieve_all_servers_from_coordinator(self, original_server_address, server_type, restrictions): try: return self._coordinator.get_all_servers(original_server_address, server_type, restrictions) except ProtocolErrors.ProtocolError as pe: # TODO: not unittested log.log( ServerLocator, log.level.Error, "Problem while asking for all servers to the coordinator server. %s" % pe) log.log_exc(ServerLocator, log.level.Warning) raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Couldn't retrieve all servers from coordinator server: " + str(pe), pe) except Exception as e: # TODO: not unittested log.log( ServerLocator, log.level.Error, "Unexpected exception while asking for all servers to the coordinator server. %s" % e) log.log_exc(ServerLocator, log.level.Warning) raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Unexpected exception while asking all servers from coordinator server: " + str(e), e)
def _call(self, *args, **kwargs): wrong_servers = [] while True: try: server = self._easy_locator.get_server(server_type) except LocatorErrors.NoServerFoundError: log.log( EasyLocator, log.level.Error, "Can't get a %s server! Error in get_server " % server_type) raise LocatorErrors.UnableToCompleteOperationError( "Couldn't connect to %s" % server_type) try: return getattr(server, method)(*args, **kwargs) except ProtocolErrors.RemoteError as re: log.log(EasyLocator, log.level.Warning, "%s failed in reserve_session" % server_type) if not server in wrong_servers: wrong_servers.append(server) self._easy_locator.inform_server_not_working( server, server_type, ()) else: log.log( EasyLocator, log.level.Error, "Locator provides twice the same server that fails in reserve_session! Can't use %s" % server_type) raise LocatorErrors.UnableToCompleteOperationError( "Error retrieving reserve_session answer", re)
def _retrieve_networks_from_coordinator(self, original_server_address, server_coord_address): try: return self._coordinator.get_networks(original_server_address, server_coord_address) except ProtocolErrors.ProtocolError as pe: # TODO: not unittested log.log( ServerLocator, log.level.Error, "Problem while asking for networks to the coordinator server. %s" % pe) log.log_exc(ServerLocator, log.level.Warning) raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Couldn't retrieve networks from coordinator server: " + str(pe), pe) except Exception as e: # TODO: not unittested log.log( ServerLocator, log.level.Error, "Unexpected exception while asking for networks to the coordinator server. %s" % e) log.log_exc(ServerLocator, log.level.Warning) import traceback traceback.print_exc() raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Unexpected exception while asking for networks from coordinator server: " + str(e), e)
def _retrieve_session_id_from_coordinator(self, original_server_address, server_type, restrictions): try: return self._coordinator.new_query(original_server_address, server_type, restrictions) except ProtocolErrors.ProtocolError as pe: log.log( ServerLocator, log.level.Error, "Problem while asking for new session id to the coordinator server. %s" % pe) log.log_exc(ServerLocator, log.level.Warning) raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Couldn't retrieve new session id from coordinator server: " + str(pe), pe) except Exception as e: log.log( ServerLocator, log.level.Error, "Unexpected exception while asking for new session id to the coordinator server. %s" % e) log.log_exc(ServerLocator, log.level.Warning) raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Unexpected exception while asking new session id from coordinator server: " + str(e), e)
def __init__(self, server_type_module, methods): """ server_type_type is a Enumeration-created type. For example: voodoo.gen.protocols.protocols.Protocols (not voodoo.gen.protocols.protocols, that's only a module). The second parameter, "methods", must be a dictionary like: { EnumerationMember1Name : 'method_name1', EnumerationMember2Name : 'method_name2', ... } """ for klass in methods: if not isinstance(methods[klass], (list, tuple)): raise LocatorErrors.InvalidListOfMethodsError( "Invalid format at ServerTypeHandler. Expected tuple or list, found: %s" % methods[klass]) # for klass in methods: # if not hasattr(server_type_type, klass): # raise LocatorErrors.MoreServersThanExpectedError("Unexpected class %s for module %s" % (klass, server_type_type)) self._module = server_type_module self._methods = methods
def get_server(self, coord_addr, server_type, restrictions=()): if server_type == ServerType.Translator: if len(self.clients['translators']) > 0: return self.clients['translators'][0] else: raise LocatorErrors.NoServerFoundError() else: return self.clients['laboratories'][0]
def get_server_from_coord_address(self, original_server_address, server_coord_address, server_type, how_many=1): networks = self._retrieve_networks_from_coordinator( original_server_address, server_coord_address) if len(networks) == 0: raise LocatorErrors.NoNetworkAvailableError( "Couldn't find a network for communicating original_server_address '%s' and server_coord_address '%s'" % (original_server_address, server_coord_address)) return self._retrieve_server_instances_from_networks( networks, server_type, how_many)
def _get_server_from_coordinator(self, session_id): try: return self._coordinator.get_server(session_id) except CoordinatorServerErrors.NoServerFoundError as nsfe: raise nsfe except ProtocolErrors.ProtocolError as pe: log.log( ServerLocator, log.level.Error, "Problem while asking for other server to the coordinator server. %s" % pe) log.log_exc(ServerLocator, log.level.Warning) raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Couldn't ask for other server to coordinator server: " + str(pe), pe) except Exception as e: log.log( ServerLocator, log.level.Error, "Unexpected exception while asking for other server to the coordinator server. %s" % e) log.log_exc(ServerLocator, log.level.Warning) raise LocatorErrors.ProblemCommunicatingWithCoordinatorError( "Unexpected exception while asking for other server to the coordinator server: " + str(e), e)
def _save_server_in_cache(self, server, server_type, restrictions): self._cache_lock.acquire() try: if not self._cache.has_key(server_type): self._cache[server_type] = {} server_type_cache = self._cache[server_type] if not server_type_cache.has_key(restrictions): server_type_cache[restrictions] = (server, self._time()) else: raise LocatorErrors.ServerFoundInCacheError( server_type_cache[restrictions][0], "There is already a server for server type %s and restrictions %s" % (server_type, restrictions)) finally: self._cache_lock.release()
def get_all_servers(self, original_server_address, server_type, restrictions=()): if not self._server_type_handler.isMember(server_type): #TODO: not tested raise LocatorErrors.NotAServerTypeError( '%s not a member of %s' % (server_type, self._server_type_handler)) all_servers = self._retrieve_all_servers_from_coordinator( original_server_address, server_type, restrictions) ret_value = [] for server, networks in all_servers: server_instances = self._retrieve_server_instances_from_networks( networks, server_type) ret_value.append((server, server_instances)) return ret_value
def get_no_server(self, coord_address, server_type, restrictions): raise LocatorErrors.NoServerFoundError("lol")
def get_server(self, original_server_address, server_type, restrictions): if not self._server_type_handler.isMember(server_type): raise LocatorErrors.NotAServerTypeError( '%s not a member of %s' % (server_type, self._server_type_handler.module)) server = self._get_server_from_cache(server_type, restrictions) if server is not None: return server # :-) session_id = self._retrieve_session_id_from_coordinator( original_server_address, server_type, restrictions) try: there_are_more_servers = True while there_are_more_servers: try: address = self._get_server_from_coordinator(session_id) except CoordinatorServerErrors.NoServerFoundError: there_are_more_servers = False continue server = self._get_server_from_registry(address) if server is not None: # The server was in the registry but not in the cache; # First check if the server is up and running if not self._test_server(server, address): # There was some error continue # Server up and running :-) # Now add it to the cache and return it try: self._save_server_in_cache(server, server_type, restrictions) except LocatorErrors.ServerFoundInCacheError as server_found_in_cache: # While we were calling the coordinator, some # other thread retrieved the server. Use the # server that was already in the cache return server_found_in_cache.get_server() else: return server # Server was not in the ServerRegistry neither in the cache methods = self._server_type_handler.retrieve_methods( server_type) try: server = address.create_client(methods) except ProtocolErrors.ClientProtocolError as ccce: # TODO: not tested # There was some error creating the client log.log( ServerLocator, log.level.Info, "Generating client for server with %s raised exception %s. Trying another server..." % (address.address, ccce)) log.log_exc(ServerLocator, log.level.Debug) continue # Check if the server is up and running if not self._test_server(server, address): # There was some error continue # Server up and running :-) try: self._save_server_in_registry_and_cache( server, server_type, restrictions, address) except LocatorErrors.ServerFoundInCacheError as e: return e.get_server() else: return server else: raise LocatorErrors.NoServerFoundError( "No server found of type %s and restrictions %s" % (server_type, restrictions)) finally: self._logout_from_coordinator(session_id)
def retrieve_methods(self, server_type): if self._methods.has_key(server_type): return self._methods[server_type] else: raise LocatorErrors.NoSuchServerTypeFoundError( "Server type '%s' not found retrieving methods" % server_type)