Skip to content
Open
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
21 changes: 15 additions & 6 deletions google/cloud/storage/internal/connection_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "google/cloud/internal/disable_deprecation_warnings.inc"
#include "google/cloud/storage/internal/connection_impl.h"
#include "google/cloud/storage/internal/hash_values.h"
#include "google/cloud/storage/internal/hedged_object_read_source.h"
#include "google/cloud/storage/internal/hedged_read_metrics.h"
#include "google/cloud/storage/internal/retry_logging.h"
Expand All @@ -33,6 +34,7 @@
#include <fstream>
#include <functional>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#include <thread>
Expand All @@ -53,6 +55,17 @@ using ::google::cloud::storage_internal::AddFeaturesHeader;
using ::google::cloud::storage_internal::FeatureBit;
using ::google::cloud::storage_internal::TrackedFeature;

void AppendUploadChunkDiagnostics(
std::ostream& os, UploadChunkRequest const& request,
QueryResumableUploadResponse const& response) {
Comment thread
kalragauri marked this conversation as resolved.
Resolved
os << " session_id="
<< (request.upload_session_url().empty() ? "" : "[censored]")
<< ", result=" << response << ", offset=" << request.offset()
<< ", range=<" << request.RangeHeader() << ">"
<< ", payload_size=" << request.payload_size() << ", known_object_hashes={"
<< Format(request.known_object_hashes()) << "}";
}

// Returns an error if the response contains an unexpected (or invalid)
// committed size.
Status ValidateCommittedSize(UploadChunkRequest const& request,
Expand All @@ -72,9 +85,7 @@ Status ValidateCommittedSize(UploadChunkRequest const& request,
<< " (https://github.com/googleapis/google-cloud-cpp/issues/new)."
<< " Please include as much information as you can including this"
<< " message and the following details:";
os << " session_id=" << request.upload_session_url();
os << ", result=" << response;
os << ", request=" << request;
AppendUploadChunkDiagnostics(os, request, response);
return google::cloud::internal::InternalError(std::move(os).str(),
GCP_ERROR_INFO());
}
Expand All @@ -100,9 +111,7 @@ Status ValidateCommittedSize(UploadChunkRequest const& request,
<< " (https://github.com/googleapis/google-cloud-cpp/issues/new)."
<< " Please include as much information as you can including this"
<< " message and the following details:";
os << " session_id=" << request.upload_session_url();
os << ", result=" << response;
os << ", request=" << request;
AppendUploadChunkDiagnostics(os, request, response);
return google::cloud::internal::InternalError(std::move(os).str(),
GCP_ERROR_INFO());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,53 @@ TEST_F(ConnectionImplFileUploadTest, UploadFileErrorsFormatEncryptionKey) {
Not(HasSubstr(kKeyBase64)))));
}

/// @test Verify `UseResumableUploadSession` option formatting in resumable
/// file-upload error statuses.
TEST_F(ConnectionImplFileUploadTest, UploadFileResumableFormatsSessionOption) {
constexpr std::string_view kSessionUrl =
"https://storage.googleapis.com/upload/storage/v1/b/test-bucket/"
"o?uploadType=resumable&upload_id=xa298sd_sdlkj2";
constexpr std::string_view kSessionToken = "xa298sd_sdlkj2";

std::shared_ptr<StorageConnectionImpl> connection = MakeConnection();
std::string const contents = "0123456789";
TempFile temp(contents);

ResumableUploadRequest resumable_missing("test-bucket", "test-object");
resumable_missing.set_option(
RestoreResumableUploadSession(std::string{kSessionUrl}));
StatusOr<std::unique_ptr<std::istream>> const missing_result =
connection->UploadFileResumable("/no/such/file/exists.txt",
resumable_missing);
EXPECT_THAT(missing_result,
StatusIs(StatusCode::kNotFound,
AllOf(HasSubstr("resumable-upload=[censored]"),
Not(HasSubstr(kSessionUrl)),
Not(HasSubstr(kSessionToken)))));

ResumableUploadRequest resumable_offset("test-bucket", "test-object");
resumable_offset.set_multiple_options(
RestoreResumableUploadSession(std::string{kSessionUrl}),
UploadFromOffset(contents.size() + 1));
StatusOr<std::unique_ptr<std::istream>> const offset_result =
connection->UploadFileResumable(temp.name(), resumable_offset);
EXPECT_THAT(offset_result,
StatusIs(StatusCode::kInvalidArgument,
AllOf(HasSubstr("resumable-upload=[censored]"),
Not(HasSubstr(kSessionUrl)),
Not(HasSubstr(kSessionToken)))));

ResumableUploadRequest new_session_missing("test-bucket", "test-object");
new_session_missing.set_option(NewResumableUploadSession());
StatusOr<std::unique_ptr<std::istream>> const new_session_result =
connection->UploadFileResumable("/no/such/file/exists.txt",
new_session_missing);
EXPECT_THAT(new_session_result,
StatusIs(StatusCode::kNotFound,
AllOf(HasSubstr("resumable-upload=,"),
Not(HasSubstr("resumable-upload=[censored]")))));
}

} // namespace
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
186 changes: 186 additions & 0 deletions google/cloud/storage/internal/connection_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
#include <functional>
#include <future>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <vector>
Expand All @@ -53,6 +55,7 @@ using ::google::cloud::testing_util::MockMeter;
using ::google::cloud::testing_util::MockMeterProvider;
using ::google::cloud::testing_util::StatusIs;
using ::testing::_;
using ::testing::AllOf;
using ::testing::AtLeast;
using ::testing::ByMove;
using ::testing::ElementsAre;
Expand Down Expand Up @@ -468,6 +471,189 @@ TEST(RetryClientTest, UploadChunkHandleOvercommit) {
HasSubstr("If you believe this is a bug in the client library")));
}

struct UploadChunkStatusFormatCase {
std::string_view name;
std::string_view session_id;
std::string_view session_marker;
};

class RetryClientUploadChunkFormatTest
: public ::testing::TestWithParam<UploadChunkStatusFormatCase> {};

/// @test Verify `UploadChunk` rollback errors format `session_id` and chunk
/// metadata fields.
TEST_P(RetryClientUploadChunkFormatTest, UploadChunkRollbackFormatsStatus) {
UploadChunkStatusFormatCase const& param = GetParam();
constexpr std::uint64_t kOffset = 1024;
constexpr std::uint64_t kRollback = 512;
std::string const payload =
"chunk-payload-marker-one" + std::string(200, 'A');

auto mock = std::make_unique<MockGenericStub>();
EXPECT_CALL(*mock, options); // Required in StorageConnectionImpl::Create()
EXPECT_CALL(*mock, UploadChunk)
.WillOnce(Return(QueryResumableUploadResponse{kRollback, std::nullopt}));

std::shared_ptr<StorageConnectionImpl> client =
StorageConnectionImpl::Create(std::move(mock));
google::cloud::internal::OptionsSpan const span(
BasicTestPolicies().set<IdempotencyPolicyOption>(
StrictIdempotencyPolicy().clone()));

UploadChunkRequest request(std::string{param.session_id}, kOffset,
{{payload}}, CreateNullHashFunction(),
HashValues{"crc32c-val", "md5-val"});
request.set_option(CustomHeader("x-custom-header", "custom-header-value"));

StatusOr<QueryResumableUploadResponse> const response =
client->UploadChunk(request);
EXPECT_THAT(
response,
StatusIs(
StatusCode::kInternal,
AllOf(HasSubstr("session_id=[censored]"),
HasSubstr("result=UploadChunkResponse={committed_size=512, "
"payload={}}"),
HasSubstr("offset=1024"),
HasSubstr("range=<Content-Range: bytes 1024-1247/1248>"),
HasSubstr("payload_size=224"),
HasSubstr("known_object_hashes={crc32c=crc32c-val, "
"md5=md5-val}"),
Not(HasSubstr(param.session_id)),
Not(HasSubstr(param.session_marker)),
Not(HasSubstr("chunk-payload-marker-one")),
Not(HasSubstr("AAAA")), Not(HasSubstr("x-custom-header")),
Not(HasSubstr("custom-header-value")),
Not(HasSubstr("request=")))));
}

/// @test Verify `UploadChunk` overcommit errors format `session_id` and chunk
/// metadata fields across multi-buffer payloads.
TEST_P(RetryClientUploadChunkFormatTest, UploadChunkOvercommitFormatsStatus) {
UploadChunkStatusFormatCase const& param = GetParam();
constexpr std::uint64_t kExcess = 1024;
std::string const buffer_one =
"chunk-payload-marker-one" + std::string(200, 'A');
std::string const buffer_two =
"chunk-payload-marker-two" + std::string(250, 'B');

auto mock = std::make_unique<MockGenericStub>();
EXPECT_CALL(*mock, options); // Required in StorageConnectionImpl::Create()
EXPECT_CALL(*mock, UploadChunk)
.WillOnce(Return(QueryResumableUploadResponse{kExcess, std::nullopt}));

std::shared_ptr<StorageConnectionImpl> client =
StorageConnectionImpl::Create(std::move(mock));
google::cloud::internal::OptionsSpan const span(
BasicTestPolicies().set<IdempotencyPolicyOption>(
StrictIdempotencyPolicy().clone()));

UploadChunkRequest request(
std::string{param.session_id}, 0, {{buffer_one, buffer_two}},
CreateNullHashFunction(), HashValues{"crc32c-val", "md5-val"});
request.set_option(CustomHeader("x-custom-header", "custom-header-value"));

StatusOr<QueryResumableUploadResponse> const response =
client->UploadChunk(request);
EXPECT_THAT(
response,
StatusIs(
StatusCode::kInternal,
AllOf(HasSubstr("session_id=[censored]"),
HasSubstr("result=UploadChunkResponse={committed_size=1024, "
"payload={}}"),
HasSubstr("offset=0"),
HasSubstr("range=<Content-Range: bytes 0-497/498>"),
HasSubstr("payload_size=498"),
HasSubstr("known_object_hashes={crc32c=crc32c-val, "
"md5=md5-val}"),
Not(HasSubstr(param.session_id)),
Not(HasSubstr(param.session_marker)),
Not(HasSubstr("chunk-payload-marker-one")),
Not(HasSubstr("chunk-payload-marker-two")),
Not(HasSubstr("AAAA")), Not(HasSubstr("BBBB")),
Not(HasSubstr("x-custom-header")),
Not(HasSubstr("custom-header-value")),
Not(HasSubstr("request=")))));
}

INSTANTIATE_TEST_SUITE_P(
SessionShapes, RetryClientUploadChunkFormatTest,
::testing::Values(
UploadChunkStatusFormatCase{
"RestUrl",
"https://storage.googleapis.com/upload/storage/v1/b/my-bucket/"
"o?uploadType=resumable&upload_id=xa298sd_sdlkj2",
"xa298sd_sdlkj2"},
UploadChunkStatusFormatCase{
"GrpcResourceId",
"projects/_/buckets/my-bucket/uploads/grpc-upload-token-42",
"grpc-upload-token-42"},
UploadChunkStatusFormatCase{"OpaqueToken", "opaque-upload-token-99",
"opaque-upload-token-99"}),
[](::testing::TestParamInfo<UploadChunkStatusFormatCase> const& info) {
return std::string{info.param.name};
});

/// @test Verify `UploadChunk` mismatch errors preserve an empty `session_id`.
TEST(RetryClientTest, UploadChunkFormatsEmptySessionId) {
constexpr std::uint64_t kRollbackOffset = 1024;
constexpr std::uint64_t kRollbackCommitted = 512;
constexpr std::uint64_t kOvercommitCommitted = 1024;
std::string const payload =
"chunk-payload-marker-one" + std::string(200, 'A');

auto mock = std::make_unique<MockGenericStub>();
EXPECT_CALL(*mock, options); // Required in StorageConnectionImpl::Create()
EXPECT_CALL(*mock, UploadChunk)
.WillOnce(Return(
QueryResumableUploadResponse{kRollbackCommitted, std::nullopt}))
.WillOnce(Return(
QueryResumableUploadResponse{kOvercommitCommitted, std::nullopt}));

std::shared_ptr<StorageConnectionImpl> client =
StorageConnectionImpl::Create(std::move(mock));
google::cloud::internal::OptionsSpan const span(
BasicTestPolicies().set<IdempotencyPolicyOption>(
StrictIdempotencyPolicy().clone()));

UploadChunkRequest const rollback_request(
"", kRollbackOffset, {{payload}}, CreateNullHashFunction(),
HashValues{"crc32c-val", "md5-val"});
StatusOr<QueryResumableUploadResponse> const rollback_response =
client->UploadChunk(rollback_request);
EXPECT_THAT(
rollback_response,
StatusIs(
StatusCode::kInternal,
AllOf(HasSubstr("session_id=, "
"result=UploadChunkResponse={committed_size=512, "
"payload={}}, offset=1024, "
"range=<Content-Range: bytes 1024-1247/1248>, "
"payload_size=224, "
"known_object_hashes={crc32c=crc32c-val, "
"md5=md5-val}"),
Not(HasSubstr("[censored]")))));

UploadChunkRequest const overcommit_request(
"", 0, {{payload}}, CreateNullHashFunction(),
HashValues{"crc32c-val", "md5-val"});
StatusOr<QueryResumableUploadResponse> const overcommit_response =
client->UploadChunk(overcommit_request);
EXPECT_THAT(
overcommit_response,
StatusIs(
StatusCode::kInternal,
AllOf(HasSubstr("session_id=, "
"result=UploadChunkResponse={committed_size=1024, "
"payload={}}, offset=0, "
"range=<Content-Range: bytes 0-223/224>, "
"payload_size=224, "
"known_object_hashes={crc32c=crc32c-val, "
"md5=md5-val}"),
Not(HasSubstr("[censored]")))));
}

/// @test Verify that retry exhaustion following a short write fails.
TEST(RetryClientTest, UploadChunkExhausted) {
auto const quantum = UploadChunkRequest::kChunkSizeQuantum;
Expand Down
Loading
Loading