2022-03-25 20:00:59 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::SystemCheck::ElasticsearchCheck < Admin::SystemCheck::BaseCheck
|
2023-08-21 14:50:22 +00:00
|
|
|
INDEXES = [
|
|
|
|
InstancesIndex,
|
|
|
|
AccountsIndex,
|
|
|
|
TagsIndex,
|
|
|
|
StatusesIndex,
|
2023-08-25 10:01:55 +00:00
|
|
|
PublicStatusesIndex,
|
2023-08-21 14:50:22 +00:00
|
|
|
].freeze
|
|
|
|
|
2022-07-05 00:41:40 +00:00
|
|
|
def skip?
|
|
|
|
!current_user.can?(:view_devops)
|
|
|
|
end
|
|
|
|
|
2022-03-25 20:00:59 +00:00
|
|
|
def pass?
|
|
|
|
return true unless Chewy.enabled?
|
|
|
|
|
2023-08-21 14:50:22 +00:00
|
|
|
running_version.present? && compatible_version? && cluster_health['status'] == 'green' && indexes_match? && preset_matches?
|
|
|
|
rescue Faraday::ConnectionFailed, Elasticsearch::Transport::Transport::Error
|
|
|
|
false
|
2022-03-25 20:00:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
2023-08-21 14:50:22 +00:00
|
|
|
if running_version.blank?
|
|
|
|
Admin::SystemCheck::Message.new(:elasticsearch_running_check)
|
|
|
|
elsif !compatible_version?
|
2023-01-05 12:44:31 +00:00
|
|
|
Admin::SystemCheck::Message.new(
|
|
|
|
:elasticsearch_version_check,
|
|
|
|
I18n.t(
|
|
|
|
'admin.system_checks.elasticsearch_version_check.version_comparison',
|
|
|
|
running_version: running_version,
|
|
|
|
required_version: required_version
|
|
|
|
)
|
|
|
|
)
|
2023-08-21 14:50:22 +00:00
|
|
|
elsif !indexes_match?
|
|
|
|
Admin::SystemCheck::Message.new(
|
|
|
|
:elasticsearch_index_mismatch,
|
|
|
|
mismatched_indexes.join(' ')
|
|
|
|
)
|
|
|
|
elsif cluster_health['status'] == 'red'
|
|
|
|
Admin::SystemCheck::Message.new(:elasticsearch_health_red)
|
|
|
|
elsif cluster_health['number_of_nodes'] < 2 && es_preset != 'single_node_cluster'
|
|
|
|
Admin::SystemCheck::Message.new(:elasticsearch_preset_single_node, nil, 'https://docs.joinmastodon.org/admin/optional/elasticsearch/#scaling')
|
2023-08-22 16:51:32 +00:00
|
|
|
elsif Chewy.client.indices.get_settings[Chewy::Stash::Specification.index_name]&.dig('settings', 'index', 'number_of_replicas')&.to_i&.positive? && es_preset == 'single_node_cluster'
|
2023-08-21 14:50:22 +00:00
|
|
|
Admin::SystemCheck::Message.new(:elasticsearch_reset_chewy)
|
|
|
|
elsif cluster_health['status'] == 'yellow'
|
|
|
|
Admin::SystemCheck::Message.new(:elasticsearch_health_yellow)
|
2022-03-25 20:00:59 +00:00
|
|
|
else
|
2023-08-21 14:50:22 +00:00
|
|
|
Admin::SystemCheck::Message.new(:elasticsearch_preset, nil, 'https://docs.joinmastodon.org/admin/optional/elasticsearch/#scaling')
|
2022-03-25 20:00:59 +00:00
|
|
|
end
|
2023-08-21 14:50:22 +00:00
|
|
|
rescue Faraday::ConnectionFailed, Elasticsearch::Transport::Transport::Error
|
|
|
|
Admin::SystemCheck::Message.new(:elasticsearch_running_check)
|
2022-03-25 20:00:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-08-21 14:50:22 +00:00
|
|
|
def cluster_health
|
|
|
|
@cluster_health ||= Chewy.client.cluster.health
|
|
|
|
end
|
|
|
|
|
2022-03-25 20:00:59 +00:00
|
|
|
def running_version
|
|
|
|
@running_version ||= begin
|
2023-01-18 15:21:48 +00:00
|
|
|
Chewy.client.info['version']['number']
|
2023-03-15 03:01:49 +00:00
|
|
|
rescue Faraday::ConnectionFailed, Elasticsearch::Transport::Transport::Error
|
2022-03-25 20:00:59 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-18 15:21:48 +00:00
|
|
|
def compatible_wire_version
|
|
|
|
Chewy.client.info['version']['minimum_wire_compatibility_version']
|
|
|
|
end
|
|
|
|
|
2022-03-25 20:00:59 +00:00
|
|
|
def required_version
|
|
|
|
'7.x'
|
|
|
|
end
|
|
|
|
|
|
|
|
def compatible_version?
|
2022-12-15 14:56:48 +00:00
|
|
|
return false if running_version.nil?
|
2023-01-18 15:21:48 +00:00
|
|
|
|
|
|
|
Gem::Version.new(running_version) >= Gem::Version.new(required_version) ||
|
|
|
|
Gem::Version.new(compatible_wire_version) >= Gem::Version.new(required_version)
|
2023-08-21 14:50:22 +00:00
|
|
|
rescue ArgumentError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def mismatched_indexes
|
|
|
|
@mismatched_indexes ||= INDEXES.filter_map do |klass|
|
2023-08-25 16:17:22 +00:00
|
|
|
klass.base_name if Chewy.client.indices.get_mapping[klass.index_name]&.deep_symbolize_keys != klass.mappings_hash
|
2023-08-21 14:50:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def indexes_match?
|
|
|
|
mismatched_indexes.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def es_preset
|
|
|
|
ENV.fetch('ES_PRESET', 'single_node_cluster')
|
|
|
|
end
|
|
|
|
|
|
|
|
def preset_matches?
|
|
|
|
case es_preset
|
|
|
|
when 'single_node_cluster'
|
|
|
|
cluster_health['number_of_nodes'] == 1
|
|
|
|
else
|
|
|
|
cluster_health['number_of_nodes'] > 1
|
|
|
|
end
|
2022-03-25 20:00:59 +00:00
|
|
|
end
|
|
|
|
end
|