NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

Support multiple saga instances to be activated for the same message

Open andreasohlund opened this issue 14 years ago • 48 comments

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.

andreasohlund avatar Apr 13 '12 09:04 andreasohlund

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 avatar Apr 18 '12 09:04 lcorneliussen

@lcorneliussen So how would you find those saga's? Query the saga storage? Wouldn't that be wrong?

ramonsmits avatar May 30 '13 11:05 ramonsmits

Well, that's what a saga finder would have to do anyway, well?

lcorneliussen avatar May 30 '13 14:05 lcorneliussen

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.

ramonsmits avatar May 30 '13 18:05 ramonsmits

Why do you need the SagaId? Couldn't you create "business" messages that use the sagas UniqueId for correlation?

lcorneliussen avatar May 30 '13 20:05 lcorneliussen

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.

shekelator avatar Jun 12 '13 12:06 shekelator

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?

kblooie avatar Jun 12 '13 12:06 kblooie

Why not just introduce another endpoint?

danielmarbach avatar Jun 12 '13 13:06 danielmarbach

@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.

lcorneliussen avatar Jun 12 '13 13:06 lcorneliussen

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?

andreasohlund avatar Jun 12 '13 14:06 andreasohlund

I don’t see anything requesting a timeout.

udidahan avatar Jun 12 '13 15:06 udidahan

The timeout was just a leftover from the old code. Is there anything in particular around timeouts that would have an effect on this?

andreasohlund avatar Jun 12 '13 15:06 andreasohlund

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?

lcorneliussen avatar Jun 12 '13 15:06 lcorneliussen

Isn't that exactly what I'm doing? One bus.sendlocal per saga instance found?

andreasohlund avatar Jun 12 '13 15:06 andreasohlund

wonderful! sorry :)

lcorneliussen avatar Jun 12 '13 15:06 lcorneliussen

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?

andreasohlund avatar Jun 12 '13 19:06 andreasohlund

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?

johnsimons avatar Jun 12 '13 21:06 johnsimons

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?

ramonsmits avatar Jun 12 '13 22:06 ramonsmits

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.

johnsimons avatar Jun 12 '13 22:06 johnsimons

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 .

kblooie avatar Jun 12 '13 22:06 kblooie