NRP Backend Components

REST Server

The REST Server implemented in the package hbp_nrp_backend. It takes care of exposing the execution of npr-core based experiment as REST resources. It leverages the Flask and Flask-Restful frameworks to implement REST Services that can create, manage, and stop experiments (see REST API).

One of its responsibilities is to manage the local execution environment in the context of which the Simulation Server will run the experiment. When serving an experiment launching request, it takes care of fetching the experiment data from the Storage Server, for the use of the Simulation Server. Once the simulation has successfully (or unsuccessfully) terminated, it upload the simulation logs to the Storage Server for the user to inspect.

Another responsibility is, once having prepared the execution environment, to spawn a Simulation Server that will execute the nrp-core based simulation script controlling the requested experiment.

../../_images/launch_experiment_backend.png

Fig. 3 Experiment launching (REST Server)

The sequence diagram Fig. 3, depicts such processes.

In the REST Server, the lifecycle of an experiment simulation is governed by an instance of SimulationLifecycle; in particular, by its specialization BackendSimulationLifecycle. For details see the relative section of this manual.

Simulation Server

The purpose of the Simulation Server is to run nrp-core based python scripts from a local directory prepared by the REST Server. When the launching of an experiment is requested, it gets spawned, as sub process, by the REST Server. It, then, loads and executes the experiment script (e.g. main_script.py) until completion or user request of stopping it. While running, the execution can be paused.

Note

The Simulation Server can pause and stop the execution of nrp-core based python script; i.e. python scripts with the following structure. It is required to use the function nrp.run_loop to loop over simulation timesteps or until the exception NRPSimulationTimeout is raised.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
   # Run a simulation until timeout

   # log to file with file_logger
   # change log level with file_logger.setLevel(level)
   # e.g. file_logger.setLevel(logging.INFO)
   # see https://docs.python.org/3/library/logging.html#logging-levels

   try:
      while True:

         _ = nrp.run_loop()

   except NRPSimulationTimeout:
      file_logger.debug("DEBUG NRPSimulationTimeout reached in script")

The execution can be controlled because any programmatic interaction with nrp-core python client (i.e. nrp_client) is mediated by the class NRPCoreWrapper. This class wraps an instance of NrpCore intercepting any call and, before forwarding the call to the wrapped instance, it performs all the required management tasks (e.g. time keeping, checking whether pause or stop has been requested).

The sequence diagram in Fig. 4, depicts the process of launching an experiment. It is the continuation of Fig. 3.

../../_images/launch_experiment_simserver.png

Fig. 4 Experiment launching (Sim Server)

The lifecycle of a Simulation Server is governed by an instance of SimulationLifecycle, in particular by its specialization SimulationServerLifecycle. For details see the relative section of this manual.