Skip to content

Commit

Permalink
optional force switching of indices. #302
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 11, 2019
1 parent a4a4b1a commit b15a4f4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ def delete_index
# delete and create inactive index to use current mappings
# Needs to run every time we change the mappings
def upgrade_index
alias_name = self.index_name
inactive_index ||= self.inactive_index

self.__elasticsearch__.delete_index!(index: inactive_index) if self.__elasticsearch__.index_exists?(index: inactive_index)
Expand All @@ -400,17 +399,21 @@ def upgrade_index
end

# switch between the two indexes, i.e. the index that is aliased
def switch_index
def switch_index(options={})
alias_name = self.index_name
index_name = self.index_name + "_v1"
alternate_index_name = self.index_name + "_v2"

client = Elasticsearch::Model.client

## only switch if both indexes have the same number of documents
stats = client.indices.stats index: [index_name, alternate_index_name], docs: true
if stats.dig("indices",index_name,"primaries","docs","count") != (stats.dig("indices",alternate_index_name,"primaries","docs","count"))
return "Not switching active index as #{index_name} and #{alternate_index_name} have different number of documents."
## only switch if both indexes have the same number of documents, unless force option is true
unless options[:force].present?
stats = client.indices.stats index: [index_name, alternate_index_name], docs: true
index_name_count = stats.dig("indices", index_name, "primaries", "docs", "count")
alternate_index_name_count = stats.dig("indices", alternate_index_name, "primaries", "docs", "count")
if index_name_count != alternate_index_name_count
return "Not switching active index as #{index_name} and #{alternate_index_name} have different number of documents: #{index_name_count} vs. #{alternate_index_name_count}."
end
end

if client.indices.exists_alias?(name: alias_name, index: [index_name])
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/activity.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace :activity do

desc "Switch index for activities"
task :switch_index => :environment do
puts Activity.switch_index
puts Activity.switch_index(force: ENV["FORCE"])
end

desc "Return active index for activities"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/client.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace :client do

desc "Switch index for clients"
task :switch_index => :environment do
puts Client.switch_index
puts Client.switch_index(force: ENV["FORCE"])
end

desc "Return active index for clients"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/event.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace :event do

desc "Switch index for events"
task :switch_index => :environment do
puts Event.switch_index
puts Event.switch_index(force: ENV["FORCE"])
end

desc "Return active index for events"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/provider.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace :provider do

desc "Switch index for providers"
task :switch_index => :environment do
puts Provider.switch_index
puts Provider.switch_index(force: ENV["FORCE"])
end

desc "Return active index for providers"
Expand Down

0 comments on commit b15a4f4

Please sign in to comment.