Skip to content

Commit

Permalink
Tune index settings for better performance during index reset (#11)
Browse files Browse the repository at this point in the history
* feat: set number_of_replicas to zero and disable refresh_interval during the index reset

* close index before update settings

* add self to confusion with Kernel#open

* fix: can not update number of replicas in a closed index

* only delete existing index when the alias name is a real index

* fix: fixing broken specs
  • Loading branch information
marcosgz authored Jul 1, 2024
1 parent fa55761 commit ebbe801
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
29 changes: 22 additions & 7 deletions lib/esse/index/indices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ module ClassMethods
#
# @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
# @see Esse::Transport#create_index
def create_index(suffix: nil, **options)
def create_index(suffix: nil, body: nil, **options)
options = CREATE_INDEX_RESERVED_KEYWORDS.merge(options)
name = build_real_index_name(suffix)
definition = [settings_hash, mappings_hash].reduce(&:merge)
definition = body || [settings_hash, mappings_hash].reduce(&:merge)

if options.delete(:alias) && name != index_name
definition[:aliases] = { index_name => {} }
Expand All @@ -48,13 +48,23 @@ def create_index(suffix: nil, **options)
# @return [Hash] the elasticsearch response
#
# @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html
def reset_index(suffix: index_suffix, import: true, reindex: false, **options)
def reset_index(suffix: index_suffix, optimize: true, import: true, reindex: false, **options)
cluster.throw_error_when_readonly!
existing = []

suffix ||= Esse.timestamp
suffix = Esse.timestamp while index_exist?(suffix: suffix).tap { |exist| existing << suffix if exist }
suffix = Esse.timestamp while index_exist?(suffix: suffix)

if optimize
definition = [settings_hash, mappings_hash].reduce(&:merge)
number_of_replicas = definition.dig(Esse::SETTING_ROOT_KEY, :index, :number_of_replicas)
refresh_interval = definition.dig(Esse::SETTING_ROOT_KEY, :index, :refresh_interval)
new_number_of_replicas = ((definition[Esse::SETTING_ROOT_KEY] ||= {})[:index] ||= {})[:number_of_replicas] = 0
new_refresh_interval = ((definition[Esse::SETTING_ROOT_KEY] ||= {})[:index] ||= {})[:refresh_interval] = '-1'
create_index(**options, suffix: suffix, alias: false, body: definition)
else
create_index(**options, suffix: suffix, alias: false)
end

create_index(**options, suffix: suffix, alias: false)
if index_exist? && aliases.none?
cluster.api.delete_index(index: index_name)
end
Expand All @@ -63,8 +73,13 @@ def reset_index(suffix: index_suffix, import: true, reindex: false, **options)
elsif reindex && (_from = indices_pointing_to_alias).any?
# @TODO: Reindex using the reindex API
end

if optimize && number_of_replicas != new_number_of_replicas || refresh_interval != new_refresh_interval
update_settings(suffix: suffix)
end

update_aliases(suffix: suffix)
existing.each { |_s| delete_index!(**options, suffix: suffix) }

true
end

Expand Down
12 changes: 7 additions & 5 deletions spec/esse/cluster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@

it 'retuns an instance of elasticsearch as default' do
expect(model.instance_variable_get(:@client)).to eq(nil)
classes = []
if defined? Elasticsearch::Client # Elasticsearch-ruby >= 8.0
classes << Elasticsearch::Client
end
if defined? Elasticsearch::Transport::Client
expect(model.client).to be_an_instance_of(Elasticsearch::Transport::Client)
expect(model.instance_variable_get(:@client)).to be_an_instance_of(Elasticsearch::Transport::Client)
else # Elasticsearch-ruby >= 8.0
expect(model.client).to be_an_instance_of(Elasticsearch::Client)
expect(model.instance_variable_get(:@client)).to be_an_instance_of(Elasticsearch::Client)
classes << Elasticsearch::Transport::Client
end
expect(classes).to include(model.client.class)
expect(model.client).to be(model.instance_variable_get(:@client))
end

it 'store connection using default key' do
Expand Down

0 comments on commit ebbe801

Please sign in to comment.