-
Notifications
You must be signed in to change notification settings - Fork 33
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
Conversation
final case class CheckedOut(cartId: String, eventTime: Instant) extends Event | ||
|
||
def init(implicit system: ActorSystem[_]): Replication[Command] = { | ||
val projectionProvider: ReplicationProjectionProvider = R2dbcProjection.atLeastOnceFlow(_, None, _, _)(_) |
There was a problem hiding this comment.
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)
929b6a6
to
6f24ea9
Compare
e67c958
to
e81ec6d
Compare
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: Lines 132 to 141 in e81ec6d
|
@@ -63,24 +64,58 @@ trait Replication[Command] { | |||
@ApiMayChange | |||
object Replication { | |||
|
|||
trait ReplicatedBehaviorFactory[Command, Event, State] { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()
}
}
}
e81ec6d
to
b8e8ee2
Compare
There was a problem hiding this 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] { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
b8e8ee2
to
769cad3
Compare
Rebased and updated. Java sample added. Sample compiles added to CI checks. Will rebase again once #757 is merged. |
Extended the docs with some example code from the samples. |
cc09143
to
599afc9
Compare
Rebased for 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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).