Commit 6ff1d24f authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Merge branch 'optimize_elasticsearch_indexes_project_query' into 'master'

Optimize use_elasticsearch? call

See merge request gitlab-org/gitlab!30612
parents 1bee9389 22a9286d
......@@ -135,10 +135,10 @@ module EE
return false unless elasticsearch_indexing?
return true unless elasticsearch_limit_indexing?
return elasticsearch_limited_projects.exists?(project.id) unless ::Feature.enabled?(:elasticsearch_indexes_project_cache, default_enabled: true)
return optimized_elasticsearch_indexes_project?(project) unless ::Feature.enabled?(:elasticsearch_indexes_project_cache, default_enabled: true)
::Gitlab::Elastic::ElasticsearchEnabledCache.fetch(:project, project.id) do
elasticsearch_limited_projects.exists?(project.id)
optimized_elasticsearch_indexes_project?(project)
end
end
......@@ -290,6 +290,26 @@ module EE
private
def optimized_elasticsearch_indexes_project?(project)
if ::Feature.enabled?(:optimized_elasticsearch_indexes_project, default_enabled: true)
indexed_namespaces = ::Gitlab::ObjectHierarchy
.new(::Namespace.where(id: project.namespace_id))
.base_and_ancestors
.joins(:elasticsearch_indexed_namespace)
indexed_namespaces = ::Project.where('EXISTS (?)', indexed_namespaces)
indexed_projects = ::Project.where('EXISTS (?)', ElasticsearchIndexedProject.where(project_id: project.id))
::Project
.from("(SELECT) as projects") # SELECT from "nothing" since the EXISTS queries have all the conditions.
.merge(indexed_namespaces.or(indexed_projects))
.exists?
else
# old behavior
elasticsearch_limited_projects.exists?(project.id)
end
end
def update_personal_access_tokens_lifetime
return unless max_personal_access_token_lifetime.present? && License.feature_available?(:personal_access_token_expiration_policy)
......
......@@ -29,6 +29,7 @@ module EE
has_one :namespace_statistics
has_one :gitlab_subscription, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_one :elasticsearch_indexed_namespace
accepts_nested_attributes_for :gitlab_subscription
......
......@@ -289,6 +289,53 @@ describe ApplicationSetting do
expect(setting.elasticsearch_limited_namespaces(true)).to match_array(
[namespaces.last, child_namespace])
end
describe '#elasticsearch_indexes_project?' do
shared_examples 'examples for #elasticsearch_indexes_project?' do
context 'when project is in a subgroup' do
let(:root_group) { create(:group) }
let(:subgroup) { create(:group, parent: root_group) }
let(:project) { create(:project, group: subgroup) }
before do
create(:elasticsearch_indexed_namespace, namespace: root_group)
end
it 'allows project to be indexed' do
expect(setting.elasticsearch_indexes_project?(project)).to be(true)
end
end
context 'when project is in a namespace' do
let(:namespace) { create(:namespace) }
let(:project) { create(:project, namespace: namespace) }
before do
create(:elasticsearch_indexed_namespace, namespace: namespace)
end
it 'allows project to be indexed' do
expect(setting.elasticsearch_indexes_project?(project)).to be(true)
end
end
end
context 'when optimized_elasticsearch_indexes_project feature flag is on' do
before do
stub_feature_flags(optimized_elasticsearch_indexes_project: true)
end
include_examples 'examples for #elasticsearch_indexes_project?'
end
context 'when optimized_elasticsearch_indexes_project feature flag is off' do
before do
stub_feature_flags(optimized_elasticsearch_indexes_project: false)
end
include_examples 'examples for #elasticsearch_indexes_project?'
end
end
end
context 'projects' do
......
......@@ -14,6 +14,7 @@ describe Namespace do
it { is_expected.to have_one(:namespace_statistics) }
it { is_expected.to have_one(:gitlab_subscription).dependent(:destroy) }
it { is_expected.to have_one(:elasticsearch_indexed_namespace) }
it { is_expected.to delegate_method(:extra_shared_runners_minutes).to(:namespace_statistics) }
it { is_expected.to delegate_method(:shared_runners_minutes).to(:namespace_statistics) }
......
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