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

Let destroy old job instances in batch #108

Merged
merged 3 commits into from
Mar 29, 2018

Conversation

riseshia
Copy link
Contributor

Follow up of #107

#107 script will affect another batch execution if it has old job instances a lot.
To reduce the effect estimated on production service, let the deletion process be worked in batch(per 1000).

@cookpad/dev-infra please review this :)

to reduce the effect which estimated on production service
old_instances.destroy_all
.order(id: :asc)
.in_batches do |old_instances|
count += old_instances.count
Copy link
Member

Choose a reason for hiding this comment

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

.size ?

Copy link
Contributor Author

@riseshia riseshia Mar 29, 2018

Choose a reason for hiding this comment

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

in_batches returns ActiveRecord::Relation, so I think count is better, isn't it?

Copy link
Member

Choose a reason for hiding this comment

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

ah yes correct; the following lines just run DELETE statement. Please disregard

Copy link
Member

Choose a reason for hiding this comment

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

💭 I forgot how Rails destroy_all behaves... if it tries to load records, .size might 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 can reduce a count query by calling size after/before calling destroy_all, using size looks great. Otherwise, we should use count as normal. In addition, we should leave a comment about that behavior.

Copy link
Contributor Author

@riseshia riseshia Mar 29, 2018

Choose a reason for hiding this comment

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

in_batches yields loaded active record relation 🙈
so, not to send count query, fix to use .size f59f230

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

🙈

relation is loaded already
Copy link
Contributor

@taiki45 taiki45 left a comment

Choose a reason for hiding this comment

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

Looks good modulo comments.

old_instances.destroy_all
.order(id: :asc)
.in_batches do |old_instances|
count += old_instances.count
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 can reduce a count query by calling size after/before calling destroy_all, using size looks great. Otherwise, we should use count as normal. In addition, we should leave a comment about that behavior.

@riseshia
Copy link
Contributor Author

riseshia commented Mar 29, 2018

ci for ruby 2.5 is failing, this is not a problem of the project, but from outside...
I will let it back when it could be recoverable. ;) cfe72cd

@riseshia riseshia merged commit 945f9f6 into cookpad:master Mar 29, 2018
@riseshia riseshia deleted the let-handle-many branch March 29, 2018 07:11
count += old_instances.size
Kuroko2::JobInstance.transaction do
old_instances.destroy_all
end
Copy link
Member

Choose a reason for hiding this comment

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

Is not sleep necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which situation are you worried? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

first, destroy_all is just call destroy to each instance.
https://github.com/rails/rails/blob/008538081b87940ad1a4fd29df730ec5d0421ee5/activerecord/lib/active_record/relation.rb#L379-L381
second, destroy seems to use the transaction which includes only itself.
https://github.com/rails/rails/blob/6aa5cf03ea8232180ffbbae4c130b051f813c670/activerecord/lib/active_record/persistence.rb#L336-L347

Based on these, we might have 2 options:

  • Just unwrap Kuroko2::JobInstance.transaction
  • old_instances.each { |i| Kuroko2::JobInstance.transaction { i.destroy } }
    • I'm doubt to do it worth 🤔

how do you think? @eisuke

Copy link
Member

Choose a reason for hiding this comment

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

I think that "Just unwrap Kuroko2::JobInstance.transaction" is good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#109 Ready for review XD

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

Successfully merging this pull request may close these issues.

4 participants