2019-03-28 10:16:11 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-24 18:55:40 +09:00
|
|
|
require_relative 'base'
|
2019-03-28 10:16:11 +09:00
|
|
|
|
2023-05-23 23:08:26 +09:00
|
|
|
module Mastodon::CLI
|
2023-05-24 18:55:40 +09:00
|
|
|
class Search < Base
|
2020-07-15 01:10:35 +09:00
|
|
|
# Indices are sorted by amount of data to be expected in each, so that
|
|
|
|
# smaller indices can go online sooner
|
|
|
|
INDICES = [
|
2023-07-27 23:11:17 +09:00
|
|
|
InstancesIndex,
|
2020-07-15 01:10:35 +09:00
|
|
|
AccountsIndex,
|
|
|
|
TagsIndex,
|
2023-08-24 23:40:04 +09:00
|
|
|
PublicStatusesIndex,
|
2020-07-15 01:10:35 +09:00
|
|
|
StatusesIndex,
|
|
|
|
].freeze
|
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
option :concurrency, type: :numeric, default: 5, aliases: [:c], desc: 'Workload will be split between this number of threads'
|
|
|
|
option :batch_size, type: :numeric, default: 100, aliases: [:b], desc: 'Number of records in each batch'
|
2023-09-12 16:59:59 +09:00
|
|
|
option :only, type: :array, enum: %w(instances accounts tags statuses public_statuses), desc: 'Only process these indices'
|
2022-05-23 05:16:43 +09:00
|
|
|
option :import, type: :boolean, default: true, desc: 'Import data from the database to the index'
|
|
|
|
option :clean, type: :boolean, default: true, desc: 'Remove outdated documents from the index'
|
2023-08-15 02:00:56 +09:00
|
|
|
option :reset_chewy, type: :boolean, default: false, desc: "Reset Chewy's internal index"
|
2021-11-26 16:30:02 +09:00
|
|
|
desc 'deploy', 'Create or upgrade Elasticsearch indices and populate them'
|
2019-03-28 10:16:11 +09:00
|
|
|
long_desc <<~LONG_DESC
|
2021-11-26 16:30:02 +09:00
|
|
|
If Elasticsearch is empty, this command will create the necessary indices
|
2019-03-28 10:16:11 +09:00
|
|
|
and then import data from the database into those indices.
|
|
|
|
|
|
|
|
This command will also upgrade indices if the underlying schema has been
|
2022-05-23 05:16:43 +09:00
|
|
|
changed since the last run. Index upgrades erase index data.
|
2019-10-03 04:50:43 +09:00
|
|
|
|
2020-07-15 01:10:35 +09:00
|
|
|
Even if creating or upgrading indices is not necessary, data from the
|
2022-08-29 00:44:34 +09:00
|
|
|
database will be imported into the indices, unless overridden with --no-import.
|
2019-03-28 10:16:11 +09:00
|
|
|
LONG_DESC
|
|
|
|
def deploy
|
2023-06-01 21:35:05 +09:00
|
|
|
verify_deploy_options!
|
2021-11-26 16:29:53 +09:00
|
|
|
|
2023-02-19 07:09:40 +09:00
|
|
|
indices = if options[:only]
|
|
|
|
options[:only].map { |str| "#{str.camelize}Index".constantize }
|
|
|
|
else
|
|
|
|
INDICES
|
|
|
|
end
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
pool = Concurrent::FixedThreadPool.new(options[:concurrency], max_queue: options[:concurrency] * 10)
|
|
|
|
importers = indices.index_with { |index| "Importer::#{index.name}Importer".constantize.new(batch_size: options[:batch_size], executor: pool) }
|
2023-12-13 19:14:19 +09:00
|
|
|
progress = ProgressBar.create(
|
|
|
|
{
|
|
|
|
total: nil,
|
|
|
|
format: '%t%c/%u |%b%i| %e (%r docs/s)',
|
|
|
|
autofinish: false,
|
|
|
|
}.merge(progress_output_options)
|
|
|
|
)
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2023-08-15 02:00:56 +09:00
|
|
|
Chewy::Stash::Specification.reset! if options[:reset_chewy]
|
|
|
|
|
2020-07-15 01:10:35 +09:00
|
|
|
# First, ensure all indices are created and have the correct
|
|
|
|
# structure, so that live data can already be written
|
|
|
|
indices.select { |index| index.specification.changed? }.each do |index|
|
|
|
|
progress.title = "Upgrading #{index} "
|
|
|
|
index.purge
|
|
|
|
index.specification.lock!
|
|
|
|
end
|
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
progress.title = 'Estimating workload '
|
|
|
|
progress.total = indices.sum { |index| importers[index].estimate! }
|
|
|
|
|
2022-04-30 05:43:07 +09:00
|
|
|
reset_connection_pools!
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
added = 0
|
|
|
|
removed = 0
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
indices.each do |index|
|
|
|
|
importer = importers[index]
|
|
|
|
importer.optimize_for_import!
|
|
|
|
|
|
|
|
importer.on_progress do |(indexed, deleted)|
|
|
|
|
progress.total = nil if progress.progress + indexed + deleted > progress.total
|
|
|
|
progress.progress += indexed + deleted
|
|
|
|
added += indexed
|
|
|
|
removed += deleted
|
|
|
|
end
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
importer.on_failure do |reason|
|
|
|
|
progress.log(pastel.red("Error while importing #{index}: #{reason}"))
|
|
|
|
end
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
if options[:import]
|
|
|
|
progress.title = "Importing #{index} "
|
|
|
|
importer.import!
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:clean]
|
|
|
|
progress.title = "Cleaning #{index} "
|
|
|
|
importer.clean_up!
|
2020-07-15 01:10:35 +09:00
|
|
|
end
|
2022-05-23 05:16:43 +09:00
|
|
|
ensure
|
|
|
|
importer.optimize_for_search!
|
2019-10-03 04:50:43 +09:00
|
|
|
end
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
progress.title = 'Done! '
|
|
|
|
progress.finish
|
2020-07-15 01:10:35 +09:00
|
|
|
|
2022-05-23 05:16:43 +09:00
|
|
|
say("Indexed #{added} records, de-indexed #{removed}", :green, true)
|
2024-03-15 23:26:23 +09:00
|
|
|
rescue Elasticsearch::Transport::Transport::ServerError => e
|
|
|
|
fail_with_message <<~ERROR
|
|
|
|
There was an issue connecting to the search server. Make sure the
|
|
|
|
server is configured and running correctly, and that the environment
|
|
|
|
variable settings match what the server is expecting.
|
|
|
|
|
|
|
|
#{e.message}
|
|
|
|
ERROR
|
2019-03-28 10:16:11 +09:00
|
|
|
end
|
2023-06-01 21:35:05 +09:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def verify_deploy_options!
|
|
|
|
verify_deploy_concurrency!
|
|
|
|
verify_deploy_batch_size!
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_deploy_concurrency!
|
2024-01-26 17:53:44 +09:00
|
|
|
fail_with_message 'Cannot run with this concurrency setting, must be at least 1' if options[:concurrency] < 1
|
2023-06-01 21:35:05 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify_deploy_batch_size!
|
2024-01-26 17:53:44 +09:00
|
|
|
fail_with_message 'Cannot run with this batch_size setting, must be at least 1' if options[:batch_size] < 1
|
2023-06-01 21:35:05 +09:00
|
|
|
end
|
2023-12-13 19:14:19 +09:00
|
|
|
|
|
|
|
def progress_output_options
|
|
|
|
Rails.env.test? ? { output: ProgressBar::Outputs::Null } : {}
|
|
|
|
end
|
2019-03-28 10:16:11 +09:00
|
|
|
end
|
|
|
|
end
|