diff options
author | Mayra Cabrera <mcabrera@gitlab.com> | 2019-06-27 14:31:01 -0500 |
---|---|---|
committer | Mayra Cabrera <mcabrera@gitlab.com> | 2019-07-01 09:08:14 -0500 |
commit | d10db045ede952b76395e94a1957cc8e2deeb10d (patch) | |
tree | e687937247fa5fb5757ddfa543e33be549b10df8 | |
parent | 4b5f9ced916ca3b258130849e5b4c0491a454e09 (diff) | |
download | gitlab-ce-62214-namespaces-storage-statistics-on-database.tar.gz |
Addresses BE maintainer review62214-namespaces-storage-statistics-on-database
- Uses find_each instead of each, as the latter will load all rows into
memory.
- Includes a new spec on UpdateProjectStatistics shared examples to
ensure we're avoiding N+1 queries.
- Changes time Prunding worker is executed as midnight is mostly used by
other workers.
4 files changed, 19 insertions, 5 deletions
diff --git a/app/workers/namespaces/prune_aggregation_schedules_worker.rb b/app/workers/namespaces/prune_aggregation_schedules_worker.rb index c660727869e..4e40feee702 100644 --- a/app/workers/namespaces/prune_aggregation_schedules_worker.rb +++ b/app/workers/namespaces/prune_aggregation_schedules_worker.rb @@ -6,9 +6,9 @@ module Namespaces include CronjobQueue # Worker to prune pending rows on Namespace::AggregationSchedule - # It's scheduled to run once a day at midnight. + # It's scheduled to run once a day at 1:05am. def perform - aggregation_schedules.each do |aggregation_schedule| + aggregation_schedules.find_each do |aggregation_schedule| aggregation_schedule.schedule_root_storage_statistics end end diff --git a/app/workers/namespaces/schedule_aggregation_worker.rb b/app/workers/namespaces/schedule_aggregation_worker.rb index 941149459c7..a4594b84b13 100644 --- a/app/workers/namespaces/schedule_aggregation_worker.rb +++ b/app/workers/namespaces/schedule_aggregation_worker.rb @@ -26,8 +26,8 @@ module Namespaces # calls UpdateProjectStatistics#schedule_namespace_statistics_worker. # # The migration and specs fails since NamespaceAggregationSchedule table - # does not exist at that point. Technical debt issue: - # https://gitlab.com/gitlab-org/gitlab-ce/issues/63775 + # does not exist at that point. + # https://gitlab.com/gitlab-org/gitlab-ce/issues/50712 def aggregation_schedules_table_exists? return true unless Rails.env.test? diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index 5c9ca97534d..bf187e9a282 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -442,7 +442,7 @@ Settings.cron_jobs['schedule_migrate_external_diffs_worker'] ||= Settingslogic.n Settings.cron_jobs['schedule_migrate_external_diffs_worker']['cron'] ||= '15 * * * *' Settings.cron_jobs['schedule_migrate_external_diffs_worker']['job_class'] = 'ScheduleMigrateExternalDiffsWorker' Settings.cron_jobs['namespaces_prune_aggregation_schedules_worker'] ||= Settingslogic.new({}) -Settings.cron_jobs['namespaces_prune_aggregation_schedules_worker']['cron'] ||= '0 0 * * *' +Settings.cron_jobs['namespaces_prune_aggregation_schedules_worker']['cron'] ||= '5 1 * * *' Settings.cron_jobs['namespaces_prune_aggregation_schedules_worker']['job_class'] = 'Namespaces::PruneAggregationSchedulesWorker' Gitlab.ee do diff --git a/spec/support/shared_examples/models/update_project_statistics_shared_examples.rb b/spec/support/shared_examples/models/update_project_statistics_shared_examples.rb index a49d04958e7..aad63982e7a 100644 --- a/spec/support/shared_examples/models/update_project_statistics_shared_examples.rb +++ b/spec/support/shared_examples/models/update_project_statistics_shared_examples.rb @@ -74,6 +74,20 @@ shared_examples_for 'UpdateProjectStatistics' do subject.save! end + it 'avoids N + 1 queries' do + subject.write_attribute(statistic_attribute, read_attribute + delta) + + control_count = ActiveRecord::QueryRecorder.new do + subject.save! + end + + subject.write_attribute(statistic_attribute, read_attribute + delta) + + expect do + subject.save! + end.not_to exceed_query_limit(control_count) + end + context 'when the feature flag is disabled for the namespace' do it 'does not schedule a namespace statistics worker' do namespace = subject.project.root_ancestor |