Commit 9da2b85e authored by Terri Chu's avatar Terri Chu Committed by Rémy Coutable

Prepare Elastic indices before context, and delete index by query

Add a new spec configuration that creates/deletes the
index only before and after each context. In between
execution, the Elasticsearch API DeleteByQuery is used
to clear out any data in the index. This is a performance
improvement for the specs that use it.
parent 0ee2d125
......@@ -36,7 +36,7 @@ RSpec.describe Search::GlobalService do
it_behaves_like 'search query applies joins based on migrations shared examples', :add_new_data_to_merge_requests_documents
end
context 'visibility', :elastic, :sidekiq_inline do
context 'visibility', :elastic_delete_by_query, :sidekiq_inline do
include_context 'ProjectPolicyTable context'
shared_examples 'search respects visibility' do
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Search::GroupService, :elastic do
RSpec.describe Search::GroupService do
include SearchResultHelpers
include ProjectHelpers
using RSpec::Parameterized::TableSyntax
......@@ -50,7 +50,7 @@ RSpec.describe Search::GroupService, :elastic do
ensure_elasticsearch_index!
end
context 'finding projects by name', :sidekiq_might_not_need_inline do
context 'finding projects by name', :elastic, :sidekiq_might_not_need_inline do
subject { results.objects('projects') }
context 'in parent group' do
......@@ -67,7 +67,7 @@ RSpec.describe Search::GroupService, :elastic do
end
end
context 'notes search' do
context 'notes search', :elastic do
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, namespace: group) }
......@@ -76,7 +76,7 @@ RSpec.describe Search::GroupService, :elastic do
it_behaves_like 'search query applies joins based on migrations shared examples', :add_permissions_data_to_notes_documents
end
context 'visibility', :sidekiq_inline do
context 'visibility', :elastic_delete_by_query, :sidekiq_inline do
include_context 'ProjectPolicyTable context'
shared_examples 'search respects visibility' do
......@@ -331,7 +331,7 @@ RSpec.describe Search::GroupService, :elastic do
end
end
context 'sorting' do
context 'sorting', :elastic do
context 'issues' do
let(:scope) { 'issues' }
let_it_be(:group) { create(:group) }
......
......@@ -42,7 +42,7 @@ RSpec.describe Search::ProjectService do
it_behaves_like 'search query applies joins based on migrations shared examples', :add_permissions_data_to_notes_documents
end
context 'visibility', :elastic, :sidekiq_inline do
context 'visibility', :elastic_delete_by_query, :sidekiq_inline do
include_context 'ProjectPolicyTable context'
shared_examples 'search respects visibility' do
......@@ -51,6 +51,7 @@ RSpec.describe Search::ProjectService do
projects.each do |project|
update_feature_access_level(project, feature_access_level)
end
ensure_elasticsearch_index!
expect_search_results(user, scope, expected_count: expected_count) do |user|
......@@ -260,6 +261,7 @@ RSpec.describe Search::ProjectService do
'issues_access_level' => issues_access_level,
'merge_requests_access_level' => merge_requests_access_level
)
ensure_elasticsearch_index!
expect_search_results(user, 'milestones', expected_count: expected_count) do |user|
......
......@@ -14,14 +14,14 @@ RSpec.describe Search::SnippetService do
let(:service) { described_class.new(user, params) }
end
describe '#execute', :elastic, :sidekiq_inline do
describe '#execute' do
include_context 'ProjectPolicyTable context'
before do
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
end
context 'visibility' do
context 'visibility', :elastic_delete_by_query, :sidekiq_inline do
include_context 'ProjectPolicyTable context'
include ProjectHelpers
......
# frozen_string_literal: true
module Elastic
class TestHelpers
include ElasticsearchHelpers
def helper
@helper ||= Gitlab::Elastic::Helper.default
end
def indices
@indices ||= [helper.target_name, helper.migrations_index_name] + helper.standalone_indices_proxies.map(&:index_name)
end
def setup
clear_tracking!
delete_indices!
helper.create_empty_index(options: { settings: { number_of_replicas: 0 } })
helper.create_migrations_index
::Elastic::DataMigrationService.mark_all_as_completed!
helper.create_standalone_indices
refresh_elasticsearch_index!
end
def teardown
delete_indices!
clear_tracking!
end
def clear_tracking!
Elastic::ProcessInitialBookkeepingService.clear_tracking!
Elastic::ProcessBookkeepingService.clear_tracking!
end
def refresh_elasticsearch_index!
refresh_index!
end
def delete_indices!
indices.each do |index_name|
helper.delete_index(index_name: index_name)
end
end
def delete_all_data_from_index!
helper.client.delete_by_query(
{
index: indices,
body: { query: { match_all: {} } },
slices: 5,
conflicts: 'proceed'
}
)
end
end
end
RSpec.configure do |config|
config.around(:each, :elastic) do |example|
helper = Gitlab::Elastic::Helper.default
helper = Elastic::TestHelpers.new
helper.setup
Elastic::ProcessBookkeepingService.clear_tracking!
example.run
# Delete all test indices
indices = [helper.target_name, helper.migrations_index_name] + helper.standalone_indices_proxies.map(&:index_name)
indices.each do |index_name|
helper.delete_index(index_name: index_name)
end
helper.teardown
end
config.before(:context, :elastic_delete_by_query) do
Elastic::TestHelpers.new.setup
end
helper.create_empty_index(options: { settings: { number_of_replicas: 0 } })
helper.create_migrations_index
::Elastic::DataMigrationService.mark_all_as_completed!
helper.create_standalone_indices
refresh_index!
config.after(:context, :elastic_delete_by_query) do
Elastic::TestHelpers.new.teardown
end
config.around(:each, :elastic_delete_by_query) do |example|
helper = Elastic::TestHelpers.new
helper.refresh_elasticsearch_index!
example.run
indices.each do |index_name|
helper.delete_index(index_name: index_name)
end
Elastic::ProcessBookkeepingService.clear_tracking!
helper.delete_all_data_from_index!
end
config.include ElasticsearchHelpers, :elastic
config.include ElasticsearchHelpers, :elastic_delete_by_query
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