elasticsearch-transport 1.0.12 → 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e32b60a7684ac31fbed63461ef4d4f7d58c8525
4
- data.tar.gz: 05b935d80dd804943ea9bf8b717c3cd77ec65beb
3
+ metadata.gz: cfbb24f919ca5c508c660a9b121fb0937b16d5ab
4
+ data.tar.gz: 2efeb80ae1defb9578b2b62d2834f61b86e75983
5
5
  SHA512:
6
- metadata.gz: c8fcccb368fe251af1a33285d4e9417e9786c40790286a7f9b347743716f299cd130037bcbcb20489dc8d8581ffdb5bcbe5b1f3b8415428c13203ee1b263804f
7
- data.tar.gz: 3d78d28acc9de111fdf5e8d0463ad756128a0de23e80954d7542a65ac148bd9b6727279ff13338973d85a8deed6faefd02bbb62eac973dede071a16ee4e827df
6
+ metadata.gz: aefe90beda8b000367535d535156786126c8f7b49d98cd637352a0cdc1a054f248fd7225973cc2b7a7be72207bfedf9b28bfd475f138144110b79082daa6c37b
7
+ data.tar.gz: e959b3c3c12990b2280ac9ad3836c3cdfb7c63a90ca5a46f2c78c50d974c3de921602b6f927749a7cc5d29f71809706bfb1705cd643a3e2680e0e1c01bb95ad2
@@ -143,22 +143,25 @@ module Elasticsearch
143
143
  end
144
144
 
145
145
  result = hosts.map do |host|
146
- case host
147
- when String
148
- if host =~ /^[a-z]+\:\/\//
149
- uri = URI.parse(host)
150
- { :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, :port => uri.port.to_s }
146
+ host_parts = case host
147
+ when String
148
+ if host =~ /^[a-z]+\:\/\//
149
+ uri = URI.parse(host)
150
+ { :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, :port => uri.port }
151
+ else
152
+ host, port = host.split(':')
153
+ { :host => host, :port => port }
154
+ end
155
+ when URI
156
+ { :scheme => host.scheme, :user => host.user, :password => host.password, :host => host.host, :path => host.path, :port => host.port }
157
+ when Hash
158
+ host
151
159
  else
152
- host, port = host.split(':')
153
- { :host => host, :port => port }
160
+ raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
154
161
  end
155
- when URI
156
- { :scheme => host.scheme, :user => host.user, :password => host.password, :host => host.host, :path => host.path, :port => host.port.to_s }
157
- when Hash
158
- host
159
- else
160
- raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
161
- end
162
+
163
+ host_parts[:port] = host_parts[:port].to_i unless host_parts[:port].nil?
164
+ host_parts
162
165
  end
163
166
 
164
167
  result.shuffle! if options[:randomize_hosts]
@@ -41,6 +41,7 @@ module Elasticsearch
41
41
 
42
42
  @sniffer = options[:sniffer_class] ? options[:sniffer_class].new(self) : Sniffer.new(self)
43
43
  @counter = 0
44
+ @counter_mtx = Mutex.new
44
45
  @last_request_at = Time.now
45
46
  @reload_connections = options[:reload_connections]
46
47
  @reload_after = options[:reload_connections].is_a?(Fixnum) ? options[:reload_connections] : DEFAULT_RELOAD_AFTER
@@ -60,7 +61,7 @@ module Elasticsearch
60
61
  resurrect_dead_connections! if Time.now > @last_request_at + @resurrect_after
61
62
 
62
63
  connection = connections.get_connection(options)
63
- @counter += 1
64
+ @counter_mtx.synchronize { @counter += 1 }
64
65
 
65
66
  reload_connections! if reload_connections && counter % reload_after == 0
66
67
  connection
@@ -26,6 +26,7 @@ module Elasticsearch
26
26
  @host = arguments[:host]
27
27
  @connection = arguments[:connection]
28
28
  @options = arguments[:options] || {}
29
+ @state_mutex = Mutex.new
29
30
 
30
31
  @options[:resurrect_timeout] ||= DEFAULT_RESURRECT_TIMEOUT
31
32
  @failures = 0
@@ -65,9 +66,11 @@ module Elasticsearch
65
66
  # @return [self]
66
67
  #
67
68
  def dead!
68
- @dead = true
69
- @failures += 1
70
- @dead_since = Time.now
69
+ @state_mutex.synchronize do
70
+ @dead = true
71
+ @failures += 1
72
+ @dead_since = Time.now
73
+ end
71
74
  self
72
75
  end
73
76
 
@@ -76,7 +79,9 @@ module Elasticsearch
76
79
  # @return [self]
77
80
  #
78
81
  def alive!
79
- @dead = false
82
+ @state_mutex.synchronize do
83
+ @dead = false
84
+ end
80
85
  self
81
86
  end
82
87
 
@@ -85,8 +90,10 @@ module Elasticsearch
85
90
  # @return [self]
86
91
  #
87
92
  def healthy!
88
- @dead = false
89
- @failures = 0
93
+ @state_mutex.synchronize do
94
+ @dead = false
95
+ @failures = 0
96
+ end
90
97
  self
91
98
  end
92
99
 
@@ -105,7 +112,9 @@ module Elasticsearch
105
112
  # @return [Boolean]
106
113
  #
107
114
  def resurrectable?
108
- Time.now > @dead_since + ( @options[:resurrect_timeout] * 2 ** (@failures-1) )
115
+ @state_mutex.synchronize {
116
+ Time.now > @dead_since + ( @options[:resurrect_timeout] * 2 ** (@failures-1) )
117
+ }
109
118
  end
110
119
 
111
120
  # @return [String]
@@ -5,7 +5,8 @@ module Elasticsearch
5
5
  # Handles node discovery ("sniffing").
6
6
  #
7
7
  class Sniffer
8
- RE_URL = /\/([^:]*):([0-9]+)\]/ # Use named groups on Ruby 1.9: /\/(?<host>[^:]*):(?<port>[0-9]+)\]/
8
+ ES1_RE_URL = /\/([^:]*):([0-9]+)\]/ # Use named groups on Ruby 1.9: /\/(?<host>[^:]*):(?<port>[0-9]+)\]/
9
+ ES2_RE_URL = /([^:]*):([0-9]+)/
9
10
 
10
11
  attr_reader :transport
11
12
  attr_accessor :timeout
@@ -30,7 +31,9 @@ module Elasticsearch
30
31
  Timeout::timeout(timeout, SnifferTimeoutError) do
31
32
  nodes = transport.perform_request('GET', '_nodes/http').body
32
33
  hosts = nodes['nodes'].map do |id,info|
33
- if matches = info["#{transport.protocol}_address"].to_s.match(RE_URL)
34
+ addr_str = info["#{transport.protocol}_address"].to_s
35
+ matches = addr_str.match(ES1_RE_URL) || addr_str.match(ES2_RE_URL)
36
+ if matches
34
37
  # TODO: Implement lightweight "indifferent access" here
35
38
  info.merge :host => matches[1], :port => matches[2], :id => id
36
39
  end
@@ -1,5 +1,5 @@
1
1
  module Elasticsearch
2
2
  module Transport
3
- VERSION = "1.0.12"
3
+ VERSION = "1.0.13"
4
4
  end
5
5
  end
@@ -5,6 +5,10 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
5
5
  Elasticsearch::Extensions::Test::Cluster.start(nodes: 2) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?
6
6
  end
7
7
 
8
+ shutdown do
9
+ Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] and Elasticsearch::Extensions::Test::Cluster.running?
10
+ end
11
+
8
12
  context "Elasticsearch client" do
9
13
  teardown do
10
14
  begin; Object.send(:remove_const, :Typhoeus); rescue NameError; end
@@ -5,6 +5,10 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
5
5
  Elasticsearch::Extensions::Test::Cluster.start(nodes: 2) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?
6
6
  end
7
7
 
8
+ shutdown do
9
+ Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] and Elasticsearch::Extensions::Test::Cluster.running?
10
+ end
11
+
8
12
  context "Transport" do
9
13
  setup do
10
14
  @port = (ENV['TEST_CLUSTER_PORT'] || 9250).to_i
@@ -127,6 +127,17 @@ class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
127
127
  hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https' } )
128
128
  assert_equal 'myhost', hosts[0][:host]
129
129
  assert_equal 'https', hosts[0][:scheme]
130
+ assert_nil hosts[0][:port]
131
+ end
132
+
133
+ should "extract from hash with a port passed as a string" do
134
+ hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => '443' } )
135
+ assert_equal 443, hosts[0][:port]
136
+ end
137
+
138
+ should "extract from hash with a port passed as an integer" do
139
+ hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => 443 } )
140
+ assert_equal 443, hosts[0][:port]
130
141
  end
131
142
 
132
143
  should "extract from Hashie::Mash" do
@@ -154,10 +165,10 @@ class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
154
165
  assert_equal 2, hosts.size
155
166
 
156
167
  assert_equal 'host1', hosts[0][:host]
157
- assert_equal '1000', hosts[0][:port]
168
+ assert_equal 1000, hosts[0][:port]
158
169
 
159
170
  assert_equal 'host2', hosts[1][:host]
160
- assert_equal '2000', hosts[1][:port]
171
+ assert_equal 2000, hosts[1][:port]
161
172
  end
162
173
 
163
174
  should "extract path" do
@@ -171,7 +182,7 @@ class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
171
182
 
172
183
  assert_equal 'https', hosts[0][:scheme]
173
184
  assert_equal 'myhost', hosts[0][:host]
174
- assert_equal '8080', hosts[0][:port]
185
+ assert_equal 8080, hosts[0][:port]
175
186
  end
176
187
 
177
188
  should "extract credentials" do
@@ -181,14 +192,14 @@ class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
181
192
  assert_equal 'USERNAME', hosts[0][:user]
182
193
  assert_equal 'PASSWORD', hosts[0][:password]
183
194
  assert_equal 'myhost', hosts[0][:host]
184
- assert_equal '8080', hosts[0][:port]
195
+ assert_equal 8080, hosts[0][:port]
185
196
  end
186
197
 
187
198
  should "pass hashes over" do
188
199
  hosts = @client.__extract_hosts [{:host => 'myhost', :port => '1000', :foo => 'bar'}]
189
200
 
190
201
  assert_equal 'myhost', hosts[0][:host]
191
- assert_equal '1000', hosts[0][:port]
202
+ assert_equal 1000, hosts[0][:port]
192
203
  assert_equal 'bar', hosts[0][:foo]
193
204
  end
194
205
 
@@ -200,7 +211,7 @@ class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
200
211
  assert_equal 'USERNAME', hosts[0][:user]
201
212
  assert_equal 'PASSWORD', hosts[0][:password]
202
213
  assert_equal 'myhost', hosts[0][:host]
203
- assert_equal '4430', hosts[0][:port]
214
+ assert_equal 4430, hosts[0][:port]
204
215
  end
205
216
 
206
217
  should "split comma-separated URLs" do
@@ -21,7 +21,7 @@ class Elasticsearch::Transport::Transport::SnifferTest < Test::Unit::TestCase
21
21
  assert_equal @transport, @sniffer.transport
22
22
  end
23
23
 
24
- should "return an array of hosts as hashes" do
24
+ should "return an array of hosts as hashes with Elasticsearch 1.x syntax" do
25
25
  @transport.expects(:perform_request).returns __nodes_info <<-JSON
26
26
  {
27
27
  "ok" : true,
@@ -48,6 +48,33 @@ class Elasticsearch::Transport::Transport::SnifferTest < Test::Unit::TestCase
48
48
  assert_equal 'Node 1', hosts.first['name']
49
49
  end
50
50
 
51
+ should "return an array of hosts as hashes with Elasticsearch 2.0 syntax" do
52
+ @transport.expects(:perform_request).returns __nodes_info <<-JSON
53
+ {
54
+ "ok" : true,
55
+ "cluster_name" : "elasticsearch_test",
56
+ "nodes" : {
57
+ "N1" : {
58
+ "name" : "Node 1",
59
+ "transport_address" : "192.168.1.23:9300",
60
+ "hostname" : "testhost1",
61
+ "version" : "0.20.6",
62
+ "http_address" : "192.168.1.23:9200",
63
+ "thrift_address" : "192.168.1.23:9500",
64
+ "memcached_address" : "192.168.1.23:11211"
65
+ }
66
+ }
67
+ }
68
+ JSON
69
+
70
+ hosts = @sniffer.hosts
71
+
72
+ assert_equal 1, hosts.size
73
+ assert_equal '192.168.1.23', hosts.first[:host]
74
+ assert_equal '9200', hosts.first[:port]
75
+ assert_equal 'Node 1', hosts.first['name']
76
+ end
77
+
51
78
  should "skip hosts without a matching transport protocol" do
52
79
  @transport = DummyTransport.new :options => { :protocol => 'memcached' }
53
80
  @sniffer = Elasticsearch::Transport::Transport::Sniffer.new @transport
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.12
4
+ version: 1.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-02 00:00:00.000000000 Z
11
+ date: 2015-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json