elastic / elasticsearch-py

Official Python client for Elasticsearch
https://ela.st/es-python
Apache License 2.0
4.2k stars 1.18k forks source link

use_dns_cache value cannot be set for AsyncElasticsearch object #2424

Closed qfliu closed 1 month ago

qfliu commented 7 months ago

elasticsearch-py version: v7.17.9

Description: In the old elasticsearch_async library, when creating a AsyncElasticsearch object, we can set value for use_dns_cache to disable dns caching. However, this does not work right now.

Code snippet:

from elasticsearch import AsyncElasticsearch

es_client = AsyncElasticsearch(
    hosts=conf['endpoints'],
    loop=loop,
    timeout=conf.get('request_timeout_seconds'),
    use_dns_cache=False,  # We use ELB to balance the load
)

connection = elastic_client.transport.get_connection()
print(connection.session.connector.use_dns_cache)

I am expecting the output to be False, but it is always True right now.

Am I doing something wrong to set the use_dns_cache value?

pquentin commented 7 months ago

Hello! It is indeed not configurable. I'm unlikely to add that feature in the 7.17 client, but could definitely consider it in the 8.x client. Thanks.

qfliu commented 6 months ago

Hello! It is indeed not configurable. I'm unlikely to add that feature in the 7.17 client, but could definitely consider it in the 8.x client. Thanks.

Thanks for the reply!

Just to clarify: We have to use dns_cache regardless 7.x or 8.x right now when creating the client. But, can we change it later through another function call by any chance? If not, does that mean we have to use dns_cache all the time right now with es client? Were it every being configurable after making AsyncElasticsearch native instead of using elasticsearch_async lib? Thanks

pquentin commented 3 months ago

Hey, sorry for missing your reply here. Trying to answer your questions point by point:

  1. I don't know about how it used to work with elasticsearch-async, this was deprecated four years ago now and I never worked on it myself (but I do remember using it!).
  2. We don't currently support changing the internals of the transport. I could teach how to make it work today in 8.13, but it could break in a future release
  3. So yes, the only supported way for now is to use_dns_cache=True all the time. But as you can see in https://docs.aiohttp.org/en/stable/client_advanced.html#tuning-the-dns-cache, the cache is only valid for 10 seconds. How is that a problem for you?
pquentin commented 1 month ago

I'm going to close this now, but I'm happy to reopen if I hear about a specific use case that would benefit from this. Thank you!