0

I am trying to conditionally change an actor's Recovery object. When an actor spins up and there are no snapshots or events that have been persisted, I restart the actor. Upon restarting I want to change the Recovery object. This is how I currently have my actor set up:

protected Recovery _recovery = Recovery.Default;
public override Recovery Recovery => _recovery;

And in the PostRestart() method, I have:

protected override void PostRestart(Exception reason)
{
    // EmptyJournalException is a custom exception
    if (reason is EmptyJournalException)
    {
        // Other code here...
        _recovery = new Recovery(SnapshotSelectionCriteria.None);
    }
}

However, when going through the recovery process again, it is still applying snapshots. If I change the value of _recovery to ignore snapshots in the constructor, it works as expected. However, when setting it in PostRestart(), it gets ignored.

What is the best way to conditionally set the Recovery property? I am wanting to only set it when restarting the actor because of an EmptyJournalException.

user247702
  • 23,641
  • 15
  • 110
  • 157
  • Why are you using exceptions to control flow of a recovery process? It's one of the first antipatterns from 101 courses about object oriented programming. – Bartosz Sypytkowski Sep 17 '18 at 14:26
  • @Horusiath, when an actor spins up and there is nothing persisted for that actor, I want to recover from a different actor (an aggregate). This was working great, but then I realized it was also trying to apply snapshots from the aggregate actor. I was wanting to prevent that. How would you propose I handle that instead? – Nicholas Reynolds Sep 17 '18 at 14:43
  • After persistent actor recovery process finishes, an [OnReplaySuccess](http://getakka.net/api/Akka.Persistence.Eventsourced.html#Akka_Persistence_Eventsourced_OnReplaySuccess) method is called - you can combine that easily with [LastSequenceNr](http://getakka.net/api/Akka.Persistence.Eventsourced.html#Akka_Persistence_Eventsourced_LastSequenceNr) to determine if actor managed to store any events. LastSequenceNr is a monotonic counter which is incremented with every persisted event and restored as part of recovery process. If it's 0, it means that no event has been persisted by that actor. – Bartosz Sypytkowski Sep 17 '18 at 18:15

0 Answers0