-
Notifications
You must be signed in to change notification settings - Fork 5.5k
grpc: implement TTL manager to re-buffer pending messages for bidirectional gRPC #18987
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
Merged
snowp
merged 12 commits into
envoyproxy:main
from
Shikugawa:buffered-message-ttl-manager
Feb 8, 2022
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1fca9d1
grpc: implement TTL manager to track buffered bidirectional gRPC mess…
Shikugawa 3fe2bbd
add test
Shikugawa ffd38ce
add test
Shikugawa 5843d85
fix
Shikugawa dad969a
fix
Shikugawa 0d6de8b
fix
Shikugawa 0fdedac
fix
Shikugawa 8fb11f7
fix
Shikugawa 709afff
tidy
Shikugawa ed4434b
fix
Shikugawa a6445b8
fix
Shikugawa 2b6919e
fix
Shikugawa 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #pragma once | ||
|
|
||
| #include <queue> | ||
|
|
||
| #include "envoy/event/dispatcher.h" | ||
|
|
||
| #include "source/common/grpc/buffered_async_client.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Grpc { | ||
|
|
||
| class BufferedMessageTtlManager { | ||
| public: | ||
| BufferedMessageTtlManager(Event::Dispatcher& dispatcher, BufferedAsyncClientCallbacks& callbacks, | ||
| std::chrono::milliseconds message_ack_timeout) | ||
| : dispatcher_(dispatcher), message_ack_timeout_(message_ack_timeout), callbacks_(callbacks), | ||
| timer_(dispatcher_.createTimer([this] { checkMessages(); })) {} | ||
|
|
||
| ~BufferedMessageTtlManager() { timer_->disableTimer(); } | ||
|
|
||
| void setDeadline(absl::flat_hash_set<uint64_t>&& ids) { | ||
|
Shikugawa marked this conversation as resolved.
Outdated
|
||
| const auto expires_at = dispatcher_.timeSource().monotonicTime() + message_ack_timeout_; | ||
| deadline_.emplace(expires_at, std::move(ids)); | ||
|
|
||
| if (!timer_->enabled()) { | ||
| timer_->enableTimer(message_ack_timeout_); | ||
| } | ||
| } | ||
|
|
||
| const std::queue<std::pair<MonotonicTime, absl::flat_hash_set<uint64_t>>>& deadline() { | ||
|
Shikugawa marked this conversation as resolved.
Outdated
|
||
| return deadline_; | ||
| } | ||
|
|
||
| private: | ||
| void checkMessages() { | ||
|
Shikugawa marked this conversation as resolved.
Outdated
|
||
| const auto now = dispatcher_.timeSource().monotonicTime(); | ||
|
|
||
| while (!deadline_.empty()) { | ||
| auto& it = deadline_.front(); | ||
| if (it.first > now) { | ||
| break; | ||
| } | ||
| for (auto&& id : it.second) { | ||
| // If the retrieved message is a PendingFlush, it means that the message | ||
| // has timed out. A timeout is treated as an error, and the callback will | ||
| // re-buffer the message. | ||
| callbacks_.onError(id); | ||
|
Shikugawa marked this conversation as resolved.
Outdated
Shikugawa marked this conversation as resolved.
Outdated
|
||
| } | ||
| deadline_.pop(); | ||
| } | ||
|
|
||
| if (!deadline_.empty()) { | ||
| const auto earliest_timepoint = deadline_.front().first; | ||
| timer_->enableTimer( | ||
| std::chrono::duration_cast<std::chrono::milliseconds>(earliest_timepoint - now)); | ||
| } | ||
| } | ||
|
|
||
| Event::Dispatcher& dispatcher_; | ||
| std::chrono::milliseconds message_ack_timeout_; | ||
| BufferedAsyncClientCallbacks& callbacks_; | ||
| Event::TimerPtr timer_; | ||
| std::queue<std::pair<MonotonicTime, absl::flat_hash_set<uint64_t>>> deadline_; | ||
| }; | ||
| } // namespace Grpc | ||
| } // namespace Envoy | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #include <chrono> | ||
| #include <memory> | ||
|
|
||
| #include "source/common/event/dispatcher_impl.h" | ||
| #include "source/common/grpc/buffered_message_ttl_manager.h" | ||
|
|
||
| #include "test/test_common/utility.h" | ||
|
|
||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Grpc { | ||
| namespace { | ||
|
|
||
| class MockBufferedAsyncClientCallbacks : public BufferedAsyncClientCallbacks { | ||
| public: | ||
| MOCK_METHOD(void, onSuccess, (uint64_t), ()); | ||
| MOCK_METHOD(void, onError, (uint64_t), ()); | ||
| }; | ||
|
|
||
| using testing::Invoke; | ||
| using testing::NiceMock; | ||
|
|
||
| class BufferedMessageTtlManagerTest : public testing::Test { | ||
| public: | ||
| BufferedMessageTtlManagerTest() | ||
| : api_(Api::createApiForTest()), dispatcher_(api_->allocateDispatcher("test_thread")) {} | ||
|
|
||
| void SetUp() override { | ||
| ttl_manager_ = std::make_shared<BufferedMessageTtlManager>(*dispatcher_, callbacks_, msec_); | ||
| } | ||
|
|
||
| Api::ApiPtr api_; | ||
| Event::DispatcherPtr dispatcher_; | ||
| std::shared_ptr<BufferedMessageTtlManager> ttl_manager_; | ||
| NiceMock<MockBufferedAsyncClientCallbacks> callbacks_; | ||
| std::chrono::milliseconds msec_{1000}; | ||
| }; | ||
|
|
||
| TEST_F(BufferedMessageTtlManagerTest, Basic) { | ||
|
Shikugawa marked this conversation as resolved.
Outdated
|
||
| absl::flat_hash_set<uint64_t> ids{0}; | ||
| EXPECT_CALL(callbacks_, onError(_)).WillOnce(Invoke([this] { | ||
| EXPECT_EQ(ttl_manager_->deadline().size(), 1); | ||
| absl::flat_hash_set<uint64_t> ids{1, 2}; | ||
| EXPECT_CALL(callbacks_, onError(_)).Times(2); | ||
| ttl_manager_->setDeadline(std::move(ids)); | ||
| })); | ||
| ttl_manager_->setDeadline(std::move(ids)); | ||
|
|
||
| dispatcher_->run(Event::Dispatcher::RunType::Block); | ||
| EXPECT_EQ(ttl_manager_->deadline().size(), 0); | ||
|
|
||
| // Test if deadline queue is empty after queue cleared once. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this comment be before L54?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| absl::flat_hash_set<uint64_t> ids2{3}; | ||
| EXPECT_CALL(callbacks_, onError(_)).WillOnce(Invoke([this] { | ||
| EXPECT_EQ(ttl_manager_->deadline().size(), 1); | ||
| })); | ||
| ttl_manager_->setDeadline(std::move(ids2)); | ||
|
|
||
| dispatcher_->run(Event::Dispatcher::RunType::Block); | ||
| EXPECT_EQ(ttl_manager_->deadline().size(), 0); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace Grpc | ||
| } // namespace Envoy | ||
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.