opensearch-transport 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +3 -0
- data/.gitignore +17 -0
- data/Gemfile +47 -0
- data/LICENSE +202 -0
- data/README.md +551 -0
- data/Rakefile +89 -0
- data/lib/opensearch/transport/client.rb +354 -0
- data/lib/opensearch/transport/redacted.rb +84 -0
- data/lib/opensearch/transport/transport/base.rb +450 -0
- data/lib/opensearch/transport/transport/connections/collection.rb +136 -0
- data/lib/opensearch/transport/transport/connections/connection.rb +169 -0
- data/lib/opensearch/transport/transport/connections/selector.rb +101 -0
- data/lib/opensearch/transport/transport/errors.rb +100 -0
- data/lib/opensearch/transport/transport/http/curb.rb +140 -0
- data/lib/opensearch/transport/transport/http/faraday.rb +101 -0
- data/lib/opensearch/transport/transport/http/manticore.rb +188 -0
- data/lib/opensearch/transport/transport/loggable.rb +94 -0
- data/lib/opensearch/transport/transport/response.rb +46 -0
- data/lib/opensearch/transport/transport/serializer/multi_json.rb +62 -0
- data/lib/opensearch/transport/transport/sniffer.rb +111 -0
- data/lib/opensearch/transport/version.rb +31 -0
- data/lib/opensearch/transport.rb +46 -0
- data/lib/opensearch-transport.rb +27 -0
- data/opensearch-transport.gemspec +92 -0
- data/spec/opensearch/connections/collection_spec.rb +275 -0
- data/spec/opensearch/connections/selector_spec.rb +183 -0
- data/spec/opensearch/transport/base_spec.rb +313 -0
- data/spec/opensearch/transport/client_spec.rb +1818 -0
- data/spec/opensearch/transport/sniffer_spec.rb +284 -0
- data/spec/spec_helper.rb +99 -0
- data/test/integration/transport_test.rb +108 -0
- data/test/profile/client_benchmark_test.rb +141 -0
- data/test/test_helper.rb +97 -0
- data/test/unit/connection_test.rb +145 -0
- data/test/unit/response_test.rb +41 -0
- data/test/unit/serializer_test.rb +42 -0
- data/test/unit/transport_base_test.rb +673 -0
- data/test/unit/transport_curb_test.rb +143 -0
- data/test/unit/transport_faraday_test.rb +237 -0
- data/test/unit/transport_manticore_test.rb +191 -0
- data.tar.gz.sig +1 -0
- metadata +456 -0
- metadata.gz.sig +1 -0
data/README.md
ADDED
@@ -0,0 +1,551 @@
|
|
1
|
+
# OpenSearch::Transport
|
2
|
+
|
3
|
+
**This library is part of the [`opensearch-ruby`](https://github.com/opensearch-project/opensearch-ruby/) package;
|
4
|
+
please refer to it, unless you want to use this library standalone.**
|
5
|
+
|
6
|
+
----
|
7
|
+
|
8
|
+
The `opensearch-transport` library provides a low-level Ruby client for connecting
|
9
|
+
to an [OpenSearch](http://opensearch.com) cluster.
|
10
|
+
|
11
|
+
It handles connecting to multiple nodes in the cluster, rotating across connections,
|
12
|
+
logging and tracing requests and responses, maintaining failed connections,
|
13
|
+
discovering nodes in the cluster, and provides an abstraction for
|
14
|
+
data serialization and transport.
|
15
|
+
|
16
|
+
It does not handle calling the OpenSearch API;
|
17
|
+
see the [`opensearch-api`](https://github.com/opensearch-project/opensearch-ruby/tree/main/opensearch-api) library.
|
18
|
+
|
19
|
+
The library is compatible with Ruby 1.9 or higher and with all versions of OpenSearch since 1.0.0.
|
20
|
+
|
21
|
+
Features overview:
|
22
|
+
|
23
|
+
* Pluggable logging and tracing
|
24
|
+
* Pluggable connection selection strategies (round-robin, random, custom)
|
25
|
+
* Pluggable transport implementation, customizable and extendable
|
26
|
+
* Pluggable serializer implementation
|
27
|
+
* Request retries and dead connections handling
|
28
|
+
* Node reloading (based on cluster state) on errors or on demand
|
29
|
+
|
30
|
+
For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections,
|
31
|
+
such as [patron](https://github.com/toland/patron) or [Typhoeus](https://github.com/typhoeus/typhoeus).
|
32
|
+
Just require the library (`require 'patron'`) in your code, and it will be automatically used.
|
33
|
+
|
34
|
+
Currently these libraries will be automatically detected and used:
|
35
|
+
- [Patron](https://github.com/toland/patron)
|
36
|
+
- [Typhoeus](https://github.com/typhoeus/typhoeus)
|
37
|
+
- [HTTPClient](https://rubygems.org/gems/httpclient)
|
38
|
+
- [Net::HTTP::Persistent](https://rubygems.org/gems/net-http-persistent)
|
39
|
+
|
40
|
+
**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.
|
41
|
+
|
42
|
+
For detailed information, see example configurations [below](#transport-implementations).
|
43
|
+
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
Install the package from [Rubygems](https://rubygems.org):
|
47
|
+
|
48
|
+
gem install opensearch-transport
|
49
|
+
|
50
|
+
To use an unreleased version, either add it to your `Gemfile` for [Bundler](http://gembundler.com):
|
51
|
+
|
52
|
+
gem 'opensearch-transport', git: 'git://github.com/opensearch-project/opensearch-ruby.git'
|
53
|
+
|
54
|
+
or install it from a source code checkout:
|
55
|
+
|
56
|
+
git clone https://github.com/opensearch-project/opensearch-ruby
|
57
|
+
cd opensearch-ruby/opensearch-transport
|
58
|
+
bundle install
|
59
|
+
rake install
|
60
|
+
|
61
|
+
## Example Usage
|
62
|
+
|
63
|
+
In the simplest form, connect to Elasticsearch running on <http://localhost:9200>
|
64
|
+
without any configuration:
|
65
|
+
|
66
|
+
require 'opensearch/transport'
|
67
|
+
|
68
|
+
client = OpenSearch::Client.new
|
69
|
+
response = client.perform_request 'GET', '_cluster/health'
|
70
|
+
# => #<OpenSearch::Transport::Transport::Response:0x007fc5d506ce38 @status=200, @body={ ... } >
|
71
|
+
|
72
|
+
## Configuration
|
73
|
+
|
74
|
+
* [Setting Hosts](#setting-hosts)
|
75
|
+
* [Default port](#default-port)
|
76
|
+
* [Authentication](#authentication)
|
77
|
+
* [Logging](#logging)
|
78
|
+
* [Custom HTTP Headers](#custom-http-headers)
|
79
|
+
* [Identifying running tasks with X-Opaque-Id](#identifying-running-tasks-with-x-opaque-id)
|
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
|
+
To connect to a specific OpenSearch host:
|
96
|
+
|
97
|
+
OpenSearch::Client.new host: 'search.myserver.com'
|
98
|
+
|
99
|
+
To connect to a host with specific port:
|
100
|
+
|
101
|
+
OpenSearch::Client.new host: 'myhost:8080'
|
102
|
+
|
103
|
+
To connect to multiple hosts:
|
104
|
+
|
105
|
+
OpenSearch::Client.new hosts: ['myhost1', 'myhost2']
|
106
|
+
|
107
|
+
Instead of Strings, you can pass host information as an array of Hashes:
|
108
|
+
|
109
|
+
OpenSearch::Client.new hosts: [ { host: 'myhost1', port: 8080 }, { host: 'myhost2', port: 8080 } ]
|
110
|
+
|
111
|
+
**NOTE:** When specifying multiple hosts, you probably want to enable the `retry_on_failure` option to
|
112
|
+
perform a failed request on another node (see the _Retrying on Failures_ chapter).
|
113
|
+
|
114
|
+
Common URL parts -- scheme, HTTP authentication credentials, URL prefixes, etc -- are handled automatically:
|
115
|
+
|
116
|
+
OpenSearch::Client.new url: 'https://username:password@api.server.org:4430/search'
|
117
|
+
|
118
|
+
You can pass multiple URLs separated by a comma:
|
119
|
+
|
120
|
+
OpenSearch::Client.new urls: 'http://localhost:9200,http://localhost:9201'
|
121
|
+
|
122
|
+
Another way to configure the URL(s) is to export the `OPENSEARCH_URL` variable.
|
123
|
+
|
124
|
+
The client will automatically round-robin across the hosts
|
125
|
+
(unless you select or implement a different [connection selector](#connection-selector)).
|
126
|
+
|
127
|
+
### Default port
|
128
|
+
|
129
|
+
The default port is `9200`. Please specify a port for your host(s) if they differ from this default.
|
130
|
+
|
131
|
+
### Authentication
|
132
|
+
|
133
|
+
You can pass the authentication credentials, scheme and port in the host configuration hash:
|
134
|
+
|
135
|
+
OpenSearch::Client.new hosts: [
|
136
|
+
{ host: 'my-protected-host',
|
137
|
+
port: '443',
|
138
|
+
user: 'USERNAME',
|
139
|
+
password: 'PASSWORD',
|
140
|
+
scheme: 'https'
|
141
|
+
} ]
|
142
|
+
|
143
|
+
... or simply use the common URL format:
|
144
|
+
|
145
|
+
OpenSearch::Client.new url: 'https://username:password@example.com:9200'
|
146
|
+
|
147
|
+
To pass a custom certificate for SSL peer verification to Faraday-based clients,
|
148
|
+
use the `transport_options` option:
|
149
|
+
|
150
|
+
OpenSearch::Client.new url: 'https://username:password@example.com:9200',
|
151
|
+
transport_options: { ssl: { ca_file: '/path/to/cacert.pem' } }
|
152
|
+
|
153
|
+
You can also use **API Key authentication**
|
154
|
+
|
155
|
+
``` ruby
|
156
|
+
OpenSearch::Client.new(
|
157
|
+
host: host,
|
158
|
+
transport_options: transport_options,
|
159
|
+
api_key: credentials
|
160
|
+
)
|
161
|
+
```
|
162
|
+
|
163
|
+
Where credentials is either the base64 encoding of `id` and `api_key` joined by a colon or a hash with the `id` and `api_key`:
|
164
|
+
|
165
|
+
``` ruby
|
166
|
+
OpenSearch::Client.new(
|
167
|
+
host: host,
|
168
|
+
transport_options: transport_options,
|
169
|
+
api_key: {id: 'my_id', api_key: 'my_api_key'}
|
170
|
+
)
|
171
|
+
```
|
172
|
+
|
173
|
+
### Logging
|
174
|
+
|
175
|
+
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:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
OpenSearch::Client.new(log: true)
|
179
|
+
```
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
logger = EcsLogging::Logger.new($stdout)
|
183
|
+
OpenSearch::Client.new(logger: logger)
|
184
|
+
```
|
185
|
+
|
186
|
+
|
187
|
+
To trace requests and responses in the _Curl_ format, set the `trace` argument:
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
OpenSearch::Client.new(trace: true)
|
191
|
+
```
|
192
|
+
|
193
|
+
You can customize the default logger or tracer:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
client.transport.logger.formatter = proc { |s, d, p, m| "#{s}: #{m}\n" }
|
197
|
+
client.transport.logger.level = Logger::INFO
|
198
|
+
```
|
199
|
+
|
200
|
+
Or, you can use a custom `::Logger` instance:
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
OpenSearch::Client.new(logger: Logger.new(STDERR))
|
204
|
+
```
|
205
|
+
|
206
|
+
You can pass the client any conforming logger implementation:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
require 'logging' # https://github.com/TwP/logging/
|
210
|
+
|
211
|
+
log = Logging.logger['opensearch']
|
212
|
+
log.add_appenders Logging.appenders.stdout
|
213
|
+
log.level = :info
|
214
|
+
|
215
|
+
client = OpenSearch::Client.new(logger: log)
|
216
|
+
```
|
217
|
+
|
218
|
+
### Custom HTTP Headers
|
219
|
+
|
220
|
+
You can set a custom HTTP header on the client's initializer:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
client = OpenSearch::Client.new(
|
224
|
+
transport_options: {
|
225
|
+
headers:
|
226
|
+
{user_agent: "My App"}
|
227
|
+
}
|
228
|
+
)
|
229
|
+
```
|
230
|
+
|
231
|
+
You can also pass in `headers` as a parameter to any of the API Endpoints to set custom headers for the request:
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
client.search(index: 'myindex', q: 'title:test', headers: {user_agent: "My App"})
|
235
|
+
```
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
client = OpenSearch::Client.new
|
239
|
+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
|
240
|
+
```
|
241
|
+
The search request will include the following HTTP Header:
|
242
|
+
```
|
243
|
+
X-Opaque-Id: 123456
|
244
|
+
```
|
245
|
+
|
246
|
+
You can also set a prefix for X-Opaque-Id when initializing the client. This will be prepended to the id you set before each request if you're using X-Opaque-Id. Example:
|
247
|
+
```ruby
|
248
|
+
client = OpenSearch::Client.new(opaque_id_prefix: 'eu-west1')
|
249
|
+
client.search(index: 'myindex', q: 'title:test', opaque_id: '123456')
|
250
|
+
```
|
251
|
+
The request will include the following HTTP Header:
|
252
|
+
```
|
253
|
+
X-Opaque-Id: eu-west1_123456
|
254
|
+
```
|
255
|
+
|
256
|
+
### Setting Timeouts
|
257
|
+
|
258
|
+
For many operations in Elasticsearch, the default timeouts of HTTP libraries are too low.
|
259
|
+
To increase the timeout, you can use the `request_timeout` parameter:
|
260
|
+
|
261
|
+
OpenSearch::Client.new request_timeout: 5*60
|
262
|
+
|
263
|
+
You can also use the `transport_options` argument documented below.
|
264
|
+
|
265
|
+
### Randomizing Hosts
|
266
|
+
|
267
|
+
If you pass multiple hosts to the client, it rotates across them in a round-robin fashion, by default.
|
268
|
+
When the same client would be running in multiple processes (eg. in a Ruby web server such as Thin),
|
269
|
+
it might keep connecting to the same nodes "at once". To prevent this, you can randomize the hosts
|
270
|
+
collection on initialization and reloading:
|
271
|
+
|
272
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], randomize_hosts: true
|
273
|
+
|
274
|
+
### Retrying on Failures
|
275
|
+
|
276
|
+
When the client is initialized with multiple hosts, it makes sense to retry a failed request
|
277
|
+
on a different host:
|
278
|
+
|
279
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], retry_on_failure: true
|
280
|
+
|
281
|
+
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`:
|
282
|
+
|
283
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], retry_on_failure: 5
|
284
|
+
|
285
|
+
These two parameters can also be used together:
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], retry_on_status: [502, 503], retry_on_failure: 10
|
289
|
+
```
|
290
|
+
|
291
|
+
### Reloading Hosts
|
292
|
+
|
293
|
+
OpenSearch by default dynamically discovers new nodes in the cluster. You can leverage this
|
294
|
+
in the client, and periodically check for new nodes to spread the load.
|
295
|
+
|
296
|
+
To retrieve and use the information from the
|
297
|
+
Nodes Info API
|
298
|
+
on every 10,000th request:
|
299
|
+
|
300
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], reload_connections: true
|
301
|
+
|
302
|
+
You can pass a specific number of requests after which the reloading should be performed:
|
303
|
+
|
304
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], reload_connections: 1_000
|
305
|
+
|
306
|
+
To reload connections on failures, use:
|
307
|
+
|
308
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], reload_on_failure: true
|
309
|
+
|
310
|
+
The reloading will timeout if not finished under 1 second by default. To change the setting:
|
311
|
+
|
312
|
+
OpenSearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], sniffer_timeout: 3
|
313
|
+
|
314
|
+
**NOTE:** When using reloading hosts ("sniffing") together with authentication, just pass the scheme,
|
315
|
+
user and password with the host info -- or, for more clarity, in the `http` options:
|
316
|
+
|
317
|
+
OpenSearch::Client.new host: 'localhost:9200',
|
318
|
+
http: { scheme: 'https', user: 'U', password: 'P' },
|
319
|
+
reload_connections: true,
|
320
|
+
reload_on_failure: true
|
321
|
+
|
322
|
+
### Connection Selector
|
323
|
+
|
324
|
+
By default, the client will rotate the connections in a round-robin fashion, using the
|
325
|
+
{OpenSearch::Transport::Transport::Connections::Selector::RoundRobin} strategy.
|
326
|
+
|
327
|
+
You can implement your own strategy to customize the behaviour. For example,
|
328
|
+
let's have a "rack aware" strategy, which will prefer the nodes with a specific
|
329
|
+
attribute
|
330
|
+
Only when these would be unavailable, the strategy will use the other nodes:
|
331
|
+
|
332
|
+
class RackIdSelector
|
333
|
+
include OpenSearch::Transport::Transport::Connections::Selector::Base
|
334
|
+
|
335
|
+
def select(options={})
|
336
|
+
connections.select do |c|
|
337
|
+
# Try selecting the nodes with a `rack_id:x1` attribute first
|
338
|
+
c.host[:attributes] && c.host[:attributes][:rack_id] == 'x1'
|
339
|
+
end.sample || connections.to_a.sample
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
OpenSearch::Client.new hosts: ['x1.search.org', 'x2.search.org'], selector_class: RackIdSelector
|
344
|
+
|
345
|
+
### Transport Implementations
|
346
|
+
|
347
|
+
By default, the client will use the [_Faraday_](https://rubygems.org/gems/faraday) HTTP library
|
348
|
+
as a transport implementation.
|
349
|
+
|
350
|
+
It will auto-detect and use an _adapter_ for _Faraday_ based on gems loaded in your code,
|
351
|
+
preferring HTTP clients with support for persistent connections.
|
352
|
+
|
353
|
+
To use the [_Patron_](https://github.com/toland/patron) HTTP, for example, just require it:
|
354
|
+
|
355
|
+
```ruby
|
356
|
+
require 'patron'
|
357
|
+
```
|
358
|
+
|
359
|
+
Then, create a new client, and the _Patron_ gem will be used as the "driver":
|
360
|
+
|
361
|
+
```ruby
|
362
|
+
client = OpenSearch::Client.new
|
363
|
+
|
364
|
+
client.transport.connections.first.connection.builder.adapter
|
365
|
+
# => Faraday::Adapter::Patron
|
366
|
+
|
367
|
+
10.times do
|
368
|
+
client.nodes.stats(metric: 'http')['nodes'].values.each do |n|
|
369
|
+
puts "#{n['name']} : #{n['http']['total_opened']}"
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
# => Stiletoo : 24
|
374
|
+
# => Stiletoo : 24
|
375
|
+
# => Stiletoo : 24
|
376
|
+
# => ...
|
377
|
+
```
|
378
|
+
|
379
|
+
To use a specific adapter for _Faraday_, pass it as the `adapter` argument:
|
380
|
+
|
381
|
+
client = OpenSearch::Client.new adapter: :net_http_persistent
|
382
|
+
|
383
|
+
client.transport.connections.first.connection.builder.handlers
|
384
|
+
# => [Faraday::Adapter::NetHttpPersistent]
|
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
|
+
client = OpenSearch::Client.new transport_options: {
|
391
|
+
request: { open_timeout: 1 },
|
392
|
+
headers: { user_agent: 'MyApp' },
|
393
|
+
params: { :format => 'yaml' },
|
394
|
+
ssl: { verify: false }
|
395
|
+
}
|
396
|
+
|
397
|
+
To configure the _Faraday_ instance directly, use a block:
|
398
|
+
|
399
|
+
require 'patron'
|
400
|
+
|
401
|
+
client = OpenSearch::Client.new(host: 'localhost', port: '9200') do |f|
|
402
|
+
f.response :logger
|
403
|
+
f.adapter :patron
|
404
|
+
end
|
405
|
+
|
406
|
+
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:
|
407
|
+
|
408
|
+
```ruby
|
409
|
+
require 'patron'
|
410
|
+
|
411
|
+
transport_configuration = lambda do |f|
|
412
|
+
f.response :logger
|
413
|
+
f.adapter :patron
|
414
|
+
end
|
415
|
+
|
416
|
+
transport = OpenSearch::Transport::Transport::HTTP::Faraday.new \
|
417
|
+
hosts: [ { host: 'localhost', port: '9200' } ],
|
418
|
+
&transport_configuration
|
419
|
+
|
420
|
+
# Pass the transport to the client
|
421
|
+
#
|
422
|
+
client = OpenSearch::Client.new transport: transport
|
423
|
+
```
|
424
|
+
|
425
|
+
|
426
|
+
Instead of passing the transport to the constructor, you can inject it at run time:
|
427
|
+
|
428
|
+
# Set up the transport
|
429
|
+
#
|
430
|
+
faraday_configuration = lambda do |f|
|
431
|
+
f.instance_variable_set :@ssl, { verify: false }
|
432
|
+
f.adapter :excon
|
433
|
+
end
|
434
|
+
|
435
|
+
faraday_client = OpenSearch::Transport::Transport::HTTP::Faraday.new \
|
436
|
+
hosts: [ { host: 'my-protected-host',
|
437
|
+
port: '443',
|
438
|
+
user: 'USERNAME',
|
439
|
+
password: 'PASSWORD',
|
440
|
+
scheme: 'https'
|
441
|
+
}],
|
442
|
+
&faraday_configuration
|
443
|
+
|
444
|
+
# Create a default client
|
445
|
+
#
|
446
|
+
client = OpenSearch::Client.new
|
447
|
+
|
448
|
+
# Inject the transport to the client
|
449
|
+
#
|
450
|
+
client.transport = faraday_client
|
451
|
+
|
452
|
+
You can also use a bundled [_Curb_](https://rubygems.org/gems/curb) based transport implementation:
|
453
|
+
|
454
|
+
require 'curb'
|
455
|
+
require 'opensearch/transport/transport/http/curb'
|
456
|
+
|
457
|
+
client = OpenSearch::Client.new transport_class: OpenSearch::Transport::Transport::HTTP::Curb
|
458
|
+
|
459
|
+
client.transport.connections.first.connection
|
460
|
+
# => #<Curl::Easy http://localhost:9200/>
|
461
|
+
|
462
|
+
It's possible to customize the _Curb_ instance by passing a block to the constructor as well
|
463
|
+
(in this case, as an inline block):
|
464
|
+
|
465
|
+
transport = OpenSearch::Transport::Transport::HTTP::Curb.new \
|
466
|
+
hosts: [ { host: 'localhost', port: '9200' } ],
|
467
|
+
& lambda { |c| c.verbose = true }
|
468
|
+
|
469
|
+
client = OpenSearch::Client.new transport: transport
|
470
|
+
|
471
|
+
You can write your own transport implementation easily, by including the
|
472
|
+
{OpenSearch::Transport::Transport::Base} module, implementing the required contract,
|
473
|
+
and passing it to the client as the `transport_class` parameter -- or injecting it directly.
|
474
|
+
|
475
|
+
### Serializer Implementations
|
476
|
+
|
477
|
+
By default, the [MultiJSON](http://rubygems.org/gems/multi_json) library is used as the
|
478
|
+
serializer implementation, and it will pick up the "right" adapter based on gems available.
|
479
|
+
|
480
|
+
The serialization component is pluggable, though, so you can write your own by including the
|
481
|
+
{OpenSearch::Transport::Transport::Serializer::Base} module, implementing the required contract,
|
482
|
+
and passing it to the client as the `serializer_class` or `serializer` parameter.
|
483
|
+
|
484
|
+
### Exception Handling
|
485
|
+
|
486
|
+
The library defines a [number of exception classes](https://github.com/opensearch-project/opensearch-ruby/blob/main/opensearch-transport/lib/opensearch/transport/transport/errors.rb)
|
487
|
+
for various client and server errors, as well as unsuccessful HTTP responses,
|
488
|
+
making it possible to `rescue` specific exceptions with desired granularity.
|
489
|
+
|
490
|
+
The highest-level exception is {OpenSearch::Transport::Transport::Error}
|
491
|
+
and will be raised for any generic client *or* server errors.
|
492
|
+
|
493
|
+
{OpenSearch::Transport::Transport::ServerError} will be raised for server errors only.
|
494
|
+
|
495
|
+
As an example for response-specific errors, a `404` response status will raise
|
496
|
+
an {OpenSearch::Transport::Transport::Errors::NotFound} exception.
|
497
|
+
|
498
|
+
Finally, {OpenSearch::Transport::Transport::SnifferTimeoutError} will be raised
|
499
|
+
when connection reloading ("sniffing") times out.
|
500
|
+
|
501
|
+
## Development and Community
|
502
|
+
|
503
|
+
For local development, clone the repository and run `bundle install`. See `rake -T` for a list of
|
504
|
+
available Rake tasks for running tests, generating documentation, starting a testing cluster, etc.
|
505
|
+
|
506
|
+
Bug fixes and features must be covered by unit tests. Integration tests are written in Ruby 1.9 syntax.
|
507
|
+
|
508
|
+
Github's pull requests and issues are used to communicate, send bug reports and code contributions.
|
509
|
+
|
510
|
+
## The Architecture
|
511
|
+
|
512
|
+
* {OpenSearch::Transport::Client} is composed of {OpenSearch::Transport::Transport}
|
513
|
+
|
514
|
+
* {OpenSearch::Transport::Transport} is composed of {OpenSearch::Transport::Transport::Connections},
|
515
|
+
and an instance of logger, tracer, serializer and sniffer.
|
516
|
+
|
517
|
+
* Logger and tracer can be any object conforming to Ruby logging interface,
|
518
|
+
ie. an instance of [`Logger`](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html),
|
519
|
+
[_log4r_](https://rubygems.org/gems/log4r), [_logging_](https://github.com/TwP/logging/), etc.
|
520
|
+
|
521
|
+
* The {OpenSearch::Transport::Transport::Serializer::Base} implementations handle converting data for OpenSearch
|
522
|
+
(eg. to JSON). You can implement your own serializer.
|
523
|
+
|
524
|
+
* {OpenSearch::Transport::Transport::Sniffer} allows to discover nodes in the cluster and use them as connections.
|
525
|
+
|
526
|
+
* {OpenSearch::Transport::Transport::Connections::Collection} is composed of
|
527
|
+
{OpenSearch::Transport::Transport::Connections::Connection} instances and a selector instance.
|
528
|
+
|
529
|
+
* {OpenSearch::Transport::Transport::Connections::Connection} contains the connection attributes such as hostname and port,
|
530
|
+
as well as the concrete persistent "session" connected to a specific node.
|
531
|
+
|
532
|
+
* The {OpenSearch::Transport::Transport::Connections::Selector::Base} implementations allow to choose connections
|
533
|
+
from the pool, eg. in a round-robin or random fashion. You can implement your own selector strategy.
|
534
|
+
|
535
|
+
## Development
|
536
|
+
|
537
|
+
To work on the code, clone and bootstrap the main repository first --
|
538
|
+
please see instructions in the main [README](../README.md#development).
|
539
|
+
|
540
|
+
To run tests, launch a testing cluster and use the Rake tasks:
|
541
|
+
|
542
|
+
```
|
543
|
+
time rake test:unit
|
544
|
+
time rake test:integration
|
545
|
+
```
|
546
|
+
|
547
|
+
Use `COVERAGE=true` before running a test task to check coverage with Simplecov.
|
548
|
+
|
549
|
+
## License
|
550
|
+
|
551
|
+
This software is licensed under the [Apache 2 license](./LICENSE).
|
data/Rakefile
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
2
|
+
#
|
3
|
+
# The OpenSearch Contributors require contributions made to
|
4
|
+
# this file be licensed under the Apache-2.0 license or a
|
5
|
+
# compatible open source license.
|
6
|
+
#
|
7
|
+
# Modifications Copyright OpenSearch Contributors. See
|
8
|
+
# GitHub history for details.
|
9
|
+
#
|
10
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
11
|
+
# license agreements. See the NOTICE file distributed with
|
12
|
+
# this work for additional information regarding copyright
|
13
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
14
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
15
|
+
# not use this file except in compliance with the License.
|
16
|
+
# You may obtain a copy of the License at
|
17
|
+
#
|
18
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
19
|
+
#
|
20
|
+
# Unless required by applicable law or agreed to in writing,
|
21
|
+
# software distributed under the License is distributed on an
|
22
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
23
|
+
# KIND, either express or implied. See the License for the
|
24
|
+
# specific language governing permissions and limitations
|
25
|
+
# under the License.
|
26
|
+
|
27
|
+
require "bundler/gem_tasks"
|
28
|
+
|
29
|
+
desc "Run unit tests"
|
30
|
+
task :default => 'test:unit'
|
31
|
+
task :test => 'test:unit'
|
32
|
+
|
33
|
+
# ----- Test tasks ------------------------------------------------------------
|
34
|
+
|
35
|
+
require 'rake/testtask'
|
36
|
+
require 'rspec/core/rake_task'
|
37
|
+
|
38
|
+
namespace :test do
|
39
|
+
desc 'Wait for OpenSearch to be in a green state'
|
40
|
+
task :wait_for_green do
|
41
|
+
sh '../scripts/wait-cluster.sh'
|
42
|
+
end
|
43
|
+
|
44
|
+
RSpec::Core::RakeTask.new(:spec)
|
45
|
+
|
46
|
+
Rake::TestTask.new(:unit) do |test|
|
47
|
+
test.libs << 'lib' << 'test'
|
48
|
+
test.test_files = FileList['test/unit/**/*_test.rb']
|
49
|
+
test.verbose = false
|
50
|
+
test.warning = false
|
51
|
+
end
|
52
|
+
|
53
|
+
Rake::TestTask.new(:integration) do |test|
|
54
|
+
test.libs << 'lib' << 'test'
|
55
|
+
test.test_files = FileList['test/integration/**/*_test.rb']
|
56
|
+
test.deps = ['test:wait_for_green', 'test:spec']
|
57
|
+
test.verbose = false
|
58
|
+
test.warning = false
|
59
|
+
end
|
60
|
+
|
61
|
+
desc 'Run all tests'
|
62
|
+
task :all do
|
63
|
+
Rake::Task['test:unit'].invoke
|
64
|
+
Rake::Task['test:integration'].invoke
|
65
|
+
end
|
66
|
+
|
67
|
+
Rake::TestTask.new(:profile) do |test|
|
68
|
+
test.libs << 'lib' << 'test'
|
69
|
+
test.test_files = FileList['test/profile/**/*_test.rb']
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
# ----- Documentation tasks ---------------------------------------------------
|
75
|
+
|
76
|
+
require 'yard'
|
77
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
78
|
+
t.options = %w| --embed-mixins --markup=markdown |
|
79
|
+
end
|
80
|
+
|
81
|
+
# ----- Code analysis tasks ---------------------------------------------------
|
82
|
+
|
83
|
+
if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
|
84
|
+
require 'cane/rake_task'
|
85
|
+
Cane::RakeTask.new(:quality) do |cane|
|
86
|
+
cane.abc_max = 15
|
87
|
+
cane.no_style = true
|
88
|
+
end
|
89
|
+
end
|