From 7a5f00e69a258de281152de7f871d7529f5c8e34 Mon Sep 17 00:00:00 2001 From: Graham Herceg Date: Tue, 23 Apr 2024 16:28:35 -0400 Subject: [PATCH 1/2] Add preference as query parameter in ES adapter --- corehq/apps/es/client.py | 9 ++++++++- corehq/apps/es/es_query.py | 12 ------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/corehq/apps/es/client.py b/corehq/apps/es/client.py index f76e20ca5058..e6f6922114c7 100644 --- a/corehq/apps/es/client.py +++ b/corehq/apps/es/client.py @@ -21,6 +21,7 @@ TransportError, bulk, ) +from corehq.toggles import ES_QUERY_PREFERENCE from corehq.util.global_request import get_request_domain from corehq.util.metrics import ( limit_domains, @@ -644,12 +645,18 @@ def _search(self, query, **kw): """Perform a "low-level" search and return the raw result. This is split into a separate method for ease of testing the result format. """ + domain = get_request_domain() + if ES_QUERY_PREFERENCE.enabled(domain): + # Use domain as key to route to a consistent set of shards.kwargs + # See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-preference.html + kw['preference'] = domain + with metrics_histogram_timer( 'commcare.elasticsearch.search.timing', timing_buckets=(1, 10), tags={ 'index': self.canonical_name, - 'domain': limit_domains(get_request_domain()), + 'domain': limit_domains(domain), }, ): return self._es.search(self.index_name, self.type, query, **kw) diff --git a/corehq/apps/es/es_query.py b/corehq/apps/es/es_query.py index 41ca468f1048..e61697537f0a 100644 --- a/corehq/apps/es/es_query.py +++ b/corehq/apps/es/es_query.py @@ -92,9 +92,7 @@ from memoized import memoized -from corehq.toggles import ES_QUERY_PREFERENCE from corehq.util.files import TransientTempfile -from corehq.util.global_request import get_request_domain from . import aggregations, filters, queries from .const import SCROLL_SIZE, SIZE_LIMIT @@ -152,16 +150,6 @@ def __init__(self, index=None, for_export=False): } } } - self._set_preference() - - def _set_preference(self): - """ - If the specified domain has ES_QUERY_PREFERENCE enabled, use domain as key to route to a consistent set - of shards. See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-preference.html - """ - domain = get_request_domain() - if ES_QUERY_PREFERENCE.enabled(domain): - self.es_query['preference'] = domain def clone(self): adapter = self.adapter From 18e75b4b290a2bda841ed6086c0367882f3c8696 Mon Sep 17 00:00:00 2001 From: Graham Herceg Date: Tue, 23 Apr 2024 17:08:16 -0400 Subject: [PATCH 2/2] Only set if not already set --- corehq/apps/es/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/corehq/apps/es/client.py b/corehq/apps/es/client.py index e6f6922114c7..8b162b32f2ac 100644 --- a/corehq/apps/es/client.py +++ b/corehq/apps/es/client.py @@ -649,7 +649,8 @@ def _search(self, query, **kw): if ES_QUERY_PREFERENCE.enabled(domain): # Use domain as key to route to a consistent set of shards.kwargs # See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-preference.html - kw['preference'] = domain + if 'preference' not in kw: + kw['preference'] = domain with metrics_histogram_timer( 'commcare.elasticsearch.search.timing',