def restore(self, *, actor: ABCActorMixin, slice_obj: ABCSlice): """ Must be invoked after creating reservation from unpickling """ self.actor = actor self.slice = slice_obj if actor is not None: self.logger = self.actor.get_logger() self.approved = False self.previous_resources = None self.bid_pending = False self.dirty = False self.expired = False self.pending_recover = False self.state_transition = False self.service_pending = ReservationPendingStates.None_ if actor is not None: if self.resources is not None: self.resources.restore(plugin=actor.get_plugin(), reservation=self) if self.requested_resources is not None: self.requested_resources.restore(plugin=actor.get_plugin(), reservation=self) if self.approved_resources is not None: self.approved_resources.restore(plugin=actor.get_plugin(), reservation=self)
def register_new_actor(self, *, actor: ABCActorMixin): db = self.get_container_database() db.remove_actor(actor_name=actor.get_name()) db.add_actor(actor=actor) ActorRegistrySingleton.get().unregister(actor=actor) ActorRegistrySingleton.get().register_actor(actor=actor) actor.actor_added() actor.start()
def fill_actor(*, actor: ABCActorMixin) -> ActorAvro: result = ActorAvro() result.set_name(actor.get_name()) result.set_description(actor.get_description()) result.set_type(actor.get_type().value) result.set_online(True) result.set_id(str(actor.get_guid())) result.set_policy_guid(str(actor.get_plugin().get_guid())) return result
def restore(self, *, actor: ABCActorMixin, slice_obj: ABCSlice): """ Must be invoked after creating reservation from unpickling """ super().restore(actor=actor, slice_obj=slice_obj) if actor is not None: if self.callback is not None: self.callback.set_logger(logger=actor.get_logger()) self.policy = actor.get_policy()
def make_plugin_instance(self, *, actor: ABCActorMixin, actor_config: ActorConfig): """ Creates Plugin instance for the Actor @param actor actor @param actor_config actor config @raises ConfigurationException in case of error """ plugin = None if actor.get_plugin() is None: if actor.get_type() == ActorType.Authority: plugin = AuthoritySubstrate( actor=actor, db=None, handler_processor=AnsibleHandlerProcessor()) elif actor.get_type() == ActorType.Orchestrator: plugin = SubstrateMixin(actor=actor, db=None, handler_processor=HandlerProcessor()) elif actor.get_type() == ActorType.Broker: plugin = BasePlugin(actor=actor, db=None, handler_processor=HandlerProcessor()) if plugin is None: raise ConfigurationException( f"Cannot instantiate plugin for actor: {actor_config.get_name()}" ) if plugin.get_database() is None: db = None user = self.config.get_global_config().get_database()[ Constants.PROPERTY_CONF_DB_USER] password = self.config.get_global_config().get_database()[ Constants.PROPERTY_CONF_DB_PASSWORD] db_host = self.config.get_global_config().get_database()[ Constants.PROPERTY_CONF_DB_HOST] db_name = self.config.get_global_config().get_database()[ Constants.PROPERTY_CONF_DB_NAME] if isinstance(plugin, SubstrateMixin): db = SubstrateActorDatabase(user=user, password=password, database=db_name, db_host=db_host, logger=self.logger) else: db = ServerActorDatabase(user=user, password=password, database=db_name, db_host=db_host, logger=self.logger) plugin.set_database(db=db) return plugin
def add_actor(self, *, actor: ABCActorMixin): """ Add an actor @param actor actor """ properties = pickle.dumps(actor) self.db.add_actor(name=actor.get_name(), guid=str(actor.get_guid()), act_type=actor.get_type().value, properties=properties)
def register_management_object(self, *, actor: ABCActorMixin): """ Register Actor Management Object """ module_name = actor.get_management_object_module() class_name = actor.get_management_object_class() mo = ReflectionUtils.create_instance(module_name=module_name, class_name=class_name) mo.set_actor(actor=actor) mo.initialize() self.management_object_manager.register_manager_object(manager=mo)
def fill_actor_from_db(*, actor: ABCActorMixin) -> ActorAvro: result = ActorAvro() result.set_description(actor.get_description()) result.set_name(actor.get_name()) result.set_type(actor.get_type().value) from fabric_cf.actor.core.registry.actor_registry import ActorRegistrySingleton aa = ActorRegistrySingleton.get().get_actor( actor_name_or_guid=actor.get_name()) result.set_online(aa is not None) return result
def register_recovered_actor(self, *, actor: ABCActorMixin): """ Registers a recovered actor. @param actor recovered actor @raises Exception in case of error """ self.logger.debug("Registering a recovered actor") actor.actor_added() self.register_common(actor=actor) self.management_object_manager.load_actor_manager_objects(actor_name=actor.get_name()) self.actor = actor self.start_actor()
def restore(self, actor: ABCActorMixin, slice_obj: ABCSlice): self.actor = actor self.slice_object = slice_obj if actor is not None: self.logger = actor.get_logger() if self.callback is not None: self.callback.set_logger(logger=actor.get_logger()) if actor.get_type() == ActorType.Authority: self.graph = FimHelper.get_arm_graph( graph_id=str(self.dlg_graph_id)) if actor.get_policy() is not None: self.policy = actor.get_policy()
def get_delegation(self, actor: ABCActorMixin) -> ABCDelegation: slice_object = SliceFactory.create(slice_id=ID(), name="inventory-slice") slice_object.set_inventory(value=True) actor.register_slice(slice_object=slice_object) delegation_name = 'primary' dlg_obj = DelegationFactory.create(did=self.adm.get_graph_id(), slice_id=slice_object.get_slice_id(), delegation_name=delegation_name) dlg_obj.set_slice_object(slice_object=slice_object) dlg_obj.set_graph(graph=self.adm) actor.register_delegation(delegation=dlg_obj) return dlg_obj
def register_actor(self, *, actor: ABCActorMixin): """ Registers a new actor: adds the actor to the database, registers actor proxies and callbacks. Must not register the actor with the clock! Clock registration is a separate phase. @param actor actor @raises Exception in case of error """ self.db.add_actor(actor=actor) actor.actor_added() self.register_management_object(actor=actor) self.register_common(actor=actor) self.actor = actor self.start_actor()
def unregister(self, *, actor: ABCActorMixin): try: self.lock.acquire() actor_name = actor.get_name() guid = actor.get_guid() if actor_name in self.actors: self.actors.pop(actor_name) if guid in self.actors_by_guid: self.actors_by_guid.pop(guid) self.callbacks.unregister(actor_name=actor_name) self.proxies.unregister(actor_name=actor_name) finally: self.lock.release()
def do_process_actor(self, *, actor: ABCActorMixin): """ Process Incoming RPC events common for all actors """ processed = True if self.rpc.get_request_type() == RPCRequestType.Query: actor.get_logger().info("processing query from <{}>".format( self.rpc.get_caller().get_name())) result = actor.query(query=self.rpc.get(), caller=self.rpc.get_caller(), id_token=self.rpc.get_id_token()) from fabric_cf.actor.core.kernel.rpc_manager_singleton import RPCManagerSingleton RPCManagerSingleton.get().query_result( actor=actor, remote_actor=self.rpc.get_callback(), request_id=self.rpc.get_message_id(), response=result, caller=actor.get_identity()) elif self.rpc.get_request_type() == RPCRequestType.QueryResult: actor.get_logger().info( "processing query response from <{}>".format( self.rpc.get_caller().get_name())) result = self.rpc.get() if self.rpc.get_response_handler() is not None: handler = self.rpc.get_response_handler() handler.handle(status=self.rpc.get_error(), result=result) else: actor.get_logger().warning( "No response handler is associated with the queryResponse. " "Ignoring queryResponse") else: processed = False return processed
def register_actor(self, *, actor: ABCActorMixin): actor_name = actor.get_name() guid = actor.get_guid() try: self.lock.acquire() if actor_name in self.actors or guid in self.actors_by_guid: raise RegistryException( "An actor name {} already exists".format(actor_name)) entry = self.ActorRegistryEntry(actor=actor) self.actors_by_guid[guid] = entry self.actors[actor_name] = entry finally: self.lock.release()
def unregister_actor(self, *, actor: ABCActorMixin): """ Unregisters the actor from the container. @param actor actor @raises Exception in case of error """ self.management_object_manager.unload_actor_manager_objects(actor_name=actor.get_name()) ActorRegistrySingleton.get().unregister(actor=actor)
def get_ticket(self, units: int, rtype: ResourceType, term: Term, source: ABCDelegation, actor: ABCActorMixin) -> Ticket: resource_ticket = ResourceTicketFactory.create(issuer=self.broker_guid, units=units, term=term, rtype=rtype) ticket = Ticket(resource_ticket=resource_ticket, plugin=actor.get_plugin(), authority=None) ticket.delegation_id = source.get_delegation_id() return ticket
def query(*, actor: ABCActorMixin, actor_proxy: ABCActorProxy, query: dict, id_token: str): handler = MyQueryResponseHandler() actor.query(query=query, actor_proxy=actor_proxy, handler=handler, id_token=id_token) with handler.condition: try: handler.condition.wait() # TODO InterruptedException except Exception as e: raise RPCException(message=str(e), error=RPCError.LocalError) if handler.error is not None: raise handler.error return handler.result
def make_actor_policy(self, *, actor: ABCActorMixin, config: ActorConfig): """ Creates Actor Policy instance @param actor actor @param actor_config actor config @raises ConfigurationException in case of error """ policy = None if actor.get_type() == ActorType.Authority: policy = self.make_site_policy(config=config) elif actor.get_type() == ActorType.Broker: policy = self.make_broker_policy(config=config) elif actor.get_type() == ActorType.Orchestrator: if config.get_policy() is not None: policy = self.make_policy(policy=config.get_policy()) else: policy = ControllerTicketReviewPolicy() if policy is None: raise ConfigurationException( f"Could not instantiate policy for actor: {config.get_name()}") return policy
def register_with_registry(self, *, actor: ABCActorMixin): """ Register an actor with Registry @param actor actor """ try: act_name = actor.get_name() act_type = actor.get_type() act_guid = actor.get_guid() from fabric_cf.actor.core.container.globals import GlobalsSingleton kafka_topic = GlobalsSingleton.get().get_config().get_actor().get_kafka_topic() entry = {self.actor_name: act_name, self.actor_guid: act_guid, self.actor_type: act_type.name, self.actor_protocol: Constants.PROTOCOL_KAFKA, self.actor_location: kafka_topic} self.add_cache_entry(guid=act_guid, entry=entry) # TODO start liveness thread except Exception as e: self.logger.debug("Could not register actor {} with local registry e: {}".format(actor.get_name(), e))
def register_proxies(self, *, actor: ABCActorMixin): """ Registers all proxies for the specified actor. @param actor actor @raises Exception in case of error """ self.logger.debug(f"Registering proxies for actor: {actor.get_name()}") for protocol in self.protocols.values(): self.logger.debug(f"Processing protocol {protocol.get_protocol()}") location = ActorLocation() location.set_descriptor(descriptor=protocol) proxy = ProxyFactorySingleton.get().new_proxy(protocol=protocol.get_protocol(), identity=actor.get_identity(), location=location) if proxy is not None: self.logger.debug(f"Registering proxy {actor.get_name()} for protocol {protocol.get_protocol()}") ActorRegistrySingleton.get().register_proxy(proxy=proxy) callback = ProxyFactorySingleton.get().new_callback(protocol=protocol.get_protocol(), identity=actor.get_identity(), location=location) if callback is not None: self.logger.debug(f"Registering callback {actor.get_name()} for protocol {protocol.get_protocol()}") ActorRegistrySingleton.get().register_callback(callback=callback)
def recover_actor(self, *, actor: ABCActorMixin): """ Recover actor """ actor_name = actor.get_name() actor.set_logger(logger=self.logger) self.logger.info(f"Recover actor: {actor_name}") self.logger.debug("Initializing the actor object") actor.initialize() # By now we have a valid actor object. We need to register it with # the container and call recovery for its reservations. self.register_recovered_actor(actor=actor) self.logger.debug(f"Starting recovery from database for actor {actor_name}") actor.recover() self.register(tickable=actor) self.logger.info(f"Actor {actor_name} recovered successfully")
def get_callback(*, actor: ABCActorMixin, protocol: str) -> ABCCallbackProxy: """ Obtains a callback for the specified actor @param actor actor @param protocol protocol @return ICallbackProxy """ if actor is None: raise ProxyException( Constants.NOT_SPECIFIED_PREFIX.format("actor")) callback = ActorRegistrySingleton.get().get_callback( protocol=protocol, actor_name=actor.get_name()) if callback is None: raise ProxyException( "Could not obtain callback proxy: protocol={}".format( protocol)) return callback
def set_actor(self, *, actor: ABCActorMixin): if self.actor is None: self.actor = actor self.db = actor.get_plugin().get_database() self.logger = actor.get_logger() self.id = actor.get_guid()
def get_kernel_wrapper(self, *, actor: ABCActorMixin) -> KernelWrapper: wrapper = KernelWrapper(actor=actor, plugin=actor.get_plugin(), policy=actor.get_policy()) return wrapper
def do_dispatch_incoming_rpc(self, *, actor: ABCActorMixin, rpc: IncomingRPC): # see if this is a response for an earlier request that has an # associated handler function. If a handler exists, attach the handler # to the incoming rpc object. request = None if rpc.get_request_id() is not None: request = self.remove_pending_request(guid=rpc.get_request_id()) if request is not None: if request.timer: actor.get_logger().debug("Canceling the timer: {}".format( request.timer)) request.cancel_timer() if request.handler is not None: rpc.set_response_handler(response_handler=request.handler) if rpc.get_request_type() == RPCRequestType.Query: actor.get_logger().info("Inbound query from <{}>".format( rpc.get_caller().get_name())) elif rpc.get_request_type() == RPCRequestType.QueryResult: actor.get_logger().info("Inbound query response from <{}>".format( rpc.get_caller().get_name())) if request is None: actor.get_logger().warning( "No queryRequest to match to inbound queryResponse. Ignoring response" ) elif rpc.get_request_type() == RPCRequestType.ClaimDelegation: actor.get_logger().info( "Inbound claim delegation request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.ReclaimDelegation: actor.get_logger().info( "Inbound reclaim delegation request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.Ticket: actor.get_logger().info( "Inbound ticket request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.ExtendTicket: actor.get_logger().info( "Inbound extend ticket request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.Relinquish: actor.get_logger().info( "Inbound relinquish request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.UpdateTicket: actor.get_logger().info( "Inbound update ticket request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.UpdateDelegation: actor.get_logger().info( "Inbound update delegation request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.Redeem: actor.get_logger().info( "Inbound redeem request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.ExtendLease: actor.get_logger().info( "Inbound extend lease request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.Close: actor.get_logger().info( "Inbound close request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.UpdateLease: actor.get_logger().info( "Inbound update lease request from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) elif rpc.get_request_type() == RPCRequestType.FailedRPC: actor.get_logger().info("Inbound FailedRPC from <{}>:{}".format( rpc.get_caller().get_name(), rpc.get())) if rpc.get_request_type() == RPCRequestType.FailedRPC: actor.get_logger().debug("Failed RPC") failed = None exception = RPCException(message=rpc.get_error_details(), error=RPCError.RemoteError) if request is not None: if request.proxy.get_identity() == rpc.get_caller(): failed = FailedRPC(e=exception, request=request) else: actor.get_logger().warning( "Failed RPC from an unauthorized caller: expected={} but was={}" .format(request.proxy.get_identity(), rpc.get_caller())) elif rpc.get_failed_reservation_id() is not None: failed = FailedRPC(e=exception, request_type=rpc.get_failed_request_type(), rid=rpc.get_failed_reservation_id(), auth=rpc.caller) else: failed = FailedRPC(e=exception, request_type=rpc.get_failed_request_type(), auth=rpc.caller) if failed is not None: actor.queue_event( incoming=FailedRPCEvent(actor=actor, failed=failed)) else: actor.get_logger().debug("Added to actor queue to be processed") actor.queue_event(incoming=IncomingRPCEvent(actor=actor, rpc=rpc))
def __init__(self, *, actor: ABCActorMixin): super().__init__(auth=actor.get_identity()) self.logger = actor.get_logger() self.proxy_type = Constants.PROTOCOL_LOCAL