-
Notifications
You must be signed in to change notification settings - Fork 897
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
Purging of ContainerQuota & ContainerQuotaItem #17167
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class ContainerQuota < ApplicationRecord | ||
module Purging | ||
extend ActiveSupport::Concern | ||
include PurgingMixin | ||
|
||
module ClassMethods | ||
def purge_date | ||
::Settings.container_entities.history.keep_archived_quotas.to_i_with_method.seconds.ago.utc | ||
end | ||
|
||
def purge_window_size | ||
::Settings.container_entities.history.purge_window_size | ||
end | ||
|
||
def purge_scope(older_than) | ||
where(arel_table[:deleted_on].lteq(older_than)) | ||
end | ||
|
||
def purge_associated_records(ids) | ||
ContainerQuotaScope.where(:container_quota_id => ids).delete_all | ||
ContainerQuotaItem.where(:container_quota_id => ids).delete_all | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class ContainerQuotaItem < ApplicationRecord | ||
module Purging | ||
extend ActiveSupport::Concern | ||
include PurgingMixin | ||
|
||
module ClassMethods | ||
def purge_date | ||
::Settings.container_entities.history.keep_archived_quotas.to_i_with_method.seconds.ago.utc | ||
end | ||
|
||
def purge_window_size | ||
::Settings.container_entities.history.purge_window_size | ||
end | ||
|
||
def purge_scope(older_than) | ||
where(arel_table[:deleted_on].lteq(older_than)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
describe ContainerQuota do | ||
context "::Purging" do | ||
context ".purge_queue" do | ||
before do | ||
EvmSpecHelper.create_guid_miq_server_zone | ||
end | ||
let(:purge_time) { (Time.zone.now + 10).round } | ||
|
||
it "submits to the queue" do | ||
expect(described_class).to receive(:purge_date).and_return(purge_time) | ||
described_class.purge_timer | ||
|
||
q = MiqQueue.all | ||
expect(q.length).to eq(1) | ||
expect(q.first).to have_attributes( | ||
:class_name => described_class.name, | ||
:method_name => "purge_by_date", | ||
:args => [purge_time] | ||
) | ||
end | ||
end | ||
|
||
context ".purge" do | ||
let(:deleted_date) { 6.months.ago } | ||
|
||
before do | ||
@old_quota = FactoryGirl.create(:container_quota, :deleted_on => deleted_date - 1.day) | ||
@old_quota_scope = FactoryGirl.create(:container_quota_scope, :container_quota => @old_quota) | ||
@old_quota_old_item = FactoryGirl.create(:container_quota_item, :container_quota => @old_quota, | ||
:deleted_on => deleted_date - 1.day) | ||
@old_quota_active_item = FactoryGirl.create(:container_quota_item, :container_quota => @old_quota, | ||
:deleted_on => nil) | ||
|
||
@purge_date_quota = FactoryGirl.create(:container_quota, :deleted_on => deleted_date) | ||
|
||
@new_quota = FactoryGirl.create(:container_quota, :deleted_on => deleted_date + 1.day) | ||
@new_quota_scope = FactoryGirl.create(:container_quota_scope, :container_quota => @new_quota) | ||
@new_quota_old_item = FactoryGirl.create(:container_quota_item, :container_quota => @new_quota, | ||
:deleted_on => deleted_date - 1.day) | ||
end | ||
|
||
def assert_unpurged_ids(model, unpurged_ids) | ||
expect(model.order(:id).pluck(:id)).to eq(Array(unpurged_ids).sort) | ||
end | ||
|
||
it "purge_date and older" do | ||
described_class.purge(deleted_date) | ||
assert_unpurged_ids(ContainerQuota, @new_quota.id) | ||
assert_unpurged_ids(ContainerQuotaScope, @new_quota_scope.id) | ||
# This quota item is itself due for purging, but not as part of ContainerQuota::Purging. | ||
assert_unpurged_ids(ContainerQuotaItem, @new_quota_old_item.id) | ||
end | ||
|
||
it "with a window" do | ||
described_class.purge(deleted_date, 1) | ||
assert_unpurged_ids(ContainerQuota, @new_quota.id) | ||
assert_unpurged_ids(ContainerQuotaScope, @new_quota_scope.id) | ||
# This quota item is itself due for purging, but not as part of ContainerQuota::Purging. | ||
assert_unpurged_ids(ContainerQuotaItem, @new_quota_old_item.id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
describe ContainerQuotaItem do | ||
context "::Purging" do | ||
context ".purge_queue" do | ||
before do | ||
EvmSpecHelper.create_guid_miq_server_zone | ||
end | ||
let(:purge_time) { (Time.zone.now + 10).round } | ||
|
||
it "submits to the queue" do | ||
expect(described_class).to receive(:purge_date).and_return(purge_time) | ||
described_class.purge_timer | ||
|
||
q = MiqQueue.all | ||
expect(q.length).to eq(1) | ||
expect(q.first).to have_attributes( | ||
:class_name => described_class.name, | ||
:method_name => "purge_by_date", | ||
:args => [purge_time] | ||
) | ||
end | ||
end | ||
|
||
context ".purge" do | ||
let(:deleted_date) { 6.months.ago } | ||
|
||
before do | ||
@old_quota = FactoryGirl.create(:container_quota, :deleted_on => deleted_date - 1.day) | ||
@old_quota_scope = FactoryGirl.create(:container_quota_scope, :container_quota => @old_quota) | ||
@old_quota_old_item = FactoryGirl.create(:container_quota_item, :container_quota => @old_quota, | ||
:deleted_on => deleted_date - 1.day) | ||
@old_quota_purge_date_item = FactoryGirl.create(:container_quota_item, :container_quota => @old_quota, | ||
:deleted_on => deleted_date) | ||
@old_quota_new_item = FactoryGirl.create(:container_quota_item, :container_quota => @old_quota, | ||
:deleted_on => deleted_date + 1.day) | ||
|
||
# Quota items may get archived as result of quota edits, while parent quota remains active. | ||
@active_quota = FactoryGirl.create(:container_quota, :deleted_on => nil) | ||
@active_quota_scope = FactoryGirl.create(:container_quota_scope, :container_quota => @active_quota) | ||
@active_quota_old_item = FactoryGirl.create(:container_quota_item, :container_quota => @active_quota, | ||
:deleted_on => deleted_date - 1.day) | ||
@active_quota_purge_date_item = FactoryGirl.create(:container_quota_item, :container_quota => @active_quota, | ||
:deleted_on => deleted_date) | ||
@active_quota_new_item = FactoryGirl.create(:container_quota_item, :container_quota => @active_quota, | ||
:deleted_on => deleted_date + 1.day) | ||
@active_quota_active_item = FactoryGirl.create(:container_quota_item, :container_quota => @active_quota, | ||
:deleted_on => nil) | ||
end | ||
|
||
def assert_unpurged_ids(model, unpurged_ids) | ||
expect(model.order(:id).pluck(:id)).to eq(Array(unpurged_ids).sort) | ||
end | ||
|
||
it "purge_date and older" do | ||
described_class.purge(deleted_date) | ||
# @old_quota is itself due for purging, but not as part of ContainerQuotaItem::Purging. | ||
assert_unpurged_ids(ContainerQuota, [@old_quota.id, @active_quota.id]) | ||
assert_unpurged_ids(ContainerQuotaScope, [@old_quota_scope.id, @active_quota_scope.id]) | ||
assert_unpurged_ids(ContainerQuotaItem, [@old_quota_new_item.id, @active_quota_new_item.id, @active_quota_active_item.id]) | ||
end | ||
|
||
it "with a window" do | ||
described_class.purge(deleted_date, 1) | ||
# @old_quota is itself due for purging, but not as part of ContainerQuotaItem::Purging. | ||
assert_unpurged_ids(ContainerQuota, [@old_quota.id, @active_quota.id]) | ||
assert_unpurged_ids(ContainerQuotaScope, [@old_quota_scope.id, @active_quota_scope.id]) | ||
assert_unpurged_ids(ContainerQuotaItem, [@old_quota_new_item.id, @active_quota_new_item.id, @active_quota_active_item.id]) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reminder: adding
purge_associated_records
currently causes us to retrieve all ids that are deleted from container quota. If the number of records is low, then this is not a problem.If there are a bunch of records to be deleted, we may want to come up with another way to clear out these associated 2 tables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect few ContainerQuota records purged — only when admins delete the whole quota definition in a project (*) — but possibly many child ContainerQuotaItem records to purge (these may be archived on inventory changes, may become comparable to ContainerGroup table).
(*) Technically it's possible to have an arbitrary number of quotas in a project, but in practice 0 or 1 will be common, unlikely to be over 4.
I'm doing this to avoid leaving orphan items. Since items should get archived when parent quota gets archived,
we could also avoid orphansby giving items a lower lifetime, e.g. 5.month vs 6.month. But I prefer ensuring DB consistency by explicit code over "it happens to work if tuned correctly" :-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch that,
purge_associated_records
is also needed to avoid leaving orphan ContainerQuotaScopes, no alternative there.