-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Fully initialize routes before the first request is handled #27647
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @arthurnn (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. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
Hum, triggering the initialization after |
605954a
to
9bac4dd
Compare
It's fixed now. |
@@ -138,6 +138,9 @@ def self.complete(_state) | |||
# some sort of reloaders dependency support, to be added. | |||
require_unload_lock! | |||
reloader.execute | |||
if app.config.eager_load | |||
app.routes.eager_load! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for being in this initializer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be in another one. But it has to be called after reloader.execute
so that routes are loaded.
I figured since this is just a byproduct of loading routes, it would fit nicely there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Yeah, it make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should move the eager_load!
call to the reloader like this:
def eager_load!
execute
route_sets.each(&:eager_load!)
end
And here
if app.config.eager_load
reloader.eager_load!
else
reloader.execute
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd think about moving the eager_load
config into the routes reloader, and having execute
do the eager load, instead: it's not a different thing a caller might ask it to do, it's a different set of behaviour for it to perform, based on configuration, when told to reload.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @pixeltrix I think you commented something about this before. |
@@ -138,6 +138,9 @@ def self.complete(_state) | |||
# some sort of reloaders dependency support, to be added. | |||
require_unload_lock! | |||
reloader.execute | |||
if app.config.eager_load | |||
app.routes.eager_load! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Yeah, it make sense.
@rafaelfranca there's some instance variables in |
@pixeltrix I tried to do it in 076a9124a7348a62decd013268fe24e3eb1f2ca6, let me know what you think. |
@@ -19,6 +21,11 @@ def reload! | |||
revert | |||
end | |||
|
|||
def execute | |||
updater.execute | |||
route_sets.each(&:eager_load!) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing an if
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely 🤦♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
it looks like it'll work but pretty it ain't 😄 |
Yeah I agree :/ |
@@ -4,11 +4,13 @@ module Rails | |||
class Application | |||
class RoutesReloader | |||
attr_reader :route_sets, :paths | |||
delegate :execute_if_updated, :execute, :updated?, to: :updater | |||
attr_accessor :eager_load | |||
delegate :execute_if_updated, :updated?, to: :updater |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think execute_if_updated
should also be defined here to run the eager load code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafaelfranca good point. I fixed it.
c3aaa1c
to
1924e72
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you squash the commits?
`AD::Journey::GTG::Simulator` is lazily built the first time `Journey::Router#find_routes` is invoked, which happens when the first request is served. On large applications with many routes, building the simulator can take several hundred milliseconds (~700ms for us). Triggering this initialization during the boot process reduces the impact of deploys on the application response time.
1924e72
to
5b1332b
Compare
@rafaelfranca squashed! |
@@ -124,6 +124,7 @@ def self.complete(_state) | |||
# the hook are taken into account. | |||
initializer :set_routes_reloader_hook do |app| | |||
reloader = routes_reloader | |||
reloader.eager_load = app.config.eager_load |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couldn't we use railties eager_load method, instead of passing the config to action pack?
In 3.2, railties could define eager_load! method https://github.com/rails/rails/blob/3-2-stable/railties/lib/rails/engine.rb#L439 and it would get called. Not sure if that was removed or still exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what would it improve. Railtie also have eager_load_namespaces
that automatically call eager_load!
on them. But since the routes_reloader is created in an initializer we can't use it.
Will this be included in a Rails 4.2.xx version? If so, which one? |
No. It will be only included in 5.1 |
Hi, we are using jruby 1.7.25 with Rails 3.2.11. Rarely, on our prod servers, it seems that some of the Journey::Routes get corrupted. These corrupted routes/endpoints then always return an action_name as an empty string or nil. The only remedy is to restart the server. This PR is fixing the race condition issue for a fresh start of the server by eager loading variables during the bootup process. Suppose however (under certain conditions) we need to trigger a Rails.application.reload_routes! in a running application. Won't the same race conditions reappear in that case because multiple threads might be processing the reload_routes! command. |
No matter what I do, I can't get these new I've added debugging statements to
If I force a reload in the rails console with
Am I missing something or is |
Yes, it is supposed to eager load it. If it is not it is a bug. Since you are investigating could you submit a PR? |
Is this fix available on 5.2.0? I am getting this same error on JRuby-9.2.6.0, Rails-5.2.0, Puma 3.11.2 |
It's definitely fixed in 5.2.3 so I recommend upgrading to that. Upgrade puma & jRuby too :) |
Tried that on our app and so far we have not see an error! Thanks! |
Why this change? When running system tests on our CI, we have been occasionally seeing server errors like: ``` Error encountered while proccessing /stylesheets/desktop_e58cf7f686aab173f9b778797f241913c2833c39.css NoMethodError: undefined method `+' for nil:NilClass /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/path/pattern.rb:139:in `[]' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:127:in `block (2 levels) in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each_with_index' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `block in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `map!' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:32:in `serve' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/routing/route_set.rb:852:in `call' ``` While looking through various Rails issues related to the error above, I came across rails/rails#27647 which is a fix to fully initialize routes before the first request is handled. However, the routes are only fully initialize only if `config.eager_load` is set to `true`. There is no reason why `config.eager_load` shouldn't be `true` in the CI environment and this is what a new Rails 7.1 app is generated with. What does this change do? Enable `config.eager_load` when `env["CI"]` is present
Why this change? When running system tests on our CI, we have been occasionally seeing server errors like: ``` Error encountered while proccessing /stylesheets/desktop_e58cf7f686aab173f9b778797f241913c2833c39.css NoMethodError: undefined method `+' for nil:NilClass /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/path/pattern.rb:139:in `[]' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:127:in `block (2 levels) in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each_with_index' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `block in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `map!' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:32:in `serve' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/routing/route_set.rb:852:in `call' ``` While looking through various Rails issues related to the error above, I came across rails/rails#27647 which is a fix to fully initialize routes before the first request is handled. However, the routes are only fully initialize only if `config.eager_load` is set to `true`. There is no reason why `config.eager_load` shouldn't be `true` in the CI environment and this is what a new Rails 7.1 app is generated with. What does this change do? Enable `config.eager_load` when `env["CI"]` is present
Why this change? When running system tests on our CI, we have been occasionally seeing server errors like: ``` Error encountered while proccessing /stylesheets/desktop_e58cf7f686aab173f9b778797f241913c2833c39.css NoMethodError: undefined method `+' for nil:NilClass /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/path/pattern.rb:139:in `[]' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:127:in `block (2 levels) in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each_with_index' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `block in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `map!' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:32:in `serve' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/routing/route_set.rb:852:in `call' ``` While looking through various Rails issues related to the error above, I came across rails/rails#27647 which is a fix to fully initialize routes before the first request is handled. However, the routes are only fully initialize only if `config.eager_load` is set to `true`. There is no reason why `config.eager_load` shouldn't be `true` in the CI environment and this is what a new Rails 7.1 app is generated with. What does this change do? Enable `config.eager_load` when `env["CI"]` is present
Why this change? When running system tests on our CI, we have been occasionally seeing server errors like: ``` Error encountered while proccessing /stylesheets/desktop_e58cf7f686aab173f9b778797f241913c2833c39.css NoMethodError: undefined method `+' for nil:NilClass /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/path/pattern.rb:139:in `[]' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:127:in `block (2 levels) in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each_with_index' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `block in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `map!' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:32:in `serve' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/routing/route_set.rb:852:in `call' ``` While looking through various Rails issues related to the error above, I came across rails/rails#27647 which is a fix to fully initialize routes before the first request is handled. However, the routes are only fully initialize only if `config.eager_load` is set to `true`. There is no reason why `config.eager_load` shouldn't be `true` in the CI environment and this is what a new Rails 7.1 app is generated with. What does this change do? Enable `config.eager_load` when `env["CI"]` is present
Why this change? When running system tests on our CI, we have been occasionally seeing server errors like: ``` Error encountered while proccessing /stylesheets/desktop_e58cf7f686aab173f9b778797f241913c2833c39.css NoMethodError: undefined method `+' for nil:NilClass /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/path/pattern.rb:139:in `[]' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:127:in `block (2 levels) in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each_with_index' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `block in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `map!' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:32:in `serve' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/routing/route_set.rb:852:in `call' ``` While looking through various Rails issues related to the error above, I came across rails/rails#27647 which is a fix to fully initialize routes before the first request is handled. However, the routes are only fully initialize only if `config.eager_load` is set to `true`. There is no reason why `config.eager_load` shouldn't be `true` in the CI environment and this is what a new Rails 7.1 app is generated with. What does this change do? Enable `config.eager_load` when `env["CI"]` is present
Why this change? When running system tests on our CI, we have been occasionally seeing server errors like: ``` Error encountered while proccessing /stylesheets/desktop_e58cf7f686aab173f9b778797f241913c2833c39.css NoMethodError: undefined method `+' for nil:NilClass /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/path/pattern.rb:139:in `[]' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:127:in `block (2 levels) in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each_with_index' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `block in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `map!' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:32:in `serve' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/routing/route_set.rb:852:in `call' ``` While looking through various Rails issues related to the error above, I came across rails/rails#27647 which is a fix to fully initialize routes before the first request is handled. However, the routes are only fully initialize only if `config.eager_load` is set to `true`. There is no reason why `config.eager_load` shouldn't be `true` in the CI environment and this is what a new Rails 7.1 app is generated with. What does this change do? Enable `config.eager_load` when `env["CI"]` is present
Why this change? When running system tests on our CI, we have been occasionally seeing server errors like: ``` Error encountered while proccessing /stylesheets/desktop_e58cf7f686aab173f9b778797f241913c2833c39.css NoMethodError: undefined method `+' for nil:NilClass /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/path/pattern.rb:139:in `[]' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:127:in `block (2 levels) in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `each_with_index' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:126:in `block in find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `map!' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:123:in `find_routes' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/journey/router.rb:32:in `serve' /__w/discourse/discourse/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.7/lib/action_dispatch/routing/route_set.rb:852:in `call' ``` While looking through various Rails issues related to the error above, I came across rails/rails#27647 which is a fix to fully initialize routes before the first request is handled. However, the routes are only fully initialize only if `config.eager_load` is set to `true`. There is no reason why `config.eager_load` shouldn't be `true` in the CI environment and this is what a new Rails 7.1 app is generated with. What does this change do? Enable `config.eager_load` when `env["CI"]` is present
AD::Journey::GTG::Simulator
is lazily built the first timeJourney::Router#find_routes
is invoked, which happens when the first request is served.On small applications with only a few dozen routes, it only takes a few milliseconds. But on large applications with hundreds of routes, building the simulator can take several hundred milliseconds (~700ms for us).
Triggering this initialization during the boot process reduces the impact of deploys on the application response time.
@rafaelfranca