Repository navigation
Make AR#increment! AR#decrement! concurency safe - #11410
Conversation
There was a problem hiding this comment.
Should this be using public_send instead? The attribute being referenced shouldn't ever be private.
|
It looks like this gains concurrency protection when the same using the same object, but I don't see where it would protect against concurrent issues on a threaded system. ActiveRecord isn't identity mapped (by default), so multiple instances could potentially change counters at the same time. Aside from that I like that the methods were moved off of the class. |
|
@sorentwo I think this is about DB concurrency not ruby-land AR thread-safety. |
|
This PR is about DB concurrency of course. changed |
|
That makes a lot more sense. |
|
This LGTM. @bogdan, could you rebase it? |
31fa16e to
aa808ff
Compare
|
@tamird done |
There was a problem hiding this comment.
how come this method is removed while the equivalent in has_many_through_association was kept?
There was a problem hiding this comment.
I simply moved method to the place where it is used: only in has_many_through_association but not has_many_association.
|
Do you think this could be implemented using optimistic locking? In other words, we should be able to go through the optimistic locking codepath to obtain the guarantees we want here. (optimistically lock on what we think is the original value of the counter). |
|
World is dangerous place. These methods used in rails internals anyway and I need them in my project anyway. And the danger is what I am trying to fix here and make it safe for user in the same way it is safe in rails internally. Of course, it is not 100% safeness. But Rails never was that safe to use. Danger exists in many methods because we didn't figure out how to make it better yet. Check "Concurency and Integrity" paragraph here: http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_uniqueness_of. |
|
@jeremy WDYT? |
When we made sure that the counter gets updated in memory, we only did it on the has many side. The has many side only does the update if the belongs to cannot. The belongs to side was updated to update the counter cache (if it is able). This means that we need to check if the belongs_to is able to update in memory on the has_many side. We also found an inconsistency where the reflection names were used to grab the association which should update the counter cache. Since reflection names are now strings, this means it was using a different instance than the one which would have the inverse instance set. Fixes #18689 [Sean Griffin & anthonynavarre]
aa808ff to
9046681
Compare
9046681 to
6c9491c
Compare
|
Rebased against master. |
There was a problem hiding this comment.
"test increment concurrency" needs to be more precise so future readers of this code understand the guarantee we're making. This is testing the I in ACID, that #increment! isn't vulnerable to stale reads.
Does this single test really cover all the code changes above? It's difficult to determine but seems unlikely.
There was a problem hiding this comment.
@jeremy Maybe the changes fix some bugs when counter cache is only updated in DB but not on instance, but it is hard to assume where it happens and if it really happens. We have no identity map and we can not be sure we updated all instances in memory with a proper counter value.
So I would consider all these changes a refactoring that just reuses code.
Renamed test name to be more precise about use case.
6c9491c to
85a1c02
Compare
|
r? @jeremy |
Make AR#increment! and #decrement! concurrency-safe
There was a problem hiding this comment.
I think this changes the behavior from the documentation, which says that these methods return true on success, but now it seems to always return self?
EDIT: the documentation also says that it passes through the attribute setter, which doesn't seem to be the case either.
Now, AR#increment! is concurrent safe(rails#11410). However, this commit breaks backward compatability because `.update_counters` doesn't update timestamp. Here is a reproduction script for this issue. https://gist.github.com/akihiro17/aa08276f5d0f72d9869b In this commit, `.update_counters` updates timestamp if both `touch` and `record_time_stamp` are true.
increment!anddecrement!should be concurency safe becauseincorrect update can cause serious problems.
Especially when rails already have a concurency-safe counters update
mechanics in
.update_countersmethod.So this patch includes the following changes:
from has_many association internals to #increment! method
#increment!and#decrement!methods where possibleinstead of
.update_countersthat doesn't update an counters onrecord instance level