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

Add samples for replicated event sourcing over gRPC #769

Merged
merged 12 commits into from
Jan 25, 2023

Conversation

pvlugter
Copy link
Contributor

@pvlugter pvlugter commented Jan 12, 2023

Trying out the replicated event sourcing over gRPC by modifying a sample. Last commit has just the replication changes. Not completely obvious just based on the current docs. Ended up with similar changes as in lightbend/akka-projection-grpc-benchmark#2 (without looking at that first).

final case class CheckedOut(cartId: String, eventTime: Instant) extends Event

def init(implicit system: ActorSystem[_]): Replication[Command] = {
val projectionProvider: ReplicationProjectionProvider = R2dbcProjection.atLeastOnceFlow(_, None, _, _)(_)
Copy link
Member

Choose a reason for hiding this comment

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

This one we'd want a predefined factory method returning ReplicationProjectionProvider for in akka-projection-r2dbc (suggested by Patrik in the benchmark)

@johanandren johanandren force-pushed the wip-grpc-replicated-es-transport branch from 929b6a6 to 6f24ea9 Compare January 20, 2023 16:36
@pvlugter pvlugter force-pushed the grpc-replicated-es-sample branch 2 times, most recently from e67c958 to e81ec6d Compare January 23, 2023 05:09
@pvlugter
Copy link
Contributor Author

pvlugter commented Jan 23, 2023

Updated the shopping cart sample so that it's convergent — simple counters for the items, and the checkout has a closing step to wait for all replicated events and only complete the checkout on one replica (similar approach as for closing in the auction sample). See e81ec6d.

To be able to do this, needed the actor context from composing with setup behaviour. Hacked in an API for this within the current one — which inverts the external replication setup to be a callback within the factory callback. Maybe a bit convoluted, but needed something. See 3477c3d, and its usage in the sample:

Replication.grpcReplicationWithSetup(replicationSettings)(ShoppingCart.apply)
}
def apply(replicationSetup: Replication.ReplicationSetup[Command, Event, State]): Behavior[Command] = {
Behaviors.setup[Command] { context =>
replicationSetup { replicationContext =>
new ShoppingCart(context, replicationContext).behavior()
}
}
}

@@ -63,24 +64,58 @@ trait Replication[Command] {
@ApiMayChange
object Replication {

trait ReplicatedBehaviorFactory[Command, Event, State] {
Copy link
Member

Choose a reason for hiding this comment

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

This kind of solution would mean that we need another one for timers, but maybe that is fine?

I think it should go in the replicated entity APIs in core Akka rather than here though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can be used with timers as well. Works with any behaviour composition. So can do this too:

Behaviors.setup[Command] { context =>
  Behaviors.withTimers { timers =>
    replicationSetup { replicationContext =>
      new ShoppingCart(context, replicationContext, timers).behavior()
    }
  }
}

This is what the replication setup variant takes:

((ReplicationContext => EventSourcedBehavior[Command, Event, State]) => Behavior[Command]) => Behavior[Command]

where you pass the replicated behaviour factory function to the given function (which creates the replication context and applies external replication) and returns a behaviour which can then be composed within another behaviour factory function.

Seems a little difficult to understand, although with some explicit types and better naming, could be ok. Maybe also calling it "setup" doesn't make it clear right now.

Copy link
Member

Choose a reason for hiding this comment

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

Aha, I misread it, good if it works for arbitrary composition so we don't have to cover any possible use case.

I wonder if we could make the ReplicatedEntity.externalReplication less picky somehow, the reason why it requires EventSourcedBehavior to externalReplication is that it needs to set the flag .withReplication(context), which is internal. Maybe we can pass open upp that and pass that responsibility to the user instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, wondered the same about just opening up withReplication. Although not sure if needing to apply that yourself is that much different from needing to use a provided factory.

I've tidied up the factory approach. Named the SAM class ReplicatedBehaviors with a setup method, so it looks similar to other behaviour factories, but provided to you in the superfactory instead of static methods. Also made this the only way, rather than two different factory approaches. See 343ae71.

Looks like this in the shopping cart now:

def apply(replicatedBehaviors: ReplicatedBehaviors[Command, Event, State]): Behavior[Command] = {
  Behaviors.setup[Command] { context =>
    replicatedBehaviors.setup { replicationContext =>
      new ShoppingCart(context, replicationContext).behavior()
    }
  }
}

Copy link
Member

@patriknw patriknw left a comment

Choose a reason for hiding this comment

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

LGTM, but let's merge Johan's first and then merge this to main.

* Can optionally be composed with other Behavior factories, to get access to actor context or timers.
*/
@ApiMayChange
abstract class ReplicatedBehaviors[Command, Event, State] {
Copy link
Member

Choose a reason for hiding this comment

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

I was pondering this for a long while to see if we could get away without the extra level of factories. ReplicationContext => Behavior with something in runtime to find the nested EventSourcedBehavior, but that's problematic with deferred behavior (lazy start) and supervision and such.

I think this looks pretty good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't see a better way either.

@pvlugter pvlugter changed the title WIP: try out sample with replicated event sourcing over gRPC Add samples for replicated event sourcing over gRPC Jan 25, 2023
@pvlugter
Copy link
Contributor Author

Rebased and updated. Java sample added. Sample compiles added to CI checks. Will rebase again once #757 is merged.

@pvlugter
Copy link
Contributor Author

Extended the docs with some example code from the samples.

Base automatically changed from wip-grpc-replicated-es-transport to main January 25, 2023 02:46
@pvlugter pvlugter marked this pull request as ready for review January 25, 2023 03:09
@pvlugter
Copy link
Contributor Author

Rebased for main and ready now.

Some code examples for the docs pulled from the samples, which are only compile checked — we may want to have these in integration tests instead.

Copy link
Member

@johanandren johanandren left a comment

Choose a reason for hiding this comment

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

LGTM!

@johanandren johanandren merged commit b5b4b4c into main Jan 25, 2023
@johanandren johanandren deleted the grpc-replicated-es-sample branch January 25, 2023 08:03
@johanandren johanandren added this to the 1.4.0-M2 milestone Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants