Commit 9f243c48 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '214749-backfill-projects-in-elasticsearch-when-moved-between-groups' into 'master'

Backfill projects in Elasticsearch when transferred

See merge request gitlab-org/gitlab!51717
parents ee96f8b7 96e5689a
...@@ -31,20 +31,21 @@ module EE ...@@ -31,20 +31,21 @@ module EE
end end
def update_elasticsearch_hooks(updated_project_ids) def update_elasticsearch_hooks(updated_project_ids)
project_ids = updated_project_ids
# Handle when group is moved to a new group. There is no way to know # Handle when group is moved to a new group. There is no way to know
# whether the group was using Elasticsearch before the transfer, so the ES cache is invalidated # whether the group was using Elasticsearch before the transfer. If Elasticsearch limit indexing is
# for each associated project. Otherwise, it is assumed all projects are indexed # enabled, each associated project has the ES cache is invalidated all associated data is indexed.
# and only those with visibility changes have their ES cache entry invalidated # If Elasticsearch limit indexing is disabled, all projects are indexed and only those with visibility
# changes have their ES cache entry invalidated.
if ::Gitlab::CurrentSettings.elasticsearch_limit_indexing? if ::Gitlab::CurrentSettings.elasticsearch_limit_indexing?
project_ids = group.all_projects.select(:id) ::Project.id_in(group.all_projects.select(:id)).find_each do |project|
end project.invalidate_elasticsearch_indexes_cache!
::Project.id_in(project_ids).find_each do |project| ::Elastic::ProcessInitialBookkeepingService.backfill_projects!(project) if project.maintaining_elasticsearch?
project.invalidate_elasticsearch_indexes_cache! end
else
project.maintain_elasticsearch_update(updated_attributes: [:visibility_level]) if project.maintaining_elasticsearch? ::Project.id_in(updated_project_ids).find_each do |project|
project.maintain_elasticsearch_update(updated_attributes: [:visibility_level]) if project.maintaining_elasticsearch?
end
end end
end end
end end
......
...@@ -42,7 +42,7 @@ module EE ...@@ -42,7 +42,7 @@ module EE
if old_namespace.use_elasticsearch? != new_namespace.use_elasticsearch? if old_namespace.use_elasticsearch? != new_namespace.use_elasticsearch?
project.invalidate_elasticsearch_indexes_cache! project.invalidate_elasticsearch_indexes_cache!
project.maintain_elasticsearch_update(updated_attributes: [:visibility_level]) if project.maintaining_elasticsearch? ::Elastic::ProcessInitialBookkeepingService.backfill_projects!(project) if project.maintaining_elasticsearch?
end end
end end
end end
......
---
title: Backfill projects in Elasticsearch when moved between groups
merge_request: 51717
author:
type: fixed
...@@ -14,7 +14,7 @@ RSpec.describe Groups::TransferService, '#execute' do ...@@ -14,7 +14,7 @@ RSpec.describe Groups::TransferService, '#execute' do
new_group&.add_owner(user) new_group&.add_owner(user)
end end
describe 'elasticsearch indexing', :elastic, :aggregate_failures do describe 'elasticsearch indexing', :aggregate_failures do
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
end end
...@@ -24,16 +24,15 @@ RSpec.describe Groups::TransferService, '#execute' do ...@@ -24,16 +24,15 @@ RSpec.describe Groups::TransferService, '#execute' do
stub_ee_application_setting(elasticsearch_limit_indexing: true) stub_ee_application_setting(elasticsearch_limit_indexing: true)
end end
let(:project) { create(:project, :repository, :public, namespace: group) }
context 'when moving from a non-indexed namespace to an indexed namespace' do context 'when moving from a non-indexed namespace to an indexed namespace' do
before do before do
create(:elasticsearch_indexed_namespace, namespace: new_group) create(:elasticsearch_indexed_namespace, namespace: new_group)
end end
it 'invalidates the cache and indexes the project and associated issues' do it 'invalidates the cache and indexes the project and all associated data' do
expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project) expect(project).not_to receive(:maintain_elasticsearch_update)
expect(ElasticAssociationIndexerWorker).to receive(:perform_async).with('Project', project.id, ['issues']) expect(project).not_to receive(:maintain_elasticsearch_destroy)
expect(::Elastic::ProcessInitialBookkeepingService).to receive(:backfill_projects!).with(project)
expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache_for_project!).with(project.id).and_call_original expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache_for_project!).with(project.id).and_call_original
transfer_service.execute(new_group) transfer_service.execute(new_group)
...@@ -46,9 +45,10 @@ RSpec.describe Groups::TransferService, '#execute' do ...@@ -46,9 +45,10 @@ RSpec.describe Groups::TransferService, '#execute' do
create(:elasticsearch_indexed_namespace, namespace: new_group) create(:elasticsearch_indexed_namespace, namespace: new_group)
end end
it 'invalidates the cache and indexes the project and associated issues' do it 'invalidates the cache and indexes the project and associated issues only' do
expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project) expect(project).not_to receive(:maintain_elasticsearch_update)
expect(ElasticAssociationIndexerWorker).to receive(:perform_async).with('Project', project.id, ['issues']) expect(project).not_to receive(:maintain_elasticsearch_destroy)
expect(::Elastic::ProcessInitialBookkeepingService).to receive(:backfill_projects!).with(project)
expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache_for_project!).with(project.id).and_call_original expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache_for_project!).with(project.id).and_call_original
transfer_service.execute(new_group) transfer_service.execute(new_group)
...@@ -60,11 +60,12 @@ RSpec.describe Groups::TransferService, '#execute' do ...@@ -60,11 +60,12 @@ RSpec.describe Groups::TransferService, '#execute' do
context 'when visibility changes' do context 'when visibility changes' do
let(:new_group) { create(:group, :private) } let(:new_group) { create(:group, :private) }
it 'reindexes projects and associated issues' do it 'does not invalidate the cache and reindexes projects and associated issues' do
project1 = create(:project, :repository, :public, namespace: group) project1 = create(:project, :repository, :public, namespace: group)
project2 = create(:project, :repository, :public, namespace: group) project2 = create(:project, :repository, :public, namespace: group)
project3 = create(:project, :repository, :private, namespace: group) project3 = create(:project, :repository, :private, namespace: group)
expect(::Gitlab::CurrentSettings).not_to receive(:invalidate_elasticsearch_indexes_cache_for_project!)
expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project1) expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project1)
expect(ElasticAssociationIndexerWorker).to receive(:perform_async).with('Project', project1.id, ['issues']) expect(ElasticAssociationIndexerWorker).to receive(:perform_async).with('Project', project1.id, ['issues'])
expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project2) expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project2)
......
...@@ -90,9 +90,9 @@ RSpec.describe Projects::TransferService do ...@@ -90,9 +90,9 @@ RSpec.describe Projects::TransferService do
create(:elasticsearch_indexed_namespace, namespace: group) create(:elasticsearch_indexed_namespace, namespace: group)
end end
it 'invalidates the cache and indexes the project and associated issues' do it 'invalidates the cache and indexes the project and all associated data' do
expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project) expect(Elastic::ProcessInitialBookkeepingService).to receive(:backfill_projects!).with(project)
expect(ElasticAssociationIndexerWorker).to receive(:perform_async).with('Project', project.id, ['issues']) expect(project).not_to receive(:maintain_elasticsearch_destroy)
expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache_for_project!).with(project.id).and_call_original expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache_for_project!).with(project.id).and_call_original
subject.execute(group) subject.execute(group)
...@@ -105,9 +105,9 @@ RSpec.describe Projects::TransferService do ...@@ -105,9 +105,9 @@ RSpec.describe Projects::TransferService do
create(:elasticsearch_indexed_namespace, namespace: project.namespace) create(:elasticsearch_indexed_namespace, namespace: project.namespace)
end end
it 'does not invalidate the cache and reindexes the project only' do it 'does not invalidate the cache does not index or delete anything' do
expect(Elastic::ProcessBookkeepingService).to receive(:track!).with(project) expect(Elastic::ProcessInitialBookkeepingService).not_to receive(:backfill_projects!).with(project)
expect(ElasticAssociationIndexerWorker).not_to receive(:perform_async) expect(project).not_to receive(:maintain_elasticsearch_destroy)
expect(::Gitlab::CurrentSettings).not_to receive(:invalidate_elasticsearch_indexes_cache_for_project!) expect(::Gitlab::CurrentSettings).not_to receive(:invalidate_elasticsearch_indexes_cache_for_project!)
subject.execute(group) subject.execute(group)
......
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