예제 #1
0
    def new_session(self, session_id, machine_req, force=False):

        #Checking if force is enabled
        if (force):
            LOGGER.debug("Force is enabled for creating new session with id {}".format(session_id))
            #Checking if there is already a session with the given id
            if (session_id in self.registry.keys()):
                #Shutting down old server if any
                if (self.registry[session_id].address):
                    LOGGER.debug("Shutting down current cartesi machine server for session {}".format(session_id))
                    utils.shutdown_cartesi_machine_server(session_id, self.registry[session_id].address)
                    LOGGER.debug("Cartesi machine server for session {} was shut".format(session_id))

        #Registering new session
        self.register_session(session_id, force)

        LOGGER.debug("Acquiring lock for session {}".format(session_id))
        with self.registry[session_id].lock:
            #Instantiate new cartesi machine server
            self.create_new_cartesi_machine_server(session_id)

            #Communication received, create new cartesi machine
            self.create_machine(session_id, machine_req)

            #calculate cartesi machine initial hash
            initial_hash = self.get_machine_root_hash(session_id)

            #Create snapshot
            self.snapshot_machine(session_id)

        LOGGER.debug("Released lock for session {}".format(session_id))

        return initial_hash
def serve(args):
    listening_add = args.address
    listening_port = args.port

    global DEFECTIVE

    #Importing the defective session registry if defective flag is set
    if args.defective:
        DEFECTIVE = True
        from defective_session_registry import SessionRegistryManager
    else:
        from session_registry import SessionRegistryManager

    manager_address = '{}:{}'.format(listening_add, listening_port)
    session_registry_manager = SessionRegistryManager(manager_address)
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    machine_manager_pb2_grpc.add_MachineManagerServicer_to_server(
        _MachineManager(session_registry_manager), server)
    machine_discovery_pb2_grpc.add_MachineDiscoveryServicer_to_server(
        _MachineDiscovery(session_registry_manager), server)

    SERVICE_NAMES = (
        machine_manager_pb2.DESCRIPTOR.services_by_name['MachineManager'].
        full_name,
        machine_discovery_pb2.DESCRIPTOR.services_by_name['MachineDiscovery'].
        full_name,
        reflection.SERVICE_NAME,
    )
    reflection.enable_server_reflection(SERVICE_NAMES, server)
    server.add_insecure_port(manager_address)
    server.start()
    LOGGER.info("Server started, listening on address {} and port {}".format(
        listening_add, listening_port))
    try:
        while True:
            time.sleep(SLEEP_TIME)
    except KeyboardInterrupt:
        LOGGER.info("\nIssued to shut down")

        LOGGER.debug("Acquiring session registry global lock")
        #Acquiring lock to write on session registry
        with session_registry_manager.global_lock:
            LOGGER.debug("Session registry global lock acquired")
            session_registry_manager.shutting_down = True

        #Shutdown all active sessions servers
        for session_id in session_registry_manager.registry.keys():
            LOGGER.debug("Acquiring lock for session {}".format(session_id))
            with session_registry_manager.registry[session_id].lock:
                LOGGER.debug("Lock for session {} acquired".format(session_id))
                if (session_registry_manager.registry[session_id].address):
                    utils.shutdown_cartesi_machine_server(
                        session_id,
                        session_registry_manager.registry[session_id].address)

        shutdown_event = server.stop(0)

        LOGGER.info("Waiting for server to stop")
        shutdown_event.wait()
        LOGGER.info("Server stopped")
예제 #3
0
    def recreate_machine(self, session_id):
        if (session_id not in self.registry.keys()):
            raise SessionIdException("No session in registry with provided session_id: {}".format(session_id))

        #Shutting down old server if any
        if (self.registry[session_id].address):
            utils.shutdown_cartesi_machine_server(session_id, self.registry[session_id].address)

        LOGGER.debug("Acquiring session registry global lock")
        #Acquiring lock to write on session registry
        with self.global_lock:
            LOGGER.debug("Session registry global lock acquired")
            #Cleaning old server session data
            self.registry[session_id].address = None
            self.registry[session_id].cycle = 0
            self.registry[session_id].snapshot_cycle = None

        LOGGER.debug("Cleaned old server session data for session '{}'".format(session_id))

        #Instantiate new cartesi machine server
        self.create_new_cartesi_machine_server(session_id)

        #Communication received, create new cartesi machine using saved parameters
        self.create_machine(session_id, self.registry[session_id].creation_machine_req)