elasticsearch-rails-ha 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/elasticsearch/rails/ha/index_stager.rb +6 -89
- data/lib/elasticsearch/rails/ha/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d15db1d837bdf59bce06cb1f7fec6620f792053
|
4
|
+
data.tar.gz: e0cc52d79063153b7efa2b47ce5b334b5743754c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb7d6ac63a2247a4f72e922e3b4bb2ab6e474c775609c73f06fec4267e4155f2ae2a5e748e21ac361acc4310ecd4270097ca6cd04f7708ec9bb1f98f3c8b89d1
|
7
|
+
data.tar.gz: 110a2572722c5a5b1c578243183695c47376bb41889bf748a5be6ee27be07ea85d54bfd975d124fa4afcc171e6db9c9e810862d1653640f717ab49df244c3c09
|
@@ -1,106 +1,23 @@
|
|
1
|
+
require 'elasticsearch/index_stager'
|
2
|
+
|
1
3
|
module Elasticsearch
|
2
4
|
module Rails
|
3
5
|
module HA
|
4
|
-
class IndexStager
|
6
|
+
class IndexStager < Elasticsearch::IndexStager
|
5
7
|
attr_reader :klass, :live_index_name
|
6
8
|
|
7
9
|
def initialize(klass)
|
8
10
|
@klass = klass.constantize
|
11
|
+
@index_name = @klass.index_name
|
12
|
+
@es_client = @klass.__elasticsearch__.client
|
9
13
|
end
|
10
14
|
|
11
15
|
def stage_index_name
|
12
16
|
if klass.respond_to?(:stage_index_name)
|
13
17
|
klass.stage_index_name
|
14
18
|
else
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def tmp_index_name
|
20
|
-
@_suffix ||= Time.now.strftime('%Y%m%d%H%M%S') + '-' + SecureRandom.hex[0..7]
|
21
|
-
"#{klass.index_name}_#{@_suffix}"
|
22
|
-
end
|
23
|
-
|
24
|
-
def alias_stage_to_tmp_index
|
25
|
-
es_client.indices.delete index: stage_index_name rescue false
|
26
|
-
es_client.indices.update_aliases body: {
|
27
|
-
actions: [
|
28
|
-
{ add: { index: tmp_index_name, alias: stage_index_name } }
|
29
|
-
]
|
30
|
-
}
|
31
|
-
end
|
32
|
-
|
33
|
-
def promote(live_index_name=klass.index_name)
|
34
|
-
@live_index_name = live_index_name || klass.index_name
|
35
|
-
|
36
|
-
# the renaming actions (performed atomically by ES)
|
37
|
-
rename_actions = [
|
38
|
-
{ remove: { index: stage_aliased_to, alias: stage_index_name } },
|
39
|
-
{ add: { index: stage_index_name, alias: @live_index_name } }
|
40
|
-
]
|
41
|
-
|
42
|
-
# zap any existing index known as index_name,
|
43
|
-
# but do it conditionally since it is reasonable that it does not exist.
|
44
|
-
to_delete = []
|
45
|
-
existing_live_index = es_client.indices.get_aliases(index: @live_index_name)
|
46
|
-
existing_live_index.each do |k,v|
|
47
|
-
|
48
|
-
# if the index is merely aliased, remove its alias as part of the aliasing transaction.
|
49
|
-
if k != @live_index_name
|
50
|
-
rename_actions.unshift({ remove: { index: k, alias: @live_index_name } })
|
51
|
-
|
52
|
-
# mark it for deletion when we've successfully updated aliases
|
53
|
-
to_delete.push k
|
54
|
-
|
55
|
-
else
|
56
|
-
# this is a real, unaliased index with this name, so it must be deleted.
|
57
|
-
# (This usually happens the first time we implement the aliasing scheme against
|
58
|
-
# an existing installation.)
|
59
|
-
es_client.indices.delete index: @live_index_name rescue false
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
# re-alias
|
64
|
-
es_client.indices.update_aliases body: { actions: rename_actions }
|
65
|
-
|
66
|
-
# clean up
|
67
|
-
to_delete.each do |idxname|
|
68
|
-
es_client.indices.delete index: idxname rescue false
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
def tmp_index_pattern
|
75
|
-
/#{klass.index_name}_(\d{14})-\w{8}$/
|
76
|
-
end
|
77
|
-
|
78
|
-
def es_client
|
79
|
-
klass.__elasticsearch__.client
|
80
|
-
end
|
81
|
-
|
82
|
-
def stage_aliased_to
|
83
|
-
# find the newest tmp index to which staged is aliased.
|
84
|
-
# we need this because we want to re-alias it.
|
85
|
-
aliased_to = find_newest_alias_for(stage_index_name)
|
86
|
-
end
|
87
|
-
|
88
|
-
def find_newest_alias_for(the_index_name)
|
89
|
-
aliased_to = nil
|
90
|
-
aliases = es_client.indices.get_aliases(index: the_index_name)
|
91
|
-
aliases.each do |k,v|
|
92
|
-
next unless k.match(tmp_index_pattern)
|
93
|
-
aliased_to ||= k
|
94
|
-
alias_tstamp = aliased_to.match(tmp_index_pattern)[1]
|
95
|
-
k_tstamp = k.match(tmp_index_pattern)[1]
|
96
|
-
if Time.parse(alias_tstamp) < Time.parse(k_tstamp)
|
97
|
-
aliased_to = k
|
98
|
-
end
|
99
|
-
end
|
100
|
-
if !aliased_to
|
101
|
-
raise "Cannot identify index aliased to by '#{the_index_name}'"
|
19
|
+
index_name + "_staged"
|
102
20
|
end
|
103
|
-
aliased_to
|
104
21
|
end
|
105
22
|
end
|
106
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticsearch-rails-ha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Karman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: elasticsearch-model
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: elasticsearch-indexstager
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: ansi
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|