-
Notifications
You must be signed in to change notification settings - Fork 5.5k
healthcheck filter: compute response based on upstream cluster health #2387
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 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b7eb01
healthcheck filter: compute response based on upstream cluster health
76da1ac
Code cleanups and a new test case for empty cluster with min health = 0%
ed13504
Update release notes
0985b13
Fix thread-safety and clean up based on code review feedback
0235233
Small cleanup: remove unneeded initializer
d665fd8
Add "Const" to shared pointer typedef name and fix comment typos
ca89315
Merge master
8a06a78
Remove redundant ".Times(1)" from unit tests
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,12 @@ | |
| #include <memory> | ||
|
|
||
| #include "common/buffer/buffer_impl.h" | ||
| #include "common/upstream/upstream_impl.h" | ||
|
|
||
| #include "server/http/health_check.h" | ||
|
|
||
| #include "test/mocks/server/mocks.h" | ||
| #include "test/mocks/upstream/cluster_info.h" | ||
| #include "test/test_common/printers.h" | ||
| #include "test/test_common/utility.h" | ||
|
|
||
|
|
@@ -36,8 +38,11 @@ class HealthCheckFilterTest : public testing::Test { | |
| prepareFilter(pass_through); | ||
| } | ||
|
|
||
| void prepareFilter(bool pass_through) { | ||
| filter_.reset(new HealthCheckFilter(context_, pass_through, cache_manager_, "/healthcheck")); | ||
| void prepareFilter( | ||
| bool pass_through, | ||
| ClusterMinHealthyPercentagesConstSharedPtr cluster_min_healthy_percentages = nullptr) { | ||
| filter_.reset(new HealthCheckFilter(context_, pass_through, cache_manager_, "/healthcheck", | ||
| cluster_min_healthy_percentages)); | ||
| filter_->setDecoderFilterCallbacks(callbacks_); | ||
| } | ||
|
|
||
|
|
@@ -49,6 +54,14 @@ class HealthCheckFilterTest : public testing::Test { | |
| NiceMock<Http::MockStreamDecoderFilterCallbacks> callbacks_; | ||
| Http::TestHeaderMapImpl request_headers_; | ||
| Http::TestHeaderMapImpl request_headers_no_hc_; | ||
|
|
||
| class MockHealthCheckCluster : public NiceMock<Upstream::MockThreadLocalCluster> { | ||
| public: | ||
| MockHealthCheckCluster(uint64_t membership_total, uint64_t membership_healthy) { | ||
| info()->stats().membership_total_.set(membership_total); | ||
| info()->stats().membership_healthy_.set(membership_healthy); | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| class HealthCheckFilterNoPassThroughTest : public HealthCheckFilterTest { | ||
|
|
@@ -84,6 +97,91 @@ TEST_F(HealthCheckFilterNoPassThroughTest, NotHcRequest) { | |
| EXPECT_STREQ("true", service_response.EnvoyImmediateHealthCheckFail()->value().c_str()); | ||
| } | ||
|
|
||
| TEST_F(HealthCheckFilterNoPassThroughTest, ComputedHealth) { | ||
| // Test non-pass-through health checks without upstream cluster minimum health specified. | ||
| prepareFilter(false); | ||
| { | ||
| Http::TestHeaderMapImpl health_check_response{{":status", "200"}}; | ||
| EXPECT_CALL(context_, healthCheckFailed()).WillOnce(Return(false)); | ||
| EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&health_check_response), true)) | ||
|
Member
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. nit: |
||
| .Times(1); | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->decodeHeaders(request_headers_, true)); | ||
| } | ||
| { | ||
| Http::TestHeaderMapImpl health_check_response{{":status", "503"}}; | ||
| EXPECT_CALL(context_, healthCheckFailed()).WillOnce(Return(true)); | ||
| EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&health_check_response), true)) | ||
| .Times(1); | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->decodeHeaders(request_headers_, true)); | ||
| } | ||
|
|
||
| // Test non-pass-through health checks with upstream cluster minimum health specified. | ||
| prepareFilter(false, ClusterMinHealthyPercentagesConstSharedPtr( | ||
| new ClusterMinHealthyPercentages{{"www1", 50.0}, {"www2", 75.0}})); | ||
| { | ||
| // This should pass, because each upstream cluster has at least the | ||
| // minimum percentage of healthy servers. | ||
| Http::TestHeaderMapImpl health_check_response{{":status", "200"}}; | ||
| MockHealthCheckCluster cluster_www1(100, 50); | ||
| MockHealthCheckCluster cluster_www2(1000, 800); | ||
| EXPECT_CALL(context_, healthCheckFailed()).WillOnce(Return(false)); | ||
| EXPECT_CALL(context_, clusterManager()).Times(1); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www1")).WillRepeatedly(Return(&cluster_www1)); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www2")).WillRepeatedly(Return(&cluster_www2)); | ||
| EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&health_check_response), true)) | ||
| .Times(1); | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->decodeHeaders(request_headers_, true)); | ||
| } | ||
| { | ||
| // This should fail, because one upstream cluster has too few healthy servers. | ||
| Http::TestHeaderMapImpl health_check_response{{":status", "503"}}; | ||
| MockHealthCheckCluster cluster_www1(100, 49); | ||
| MockHealthCheckCluster cluster_www2(1000, 800); | ||
| EXPECT_CALL(context_, healthCheckFailed()).WillOnce(Return(false)); | ||
| EXPECT_CALL(context_, clusterManager()).Times(1); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www1")).WillRepeatedly(Return(&cluster_www1)); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www2")).WillRepeatedly(Return(&cluster_www2)); | ||
| EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&health_check_response), true)) | ||
| .Times(1); | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->decodeHeaders(request_headers_, true)); | ||
| } | ||
| { | ||
| // This should fail, because one upstream cluster has no servers at all. | ||
|
Member
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. Consider adding a test for the empty cluster but min health == 0% case. |
||
| Http::TestHeaderMapImpl health_check_response{{":status", "503"}}; | ||
| MockHealthCheckCluster cluster_www1(0, 0); | ||
| MockHealthCheckCluster cluster_www2(1000, 800); | ||
| EXPECT_CALL(context_, healthCheckFailed()).WillOnce(Return(false)); | ||
| EXPECT_CALL(context_, clusterManager()).Times(1); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www1")).WillRepeatedly(Return(&cluster_www1)); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www2")).WillRepeatedly(Return(&cluster_www2)); | ||
| EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&health_check_response), true)) | ||
| .Times(1); | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->decodeHeaders(request_headers_, true)); | ||
| } | ||
| // Test the cases where an upstream cluster is empty, or has no healthy servers, but | ||
| // the minimum required percent healthy is zero. The health check should return a 200. | ||
| prepareFilter(false, ClusterMinHealthyPercentagesConstSharedPtr( | ||
| new ClusterMinHealthyPercentages{{"www1", 0.0}, {"www2", 0.0}})); | ||
| { | ||
| Http::TestHeaderMapImpl health_check_response{{":status", "200"}}; | ||
| MockHealthCheckCluster cluster_www1(0, 0); | ||
| MockHealthCheckCluster cluster_www2(1000, 0); | ||
| EXPECT_CALL(context_, healthCheckFailed()).WillOnce(Return(false)); | ||
| EXPECT_CALL(context_, clusterManager()).Times(1); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www1")).WillRepeatedly(Return(&cluster_www1)); | ||
| EXPECT_CALL(context_.cluster_manager_, get("www2")).WillRepeatedly(Return(&cluster_www2)); | ||
| EXPECT_CALL(callbacks_, encodeHeaders_(HeaderMapEqualRef(&health_check_response), true)) | ||
| .Times(1); | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->decodeHeaders(request_headers_, true)); | ||
| } | ||
| } | ||
|
|
||
| TEST_F(HealthCheckFilterNoPassThroughTest, HealthCheckFailedCallbackCalled) { | ||
| EXPECT_CALL(context_, healthCheckFailed()).WillOnce(Return(true)); | ||
| EXPECT_CALL(callbacks_.request_info_, healthCheck(true)); | ||
|
|
||
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.
Can you add some comments here? I guess I see how the lack of the cluster at all is sufficient ground for failure, though I wonder if there should be a stat. At the very least I would probably add a small comment.