Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow persisting events when recovery has completed #3366

Merged
merged 1 commit into from
Jun 25, 2018

Conversation

ismaelhamed
Copy link
Member

@ismaelhamed ismaelhamed commented Mar 17, 2018

DO NOT MERGE before #3372

This PR adds support for persisting when recovery is completed, before any commands are received. It's still not possible to persist new events when replaying events. Replaying events should typically be free of external side effects so it doesn't make much sense to support persisting during the actual replay, but there might be some use cases for doing it right afterwards.

Original issues: #21736 and #22609
Original PR: #21893

  • core implementation
  • unit tests

@@ -17,17 +17,15 @@ namespace Akka.Persistence

internal class EventsourcedState
{
public EventsourcedState(string name, bool isRecoveryRunning, StateReceive stateReceive)
public EventsourcedState(string name, Func<bool> isRecoveryRunning, StateReceive stateReceive)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best option?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: how often is the EventsourcedState object allocated? From a functional standpoint it should be fine, but if we allocate a new EventsourcedState instance regularly then anonymous method allocations could add up. I'm not familiar enough with Akka.Persistence's internals to know off-hand. Do you or @Horusiath have some idea?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aaronontheweb not sure about this one, honestly. Maybe @Horusiath wants to weigh in.


return new EventsourcedState("replay started", true, (receive, message) =>
return new EventsourcedState("replay started", () => recoveryRunning, (receive, message) =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implicitly capture closure

@Aaronontheweb
Copy link
Member

@ismaelhamed still waiting on more changes first?

@ismaelhamed
Copy link
Member Author

@Aaronontheweb code is complete, but I'd appreciate comments on those remarks. Until then, this is still WIP

@Aaronontheweb
Copy link
Member

Thanks, I'll take a look


public bool IsRecoveryRunning { get; }

public Func<bool> IsRecoveryRunning { get; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the API?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, this is an internal API. None of the third party Akka.Persistence plugins have access to this so we're able to preserve binary compatibility here. It's only the Akka.Persistence engine itself that consumes this directly so I think you're fine.

Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some questions about the specs and performance impacts that I'd like to have answered before pushing this through into a patch.

ExpectMsgInOrder("a-1", "a-2", "rc-1", "rc-2", "rc-3");
persistentActor.Tell(new Cmd("invalid"));
persistentActor.Tell(GetState.Instance);
ExpectMsgInOrder("a-1", "a-2", "rc-1", "rc-2", "rc-3", "invalid");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ismaelhamed shouldn't there be an invalid-recovery event recovered here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aaronontheweb No, the point of this PR is twofold: First, prevent commands from interrupting the events being persisted in RecoveryCompleted. Second, make sure an actor blows up if Persist() is invoked when replaying events (due to "invalid-recovery" event in this case).

So basically, persist is allowed in RecoveryCompleted, but it will fail-fast during recovery. You'll get a nice exception along with the intrusive ("invalid") message in OnRecoveryFailure though.


public bool IsRecoveryRunning { get; }

public Func<bool> IsRecoveryRunning { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, this is an internal API. None of the third party Akka.Persistence plugins have access to this so we're able to preserve binary compatibility here. It's only the Akka.Persistence engine itself that consumes this directly so I think you're fine.

{
try
{
if (message is ReplayedMessage)
switch (message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change

@@ -17,17 +17,15 @@ namespace Akka.Persistence

internal class EventsourcedState
{
public EventsourcedState(string name, bool isRecoveryRunning, StateReceive stateReceive)
public EventsourcedState(string name, Func<bool> isRecoveryRunning, StateReceive stateReceive)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: how often is the EventsourcedState object allocated? From a functional standpoint it should be fine, but if we allocate a new EventsourcedState instance regularly then anonymous method allocations could add up. I'm not familiar enough with Akka.Persistence's internals to know off-hand. Do you or @Horusiath have some idea?

@Aaronontheweb
Copy link
Member

Left some review notes for you @ismaelhamed

persistentActor.Tell(GetState.Instance);
ExpectMsgInOrder("a-1", "a-2", "rc-1", "rc-2");
persistentActor.Tell(GetState.Instance);
ExpectMsgInOrder("a-1", "a-2", "rc-1", "rc-2", "rc-3");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some race condition (at least in the JVM) between the GetState and the PersistAsync, so they've simplified the test like this:

expectMsgAnyOf(List("a-1", "a-2", "rc-1", "rc-2"), List("a-1", "a-2", "rc-1", "rc-2", "rc-3"))

But I'm not sure this is possible with the current ExpectMsgAnyOf in Akka.NET

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aaronontheweb about this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw the comment - the test passes currently, right? If it's going to be racy, do we need to implement some new features in the TestKit in order to do this correctly? What's the issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test runs fine, at least every time I've run it. But I noticed they (JVM) changed it since the original PR for the reason I mentioned, just so you know.

@Aaronontheweb
Copy link
Member

Bleh, I forgot to come back and revisit this - will do

@Aaronontheweb Aaronontheweb self-assigned this Jun 7, 2018
@Aaronontheweb Aaronontheweb added this to the 1.3.9 milestone Jun 7, 2018
@ismaelhamed ismaelhamed force-pushed the wip-persistence branch 2 times, most recently from 68c50a3 to 5980c05 Compare June 10, 2018 11:05
Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Just rebased on latest dev changes. Ready for merge when those specs come back.

@ismaelhamed ismaelhamed changed the title [WIP] Allow persisting events when recovery has completed Allow persisting events when recovery has completed Jun 22, 2018
@Aaronontheweb Aaronontheweb merged commit fdc1765 into akkadotnet:dev Jun 25, 2018
@Aaronontheweb
Copy link
Member

Thanks for your contribution @ismaelhamed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants