Class: Elasticsearch::IndexStager

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/index_stager.rb

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ IndexStager

Returns a new instance of IndexStager.



7
8
9
10
# File 'lib/elasticsearch/index_stager.rb', line 7

def initialize(opts)
  @index_name = opts[:index_name] or fail ":index_name required"
  @es_client = opts[:es_client] or fail ":es_client required"
end

Instance Attribute Details

#es_clientObject (readonly)

Returns the value of attribute es_client.



5
6
7
# File 'lib/elasticsearch/index_stager.rb', line 5

def es_client
  @es_client
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



5
6
7
# File 'lib/elasticsearch/index_stager.rb', line 5

def index_name
  @index_name
end

Instance Method Details

#alias_stage_to_tmp_indexObject



21
22
23
24
25
26
27
28
# File 'lib/elasticsearch/index_stager.rb', line 21

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 = index_name) ⇒ Object



30
31
32
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
# File 'lib/elasticsearch/index_stager.rb', line 30

def promote(live_index_name=index_name)
  @live_index_name = live_index_name || 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 != @live_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_nameObject



12
13
14
# File 'lib/elasticsearch/index_stager.rb', line 12

def stage_index_name
  index_name + "_staged"
end

#tmp_index_nameObject



16
17
18
19
# File 'lib/elasticsearch/index_stager.rb', line 16

def tmp_index_name
  @_suffix ||= Time.now.strftime('%Y%m%d%H%M%S') + '-' + SecureRandom.hex[0..7]
  "#{index_name}_#{@_suffix}"
end