Skip to content

Commit 19f9509

Browse files
authored
feat(storage): Update the appendable object function to store the Object metadata (#15422)
1 parent e44dbfa commit 19f9509

File tree

8 files changed

+36
-30
lines changed

8 files changed

+36
-30
lines changed

google/cloud/storage/async/client.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ AsyncClient::StartAppendableObjectUpload(
136136
.then([](auto f) -> StatusOr<std::pair<AsyncWriter, AsyncToken>> {
137137
auto w = f.get();
138138
if (!w) return std::move(w).status();
139-
auto t = absl::holds_alternative<google::storage::v2::Object>(
140-
(*w)->PersistedState())
141-
? AsyncToken()
142-
: storage_internal::MakeAsyncToken(w->get());
139+
auto t = storage_internal::MakeAsyncToken(w->get());
143140
return std::make_pair(AsyncWriter(*std::move(w)), std::move(t));
144141
});
145142
}
@@ -163,10 +160,7 @@ AsyncClient::ResumeAppendableObjectUpload(BucketName const& bucket_name,
163160
.then([](auto f) -> StatusOr<std::pair<AsyncWriter, AsyncToken>> {
164161
auto w = f.get();
165162
if (!w) return std::move(w).status();
166-
auto t = absl::holds_alternative<google::storage::v2::Object>(
167-
(*w)->PersistedState())
168-
? AsyncToken()
169-
: storage_internal::MakeAsyncToken(w->get());
163+
auto t = storage_internal::MakeAsyncToken(w->get());
170164
return std::make_pair(AsyncWriter(*std::move(w)), std::move(t));
171165
});
172166
}

google/cloud/storage/async/client_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ TEST(AsyncClient, StartAppendableObjectUpload1) {
400400
EXPECT_TRUE(TextFormat::ParseFromString(kExpectedRequest, &expected));
401401
EXPECT_THAT(p.request, IsProtoEqual(expected));
402402
auto writer = std::make_unique<MockAsyncWriterConnection>();
403-
EXPECT_CALL(*writer, PersistedState).WillOnce(Return(0));
403+
EXPECT_CALL(*writer, PersistedState).Times(0);
404404
EXPECT_CALL(*writer, Finalize).WillRepeatedly([] {
405405
return make_ready_future(make_status_or(TestProtoObject()));
406406
});
@@ -446,7 +446,7 @@ TEST(AsyncClient, StartAppendableObjectUpload2) {
446446
EXPECT_TRUE(TextFormat::ParseFromString(kExpectedRequest, &expected));
447447
EXPECT_THAT(p.request, IsProtoEqual(expected));
448448
auto writer = std::make_unique<MockAsyncWriterConnection>();
449-
EXPECT_CALL(*writer, PersistedState).WillOnce(Return(0));
449+
EXPECT_CALL(*writer, PersistedState).Times(0);
450450
EXPECT_CALL(*writer, Finalize).WillRepeatedly([] {
451451
return make_ready_future(make_status_or(TestProtoObject()));
452452
});
@@ -513,7 +513,7 @@ TEST(AsyncClient, ResumeAppendableObjectUpload1) {
513513
AsyncWriter w;
514514
AsyncToken t;
515515
std::tie(w, t) = *std::move(wt);
516-
EXPECT_FALSE(t.valid());
516+
EXPECT_TRUE(t.valid());
517517
EXPECT_THAT(w.PersistedState(), VariantWith<google::storage::v2::Object>(
518518
IsProtoEqual(TestProtoObject())));
519519
}

google/cloud/storage/internal/async/connection_impl.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,17 @@ AsyncConnectionImpl::AppendableObjectUploadImpl(AppendableUploadParams p) {
384384
std::unique_ptr<storage_experimental::AsyncWriterConnection>> {
385385
auto rpc = f.get();
386386
if (!rpc) return std::move(rpc).status();
387-
persisted_size = rpc->first_response.resource().size();
388-
auto impl = std::make_unique<AsyncWriterConnectionImpl>(
389-
current, request, std::move(rpc->stream), hash, persisted_size,
390-
false);
387+
std::unique_ptr<AsyncWriterConnectionImpl> impl;
388+
if (rpc->first_response.has_resource()) {
389+
impl = std::make_unique<AsyncWriterConnectionImpl>(
390+
current, request, std::move(rpc->stream), hash,
391+
rpc->first_response.resource(), false);
392+
} else {
393+
persisted_size = rpc->first_response.persisted_size();
394+
impl = std::make_unique<AsyncWriterConnectionImpl>(
395+
current, request, std::move(rpc->stream), hash, persisted_size,
396+
false);
397+
}
391398
return MakeWriterConnectionResumed(std::move(fa), std::move(impl),
392399
std::move(request), std::move(hash),
393400
rpc->first_response, *current);

google/cloud/storage/internal/async/connection_impl_appendable_upload_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ std::unique_ptr<AsyncBidiWriteObjectStream> MakeSuccessfulAppendStream(
106106
return sequencer.PushBack("Read(PersistedSize)")
107107
.then([persisted_size](auto) {
108108
auto response = google::storage::v2::BidiWriteObjectResponse{};
109-
response.mutable_resource()->set_size(persisted_size);
109+
response.set_persisted_size(persisted_size);
110110
return absl::make_optional(std::move(response));
111111
});
112112
})

google/cloud/storage/internal/async/writer_connection_impl.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ AsyncWriterConnectionImpl::AsyncWriterConnectionImpl(
6060
google::storage::v2::BidiWriteObjectRequest request,
6161
std::unique_ptr<StreamingRpc> impl,
6262
std::shared_ptr<storage::internal::HashFunction> hash_function,
63-
google::storage::v2::Object metadata)
64-
: AsyncWriterConnectionImpl(std::move(options), std::move(request),
65-
std::move(impl), std::move(hash_function),
66-
PersistedStateType(std::move(metadata)),
67-
/*offset=*/0) {}
63+
google::storage::v2::Object metadata, bool first_request)
64+
: AsyncWriterConnectionImpl(
65+
std::move(options), std::move(request), std::move(impl),
66+
std::move(hash_function), PersistedStateType(metadata),
67+
/*offset=*/metadata.size(), std::move(first_request)) {}
6868

6969
AsyncWriterConnectionImpl::AsyncWriterConnectionImpl(
7070
google::cloud::internal::ImmutableOptions options,

google/cloud/storage/internal/async/writer_connection_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AsyncWriterConnectionImpl
4848
google::storage::v2::BidiWriteObjectRequest request,
4949
std::unique_ptr<StreamingRpc> impl,
5050
std::shared_ptr<storage::internal::HashFunction> hash_function,
51-
google::storage::v2::Object metadata);
51+
google::storage::v2::Object metadata, bool first_request = true);
5252
~AsyncWriterConnectionImpl() override;
5353

5454
void Cancel() override { return impl_->Cancel(); }

google/cloud/storage/internal/async/writer_connection_resumed.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,10 @@ class AsyncWriterConnectionResumedState
7878
options_ = internal::MakeImmutableOptions(options);
7979
auto state = impl_->PersistedState();
8080
if (absl::holds_alternative<google::storage::v2::Object>(state)) {
81-
SetFinalized(std::unique_lock<std::mutex>(mu_),
82-
absl::get<google::storage::v2::Object>(std::move(state)));
83-
cancelled_ = true;
84-
resume_status_ = internal::CancelledError("upload already finalized",
85-
GCP_ERROR_INFO());
86-
return;
81+
buffer_offset_ = absl::get<google::storage::v2::Object>(state).size();
82+
} else {
83+
buffer_offset_ = absl::get<std::int64_t>(state);
8784
}
88-
buffer_offset_ = absl::get<std::int64_t>(state);
8985
}
9086

9187
void Cancel() {

google/cloud/storage/internal/async/writer_connection_resumed_test.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ TEST(WriteConnectionResumed, FinalizedOnConstruction) {
101101
auto mock = std::make_unique<MockAsyncWriterConnection>();
102102
EXPECT_CALL(*mock, UploadId).WillRepeatedly(Return("test-upload-id"));
103103
EXPECT_CALL(*mock, PersistedState).WillRepeatedly(Return(TestObject()));
104-
EXPECT_CALL(*mock, Finalize).Times(0);
104+
EXPECT_CALL(*mock, Finalize(_)).WillOnce([&](auto) {
105+
return sequencer.PushBack("Finalize").then([](auto) {
106+
return StatusOr<google::storage::v2::Object>(TestObject());
107+
});
108+
});
105109

106110
MockFactory mock_factory;
107111
EXPECT_CALL(mock_factory, Call).Times(0);
@@ -115,6 +119,11 @@ TEST(WriteConnectionResumed, FinalizedOnConstruction) {
115119
VariantWith<google::storage::v2::Object>(IsProtoEqual(TestObject())));
116120

117121
auto finalize = connection->Finalize({});
122+
123+
EXPECT_FALSE(finalize.is_ready());
124+
auto next = sequencer.PopFrontWithName();
125+
EXPECT_EQ(next.second, "Finalize");
126+
next.first.set_value(true);
118127
EXPECT_TRUE(finalize.is_ready());
119128
EXPECT_THAT(finalize.get(), IsOkAndHolds(IsProtoEqual(TestObject())));
120129
}

0 commit comments

Comments
 (0)