3

I've read the Akka.Persistence intro at the Petabridge blog, and I find this part of the code a little confusing:

    Recover<string>(str => _msgs.Add(str)); // from the journal
    Recover<SnapshotOffer>(offer => {
        var messages = offer.Snapshot as List<string>;
        if(messages != null) // null check
            _msgs = _msgs.Concat(messages);
    });

What you probably want to do on recovery is... first take the last snapshot, then replay messages from the journal on top of it.

But here we have two Recover() declarations, and the journal one is first. When Akka .NET executes recovery, how does the order of these Recover() methods actually play out in practice?

Gigi
  • 28,163
  • 29
  • 106
  • 188

1 Answers1

2

Recover is used only to register message handler within declared actor, the same way as Receive in ReceiveActor works. Therefore order of declaring recovery handlers doesn't matter.

From comment below:

During recovery persistent actor first asks if there are any snapshots, it can use to recover from - therefore SnapshotOffer will always be triggered before rest of events. Then it asks for events that occurred after snapshot, it received. They will be processed in order, they come from event journal and processed by the first matching Recover handler.

Bartosz Sypytkowski
  • 7,463
  • 19
  • 36
  • No, `Command` is used to register the message handler. `Recover` is something else. – Gigi Aug 10 '16 at 14:37
  • `Command` is used to register message handlers. `Recover` is used to register event handlers (objects stored inside event journal), that gets replayed once persistent actor is recovering its state. Sorry I didn't write that, it seemed to be self explanatory in your use case. – Bartosz Sypytkowski Aug 10 '16 at 18:34
  • OK, still, given that you have two Recover() declarations, how does Akka .NET decide which to do first? – Gigi Aug 11 '16 at 07:21
  • During recovery persistent actor first asks if there are any snapshots, it can use to recover from - therefore `SnapshotOffer` will always be triggered before rest of events. Then it asks for events that occurred after snapshot, it received. They will be processed in order, they come from event journal and processed by the first matching `Recover` handler. – Bartosz Sypytkowski Aug 11 '16 at 09:40
  • Good, why don't you incorporate this explanation in your answer (with maybe a reference to back it up)? – Gigi Aug 11 '16 at 16:12