Make the OpenSearch client timeout configurable (searches fail with read timeout=10 on cold k-NN graphs)
Self Checks
- [x] I have read the Contributing Guide and Language Policy.
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] Please do not modify this template :) and fill in all the required fields.
1. Is this request related to a challenge you're experiencing? Tell me about your story.
We run Dify with Amazon OpenSearch Service as the vector store. Knowledge retrieval fails whenever a k-NN search has to load a graph that is not in the native memory cache (after the k-NN cache evicts it, after knn.cache.item.expiry is enabled, or after a node is replaced or restarted).
The cause is that the OpenSearch client in Dify never sets a timeout, so every request uses the opensearch-py default of 10 seconds:
opensearchpy.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(...): Read timed out. (read timeout=10)
A warm k-NN search takes a few milliseconds. The first search after a graph has been evicted takes longer, because OpenSearch has to read the whole graph file from disk into memory before it can search. In our tests (r6g.large data nodes, gp3), the load ran at about 130 MiB/s, so a single shard holding about 1.25 GiB of graph (around 100k vectors in fp32) already goes over 10 seconds. If one shard times out, the whole retrieval returns an error to the user.
This also stops us from using the cache expiry setting. Without expiry, the k-NN cache fills up and the k-NN circuit breaker trips, which blocks indexing. With expiry, the first search on a large knowledge base that has not been used for a while times out.
Other vector stores already let you configure this:
- Elasticsearch:
ELASTICSEARCH_REQUEST_TIMEOUT(default 100000 ms),ELASTICSEARCH_RETRY_ON_TIMEOUT,ELASTICSEARCH_MAX_RETRIES - Qdrant:
QDRANT_CLIENT_TIMEOUT(default 20 s)
OpenSearchConfig (api/configs/middleware/vdb/opensearch_config.py) has no such field. In api/providers/vdb/vdb-opensearch/src/dify_vdb_opensearch/opensearch_vector.py, to_opensearch_params() does not pass timeout. Only helpers.bulk() uses timeout=30, and that does not apply to searches. This is still the case in 1.20.0.
Proposal
- Add
OPENSEARCH_TIMEOUT(seconds), and optionallyOPENSEARCH_MAX_RETRIESandOPENSEARCH_RETRY_ON_TIMEOUT, toOpenSearchConfig. - Pass them through
to_opensearch_params()astimeout,max_retriesandretry_on_timeoutfor theOpenSearch(...)client. - Keep the default at 10 seconds so that current behaviour does not change.
2. Additional context or comments
- The same request was made in #20927 and was closed as stale with no response.
- opensearch-py
OpenSearch/Transportalready accepttimeout,max_retriesandretry_on_timeout, so the change is small.
3. Can you help us with this feature?
- [x] I am interested in contributing to this feature.
Implementing this: OPENSEARCH_REQUEST_TIMEOUT (seconds), OPENSEARCH_MAX_RETRIES and OPENSEARCH_RETRY_ON_TIMEOUT on OpenSearchConfig, forwarded through to_opensearch_params() to the OpenSearch(...) client - the same trio shape Elasticsearch already exposes. Defaults are 10 s / 3 / False, i.e. opensearch-py's own values, so nothing changes for a deployment that sets nothing. api/.env.example documents them. Two tests pin the pass-through and both fail on main.
Please assign @Luckyou-d if available.
Spot-on diagnosis. Loading cold HNSW graphs from EBS into off-heap memory on baseline gp3 (~125 MiB/s) consistently crosses the 10s default once shard graph segments hit ~1 GiB+.
Thanks a lot @Luckyou-d for picking this up so quickly and opening #46755 — keeping the defaults identical to opensearch-py's is exactly what I had in mind. And thanks @grinem for the extra data point on gp3 throughput; that matches what we observed.
Looking forward to the review.
#46755 will definitely help with the headroom while OpenSearch page-faults cold graphs back in, but you can also stop the evictions on the cluster side in the meantime.
Take a look at knn.memory.circuit_breaker.limit (usually 50% RAM by default). If you're hitting that limit under concurrent Lucene segment queries, it dumps the graphs to disk, which totally tanks gp3 throughput when they reload. A quick workaround while waiting for the merge is setting up an index warmer or pinging it with a periodic dummy query during quiet periods to keep the HNSW graphs pinned in off-heap memory.
You can pull /_nodes/stats/indices/knn to verify if graph_memory_usage_percentage drops right before your 10s read timeout spikes.