Class: Elasticsearch::Rails::HA::IndexStager
- Inherits:
-
Object
- Object
- Elasticsearch::Rails::HA::IndexStager
- Defined in:
- lib/elasticsearch/rails/ha/index_stager.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#live_index_name ⇒ Object
readonly
Returns the value of attribute live_index_name.
Instance Method Summary collapse
- #alias_stage_to_tmp_index ⇒ Object
-
#initialize(klass) ⇒ IndexStager
constructor
A new instance of IndexStager.
- #promote(live_index_name = klass.index_name) ⇒ Object
- #stage_index_name ⇒ Object
- #tmp_index_name ⇒ Object
Constructor Details
#initialize(klass) ⇒ IndexStager
Returns a new instance of IndexStager.
7 8 9 |
# File 'lib/elasticsearch/rails/ha/index_stager.rb', line 7 def initialize(klass) @klass = klass.constantize end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/elasticsearch/rails/ha/index_stager.rb', line 5 def klass @klass end |
#live_index_name ⇒ Object (readonly)
Returns the value of attribute live_index_name.
5 6 7 |
# File 'lib/elasticsearch/rails/ha/index_stager.rb', line 5 def live_index_name @live_index_name end |
Instance Method Details
#alias_stage_to_tmp_index ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/elasticsearch/rails/ha/index_stager.rb', line 24 def alias_stage_to_tmp_index es_client.indices.delete index: stage_index_name rescue false es_client.indices.update_aliases body: { actions: [ { add: { index: tmp_index_name, alias: stage_index_name } } ] } end |
#promote(live_index_name = klass.index_name) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/elasticsearch/rails/ha/index_stager.rb', line 33 def promote(live_index_name=klass.index_name) @live_index_name = live_index_name # the renaming actions (performed atomically by ES) rename_actions = [ { remove: { index: stage_aliased_to, alias: stage_index_name } }, { add: { index: stage_index_name, alias: live_index_name } } ] # zap any existing index known as index_name, # but do it conditionally since it is reasonable that it does not exist. to_delete = [] existing_live_index = es_client.indices.get_aliases(index: live_index_name) existing_live_index.each do |k,v| # if the index is merely aliased, remove its alias as part of the aliasing transaction. if k != klass.index_name rename_actions.unshift({ remove: { index: k, alias: live_index_name } }) # mark it for deletion when we've successfully updated aliases to_delete.push k else # this is a real, unaliased index with this name, so it must be deleted. # (This usually happens the first time we implement the aliasing scheme against # an existing installation.) es_client.indices.delete index: live_index_name rescue false end end # re-alias es_client.indices.update_aliases body: { actions: rename_actions } # clean up to_delete.each do |idxname| es_client.indices.delete index: idxname rescue false end end |
#stage_index_name ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/elasticsearch/rails/ha/index_stager.rb', line 11 def stage_index_name if klass.respond_to?(:stage_index_name) klass.stage_index_name else klass.index_name + "_staged" end end |
#tmp_index_name ⇒ Object
19 20 21 22 |
# File 'lib/elasticsearch/rails/ha/index_stager.rb', line 19 def tmp_index_name @_suffix ||= Time.now.strftime('%Y%m%d%H%M%S') + '-' + SecureRandom.hex[0..7] "#{klass.index_name}_#{@_suffix}" end |