Skip to content

Make AR#increment! AR#decrement! concurency safe - #11410

Merged
jeremy merged 1 commit into
rails:masterfrom
bogdan:increment-concurency
Oct 10, 2015
Merged

jeremy merged 1 commit into
rails:masterfrom
bogdan:increment-concurency

Conversation

@bogdan

@bogdan bogdan commented Jul 12, 2013

Copy link
Copy Markdown
Contributor

increment! and decrement! should be concurency safe because
incorrect update can cause serious problems.

Especially when rails already have a concurency-safe counters update
mechanics in .update_counters method.

So this patch includes the following changes:

  • Reuse code between increment and decrement methods
  • Extracted a code snippet (search for ultimate 'eww' comment in diff)
    from has_many association internals to #increment! method
  • Make use of #increment! and #decrement! methods where possible
    instead of .update_counters that doesn't update an counters on
    record instance level

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be using public_send instead? The attribute being referenced shouldn't ever be private.

@sorentwo

Copy link
Copy Markdown
Contributor

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.

@thedarkone

Copy link
Copy Markdown
Contributor

@sorentwo I think this is about DB concurrency not ruby-land AR thread-safety.

@bogdan

bogdan commented Jul 19, 2013

Copy link
Copy Markdown
Contributor Author

This PR is about DB concurrency of course.
Ruby level can only be solved with identity map but this is another story.

changed send to public_send as @sorentwo suggested.

@sorentwo

Copy link
Copy Markdown
Contributor

That makes a lot more sense.

@tamird

tamird commented Sep 24, 2014

Copy link
Copy Markdown
Contributor

This LGTM. @bogdan, could you rebase it?

@bogdan
bogdan force-pushed the increment-concurency branch from 31fa16e to aa808ff Compare September 26, 2014 15:36
@bogdan

bogdan commented Sep 26, 2014

Copy link
Copy Markdown
Contributor Author

@tamird done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come this method is removed while the equivalent in has_many_through_association was kept?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simply moved method to the place where it is used: only in has_many_through_association but not has_many_association.

@tamird

tamird commented Sep 26, 2014

Copy link
Copy Markdown
Contributor

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

@tamird

tamird commented Sep 26, 2014

Copy link
Copy Markdown
Contributor

@bogdan I think the safest thing to do is kill these methods with fire, since they are so dangerous and provide hardly any value at all. #17073

@bogdan

bogdan commented Sep 27, 2014

Copy link
Copy Markdown
Contributor Author

World is dangerous place.

These methods used in rails internals anyway and I need them in my project anyway.
Killing this methods will not kill the fact that we need to update counter at instance level with updating it in database. It will just make every person to reinvent the pattern that already exists in Rails.

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.

@rafaelfranca

Copy link
Copy Markdown
Member

@jeremy WDYT?

bogdan referenced this pull request Jan 26, 2015
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]
@bogdan
bogdan force-pushed the increment-concurency branch from aa808ff to 9046681 Compare January 27, 2015 16:18
@bogdan
bogdan force-pushed the increment-concurency branch from 9046681 to 6c9491c Compare October 3, 2015 13:38
@bogdan

bogdan commented Oct 3, 2015

Copy link
Copy Markdown
Contributor Author

Rebased against master.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@bogdan
bogdan force-pushed the increment-concurency branch from 6c9491c to 85a1c02 Compare October 5, 2015 12:13
@rafaelfranca

Copy link
Copy Markdown
Member

r? @jeremy

@jeremy
jeremy merged commit 85a1c02 into rails:master Oct 10, 2015
jeremy added a commit that referenced this pull request Oct 10, 2015
Make AR#increment! and #decrement! concurrency-safe

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. /cc @bogdan

akihiro17 added a commit to akihiro17/rails that referenced this pull request Oct 17, 2015
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.
@zillou zillou mentioned this pull request Sep 7, 2016
1 of 2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants