Supervision and Monitoring in Akka


What is supervision?

Describes a dependency relationship between actors, the parent and child releationship. Parent is unique because it has created the child actor, so the parent is responsible for reacting when failures happens in his child.

And parent decides which choice is need to be selected. When a parent receives the failure signal from it’s child, depends on the nature of failure the parent decides from following options:

Resume: Parent starts the child actor keeping it internal state.

Restart: Parent starts the child actor by clearing it’s internal state.

Stop: Stop the child permanently.

Escalate: Escalate the failure by failing itself and propagate failure to its parent.

akka-life-cycle

Akka Actor Life Cycle

It is always important to view an part of supervision hierarchy, which explains the escalate option. Each supervisor should cover with all possible failure cases.

guardians

Actor System

Source: doc.akka.io

/user: The User Guardian Actor

Actor created using system.actorOf() are children of user guardian actor. Whenever user guardian terminates all user created actors will be terminated too. Top level user created actors are determined by user guardian actor that how they will be supervised. Root Guardian is the supervisor of user guardian.

/root: The Root Guardian

The root guardian actor is the father all actor system. It supervises user guardian actor and system guardian actor.

Supervision Strategies

There are two type of supervision strategies that we follows to supervise any actor:

  1. One-For-One Strategy

  2. One-For-All Strategy

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

Monitoring is particularly useful if a supervisor cannot simply restart its children and has to terminate them, e.g. in 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  

References: akka.io/docs


KNOLDUS-advt-sticker

Advertisements

About Prabhat Kashyap

Consultant(Engineer) at Knoldus Software LLP
This entry was posted in Akka, Scala and tagged , , , . Bookmark the permalink.

5 Responses to Supervision and Monitoring in Akka

  1. Prabhat Kashyap says:

    Reblogged this on Prabhat Kashyap – Scala-Trek.

  2. Pingback: Getting Started with Actors [Akka in a Nutshell #2] | Knoldus

  3. Pingback: Introduction to Actor Model [Akka in a Nutshell #1] | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s