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

cloud_storage_clients: Fix xml parsing when searching for error code in response #14848

Merged
merged 2 commits into from
Nov 9, 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
5 changes: 3 additions & 2 deletions src/v/cloud_storage_clients/s3_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,13 @@ ss::future<> s3_client::do_delete_object(
});
}

static std::variant<client::delete_objects_result, rest_error_response>
std::variant<client::delete_objects_result, rest_error_response>
iobuf_to_delete_objects_result(iobuf&& buf) {
auto root = util::iobuf_to_ptree(std::move(buf), s3_log);
auto result = client::delete_objects_result{};
try {
if (root.count("Error.Code") > 0) {
if (auto error_code = root.get_optional<ss::sstring>("Error.Code");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't expect you to change anything here, but (I believe) we can save some CPU instructions by doing extracting the child object here

if( auto error_payload = root.get_child_optional("Error"); error_payload.has_value() {
...
auto code = error_payload.get<ss::sstring>("Code");
}

changes:

  • remove tree walking for each property
  • remove the empty string optional and let it throw to show the expectations and intentions
    • these properties must always be present, if they are not we are likely doing something wrong or parsing a bogus message

error_code) {
// This is an error response. S3 can reply with 200 error code and
// error response in the body.
constexpr const char* empty = "";
Expand Down
3 changes: 3 additions & 0 deletions src/v/cloud_storage_clients/s3_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,7 @@ class s3_client : public client {
ss::shared_ptr<client_probe> _probe;
};

std::variant<client::delete_objects_result, rest_error_response>
iobuf_to_delete_objects_result(iobuf&& buf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know you didn't add this function here, but you are making it "public" so i believe the following comment is appropriate

This function can have a better name.

  • x_to_* usually means a type conversion/format of some sort, not the case here
  • this method expects the input to be xml, the method doesn't say anything about the input expectations

something along the line of parse_delete_objects_result is much better name

  • it tells what it is doing: parsing
  • it tells the input expectations: delete objects result (from the context (ie the namespace location) we know that it must be a cloud storage delete objects result
    • i'll correct myself parse_**s3**_delete_objects_result is a better name, it won't work for azure but since it is in the same namespace it might confuse users
  • iobuf is a detail and is part of the type declaration, we don't need it in the name. moreover, we can have multiple overloads for different type of "input carriers" and won't need to have dozen of function names


} // namespace cloud_storage_clients
53 changes: 53 additions & 0 deletions src/v/cloud_storage_clients/tests/s3_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,56 @@ FIXTURE_TEST(test_client_pool_reconnect, client_pool_fixture) {
auto count = std::count(result.begin(), result.end(), true);
BOOST_REQUIRE(count == 20);
}

SEASTAR_THREAD_TEST_CASE(test_parse_delete_object_response_infra_error) {
const ss::sstring xml_response
= "<Error><Code>SlowDown</Code><Message>Please reduce your request "
"rate.</Message><RequestId>R123</RequestId><HostId>H123</HostId></"
"Error>";

iobuf b;
b.append(xml_response.data(), xml_response.size());
const auto result = cloud_storage_clients::iobuf_to_delete_objects_result(
std::move(b));
const auto* error = std::get_if<cloud_storage_clients::rest_error_response>(
&result);
BOOST_REQUIRE_NE(error, nullptr);
BOOST_REQUIRE_EQUAL(error->code_string(), "SlowDown");
}

SEASTAR_THREAD_TEST_CASE(test_parse_delete_object_response_key_error) {
const ss::sstring xml_response
= R"XML(<DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Error>
<Key>0</Key>
<Code>TestFailure</Code>
</Error>
</DeleteResult>)XML";

iobuf b;
b.append(xml_response.data(), xml_response.size());
const auto result = cloud_storage_clients::iobuf_to_delete_objects_result(
std::move(b));
const auto* response
= std::get_if<cloud_storage_clients::client::delete_objects_result>(
&result);
BOOST_REQUIRE_NE(response, nullptr);
BOOST_REQUIRE_EQUAL(response->undeleted_keys.size(), 1);
BOOST_REQUIRE_EQUAL(response->undeleted_keys.front().reason, "TestFailure");
}

SEASTAR_THREAD_TEST_CASE(test_parse_delete_object_response_no_error) {
const ss::sstring xml_response
= R"XML(<DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
</DeleteResult>)XML";

iobuf b;
b.append(xml_response.data(), xml_response.size());
const auto result = cloud_storage_clients::iobuf_to_delete_objects_result(
std::move(b));
const auto* response
= std::get_if<cloud_storage_clients::client::delete_objects_result>(
&result);
BOOST_REQUIRE_NE(response, nullptr);
BOOST_REQUIRE(response->undeleted_keys.empty());
}
Loading