Commit b8991d4b authored by Kamil Trzciński's avatar Kamil Trzciński

UpdateProjectStatistics updates after commit

This moves update from the main transaction to
the after commit. Thus, creates a very small transaction
with just update
parent f325035b
......@@ -49,8 +49,7 @@ module UpdateProjectStatistics
attr = self.class.statistic_attribute
delta = read_attribute(attr).to_i - attribute_before_last_save(attr).to_i
update_project_statistics(delta)
schedule_namespace_aggregation_worker
schedule_update_project_statistic(delta)
end
def update_project_statistics_attribute_changed?
......@@ -58,24 +57,35 @@ module UpdateProjectStatistics
end
def update_project_statistics_after_destroy
update_project_statistics(-read_attribute(self.class.statistic_attribute).to_i)
delta = -read_attribute(self.class.statistic_attribute).to_i
schedule_namespace_aggregation_worker
schedule_update_project_statistic(delta)
end
def project_destroyed?
project.pending_delete?
end
def update_project_statistics(delta)
ProjectStatistics.increment_statistic(project_id, self.class.project_statistics_name, delta)
def schedule_update_project_statistic(delta)
return if delta.zero?
if Feature.enabled?(:update_project_statistics_after_commit, default_enabled: true)
# Update ProjectStatistics after the transaction
run_after_commit do
ProjectStatistics.increment_statistic(
project_id, self.class.project_statistics_name, delta)
end
else
# Use legacy-way to update within transaction
ProjectStatistics.increment_statistic(
project_id, self.class.project_statistics_name, delta)
end
def schedule_namespace_aggregation_worker
run_after_commit do
next if project.nil?
Namespaces::ScheduleAggregationWorker.perform_async(project.namespace_id)
Namespaces::ScheduleAggregationWorker.perform_async(
project.namespace_id)
end
end
end
......
---
title: UpdateProjectStatistics updates after commit
merge_request: 20852
author:
type: performance
......@@ -19,9 +19,25 @@ describe Ci::JobArtifact do
it_behaves_like 'having unique enum values'
context 'with update_project_statistics_after_commit enabled' do
before do
stub_feature_flags(update_project_statistics_after_commit: true)
end
it_behaves_like 'UpdateProjectStatistics' do
subject { build(:ci_job_artifact, :archive, size: 106365) }
end
end
context 'with update_project_statistics_after_commit disabled' do
before do
stub_feature_flags(update_project_statistics_after_commit: false)
end
it_behaves_like 'UpdateProjectStatistics' do
subject { build(:ci_job_artifact, :archive, size: 106365) }
end
end
describe '.with_reports' do
let!(:artifact) { create(:ci_job_artifact, :archive) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment