Grails uses overly eager hibernate proxy unwrapping

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Grails uses overly eager hibernate proxy unwrapping

longwa
As we start to move more and more to asynchronous programming models, I'm finding that Grails is WAY too eager about wanting to unwrap proxies, to the point of making them almost unusable in many cases.

For instance, given this scenario where a book is loaded in a different session (imagine the books are loaded in a thread, for instance):

Book book = null
Book.withNewSession {
    book = Book.get(1L)
}

Calling:

book.author.id

or even just doing something like:

new Book(author: book.author).save()

Causes a lazy initialization exception, even though nothing but the "id" of the Author is ever needed for this. I mean, if you can't even access the id of a proxy, what good is it?

The problem is that HibernateUtils.handleLazyProxy() is being invoked when "book.author" is called and wants to unwrap the author to "avoid proxy hell" as the comment says.

This example would work just fine without "avoiding proxy hell" and actually prevents the use of concurrent reader threads for batch processing or just simple asynchronous retrieval of related objects for other operations.

Is there some good reason (other than proxy hell, whatever that means) why handleLazyProxy() is so aggressive? Is there some way to disable this on a case-by-case basis? 

We have a scenario where we are loading thousands or records in multiple concurrent threads and then want to pass those along to something that is inserting data in multiple threads. We have to eagerly fetch all of the associated objects even though they are just being assigned across such as "a.someProperty = b.someProperty" with no actually use of the proxy. Obviously this is killing performance. In some cases, even using the handy and undocumented "Id" for Hibernate is still causing issues.

-Aaron

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/e4708c70-7d4a-4c63-abef-f5d647d2c28e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

longwa
So the more I think about this, the more it seems like this really can't be useful except possibly in some rare cases. The OpenSessionInView strategy tends to eliminate the majority of lazy initialization hell for typical applications. To prove my point, I built a new version of the grails-datastore-gorm-hibernate-core with this line commented out in the AbstractGrailsDomainBinder:

handleLazyProxy(grailsProperty.getDomainClass(), grailsProperty);

Our application starts up and doesn't have any immediately obvious issues. I opened a console and confirmed my changes along with testing a few cases of using proxies directly in withCriteria and DetachedCriteria and they all worked. I thought maybe something like Book.findAll { author == book.author } would fail if book.author returned a proxy...it doesn't...works great. Same thing with withCriteria. The only place that got me was an asynchronous section of code that ended up putting objects in a Map. The hashCode() method tried to unwrap (as it should) and it failed. Simple fix to just eager fetch that object in the threads and I'm up and running.

Next, I ran a big billing/invoicing batch job that processes a lot of data in multiple threads (so it was already pretty much lazy-safe). No issues with lazy initialization and an amazing reduction in the number of queries. Again, this is due to the fact that we are "copying" a lot of to-one associations that don't need to be unwrapped....a.something = b.something where b.something is a proxy.

I compared the SQL statements in the debug log for each version and the modified version ran significantly less SQL (around 20,000 less statements).

I'd really like to understand what problem this is supposed to be solving because it definitely impacts the default performance of our application. I suppose it might work to add an additional mapping parameter, similar to lazy: true, which allows you to skip this behavior on a per-property basis for performance reasons.

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/dce8de8e-b180-4b8d-997c-323ad46f895a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

Graeme Rocher-2
In reply to this post by longwa
Can you not use 'book.authorId' instead?

On Tue, Apr 21, 2015 at 6:50 PM, Aaron <[hidden email]> wrote:

> As we start to move more and more to asynchronous programming models, I'm
> finding that Grails is WAY too eager about wanting to unwrap proxies, to the
> point of making them almost unusable in many cases.
>
> For instance, given this scenario where a book is loaded in a different
> session (imagine the books are loaded in a thread, for instance):
>
> Book book = null
> Book.withNewSession {
>     book = Book.get(1L)
> }
>
> Calling:
>
> book.author.id
>
> or even just doing something like:
>
> new Book(author: book.author).save()
>
> Causes a lazy initialization exception, even though nothing but the "id" of
> the Author is ever needed for this. I mean, if you can't even access the id
> of a proxy, what good is it?
>
> The problem is that HibernateUtils.handleLazyProxy() is being invoked when
> "book.author" is called and wants to unwrap the author to "avoid proxy hell"
> as the comment says.
>
> This example would work just fine without "avoiding proxy hell" and actually
> prevents the use of concurrent reader threads for batch processing or just
> simple asynchronous retrieval of related objects for other operations.
>
> Is there some good reason (other than proxy hell, whatever that means) why
> handleLazyProxy() is so aggressive? Is there some way to disable this on a
> case-by-case basis?
>
> We have a scenario where we are loading thousands or records in multiple
> concurrent threads and then want to pass those along to something that is
> inserting data in multiple threads. We have to eagerly fetch all of the
> associated objects even though they are just being assigned across such as
> "a.someProperty = b.someProperty" with no actually use of the proxy.
> Obviously this is killing performance. In some cases, even using the handy
> and undocumented "Id" for Hibernate is still causing issues.
>
> -Aaron
>
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/e4708c70-7d4a-4c63-abef-f5d647d2c28e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Graeme Rocher

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAO_wNoeYFMLcKkf2__ckqf-M0ET-yjDCLXK-gic0xZbUv1msXg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

longwa
No, because book.authorId does not work for assignment (it's a readonly property). A very common scenario (translated into Book-Author model):

def newBook = new Book()
newBook.author = oldDetachedBook.author
newBook.save()

In this case, the author is unwrapped for no reason. I suppose a workaround might be to do Author.load(oldDetachedBook.authorId), but that seems like a silly requirement.

There are other problems with using the authorId syntax as well. The first is that it completely breaks the object abstraction. The second is that it doesn't work in normal unit tests (yes, I know it works in HibernateUnitTest, but they are impractical for anyone with a large connected object graph). It also percolates down into queries and such. Now instead of writing:

Using the authorId syntax just feels wrong. We've also found scenarios where it also seems to want to unwrap the proxy (I think for certain id generators, haven't tracked it down).

We have a very large, complex application and removing this aggressive unwrap, so far, doesn't seem to have broken a single thing. I just don't see the problem it's solving.

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/003d9637-30d3-49cf-99dd-ccd0cdeb2dc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

longwa
Looking at the history for HibernateUtils, Lari Hotari has been down this road before. On 5/3/13 he committed a change that commented out the handle lazy and returned proxies. A few revisions later, he added that code back for "compatibility reasons".

I'd like to at least propose a property or configuration value that allows us to control this behavior in 2.4.x and later. I can submit a pull request against 2.4.6 if there is willingness. 

I also think Grails 3.0 and later should remove this behavior and return true proxies for performance reasons. For applications like ours, it can be a huge gain with zero additional effort.

-Aaron

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/88759ec0-043d-4a89-bedc-1b735b5b3622%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

Lari Hotari-3
Yes I remember working on that. I reverted some of the changes after some test failures. It turned out that it would have been a breaking change at that point to remove the eager proxy initialization.

This is the change that removed eagerly initializing proxies:
https://github.com/grails/grails-data-mapping/commit/37f5da37

And this is where I reverted it:
https://github.com/grails/grails-data-mapping/commit/8ef8d2b0

The eager proxy initialization was originally introduced by this commit:
https://github.com/grails/grails-core/commit/58182b40

That commit references:
https://jira.grails.org/browse/GRAILS-4242
https://jira.grails.org/browse/GRAILS-4230

-Lari


On Wednesday, April 22, 2015 at 3:32:31 PM UTC-4, Aaron wrote:
Looking at the history for HibernateUtils, Lari Hotari has been down this road before. On 5/3/13 he committed a change that commented out the handle lazy and returned proxies. A few revisions later, he added that code back for "compatibility reasons".

I'd like to at least propose a property or configuration value that allows us to control this behavior in 2.4.x and later. I can submit a pull request against 2.4.6 if there is willingness. 

I also think Grails 3.0 and later should remove this behavior and return true proxies for performance reasons. For applications like ours, it can be a huge gain with zero additional effort.

-Aaron

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/21c8eb06-c8bf-40ab-bac1-2d8bb1a035d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

Lari Hotari-3
further on, those jira issues reference
http://jira.codehaus.org/browse/GROOVY-3433
and
https://hibernate.atlassian.net/browse/HHH-3870

I believe I've handled that problem in later proxy handling / generation changes that I worked on in grails-data-mapping.
like the commit "Hold MetaClass of Javassist proxy in the MethodHandler" https://github.com/grails/grails-data-mapping/commit/37f5da37 and related commit
https://github.com/grails/grails-data-mapping/commit/c7d6544
I've also made other changes in that area.

history of changes to org.grails.datastore.mapping.proxy package:
https://github.com/grails/grails-data-mapping/commits/master/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/proxy

The main goal of some of the changes were to solve a performance problem that showed up in performance tests:
https://jira.grails.org/browse/GRAILS-11614
and as the part of that, for consistency https://jira.grails.org/browse/GRAILS-11792

I hope this helps when digging in to the background of changes. :)

Lari




On Thursday, April 23, 2015 at 8:56:30 AM UTC-4, Lari Hotari wrote:
Yes I remember working on that. I reverted some of the changes after some test failures. It turned out that it would have been a breaking change at that point to remove the eager proxy initialization.

This is the change that removed eagerly initializing proxies:
<a href="https://github.com/grails/grails-data-mapping/commit/37f5da37" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-data-mapping%2Fcommit%2F37f5da37\46sa\75D\46sntz\0751\46usg\75AFQjCNEQklgOMNZpXKYHMi9WybI9vCnDtw';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-data-mapping%2Fcommit%2F37f5da37\46sa\75D\46sntz\0751\46usg\75AFQjCNEQklgOMNZpXKYHMi9WybI9vCnDtw';return true;">https://github.com/grails/grails-data-mapping/commit/37f5da37

And this is where I reverted it:
<a href="https://github.com/grails/grails-data-mapping/commit/8ef8d2b0" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-data-mapping%2Fcommit%2F8ef8d2b0\46sa\75D\46sntz\0751\46usg\75AFQjCNFcQTnoXStqiNONVo2gyAZ0DsdDwQ';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-data-mapping%2Fcommit%2F8ef8d2b0\46sa\75D\46sntz\0751\46usg\75AFQjCNFcQTnoXStqiNONVo2gyAZ0DsdDwQ';return true;">https://github.com/grails/grails-data-mapping/commit/8ef8d2b0

The eager proxy initialization was originally introduced by this commit:
<a href="https://github.com/grails/grails-core/commit/58182b40" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-core%2Fcommit%2F58182b40\46sa\75D\46sntz\0751\46usg\75AFQjCNHRmH96qcPhKO-uUWJTtl75buGbkQ';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-core%2Fcommit%2F58182b40\46sa\75D\46sntz\0751\46usg\75AFQjCNHRmH96qcPhKO-uUWJTtl75buGbkQ';return true;">https://github.com/grails/grails-core/commit/58182b40

That commit references:
<a href="https://jira.grails.org/browse/GRAILS-4242" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fjira.grails.org%2Fbrowse%2FGRAILS-4242\46sa\75D\46sntz\0751\46usg\75AFQjCNECosuQV3Pgf5KreS06mrf5wJyzMA';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fjira.grails.org%2Fbrowse%2FGRAILS-4242\46sa\75D\46sntz\0751\46usg\75AFQjCNECosuQV3Pgf5KreS06mrf5wJyzMA';return true;">https://jira.grails.org/browse/GRAILS-4242
<a href="https://jira.grails.org/browse/GRAILS-4230" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fjira.grails.org%2Fbrowse%2FGRAILS-4230\46sa\75D\46sntz\0751\46usg\75AFQjCNHxo3IxxxbHDU3kl_u3kcuPYT_e2A';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fjira.grails.org%2Fbrowse%2FGRAILS-4230\46sa\75D\46sntz\0751\46usg\75AFQjCNHxo3IxxxbHDU3kl_u3kcuPYT_e2A';return true;">https://jira.grails.org/browse/GRAILS-4230

-Lari


On Wednesday, April 22, 2015 at 3:32:31 PM UTC-4, Aaron wrote:
Looking at the history for HibernateUtils, Lari Hotari has been down this road before. On 5/3/13 he committed a change that commented out the handle lazy and returned proxies. A few revisions later, he added that code back for "compatibility reasons".

I'd like to at least propose a property or configuration value that allows us to control this behavior in 2.4.x and later. I can submit a pull request against 2.4.6 if there is willingness. 

I also think Grails 3.0 and later should remove this behavior and return true proxies for performance reasons. For applications like ours, it can be a huge gain with zero additional effort.

-Aaron

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/ba657ee1-49de-4dde-abdd-af571b30ec40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

longwa
Thanks Lari, that's very helpful.

Given that you fixed the metaClass issues in 37f5da37 and GRAILS-11614 fixed the performance issues with the proxy factory, do you think it's worth returning to the non-eager version and returning the proxies again?

Like I said, we are running with a version that does just that and have found 0 problems so far in 2.4.4. Performance is much better in many scenarios, particularly some of the batch jobs that were fetching/copying associations. I've been playing around with the behavior for many different scenarios and it seems to behave exactly as I'd expect.

-Aaron

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/978e04a6-9f90-4734-ae5f-d90e46352b02%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Grails uses overly eager hibernate proxy unwrapping

Lari Hotari-3

Yes I think it would make sense to return proxies. There might be some other areas where proxies are initiated before the view is rendered making the proxies useless. I think that was the case at the time I was working on the changes. That code might have been removed already.
Since it's now easy to setup Travis CI for your own fork, I'd first suggest to run the tests against your fork with Travis CI to see if any other tests break after making the change.

-Lari

On Thursday, April 23, 2015 at 9:48:05 AM UTC-4, Aaron wrote:
Thanks Lari, that's very helpful.

Given that you fixed the metaClass issues in 37f5da37 and <a href="https://jira.grails.org/browse/GRAILS-11614" rel="nofollow" style="line-height:17px;font-family:Arial,sans-serif;font-size:14px;color:rgb(102,153,51);background-color:rgb(245,245,245)" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fjira.grails.org%2Fbrowse%2FGRAILS-11614\46sa\75D\46sntz\0751\46usg\75AFQjCNGjj3L0Nffo2772I70joDa8Uc739g';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fjira.grails.org%2Fbrowse%2FGRAILS-11614\46sa\75D\46sntz\0751\46usg\75AFQjCNGjj3L0Nffo2772I70joDa8Uc739g';return true;">GRAILS-11614 fixed the performance issues with the proxy factory, do you think it's worth returning to the non-eager version and returning the proxies again?

Like I said, we are running with a version that does just that and have found 0 problems so far in 2.4.4. Performance is much better in many scenarios, particularly some of the batch jobs that were fetching/copying associations. I've been playing around with the behavior for many different scenarios and it seems to behave exactly as I'd expect.

-Aaron

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/0237687d-258a-4c50-8e9e-b5fb8f3b8252%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.