How to concurrent update of document collection property?

74 views
Skip to first unread message

Alexey Koptyaev

unread,
Aug 3, 2018, 10:26:54 AM8/3/18
to RavenDB - 2nd generation document database
Hello, I'm struggling with such a problem.

I have a model, say Product, and this model has a collection of Description objects.

public class Product
{
        public List<Description> Descriptions { get; set; }
}

The external service sends notifications that trigger the update of a specific Description of the Product object.
The problem is, that these notifications are sent in groups, about 7 notifications at one go (by one specific description for each country). So when I start to process these messages, I start to do it simultaniously, and I suppose that multiple writes overwrites each other because the result is rather unpredictable - sometimes it works, sometimes the descriptions remain unchnaged.

Here is my code:

 _session.Advanced.Defer(new PatchCommandData(
                        id: product.Id,
                        changeVector: null,
                        patch: new PatchRequest
                        {
                            Script = @"this.Descriptions = this.Descriptions.filter(desc=> desc.CountryCode.toLowerCase() != args.CountryCode.toLowerCase())",
                            Values =
                            {
                                {"CountryCode", description.CountryCode},
                                {"LangCode", description.LangCode}
                            }
                        },
                        patchIfMissing: null));

                if (description != null)
                {
                    _session.Advanced.Patch<Product, Description>(product, x => x.Descriptions, c => c.Add(description));
                }

_session.SaveChanges();

The JS script means "leave descriptions for any other country than one that is being processed right now". And then goes patch request, if necessary.

Long story short - I'd like to know how should I update nested collection objects in this case?

I thought that patches stuff works like requests that are placed in kind of a queue on raven server and are processed in strict order, but it doesn't seem like that.

Thanks.

PS I've also had an idea to change the script to take index of a Description entry, remove it with JS 'splice', and then insert the new one, but since the request execution is not strictly ordered, the indexes might bias and this makes no sense.



Maxim Buryak

unread,
Aug 5, 2018, 8:22:44 AM8/5/18
to rav...@googlegroups.com
Hi,
I'm afraid I don't follow, do you have concurrent sessions performing those patches?
In general, in you case, I think you could use a single patch operation for you needs:


_session.Advanced.Defer(new PatchCommandData(
                        id: product.Id,
                        changeVector: null,
                        patch: new PatchRequest
                        {
                            Script = @"this.Descriptions = this.Descriptions.filter(desc=> desc.CountryCode.toLowerCase() != args.Description.CountryCode.toLowerCase());
if (!!args.Description)
this.Description.push(args.Description);
",
                            Values =
                            {
                                {"Description", description}
                            }
                        },
                        patchIfMissing: null));


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexey Koptyaev

unread,
Aug 6, 2018, 8:17:08 AM8/6/18
to RavenDB - 2nd generation document database
Hi,
Yes, each patch is made in different session.
Thanks for the advice with single patch. 
It seems that the problem was in my code and it works now as excepted, but, still, it is not clear for me how does Raven process concurrent patches. Maybe there is any documentation about how it is implemented?

Oren Eini (Ayende Rahien)

unread,
Aug 7, 2018, 3:08:17 AM8/7/18
to ravendb
From your perspective, patches on a document are processed serially from the point of view of the document.
However, there are no promises with regards to the order they will run if you issue concurrent patch requests 

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.

Alexey Koptyaev

unread,
Aug 9, 2018, 4:39:48 AM8/9/18
to RavenDB - 2nd generation document database
OK, thanks. I'll keep that in mind.
Reply all
Reply to author
Forward
0 new messages