Skip to content

Fix data to fix broken group delete with moved projects adherences

What does this MR do and why?

This queueing worker is being added to trigger the RefreshWorker to repair broken adherence rows. The offending rows are related to projects that have been moved while there was an active bug related to not updating the namespace_id when moved. The code has since been fixed to update this field, but during the delay between original deploy and fixed deploy there broken rows were created. Initially this was not deemed critical since an edit of the adherences would fix them. The wrong namespace_id however lead to Group deletion being impossible because of the mismatched namespace_id not triggering correct dependent row deletion in adherences.

  • Guarded by feature flag. Defines ff_compliance_repair_adherences to control execution and be able to shut down worker when no longer needed.
  • Worker adds logging context with group_id, when we no longer see any of these added we can assume the worker is no longer finding broken adherences and it can be turned off and then removed again.

References

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

  1. Create adherences for all groups and mess up the namespace IDs. Last output number should be >0. Rails console (bin/rails console):
   150.times do |count|
  	a = Projects::ComplianceStandards::Adherence.new
  	a.status = ::Enums::Projects::ComplianceStandards::Adherence.status.to_a.sample.first
  	a.check_name =  ::Enums::Projects::ComplianceStandards::Adherence.check_name.to_a.sample.first
  	a.standard = ::Enums::Projects::ComplianceStandards::Adherence.standard.to_a.sample.first
  
  	g = Group.all.sample
  	
  	a.namespace = Namespace.find(g.id)
  	a.project = g.projects.all.sample
  	a.save
  	
  	rescue Exception => e
  		puts e.message
      puts a.errors
  	next
  end

  Projects::ComplianceStandards::Adherence.find_each do |a|
    a.update_columns namespace_id: Namespace.all.pluck(:id).sample
   	rescue Exception => e
  		puts e.message
      puts a.errors
  	next
  end

  puts Group.connection.execute(%q{SELECT DISTINCT "project_compliance_standards_adherence"."id" FROM "project_compliance_standards_adherence" INNER JOIN "projects" ON "projects"."id" = "project_compliance_standards_adherence"."project_id" WHERE (project_compliance_standards_adherence.namespace_id != projects.namespace_id)}).count
  1. Visit http://127.0.0.1:3000/admin/sidekiq/cron
  2. Find 'queue_refresh_of_broken_adherence_groups_worker' and click "Enqueue Now", confirm
  3. Rails console:
      Group.connection.execute(%q{SELECT DISTINCT "project_compliance_standards_adherence"."id" FROM "project_compliance_standards_adherence" INNER JOIN "projects" ON "projects"."id" = "project_compliance_standards_adherence"."project_id" WHERE (project_compliance_standards_adherence.namespace_id != projects.namespace_id)}).count
    should equal 0

Database

Query timing for per batch selecting the group IDs that need to be fed to RefreshWorker to repair adherences

Details: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/36109/commands/111314

-- Adherences with mismatched namespaces (project was moved before fix)

SELECT DISTINCT "projects"."namespace_id" 
FROM "project_compliance_standards_adherence" 
INNER JOIN "projects" ON "projects"."id" = "project_compliance_standards_adherence"."project_id" 
WHERE (project_compliance_standards_adherence.namespace_id != projects.namespace_id) AND "project_compliance_standards_adherence"."id" >= 1 
  AND "project_compliance_standards_adherence"."id" < 101
Time: 6.828 ms
  - planning: 6.097 ms
  - execution: 0.731 ms
    - I/O read: 0.000 ms
    - I/O write: 0.000 ms

Shared buffers:
  - hits: 611 (~4.80 MiB) from the buffer pool
  - reads: 0 from the OS file cache, including disk I/O
  - dirtied: 0
  - writes: 0
Edited by SAM FIGUEROA

Merge request reports

Loading