-
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
Comparing Time
object with an integer doesn't throw an error
#50868
Comments
Why this change? In `Discourse::Prometheus::InternalMetric#calc_postgres_highest_sequence`, it was initially written such that we will only query the database once every 60 seconds by setting the `postgres_highest_sequence_last_check` class variable. However, there is a subtle bug in Rails where we were storing a `Time` object in `@@postgres_highest_sequence_last_check` and then subsequently using it to compare against an `Integer` which ends up always evaluating to `false`. ``` [3] pry(main)> now = Time.now => 2024-01-25 06:01:07.44213925 +0800 [4] pry(main)> now >= Time.now.to_i - 60 => false [5] pry(main)> now.to_i >= Time.now.to_i - 60 => true ``` Due to this bug, we end up querying all the databases every time metrics are collected (defaults to every 5 seconds) instead of only querying the databases every 60 seconds which is what we want to reduce the load on our databases. The bug in Rails has been reported in rails/rails#50868
Why this change? In `Discourse::Prometheus::InternalMetric#calc_postgres_highest_sequence`, it was initially written such that we will only query the database once every 60 seconds by setting the `postgres_highest_sequence_last_check` class variable. However, there is a subtle bug in Rails where we were storing a `Time` object in `@@postgres_highest_sequence_last_check` and then subsequently using it to compare against an `Integer` which ends up always evaluating to `false`. ``` [3] pry(main)> now = Time.now => 2024-01-25 06:01:07.44213925 +0800 [4] pry(main)> now >= Time.now.to_i - 60 => false [5] pry(main)> now.to_i >= Time.now.to_i - 60 => true ``` Due to this bug, we end up querying all the databases every time metrics are collected (defaults to every 5 seconds) instead of only querying the databases every 60 seconds which is what we want to reduce the load on our databases. The bug in Rails has been reported in rails/rails#50868
I tried to follow the method chain for rails/activesupport/lib/active_support/core_ext/time/calculations.rb Lines 315 to 328 in c7551d0
The integer value on the right side of the comparison isn't rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb Lines 206 to 214 in c7551d0
The super-method in this case is rails/activesupport/lib/active_support/core_ext/date/calculations.rb Lines 151 to 160 in c7551d0
Here's the twist. While I didn't read through the C code for the native I haven't checked to see how far back this behavior goes. |
To follow up on this a bit and lend support to the original statement of the issue, I think it makes sense that for any In terms of solving this, I'm not sure whether it makes more sense to fix it in |
Steps to reproduce
Expected behavior
I'm not sure but I would expect one of the following:
Time
object with anInteger
Time
object to anInteger
before comparingInteger
to aTime
object before comparingActual behavior
No errors are raised and the code will always evaluate to
false
.System configuration
Rails version: 7.1.3
Ruby version: 3.2.2
The text was updated successfully, but these errors were encountered: