Skip to content

Commit 7bb2816

Browse files
authored
Clean up HTTP feature support (#9)
1 parent 204cae8 commit 7bb2816

23 files changed

+220
-1890
lines changed

docs/guide/configuration.asciidoc

Lines changed: 20 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,47 @@
44
This page contains information about the most important configuration options of
55
the Python {es} client.
66

7-
87
[discrete]
98
[[tls-and-ssl]]
109
=== TLS/SSL
1110

12-
The options in this section can only be used when the node is configured for HTTPS. An error will be raised if using these options with an HTTP node.
11+
The options in this section will only be necessary when connecting to Elasticsearch Serverless via a proxy not managed by Elastic that uses its own certificates.
1312

1413
[discrete]
15-
==== Verifying server certificates
14+
==== Verifying certificates
1615

17-
The typical route to verify a cluster certificate is via a "CA bundle" which can be specified via the `ca_certs` parameter. If no options are given and the https://github.com/certifi/python-certifi[certifi package] is installed then certifi's CA bundle is used by default.
16+
The typical route to verify a certificate is via a "CA bundle" which can be specified via the `ca_certs` parameter. If no options are given and the https://github.com/certifi/python-certifi[certifi package] is installed then certifi's CA bundle is used by default.
1817

1918
If you have your own CA bundle to use you can configure via the `ca_certs` parameter:
2019

2120
[source,python]
2221
------------------------------------
2322
es = Elasticsearch(
24-
"https://...",
23+
cloud_id='project-name:ABCD...',
2524
ca_certs="/path/to/certs.pem"
2625
)
2726
------------------------------------
2827

2928
If using a generated certificate or certificate with a known fingerprint you can use the `ssl_assert_fingerprint` to specify the fingerprint which tries to match the server's leaf certificate during the TLS handshake. If there is any matching certificate the connection is verified, otherwise a `TlsError` is raised.
3029

31-
In Python 3.9 and earlier only the leaf certificate will be verified but in Python 3.10+ private APIs are used to verify any certificate in the certificate chain. This helps when using certificates that are generated on a multi-node cluster.
30+
In Python 3.9 and earlier only the leaf certificate will be verified but in Python 3.10+ private APIs are used to verify any certificate in the certificate chain.
3231

3332
[source,python]
3433
------------------------------------
3534
es = Elasticsearch(
36-
"https://...",
35+
cloud_id='project-name:ABCD...',
3736
ssl_assert_fingerprint=(
3837
"315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3"
3938
)
4039
)
4140
------------------------------------
4241

43-
To disable certificate verification use the `verify_certs=False` parameter. This option should be avoided in production, instead use the other options to verify the clusters' certificate.
42+
To disable certificate verification use the `verify_certs=False` parameter. This option should be avoided in production, instead use the other options to verify the certificate.
4443

4544
[source,python]
4645
------------------------------------
4746
es = Elasticsearch(
48-
"https://...",
47+
cloud_id='project-name:ABCD...',
4948
verify_certs=False
5049
)
5150
------------------------------------
@@ -58,7 +57,6 @@ Configuring the minimum TLS version to connect to is done via the `ssl_version`
5857
[source,python]
5958
------------------------------------
6059
import ssl
61-
6260
es = Elasticsearch(
6361
...,
6462
ssl_version=ssl.TLSVersion.TLSv1_2
@@ -88,43 +86,38 @@ For advanced users an `ssl.SSLContext` object can be used for configuring TLS vi
8886
[source,python]
8987
------------------------------------
9088
import ssl
91-
9289
# Create and configure an SSLContext
9390
ctx = ssl.create_default_context()
9491
ctx.load_verify_locations(...)
95-
9692
es = Elasticsearch(
9793
...,
9894
ssl_context=ctx
9995
)
10096
------------------------------------
10197

102-
10398
[discrete]
10499
[[compression]]
105100
=== HTTP compression
106101

107102
Compression of HTTP request and response bodies can be enabled with the `http_compress` parameter.
108103
If enabled then HTTP request bodies will be compressed with `gzip` and HTTP responses will include
109-
the `Accept-Encoding: gzip` HTTP header. By default compression is disabled.
104+
the `Accept-Encoding: gzip` HTTP header. HTTP compression is recommended for all Serverless requests, and is enabled by default.
105+
106+
To disable:
110107

111108
[source,python]
112109
------------------------------------
113110
es = Elasticsearch(
114111
...,
115-
http_compress=True # Enable compression!
112+
http_compress=False
116113
)
117114
------------------------------------
118115

119-
HTTP compression is recommended to be enabled when requests are traversing the network.
120-
Compression is automatically enabled when connecting to Elastic Cloud.
121-
122-
123116
[discrete]
124117
[[timeouts]]
125118
=== Request timeouts
126119

127-
Requests can be configured to timeout if taking too long to be serviced. The `request_timeout` parameter can be passed via the client constructor or the client `.options()` method. When the request times out the node will raise a `ConnectionTimeout` exception which can trigger retries.
120+
Requests can be configured to timeout if taking too long to be serviced. The `request_timeout` parameter can be passed via the client constructor or the client `.options()` method. When the request times out the project will raise a `ConnectionTimeout` exception which can trigger retries.
128121

129122
Setting `request_timeout` to `None` will disable timeouts.
130123

@@ -159,12 +152,11 @@ es.options(
159152
)
160153
------------------------------------
161154

162-
163155
[discrete]
164156
[[retries]]
165157
=== Retries
166158

167-
Requests can be retried if they don't return with a successful response. This provides a way for requests to be resilient against transient failures or overloaded nodes.
159+
Requests can be retried if they don't return with a successful response. This provides a way for requests to be resilient against transient failures.
168160

169161
The maximum number of retries per request can be configured via the `max_retries` parameter. Setting this parameter to 0 disables retries. This parameter can be set in the client constructor or per-request via the client `.options()` method:
170162

@@ -246,115 +238,6 @@ resp.meta.status # Can be either '2XX' or '400'
246238

247239
When using the `ignore_status` parameter the error response will be returned serialized just like a non-error response. In these cases it can be useful to inspect the HTTP status of the response. To do this you can inspect the `resp.meta.status`.
248240

249-
[discrete]
250-
[[sniffing]]
251-
=== Sniffing for new nodes
252-
253-
Additional nodes can be discovered by a process called "sniffing" where the client will query the cluster for more nodes that can handle requests.
254-
255-
Sniffing can happen at three different times: on client instantiation, before requests, and on a node failure. These three behaviors can be enabled and disabled with the `sniff_on_start`, `sniff_before_requests`, and `sniff_on_node_failure` parameters.
256-
257-
IMPORTANT: When using an HTTP load balancer or proxy you cannot use sniffing functionality as the cluster would supply the client with IP addresses to directly connect to the cluster, circumventing the load balancer. Depending on your configuration this might be something you don't want or break completely.
258-
259-
[discrete]
260-
==== Waiting between sniffing attempts
261-
262-
To avoid needlessly sniffing too often there is a delay between attempts to discover new nodes. This value can be controlled via the `min_delay_between_sniffing` parameter.
263-
264-
[discrete]
265-
==== Filtering nodes which are sniffed
266-
267-
By default nodes which are marked with only a `master` role will not be used. To change the behavior the parameter `sniffed_node_callback` can be used. To mark a sniffed node not to be added to the node pool
268-
return `None` from the `sniffed_node_callback`, otherwise return a `NodeConfig` instance.
269-
270-
[source,python]
271-
------------------------------------
272-
from typing import Optional, Dict, Any
273-
from elastic_transport import NodeConfig
274-
from elasticsearch import Elasticsearch
275-
276-
def filter_master_eligible_nodes(
277-
node_info: Dict[str, Any],
278-
node_config: NodeConfig
279-
) -> Optional[NodeConfig]:
280-
# This callback ignores all nodes that are master eligible
281-
# instead of master-only nodes (default behavior)
282-
if "master" in node_info.get("roles", ()):
283-
return None
284-
return node_config
285-
286-
client = Elasticsearch(
287-
"https://localhost:9200",
288-
sniffed_node_callback=filter_master_eligible_nodes
289-
)
290-
------------------------------------
291-
292-
The `node_info` parameter is part of the response from the `nodes.info()` API, below is an example
293-
of what that object looks like:
294-
295-
[source,json]
296-
------------------------------------
297-
{
298-
"name": "SRZpKFZ",
299-
"transport_address": "127.0.0.1:9300",
300-
"host": "127.0.0.1",
301-
"ip": "127.0.0.1",
302-
"version": "5.0.0",
303-
"build_hash": "253032b",
304-
"roles": ["master", "data", "ingest"],
305-
"http": {
306-
"bound_address": ["[fe80::1]:9200", "[::1]:9200", "127.0.0.1:9200"],
307-
"publish_address": "1.1.1.1:123",
308-
"max_content_length_in_bytes": 104857600
309-
}
310-
}
311-
------------------------------------
312-
313-
314-
[discrete]
315-
[[node-pool]]
316-
=== Node Pool
317-
318-
[discrete]
319-
==== Selecting a node from the pool
320-
321-
You can specify a node selector pattern via the `node_selector_class` parameter. The supported values are `round_robin` and `random`. Default is `round_robin`.
322-
323-
[source,python]
324-
------------------------------------
325-
es = Elasticsearch(
326-
...,
327-
node_selector_class="round_robin"
328-
)
329-
------------------------------------
330-
331-
Custom selectors are also supported:
332-
333-
[source,python]
334-
------------------------------------
335-
from elastic_transport import NodeSelector
336-
337-
class CustomSelector(NodeSelector):
338-
def select(nodes): ...
339-
340-
es = Elasticsearch(
341-
...,
342-
node_selector_class=CustomSelector
343-
)
344-
------------------------------------
345-
346-
[discrete]
347-
==== Marking nodes dead and alive
348-
349-
Individual nodes of Elasticsearch may have transient connectivity or load issues which may make them unable to service requests. To combat this the pool of nodes will detect when a node isn't able to service requests due to transport or API errors.
350-
351-
After a node has been timed out it will be moved back to the set of "alive" nodes but only after the node returns a successful response will the node be marked as "alive" in terms of consecutive errors.
352-
353-
The `dead_node_backoff_factor` and `max_dead_node_backoff` parameters can be used to configure how long the node pool will put the node into timeout with each consecutive failure. Both parameters use a unit of seconds.
354-
355-
The calculation is equal to `min(dead_node_backoff_factor * (2 ** (consecutive_failures - 1)), max_dead_node_backoff)`.
356-
357-
358241
[discrete]
359242
[[serializer]]
360243
=== Serializers
@@ -365,7 +248,7 @@ You can define custom serializers via the `serializers` parameter:
365248

366249
[source,python]
367250
------------------------------------
368-
from elasticsearch import Elasticsearch, JsonSerializer
251+
from elasticsearch_serverless import Elasticsearch, JsonSerializer
369252
370253
class JsonSetSerializer(JsonSerializer):
371254
"""Custom JSON serializer that handles Python sets"""
@@ -395,7 +278,7 @@ For all of the built-in HTTP node implementations like `urllib3`, `requests`, an
395278

396279
[source,python]
397280
------------------------------------
398-
from elasticsearch import Elasticsearch
281+
from elasticsearch_serverless import Elasticsearch
399282
400283
es = Elasticsearch(
401284
...,
@@ -407,7 +290,7 @@ You can also specify a custom node implementation via the `node_class` parameter
407290

408291
[source,python]
409292
------------------------------------
410-
from elasticsearch import Elasticsearch
293+
from elasticsearch_serverless import Elasticsearch
411294
from elastic_transport import Urllib3HttpNode
412295
413296
class CustomHttpNode(Urllib3HttpNode):
@@ -420,14 +303,14 @@ es = Elasticsearch(
420303
------------------------------------
421304

422305
[discrete]
423-
==== HTTP connections per node
306+
==== HTTP connections
424307

425-
Each node contains its own pool of HTTP connections to allow for concurrent requests. This value is configurable via the `connections_per_node` parameter:
308+
The client maintains a pool of HTTP connections to the Elasticsearch Serverless project to allow for concurrent requests. This value is configurable via the `connections` parameter:
426309

427310
[source,python]
428311
------------------------------------
429312
es = Elasticsearch(
430313
...,
431-
connections_per_node=5
314+
connections=5
432315
)
433316
------------------------------------

0 commit comments

Comments
 (0)