-
Notifications
You must be signed in to change notification settings - Fork 459
feat(storage): add a static delay open request hedging strategy #16344
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
Open
ajayky-os
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
ajayky-os:feature/static-hedging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
google/cloud/storage/internal/hedged_object_read_source.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "google/cloud/storage/internal/hedged_object_read_source.h" | ||
| #include <atomic> | ||
| #include <cstring> | ||
| #include <future> | ||
| #include <utility> | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace storage { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
| namespace internal { | ||
| namespace { | ||
|
|
||
| struct RaceResult { | ||
| StatusOr<ReadSourceResult> result; | ||
| std::unique_ptr<ObjectReadSource> source; | ||
| std::unique_ptr<char[]> buffer; | ||
| }; | ||
|
|
||
| struct RaceState { | ||
| std::promise<RaceResult> promise; | ||
| std::atomic<bool> resolved{false}; | ||
| }; | ||
|
|
||
| // Opens a new child and performs its initial read, resolving the race if this | ||
| // attempt finishes first. Losing attempts close their child. Only the primary | ||
| // attempt resolves the race on an open error: a hedge that fails to open must | ||
| // not mask a slower, but successful, primary. | ||
| void RunAttempt(std::shared_ptr<RaceState> const& state, | ||
| HedgedObjectReadSource::ChildFactory const& factory, | ||
| std::size_t n, bool resolve_on_open_error, | ||
| std::shared_ptr<HedgingThreadPool> release_slot) { | ||
| struct SlotGuard { | ||
| std::shared_ptr<HedgingThreadPool> pool; | ||
| ~SlotGuard() { | ||
| if (pool) pool->ReleaseHedgeSlot(); | ||
| } | ||
| } guard{std::move(release_slot)}; | ||
|
|
||
| auto source = factory(); | ||
| if (!source) { | ||
| if (!resolve_on_open_error) return; | ||
| auto expected = false; | ||
| if (state->resolved.compare_exchange_strong(expected, true)) { | ||
| state->promise.set_value( | ||
| RaceResult{std::move(source).status(), nullptr, {}}); | ||
| } | ||
| return; | ||
| } | ||
| std::unique_ptr<char[]> buffer(new char[n]); | ||
| auto result = (*source)->Read(buffer.get(), n); | ||
| auto expected = false; | ||
| if (state->resolved.compare_exchange_strong(expected, true)) { | ||
| state->promise.set_value( | ||
| RaceResult{std::move(result), *std::move(source), std::move(buffer)}); | ||
| } else { | ||
| (*source)->Close(); | ||
| } | ||
| } | ||
|
ajayky-os marked this conversation as resolved.
|
||
|
|
||
| } // namespace | ||
|
|
||
| HedgedObjectReadSource::HedgedObjectReadSource( | ||
| std::shared_ptr<HedgingThreadPool> hedge_pool, ChildFactory child_factory, | ||
| std::chrono::milliseconds delay, int max_hedges) | ||
| : hedge_pool_(std::move(hedge_pool)), | ||
| child_factory_(std::move(child_factory)), | ||
| delay_(delay), | ||
| max_hedges_(max_hedges) {} | ||
|
|
||
| bool HedgedObjectReadSource::IsOpen() const { | ||
| if (active_child_) return active_child_->IsOpen(); | ||
| return true; | ||
| } | ||
|
|
||
| StatusOr<HttpResponse> HedgedObjectReadSource::Close() { | ||
| if (active_child_) return active_child_->Close(); | ||
| // The source was never read from, there is no child (or HTTP response) to | ||
| // close. | ||
| return HttpResponse{HttpStatusCode::kOk, {}, {}}; | ||
| } | ||
|
|
||
| StatusOr<ReadSourceResult> HedgedObjectReadSource::Read(char* buf, | ||
| std::size_t n) { | ||
| // Only the stream open is hedged. Once a child has won the race all | ||
| // subsequent reads continue on it, at its current offset, without any | ||
| // thread hops or extra copies. | ||
| if (active_child_) return active_child_->Read(buf, n); | ||
|
|
||
| auto state = std::make_shared<RaceState>(); | ||
| auto future = state->promise.get_future(); | ||
|
|
||
| auto primary = [state, factory = child_factory_, n] { | ||
| RunAttempt(state, factory, n, /*resolve_on_open_error=*/true, nullptr); | ||
| }; | ||
| // If the pool is shutting down run the attempt inline, the read must | ||
| // complete either way. | ||
| if (!hedge_pool_->Enqueue(primary)) primary(); | ||
|
|
||
| for (int i = 0; i != max_hedges_; ++i) { | ||
| if (future.wait_for(delay_) != std::future_status::timeout) break; | ||
| if (!hedge_pool_->TryAcquireHedgeToken()) continue; | ||
| auto hedge = [state, factory = child_factory_, n, pool = hedge_pool_] { | ||
| RunAttempt(state, factory, n, /*resolve_on_open_error=*/false, pool); | ||
| }; | ||
|
ajayky-os marked this conversation as resolved.
|
||
| if (!hedge_pool_->Enqueue(hedge)) { | ||
| hedge_pool_->ReleaseHedgeSlot(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| auto race = future.get(); | ||
| active_child_ = std::move(race.source); | ||
| if (race.result.ok() && race.result->bytes_received > 0) { | ||
| std::memcpy(buf, race.buffer.get(), race.result->bytes_received); | ||
| } | ||
|
ajayky-os marked this conversation as resolved.
|
||
| return race.result; | ||
| } | ||
|
|
||
| } // namespace internal | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
| } // namespace storage | ||
| } // namespace cloud | ||
| } // namespace google | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.