Skip to content

Commit 72e7aa0

Browse files
committed
checkers-pr fix, handle resume and cancellation
1 parent 197cb44 commit 72e7aa0

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ ObjectDescriptorImpl::ObjectDescriptorImpl(
3939
read_object_spec_(std::move(read_object_spec)),
4040
options_(std::move(options)),
4141
streams_{std::move(stream)} {
42-
std::unordered_map<std::int64_t, std::shared_ptr<ReadRange>> initial_ranges;
43-
active_ranges_.push_back(std::move(initial_ranges));
44-
}
42+
AddNewActiveRanges();
43+
}
4544

4645
ObjectDescriptorImpl::~ObjectDescriptorImpl() {
4746
for (auto const& stream : streams_) {
@@ -60,6 +59,10 @@ void ObjectDescriptorImpl::Cancel() {
6059
}
6160
}
6261

62+
void ObjectDescriptorImpl::CancelStream(std::shared_ptr<OpenStream> stream) {
63+
stream->Cancel();
64+
}
65+
6366
absl::optional<google::storage::v2::Object> ObjectDescriptorImpl::metadata()
6467
const {
6568
std::unique_lock<std::mutex> lk(mu_);
@@ -76,8 +79,7 @@ void ObjectDescriptorImpl::MakeSubsequentStream() {
7679
std::unique_lock<std::mutex> lk(mu_);
7780
active_stream_ = streams_.size();
7881
streams_.push_back(std::move(stream));
79-
std::unordered_map<std::int64_t, std::shared_ptr<ReadRange>> active_ranges;
80-
active_ranges_.push_back(std::move(active_ranges));
82+
AddNewActiveRanges(lk);
8183
lk.unlock();
8284
OnRead(std::move(stream_result->first_response));
8385
}
@@ -173,7 +175,7 @@ void ObjectDescriptorImpl::OnRead(
173175

174176
void ObjectDescriptorImpl::CleanupDoneRanges(
175177
std::unique_lock<std::mutex> const&) {
176-
auto &active_ranges = active_ranges_[active_stream_];
178+
auto& active_ranges = active_ranges_[active_stream_];
177179
for (auto i = active_ranges.begin(); i != active_ranges.end();) {
178180
if (i->second->IsDone()) {
179181
i = active_ranges.erase(i);
@@ -204,6 +206,10 @@ void ObjectDescriptorImpl::OnFinish(Status const& status) {
204206
for (auto const& kv : copy) {
205207
kv.second->OnFinish(status);
206208
}
209+
CancelStream(streams_[active_stream_]);
210+
streams_.erase(streams_.begin() + active_stream_);
211+
active_ranges_.erase(active_ranges_.begin() + active_stream_);
212+
active_stream_ = streams_.size();
207213
}
208214

209215
void ObjectDescriptorImpl::Resume(google::rpc::Status const& proto_status) {
@@ -228,7 +234,9 @@ void ObjectDescriptorImpl::Resume(google::rpc::Status const& proto_status) {
228234
void ObjectDescriptorImpl::OnResume(StatusOr<OpenStreamResult> result) {
229235
if (!result) return OnFinish(std::move(result).status());
230236
std::unique_lock<std::mutex> lk(mu_);
231-
streams_[0] = std::move(result->stream);
237+
active_stream_ = streams_.size();
238+
streams_.push_back(std::move(result->stream));
239+
AddNewActiveRanges(lk);
232240
// TODO(#15105) - this should be done without release the lock.
233241
Flush(std::move(lk));
234242
OnRead(std::move(result->first_response));

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,20 @@ class ObjectDescriptorImpl
7979
return CopyActiveRanges(std::unique_lock<std::mutex>(mu_));
8080
}
8181

82+
auto AddNewActiveRanges(std::unique_lock<std::mutex> const&) {
83+
std::unordered_map<std::int64_t, std::shared_ptr<ReadRange>> active_ranges;
84+
return active_ranges_.push_back(std::move(active_ranges));
85+
}
86+
87+
auto AddNewActiveRanges() {
88+
return AddNewActiveRanges(std::unique_lock<std::mutex>(mu_));
89+
}
90+
8291
auto CurrentStream(std::unique_lock<std::mutex>) const {
8392
return streams_[active_stream_];
8493
}
8594

95+
void CancelStream(std::shared_ptr<OpenStream> stream);
8696
void Flush(std::unique_lock<std::mutex> lk);
8797
void OnWrite(bool ok);
8898
void DoRead(std::unique_lock<std::mutex>);
@@ -106,7 +116,8 @@ class ObjectDescriptorImpl
106116
bool write_pending_ = false;
107117
google::storage::v2::BidiReadObjectRequest next_request_;
108118

109-
std::vector<std::unordered_map<std::int64_t, std::shared_ptr<ReadRange>>> active_ranges_;
119+
std::vector<std::unordered_map<std::int64_t, std::shared_ptr<ReadRange>>>
120+
active_ranges_;
110121
Options options_;
111122
std::size_t active_stream_ = 0;
112123
std::vector<std::shared_ptr<OpenStream>> streams_;

0 commit comments

Comments
 (0)