Commit Graph

9559 Commits

Author SHA1 Message Date
John Hawthorn
578cdf8004 Avoid empty Arrays in ActiveSupport::Callbacks
ActiveSupport::Callbacks often ended up with empty Arrays: on callbacks
without a conditional, and on callback sequences which had no before
and/or after callbacks.
2023-10-21 06:51:03 -07:00
John Hawthorn
6c5a042824 Reduce memory used by ActiveSupport::Callbacks
Previously, callbacks which were shared with subclasses would not share
between them the procs generated in Filters::Before and Filters::After.
This was also lazily generated so would cause memory growth after an
application is booted (and not sharable between workers via CoW).

This was because we would rebuild the objects used to invoke the
callbacks via CallbackChain#compile, so any difference in the callback
chain would result in all of the callback procs being rebuilt.

This commit changes before and after callbacks (but not around!) to be
shared between all subclasses of where it was defined. This is done by
changing Filters::Before and Filters::After to plain classes which
respond to call instead of generating procs (which wasn't strictly
necessary but was easier to implement, and also results in simpler
objects which use less memory). These objects avoid referencing and tied
to a specific callback sequence and so can be memoized and reused.

This has the most impact on applications with many Controllers, and many
callbacks in the ApplicationController (or similar).

I also took this opportunity to merge together all the different forms
of procs generated (halting, halting_and_conditional, conditional,
simple) into one form with if statements. There isn't any significant
performance benefit from the specialization previously being done.
2023-10-20 18:02:33 -07:00
Rafael Mendonça França
7c58911bd8
Merge pull request #49721 from andrewn617/dont-overwrite-broadcast-loggers-broadcast-level-in-bootstrap-rb
Fix issue where `bootstrap.rb` overwrites the `level` of a `BroadcastLogger`'s `broadcasts`
2023-10-20 12:59:24 -04:00
Andrew Novoselac
f212fb8b83 Fix issue where bootstrap.rb overwrites the level of a BroadcastLogger's broadcasts.
In `bootstrap.rb` we set the `Rails.logger.level` to `config.log_level`. But at this point, we may have already set up a `BroadcastLogger` with multiple broadcasts that have different levels. So, calling `level=` on the `BroadcastLogger` will overwrite the level of the individual broadcasts. So instead, let's only set the `Rails.logger.level` if the logger is not a `BroadcastLogger`.
2023-10-20 10:37:54 -04:00
Jean Boussier
f11c6ac45c
Merge pull request #49716 from Shopify/invalid-compressed-cache-entries
Handle outdated Marshal payloads in Cache::Entry with 6.1 cache_format
2023-10-20 15:59:25 +02:00
Ryuta Kamizono
10d880dcd2
Merge pull request #49718 from fatkodima/fix-ordered_options-nested-dig
Fix `OrderedOptions#dig` for array indexes
2023-10-20 19:57:22 +09:00
fatkodima
1b211421cd Fix OrderedOptions#dig for array indexes 2023-10-20 13:44:33 +03:00
Jean Boussier
a6be798e5c Handle outdated Marshal payloads in Cache::Entry with 6.1 cache_format
Ref: https://github.com/rails/rails/issues/48611
Followup: https://github.com/rails/rails/pull/48663

It's the same logic than https://github.com/rails/rails/pull/48663
but now works for the 6.1 cache format.
2023-10-20 10:21:39 +02:00
fatkodima
7fd26579c0 Fix time travel helpers to work when nested using with separate classes 2023-10-20 02:46:42 +03:00
Jonathan Hefner
9f6b721b1d
Merge pull request #49694 from fatkodima/fix-file_store-key-splitting
Fix file cache store `delete_matched` to work on keys longer than allowed filename size
2023-10-19 13:48:01 -05:00
fatkodima
9e9fe7f391 Fix file cache store to split url-encoded keys on encode-sequence boundaries
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-10-19 21:33:02 +03:00
Jean Boussier
bcdeea5da7 Drop dependency on mutex_m
It used to be stdlib but is being extracted in modern rubies.

Overall its usefulness is dubious. In all cases it is included in
Rails, it's only for the `synchronize` method, but end up exposing
a dozen other useless methods.

In the end just using a Mutex is clearer and simpler.

In some cases we can even get away with a single mutex in a constant.
2023-10-18 14:27:26 +02:00
zzak
93205a8805
Suppress mail warning for "unused variable - disp_type_s"
e.g.:

```
zzak@mbp16 railties % bin/test test/application/assets_test.rb
Run options: --seed 32028

.............../Users/zzak/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/parsers/content_location_parser.rb:592: warning: assigned but unused variable - disp_type_s
I, [2023-10-15T10:38:33.005925 #97662]  INFO -- : [0ca46786-853f-4740-9e71-f644c4893502] Started GET "/assets/demo.js" for 127.0.0.1 at 2023-10-15 10:38:33 +0900
E, [2023-10-15T10:38:33.006641 #97662] ERROR -- : [0ca46786-853f-4740-9e71-f644c4893502]
[0ca46786-853f-4740-9e71-f644c4893502] ActionController::RoutingError (No route matches [GET] "/assets/demo.js"):
[0ca46786-853f-4740-9e71-f644c4893502]
../Users/zzak/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/parsers/content_location_parser.rb:592: warning: assigned but unused variable - disp_type_s
..../Users/zzak/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/parsers/content_location_parser.rb:592: warning: assigned but unused variable - disp_type_s
I, [2023-10-15T10:38:33.973335 #97660]  INFO -- : [fa848d82-0d80-4b23-925b-73fa5d0f47d4] Started GET "/posts?debug_assets=true" for 127.0.0.1 at 2023-10-15 10:38:33 +0900
I, [2023-10-15T10:38:33.975021 #97660]  INFO -- : [fa848d82-0d80-4b23-925b-73fa5d0f47d4] Processing by PostsController#index as HTML
I, [2023-10-15T10:38:33.975209 #97660]  INFO -- : [fa848d82-0d80-4b23-925b-73fa5d0f47d4]   Parameters: {"debug_assets"=>"true"}
I, [2023-10-15T10:38:33.977560 #97660]  INFO -- : [fa848d82-0d80-4b23-925b-73fa5d0f47d4] Completed 200 OK in 2ms (Views: 1.9ms | ActiveRecord: 0.0ms | Allocations: 1266)
......./Users/zzak/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/parsers/content_location_parser.rb:592: warning: assigned but unused variable - disp_type_s
.

Finished in 4.842481s, 5.9887 runs/s, 23.3351 assertions/s.
29 runs, 113 assertions, 0 failures, 0 errors, 0 skips
```

For example:
https://buildkite.com/rails/rails/builds/94132#0186806f-8a23-4d07-ae8f-3b937f8517ae/1162-1171

See also #47484
2023-10-15 10:45:01 +09:00
Nikita Vasilevsky
19f8ab2e7d
[Tests only] Enable Minitest/AssertPredicate rule 2023-10-13 19:26:47 +00:00
Jean Boussier
e01d1e25dd ActiveSupport::LogSubscriber restore compatibility with SemanticLogger
Fix: https://github.com/rails/rails/pull/49563

The semantic_logger gems doesn't behave exactly like stdlib logger
in that `SemanticLogger#level` returns a Symbol while stdlib `Logger#level`
returns an Integer.

Because of this we can't simply compare integers, we have to use the
various `#{level}?` methods.
2023-10-13 14:21:23 +02:00
fatkodima
b8829cabec Enable Style/RedundantDoubleSplatHashBraces rubocop cop 2023-10-11 14:55:00 +03:00
Ryuta Kamizono
e8cad01402
Merge pull request #49576 from fatkodima/fix-number-helper-to_d
`NumberHelper`: handle objects responding `to_d`
2023-10-11 17:46:33 +09:00
fatkodima
fe3b07f683 NumberHelper: handle objects responding to_d 2023-10-11 01:38:21 +03:00
Jean Boussier
d4172bd44f
Merge pull request #49554 from Thomascountz/fix-redis-lt7-ttl-not-set-on-first-incr-decr
Fix `RedisCacheStore` INCR/DECR for Redis < v7.0.0
2023-10-11 00:15:16 +02:00
Jenny Shen
6070685cf9
Add support for kwargs when delegating calls to custom loggers
Currently, when a method is called on Rails.logger, the BroadcastLogger will find the loggers that will respond to that method. However, when the method has keyword arguments, they are passed as regular arguments and will throw an ArgumentError. This adds keyword argument support by double splatting hash args.

```
class CustomLogger
  def foo(bar:)
    true
  end
end

Rails.logger.foo(bar: "baz")
```

Expected: `true`

Actual: `wrong number of arguments (given 1, expected 0) (ArgumentError)`
2023-10-10 21:43:13 +01:00
Thomas Countz
600a052c8c Fix RedisCacheStore INCR/DECR for Redis < v7.0.0
This commit fixes a discrepancy in the behavior of the `#increment` and
`#decrement` methods in `RedisCacheStore` when used with Redis versions less
than 7.0.0. The existing condition `count != amount` prevented setting the
Time-To-Live (TTL) for keys that were equal to the increment/decrement amount
after the `INCRBY`/`DECRBY` operation. This occurs when incrementing a
non-existent key by `1`, for example.

Using Redis pipelining, we minimize the network overhead incurred by checking
for existing TTLs. It decouples the TTL operations from the increment/decrement
operation, allowing the TTL to be set correctly regardless of the resulting
value from the `INCRBY`/`DECRBY`.

New tests have been added to verify the correct behavior of `#increment` and
`#decrement` methods, specifically when the `expires_in` option is not used.
Using a separate cache store instance (`@cache_no_ttl`), these tests ensure that
keys are correctly incremented or decremented and that their TTL remains unset.

Co-authored-by: Benjamin Quorning <benjamin@quorning.net>
Co-authored-by: Jury Razumau <jury.razumau@zendesk.com>
Co-authored-by: Edyta Rozczypała <edyta.rozczypala@zendesk.com>
2023-10-10 19:32:26 +00:00
Jean Boussier
a385d7b170
Merge pull request #49542 from pjambet/pj/fix-memory-store-race-condition
MemoryStore: prevent race condition
2023-10-09 09:24:47 +02:00
Pierre Jambet
7f2e0ffb7e
MemoryStore: prevent race condition
It looks like #46305 accidentally removed the synchronize block that
would prevent a race conidition where two threads would read the same
value and only a single increment/decrement would take effect as they
would both write the same value.
2023-10-08 13:57:31 -04:00
Hartley McGuire
ec0600b7c7
Remove require of ActiveSupport::ForkTracker
ForkTracker has been autoloaded since [before][1] the [require][2] was
added.

[1]: 78b9580e5f3208c7048659de24f2220693afb23c
[2]: eba1534939fe1cf005746f12446235bdd52014c1
2023-10-07 23:29:02 -04:00
Jonathan Hefner
e5124aed3f Autolink references for AS::Notifications::Instrumenter [ci-skip]
This also rewords occurrences of "made since the call to `start!` and
the call to `finish!`" to "made between the call to `start!` and the
call to `finish!`", for clarity.
2023-10-07 12:26:33 -05:00
Jonathan Hefner
ed08eea99f Capitalize framework names [ci-skip] 2023-10-07 12:26:26 -05:00
Jonathan Hefner
4726b1ab47 Ensure on_rotation appears in RDoc [ci-skip]
When a `:method:` doc is immediately followed by the `private` keyword,
RDoc will hide that doc as if it were a private method.

To ensure that `ActiveSupport::MessageEncryptors#on_rotation` and
`ActiveSupport::MessageVerifiers#on_rotation` both appear in the
rendered docs, this commit adds a delimiter comment before each
`private` keyword.
2023-10-07 12:26:20 -05:00
Jonathan Hefner
dc99003487 Autolink AS::Notifications and AS::ErrorReporter [ci-skip] 2023-10-07 11:55:35 -05:00
Jonathan Hefner
d601d9d78a Fix typos for Cache::Store#fetch "Dynamic Options" [ci-skip] 2023-10-07 11:55:35 -05:00
Jonathan Hefner
46c42a2ff9 Omit list for Cache::Store#fetch "Dynamic Options" [ci-skip]
The extra indentation on this list causes it to be rendered as code
instead of as an unordered list.  But, furthermore, the items in this
list should be setter methods for `ActiveSupport::Cache::WriteOptions`,
not symbols.  Since the `ActiveSupport::Cache::WriteOptions` class is
linked in the preceding paragraph, we can simply omit this list.
2023-10-07 11:55:34 -05:00
Edouard CHIN
8be0c10c48 Add doc to explain how to migrate to the new BroadcastLogger:
- This should make it easier for apps or libraries that were
  previously relying on the private API.
  Also took the opportunity to tweak the doc of the BroadcastLogger
  to mention what happens when calling a non-standard method.

  Fix #49494
2023-10-07 14:43:46 +02:00
Rafael Mendonça França
fed3125ea1
Merge pull request #49470 from rails/rm-eager-load-model-schema
Load the model schema when running test in eager load context
2023-10-04 05:51:39 -04:00
Rafael Mendonça França
1f0262aa2b
Separate the CI environment from the application CI environment
Right now we are using both to test the Rails applications we generate
and to test Rails itself. Let's keep CI for the app and BUILDKITE to
the framework.
2023-10-04 09:36:51 +00:00
fatkodima
39438318c7 Implement HashWithIndifferentAccess#to_proc
Previously, calling `#to_proc` on `HashWithIndifferentAccess` object used inherited `#to_proc`
method from the `Hash` class, which was not able to access values using indifferent keys.

Fixes #48770.
2023-10-03 21:26:08 +03:00
Alex
ce321c4539 NumberHelper: handle very large numbers
Fixes https://github.com/rails/rails/issues/49461

Co-authored-by: fatkodima <5657035+fatkodima@users.noreply.github.com>
2023-10-03 14:46:06 +10:00
Eugene Kenny
5574a2fcb4 Delegate block in broadcast logger method_missing
When a method called on a broadcast logger is passed a block, it should
be forwarded to all subscribed loggers.
2023-10-02 16:35:01 +01:00
Jean Boussier
02e679ba75 Get rid of the jruby_skip test helper
The last test calling it actually passes on latest
JRuby.
2023-10-02 13:01:44 +02:00
Jonathan Hefner
b68cc94096
Merge pull request #49447 from jonathanhefner/message_pack-fix-rails_max_threads
Fix `AS::MessagePack` with `ENV["RAILS_MAX_THREADS"]`
2023-10-01 15:51:43 -05:00
Jonathan Hefner
bb8ad695f4 Fix AS::MessagePack with ENV["RAILS_MAX_THREADS"]
`ENV` values are strings, so `ENV["RAILS_MAX_THREADS"]` must be parsed
as an int.

Unfortunately, `MessagePack::Factory::Pool::MemberPool` does not expose
a method to check its member count, so the most we can assert is that
roundtripping works as expected.

Fixes #49446.
2023-10-01 15:22:05 -05:00
Rafael Mendonça França
0f7fe4ab01
Revert "Also rescue TZInfo::ZoneinfoDirectoryNotFound when loading time zone data"
This reverts commit 54f30488e190eea5e923fe51914051df0e8c33f6.

Reason: THis isn't necessary. `TZInfo::DataSource.get` makes sure
the exception gets translated.
2023-10-01 20:20:06 +00:00
Rafael Mendonça França
db2ef1d250
Merge pull request #49417 from Edouard-chin/ec-logger-fix
Fix the BroadcastLogger being initialized too late:
2023-09-29 15:45:03 -04:00
Bart de Water
95b6fbd00f Stop building AS::Notifications::Event manually
It's possible since Rails 6 (3ea2857943dc294d7809930b4cc5b318b9c39577) to let the framework create Event objects, but the guides and docs weren't updated to lead with this example.

Manually instantiating an Event doesn't record CPU time and allocations, I've seen it more than once that people copy-pasting the example code get confused about these stats returning 0. The tests here show that - just like the apps I've worked on - the old pattern keeps getting copy-pasted.
2023-09-29 12:34:23 -04:00
Edouard CHIN
40cb50e06e Fix the BroadcastLogger being initialized too late:
- An oversight of #48615 is that it changes the `Rails.logger` to be
  a broadcast logger after the app is booted. Anything referencing
  `Rails.logger` during the boot process will get a simple logger and
  ultimately resulting in logs not being broadcasted.

  For example `ActionController::Base.logger.info("abc")` would
  just output logs in the `development.log` file, not on STDOUT.

  ----

  The only solution I could think of is to create a BroadcastLogger
  earlier at boot, and add logger to that broadcast when needed (instead
  of modiyfing the `Rails.logger` variable).
2023-09-29 15:42:47 +02:00
fatkodima
7ef86b6a49 Enable Lint/RedundantSafeNavigation rubocop cop 2023-09-27 14:55:07 +03:00
Rafael Mendonça França
fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França
e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00:00
Rafael Mendonça França
54f30488e1
Also rescue TZInfo::ZoneinfoDirectoryNotFound when loading time zone data
Fixes #49375.
2023-09-27 02:52:20 +00:00
Rafael Mendonça França
4c72cc2b04
Merge pull request #48615 from Edouard-chin/ec-logger
Add a public API for broadcasting logs
2023-09-25 17:13:58 -04:00
Rafael Mendonça França
88bb5f2749
Define the method in the right place 2023-09-25 21:04:41 +00:00
Rafael Mendonça França
4e605f5d0f
Fix ruby warning
Remove `.[]` before redefining it.
2023-09-25 20:47:36 +00:00