-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Listener: respect the connection balancer of the redirected listener #15842
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
Merged
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3515417
techdebt: clean up connection_handler_impl dependency issue
lambdai 99bf747
pass all existing tests
lambdai 3b9aec1
add initial active_tcp_listener test
lambdai fd23487
fixing test
lambdai a5f26c9
llvm-11 formatter
lambdai aede638
fix format
lambdai fddcb00
fix quic
lambdai 0532563
ctidy
lambdai 4402535
Merge branch 'main' into rebalancer
lambdai 9ae7490
fix merge
lambdai ad29747
clean up
lambdai 4c28c5d
dont use atomic in mock
lambdai 01b8d5a
add integration test
lambdai 11ff0f3
Merge branch 'main' into rebalancer
lambdai cda38a9
cleanup
lambdai 3b25b2b
add version history
lambdai cafb8c5
more doc
lambdai 46e8732
doc
lambdai 381423a
sync api v4
lambdai 799d2e1
cleared -> disabled
lambdai be4033a
update proto inline comment
lambdai 726ba10
add ref
lambdai eeb6413
add comment and rename to virtual_listener
lambdai 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| #include <memory> | ||
|
|
||
| #include "envoy/network/filter.h" | ||
| #include "envoy/network/listener.h" | ||
| #include "envoy/stats/scope.h" | ||
|
|
||
| #include "common/network/address_impl.h" | ||
| #include "common/network/connection_balancer_impl.h" | ||
| #include "common/network/raw_buffer_socket.h" | ||
| #include "common/network/utility.h" | ||
|
|
||
| #include "server/active_tcp_listener.h" | ||
|
|
||
| #include "test/mocks/api/mocks.h" | ||
| #include "test/mocks/common.h" | ||
| #include "test/mocks/network/mocks.h" | ||
| #include "test/test_common/network_utility.h" | ||
|
|
||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using testing::_; | ||
| using testing::Invoke; | ||
| using testing::NiceMock; | ||
| using testing::Return; | ||
| using testing::ReturnRef; | ||
|
|
||
| namespace Envoy { | ||
| namespace Server { | ||
| namespace { | ||
|
|
||
| class MockTcpConnectionHandler : public Network::TcpConnectionHandler, | ||
|
lambdai marked this conversation as resolved.
|
||
| public Network::MockConnectionHandler { | ||
| public: | ||
| MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); | ||
| MOCK_METHOD(Network::BalancedConnectionHandlerOptRef, getBalancedHandlerByTag, | ||
| (uint64_t listener_tag)); | ||
| MOCK_METHOD(Network::BalancedConnectionHandlerOptRef, getBalancedHandlerByAddress, | ||
| (const Network::Address::Instance& address)); | ||
| }; | ||
| class ActiveTcpListenerTest : public testing::Test, protected Logger::Loggable<Logger::Id::main> { | ||
| public: | ||
| ActiveTcpListenerTest() { | ||
| EXPECT_CALL(conn_handler_, dispatcher()).WillRepeatedly(ReturnRef(dispatcher_)); | ||
| EXPECT_CALL(conn_handler_, numConnections()).Times(testing::AnyNumber()); | ||
|
lambdai marked this conversation as resolved.
|
||
| EXPECT_CALL(conn_handler_, statPrefix()).WillRepeatedly(ReturnRef(listener_stat_prefix_)); | ||
| listener_filter_matcher_ = std::make_shared<NiceMock<Network::MockListenerFilterMatcher>>(); | ||
| } | ||
|
|
||
| std::string listener_stat_prefix_{"listener_stat_prefix"}; | ||
| std::shared_ptr<Network::MockListenSocketFactory> socket_factory_{ | ||
| std::make_shared<Network::MockListenSocketFactory>()}; | ||
| NiceMock<Event::MockDispatcher> dispatcher_{"test"}; | ||
| BasicResourceLimitImpl resource_limit_; | ||
| NiceMock<MockTcpConnectionHandler> conn_handler_; | ||
| Network::MockListener* generic_listener_; | ||
| Network::MockListenerConfig listener_config_; | ||
| NiceMock<Network::MockFilterChainManager> manager_; | ||
| NiceMock<Network::MockFilterChainFactory> filter_chain_factory_; | ||
| std::shared_ptr<Network::MockFilterChain> filter_chain_; | ||
| std::shared_ptr<NiceMock<Network::MockListenerFilterMatcher>> listener_filter_matcher_; | ||
| }; | ||
|
|
||
| // Verify that the server connection with recovered address is rebalanced at redirected listener. | ||
| TEST_F(ActiveTcpListenerTest, RedirectedRebalancer) { | ||
| NiceMock<Network::MockListenerConfig> listener_config1; | ||
| NiceMock<Network::MockConnectionBalancer> balancer1; | ||
| EXPECT_CALL(balancer1, registerHandler(_)); | ||
| EXPECT_CALL(balancer1, unregisterHandler(_)); | ||
|
|
||
| Network::Address::InstanceConstSharedPtr normal_address( | ||
| new Network::Address::Ipv4Instance("127.0.0.1", 10001)); | ||
| EXPECT_CALL(*socket_factory_, localAddress()).WillRepeatedly(ReturnRef(normal_address)); | ||
| EXPECT_CALL(listener_config1, connectionBalancer()).WillRepeatedly(ReturnRef(balancer1)); | ||
| EXPECT_CALL(listener_config1, listenerScope).Times(testing::AnyNumber()); | ||
| EXPECT_CALL(listener_config1, listenerFiltersTimeout()); | ||
| EXPECT_CALL(listener_config1, continueOnListenerFiltersTimeout()); | ||
| EXPECT_CALL(listener_config1, filterChainManager()).WillRepeatedly(ReturnRef(manager_)); | ||
| EXPECT_CALL(listener_config1, openConnections()).WillRepeatedly(ReturnRef(resource_limit_)); | ||
| EXPECT_CALL(listener_config1, handOffRestoredDestinationConnections()) | ||
| .WillRepeatedly(Return(true)); | ||
|
|
||
| auto mock_listener_will_be_moved1 = std::make_unique<Network::MockListener>(); | ||
| auto& listener1 = *mock_listener_will_be_moved1; | ||
| auto active_listener1 = std::make_unique<ActiveTcpListener>( | ||
| conn_handler_, std::move(mock_listener_will_be_moved1), listener_config1); | ||
|
|
||
| NiceMock<Network::MockListenerConfig> listener_config2; | ||
| Network::MockConnectionBalancer balancer2; | ||
| EXPECT_CALL(balancer2, registerHandler(_)); | ||
| EXPECT_CALL(balancer2, unregisterHandler(_)); | ||
|
|
||
| Network::Address::InstanceConstSharedPtr alt_address( | ||
| new Network::Address::Ipv4Instance("127.0.0.2", 20002)); | ||
| EXPECT_CALL(*socket_factory_, localAddress()).WillRepeatedly(ReturnRef(alt_address)); | ||
| EXPECT_CALL(listener_config2, listenerFiltersTimeout()); | ||
| EXPECT_CALL(listener_config2, connectionBalancer()).WillRepeatedly(ReturnRef(balancer2)); | ||
| EXPECT_CALL(listener_config2, listenerScope).Times(testing::AnyNumber()); | ||
| EXPECT_CALL(listener_config2, handOffRestoredDestinationConnections()) | ||
| .WillRepeatedly(Return(false)); | ||
| EXPECT_CALL(listener_config2, continueOnListenerFiltersTimeout()); | ||
| EXPECT_CALL(listener_config2, filterChainManager()).WillRepeatedly(ReturnRef(manager_)); | ||
| EXPECT_CALL(listener_config2, openConnections()).WillRepeatedly(ReturnRef(resource_limit_)); | ||
| auto mock_listener_will_be_moved2 = std::make_unique<Network::MockListener>(); | ||
| auto& listener2 = *mock_listener_will_be_moved2; | ||
| auto active_listener2 = std::make_shared<ActiveTcpListener>( | ||
| conn_handler_, std::move(mock_listener_will_be_moved2), listener_config2); | ||
|
|
||
| auto* test_filter = new NiceMock<Network::MockListenerFilter>(); | ||
| EXPECT_CALL(*test_filter, destroy_()); | ||
| Network::MockConnectionSocket* accepted_socket = new NiceMock<Network::MockConnectionSocket>(); | ||
| bool redirected = false; | ||
|
|
||
| // 1. Listener1 re-balance. Set the balance target to the the active listener itself. | ||
| EXPECT_CALL(balancer1, pickTargetHandler(_)) | ||
| .WillOnce(testing::DoAll( | ||
| testing::WithArg<0>(Invoke([](auto& target) { target.incNumConnections(); })), | ||
| ReturnRef(*active_listener1))); | ||
|
|
||
| EXPECT_CALL(listener_config1, filterChainFactory()) | ||
| .WillRepeatedly(ReturnRef(filter_chain_factory_)); | ||
|
|
||
| // Listener1 has a listener filter in the listener filter chain. | ||
| EXPECT_CALL(filter_chain_factory_, createListenerFilterChain(_)) | ||
| .WillRepeatedly(Invoke([&](Network::ListenerFilterManager& manager) -> bool { | ||
| // Insert the Mock filter. | ||
| if (!redirected) { | ||
| manager.addAcceptFilter(nullptr, Network::ListenerFilterPtr{test_filter}); | ||
| redirected = true; | ||
| } | ||
| return true; | ||
| })); | ||
| EXPECT_CALL(*test_filter, onAccept(_)) | ||
| .WillOnce(Invoke([&](Network::ListenerFilterCallbacks& cb) -> Network::FilterStatus { | ||
| cb.socket().addressProvider().restoreLocalAddress(alt_address); | ||
| return Network::FilterStatus::Continue; | ||
| })); | ||
| // Verify that listener1 hands off the connection by not creating network filter chain. | ||
| EXPECT_CALL(manager_, findFilterChain(_)).Times(0); | ||
|
|
||
| // 2. Redirect to Listener2. | ||
| EXPECT_CALL(conn_handler_, getBalancedHandlerByAddress(_)) | ||
| .WillOnce(Return(Network::BalancedConnectionHandlerOptRef(*active_listener2))); | ||
|
|
||
| // 3. Listener2 re-balance. Set the balance target to the the active listener itself. | ||
| EXPECT_CALL(balancer2, pickTargetHandler(_)) | ||
| .WillOnce(testing::DoAll( | ||
| testing::WithArg<0>(Invoke([](auto& target) { target.incNumConnections(); })), | ||
| ReturnRef(*active_listener2))); | ||
|
|
||
| auto filter_factory_callback = std::make_shared<std::vector<Network::FilterFactoryCb>>(); | ||
| auto transport_socket_factory = Network::Test::createRawBufferSocketFactory(); | ||
| filter_chain_ = std::make_shared<NiceMock<Network::MockFilterChain>>(); | ||
|
|
||
| EXPECT_CALL(conn_handler_, incNumConnections()); | ||
| EXPECT_CALL(manager_, findFilterChain(_)).WillOnce(Return(filter_chain_.get())); | ||
| EXPECT_CALL(*filter_chain_, transportSocketFactory) | ||
| .WillOnce(testing::ReturnRef(*transport_socket_factory)); | ||
| EXPECT_CALL(*filter_chain_, networkFilterFactories).WillOnce(ReturnRef(*filter_factory_callback)); | ||
| EXPECT_CALL(listener_config2, filterChainFactory()) | ||
| .WillRepeatedly(ReturnRef(filter_chain_factory_)); | ||
|
|
||
| auto* connection = new NiceMock<Network::MockServerConnection>(); | ||
| EXPECT_CALL(dispatcher_, createServerConnection_()).WillOnce(Return(connection)); | ||
| EXPECT_CALL(filter_chain_factory_, createNetworkFilterChain(_, _)).WillOnce(Return(true)); | ||
| active_listener1->onAccept(Network::ConnectionSocketPtr{accepted_socket}); | ||
|
|
||
| // Verify per-listener connection stats. | ||
| EXPECT_EQ(1UL, conn_handler_.numConnections()); | ||
|
|
||
| EXPECT_CALL(conn_handler_, decNumConnections()); | ||
| connection->close(Network::ConnectionCloseType::NoFlush); | ||
|
|
||
| EXPECT_CALL(listener1, onDestroy()); | ||
| active_listener1.reset(); | ||
| EXPECT_CALL(listener2, onDestroy()); | ||
| active_listener2.reset(); | ||
| } | ||
| } // namespace | ||
| } // namespace Server | ||
| } // 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.