Commit 6daa44cd authored by Valery Sizov's avatar Valery Sizov

Remove orphaned project registries

Some registries can be orphaned because of lost Sidekiq job for example
parent 75ff95ed
......@@ -55,3 +55,20 @@ sudo gitlab-rake geo:git:housekeeping:gc
```bash
sudo -u git -H bundle exec rake geo:git:housekeeping:gc RAILS_ENV=production
```
## Remove orphaned project registries
Under certain conditions your project registry can contain obsolete records, you
can remove them using the rake task `geo:run_orphaned_project_registry_cleaner`:
**Omnibus Installation**
```
sudo gitlab-rake geo:run_orphaned_project_registry_cleaner
```
**Source Installation**
```bash
sudo -u git -H bundle exec rake geo:run_orphaned_project_registry_cleaner RAILS_ENV=production
```
---
title: 'Geo: Add orphaned project registry cleaner'
merge_request: 15021
author:
type: changed
......@@ -166,6 +166,46 @@ namespace :geo do
end
end
desc 'Run orphaned project registry cleaner'
task run_orphaned_project_registry_cleaner: :environment do
abort GEO_LICENSE_ERROR_TEXT unless Gitlab::Geo.license_allows?
unless Gitlab::Geo.secondary?
abort 'This is not a secondary node'
end
from_project_id = ENV['FROM_PROJECT_ID'] || Geo::ProjectRegistry.minimum(:project_id)
to_project_id = ENV['TO_PROJECT_ID'] || Geo::ProjectRegistry.maximum(:project_id)
if from_project_id > to_project_id
abort 'FROM_PROJECT_ID can not be greater than TO_PROJECT_ID'
end
batch_size = 1000
total_count = 0
current_max_id = 0
until current_max_id >= to_project_id
current_max_id = [from_project_id + batch_size, to_project_id + 1].min
project_ids = Project
.where('id >= ? AND id < ?', from_project_id, current_max_id)
.pluck_primary_key
orphaned_registries = Geo::ProjectRegistry
.where('project_id NOT IN(?)', project_ids)
.where('project_id >= ? AND project_id < ?', from_project_id, current_max_id)
count = orphaned_registries.delete_all
total_count += count
puts "Checked project ids from #{from_project_id} to #{current_max_id} registries. Removed #{count} orphaned registries"
from_project_id = current_max_id
end
puts "Orphaned registries removed(total): #{total_count}"
end
desc 'Make this node the Geo primary'
task set_primary_node: :environment do
abort GEO_LICENSE_ERROR_TEXT unless Gitlab::Geo.license_allows?
......
......@@ -347,4 +347,51 @@ describe 'geo rake tasks', :geo do
end
end
end
describe 'geo:run_orphaned_project_registry_cleaner' do
let!(:current_node) { create(:geo_node) }
before do
stub_current_geo_node(current_node)
create(:geo_project_registry)
create(:geo_project_registry)
@orphaned = create(:geo_project_registry)
@orphaned.project.delete
@orphaned1 = create(:geo_project_registry)
@orphaned1.project.delete
create(:geo_project_registry)
end
it 'removes orphaned registries' do
run_rake_task('geo:run_orphaned_project_registry_cleaner')
expect(Geo::ProjectRegistry.count).to be 3
expect(Geo::ProjectRegistry.find_by_id(@orphaned.id)).to be nil
end
it 'removes orphaned registries taking into account TO_PROJECT_ID' do
allow(ENV).to receive(:[]).with('FROM_PROJECT_ID').and_return(nil)
allow(ENV).to receive(:[]).with('TO_PROJECT_ID').and_return(@orphaned.project_id)
run_rake_task('geo:run_orphaned_project_registry_cleaner')
expect(Geo::ProjectRegistry.count).to be 4
expect(Geo::ProjectRegistry.find_by_id(@orphaned.id)).to be nil
expect(Geo::ProjectRegistry.find_by_id(@orphaned1.id)).not_to be nil
end
it 'removes orphaned registries taking into account FROM_PROJECT_ID' do
allow(ENV).to receive(:[]).with('FROM_PROJECT_ID').and_return(@orphaned1.project_id)
allow(ENV).to receive(:[]).with('TO_PROJECT_ID').and_return(nil)
run_rake_task('geo:run_orphaned_project_registry_cleaner')
expect(Geo::ProjectRegistry.count).to be 4
expect(Geo::ProjectRegistry.find_by_id(@orphaned.id)).not_to be nil
expect(Geo::ProjectRegistry.find_by_id(@orphaned1.id)).to be nil
end
end
end
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