Reputation: 415
I am trying to Supervisor Strategy to work. Here is an illustration of the scenario.
A diagram of a demo app the implements a SupversiorStrategy
I have an actor, FloorTrader that upon OnReceive(object message), creates a FederalRegulator Actor:
var regulator = Context.ActorOf(Props.Create(() => new FederalRegulator("EAST_USA",trade)), "EastFedRegulator");
Then, upon construction, FederalRegulator creates StateRegulators
public class FederalRegulator : RegulatorBase
{
public FederalRegulator(string name, Trade trade) : base(name, trade)
{
StateRegulate(trade);
}
protected override void OnReceive(object message)
{
var msg = message as string;
if(!string.IsNullOrEmpty(msg) && msg.ToUpper().Equals("STOP"))throw new TradeException("No Trading Today");
}
private static void StateRegulate(Trade trade)
{
Context.ActorOf(Props.Create(() => new StateRegulator("NY", trade)), "NYRegulator");
Context.ActorOf(Props.Create(() => new StateRegulator("MA", trade)), "MARegulator");
Context.ActorOf(Props.Create(() => new StateRegulator("CT", trade)), "CTRegulator");
}
}
All the Regulators emit Console.Write() behavior upon construction like so:
public abstract class RegulatorBase : UntypedActor
{
protected RegulatorBase(string name, Trade trade)
{
Name = name;
Trade = trade;
Regulate(Name, Trade);
}
public string Name { get; private set; }
public Trade Trade { get; private set; }
protected void Regulate(string name, Trade trade)
{ // Create a timer
var myTimer = new System.Timers.Timer();
// Tell the timer what to do when it elapses
myTimer.Elapsed += delegate { Console.WriteLine("{0} is regulating the trade for, {1} ", Name,Trade.Ticker); };
// Set it to go off every 1/2 second,
myTimer.Interval = 500;
// And start it
myTimer.Enabled = true;
}
protected override void OnReceive(object message)
{
//put stuff in later
}
}
The implementation of SupervisionStrategy() in the FloorTrader actor is:
protected override SupervisorStrategy SupervisorStrategy()
{
return new OneForOneStrategy(
0, // maxNumberOfRetries
TimeSpan.FromSeconds(1), // duration
x =>
{
if (x is TradeException)
{
Console.WriteLine("---BLOW UP-----");
return Directive.Stop;
}
return Directive.Restart;
});
}
When the FederalRegulator receives a STOP message, it fires a custom exception, TradeException, as is shown above in the FederalRegulator code.
The output before a fire the STOP message is the expected:
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
NY is regulating the trade for, HP
CT is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
MA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
My thinking is that because I am using a OneForOneStrategy, once I fire the STOP message, the FederalRegulator actor, the one emitting , EAST_USA is regulating the trade for, HP
should stop, but its children, the StateRegulators should keep on going.
HOWEVER, when I fire the STOP message using: regulator.Tell("STOP");
TradeException gets thrown, but the FederalRegulator keeps emitting. In addition, I am getting Dead Letter messages:
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
---BLOW UP-----
[ERROR][2/27/2016 12:18:49 AM][Thread 0011][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] No Trading Today
Cause: AkkaNetDemo.Exceptions.TradeException: No Trading Today
at AkkaNetDemo.Regulators.FederalRegulator.OnReceive(Object message) in c:\Users\Bob\Documents\GitHub\AkkaNetDemo\AkkaNetDemo\Regulators\FederalRegulator.cs:line 20
at Akka.Actor.UntypedActor.Receive(Object message)
at Akka.Actor.ActorBase.AroundReceive(Receive receive, Object message)
at Akka.Actor.ActorCell.ReceiveMessage(Object message)
at Akka.Actor.ActorCell.Invoke(Envelope envelope)
[INFO][2/27/2016 12:18:49 AM][Thread 0013][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] Message DeathWatchNotification from akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator to akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator was not delivered. 1 dead letters encountered.
[INFO][2/27/2016 12:18:49 AM][Thread 0012][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] Message DeathWatchNotification from akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator to akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator was not delivered. 2 dead letters encountered.
[INFO][2/27/2016 12:18:49 AM][Thread 0011][akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator] Message DeathWatchNotification from akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator to akka://TradingSystem/user/MyBroker/SellFloorTrader/EastFedRegulator was not delivered. 3 dead letters encountered.
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
MA is regulating the trade for, HP
NY is regulating the trade for, HP
Enter Trade: EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
MA is regulating the trade for, HP
NY is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
CT is regulating the trade for, HP
NY is regulating the trade for, HP
MA is regulating the trade for, HP
EAST_USA is regulating the trade for, HP
Can anybody help me figure out the error of my ways. From what I have been reading, when one uses a OneForOneStrategy()
, the parent should stop and the children will continue.
Upvotes: 2
Views: 607
Reputation: 7542
Fact that you've received message in dead letters means, that actor has been stopped correctly.
However I think, that the major problem here could be caused by the fact, that you're using Timer
class - which is a disposable resource - without disposing it and unpinning the delegates, which may result in emitting console writes even after an actor is dead.
In general you probably don't want to use timers, as Akka.NET gives you a free scheduler feature, which supports sending messages or executing actions in delayed or repeatable fashion. It supports cancellation too.
Also:
My thinking is that because I am using a OneForOneStrategy, once I fire the STOP message, the FederalRegulator actor, the one emitting , EAST_USA is regulating the trade for, HP should stop, but its children, the StateRegulators should keep on going.
In case of OneForOneStrategy
, the resulting directive will be applied to an actor, but that means it will also have consequences in it's children. If actor will be stopped, it's children will be too, as they cannot live without the parent. Difference between one-for-one and all-for-one is that all-for-one rule works horizontally (in case of actor failure it will be applied to all of it's siblings).
Upvotes: 2