elastic-transport 8.1.1 → 8.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +47 -7
- data/.gitignore +1 -1
- data/CHANGELOG.md +6 -5
- data/Gemfile +4 -8
- data/Gemfile-faraday1.gemfile +39 -0
- data/README.md +1 -1
- data/Rakefile +48 -1
- data/elastic-transport.gemspec +5 -8
- data/lib/elastic/transport/client.rb +12 -10
- data/lib/elastic/transport/meta_header.rb +8 -8
- data/lib/elastic/transport/transport/base.rb +53 -50
- data/lib/elastic/transport/transport/errors.rb +2 -4
- data/lib/elastic/transport/transport/http/curb.rb +28 -25
- data/lib/elastic/transport/transport/http/faraday.rb +29 -27
- data/lib/elastic/transport/transport/http/manticore.rb +1 -1
- data/lib/elastic/transport/transport/sniffer.rb +1 -3
- data/lib/elastic/transport/version.rb +1 -1
- data/spec/elastic/transport/base_spec.rb +4 -2
- data/spec/elastic/transport/client_spec.rb +13 -16
- data/spec/elastic/transport/meta_header_spec.rb +2 -2
- data/spec/elastic/transport/sniffer_spec.rb +0 -18
- data/spec/spec_helper.rb +4 -1
- data/test/integration/jruby_test.rb +1 -1
- data/test/integration/transport_test.rb +86 -40
- data/test/test_helper.rb +8 -5
- data/test/unit/adapters_test.rb +88 -0
- data/test/unit/transport_curb_test.rb +2 -3
- metadata +17 -70
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aca3fc15486eeb7455f903dbaa5b6a594d4fd410f895cff6b3bf832c0dcad8b4
|
4
|
+
data.tar.gz: 4fd798b71008e3bba436434a5c98321fcd2b15934a6244908966c8b9cdd5c3e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a33a557b17fc75fbc2cd9b8fb713603b67c35097fce929ccdee0b81b76c58c05885286aeb1aeed8c72068d6c12a83b51e6dc1f9f07678d6e17f51ce962cb0af8
|
7
|
+
data.tar.gz: 5105d1a01106314206e250a105329abfb9553f6c12170bafaf8b7eb4741f5d7b9b414027919aac9df9867ee0859fbfce6b492fbc64a8335f17eebcc7fd109cf4
|
data/.github/workflows/tests.yml
CHANGED
@@ -1,20 +1,22 @@
|
|
1
|
-
name:
|
1
|
+
name: main tests
|
2
2
|
on:
|
3
3
|
push:
|
4
4
|
branches:
|
5
|
-
-
|
5
|
+
- main
|
6
6
|
pull_request:
|
7
7
|
branches:
|
8
|
-
-
|
8
|
+
- main
|
9
9
|
jobs:
|
10
|
-
test
|
10
|
+
test:
|
11
|
+
name: 'Main tests'
|
11
12
|
env:
|
12
13
|
TEST_ES_SERVER: http://localhost:9250
|
13
14
|
PORT: 9250
|
14
15
|
strategy:
|
15
16
|
fail-fast: false
|
16
17
|
matrix:
|
17
|
-
ruby: [ '2.7', '3.0', '3.1', 'jruby-9.3', 'jruby-9.4' ]
|
18
|
+
ruby: [ '2.7', '3.0', '3.1', '3.2', 'jruby-9.3', 'jruby-9.4' ]
|
19
|
+
es_version: ['8.4-SNAPSHOT', '8.5-SNAPSHOT', '8.6-SNAPSHOT']
|
18
20
|
runs-on: ubuntu-latest
|
19
21
|
steps:
|
20
22
|
- uses: actions/checkout@v2
|
@@ -26,7 +28,7 @@ jobs:
|
|
26
28
|
sudo sysctl -w vm.max_map_count=262144
|
27
29
|
- uses: elastic/elastic-github-actions/elasticsearch@master
|
28
30
|
with:
|
29
|
-
stack-version:
|
31
|
+
stack-version: ${{ matrix.es_version }}
|
30
32
|
security-enabled: false
|
31
33
|
- uses: ruby/setup-ruby@v1
|
32
34
|
with:
|
@@ -43,4 +45,42 @@ jobs:
|
|
43
45
|
run: bundle exec rake test:spec
|
44
46
|
- name: integration tests
|
45
47
|
run: bundle exec rake test:integration
|
46
|
-
|
48
|
+
test-faraday1:
|
49
|
+
name: 'Test Faraday 1'
|
50
|
+
env:
|
51
|
+
TEST_ES_SERVER: http://localhost:9250
|
52
|
+
PORT: 9250
|
53
|
+
strategy:
|
54
|
+
fail-fast: false
|
55
|
+
matrix:
|
56
|
+
ruby: [ '2.7', '3.0', '3.1', '3.2', 'jruby-9.3' ]
|
57
|
+
es_version: ['8.6-SNAPSHOT']
|
58
|
+
runs-on: ubuntu-latest
|
59
|
+
steps:
|
60
|
+
- uses: actions/checkout@v2
|
61
|
+
- name: Increase system limits
|
62
|
+
run: |
|
63
|
+
sudo swapoff -a
|
64
|
+
sudo sysctl -w vm.swappiness=1
|
65
|
+
sudo sysctl -w fs.file-max=262144
|
66
|
+
sudo sysctl -w vm.max_map_count=262144
|
67
|
+
- uses: elastic/elastic-github-actions/elasticsearch@master
|
68
|
+
with:
|
69
|
+
stack-version: ${{ matrix.es_version }}
|
70
|
+
security-enabled: false
|
71
|
+
- uses: ruby/setup-ruby@v1
|
72
|
+
with:
|
73
|
+
ruby-version: ${{ matrix.ruby }}
|
74
|
+
- name: Build and test with Rake
|
75
|
+
run: |
|
76
|
+
sudo apt-get update
|
77
|
+
sudo apt-get install libcurl4-openssl-dev
|
78
|
+
ruby -v
|
79
|
+
gem install bundler
|
80
|
+
BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle install
|
81
|
+
- name: faraday1 unit tests
|
82
|
+
run: BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle exec rake test:faraday1:unit
|
83
|
+
- name: specs
|
84
|
+
run: BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle exec rake test:faraday1:spec
|
85
|
+
- name: integration tests
|
86
|
+
run: BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle exec rake test:faraday1:integration
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
## 8.
|
1
|
+
## 8.2.0
|
2
2
|
|
3
|
-
|
3
|
+
Tested versions of Ruby: (MRI) 2.7, 3.0, 3.1, 3.2, JRuby 9.3 and JRuby 9.4.
|
4
4
|
|
5
|
-
-
|
6
|
-
-
|
7
|
-
-
|
5
|
+
- Fixes [issue #44](https://github.com/elastic/elastic-transport-ruby/issues/44), raising `Elasticsearch::Transport::Transport::Error` instead of `Faraday::ConnectionFailed` (or any other Faraday error class) when a host is unreachable.
|
6
|
+
- Removes development dependency on `ansi`, it hasn't been updated for years.
|
7
|
+
- Adds `rake console` task to run IRB with the Elastic transport client libraries loaded.
|
8
|
+
- General refactors, cleanup and updates in code for `base.rb` (The base class used by HTTP implementations Faraday, Manticore, Curb), `errors.rb`, `faraday.rb`, and more.
|
8
9
|
|
9
10
|
## 8.1.0
|
10
11
|
|
data/Gemfile
CHANGED
@@ -20,15 +20,11 @@ source 'https://rubygems.org'
|
|
20
20
|
# Specify your gem's dependencies in elasticsearch-transport.gemspec
|
21
21
|
gemspec
|
22
22
|
|
23
|
-
if File.exist? File.expand_path('../elasticsearch-api/elasticsearch-api.gemspec', __dir__)
|
24
|
-
gem 'elasticsearch-api', path: File.expand_path('../elasticsearch-api', __dir__), require: false
|
25
|
-
end
|
26
|
-
|
27
|
-
if File.exist? File.expand_path('../elasticsearch/elasticsearch.gemspec', __dir__)
|
28
|
-
gem 'elasticsearch', path: File.expand_path('../elasticsearch', __dir__), require: false
|
29
|
-
end
|
30
|
-
|
31
23
|
group :development, :test do
|
24
|
+
gem 'faraday-httpclient'
|
25
|
+
gem 'faraday-net_http_persistent'
|
26
|
+
gem 'faraday-patron' unless defined? JRUBY_VERSION
|
27
|
+
gem 'faraday-typhoeus'
|
32
28
|
gem 'rspec'
|
33
29
|
if defined?(JRUBY_VERSION)
|
34
30
|
gem 'pry-nav'
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
source 'https://rubygems.org'
|
19
|
+
|
20
|
+
# Usage:
|
21
|
+
#
|
22
|
+
# $ BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle install
|
23
|
+
# $ BUNDLE_GEMFILE=./Gemfile-faraday1.gemfile bundle exec rake test:faraday1:unit
|
24
|
+
|
25
|
+
gem 'faraday', '~> 1'
|
26
|
+
gemspec path: './'
|
27
|
+
|
28
|
+
group :development, :test do
|
29
|
+
gem 'httpclient'
|
30
|
+
gem 'net-http-persistent'
|
31
|
+
gem 'patron' unless defined? JRUBY_VERSION
|
32
|
+
gem 'rspec'
|
33
|
+
gem 'typhoeus'
|
34
|
+
if defined?(JRUBY_VERSION)
|
35
|
+
gem 'pry-nav'
|
36
|
+
else
|
37
|
+
gem 'pry-byebug'
|
38
|
+
end
|
39
|
+
end
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ response = client.perform_request('GET', '_cluster/health')
|
|
15
15
|
|
16
16
|
**Refer to [the official documentation on Elastic Transport](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/transport.html).**
|
17
17
|
|
18
|
-
**Refer to [Advanced Configuration](https://
|
18
|
+
**Refer to [Advanced Configuration](https://elasticsearch-ruby_1881.docs-preview.app.elstc.co/guide/en/elasticsearch/client/ruby-api/master/advanced-config.html) to read about more configuration options.**
|
19
19
|
|
20
20
|
## Compatibility
|
21
21
|
|
data/Rakefile
CHANGED
@@ -18,13 +18,22 @@
|
|
18
18
|
require 'bundler/gem_tasks'
|
19
19
|
require 'mkmf'
|
20
20
|
|
21
|
-
desc
|
21
|
+
desc 'Run unit tests'
|
22
22
|
task default: 'test:unit'
|
23
23
|
task test: 'test:unit'
|
24
24
|
|
25
25
|
# ----- Test tasks ------------------------------------------------------------
|
26
26
|
require 'rake/testtask'
|
27
27
|
require 'rspec/core/rake_task'
|
28
|
+
FARADAY1_GEMFILE = 'Gemfile-faraday1.gemfile'.freeze
|
29
|
+
GEMFILES = ['Gemfile', FARADAY1_GEMFILE].freeze
|
30
|
+
|
31
|
+
task :install do
|
32
|
+
GEMFILES.each do |gemfile|
|
33
|
+
gemfile = File.expand_path("../#{gemfile}", __FILE__)
|
34
|
+
sh "bundle install --gemfile #{gemfile}"
|
35
|
+
end
|
36
|
+
end
|
28
37
|
|
29
38
|
namespace :test do
|
30
39
|
RSpec::Core::RakeTask.new(:spec)
|
@@ -46,6 +55,7 @@ namespace :test do
|
|
46
55
|
desc 'Run all tests'
|
47
56
|
task :all do
|
48
57
|
Rake::Task['test:unit'].invoke
|
58
|
+
Rake::Task['test:spec'].invoke
|
49
59
|
Rake::Task['test:integration'].invoke
|
50
60
|
end
|
51
61
|
|
@@ -53,6 +63,34 @@ namespace :test do
|
|
53
63
|
test.libs << 'lib' << 'test'
|
54
64
|
test.test_files = FileList['test/profile/**/*_test.rb']
|
55
65
|
end
|
66
|
+
|
67
|
+
namespace :faraday1 do
|
68
|
+
desc 'Faraday 1: Run RSpec with dependency on Faraday 1'
|
69
|
+
task :spec do
|
70
|
+
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} bundle exec rspec"
|
71
|
+
end
|
72
|
+
|
73
|
+
desc 'Faraday 1: Run unit tests with dependency on Faraday 1'
|
74
|
+
task :unit do
|
75
|
+
Dir.glob('./test/unit/**/**.rb').each do |test|
|
76
|
+
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} ruby -Ilib:test #{test}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
desc 'Faraday 1: Run integration tests with dependency on Faraday 1'
|
81
|
+
task :integration do
|
82
|
+
Dir.glob('./test/integration/**/**.rb').each do |test|
|
83
|
+
sh "BUNDLE_GEMFILE=#{FARADAY1_GEMFILE} ruby -Ilib:test #{test}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
desc 'Faraday 1: Run all tests'
|
88
|
+
task :all do
|
89
|
+
Rake::Task['test:faraday1:unit'].invoke
|
90
|
+
Rake::Task['test:faraday1:spec'].invoke
|
91
|
+
Rake::Task['test:faraday1:integration'].invoke
|
92
|
+
end
|
93
|
+
end
|
56
94
|
end
|
57
95
|
|
58
96
|
namespace :docker do
|
@@ -72,6 +110,15 @@ namespace :docker do
|
|
72
110
|
end
|
73
111
|
end
|
74
112
|
|
113
|
+
desc 'Run Ruby console with the Elastic transport client libraries loaded'
|
114
|
+
task :console do
|
115
|
+
require 'irb'
|
116
|
+
require 'irb/completion'
|
117
|
+
require 'elastic-transport'
|
118
|
+
ARGV.clear
|
119
|
+
IRB.start
|
120
|
+
end
|
121
|
+
|
75
122
|
# ----- Documentation tasks ---------------------------------------------------
|
76
123
|
require 'yard'
|
77
124
|
YARD::Rake::YardocTask.new(:doc) do |t|
|
data/elastic-transport.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.name = "elastic-transport"
|
25
25
|
s.version = Elastic::Transport::VERSION
|
26
26
|
s.authors = ['Karel Minarik', 'Emily Stolfo', 'Fernando Briano']
|
27
|
-
s.email = ['
|
27
|
+
s.email = ['support@elastic.co']
|
28
28
|
s.summary = 'Low level Ruby client for Elastic services.'
|
29
29
|
s.homepage = 'https://github.com/elastic/elastic-transport-ruby'
|
30
30
|
s.license = 'Apache-2.0'
|
@@ -44,20 +44,18 @@ Gem::Specification.new do |s|
|
|
44
44
|
|
45
45
|
s.required_ruby_version = '>= 2.5'
|
46
46
|
|
47
|
+
s.add_dependency 'faraday', '< 3'
|
47
48
|
s.add_dependency 'multi_json'
|
48
|
-
s.add_dependency 'faraday', '~> 1'
|
49
49
|
|
50
|
+
# Faraday Adapters
|
51
|
+
s.add_development_dependency 'manticore' if defined? JRUBY_VERSION
|
52
|
+
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
|
50
53
|
s.add_development_dependency 'bundler'
|
51
54
|
s.add_development_dependency 'cane'
|
52
|
-
s.add_development_dependency 'curb' unless defined? JRUBY_VERSION
|
53
55
|
s.add_development_dependency 'hashie'
|
54
|
-
s.add_development_dependency 'httpclient'
|
55
|
-
s.add_development_dependency 'manticore' if defined? JRUBY_VERSION
|
56
56
|
s.add_development_dependency 'minitest'
|
57
57
|
s.add_development_dependency 'minitest-reporters'
|
58
58
|
s.add_development_dependency 'mocha'
|
59
|
-
s.add_development_dependency 'net-http-persistent'
|
60
|
-
s.add_development_dependency 'patron' unless defined? JRUBY_VERSION
|
61
59
|
s.add_development_dependency 'pry'
|
62
60
|
s.add_development_dependency 'rake', '~> 13'
|
63
61
|
s.add_development_dependency 'require-prof' unless defined?(JRUBY_VERSION) || defined?(Rubinius)
|
@@ -65,7 +63,6 @@ Gem::Specification.new do |s|
|
|
65
63
|
s.add_development_dependency 'shoulda-context'
|
66
64
|
s.add_development_dependency 'simplecov'
|
67
65
|
s.add_development_dependency 'test-unit', '~> 2'
|
68
|
-
s.add_development_dependency 'typhoeus', '~> 1.4'
|
69
66
|
s.add_development_dependency 'yard'
|
70
67
|
|
71
68
|
s.description = <<-DESC.gsub(/^ /, '')
|
@@ -283,24 +283,26 @@ module Elastic
|
|
283
283
|
# Auto-detect the best adapter (HTTP "driver") available, based on libraries
|
284
284
|
# loaded by the user, preferring those with persistent connections
|
285
285
|
# ("keep-alive") by default
|
286
|
+
# Check adapters based on the usage of Faraday 1 or 2. Faraday should be defined here
|
287
|
+
# since this is only called when transport class is Transport::HTTP::Faraday
|
286
288
|
#
|
287
289
|
# @return [Symbol]
|
288
290
|
#
|
289
291
|
# @api private
|
290
292
|
#
|
291
293
|
def __auto_detect_adapter
|
292
|
-
|
293
|
-
|
294
|
-
:
|
295
|
-
|
296
|
-
:
|
297
|
-
when defined?(::HTTPClient)
|
298
|
-
:httpclient
|
299
|
-
when defined?(::Net::HTTP::Persistent)
|
300
|
-
:net_http_persistent
|
294
|
+
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new(2)
|
295
|
+
return :patron if defined?(Faraday::Adapter::Patron)
|
296
|
+
return :typhoeus if defined?(Faraday::Adapter::Typhoeus)
|
297
|
+
return :httpclient if defined?(Faraday::Adapter::HTTPClient)
|
298
|
+
return :net_http_persistent if defined?(Faraday::Adapter::NetHttpPersistent)
|
301
299
|
else
|
302
|
-
::
|
300
|
+
return :patron if defined?(::Patron)
|
301
|
+
return :typhoeus if defined?(::Typhoeus)
|
302
|
+
return :httpclient if defined?(::HTTPClient)
|
303
|
+
return :net_http_persistent if defined?(::Net::HTTP::Persistent)
|
303
304
|
end
|
305
|
+
::Faraday.default_adapter
|
304
306
|
end
|
305
307
|
end
|
306
308
|
end
|
@@ -103,31 +103,31 @@ module Elastic
|
|
103
103
|
adapter_version = case @arguments[:adapter]
|
104
104
|
when :patron
|
105
105
|
version = Patron::VERSION if defined?(::Patron::VERSION)
|
106
|
-
{pt: version}
|
106
|
+
{ pt: version }
|
107
107
|
when :net_http
|
108
108
|
version = if defined?(Net::HTTP::VERSION)
|
109
109
|
Net::HTTP::VERSION
|
110
110
|
elsif defined?(Net::HTTP::HTTPVersion)
|
111
111
|
Net::HTTP::HTTPVersion
|
112
112
|
end
|
113
|
-
{nh: version}
|
113
|
+
{ nh: version }
|
114
114
|
when :typhoeus
|
115
115
|
version = Typhoeus::VERSION if defined?(::Typhoeus::VERSION)
|
116
|
-
{ty: version}
|
116
|
+
{ ty: version }
|
117
117
|
when :httpclient
|
118
118
|
version = HTTPClient::VERSION if defined?(HTTPClient::VERSION)
|
119
|
-
{hc: version}
|
119
|
+
{ hc: version }
|
120
120
|
when :net_http_persistent
|
121
121
|
version = Net::HTTP::Persistent::VERSION if defined?(Net::HTTP::Persistent::VERSION)
|
122
|
-
{np: version}
|
122
|
+
{ np: version }
|
123
123
|
else
|
124
124
|
{}
|
125
125
|
end
|
126
|
-
{fd: Faraday::VERSION}.merge(adapter_version)
|
126
|
+
{ fd: Faraday::VERSION }.merge(adapter_version)
|
127
127
|
elsif defined?(Transport::HTTP::Curb) && @transport_class == Transport::HTTP::Curb
|
128
|
-
{cl: Curl::CURB_VERSION}
|
128
|
+
{ cl: Curl::CURB_VERSION }
|
129
129
|
elsif defined?(Transport::HTTP::Manticore) && @transport_class == Transport::HTTP::Manticore
|
130
|
-
{mc: Manticore::VERSION}
|
130
|
+
{ mc: Manticore::VERSION }
|
131
131
|
end
|
132
132
|
end
|
133
133
|
end
|
@@ -24,12 +24,12 @@ module Elastic
|
|
24
24
|
include Loggable
|
25
25
|
|
26
26
|
DEFAULT_PORT = 9200
|
27
|
-
DEFAULT_PROTOCOL = 'http'
|
27
|
+
DEFAULT_PROTOCOL = 'http'.freeze
|
28
28
|
DEFAULT_RELOAD_AFTER = 10_000 # Requests
|
29
29
|
DEFAULT_RESURRECT_AFTER = 60 # Seconds
|
30
30
|
DEFAULT_MAX_RETRIES = 3 # Requests
|
31
31
|
DEFAULT_SERIALIZER_CLASS = Serializer::MultiJson
|
32
|
-
SANITIZED_PASSWORD = '*' * (rand(14)+1)
|
32
|
+
SANITIZED_PASSWORD = '*' * (rand(14) + 1)
|
33
33
|
|
34
34
|
attr_reader :hosts, :options, :connections, :counter, :last_request_at, :protocol
|
35
35
|
attr_accessor :serializer, :sniffer, :logger, :tracer,
|
@@ -59,7 +59,7 @@ module Elastic
|
|
59
59
|
@compression = !!@options[:compression]
|
60
60
|
@connections = __build_connections
|
61
61
|
|
62
|
-
@serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self)
|
62
|
+
@serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self))
|
63
63
|
@protocol = options[:protocol] || DEFAULT_PROTOCOL
|
64
64
|
|
65
65
|
@logger = options[:logger]
|
@@ -72,7 +72,7 @@ module Elastic
|
|
72
72
|
@reload_connections = options[:reload_connections]
|
73
73
|
@reload_after = options[:reload_connections].is_a?(Integer) ? options[:reload_connections] : DEFAULT_RELOAD_AFTER
|
74
74
|
@resurrect_after = options[:resurrect_after] || DEFAULT_RESURRECT_AFTER
|
75
|
-
@retry_on_status = Array(options[:retry_on_status]).map
|
75
|
+
@retry_on_status = Array(options[:retry_on_status]).map(&:to_i)
|
76
76
|
end
|
77
77
|
|
78
78
|
# Returns a connection from the connection pool by delegating to {Connections::Collection#get_connection}.
|
@@ -87,7 +87,7 @@ module Elastic
|
|
87
87
|
resurrect_dead_connections! if Time.now > @last_request_at + @resurrect_after
|
88
88
|
|
89
89
|
@counter_mtx.synchronize { @counter += 1 }
|
90
|
-
reload_connections!
|
90
|
+
reload_connections! if reload_connections && (counter % reload_after).zero?
|
91
91
|
connections.get_connection(options)
|
92
92
|
end
|
93
93
|
|
@@ -97,10 +97,10 @@ module Elastic
|
|
97
97
|
#
|
98
98
|
def reload_connections!
|
99
99
|
hosts = sniffer.hosts
|
100
|
-
__rebuild_connections
|
100
|
+
__rebuild_connections(hosts: hosts, options: options)
|
101
101
|
self
|
102
102
|
rescue SnifferTimeoutError
|
103
|
-
log_error
|
103
|
+
log_error('[SnifferTimeoutError] Timeout when reloading connections.')
|
104
104
|
self
|
105
105
|
end
|
106
106
|
|
@@ -109,7 +109,7 @@ module Elastic
|
|
109
109
|
# @see Connections::Connection#resurrect!
|
110
110
|
#
|
111
111
|
def resurrect_dead_connections!
|
112
|
-
connections.dead.each
|
112
|
+
connections.dead.each(&:resurrect!)
|
113
113
|
end
|
114
114
|
|
115
115
|
# Rebuilds the connections collection in the transport.
|
@@ -128,7 +128,7 @@ module Elastic
|
|
128
128
|
__close_connections
|
129
129
|
|
130
130
|
new_connections = __build_connections
|
131
|
-
stale_connections = @connections.all.
|
131
|
+
stale_connections = @connections.all.reject { |c| new_connections.include?(c) }
|
132
132
|
new_connections = new_connections.reject { |c| @connections.all.include?(c) }
|
133
133
|
|
134
134
|
@connections.remove(stale_connections)
|
@@ -177,8 +177,8 @@ module Elastic
|
|
177
177
|
# @return [Connections::Connection]
|
178
178
|
# @api private
|
179
179
|
#
|
180
|
-
def __build_connection(host, options={}, block=nil)
|
181
|
-
raise NoMethodError,
|
180
|
+
def __build_connection(host, options = {}, block = nil)
|
181
|
+
raise NoMethodError, 'Implement this method in your class'
|
182
182
|
end
|
183
183
|
|
184
184
|
# Closes the connections collection
|
@@ -209,14 +209,14 @@ module Elastic
|
|
209
209
|
#
|
210
210
|
def __trace(method, path, params, headers, body, url, response, json, took, duration)
|
211
211
|
trace_url = "http://localhost:9200/#{path}?pretty" +
|
212
|
-
(
|
212
|
+
(params.empty? ? '' : "&#{::Faraday::Utils::ParamsHash[params].to_query}")
|
213
213
|
trace_body = body ? " -d '#{__convert_to_json(body, :pretty => true)}'" : ''
|
214
214
|
trace_command = "curl -X #{method.to_s.upcase}"
|
215
|
-
trace_command += " -H '#{headers.collect { |k,v| "#{k}: #{v}" }.join(", ")}'" if headers && !headers.empty?
|
215
|
+
trace_command += " -H '#{headers.collect { |k, v| "#{k}: #{v}" }.join(", ")}'" if headers && !headers.empty?
|
216
216
|
trace_command += " '#{trace_url}'#{trace_body}\n"
|
217
217
|
tracer.info trace_command
|
218
218
|
tracer.debug "# #{Time.now.iso8601} [#{response.status}] (#{format('%.3f', duration)}s)\n#"
|
219
|
-
tracer.debug json ? serializer.dump(json, :
|
219
|
+
tracer.debug json ? serializer.dump(json, pretty: true).gsub(/^/, '# ').sub(/\}$/, "\n# }")+"\n" : "# #{response.body}\n"
|
220
220
|
end
|
221
221
|
|
222
222
|
# Raise error specific for the HTTP response status or a generic server error
|
@@ -276,40 +276,37 @@ module Elastic
|
|
276
276
|
reload_on_failure = opts.fetch(:reload_on_failure, @options[:reload_on_failure])
|
277
277
|
delay_on_retry = opts.fetch(:delay_on_retry, @options[:delay_on_retry])
|
278
278
|
|
279
|
-
max_retries =
|
280
|
-
opts[:retry_on_failure] === true ? DEFAULT_MAX_RETRIES : opts[:retry_on_failure]
|
281
|
-
elsif options.key?(:retry_on_failure)
|
282
|
-
options[:retry_on_failure] === true ? DEFAULT_MAX_RETRIES : options[:retry_on_failure]
|
283
|
-
end
|
279
|
+
max_retries = max_retries(opts) || max_retries(options)
|
284
280
|
|
285
281
|
params = params.clone
|
286
|
-
|
282
|
+
# Transforms ignore status codes to Integer
|
283
|
+
ignore = Array(params.delete(:ignore)).compact.map(&:to_i)
|
287
284
|
|
288
285
|
begin
|
289
286
|
sleep(delay_on_retry / 1000.0) if tries > 0
|
290
|
-
tries
|
287
|
+
tries += 1
|
291
288
|
connection = get_connection or raise Error.new('Cannot get new connection from pool.')
|
292
289
|
|
293
|
-
if connection.connection.respond_to?(:params) &&
|
290
|
+
if connection.connection.respond_to?(:params) &&
|
291
|
+
connection.connection.params.respond_to?(:to_hash)
|
294
292
|
params = connection.connection.params.merge(params.to_hash)
|
295
293
|
end
|
296
294
|
|
297
|
-
url
|
295
|
+
url = connection.full_url(path, params)
|
298
296
|
response = block.call(connection, url)
|
299
|
-
connection.healthy! if connection.failures
|
297
|
+
connection.healthy! if connection.failures.positive?
|
300
298
|
|
301
299
|
# Raise an exception so we can catch it for `retry_on_status`
|
302
|
-
__raise_transport_error(response) if response.status.to_i >= 300 &&
|
300
|
+
__raise_transport_error(response) if response.status.to_i >= 300 &&
|
301
|
+
@retry_on_status.include?(response.status.to_i)
|
303
302
|
rescue Elastic::Transport::Transport::ServerError => e
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
log_fatal "[#{e.class}] Cannot get response from #{url} after #{tries} tries"
|
310
|
-
raise e
|
311
|
-
end
|
303
|
+
raise e unless response && @retry_on_status.include?(response.status)
|
304
|
+
|
305
|
+
log_warn "[#{e.class}] Attempt #{tries} to get response from #{url}"
|
306
|
+
if tries <= (max_retries || DEFAULT_MAX_RETRIES)
|
307
|
+
retry
|
312
308
|
else
|
309
|
+
log_fatal "[#{e.class}] Cannot get response from #{url} after #{tries} tries"
|
313
310
|
raise e
|
314
311
|
end
|
315
312
|
rescue *host_unreachable_exceptions => e
|
@@ -317,22 +314,20 @@ module Elastic
|
|
317
314
|
|
318
315
|
connection.dead!
|
319
316
|
|
320
|
-
if reload_on_failure
|
317
|
+
if reload_on_failure && tries < connections.all.size
|
321
318
|
log_warn "[#{e.class}] Reloading connections (attempt #{tries} of #{connections.all.size})"
|
322
319
|
reload_connections! and retry
|
323
320
|
end
|
324
321
|
|
325
322
|
exception = Elastic::Transport::Transport::Error.new(e.message)
|
326
323
|
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
log_fatal "[#{e.class}] Cannot connect to #{connection.host.inspect} after #{tries} tries"
|
333
|
-
raise exception
|
334
|
-
end
|
324
|
+
raise exception unless max_retries
|
325
|
+
|
326
|
+
log_warn "[#{e.class}] Attempt #{tries} connecting to #{connection.host.inspect}"
|
327
|
+
if tries <= max_retries
|
328
|
+
retry
|
335
329
|
else
|
330
|
+
log_fatal "[#{e.class}] Cannot connect to #{connection.host.inspect} after #{tries} tries"
|
336
331
|
raise exception
|
337
332
|
end
|
338
333
|
rescue Exception => e
|
@@ -351,8 +346,11 @@ module Elastic
|
|
351
346
|
__raise_transport_error response unless ignore.include?(response.status.to_i)
|
352
347
|
end
|
353
348
|
|
354
|
-
json
|
355
|
-
|
349
|
+
json = serializer.load(response.body) if response.body &&
|
350
|
+
!response.body.empty? &&
|
351
|
+
response.headers &&
|
352
|
+
response.headers["content-type"] =~ /json/
|
353
|
+
took = (json['took'] ? sprintf('%.3fs', json['took'] / 1000.0) : 'n/a') rescue 'n/a'
|
356
354
|
__log_response(method, path, params, body, url, response, json, took, duration) unless ignore.include?(response.status.to_i)
|
357
355
|
__trace(method, path, params, connection_headers(connection), body, url, response, nil, 'N/A', duration) if tracer
|
358
356
|
log_warn(response.headers['warning']) if response.headers&.[]('warning')
|
@@ -374,17 +372,21 @@ module Elastic
|
|
374
372
|
private
|
375
373
|
|
376
374
|
USER_AGENT_STR = 'User-Agent'.freeze
|
377
|
-
USER_AGENT_REGEX = /user
|
375
|
+
USER_AGENT_REGEX = /user-?_?agent/
|
378
376
|
ACCEPT_ENCODING = 'Accept-Encoding'.freeze
|
379
377
|
CONTENT_ENCODING = 'Content-Encoding'.freeze
|
380
378
|
CONTENT_TYPE_STR = 'Content-Type'.freeze
|
381
|
-
CONTENT_TYPE_REGEX = /content
|
379
|
+
CONTENT_TYPE_REGEX = /content-?_?type/
|
382
380
|
DEFAULT_CONTENT_TYPE = 'application/json'.freeze
|
383
381
|
GZIP = 'gzip'.freeze
|
384
382
|
GZIP_FIRST_TWO_BYTES = '1f8b'.freeze
|
385
383
|
HEX_STRING_DIRECTIVE = 'H*'.freeze
|
386
384
|
RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)
|
387
385
|
|
386
|
+
def max_retries(opts)
|
387
|
+
opts[:retry_on_failure] == true ? DEFAULT_MAX_RETRIES : opts[:retry_on_failure]
|
388
|
+
end
|
389
|
+
|
388
390
|
def compress_request(body, headers)
|
389
391
|
if body
|
390
392
|
headers ||= {}
|
@@ -411,7 +413,7 @@ module Elastic
|
|
411
413
|
|
412
414
|
io = StringIO.new(body)
|
413
415
|
gzip_reader = if RUBY_ENCODING
|
414
|
-
Zlib::GzipReader.new(io, :
|
416
|
+
Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT')
|
415
417
|
else
|
416
418
|
Zlib::GzipReader.new(io)
|
417
419
|
end
|
@@ -429,7 +431,7 @@ module Elastic
|
|
429
431
|
end
|
430
432
|
|
431
433
|
def apply_headers(client, options)
|
432
|
-
headers = options[:headers]
|
434
|
+
headers = options[:headers] || {}
|
433
435
|
headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
|
434
436
|
headers[USER_AGENT_STR] = find_value(headers, USER_AGENT_REGEX) || user_agent_header(client)
|
435
437
|
client.headers[ACCEPT_ENCODING] = GZIP if use_compression?
|
@@ -444,7 +446,7 @@ module Elastic
|
|
444
446
|
end
|
445
447
|
end
|
446
448
|
|
447
|
-
def user_agent_header(
|
449
|
+
def user_agent_header(_client)
|
448
450
|
@user_agent ||= begin
|
449
451
|
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
|
450
452
|
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
|
@@ -455,7 +457,8 @@ module Elastic
|
|
455
457
|
end
|
456
458
|
|
457
459
|
def connection_headers(connection)
|
458
|
-
if defined?(Elastic::Transport::Transport::HTTP::Manticore) &&
|
460
|
+
if defined?(Elastic::Transport::Transport::HTTP::Manticore) &&
|
461
|
+
instance_of?(Elastic::Transport::Transport::HTTP::Manticore)
|
459
462
|
@request_options[:headers]
|
460
463
|
else
|
461
464
|
connection.connection.headers
|