Commit c6aebae7 authored by Valery Sizov's avatar Valery Sizov

Merge branch...

Merge branch '932-es-elastic-workers-should-check-settings-each-time-when-they-are-running' into 'master'

[ES] Elastic workers should check settings each time when they are running

Closes #932

See merge request !693
parents 2345cc87 b4e76e64
...@@ -4,6 +4,7 @@ v 8.12.0 (Unreleased) ...@@ -4,6 +4,7 @@ v 8.12.0 (Unreleased)
v 8.11.3 (Unreleased) v 8.11.3 (Unreleased)
- [ES] Add logging to indexer - [ES] Add logging to indexer
- Set the correct `GL_PROTOCOL` when rebasing !691 - Set the correct `GL_PROTOCOL` when rebasing !691
- [ES] Elasticsearch workers checks ES settings before running
v 8.11.2 v 8.11.2
- Additional documentation on protected branches for EE - Additional documentation on protected branches for EE
......
class ElasticCommitIndexerWorker class ElasticCommitIndexerWorker
include Sidekiq::Worker include Sidekiq::Worker
include Gitlab::CurrentSettings
sidekiq_options queue: :elasticsearch, retry: 2 sidekiq_options queue: :elasticsearch, retry: 2
def perform(project_id, oldrev = nil, newrev = nil) def perform(project_id, oldrev = nil, newrev = nil)
return true unless current_application_settings.elasticsearch_indexing?
project = Project.find(project_id) project = Project.find(project_id)
repository = project.repository repository = project.repository
......
class ElasticIndexerWorker class ElasticIndexerWorker
include Sidekiq::Worker include Sidekiq::Worker
include Elasticsearch::Model::Client::ClassMethods include Elasticsearch::Model::Client::ClassMethods
include Gitlab::CurrentSettings
sidekiq_options queue: :elasticsearch, retry: 2 sidekiq_options queue: :elasticsearch, retry: 2
ISSUE_TRACKED_FIELDS = %w(assignee_id author_id confidential) ISSUE_TRACKED_FIELDS = %w(assignee_id author_id confidential)
def perform(operation, class_name, record_id, options = {}) def perform(operation, class_name, record_id, options = {})
return true unless current_application_settings.elasticsearch_indexing?
klass = class_name.constantize klass = class_name.constantize
case operation.to_s case operation.to_s
......
...@@ -6,6 +6,10 @@ describe ElasticCommitIndexerWorker do ...@@ -6,6 +6,10 @@ describe ElasticCommitIndexerWorker do
subject { described_class.new } subject { described_class.new }
describe '#perform' do describe '#perform' do
before do
stub_application_setting(elasticsearch_indexing: true)
end
it 'runs indexer' do it 'runs indexer' do
expect_any_instance_of(Gitlab::Elastic::Indexer).to receive(:run) expect_any_instance_of(Gitlab::Elastic::Indexer).to receive(:run)
subject.perform(project.id, '0000', '0000') subject.perform(project.id, '0000', '0000')
...@@ -27,5 +31,13 @@ describe ElasticCommitIndexerWorker do ...@@ -27,5 +31,13 @@ describe ElasticCommitIndexerWorker do
expect(subject.perform(project.id)).to be_truthy expect(subject.perform(project.id)).to be_truthy
end end
it 'returns true if ES disabled' do
stub_application_setting(elasticsearch_indexing: false)
expect_any_instance_of(Gitlab::Elastic::Indexer).not_to receive(:run)
expect(subject.perform(1)).to be_truthy
end
end end
end end
...@@ -11,119 +11,171 @@ describe ElasticIndexerWorker, elastic: true do ...@@ -11,119 +11,171 @@ describe ElasticIndexerWorker, elastic: true do
) )
Gitlab::Elastic::Helper.create_empty_index Gitlab::Elastic::Helper.create_empty_index
stub_application_setting(elasticsearch_indexing: true)
end end
after do after do
Gitlab::Elastic::Helper.delete_index Gitlab::Elastic::Helper.delete_index
end end
Sidekiq::Testing.disable! do it 'returns true if ES disabled' do
describe 'Indexing new records' do stub_application_setting(elasticsearch_indexing: false)
it 'indexes a project' do
project = create :empty_project expect_any_instance_of(Elasticsearch::Model).not_to receive(:__elasticsearch__)
expect(subject.perform("index", "Milestone", 1)).to be_truthy
end
describe 'Indexing new records' do
it 'indexes a project' do
project = nil
expect do Sidekiq::Testing.disable! do
subject.perform("index", "Project", project.id) project = create :empty_project
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end end
it 'indexes an issue' do expect do
issue = create :issue subject.perform("index", "Project", project.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
it 'indexes an issue' do
issue = nil
expect do Sidekiq::Testing.disable! do
subject.perform("index", "Issue", issue.id) issue = create :issue
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end end
it 'indexes a note' do expect do
note = create :note subject.perform("index", "Issue", issue.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
it 'indexes a note' do
note = nil
expect do Sidekiq::Testing.disable! do
subject.perform("index", "Note", note.id) note = create :note
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end end
it 'indexes a milestone' do expect do
milestone = create :milestone subject.perform("index", "Note", note.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
expect do it 'indexes a milestone' do
subject.perform("index", "Milestone", milestone.id) milestone = nil
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1) Sidekiq::Testing.disable! do
milestone = create :milestone
end end
it 'indexes a merge request' do expect do
merge_request = create :merge_request subject.perform("index", "Milestone", milestone.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end
expect do it 'indexes a merge request' do
subject.perform("index", "MergeRequest", merge_request.id) merge_request = nil
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1) Sidekiq::Testing.disable! do
merge_request = create :merge_request
end end
expect do
subject.perform("index", "MergeRequest", merge_request.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').records.size }.by(1)
end end
end
describe 'Updating index' do
it 'updates a project' do
project = nil
describe 'Updating index' do Sidekiq::Testing.disable! do
it 'updates a project' do
project = create :empty_project project = create :empty_project
subject.perform("index", "Project", project.id) subject.perform("index", "Project", project.id)
project.update(name: "new") project.update(name: "new")
expect do
subject.perform("update", "Project", project.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end end
it 'updates an issue' do expect do
subject.perform("update", "Project", project.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates an issue' do
issue = nil
Sidekiq::Testing.disable! do
issue = create :issue issue = create :issue
subject.perform("index", "Issue", issue.id) subject.perform("index", "Issue", issue.id)
issue.update(title: "new") issue.update(title: "new")
expect do
subject.perform("update", "Issue", issue.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end end
it 'updates a note' do expect do
subject.perform("update", "Issue", issue.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates a note' do
note = nil
Sidekiq::Testing.disable! do
note = create :note note = create :note
subject.perform("index", "Note", note.id) subject.perform("index", "Note", note.id)
note.update(note: 'new') note.update(note: 'new')
expect do
subject.perform("update", "Note", note.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end end
it 'updates a milestone' do expect do
subject.perform("update", "Note", note.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates a milestone' do
milestone = nil
Sidekiq::Testing.disable! do
milestone = create :milestone milestone = create :milestone
subject.perform("index", "Milestone", milestone.id) subject.perform("index", "Milestone", milestone.id)
milestone.update(title: 'new') milestone.update(title: 'new')
expect do
subject.perform("update", "Milestone", milestone.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end end
it 'updates a merge request' do expect do
subject.perform("update", "Milestone", milestone.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end
it 'updates a merge request' do
merge_request = nil
Sidekiq::Testing.disable! do
merge_request = create :merge_request merge_request = create :merge_request
subject.perform("index", "MergeRequest", merge_request.id) subject.perform("index", "MergeRequest", merge_request.id)
merge_request.update(title: 'new') merge_request.update(title: 'new')
expect do
subject.perform("index", "MergeRequest", merge_request.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end end
expect do
subject.perform("index", "MergeRequest", merge_request.id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('new').records.size }.by(1)
end end
end
describe 'Delete' do
it 'deletes a project with all nested objects' do
project, issue, milestone, note, merge_request = nil
describe 'Delete' do Sidekiq::Testing.disable! do
it 'deletes a project with all nested objects' do
project = create :project project = create :project
subject.perform("index", "Project", project.id) subject.perform("index", "Project", project.id)
...@@ -138,70 +190,86 @@ describe ElasticIndexerWorker, elastic: true do ...@@ -138,70 +190,86 @@ describe ElasticIndexerWorker, elastic: true do
merge_request = create :merge_request, target_project: project, source_project: project merge_request = create :merge_request, target_project: project, source_project: project
subject.perform("index", "MergeRequest", merge_request.id) subject.perform("index", "MergeRequest", merge_request.id)
end
ElasticCommitIndexerWorker.new.perform(project.id) ElasticCommitIndexerWorker.new.perform(project.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
## All database objects + data from repository. The absolute value does not matter ## All database objects + data from repository. The absolute value does not matter
expect(Elasticsearch::Model.search('*').total_count).to be > 40 expect(Elasticsearch::Model.search('*').total_count).to be > 40
subject.perform("delete", "Project", project.id) subject.perform("delete", "Project", project.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect(Elasticsearch::Model.search('*').total_count).to be(0) expect(Elasticsearch::Model.search('*').total_count).to be(0)
end end
it 'deletes an issue' do
issue, project_id = nil
it 'deletes an issue' do Sidekiq::Testing.disable! do
issue = create :issue issue = create :issue
subject.perform("index", "Issue", issue.id) subject.perform("index", "Issue", issue.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
project_id = issue.project_id
issue.destroy issue.destroy
expect do
subject.perform("delete", "Issue", issue.id, "project_id" => issue.project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end end
it 'deletes a note' do expect do
subject.perform("delete", "Issue", issue.id, "project_id" => project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end
it 'deletes a note' do
note, project_id = nil
Sidekiq::Testing.disable! do
note = create :note note = create :note
subject.perform("index", "Note", note.id) subject.perform("index", "Note", note.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
project_id = note.project_id
note.destroy note.destroy
expect do
subject.perform("delete", "Note", note.id, "project_id" => note.project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end end
it 'deletes a milestone' do expect do
subject.perform("delete", "Note", note.id, "project_id" => project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end
it 'deletes a milestone' do
milestone, project_id = nil
Sidekiq::Testing.disable! do
milestone = create :milestone milestone = create :milestone
subject.perform("index", "Milestone", milestone.id) subject.perform("index", "Milestone", milestone.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
project_id = milestone.project_id
milestone.destroy milestone.destroy
expect do
subject.perform("delete", "Milestone", milestone.id, "project_id" => milestone.project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end end
it 'deletes a merge request' do expect do
subject.perform("delete", "Milestone", milestone.id, "project_id" => project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end
it 'deletes a merge request' do
merge_request, project_id = nil
Sidekiq::Testing.disable! do
merge_request = create :merge_request merge_request = create :merge_request
subject.perform("index", "MergeRequest", merge_request.id) subject.perform("index", "MergeRequest", merge_request.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
project_id = merge_request.target_project_id
merge_request.destroy merge_request.destroy
expect do
subject.perform("delete", "MergeRequest", merge_request.id, "project_id" => merge_request.target_project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end end
expect do
subject.perform("delete", "MergeRequest", merge_request.id, "project_id" => project_id)
Gitlab::Elastic::Helper.refresh_index
end.to change{ Elasticsearch::Model.search('*').total_count }.by(-1)
end end
end 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