Support multiple saga instances to be activated for the same message
Right now we don't support multiple saga instances being activated by the same message. To do this we must modify the ISagaPersister interface to allow the Get(string property..) (https://github.com/NServiceBus/NServiceBus/blob/master/src/core/NServiceBus.Saga/ISagaPersister.cs#L37) to return a IEnumerable instead.
If we find that we have a unique constraint for the given property we would use the safer and better performing way of fetching the saga mentioned here:
https://github.com/NServiceBus/NServiceBus/pull/268
In order to support this in a safe way for our users we must consider batching when the number of returned sagas is greater than X. Otherwise there would be to much things going on inside a single transaction and the risk for a TX timeout would be high. To support this we could do a bus.SendLocal() for each saga instance putting the saga id and type in the headers. This would effectively make each instance execute in its own transaction.
Note: To do this we must avoid our own conventions since it's not allow to "send" events.
This can be done easily by creating a handler that handles the events, finds corresponding sagas and then passes on single (new) messages to the saga.
I really think NSB shouldn't get to deep into that business. It is a business concern, not a technical one.
Making it more convenient to program such things would be ok though.
@lcorneliussen So how would you find those saga's? Query the saga storage? Wouldn't that be wrong?
Well, that's what a saga finder would have to do anyway, well?
I now have a handler that queries the saga database table for saga ids and convert the message to a saga specific command but this really feels pretty dirty. First of all for querying a set that I should do in batches or streaming and second for having this logic now coupled with the saga.
Why do you need the SagaId? Couldn't you create "business" messages that use the sagas UniqueId for correlation?
I'm not a contributor or an expert on NSB (just a user), but I think the idea of starting multiple sagas from a single message is an unfortunate omission. It makes a lot of sense to be able to do that, and creating new messages that kick off multiple sagas is a lame hack. Is there any chance this will make it into an upcoming version?
I apologize that I'm not prepared to actually submit a pull request for this.
I agree that there is friction here when you have a small number of sagas to update. It's really when you get to the larger number of instances that this idea has issues.
There are various problems with loading multiple sagas as part of one message. First, you wouldn't be able to reliably participate in transactions. If your one message tries to load and modify a million saga instances, then participating in a single transaction is most likely out of the question. Another thing is that you lose the ability to parallelize. If you send multiple messages, you can handle each of them in parallel across boxes. Not so with a single message.
I think that one way to deal with this friction would be to reduce the overhead of mapping one message to another. It would be nice to have an autoMapper like API that you could use to handle a message to map to another. This could add a "mapping handler" in the background. It would keep you from having to write separate handler classes just to map one message to another.
Thoughts?
Why not just introduce another endpoint?
@danielmarbach I think that is exactly what @kblooie is suggesting - just that the endpoint (message handler) is created automagically.
A saga is an aggregate root (in most cases) and as such a consistency and transaction boundary. I think it is plain wrong to address multiple of them in the same tx. You can still do that with Bus.Send(object[]) - but that is a different story.
Hey, I did some thinking and having a message dispatch to multiple sagas is quite trivial. I've fixed this on the multisaga branch. Can you guys take it for a spin and see what you think?
I've modified the timeoutmanager sample to show the usage:
https://github.com/NServiceBus/NServiceBus/blob/multisaga/IntegrationTests/TimeoutManager/MyServer/Saga/SimpleSaga.cs
Just hit "s" a couple of times to place orders and then hit "c" to fake a "customer made preferred" event. When that happens that same event is dispatched (as separate messages) to the different saga instances.
@udidahan what is your take on this?
I don’t see anything requesting a timeout.
The timeout was just a leftover from the old code. Is there anything in particular around timeouts that would have an effect on this?
I think it is wrong to apply a message to multiple sagas within one technical transaction. They should be independent. What about delivering them by resending them with a SagaID header?
Isn't that exactly what I'm doing? One bus.sendlocal per saga instance found?
wonderful! sorry :)
I think we should have a limit on how many instances we allow to avoid flooding the queue with messages if there is millions of saga instances. We can of course make this limit configurable. Thoughts?
Awesome :) Regarding limits, have u done any performance analysis? I would not add any limits unless really required.
I assume this will be out in v5 as it is a non backwards change?
Having a limit would be strange to have here. The only problem I can think of is that the key resultset returned on the saga id finder would be to big resulting in large memory consumption. Maybe better to page through the result to minimize such issues?
I don't think memory is the issue here. The problem is tx timeouts, because the query it can be done within a tx. And I agree we need to do paging specially for raven.
Why do a sendLocal? Could we just send to the master endpoint instead? That way you wouldn't need the limit.
On Jun 12, 2013, at 5:00 PM, Ramon Smits [email protected] wrote:
Having a limit would be strange to have here. The only problem I can think of is that the key resultset returned on the saga id finder would be to big resulting in large memory consumption. Maybe better to page through the result to minimize such issues? On Jun 12, 2013 9:52 PM, "Andreas Öhlund" [email protected] wrote:
I think we should have a limit on how many instances we allow to avoid flooding the queue with messages if there is millions of saga instances. We can of course make this limit configurable. Thoughts?
— Reply to this email directly or view it on GitHub< https://github.com/NServiceBus/NServiceBus/issues/298#issuecomment-19350963>
.
— Reply to this email directly or view it on GitHubhttps://github.com/NServiceBus/NServiceBus/issues/298#issuecomment-19358622 .