Commit 0cd390fe authored by Dmitry Gruzd's avatar Dmitry Gruzd

Add cache for elasticsearch_indexes_namespace?

When doing a group scoped search we are spending ~800ms just checking if
ES is enabled for the namespace. This change introduces caching
for elasticsearch_indexes_namespace?
parent 1a9d624a
......@@ -6,11 +6,11 @@ module ElasticsearchIndexedContainer
included do
after_commit :index, on: :create
after_commit :delete_from_index, on: :destroy
after_commit :invalidate_elasticsearch_indexes_project_cache!, on: [:create, :destroy]
after_commit :invalidate_elasticsearch_indexes_cache!, on: [:create, :destroy]
end
def invalidate_elasticsearch_indexes_project_cache!
self.class.invalidate_elasticsearch_indexes_project_cache!
def invalidate_elasticsearch_indexes_cache!
self.class.invalidate_elasticsearch_indexes_cache!
end
class_methods do
......@@ -24,8 +24,8 @@ module ElasticsearchIndexedContainer
end
end
def invalidate_elasticsearch_indexes_project_cache!
::Gitlab::CurrentSettings.invalidate_elasticsearch_indexes_project_cache!
def invalidate_elasticsearch_indexes_cache!
::Gitlab::CurrentSettings.invalidate_elasticsearch_indexes_cache!
end
end
end
......@@ -152,15 +152,20 @@ module EE
end
end
def invalidate_elasticsearch_indexes_project_cache!
::Gitlab::Elastic::ElasticsearchEnabledCache.delete(:project)
end
def elasticsearch_indexes_namespace?(namespace)
return false unless elasticsearch_indexing?
return true unless elasticsearch_limit_indexing?
elasticsearch_limited_namespaces.exists?(namespace.id)
elasticsearch_limited_namespaces.exists?(namespace.id) unless ::Feature.enabled?(:elasticsearch_indexes_namespace_cache)
::Gitlab::Elastic::ElasticsearchEnabledCache.fetch(:namespace, namespace.id) do
elasticsearch_limited_namespaces.exists?(namespace.id)
end
end
def invalidate_elasticsearch_indexes_cache!
::Gitlab::Elastic::ElasticsearchEnabledCache.delete(:project)
::Gitlab::Elastic::ElasticsearchEnabledCache.delete(:namespace)
end
def elasticsearch_limited_projects(ignore_namespaces = false)
......
......@@ -38,7 +38,7 @@ class ElasticsearchIndexedNamespace < ApplicationRecord
end
Gitlab::Database.bulk_insert(table_name, insert_rows) # rubocop:disable Gitlab/BulkInsert
invalidate_elasticsearch_indexes_project_cache!
invalidate_elasticsearch_indexes_cache!
jobs = batch_ids.map { |id| [id, :index] }
......@@ -56,7 +56,7 @@ class ElasticsearchIndexedNamespace < ApplicationRecord
ids.in_groups_of(BATCH_OPERATION_SIZE, false) do |batch_ids|
where(namespace_id: batch_ids).delete_all
invalidate_elasticsearch_indexes_project_cache!
invalidate_elasticsearch_indexes_cache!
jobs = batch_ids.map { |id| [id, :delete] }
......
---
title: Add cache for elasticsearch_indexes_namespace check
merge_request: 41274
author:
type: performance
---
name: elasticsearch_indexes_namespace_cache
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/41274
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/244847
group: group::global search
type: development
default_enabled: false
......@@ -397,11 +397,12 @@ RSpec.describe ApplicationSetting do
end
end
describe '#invalidate_elasticsearch_indexes_project_cache!' do
it 'deletes the ElasticsearchEnabledCache for projects' do
describe '#invalidate_elasticsearch_indexes_cache' do
it 'deletes the ElasticsearchEnabledCache for projects and namespaces' do
expect(::Gitlab::Elastic::ElasticsearchEnabledCache).to receive(:delete).with(:project)
expect(::Gitlab::Elastic::ElasticsearchEnabledCache).to receive(:delete).with(:namespace)
setting.invalidate_elasticsearch_indexes_project_cache!
setting.invalidate_elasticsearch_indexes_cache!
end
end
......
......@@ -56,7 +56,7 @@ RSpec.describe ElasticsearchIndexedNamespace do
describe '.index_first_n_namespaces_of_plan' do
it 'creates records, scoped by plan and ordered by namespace id' do
expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_project_cache!).and_call_original.exactly(3).times
expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache!).and_call_original.exactly(3).times
ids = namespaces.map(&:id)
......@@ -84,7 +84,7 @@ RSpec.describe ElasticsearchIndexedNamespace do
end
it 'creates records, scoped by plan and ordered by namespace id' do
expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_project_cache!).and_call_original.exactly(3).times
expect(::Gitlab::CurrentSettings).to receive(:invalidate_elasticsearch_indexes_cache!).and_call_original.exactly(3).times
ids = namespaces.map(&:id)
......
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