Skip to content

Commit

Permalink
Performance: Use all cores to reindex models in elasticsearch.
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfschmidt committed Oct 13, 2023
1 parent cd8087a commit bb96e26
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -18,6 +18,7 @@ gem 'activerecord-session_store'
gem 'bootsnap', require: false
gem 'composite_primary_keys'
gem 'json'
gem 'parallel'

# core - application servers
gem 'puma', group: :puma
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -734,6 +734,7 @@ DEPENDENCIES
omniauth-weibo-oauth2!
openssl
overcommit
parallel
pg (~> 1.2.0)
pry-rails
pry-remote
Expand Down
13 changes: 8 additions & 5 deletions app/models/concerns/has_search_index_backend.rb
Expand Up @@ -176,15 +176,18 @@ def search_index_attributes_relevant(*attributes)
=end

def search_index_reload(silent: false)
def search_index_reload(silent: false, worker: 0)
tolerance = 10
tolerance_count = 0
query = reorder(created_at: :desc)
total = query.count
record_count = 0
batch_size = 100
query.as_batches(size: batch_size) do |record|
if !record.ignore_search_indexing?(:destroy)
batch_size = 200

query.in_batches(of: batch_size) do |records|
Parallel.map(records, { in_processes: worker }) do |record|
next if record.ignore_search_indexing?(:destroy)

begin
record.search_index_update_backend
rescue => e
Expand All @@ -197,7 +200,7 @@ def search_index_reload(silent: false)

next if silent

record_count += 1
record_count += records.count
if (record_count % batch_size).zero? || record_count == total
print "\r #{record_count}/#{total}" # rubocop:disable Rails/Output
end
Expand Down
8 changes: 4 additions & 4 deletions lib/tasks/zammad/search_index_es.rake
Expand Up @@ -34,12 +34,12 @@ namespace :zammad do
end

desc 'Reload all indexable data'
task reload: %i[zammad:searchindex:version_supported] do
task :reload, [:worker] => %i[zammad:searchindex:version_supported] do |_task, args|
puts 'Reloading data... '
Models.indexable.each do |model_class|
puts " - #{model_class}... "
time_spent = Benchmark.realtime do
model_class.search_index_reload
model_class.search_index_reload(worker: args[:worker].to_i)
end
# Add whitespace at the end to overwrite text from progress indicator line.
puts "\r done in #{time_spent.to_i} seconds.#{' ' * 20}"
Expand All @@ -54,10 +54,10 @@ namespace :zammad do
end

desc 'Full re-creation of all search indexes and re-indexing of all data'
task rebuild: %i[zammad:searchindex:version_supported] do
task :rebuild, [:worker] => %i[zammad:searchindex:version_supported] do |_task, args|
Rake::Task['zammad:searchindex:drop'].execute
Rake::Task['zammad:searchindex:create'].execute
Rake::Task['zammad:searchindex:reload'].execute
Rake::Task['zammad:searchindex:reload'].execute(args)
end

task version_supported: %i[zammad:searchindex:configured] do
Expand Down

0 comments on commit bb96e26

Please sign in to comment.