Commit 908a4c44 authored by Nick Thomas's avatar Nick Thomas

Merge branch '11269-index-wikis-using-new-go-indexer' into 'master'

Use elasticsearch go indexer for wikis

Closes #11269

See merge request gitlab-org/gitlab-ee!13743
parents 5b65beb7 44a25f99
...@@ -23,10 +23,18 @@ module Elastic ...@@ -23,10 +23,18 @@ module Elastic
self.__elasticsearch__.client self.__elasticsearch__.client
end end
def index_wiki_blobs
if ::Gitlab::CurrentSettings.elasticsearch_experimental_indexer?
ElasticCommitIndexerWorker.perform_async(project.id, nil, nil, true)
else
project.wiki.index_blobs
end
end
def self.import def self.import
Project.with_wiki_enabled.find_each do |project| Project.with_wiki_enabled.find_each do |project|
if project.use_elasticsearch? && !project.wiki.empty? if project.use_elasticsearch? && !project.wiki.empty?
project.wiki.index_blobs project.wiki.index_wiki_blobs
end end
end end
end end
......
...@@ -16,7 +16,7 @@ module EE ...@@ -16,7 +16,7 @@ module EE
end end
def update_elastic_index def update_elastic_index
index_blobs if project.use_elasticsearch? index_wiki_blobs if project.use_elasticsearch?
end end
def path_to_repo def path_to_repo
......
...@@ -31,7 +31,7 @@ module EE ...@@ -31,7 +31,7 @@ module EE
def update_wiki_es_indexes(post_received) def update_wiki_es_indexes(post_received)
return unless post_received.project.use_elasticsearch? return unless post_received.project.use_elasticsearch?
post_received.project.wiki.index_blobs post_received.project.wiki.index_wiki_blobs
end end
end end
end end
...@@ -5,13 +5,13 @@ class ElasticCommitIndexerWorker ...@@ -5,13 +5,13 @@ class ElasticCommitIndexerWorker
sidekiq_options retry: 2 sidekiq_options retry: 2
def perform(project_id, oldrev = nil, newrev = nil) def perform(project_id, oldrev = nil, newrev = nil, wiki = false)
return true unless Gitlab::CurrentSettings.elasticsearch_indexing? return true unless Gitlab::CurrentSettings.elasticsearch_indexing?
project = Project.find(project_id) project = Project.find(project_id)
return true unless project.use_elasticsearch? return true unless project.use_elasticsearch?
Gitlab::Elastic::Indexer.new(project).run(newrev) Gitlab::Elastic::Indexer.new(project, wiki: wiki).run(newrev)
end end
end end
---
title: Use elasticsearch go indexer for wikis
merge_request: 13743
author:
type: fixed
...@@ -24,8 +24,9 @@ module Gitlab ...@@ -24,8 +24,9 @@ module Gitlab
attr_reader :project, :index_status attr_reader :project, :index_status
def initialize(project) def initialize(project, wiki: false)
@project = project @project = project
@wiki = wiki
# We accept any form of settings, including string and array # We accept any form of settings, including string and array
# This is why JSON is needed # This is why JSON is needed
...@@ -41,7 +42,7 @@ module Gitlab ...@@ -41,7 +42,7 @@ module Gitlab
end end
# Use the eager-loaded association if available. # Use the eager-loaded association if available.
@index_status = project.index_status @index_status = project.index_status unless wiki?
end end
def run(to_sha = nil) def run(to_sha = nil)
...@@ -50,20 +51,24 @@ module Gitlab ...@@ -50,20 +51,24 @@ module Gitlab
head_commit = repository.try(:commit) head_commit = repository.try(:commit)
if repository.nil? || !repository.exists? || repository.empty? || head_commit.nil? if repository.nil? || !repository.exists? || repository.empty? || head_commit.nil?
update_index_status(Gitlab::Git::BLANK_SHA) update_index_status(Gitlab::Git::BLANK_SHA) unless wiki?
return return
end end
run_indexer!(to_sha) run_indexer!(to_sha)
update_index_status(to_sha) update_index_status(to_sha) unless wiki?
true true
end end
private private
def wiki?
@wiki
end
def repository def repository
project.repository wiki? ? project.wiki.repository : project.repository
end end
def path_to_indexer def path_to_indexer
...@@ -76,16 +81,26 @@ module Gitlab ...@@ -76,16 +81,26 @@ module Gitlab
def use_experimental_indexer? def use_experimental_indexer?
strong_memoize(:use_experimental_indexer) do strong_memoize(:use_experimental_indexer) do
Gitlab::CurrentSettings.elasticsearch_experimental_indexer? && self.class.experimental_indexer_present? if wiki?
raise '`gitlab-elasticsearch-indexer` is required for indexing wikis' unless self.class.experimental_indexer_present?
true
else
Gitlab::CurrentSettings.elasticsearch_experimental_indexer? && self.class.experimental_indexer_present?
end
end end
end end
def run_indexer!(to_sha) def run_indexer!(to_sha)
if index_status && !repository_contains_last_indexed_commit? if index_status && !repository_contains_last_indexed_commit?
project.repository.delete_index_for_commits_and_blobs repository.delete_index_for_commits_and_blobs
end end
command = [path_to_indexer, project.id.to_s, repository_path] command = if wiki?
[path_to_indexer, "--blob-type=wiki_blob", "--skip-commits", project.id.to_s, repository_path]
else
[path_to_indexer, project.id.to_s, repository_path]
end
vars = @vars.merge('FROM_SHA' => from_sha, 'TO_SHA' => to_sha) vars = @vars.merge('FROM_SHA' => from_sha, 'TO_SHA' => to_sha)
......
...@@ -55,8 +55,8 @@ namespace :gitlab do ...@@ -55,8 +55,8 @@ namespace :gitlab do
puts "Indexing wiki of #{project.full_name}..." puts "Indexing wiki of #{project.full_name}..."
begin begin
project.wiki.index_blobs project.wiki.index_wiki_blobs
puts "Done!".color(:green) puts "Enqueued!".color(:green)
rescue StandardError => e rescue StandardError => e
puts "#{e.message}, trace - #{e.backtrace}" puts "#{e.message}, trace - #{e.backtrace}"
end end
......
...@@ -156,7 +156,7 @@ describe 'GlobalSearch' do ...@@ -156,7 +156,7 @@ describe 'GlobalSearch' do
project.repository.index_blobs project.repository.index_blobs
project.repository.index_commits project.repository.index_commits
project.wiki.index_blobs project.wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
end end
......
...@@ -145,7 +145,7 @@ describe 'Global elastic search', :elastic do ...@@ -145,7 +145,7 @@ describe 'Global elastic search', :elastic do
describe 'I search through the wiki blobs' do describe 'I search through the wiki blobs' do
before do before do
project.wiki.create_page('test.md', '# term') project.wiki.create_page('test.md', '# term')
project.wiki.index_blobs project.wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
end end
......
...@@ -73,7 +73,7 @@ describe 'Group elastic search', :js do ...@@ -73,7 +73,7 @@ describe 'Group elastic search', :js do
before do before do
wiki.create_page('test.md', '# term') wiki.create_page('test.md', '# term')
wiki.index_blobs wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
end end
......
...@@ -30,6 +30,52 @@ describe Gitlab::Elastic::Indexer do ...@@ -30,6 +30,52 @@ describe Gitlab::Elastic::Indexer do
end end
end end
context 'wikis' do
let!(:project) { create(:project, :wiki_repo) }
let(:indexer) { described_class.new(project, wiki: true) }
before do
project.wiki.create_page('test.md', '# term')
end
it 'does not ask for IndexStatus' do
expect(project).not_to receive(:index_status)
expect(project.wiki).not_to receive(:index_status)
expect_popen.and_return(popen_success)
indexer.run
end
it 'raises if it cannot find gitlab-elasticsearch-indexer' do
expect(described_class).to receive(:experimental_indexer_present?).and_return(false)
expect { indexer.run }.to raise_error('`gitlab-elasticsearch-indexer` is required for indexing wikis')
end
it 'runs the indexer with the right flags' do
expect(described_class).to receive(:experimental_indexer_present?).and_return(true)
expect_popen.with(
[
'gitlab-elasticsearch-indexer',
'--blob-type=wiki_blob',
'--skip-commits',
project.id.to_s,
"#{project.wiki.repository.disk_path}.git"
],
nil,
hash_including(
'ELASTIC_CONNECTION_INFO' => Gitlab::CurrentSettings.elasticsearch_config.to_json,
'RAILS_ENV' => Rails.env,
'FROM_SHA' => expected_from_sha,
'TO_SHA' => nil
)
).and_return(popen_success)
indexer.run
end
end
context 'repository has unborn head' do context 'repository has unborn head' do
it 'updates the index status without running the indexing command' do it 'updates the index status without running the indexing command' do
allow(project.repository).to receive(:exists?).and_return(false) allow(project.repository).to receive(:exists?).and_return(false)
......
...@@ -47,9 +47,9 @@ describe Gitlab::Elastic::ProjectSearchResults do ...@@ -47,9 +47,9 @@ describe Gitlab::Elastic::ProjectSearchResults do
# Wiki # Wiki
project.wiki.create_page('index_page', 'term') project.wiki.create_page('index_page', 'term')
project.wiki.index_blobs project.wiki.index_wiki_blobs
project1.wiki.create_page('index_page', ' term') project1.wiki.create_page('index_page', ' term')
project1.wiki.index_blobs project1.wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
...@@ -70,7 +70,7 @@ describe Gitlab::Elastic::ProjectSearchResults do ...@@ -70,7 +70,7 @@ describe Gitlab::Elastic::ProjectSearchResults do
# Wiki # Wiki
project.wiki.create_page('index_page', 'term') project.wiki.create_page('index_page', 'term')
project.wiki.index_blobs project.wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
......
...@@ -555,7 +555,7 @@ describe Gitlab::Elastic::SearchResults, :elastic do ...@@ -555,7 +555,7 @@ describe Gitlab::Elastic::SearchResults, :elastic do
before do before do
if project_1.wiki_enabled? if project_1.wiki_enabled?
project_1.wiki.create_page('index_page', 'term') project_1.wiki.create_page('index_page', 'term')
project_1.wiki.index_blobs project_1.wiki.index_wiki_blobs
end end
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
...@@ -579,7 +579,7 @@ describe Gitlab::Elastic::SearchResults, :elastic do ...@@ -579,7 +579,7 @@ describe Gitlab::Elastic::SearchResults, :elastic do
it 'finds wiki blobs from public projects only' do it 'finds wiki blobs from public projects only' do
project_2 = create :project, :repository, :private, :wiki_repo project_2 = create :project, :repository, :private, :wiki_repo
project_2.wiki.create_page('index_page', 'term') project_2.wiki.create_page('index_page', 'term')
project_2.wiki.index_blobs project_2.wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect(results.wiki_blobs_count).to eq 1 expect(results.wiki_blobs_count).to eq 1
...@@ -870,7 +870,7 @@ describe Gitlab::Elastic::SearchResults, :elastic do ...@@ -870,7 +870,7 @@ describe Gitlab::Elastic::SearchResults, :elastic do
before do before do
[public_project, internal_project, private_project1, private_project2].each do |project| [public_project, internal_project, private_project1, private_project2].each do |project|
project.wiki.create_page('index_page', 'term') project.wiki.create_page('index_page', 'term')
project.wiki.index_blobs project.wiki.index_wiki_blobs
end end
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
......
require 'spec_helper' require 'spec_helper'
describe ProjectWiki, :elastic do describe ProjectWiki, :elastic do
set(:project) { create(:project, :wiki_repo) }
before do before do
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
end
it "searches wiki page" do
project = create :project, :wiki_repo
Sidekiq::Testing.inline! do Sidekiq::Testing.inline! do
project.wiki.create_page("index_page", "Bla bla term1") project.wiki.create_page("index_page", "Bla bla term1")
project.wiki.create_page("omega_page", "Bla bla term2") project.wiki.create_page("omega_page", "Bla bla term2")
project.wiki.index_blobs project.wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
end end
end
it "searches wiki page" do
expect(project.wiki.search('term1', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(1) expect(project.wiki.search('term1', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(1)
expect(project.wiki.search('term1 | term2', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(2) expect(project.wiki.search('term1 | term2', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(2)
end end
context 'with old indexer' do
before do
stub_ee_application_setting(elasticsearch_experimental_indexer: false)
end
it 'searches wiki page' do
expect(project.wiki.search('term1', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(1)
expect(project.wiki.search('term1 | term2', type: :wiki_blob)[:wiki_blobs][:total_count]).to eq(2)
end
end
it 'uses the experimental indexer if enabled' do
stub_ee_application_setting(elasticsearch_experimental_indexer: true)
expect(project.wiki).not_to receive(:index_blobs)
expect(ElasticCommitIndexerWorker).to receive(:perform_async).with(project.id, nil, nil, true)
project.wiki.index_wiki_blobs
end
it 'indexes inside Rails if experiemntal indexer is not enabled' do
stub_ee_application_setting(elasticsearch_experimental_indexer: false)
expect(project.wiki).to receive(:index_blobs)
expect(ElasticCommitIndexerWorker).not_to receive(:perform_async)
project.wiki.index_wiki_blobs
end
end end
...@@ -49,7 +49,7 @@ describe API::Search do ...@@ -49,7 +49,7 @@ describe API::Search do
wiki = create(:project_wiki, project: project) wiki = create(:project_wiki, project: project)
create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: "Awesome page" }) create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: "Awesome page" })
project.wiki.index_blobs project.wiki.index_wiki_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' } get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' }
......
...@@ -23,5 +23,14 @@ describe ElasticCommitIndexerWorker do ...@@ -23,5 +23,14 @@ describe ElasticCommitIndexerWorker do
expect(subject.perform(1)).to be_truthy expect(subject.perform(1)).to be_truthy
end end
it 'runs indexer in wiki mode if asked to' do
indexer = double
expect(indexer).to receive(:run)
expect(Gitlab::Elastic::Indexer).to receive(:new).with(project, wiki: true).and_return(indexer)
subject.perform(project.id, nil, nil, true)
end
end end
end end
...@@ -72,7 +72,7 @@ describe PostReceive do ...@@ -72,7 +72,7 @@ describe PostReceive do
it 'triggers wiki index update when ElasticSearch is enabled', :elastic do it 'triggers wiki index update when ElasticSearch is enabled', :elastic do
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
expect_any_instance_of(ProjectWiki).to receive(:index_blobs) expect_any_instance_of(ProjectWiki).to receive(:index_wiki_blobs)
described_class.new.perform(gl_repository, key_id, base64_changes) described_class.new.perform(gl_repository, key_id, base64_changes)
end end
...@@ -88,7 +88,7 @@ describe PostReceive do ...@@ -88,7 +88,7 @@ describe PostReceive do
context 'when the project is not enabled specifically' do context 'when the project is not enabled specifically' do
it 'does not trigger wiki index update' do it 'does not trigger wiki index update' do
expect_any_instance_of(ProjectWiki).not_to receive(:index_blobs) expect_any_instance_of(ProjectWiki).not_to receive(:index_wiki_blobs)
described_class.new.perform(gl_repository, key_id, base64_changes) described_class.new.perform(gl_repository, key_id, base64_changes)
end end
...@@ -100,7 +100,7 @@ describe PostReceive do ...@@ -100,7 +100,7 @@ describe PostReceive do
end end
it 'triggers wiki index update' do it 'triggers wiki index update' do
expect_any_instance_of(ProjectWiki).to receive(:index_blobs) expect_any_instance_of(ProjectWiki).to receive(:index_wiki_blobs)
described_class.new.perform(gl_repository, key_id, base64_changes) described_class.new.perform(gl_repository, key_id, base64_changes)
end end
...@@ -116,7 +116,7 @@ describe PostReceive do ...@@ -116,7 +116,7 @@ describe PostReceive do
end end
it 'triggers wiki index update' do it 'triggers wiki index update' do
expect_any_instance_of(ProjectWiki).to receive(:index_blobs) expect_any_instance_of(ProjectWiki).to receive(:index_wiki_blobs)
described_class.new.perform(gl_repository, key_id, base64_changes) described_class.new.perform(gl_repository, key_id, base64_changes)
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