Method to wait for healthy cluster, that can be called before ES connection exists
See original GitHub issueThis code:
es = Elasticsearch(["elasticsearch:9200"])
es.cluster.health(wait_for_status='yellow')
Fails with:
index_test_data_1 | Traceback (most recent call last):
index_test_data_1 | File "indexer.py", line 74, in <module>
index_test_data_1 | main()
index_test_data_1 | File "indexer.py", line 55, in main
index_test_data_1 | es = init_elasticsearch()
index_test_data_1 | File "indexer.py", line 34, in init_elasticsearch
index_test_data_1 | es.cluster.health(wait_for_status='yellow')
index_test_data_1 | File "/usr/local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 76, in _wrapped
index_test_data_1 | return func(*args, params=params, **kwargs)
index_test_data_1 | File "/usr/local/lib/python2.7/site-packages/elasticsearch/client/cluster.py", line 33, in health
index_test_data_1 | 'health', index), params=params)
index_test_data_1 | File "/usr/local/lib/python2.7/site-packages/elasticsearch/transport.py", line 314, in perform_request
index_test_data_1 | status, headers_response, data = connection.perform_request(method, url, params, body, headers=headers, ignore=ignore, timeout=timeout)
index_test_data_1 | File "/usr/local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 175, in perform_request
index_test_data_1 | raise ConnectionError('N/A', str(e), e)
index_test_data_1 | elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7fa33e09f110>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7fa33e09f110>: Failed to establish a new connection: [Errno 111] Connection refused)
I use docker-compose to start index_test_data
(Python script) and elasticsearch at the same time. The ConnectionError
is because nothing is listening on port 9200 yet.
I would like a method like this:
es.wait_for_status('yellow')
es = Elasticsearch(["elasticsearch:9200"])
That will swallow the ConnectionError
’s, and block until elasticsearch is up.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Cluster health API | Elasticsearch Guide [8.5] | Elastic
The cluster health API returns a simple status on the health of the cluster. ... For example, the following will wait for 50...
Read more >API Documentation — Elasticsearch 7.16.0 documentation
Elasticsearch low-level client. Provides a straightforward mapping from Python to ES REST endpoints. The instance has attributes cat , cluster , ...
Read more >How to check if ElasticSearch index exists and is ready?
You can pass a query string param wait_for_status=green that will wait until the cluster is in the given status (or until the timeout ......
Read more >How to Resolve Unassigned Shards in Elasticsearch - Datadog
In Elasticsearch, a healthy cluster is a balanced cluster: ... process), and the node left the cluster before the data could be replicated....
Read more >Troubleshooting Amazon OpenSearch Service
Go to the Cluster health tab and find the Total nodes metric. See if the reported number of nodes is fewer than the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the issue @melissachang, and I’m sorry to hear you’re having some troubles.
This is something other people have come across as well (please see https://github.com/elastic/elasticsearch-py/issues/715). But building this into the client is not something I feel would add any value. If someone configures their ES instances incorrectly, we’ll end up swallowing errors and nothing will get reported. (even if we built a timeout into it, we have to wait till the timeout expires before the application reports that the cluster is no reachable).
I feel that this is something that should be solved in the application code that implements the Elasticsearch client and not something that should be built into the client itself.
The standard pattern for connecting to any service in docker is to build a “wait” into the application code that waits for a service to start before continuing. Please reference the Docker documentation on controlling startup order.
I have in the past created this bash script which will wait for elasticsearch before starting the command in your container:
You can use this script in your
Dockerfile
with:or similarly you can use this script in your
docker-compose.yml
by overriding thecommand
in a similar fashion.Just contributing back here, since I needed a script to monitor elasticsearch startup, but with a timeout:
Feel free to grab, butcher or burn it with fire 😃