Akka.Net (Hierarchy and Supervision)

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

In this post we will cover the concept hierarchy and supervision. In the Actor pattern all actors can be supervisors of child actors. Actors we create are themselves being supervised by the guardians of the system, the most immediate parent being the /user actor.

The relationship between parent and child is established by creating the child in the context of the parent.

[sourcecode language="csharp"]
public class ParentActor : UntypedActor
{
protected override void OnReceive(object message)
{
var childActor = Context.ActorOf(Props.Create<ChildActor>()
, "child1");
childActor.Tell("Hi!");
}
}
[/sourcecode]

The parent will being notified when the child generates an unhandled exception. Parent is  in control of how to handle the exception. The parent can perform one of these actions.

  • If the error is not recoverable. Stop the child.
  • Restart the child.
  • Ignore the error.
  • Delegate the decision to the parent.

Important to note that a parent has direct visibility of errors being generated by its immediate children. If no custom strategies are defined the default behaviour is for the child Actor to be restarted also actions enforced by the parent will effect the immediate children. Unless a custom strategy is implemented.

In Akka.NET a parent can define two different strategies.

  • One-for-One. Action is applied to the underlying child.
  • One-for-All. Action is applied to the underlying child and all children being managed by the child.

In the strategy we can define how many times the child actor can fail within a given timestamp before it being stopped.

The following is an example of a custom strategy.

[code language="csharp"]
public class LightException : Exception {}
public class BadException : Exception {}
public class CriticalException : Exception {}
public class ChildActor : UntypedActor
{
protected override void OnReceive(object message)
{
if (message is string)
{
var msg = message as string;
Console.WriteLine(msg);

if (msg == "a")
throw new LightException();

if (msg == "b")
throw new CoreException();

if (msg == "c")
throw new CriticalException();
}
}
}

public class ParentActor : UntypedActor
{
protected override void OnReceive(object message)
{
var childActor = Context.ActorOf(Props.Create<ChildActor>()
, "child1");
childActor.Tell("Hi!");
}

protected override SupervisorStrategy SupervisorStrategy()
{
return new OneForOneStrategy(maxNrOfRetries: 10, withinTimeRange: TimeSpan.FromSeconds(30), localOnlyDecider: x =>
{
// Ignore the error and keep going.
if (x is LightException)
return Directive.Resume;
// Error that we have no idea what to do with
else if (x is CoreException)
return Directive.Escalate;
// Error that we can't recover from,
// stop the failing child
else if (x is BadException)
return Directive.Stop;
// otherwise restart the failing child
else return Directive.Restart;
});
}
}
[/code]

Another Core aspect related to Supervision is the concept of containment. “Unstable” tasks are placed at child nodes and errors generated from these nodes are managed by the immediate parents. Protecting the parents up the hierarchy. Keeping core functionality at the top stable.

e.g. Eg if we have business logic that needs to perform a rest call and based on the result  of this call do a decision. If rest call is placed in a child actor. If the call fails for any reason (Internet connectivity, server error etc). The error is restricted to the actor and its immediate parent. Top level Actors are not effected. This intrinsically  reduces the amount of the code as we don’t need to write defensive code at every layer. Improving the overall readability of the code.

Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.