Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema_load triggers 2nd schema_load (via locking) #24653

Merged
merged 2 commits into from
Apr 28, 2016

Conversation

kbrock
Copy link
Contributor

@kbrock kbrock commented Apr 20, 2016

Summary

Rails is double loading (schema_load!) for all of our models.

Details

Currently, loading the schema (schema_load)
accesses the locking column (locking_column)
which defaults the value (reset_locking_column)
which invalidates the schema (reload_schema_from_cache)
which forces another schema load (schema_load).

Good news:

The second schema_load does accesses locking_column,
but locking_column is set this time through, so it does not call reset_locking_column
and it does not trigger an infinite loop.

Solution

I feel reset_locking_column should still invalidate the schema.
So instead, this sets @locking_column directly in the default case only.
Please let me know if you have another solution.

/cc @matthewd

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @matthewd (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@@ -150,7 +150,7 @@ def locking_column=(value)

# The version column used for optimistic locking. Defaults to +lock_version+.
def locking_column
reset_locking_column unless defined?(@locking_column)
@locking_column = DEFAULT_LOCKING_COLUMN unless defined?(@locking_column)
Copy link
Member

Choose a reason for hiding this comment

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

Do you know why reset_locking_column invalidated the schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca I am not sure if you asked do I know why that is wanted, or literally how is that done.

  • locking_column calls reset_locking_column
  • reset_locking_column calls locking_column=
  • locking_column= calls reload_schema_from_cache

I'm looking into writing tests for this. but they are a little tricky

Copy link
Contributor Author

@kbrock kbrock Apr 20, 2016

Choose a reason for hiding this comment

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

@rafaelfranca looked through history of this file (didn't branch out to other files) and even wondering if locking_column= or reset_locking_column even needs to reload the schema.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, that was the information I was looking, thanks.

Copy link
Member

Choose a reason for hiding this comment

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

If table_name= reload the schema so they should.

@kbrock
Copy link
Contributor Author

kbrock commented Apr 21, 2016

added tests. Here are the numbers from before this PR and after.

test # loads before # loads after
test_basic_model_is_loaded_once 2 1
test_model_with_custom_lock_is_loaded_once 1 1
test_model_with_changed_custom_lock_is_loaded_twice 3 2
  • I had expected basic_model and model_with_custom_lock to behave similarily. The first one just uses the default.
  • I expected model_with_changed_custom_lock to load a second time. (the 3 value makes sense there, but really should be 2)

@kbrock
Copy link
Contributor Author

kbrock commented Apr 21, 2016

rebased to kick build. (jruby timeout looked bogus) (I didn't squash, but can if you want that)

Currently, loading the schema (schema_load)
accesses the locking column (locking_column)
which defaults the value (reset_locking_column)
which invalidates the schema (reload_schema_from_cache)
which forces another schema load.

Good news:
The second schema_load does accesses locking_column,
but locking_column is set, so it does not reset_locking_column
and it does not trigger an infinite loop.

The solution is not invalidate the cache while default locking_column
@kbrock
Copy link
Contributor Author

kbrock commented Apr 28, 2016

kicking again. hope faye build failure is not related

@rafaelfranca rafaelfranca merged commit 03b62b8 into rails:master Apr 28, 2016
@kbrock
Copy link
Contributor Author

kbrock commented Apr 28, 2016

Thanks @rafaelfranca

@kbrock kbrock deleted the kinder_locking_column branch April 28, 2016 22:00
@@ -0,0 +1,53 @@
require 'thread'
Copy link
Member

Choose a reason for hiding this comment

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

@kbrock How are we relying on this here? See #24790

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @jeremy - very good eye. I found a solution that no longer needed the thread but it got left in.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you but all kudos to @vipulnsward's sharp eye 😊

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.

None yet

6 participants