Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Answer Elasticsearch timeouts and outages with 504/503 instead of 500 (#5686)
Dhruv Bhanushalie58cf051a148
diff --git a/api/api/utils/exceptions.py b/api/api/utils/exceptions.pyindex 7dce1b457..05bc00526 100644--- a/api/api/utils/exceptions.py+++ b/api/api/utils/exceptions.py@@ -3,9 +3,34 @@ from rest_framework.exceptions import ValidationErrorfrom rest_framework.response import Responsefrom rest_framework.views import exception_handler as drf_exception_handler+import structlog+from elastic_transport import ConnectionError as ESConnectionError+from elastic_transport import ConnectionTimeout+from api.utils.es_transport import SearchCapacityExceeded+logger = structlog.get_logger(__name__)++# Elasticsearch failing to answer is a backend outage the client can only wait+# out, so it is reported as one. Answering here also keeps these out of Sentry,+# which otherwise receives one 500 per timed-out search.+SEARCH_BACKEND_FAILURES = {+ SearchCapacityExceeded: (+ status.HTTP_503_SERVICE_UNAVAILABLE,+ "Search is over capacity, please retry shortly.",+ ),+ ConnectionTimeout: (+ status.HTTP_504_GATEWAY_TIMEOUT,+ "Search timed out, please retry shortly.",+ ),+ ESConnectionError: (+ status.HTTP_503_SERVICE_UNAVAILABLE,+ "Search is temporarily unavailable, please retry shortly.",+ ),+}++def exception_handler(ex, context):"""Handle the exception raised in a DRF context.@@ -18,12 +43,16 @@ def exception_handler(ex, context)::return: the response to show for the exception""" # noqa: E501- if isinstance(ex, SearchCapacityExceeded):- return Response(- {"detail": "Search is over capacity, please retry shortly."},- status=status.HTTP_503_SERVICE_UNAVAILABLE,- headers={"Retry-After": "5"},- )+ for exc_type, (status_code, detail) in SEARCH_BACKEND_FAILURES.items():+ if isinstance(ex, exc_type):+ logger.warning(+ "Search backend failure", exc_type=type(ex).__name__, status=status_code+ )+ return Response(+ {"detail": detail},+ status=status_code,+ headers={"Retry-After": "5"},+ )res = drf_exception_handler(ex, context)if isinstance(ex, ValidationError):diff --git a/api/test/unit/utils/test_exceptions.py b/api/test/unit/utils/test_exceptions.pynew file mode 100644index 000000000..03eaad2af--- /dev/null+++ b/api/test/unit/utils/test_exceptions.py@@ -0,0 +1,30 @@+from rest_framework.exceptions import NotFound++import pytest+from elastic_transport import ConnectionError as ESConnectionError+from elastic_transport import ConnectionTimeout++from api.utils.es_transport import SearchCapacityExceeded+from api.utils.exceptions import exception_handler+++ "ex, status_code",+ [+ (SearchCapacityExceeded("no socket free"), 503),+ (ConnectionTimeout("Connection timed out"), 504),+ (ESConnectionError("connection refused"), 503),+ ],+)+def test_search_backend_failures_are_not_server_errors(ex, status_code):+ res = exception_handler(ex, {})++ assert res.status_code == status_code+ assert res["Retry-After"] == "5"+ assert "retry" in res.data["detail"]+++def test_other_exceptions_are_left_to_drf():+ res = exception_handler(NotFound(), {})++ assert res.status_code == 404