-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
extensions: add retry host predicate for attempted hosts #4452
Merged
Merged
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 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 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 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,17 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_library", | ||
"envoy_package", | ||
) | ||
|
||
envoy_package() | ||
|
||
envoy_cc_library( | ||
name = "well_known_names", | ||
hdrs = ["well_known_names.h"], | ||
deps = [ | ||
"//source/common/singleton:const_singleton", | ||
], | ||
) |
This file contains 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,29 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_library", | ||
"envoy_package", | ||
) | ||
|
||
envoy_package() | ||
|
||
envoy_cc_library( | ||
name = "other_hosts_predicate_lib", | ||
hdrs = ["other_hosts.h"], | ||
deps = [ | ||
"//include/envoy/upstream:retry_interface", | ||
], | ||
) | ||
|
||
envoy_cc_library( | ||
name = "config", | ||
srcs = ["config.cc"], | ||
hdrs = ["config.h"], | ||
deps = [ | ||
":other_hosts_predicate_lib", | ||
"//include/envoy/registry", | ||
"//include/envoy/upstream:retry_interface", | ||
"//source/extensions/retry/host:well_known_names", | ||
], | ||
) |
This file contains 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,17 @@ | ||
#include "extensions/retry/host/other_hosts/config.h" | ||
|
||
#include "envoy/registry/registry.h" | ||
#include "envoy/upstream/retry.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Retry { | ||
namespace Host { | ||
|
||
static Registry::RegisterFactory<OtherHostsRetryPredicateFactory, | ||
Upstream::RetryHostPredicateFactory> | ||
register_; | ||
} | ||
} // namespace Retry | ||
} // namespace Extensions | ||
} // namespace Envoy |
This file contains 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,26 @@ | ||
#pragma once | ||
|
||
#include "envoy/upstream/retry.h" | ||
|
||
#include "extensions/retry/host/other_hosts/other_hosts.h" | ||
#include "extensions/retry/host/well_known_names.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Retry { | ||
namespace Host { | ||
|
||
class OtherHostsRetryPredicateFactory : public Upstream::RetryHostPredicateFactory { | ||
public: | ||
void createHostPredicate(Upstream::RetryHostPredicateFactoryCallbacks& callbacks, | ||
const Protobuf::Message&) override { | ||
callbacks.addHostPredicate(std::make_shared<OtherHostsRetryPredicate>()); | ||
} | ||
|
||
std::string name() override { return RetryHostPredicateValues::get().PreviousHostsPredicate; } | ||
}; | ||
|
||
} // namespace Host | ||
} // namespace Retry | ||
} // namespace Extensions | ||
} // namespace Envoy |
This file contains 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,19 @@ | ||
#pragma once | ||
|
||
#include "envoy/upstream/retry.h" | ||
#include "envoy/upstream/upstream.h" | ||
|
||
namespace Envoy { | ||
class OtherHostsRetryPredicate : public Upstream::RetryHostPredicate { | ||
public: | ||
bool shouldSelectAnotherHost(const Upstream::Host& candidate_host) override { | ||
return attempted_hosts_.find(candidate_host.address()->asString()) != attempted_hosts_.end(); | ||
} | ||
void onHostAttempted(Upstream::HostDescriptionConstSharedPtr attempted_host) override { | ||
attempted_hosts_.insert(attempted_host->address()->asString()); | ||
} | ||
|
||
private: | ||
std::unordered_set<std::string> attempted_hosts_; | ||
}; | ||
} // namespace Envoy |
This file contains 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,24 @@ | ||
#pragma once | ||
|
||
#include "common/singleton/const_singleton.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Retry { | ||
namespace Host { | ||
|
||
/** | ||
* Well-known retry host predicate names. | ||
*/ | ||
class RetryHostPredicatesNameValues { | ||
public: | ||
// Previous host predicate. Rejects hosts that have already been tried. | ||
const std::string PreviousHostsPredicate = "envoy.retry_host_predicates.previous_hosts"; | ||
}; | ||
|
||
typedef ConstSingleton<RetryHostPredicatesNameValues> RetryHostPredicateValues; | ||
|
||
} // namespace Host | ||
} // namespace Retry | ||
} // namespace Extensions | ||
} // namespace Envoy |
This file contains 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,22 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_package", | ||
) | ||
load( | ||
"//test/extensions:extensions_build_system.bzl", | ||
"envoy_extension_cc_test", | ||
) | ||
|
||
envoy_package() | ||
|
||
envoy_extension_cc_test( | ||
name = "config_test", | ||
srcs = ["config_test.cc"], | ||
extension_name = "envoy.retry_host_predicates.other_hosts", | ||
deps = [ | ||
"//source/extensions/retry/host/other_hosts:config", | ||
"//test/mocks/upstream:upstream_mocks", | ||
], | ||
) |
This file contains 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,65 @@ | ||
#include "envoy/registry/registry.h" | ||
#include "envoy/upstream/retry.h" | ||
|
||
#include "extensions/retry/host/other_hosts/config.h" | ||
#include "extensions/retry/host/well_known_names.h" | ||
|
||
#include "test/mocks/upstream/mocks.h" | ||
|
||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace testing; | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Retry { | ||
namespace Host { | ||
|
||
struct TestHostPredicateFactoryCallback : public Upstream::RetryHostPredicateFactoryCallbacks { | ||
|
||
void addHostPredicate(Upstream::RetryHostPredicateSharedPtr host_predicate) { | ||
host_predicate_ = host_predicate; | ||
} | ||
|
||
Upstream::RetryHostPredicateSharedPtr host_predicate_; | ||
}; | ||
|
||
TEST(OtherHostsRetryPredicateConfigTest, PredicateTest) { | ||
auto factory = Registry::FactoryRegistry<Upstream::RetryHostPredicateFactory>::getFactory( | ||
RetryHostPredicateValues::get().PreviousHostsPredicate); | ||
|
||
ASSERT_NE(nullptr, factory); | ||
|
||
TestHostPredicateFactoryCallback callback; | ||
ProtobufWkt::Struct config; | ||
factory->createHostPredicate(callback, config); | ||
|
||
auto predicate = callback.host_predicate_; | ||
|
||
auto host1 = std::make_shared<NiceMock<Upstream::MockHost>>(); | ||
auto host1_address = std::make_shared<Network::Address::Ipv4Instance>("127.0.0.1", 123); | ||
ON_CALL(*host1, address()).WillByDefault(Return(host1_address)); | ||
|
||
auto host2 = std::make_shared<NiceMock<Upstream::MockHost>>(); | ||
auto host2_address = std::make_shared<Network::Address::Ipv4Instance>("127.0.0.1", 456); | ||
ON_CALL(*host2, address()).WillByDefault(Return(host2_address)); | ||
|
||
ASSERT_FALSE(predicate->shouldSelectAnotherHost(*host1)); | ||
ASSERT_FALSE(predicate->shouldSelectAnotherHost(*host2)); | ||
|
||
predicate->onHostAttempted(host1); | ||
|
||
ASSERT_TRUE(predicate->shouldSelectAnotherHost(*host1)); | ||
ASSERT_FALSE(predicate->shouldSelectAnotherHost(*host2)); | ||
|
||
predicate->onHostAttempted(host2); | ||
|
||
ASSERT_TRUE(predicate->shouldSelectAnotherHost(*host1)); | ||
ASSERT_TRUE(predicate->shouldSelectAnotherHost(*host2)); | ||
} | ||
|
||
} // namespace Host | ||
} // namespace Retry | ||
} // namespace Extensions | ||
} // namespace Envoy |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snowp I'm not sure of my comment came through here or not: I suspect that a vector or fixed size array initialized to the number of max retries would end up being faster here and use less memory given the generally tiny number of allowed retries. Something to think about / TODO in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a good idea. I think the only complication is having to plumb through the max retries count, but since other implementations might also find it useful for similar use cases it should probably be part of the factory interface.