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

[WIP] Use zeitwerk on load instead of hooking descendants subclasses #22945

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jrafanie
Copy link
Member

WIP - don't merge yet

@jrafanie
Copy link
Member Author

@miq-bot cross-repo-tests /all ManageIQ/manageiq-ui-classic#9119

miq-bot pushed a commit to ManageIQ/manageiq-cross_repo-tests that referenced this pull request Mar 14, 2024
@miq-bot miq-bot removed the wip label Mar 14, 2024
@Fryguy Fryguy changed the title Use zeitwerk on load instead of hooking descendants subclasses [WIP] Use zeitwerk on load instead of hooking descendants subclasses Mar 15, 2024
@miq-bot miq-bot added the wip label Mar 15, 2024
Comment on lines +21 to +29
child.safe_constantize
rescue NameError => err
Copy link
Member

Choose a reason for hiding this comment

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

Can safe_constantize actually raise a NameError? I thought under the covers they caught that and just returned nil.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, it actually did, thanks for reminding me. I need to investigate why.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, the short of it is that some top level classes can introduce cycles if you try to constantize their children. Their children can reference constants that get loaded and loading of mixins and perent classes and some of those kick off more constantizing of children classes and some of those can be cyclic loads. So far,MiqDecorator, OrchestrationStack::Status, and ResourcePool look to be the problems.

Copy link
Member

Choose a reason for hiding this comment

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

Ewwww ResourcePool is bad. We need to fix that

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, I'm getting things green with the current code and will try to figure out how to fix these classes or fix how we load.

We're now eager loading subclasses when loading the parent but this is far
less reliant on hacks and closer to full eager loading, which is what rails has
been pushing us to do for years.  Let's try it out.

Rails changed in 7.0 to call subclasses from reload_schema_from_cache here:
rails/rails@6f30cc0

It also changed to call descendants on the callback class (self) insead of
the ActiveSupport::DescendantsTracker here:
rails/rails@ffae3bd

We are not expecting to be called from these locations.

Avoiding descendants / subclasses calls in conditionals like this are just brittle
so we need to stop hooking through these methods:
if !%w[reload_schema_from_cache __update_callbacks descendants subclasses].include?(caller_locations.first.base_label)
@jrafanie jrafanie force-pushed the use_zeitwerk_on_load_instead_of_hooking_descendants_subclasses branch from e54f435 to f0715ae Compare March 19, 2024 14:22
@miq-bot
Copy link
Member

miq-bot commented Mar 19, 2024

Some comments on commits jrafanie/manageiq@cdb773d~...f0715ae

config/initializers/zeitwerk.rb

  • ⚠️ - 23 - Detected puts. Remove all debugging statements.
  • ⚠️ - 25 - Detected puts. Remove all debugging statements.
  • ⚠️ - 30 - Detected puts. Remove all debugging statements.

@miq-bot
Copy link
Member

miq-bot commented Mar 19, 2024

Checked commits jrafanie/manageiq@cdb773d~...f0715ae with ruby 2.7.8, rubocop 1.56.3, haml-lint 0.51.0, and yamllint
5 files checked, 5 offenses detected

config/initializers/zeitwerk.rb

lib/extensions/descendant_loader.rb

DescendantLoader.instance.discovered_parent_child_classes.each do |parent, children|
next if excluded_from_eager_load.include?(parent)

puts "Registering on_load for class: #{parent}"
Copy link
Member

Choose a reason for hiding this comment

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

I know this is for debug but I wonder if we should keep it and use $log.debug instead. (Same comment for the other puts lines)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants