diff options
author | Bryan Newbold <bnewbold@archive.org> | 2021-04-06 16:23:53 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2021-04-06 16:23:55 -0700 |
commit | 9c2e7cec2397ce103565015d9a1662287f62341e (patch) | |
tree | d12edfd3efa4ce9b706ac1b2af48d5f2280038f9 /fatcat_scholar/search.py | |
parent | e1030e29bbd192953ab742f593dd8da43a7af684 (diff) | |
download | fatcat-scholar-9c2e7cec2397ce103565015d9a1662287f62341e.tar.gz fatcat-scholar-9c2e7cec2397ce103565015d9a1662287f62341e.zip |
health check: use /<index>/_count endpoint; verify shards
In actual production verification, the /_mapping endpoint didn't seem to
work.
Diffstat (limited to 'fatcat_scholar/search.py')
-rw-r--r-- | fatcat_scholar/search.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/fatcat_scholar/search.py b/fatcat_scholar/search.py index e9f2bc8..c52cae3 100644 --- a/fatcat_scholar/search.py +++ b/fatcat_scholar/search.py @@ -420,22 +420,27 @@ def do_fulltext_search( def es_scholar_index_alive() -> bool: """ Checks if the configured back-end elasticsearch index exists and can - service queries. Intended to be used in health checks. + service queries. Intended to be used in health checks, called every couple + seconds. Note that the regular client.indices.exists(index) function call will return an error if the cluster leader can not be reached, even if the local node could service queries in a read-only manner. - The client.indices.get_mapping(index) API, or the client.cat.count(index) - API, both return quickly and indicate that queries can be run against the - index. + The client.count(body=None, index=index) API returns quickly enough (though + might be slow during indexing?), and returns context about the number of + shards queried, and thus seems like a good fit for this check. """ try: - resp = es_client.indices.get_mapping(settings.ELASTICSEARCH_QUERY_FULLTEXT_INDEX) + resp = es_client.count( + body=None, index=settings.ELASTICSEARCH_QUERY_FULLTEXT_INDEX + ) except elasticsearch.exceptions.RequestError as e_raw: if e_raw.status_code == 404: return False else: raise e_raw - return resp - + try: + return bool(resp["_shards"]["successful"] == resp["_shards"]["total"]) + except KeyError: + return False |