elastic-transport 8.1.0.pre2 → 8.1.1
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 +7 -47
- data/.gitignore +1 -1
- data/CHANGELOG.md +8 -4
- data/Gemfile +8 -4
- data/README.md +7 -504
- data/Rakefile +1 -39
- data/elastic-transport.gemspec +8 -5
- data/lib/elastic/transport/client.rb +10 -12
- data/lib/elastic/transport/meta_header.rb +8 -8
- data/lib/elastic/transport/transport/base.rb +5 -3
- data/lib/elastic/transport/transport/http/curb.rb +25 -28
- data/lib/elastic/transport/transport/http/manticore.rb +1 -1
- data/lib/elastic/transport/transport/response.rb +3 -2
- data/lib/elastic/transport/transport/sniffer.rb +3 -1
- data/lib/elastic/transport/version.rb +1 -1
- data/spec/elastic/transport/base_spec.rb +24 -27
- data/spec/elastic/transport/client_spec.rb +7 -14
- data/spec/elastic/transport/meta_header_spec.rb +2 -2
- data/spec/elastic/transport/sniffer_spec.rb +18 -0
- data/spec/spec_helper.rb +0 -4
- data/test/integration/jruby_test.rb +1 -1
- data/test/integration/transport_test.rb +40 -86
- data/test/test_helper.rb +5 -9
- data/test/unit/transport_base_test.rb +7 -8
- data/test/unit/transport_curb_test.rb +3 -2
- metadata +69 -16
- data/Gemfile-faraday1.gemfile +0 -39
- data/test/unit/adapters_test.rb +0 -88
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c45579654af5263b19d9ed8a814727d7d63fa3c4ecb3e1313adcffe6b5f9e185
|
4
|
+
data.tar.gz: 278b4e2bf242cf1acf9cea3f9bb297b45a5517c3f9f38921484913e337afa4c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '078d4316c2ea7222c02905522389c47494c049a35b0b924d76afeeb340517d3f85fc6824294870dc7399884581233c1564424cae33f6381d21476a4f39758b91'
|
7
|
+
data.tar.gz: bd074dd0d1eeca808efe02e48bfc40232d12b7923e077352e1dc72749e030a85c9b16112d836506fce7cef076dfedd7d3d5f44659598b68ebb7fc00bf68f4a83
|
data/.github/workflows/tests.yml
CHANGED
@@ -1,22 +1,20 @@
|
|
1
|
-
name:
|
1
|
+
name: 8.1 tests
|
2
2
|
on:
|
3
3
|
push:
|
4
4
|
branches:
|
5
|
-
-
|
5
|
+
- 8.1
|
6
6
|
pull_request:
|
7
7
|
branches:
|
8
|
-
-
|
8
|
+
- 8.1
|
9
9
|
jobs:
|
10
|
-
test:
|
11
|
-
name: 'Main tests'
|
10
|
+
test-ruby:
|
12
11
|
env:
|
13
12
|
TEST_ES_SERVER: http://localhost:9250
|
14
13
|
PORT: 9250
|
15
14
|
strategy:
|
16
15
|
fail-fast: false
|
17
16
|
matrix:
|
18
|
-
ruby: [ '2.7', '3.0', '3.1', 'jruby-9.3' ]
|
19
|
-
es_version: ['8.2-SNAPSHOT', '8.3-SNAPSHOT', '8.4-SNAPSHOT']
|
17
|
+
ruby: [ '2.7', '3.0', '3.1', 'jruby-9.3', 'jruby-9.4' ]
|
20
18
|
runs-on: ubuntu-latest
|
21
19
|
steps:
|
22
20
|
- uses: actions/checkout@v2
|
@@ -28,7 +26,7 @@ jobs:
|
|
28
26
|
sudo sysctl -w vm.max_map_count=262144
|
29
27
|
- uses: elastic/elastic-github-actions/elasticsearch@master
|
30
28
|
with:
|
31
|
-
stack-version:
|
29
|
+
stack-version: 8.9-SNAPSHOT
|
32
30
|
security-enabled: false
|
33
31
|
- uses: ruby/setup-ruby@v1
|
34
32
|
with:
|
@@ -45,42 +43,4 @@ jobs:
|
|
45
43
|
run: bundle exec rake test:spec
|
46
44
|
- name: integration tests
|
47
45
|
run: bundle exec rake test:integration
|
48
|
-
|
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', 'jruby-9.3' ]
|
57
|
-
es_version: ['8.3.0-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
|
46
|
+
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,14 +1,18 @@
|
|
1
|
-
## 8.1.
|
1
|
+
## 8.1.1
|
2
2
|
|
3
|
-
|
3
|
+
Backports the following updates from 8.2:
|
4
4
|
|
5
|
-
|
5
|
+
- Refactors `apply_headers` in base and manticore implementation: When passing in an object to the initializer, `apply_headers` would mutate this object and in certain conditions, this would raise `RuntimeError` in JRuby 9.3 and `ConcurrencyError` in JRuby 9.4. This update clones the options object instead.
|
6
|
+
- Fixes [issue #44](https://github.com/elastic/elastic-transport-ruby/issues/44), raising `Elastic::Transport::Transport::Error` instead of `Faraday::ConnectionFailed` (or any other Faraday error class) when a host is unreachable.
|
7
|
+
- Fixes parsing ipv4 addresses like `inet[/127.0.0.1:9200]` in sniffer, [issue #48](https://github.com/elastic/elastic-transport-ruby/issues/48). [Pull Request](https://github.com/elastic/elastic-transport-ruby/pull/49) by [@robbat2](https://github.com/robbat2), thank you!
|
8
|
+
|
9
|
+
## 8.1.0
|
6
10
|
|
7
11
|
Adds support for Faraday version 2. From [Faraday's Upgrading guide](https://github.com/lostisland/faraday/blob/main/UPGRADING.md#faraday-20), the main change is the adapters have moved:
|
8
12
|
|
9
13
|
> With this release, we've officially moved all adapters, except for the net_http one, out of Faraday. What that means, is that they won't be available out-of-the-box anymore, and you'll instead need to add them to your Gemfile.
|
10
14
|
> If you just use the default net_http adapter, then you don't need to do anything!
|
11
|
-
> Otherwise, add the corresponding adapter gem to your Gemfile (e.g. faraday-net_http_persistent). Then, simply require them after you require faraday.
|
15
|
+
> Otherwise, add the corresponding adapter gem to your Gemfile (e.g. faraday-net_http_persistent). Then, simply require them after you require faraday.
|
12
16
|
|
13
17
|
We're now supporting Faraday v2 and Faraday v1. The adapters were removed as development dependency in the gemspec and added to the Gemfile. A new file `Gemfile-faraday1.gemfile` was added to run tests with version `1.x` of Faraday too.
|
14
18
|
|
data/Gemfile
CHANGED
@@ -20,11 +20,15 @@ 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
|
+
|
23
31
|
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'
|
28
32
|
gem 'rspec'
|
29
33
|
if defined?(JRUBY_VERSION)
|
30
34
|
gem 'pry-nav'
|
data/README.md
CHANGED
@@ -3,62 +3,7 @@
|
|
3
3
|
|
4
4
|
This gem provides a low-level Ruby client for connecting to an [Elastic](http://elastic.co) cluster. It powers both the [Elasticsearch client](https://github.com/elasticsearch/elasticsearch-ruby/) and the [Elastic Enterprise Search](https://github.com/elastic/enterprise-search-ruby/) client.
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
This gem is compatible with maintained Ruby versions. See [Ruby Maintenance Branches](https://www.ruby-lang.org/en/downloads/branches/). We don't provide support to versions which have reached their end of life.
|
9
|
-
|
10
|
-
## Installation
|
11
|
-
|
12
|
-
Install the package from [Rubygems](https://rubygems.org):
|
13
|
-
|
14
|
-
gem install elastic-transport
|
15
|
-
|
16
|
-
To use an unreleased version, either add it to your `Gemfile` for [Bundler](http://gembundler.com):
|
17
|
-
|
18
|
-
gem 'elastic-transport', git: 'git://github.com/elastic/elastic-transport-ruby.git'
|
19
|
-
|
20
|
-
or install it from a source code checkout:
|
21
|
-
|
22
|
-
```bash
|
23
|
-
git clone https://github.com/elastic/elastic-transport-ruby.git
|
24
|
-
cd elastic-transport-ruby
|
25
|
-
bundle install
|
26
|
-
rake install
|
27
|
-
```
|
28
|
-
|
29
|
-
## Description
|
30
|
-
|
31
|
-
It handles connecting to multiple nodes in the cluster, rotating across connections, logging and tracing requests and responses, maintaining failed connections, discovering nodes in the cluster, and provides an abstraction for
|
32
|
-
data serialization and transport.
|
33
|
-
|
34
|
-
It does not handle calling the Elasticsearch API; see the [`elasticsearch`](https://github.com/elasticsearch/elasticsearch-ruby) library for that.
|
35
|
-
|
36
|
-
Features overview:
|
37
|
-
|
38
|
-
* Pluggable logging and tracing
|
39
|
-
* Pluggable connection selection strategies (round-robin, random, custom)
|
40
|
-
* Pluggable transport implementation, customizable and extendable
|
41
|
-
* Pluggable serializer implementation
|
42
|
-
* Request retries and dead connections handling
|
43
|
-
* Node reloading (based on cluster state) on errors or on demand
|
44
|
-
|
45
|
-
For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections, such as [patron](https://github.com/toland/patron) or [Typhoeus](https://github.com/typhoeus/typhoeus).
|
46
|
-
Just require the library (`require 'patron'`) in your code, and it will be automatically used.
|
47
|
-
|
48
|
-
Currently these libraries will be automatically detected and used:
|
49
|
-
- [Patron](https://github.com/toland/patron)
|
50
|
-
- [Typhoeus](https://github.com/typhoeus/typhoeus)
|
51
|
-
- [HTTPClient](https://rubygems.org/gems/httpclient)
|
52
|
-
- [Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent)
|
53
|
-
|
54
|
-
**Note on [Typhoeus](https://github.com/typhoeus/typhoeus)**: You need to use v1.4.0 or up since older versions are not compatible with Faraday 1.0.
|
55
|
-
|
56
|
-
For detailed information, see example configurations [below](#transport-implementations).
|
57
|
-
|
58
|
-
## Example Usage
|
59
|
-
|
60
|
-
In the simplest form, connect to Elasticsearch running on <http://localhost:9200>
|
61
|
-
without any configuration:
|
6
|
+
In the simplest form, connect to Elasticsearch running on `http://localhost:9200` without any configuration:
|
62
7
|
|
63
8
|
```ruby
|
64
9
|
require 'elastic/transport'
|
@@ -68,442 +13,13 @@ response = client.perform_request('GET', '_cluster/health')
|
|
68
13
|
# => #<Elastic::Transport::Transport::Response:0x007fc5d506ce38 @status=200, @body={ ... } >
|
69
14
|
```
|
70
15
|
|
71
|
-
|
72
|
-
|
73
|
-
## Configuration
|
74
|
-
|
75
|
-
* [Setting Hosts](#setting-hosts)
|
76
|
-
* [Default port](#default-port)
|
77
|
-
* [Authentication](#authentication)
|
78
|
-
* [Logging](#logging)
|
79
|
-
* [Custom HTTP Headers](#custom-http-headers)
|
80
|
-
* [Setting Timeouts](#setting-timeouts)
|
81
|
-
* [Randomizing Hosts](#randomizing-hosts)
|
82
|
-
* [Retrying on Failures](#retrying-on-failures)
|
83
|
-
* [Reloading Hosts](#reloading-hosts)
|
84
|
-
* [Connection Selector](#connection-selector)
|
85
|
-
* [Transport Implementations](#transport-implementations)
|
86
|
-
* [Serializer implementations](#serializer-implementations)
|
87
|
-
* [Exception Handling](#exception-handling)
|
88
|
-
* [Development and Community](#development-and-community)
|
89
|
-
|
90
|
-
The client supports many configurations options for setting up and managing connections,
|
91
|
-
configuring logging, customizing the transport library, etc.
|
92
|
-
|
93
|
-
### Setting Hosts
|
94
|
-
|
95
|
-
This behaviour is going to be simplified, see [#5](https://github.com/elastic/elastic-transport-ruby/issues/5). To connect to a specific Elasticsearch host:
|
96
|
-
|
97
|
-
```ruby
|
98
|
-
Elastic::Transport::Client.new(host: 'search.myserver.com')
|
99
|
-
```
|
100
|
-
|
101
|
-
To connect to a host with specific port:
|
102
|
-
|
103
|
-
```ruby
|
104
|
-
Elastic::Transport::Client.new(host: 'myhost:8080')
|
105
|
-
```
|
106
|
-
|
107
|
-
To connect to multiple hosts:
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
Elastic::Transport::Client.new(hosts: ['myhost1', 'myhost2'])
|
111
|
-
```
|
112
|
-
|
113
|
-
Instead of Strings, you can pass host information as an array of Hashes:
|
114
|
-
|
115
|
-
```ruby
|
116
|
-
Elastic::Transport::Client.new(hosts: [{ host: 'myhost1', port: 8080 }, { host: 'myhost2', port: 8080 }])
|
117
|
-
```
|
118
|
-
|
119
|
-
**NOTE:** When specifying multiple hosts, you probably want to enable the `retry_on_failure` or `retry_on_status` options to perform a failed request on another node (see the _Retrying on Failures_ chapter).
|
120
|
-
|
121
|
-
Common URL parts -- scheme, HTTP authentication credentials, URL prefixes, etc -- are handled automatically:
|
122
|
-
```ruby
|
123
|
-
Elastic::Transport::Client.new(url: 'https://username:password@api.server.org:4430/search')
|
124
|
-
```
|
125
|
-
|
126
|
-
You can pass multiple URLs separated by a comma:
|
127
|
-
```ruby
|
128
|
-
Elastic::Transport::Client.new(urls: 'http://localhost:9200,http://localhost:9201')
|
129
|
-
```
|
130
|
-
|
131
|
-
Another way to configure the URL(s) is to export the `ELASTICSEARCH_URL` variable.
|
132
|
-
|
133
|
-
The client will automatically round-robin across the hosts (unless you select or implement a different [connection selector](#connection-selector)).
|
134
|
-
|
135
|
-
### Default port
|
136
|
-
|
137
|
-
The default port is `9200`. Please specify a port for your host(s) if they differ from this default. Please see below for an exception to this when connecting using an Elastic Cloud ID.
|
138
|
-
|
139
|
-
### Authentication
|
140
|
-
|
141
|
-
You can pass the authentication credentials, scheme and port in the host configuration hash:
|
142
|
-
|
143
|
-
```ruby
|
144
|
-
Elastic::Transport::Client.new(
|
145
|
-
hosts: [
|
146
|
-
{
|
147
|
-
host: 'my-protected-host',
|
148
|
-
port: '443',
|
149
|
-
user: 'USERNAME',
|
150
|
-
password: 'PASSWORD',
|
151
|
-
scheme: 'https'
|
152
|
-
}
|
153
|
-
]
|
154
|
-
)
|
155
|
-
```
|
156
|
-
Or use the common URL format:
|
157
|
-
|
158
|
-
```ruby
|
159
|
-
Elastic::Transport::Client.new(url: 'https://username:password@example.com:9200')
|
160
|
-
```
|
161
|
-
|
162
|
-
To pass a custom certificate for SSL peer verification to Faraday-based clients, use the `transport_options` option:
|
163
|
-
|
164
|
-
```ruby
|
165
|
-
Elastic::Transport::Client.new(
|
166
|
-
url: 'https://username:password@example.com:9200',
|
167
|
-
transport_options: { ssl: { ca_file: '/path/to/cacert.pem' } }
|
168
|
-
)
|
169
|
-
```
|
170
|
-
|
171
|
-
### Logging
|
172
|
-
|
173
|
-
To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class), set the `log` argument to true:
|
174
|
-
|
175
|
-
```ruby
|
176
|
-
Elastic::Transport::Client.new(log: true)
|
177
|
-
```
|
178
|
-
|
179
|
-
You can also use [ecs-logging](https://github.com/elastic/ecs-logging-ruby). `ecs-logging` is a set of libraries that allows you to transform your application logs to structured logs that comply with the [Elastic Common Schema (ECS)](https://www.elastic.co/guide/en/ecs/current/ecs-reference.html):
|
180
|
-
|
181
|
-
```ruby
|
182
|
-
logger = EcsLogging::Logger.new($stdout)
|
183
|
-
Elastic::Transport::Client.new(logger: logger)
|
184
|
-
```
|
185
|
-
|
186
|
-
To trace requests and responses in the _Curl_ format, set the `trace` argument:
|
187
|
-
|
188
|
-
```ruby
|
189
|
-
Elastic::Transport::Client.new(trace: true)
|
190
|
-
```
|
191
|
-
|
192
|
-
You can customize the default logger or tracer:
|
193
|
-
|
194
|
-
```ruby
|
195
|
-
client.transport.logger.formatter = proc { |s, d, p, m| "#{s}: #{m}\n" }
|
196
|
-
client.transport.logger.level = Logger::INFO
|
197
|
-
```
|
198
|
-
|
199
|
-
Or, you can use a custom `::Logger` instance:
|
200
|
-
|
201
|
-
```ruby
|
202
|
-
Elastic::Transport::Client.new(logger: Logger.new(STDERR))
|
203
|
-
```
|
204
|
-
|
205
|
-
You can pass the client any conforming logger implementation:
|
206
|
-
|
207
|
-
```ruby
|
208
|
-
require 'logging' # https://github.com/TwP/logging/
|
209
|
-
|
210
|
-
log = Logging.logger['elasticsearch']
|
211
|
-
log.add_appenders Logging.appenders.stdout
|
212
|
-
log.level = :info
|
213
|
-
|
214
|
-
client = Elastic::Transport::Client.new(logger: log)
|
215
|
-
```
|
216
|
-
|
217
|
-
### Custom HTTP Headers
|
218
|
-
|
219
|
-
You can set a custom HTTP header on the client's initializer:
|
220
|
-
|
221
|
-
```ruby
|
222
|
-
client = Elastic::Transport::Client.new(
|
223
|
-
transport_options: {
|
224
|
-
headers:
|
225
|
-
{user_agent: "My App"}
|
226
|
-
}
|
227
|
-
)
|
228
|
-
```
|
229
|
-
|
230
|
-
You can also pass in `headers` as a parameter to any of the API Endpoints to set custom headers for the request:
|
231
|
-
|
232
|
-
```ruby
|
233
|
-
client.search(index: 'myindex', q: 'title:test', headers: { user_agent: "My App" })
|
234
|
-
```
|
16
|
+
**Refer to [the official documentation on Elastic Transport](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/transport.html).**
|
235
17
|
|
236
|
-
|
18
|
+
**Refer to [Advanced Configuration](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/advanced-config.html) to read about more configuration options.**
|
237
19
|
|
238
|
-
|
239
|
-
To increase the timeout, you can use the `request_timeout` parameter:
|
240
|
-
|
241
|
-
```ruby
|
242
|
-
Elastic::Transport::Client.new(request_timeout: 5 * 60)
|
243
|
-
```
|
244
|
-
|
245
|
-
You can also use the `transport_options` argument documented below.
|
246
|
-
|
247
|
-
### Randomizing Hosts
|
248
|
-
|
249
|
-
If you pass multiple hosts to the client, it rotates across them in a round-robin fashion, by default.
|
250
|
-
When the same client would be running in multiple processes (eg. in a Ruby web server such as Thin),
|
251
|
-
it might keep connecting to the same nodes "at once". To prevent this, you can randomize the hosts
|
252
|
-
collection on initialization and reloading:
|
253
|
-
|
254
|
-
```ruby
|
255
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], randomize_hosts: true)
|
256
|
-
```
|
257
|
-
|
258
|
-
### Retrying on Failures
|
259
|
-
|
260
|
-
When the client is initialized with multiple hosts, it makes sense to retry a failed request
|
261
|
-
on a different host:
|
262
|
-
|
263
|
-
```ruby
|
264
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_failure: true)
|
265
|
-
```
|
266
|
-
|
267
|
-
By default, the client will retry the request 3 times. You can specify how many times to retry before it raises an exception by passing a number to `retry_on_failure`:
|
268
|
-
|
269
|
-
```ruby
|
270
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_failure: 5)
|
271
|
-
```
|
272
|
-
|
273
|
-
You can also use `retry_on_status` to retry when specific status codes are returned:
|
274
|
-
|
275
|
-
```ruby
|
276
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_status: [502, 503])
|
277
|
-
```
|
278
|
-
|
279
|
-
These two parameters can also be used together:
|
280
|
-
|
281
|
-
```ruby
|
282
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], retry_on_status: [502, 503], retry_on_failure: 10)
|
283
|
-
```
|
284
|
-
|
285
|
-
### Reloading Hosts
|
286
|
-
|
287
|
-
Elasticsearch by default dynamically discovers new nodes in the cluster. You can leverage this in the client, and periodically check for new nodes to spread the load.
|
288
|
-
|
289
|
-
To retrieve and use the information from the [_Nodes Info API_](http://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html) on every 10,000th request:
|
290
|
-
|
291
|
-
```ruby
|
292
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], reload_connections: true)
|
293
|
-
```
|
294
|
-
|
295
|
-
You can pass a specific number of requests after which the reloading should be performed:
|
296
|
-
|
297
|
-
```ruby
|
298
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], reload_connections: 1_000)
|
299
|
-
```
|
300
|
-
|
301
|
-
To reload connections on failures, use:
|
302
|
-
|
303
|
-
```ruby
|
304
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], reload_on_failure: true)
|
305
|
-
```
|
306
|
-
|
307
|
-
The reloading will timeout if not finished under 1 second by default. To change the setting:
|
308
|
-
|
309
|
-
```ruby
|
310
|
-
Elastic::Transport::Client.new(hosts: ['localhost:9200', 'localhost:9201'], sniffer_timeout: 3)
|
311
|
-
```
|
312
|
-
|
313
|
-
**NOTE:** When using reloading hosts ("sniffing") together with authentication, just pass the scheme, user and password with the host info -- or, for more clarity, in the `http` options:
|
314
|
-
|
315
|
-
```ruby
|
316
|
-
Elastic::Transport::Client.new(
|
317
|
-
host: 'localhost:9200',
|
318
|
-
http: { scheme: 'https', user: 'U', password: 'P' },
|
319
|
-
reload_connections: true,
|
320
|
-
reload_on_failure: true
|
321
|
-
)
|
322
|
-
```
|
323
|
-
|
324
|
-
### Connection Selector
|
325
|
-
|
326
|
-
By default, the client will rotate the connections in a round-robin fashion, using the {Elastic::Transport::Transport::Connections::Selector::RoundRobin} strategy.
|
327
|
-
|
328
|
-
You can implement your own strategy to customize the behaviour. For example, let's have a "rack aware" strategy, which will prefer the nodes with a specific [attribute](https://github.com/elasticsearch/elasticsearch/blob/1.0/config/elasticsearch.yml#L81-L85). Only when these would be unavailable, the strategy will use the other nodes:
|
329
|
-
|
330
|
-
```ruby
|
331
|
-
class RackIdSelector
|
332
|
-
include Elastic::Transport::Transport::Connections::Selector::Base
|
333
|
-
|
334
|
-
def select(options={})
|
335
|
-
connections.select do |c|
|
336
|
-
# Try selecting the nodes with a `rack_id:x1` attribute first
|
337
|
-
c.host[:attributes] && c.host[:attributes][:rack_id] == 'x1'
|
338
|
-
end.sample || connections.to_a.sample
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
Elastic::Transport::Client.new hosts: ['x1.search.org', 'x2.search.org'], selector_class: RackIdSelector
|
343
|
-
```
|
344
|
-
|
345
|
-
### Transport Implementations
|
346
|
-
|
347
|
-
By default, the client will use the [_Faraday_](https://rubygems.org/gems/faraday) HTTP library as a transport implementation.
|
348
|
-
|
349
|
-
It will auto-detect and use an _adapter_ for _Faraday_ based on gems loaded in your code, preferring HTTP clients with support for persistent connections.
|
350
|
-
|
351
|
-
To use the [_Patron_](https://github.com/toland/patron) HTTP, for example, just require it:
|
352
|
-
|
353
|
-
```ruby
|
354
|
-
require 'patron'
|
355
|
-
```
|
356
|
-
|
357
|
-
Then, create a new client, and the _Patron_ gem will be used as the "driver":
|
358
|
-
|
359
|
-
```ruby
|
360
|
-
client = Elastic::Transport::Client.new
|
361
|
-
|
362
|
-
client.transport.connections.first.connection.builder.adapter
|
363
|
-
# => Faraday::Adapter::Patron
|
364
|
-
|
365
|
-
10.times do
|
366
|
-
client.nodes.stats(metric: 'http')['nodes'].values.each do |n|
|
367
|
-
puts "#{n['name']} : #{n['http']['total_opened']}"
|
368
|
-
end
|
369
|
-
end
|
370
|
-
|
371
|
-
# => Stiletoo : 24
|
372
|
-
# => Stiletoo : 24
|
373
|
-
# => Stiletoo : 24
|
374
|
-
# => ...
|
375
|
-
```
|
376
|
-
|
377
|
-
To use a specific adapter for _Faraday_, pass it as the `adapter` argument:
|
378
|
-
|
379
|
-
```ruby
|
380
|
-
client = Elastic::Transport::Client.new(adapter: :net_http_persistent)
|
381
|
-
|
382
|
-
client.transport.connections.first.connection.builder.handlers
|
383
|
-
# => [Faraday::Adapter::NetHttpPersistent]
|
384
|
-
```
|
385
|
-
|
386
|
-
To pass options to the
|
387
|
-
[`Faraday::Connection`](https://github.com/lostisland/faraday/blob/master/lib/faraday/connection.rb)
|
388
|
-
constructor, use the `transport_options` key:
|
389
|
-
|
390
|
-
```ruby
|
391
|
-
client = Elastic::Transport::Client.new(
|
392
|
-
transport_options: {
|
393
|
-
request: { open_timeout: 1 },
|
394
|
-
headers: { user_agent: 'MyApp' },
|
395
|
-
params: { :format => 'yaml' },
|
396
|
-
ssl: { verify: false }
|
397
|
-
}
|
398
|
-
)
|
399
|
-
```
|
400
|
-
|
401
|
-
To configure the _Faraday_ instance directly, use a block:
|
402
|
-
|
403
|
-
```ruby
|
404
|
-
require 'patron'
|
405
|
-
|
406
|
-
client = Elastic::Transport::Client.new(host: 'localhost', port: '9200') do |f|
|
407
|
-
f.response :logger
|
408
|
-
f.adapter :patron
|
409
|
-
end
|
410
|
-
```
|
411
|
-
|
412
|
-
You can use any standard Faraday middleware and plugins in the configuration block. You can also initialize the transport class yourself, and pass it to the client constructor as the `transport` argument:
|
413
|
-
|
414
|
-
```ruby
|
415
|
-
require 'patron'
|
416
|
-
|
417
|
-
transport_configuration = lambda do |f|
|
418
|
-
f.response :logger
|
419
|
-
f.adapter :patron
|
420
|
-
end
|
421
|
-
|
422
|
-
transport = Elastic::Transport::Transport::HTTP::Faraday.new(
|
423
|
-
hosts: [ { host: 'localhost', port: '9200' } ],
|
424
|
-
&transport_configuration
|
425
|
-
)
|
426
|
-
|
427
|
-
# Pass the transport to the client
|
428
|
-
#
|
429
|
-
client = Elastic::Transport::Client.new(transport: transport)
|
430
|
-
```
|
431
|
-
|
432
|
-
Instead of passing the transport to the constructor, you can inject it at run time:
|
433
|
-
|
434
|
-
```ruby
|
435
|
-
# Set up the transport
|
436
|
-
#
|
437
|
-
faraday_configuration = lambda do |f|
|
438
|
-
f.instance_variable_set :@ssl, { verify: false }
|
439
|
-
f.adapter :excon
|
440
|
-
end
|
441
|
-
|
442
|
-
faraday_client = Elastic::Transport::Transport::HTTP::Faraday.new(
|
443
|
-
hosts: [
|
444
|
-
{
|
445
|
-
host: 'my-protected-host',
|
446
|
-
port: '443',
|
447
|
-
user: 'USERNAME',
|
448
|
-
password: 'PASSWORD',
|
449
|
-
scheme: 'https'
|
450
|
-
}
|
451
|
-
],
|
452
|
-
&faraday_configuration
|
453
|
-
)
|
454
|
-
|
455
|
-
# Create a default client
|
456
|
-
#
|
457
|
-
client = Elastic::Transport::Client.new
|
458
|
-
|
459
|
-
# Inject the transport to the client
|
460
|
-
#
|
461
|
-
client.transport = faraday_client
|
462
|
-
```
|
463
|
-
|
464
|
-
You can also use a bundled [_Curb_](https://rubygems.org/gems/curb) based transport implementation:
|
465
|
-
|
466
|
-
```ruby
|
467
|
-
require 'curb'
|
468
|
-
require 'elastic/transport/transport/http/curb'
|
469
|
-
|
470
|
-
client = Elastic::Transport::Client.new(transport_class: Elastic::Transport::Transport::HTTP::Curb)
|
471
|
-
|
472
|
-
client.transport.connections.first.connection
|
473
|
-
# => #<Curl::Easy http://localhost:9200/>
|
474
|
-
```
|
475
|
-
|
476
|
-
It's possible to customize the _Curb_ instance by passing a block to the constructor as well (in this case, as an inline block):
|
477
|
-
|
478
|
-
```ruby
|
479
|
-
transport = Elastic::Transport::Transport::HTTP::Curb.new(
|
480
|
-
hosts: [ { host: 'localhost', port: '9200' } ],
|
481
|
-
& lambda { |c| c.verbose = true }
|
482
|
-
)
|
483
|
-
|
484
|
-
client = Elastic::Transport::Client.new(transport: transport)
|
485
|
-
```
|
486
|
-
|
487
|
-
You can write your own transport implementation by including the `Elastic::Transport::Transport::Base` module, implementing the required contract, and passing it to the client as the `transport_class` parameter -- or injecting it directly.
|
488
|
-
|
489
|
-
### Serializer Implementations
|
490
|
-
|
491
|
-
By default, the [MultiJSON](http://rubygems.org/gems/multi_json) library is used as the serializer implementation, and it will pick up the "right" adapter based on gems available.
|
492
|
-
|
493
|
-
The serialization component is pluggable, though, so you can write your own by including the `Elastic::Transport::Transport::Serializer::Base` module, implementing the required contract, and passing it to the client as the `serializer_class` or `serializer` parameter.
|
494
|
-
|
495
|
-
### Exception Handling
|
496
|
-
|
497
|
-
The library defines a [number of exception classes](https://github.com/elastic/elastic-transport-ruby/blob/main/lib/elastic/transport/transport/errors.rb) for various client and server errors, as well as unsuccessful HTTP responses,
|
498
|
-
making it possible to `rescue` specific exceptions with desired granularity.
|
499
|
-
|
500
|
-
The highest-level exception is `Elastic::Transport::Transport::Error` and will be raised for any generic client *or* server errors.
|
501
|
-
|
502
|
-
`Elastic::Transport::Transport::ServerError` will be raised for server errors only.
|
503
|
-
|
504
|
-
As an example for response-specific errors, a `404` response status will raise an `Elastic::Transport::Transport::Errors::NotFound` exception.
|
20
|
+
## Compatibility
|
505
21
|
|
506
|
-
|
22
|
+
This gem is compatible with maintained Ruby versions. See [Ruby Maintenance Branches](https://www.ruby-lang.org/en/downloads/branches/). We don't provide support to versions which have reached their end of life.
|
507
23
|
|
508
24
|
## Development and Community
|
509
25
|
|
@@ -511,21 +27,6 @@ For local development, clone the repository and run `bundle install`. See `rake
|
|
511
27
|
|
512
28
|
Bug fixes and features must be covered by unit tests.
|
513
29
|
|
514
|
-
Github's pull requests and issues are used to communicate, send bug reports and code contributions.
|
515
|
-
|
516
|
-
## The Architecture
|
517
|
-
|
518
|
-
* `Elastic::Transport::Client` is composed of `Elastic::Transport::Transport`.
|
519
|
-
* `Elastic::Transport::Transport` is composed of `Elastic::Transport::Transport::Connections`, and an instance of logger, tracer, serializer and sniffer.
|
520
|
-
* Logger and tracer can be any object conforming to the Ruby logging interface, ie. an instance of [`Logger`](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html), [_log4r_](https://rubygems.org/gems/log4r), [_logging_](https://github.com/TwP/logging/), etc.
|
521
|
-
* The `Elastic::Transport::Transport::Serializer::Base` implementations handles converting data for Elasticsearch (eg. to JSON). You can implement your own serializer.
|
522
|
-
* `Elastic::Transport::Transport::Sniffer` allows discovering nodes in the cluster and use them as connections.
|
523
|
-
* `Elastic::Transport::Transport::Connections::Collection` is composed of `Elastic::Transport::Transport::Connections::Connection` instances and a selector instance.
|
524
|
-
* `Elastic::Transport::Transport::Connections::Connection` contains the connection attributes such as hostname and port, as well as the concrete persistent "session" connected to a specific node.
|
525
|
-
* The `Elastic::Transport::Transport::Connections::Selector::Base` implementations allows you to choose connections from the pool, eg. in a round-robin or random fashion. You can implement your own selector strategy.
|
526
|
-
|
527
|
-
## Development
|
528
|
-
|
529
30
|
A rake task is included to launch an Elasticsearch cluster with Docker. You need to install docker on your system and then run:
|
530
31
|
```bash
|
531
32
|
$ rake docker:start[VERSION]
|
@@ -547,6 +48,8 @@ time rake test:integration
|
|
547
48
|
|
548
49
|
Use `COVERAGE=true` before running a test task to check coverage with Simplecov.
|
549
50
|
|
51
|
+
Github's pull requests and issues are used to communicate, send bug reports and code contributions.
|
52
|
+
|
550
53
|
## License
|
551
54
|
|
552
55
|
This software is licensed under the [Apache 2 license](./LICENSE).
|