Another feature of PyActive, is that it provides a supervisor. The supervisor warns supervisor actor, when an Actor fails, if it is one that you decided supervise. Therefore, you need to create a Actor charged to supervise, and tell to him all the actors that going to supervise.

How construct a supervisor actor? We will create a normal actor like always, but into the actor object, we will instantiate a new object called Supervisor ( the Supervisor class is created like Multi class). Once, the Supervisor object will have been initialized, it starts to send keep alive message, in an internally way, to Actors assigned.

How can we assign Actors to supervise in the Supervisor object? First of all, when we create a Supervisor object, we can pass as a parameter a list of actors to supervise. Moreover, it offers an API that allows us to add more actors. Following, you can see the API of the Supervisor:

  •    def __init__(self, time, retries, owner, actors = []):
  •     def add_actor(self, actor):
  •    def add_actors(self, actors):

As you can see in the API, the Supervisor offers us the possibility to increase the list of actors to supervise in order to attach actors dynamically. Note that the init function ask as a parameters the time, the retries and the owner. The time refers to the time between each time that the supervisor checks the actors. The retries parameter is the number of times that the Supervisor try to connect to an actor which fails, before consider that he is dead. Finally, the owner is a reference to the actor who creates the Supervisor, this reference is used by the Supervisor to comunicate to the owner to tell him the actors failures.

What does the Supervisor when it detects an actor failure? When the supervisor considers that an actor is dead, he calls the failure_detect method that must be implemented by the owner. Hence, the owner actor who have implemented this method, will execute this method each time that one node fails. Therefore, into this method is the site where the developer can code the policy that he believes correct in order to handle a node failure.

The failure_detect method must follow the structure described below:

  • def failure_detect(self, actor_ref):

As you can see, this method receive as a parameter the reference of the actor that fail. 

How the Supervisor sends the message to all the actors? The Supervisor is helped by the Multi class. Using the Multi it sends keep alive message to all Actors easily. Basically, it uses the Synchronous Multi class, because of allows to receive a response from the actors. If one actor not respond, the Supervisor consider it as a failure, and the counter of retries starts to count.

Example

Finally, here you can see an example/test that shows how to use the supervisor:

class Server(object):

    _sync = {'add_actors':'20'}
    _async = ['add', 'sync_add','print_references']
    _ref = ['add_actors']
    _parallel = []
   
    def add_actors(self, list_actors):
        self.supervisor = Supervisor(5, 3, self._atom, list_actors)
        self.amulti = AMulti(list_actors)
        self.smulti = SMulti(list_actors, self._atom)
        return True
    def add(self, x, y):
        self.amulti.add(x, y)
   
    def sync_add(self, x, y):
        result =  self.smulti.sync_add(x, y)
        print result.values()
    def print_references(self):
        print self.smulti.get_reference().values()
    def failure_detect(self, from_actor):
        print 'failure', from_actor   

In the example we can see that four parameters are needed to initialize the supervisor. First parameter indicate the interval time to call actors, second parameter indicate the retries before to alert of a fail, third parameter is the owner actor of this active object, and the last parameter is the list of Actors to supervise. Last method is the failure_detect, that we must implement. In this example, this method only prints the reference of the actor who fails.

If you want check this example, or use it to practice with the supervisor class, you can find the full example in examples folder -> test_supervisor