Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle raw response exceptions #1805

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions esrally/driver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2053,28 +2053,51 @@ async def execute_single(runner, es, params, on_error):
total_ops = 0
total_ops_unit = "ops"
request_meta_data = {"success": False, "error-type": "api"}

if isinstance(e.error, bytes):
error_message = e.error.decode("utf-8")
elif isinstance(e.error, BytesIO):
error_message = e.error.read().decode("utf-8")
error_message = ""

# Some runners return a raw response, causing the 'error' property to be a string literal of the bytes/BytesIO object,
# we should avoid bubbling that up
# e.g. ApiError(413, '<_io.BytesIO object at 0xffffaf146a70>')
if isinstance(e.body, bytes):
# could be an empty body
if error_body := e.body.decode("utf-8"):
error_message = error_body
else:
# to be consistent with an empty 'e.error'
error_message = str(None)
elif isinstance(e.body, BytesIO):
# could be an empty body
if error_body := e.body.read().decode("utf-8"):
error_message = error_body
else:
# to be consistent with an empty 'e.error'
error_message = str(None)
# fallback to 'error' property if the body isn't bytes/BytesIO
else:
error_message = e.error
if isinstance(e.error, bytes):
error_message = e.error.decode("utf-8")
elif isinstance(e.error, BytesIO):
error_message = e.error.read().decode("utf-8")
else:
# if the 'error' is empty, we get back str(None)
error_message = e.error

if isinstance(e.info, bytes):
error_body = e.info.decode("utf-8")
error_info = e.info.decode("utf-8")
elif isinstance(e.info, BytesIO):
error_body = e.info.read().decode("utf-8")
error_info = e.info.read().decode("utf-8")
else:
error_body = e.info
error_info = e.info

if error_info:
error_message += f" ({error_info})"

if error_body:
error_message += f" ({error_body})"
error_description = error_message

request_meta_data["error-description"] = error_description
if e.status_code:
request_meta_data["http-status"] = e.status_code

except KeyError as e:
logging.getLogger(__name__).exception("Cannot execute runner [%s]; most likely due to missing parameters.", str(runner))
msg = "Cannot execute [%s]. Provided parameters are: %s. Error: [%s]." % (str(runner), list(params.keys()), str(e))
Expand All @@ -2083,10 +2106,15 @@ async def execute_single(runner, es, params, on_error):
if not request_meta_data["success"]:
if on_error == "abort" or fatal_error:
msg = "Request returned an error. Error type: %s" % request_meta_data.get("error-type", "Unknown")
description = request_meta_data.get("error-description")
if description:
msg += ", Description: %s" % description

if description := request_meta_data.get("error-description"):
msg += f", Description: {description}"

if http_status := request_meta_data.get("http-status"):
msg += f", HTTP Status: {http_status}"

raise exceptions.RallyAssertionError(msg)

return total_ops, total_ops_unit, request_meta_data


Expand Down
29 changes: 28 additions & 1 deletion tests/driver/driver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1901,9 +1901,36 @@ async def test_execute_single_with_http_400_aborts_when_specified(self):
with pytest.raises(exceptions.RallyAssertionError) as exc:
await driver.execute_single(self.context_managed(runner), es, params, on_error="abort")
assert exc.value.args[0] == (
"Request returned an error. Error type: api, Description: not found (the requested document could not be found)"
"Request returned an error. Error type: api, Description: not found (the requested document could not be found),"
" HTTP Status: 404"
)

@pytest.mark.asyncio
async def test_execute_single_with_http_400_with_empty_raw_response_body(self):
es = None
params = None
empty_body = io.BytesIO(b"")
str_literal_empty_body = str(empty_body)
error_meta = elastic_transport.ApiResponseMeta(status=413, http_version="1.1", headers={}, duration=0.0, node=None)
runner = mock.AsyncMock(side_effect=elasticsearch.ApiError(message=str_literal_empty_body, meta=error_meta, body=empty_body))

with pytest.raises(exceptions.RallyAssertionError) as exc:
await driver.execute_single(self.context_managed(runner), es, params, on_error="abort")
assert exc.value.args[0] == ("Request returned an error. Error type: api, Description: None, HTTP Status: 413")

@pytest.mark.asyncio
async def test_execute_single_with_http_400_with_raw_response_body(self):
es = None
params = None
body = io.BytesIO(b"Huge error")
str_literal = str(body)
error_meta = elastic_transport.ApiResponseMeta(status=499, http_version="1.1", headers={}, duration=0.0, node=None)
runner = mock.AsyncMock(side_effect=elasticsearch.ApiError(message=str_literal, meta=error_meta, body=body))

with pytest.raises(exceptions.RallyAssertionError) as exc:
await driver.execute_single(self.context_managed(runner), es, params, on_error="abort")
assert exc.value.args[0] == ("Request returned an error. Error type: api, Description: Huge error, HTTP Status: 499")

@pytest.mark.asyncio
async def test_execute_single_with_http_400(self):
es = None
Expand Down