Skip to content

Conversation

@nidhi-soni1104
Copy link
Contributor

@nidhi-soni1104 nidhi-soni1104 commented Mar 21, 2024

What this PR does / why we need it:

We need a rake task to go over all tables with the tenant_id field and make check whether that tenant still exists, reporting and optionally removing these on the way.

complete description is in JIRA

Which issue(s) this PR fixes

https://issues.redhat.com/browse/THREESCALE-10855

Note: I have commented the deleted line for now but added puts so whoever will run it while reviewing can see the puts

@nidhi-soni1104 nidhi-soni1104 self-assigned this Mar 21, 2024
@nidhi-soni1104
Copy link
Contributor Author

@akostadinov Updated

# Tables to exclude from orphaned objects check
excluded_tables = ['accounts', 'audits', 'categories', 'category_types', 'cms_templates', 'legal_term_acceptances', 'legal_term_bindings',
'legal_term_versions', 'proxy_logs', 'schema_migrations', 'service_cubert_infos', 'slugs', 'taggings', 'tags']
excluded_tables = ['cms_templates']
Copy link
Contributor

Choose a reason for hiding this comment

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

Why exclude this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this exclusion can be removed

Copy link
Contributor

Choose a reason for hiding this comment

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

I removed the exclusion, but I don't know if a coincidence or not - the task got stuck on Found orphaned objects in cms_templates: when I tried it in staging...

Copy link
Contributor

Choose a reason for hiding this comment

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

If it doesn't persist, let's think it's a coincidence. Why should cms_templates be special?


if orphaned_objects.exists?
puts "Found orphaned objects in #{model.table_name}:"
orphaned_objects.each { |obj| puts "- ID: #{obj.id}, Tenant ID: #{obj.tenant_id}" }
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd use find_each here instead of each to use batching.

Copy link
Contributor

Choose a reason for hiding this comment

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

Addressed in ee9e0a8

@github-actions github-actions bot added the Stale label May 11, 2024
@3scale 3scale deleted a comment from github-actions bot May 11, 2024
@github-actions github-actions bot removed the Stale label May 12, 2024
@jlledom jlledom force-pushed the THREESCALE-10855-eliminate-orphan-objects branch from 89a470f to 8e47c60 Compare June 10, 2024 12:30
@jlledom
Copy link
Contributor

jlledom commented Jun 10, 2024

I rebased this, It was too old.

Copy link
Contributor

@jlledom jlledom left a comment

Choose a reason for hiding this comment

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

I wrote a couple of comments but this LGTM. I haven't tried to run the script locally though.

It would be good to write a couple of tests. Do we have tests for rake tasks?

# Tables to exclude from orphaned objects check
excluded_tables = ['accounts', 'audits', 'categories', 'category_types', 'cms_templates', 'legal_term_acceptances', 'legal_term_bindings',
'legal_term_versions', 'proxy_logs', 'schema_migrations', 'service_cubert_infos', 'slugs', 'taggings', 'tags']
excluded_tables = ['cms_templates']
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this exclusion can be removed

@github-actions
Copy link

This PR is stale because it has not received activity for more than 30 days. Remove stale label or comment or this will be closed in 15 days.

@github-actions github-actions bot added Stale and removed Stale labels Jul 11, 2024
@github-actions
Copy link

This PR is stale because it has not received activity for more than 30 days. Remove stale label or comment or this will be closed in 15 days.

@github-actions github-actions bot added the Stale label Aug 12, 2024
@mayorova mayorova force-pushed the THREESCALE-10855-eliminate-orphan-objects branch 2 times, most recently from 23b6efa to ee9e0a8 Compare August 16, 2024 11:17
task :cleanup_orphans, [:mode] => :environment do |_task, args|
puts 'Checking and removing orphaned objects...'

destroy = args[:mode] == "destroy"
Copy link
Contributor

@mayorova mayorova Aug 16, 2024

Choose a reason for hiding this comment

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

So, the idea is that by default calling the task would only print the logs.
To actually destroy stuff, one would need to run

bundle exec rake "multitenant:tenants:cleanup_orphans[destroy]"

By the way, the logs are probably too verbose... Or maybe that's just the first time, as we have lots of orphans. Once the big batch is deleted, probably it will be fine.


puts "WARNING: the found orphan objects will be destroyed" if destroy

provider_account_ids = Account.where(provider: true).pluck(:id) + [Account.master.id]
Copy link
Contributor

@mayorova mayorova Aug 16, 2024

Choose a reason for hiding this comment

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

I've added Master's ID here, to avoid deleting master's objects. Specifically, the master itself was being reported as an orphan 😅 (because it does not have provider: true)

Found orphaned objects in accounts:
- ID: 1, Tenant ID: 1

And we probably don't want to destroy the master 🙃

Copy link
Contributor

@akostadinov akostadinov Aug 17, 2024

Choose a reason for hiding this comment

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

You're too goodhearted..

Copy link
Contributor

Choose a reason for hiding this comment

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

But this could be hundreds of thousands of objects. Can't we filter this out in the query itself?


if destroy
puts "Destroying orphan #{model.table_name}..."
orphaned_objects.in_batches(of: 100).destroy_all
Copy link
Contributor

@mayorova mayorova Aug 16, 2024

Choose a reason for hiding this comment

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

Added batched deletion, but still not sure if it still may have negative effects on the DB...

We might also need some more logging to see whether the operation was succeeded.

Copy link
Contributor

Choose a reason for hiding this comment

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

we need to revisit how we iterate over many objects

Copy link
Contributor

Choose a reason for hiding this comment

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

Added batched deletion, but still not sure if it still may have negative effects on the DB...

For sure batching is an improvement, what else could we do? We should be sure to run this after the billing jobs have finished and heads up ops.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we would use the new Sidekiq 7.3 iteration jobs. But if we just do in a rake task in the foreground, batches would be better.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to run thins in produciton, we most likely need to use this iteration sidekiq job type. Because running haevy deletion in production may overburden the database. While recent hardware is much faster, I'm not sure how good this would be.

@github-actions github-actions bot removed the Stale label Aug 17, 2024
jlledom
jlledom previously approved these changes Aug 20, 2024
@mayorova mayorova force-pushed the THREESCALE-10855-eliminate-orphan-objects branch 2 times, most recently from 4d3c822 to e2c5276 Compare September 16, 2024 09:45

provider_account_ids = Account.where(provider: true).pluck(:id) + [Account.master.id]

ApplicationRecord.descendants.each do |model|
Copy link
Contributor

@akostadinov akostadinov Sep 17, 2024

Choose a reason for hiding this comment

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

We can filter the classes further by filtering out children STI classes. Because if we run the query with the base STI class, running with the children will be redundant. SOmething along the lines of:

     def base_models
      all_models = ApplicationRecord.descendants.select(&:arel_table).reject(&:abstract_class?)
      all_models.select! { _1.attribute_names.include? "tenant_id"}
      # we only want base STI classes, not the children
      all_models.select! do |model|
        base_class = model.base_class
        # either current model is the base_class or we can't find a base class amongst the discovered models /which would be very weird/
        base_class == model || all_models.none? { |potential_parent| potential_parent == base_class }
      end
    end

Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI adding a base_models helper (app/lib/three_scale/models.rb) method in #3958

@mayorova mayorova force-pushed the THREESCALE-10855-eliminate-orphan-objects branch 2 times, most recently from eea080b to a9d31ad Compare September 30, 2024 14:08
@github-actions github-actions bot removed the Stale label Mar 11, 2025
@github-actions github-actions bot added the Stale label Apr 12, 2025
@akostadinov akostadinov removed the Stale label Apr 12, 2025
@3scale 3scale deleted a comment from github-actions bot Apr 12, 2025
@github-actions github-actions bot added the Stale label May 13, 2025
@3scale 3scale deleted a comment from github-actions bot May 13, 2025
@akostadinov akostadinov removed the Stale label May 13, 2025
@github-actions github-actions bot added the Stale label Jun 13, 2025
@3scale 3scale deleted a comment from github-actions bot Jun 13, 2025
@akostadinov akostadinov removed the Stale label Jun 13, 2025
@github-actions
Copy link

This PR is stale because it has not received activity for more than 30 days. Remove stale label or comment or this will be closed in 15 days.

@github-actions github-actions bot added the Stale label Jul 14, 2025
@github-actions github-actions bot closed this Jul 29, 2025
@akostadinov akostadinov reopened this Jul 29, 2025
@akostadinov akostadinov removed the Stale label Jul 29, 2025
@github-actions github-actions bot added the Stale label Aug 29, 2025
@3scale 3scale deleted a comment from github-actions bot Aug 29, 2025
@akostadinov akostadinov removed the Stale label Aug 29, 2025
@github-actions github-actions bot added the Stale label Sep 29, 2025
@3scale 3scale deleted a comment from github-actions bot Sep 29, 2025
@akostadinov akostadinov removed the Stale label Sep 29, 2025
@github-actions github-actions bot added the Stale label Oct 30, 2025
@akostadinov akostadinov removed the Stale label Oct 30, 2025
@3scale 3scale deleted a comment from github-actions bot Oct 30, 2025
@github-actions github-actions bot added the Stale label Nov 30, 2025
@3scale 3scale deleted a comment from github-actions bot Dec 1, 2025
@akostadinov akostadinov removed the Stale label Dec 1, 2025
@github-actions github-actions bot added the Stale label Jan 1, 2026
@3scale 3scale deleted a comment from github-actions bot Jan 5, 2026
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.

5 participants