Class: Elasticsearch::IndexStager

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

Constant Summary collapse

VERSION =
'1.1.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ IndexStager

Returns a new instance of IndexStager.



9
10
11
12
# File 'lib/elasticsearch/index_stager.rb', line 9

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.



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

def es_client
  @es_client
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



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

def index_name
  @index_name
end

Instance Method Details

#alias_stage_to_tmp_indexObject



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

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



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
68
69
70
71
72
73
74
75
76
# File 'lib/elasticsearch/index_stager.rb', line 32

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_aliased_to, 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 = []
  live_index_exists = false
  begin
    existing_live_index = es_client.indices.get_alias(index: @live_index_name, name: '*')
    live_index_exists = true
  rescue Elasticsearch::Transport::Transport::Errors::NotFound => _err
    existing_live_index = {}
  rescue => _err
    raise _err
  end
  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
      raise "Found existing index called #{@live_index_name} aliased to itself"
    end
  end

  rename_live_index if live_index_exists

  # 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



14
15
16
# File 'lib/elasticsearch/index_stager.rb', line 14

def stage_index_name
  index_name + "_staged"
end

#tmp_index_nameObject



18
19
20
21
# File 'lib/elasticsearch/index_stager.rb', line 18

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