Skip to content

Filter projects by ID for the registry repair worker

Adie (she/her) requested to merge 390842-scope-repair-worker-id into master

What does this MR do and why?

In this MR, we aim to make the repair worker query smaller, faster, and fixed by scoping the projects to be analyzed by their respective IDs.

Previously, we only do a left join from projects to container_registry_data_repair_details. But now as the container_registry_data_repair_details table gets filled up with rows, our query is getting bigger and slower. In this MR, we aim to scope the data repair analysis based on ID so:

  1. Our query can run faster with a smaller scope and can use the existing index on id
  2. We can track progress of our data repair detail worker per ID range

In this MR, we add the ID scopes as part of the application setting so that when we want and need to update the scopes, we can easily do so via the Rails console in a change request.

Database Analysis:

Before

We do two queries, one to get the remaining work count and another one to fetch the next project:

Project.pending_data_repair_analysis.limit(max_running_jobs + 1).count

Analysis: https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/20063/commands/67255

Project.pending_data_repair_analysis.limit(max_running_jobs * 2).sample

Analysis: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/20063/commands/67671

After

We will only do one query and memoize the value so it can be reused.

Project
.pending_data_repair_analysis_and_with_id_between(START_ID, END_ID)
.limit(max_running_jobs * 2)

Analysis: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/21263/commands/69336

Migration Output

% bin/rails db:migrate RAILS_ENV=development
main: == [advisory_lock_connection] object_id: 225200, pg_backend_pid: 16290
main: == 20230828115339 AddContainerRegistryRepairIdStartAndEndToApplicationSettings: migrating
main: -- column_exists?(:application_settings, :container_registry_data_repair_detail_worker_id_start)
main:    -> 0.2600s
main: -- add_column(:application_settings, :container_registry_data_repair_detail_worker_id_start, :integer, {:default=>0, :null=>false})
main:    -> 0.0038s
main: -- column_exists?(:application_settings, :container_registry_data_repair_detail_worker_id_end)
main:    -> 0.1384s
main: -- add_column(:application_settings, :container_registry_data_repair_detail_worker_id_end, :integer, {:default=>1000000, :null=>false})
main:    -> 0.0013s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE application_settings\nADD CONSTRAINT app_settings_registry_repair_worker_positive_id_start\nCHECK ( container_registry_data_repair_detail_worker_id_start >= 0 )\nNOT VALID;\n")
main:    -> 0.0019s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0003s
main: -- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT app_settings_registry_repair_worker_positive_id_start;")
main:    -> 0.0008s
main: -- execute("RESET statement_timeout")
main:    -> 0.0002s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE application_settings\nADD CONSTRAINT app_settings_registry_repair_worker_positive_id_end\nCHECK ( container_registry_data_repair_detail_worker_id_end >= 0 )\nNOT VALID;\n")
main:    -> 0.0009s
main: -- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT app_settings_registry_repair_worker_positive_id_end;")
main:    -> 0.0006s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE application_settings\nADD CONSTRAINT app_settings_registry_repair_worker_end_id_gteq_start_id\nCHECK ( container_registry_data_repair_detail_worker_id_end >= container_registry_data_repair_detail_worker_id_start )\nNOT VALID;\n")
main:    -> 0.0009s
main: -- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT app_settings_registry_repair_worker_end_id_gteq_start_id;")
main:    -> 0.0007s
main: == 20230828115339 AddContainerRegistryRepairIdStartAndEndToApplicationSettings: migrated (0.4334s)

main: == [advisory_lock_connection] object_id: 225200, pg_backend_pid: 16290

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #390842

Edited by Adie (she/her)

Merge request reports