From b15a4f41f63b6064ad5d42ca27fa6bbcb16bd7ff Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 11 Jul 2019 14:29:28 +0200 Subject: [PATCH] optional force switching of indices. #302 --- app/models/concerns/indexable.rb | 15 +++++++++------ lib/tasks/activity.rake | 2 +- lib/tasks/client.rake | 2 +- lib/tasks/event.rake | 2 +- lib/tasks/provider.rake | 2 +- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 039fcd82a..dcac59c92 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -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) @@ -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]) diff --git a/lib/tasks/activity.rake b/lib/tasks/activity.rake index 3e13d62c9..033c2ce13 100644 --- a/lib/tasks/activity.rake +++ b/lib/tasks/activity.rake @@ -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" diff --git a/lib/tasks/client.rake b/lib/tasks/client.rake index ab7f09b18..2525738e2 100644 --- a/lib/tasks/client.rake +++ b/lib/tasks/client.rake @@ -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" diff --git a/lib/tasks/event.rake b/lib/tasks/event.rake index 52c027b58..def0ca784 100644 --- a/lib/tasks/event.rake +++ b/lib/tasks/event.rake @@ -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" diff --git a/lib/tasks/provider.rake b/lib/tasks/provider.rake index ae1ca68e5..b36b067b2 100644 --- a/lib/tasks/provider.rake +++ b/lib/tasks/provider.rake @@ -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"