Supervision and Monitoring in Akka
It's important to know your Akka actor hierarchy so you know how to react to failures. This guide covers the actor system and how you can supervise parents and children.
Join the DZone community and get the full member experience.
Join For FreeSupervision describes a dependency relationship between actors: the parent and child relationship. A parent is unique because it created the child actor, so the parent is responsible for reacting when failures happen in the child.
And the parent decides which choice needs to be selected. When a parent receives the failure signal from its child, depending on the nature of the failure, the parent decides from following options:
Resume: Parent starts the child actor, keeping its internal state.
Restart: Parent starts the child actor by clearing its internal state.
Stop: Stop the child permanently.
Escalate: Escalate the failure by failing itself and propagating the failure to its parent.
Akka Actor Lifecycle
It is always important to view all parts of the supervision hierarchy, which explains the escalate option. Each supervisor should cover all possible failure cases.
Actor System
/user: The User Guardian Actor
An actor created using system.actorOf() are children of the user guardian actor. Whenever a user guardian terminates, all user-created actors will be terminated, too. Top-level user-created actors are determined by the user guardian actor as to how they will be supervised. Root Guardian is the supervisor of the user guardian.
/root: The Root Guardian
The root guardian actor is the father of the actor system. It supervises the user guardian actor and system guardian actor.
Supervision Strategies
There are two types of supervision strategies that we follow to supervise any actor:
One-For-One
One-For-All
case object ResumeException extends Exception
case object StopException extends Exception
case object RestartException extends Exception
override val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 second){
case ResumeException => Resume
case RestartException => Restart
case StopException => Stop
case _: Exception => Escalate
}
Example of One-For-One Strategy
What is Monitoring?
Lifecycle Monitoring in Akka is usually referred to as DeathWatch
Monitoring is thus used to tie one actor to another so that it may react to the other actor’s termination, in contrast to supervision which reacts to failure.
Monitoring is particularly useful if a supervisor cannot simply restart its children and has to terminate them, such as in the case of errors during actor initialization. In that case, it should monitor those children and re-create them or schedule itself to retry this at a later time.
Code Repository: Supervision and Monitoring
Published at DZone with permission of Prabhat Kashyap, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments