Skip to content
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

Ensure the pending requests are popped before the callback is called. #7843

Merged
merged 2 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions source/extensions/filters/network/common/redis/client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,20 @@ void ClientImpl::onEvent(Network::ConnectionEvent event) {
void ClientImpl::onRespValue(RespValuePtr&& value) {
ASSERT(!pending_requests_.empty());
PendingRequest& request = pending_requests_.front();
const bool canceled = request.canceled_;
PoolCallbacks& callbacks = request.callbacks_;

if (request.canceled_) {
// We need to ensure the request is popped before calling the callback, since the callback might
// result in closing the connection.
pending_requests_.pop_front();
if (canceled) {
host_->cluster().stats().upstream_rq_cancelled_.inc();
} else if (config_.enableRedirection() && (value->type() == Common::Redis::RespType::Error)) {
std::vector<absl::string_view> err = StringUtil::splitToken(value->asString(), " ", false);
bool redirected = false;
if (err.size() == 3) {
if (err[0] == RedirectionResponse::get().MOVED || err[0] == RedirectionResponse::get().ASK) {
redirected = request.callbacks_.onRedirection(*value);
redirected = callbacks.onRedirection(*value);
if (redirected) {
host_->cluster().stats().upstream_internal_redirect_succeeded_total_.inc();
} else {
Expand All @@ -177,14 +182,12 @@ void ClientImpl::onRespValue(RespValuePtr&& value) {
}
}
if (!redirected) {
request.callbacks_.onResponse(std::move(value));
callbacks.onResponse(std::move(value));
}
} else {
request.callbacks_.onResponse(std::move(value));
callbacks.onResponse(std::move(value));
}

pending_requests_.pop_front();

// If there are no remaining ops in the pipeline we need to disable the timer.
// Otherwise we boost the timer since we are receiving responses and there are more to flush
// out.
Expand Down
58 changes: 58 additions & 0 deletions test/extensions/filters/network/common/redis/client_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,64 @@ TEST_F(RedisClientImplTest, MovedRedirectionNotEnabled) {
client_->close();
}

TEST_F(RedisClientImplTest, RemoveFailedHealthCheck) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you add a small // comment on top of each new test that briefly describes what the test does since it's non-obvious?

// This test simulates a health check response signaling traffic should be drained from the host.
// As a result, the health checker will close the client in the call back.
InSequence s;

setup();

Common::Redis::RespValue request1;
MockPoolCallbacks callbacks1;
EXPECT_CALL(*encoder_, encode(Ref(request1), _));
EXPECT_CALL(*flush_timer_, enabled()).WillOnce(Return(false));
PoolRequest* handle1 = client_->makeRequest(request1, callbacks1);
EXPECT_NE(nullptr, handle1);

onConnected();

Common::Redis::RespValuePtr response1(new Common::Redis::RespValue());
// Each call should result in either onResponse or onFailure, never both.
EXPECT_CALL(callbacks1, onFailure()).Times(0);
EXPECT_CALL(callbacks1, onResponse_(Ref(response1)))
.WillOnce(Invoke([&](Common::Redis::RespValuePtr&) {
// The health checker might fail the active health check based on the response content, and
// result in removing the host and closing the client.
client_->close();
}));
EXPECT_CALL(*connect_or_op_timer_, disableTimer()).Times(2);
EXPECT_CALL(host_->outlier_detector_,
putResult(Upstream::Outlier::Result::EXT_ORIGIN_REQUEST_SUCCESS, _));
callbacks_->onRespValue(std::move(response1));
}

TEST_F(RedisClientImplTest, RemoveFailedHost) {
// This test simulates a health check request failed due to remote host closing the connection.
// As a result the health checker will close the client in the call back.
InSequence s;

setup();

NiceMock<Network::MockConnectionCallbacks> connection_callbacks;
client_->addConnectionCallbacks(connection_callbacks);

Common::Redis::RespValue request1;
MockPoolCallbacks callbacks1;
EXPECT_CALL(*encoder_, encode(Ref(request1), _));
EXPECT_CALL(*flush_timer_, enabled()).WillOnce(Return(false));
PoolRequest* handle1 = client_->makeRequest(request1, callbacks1);
EXPECT_NE(nullptr, handle1);

onConnected();

EXPECT_CALL(host_->outlier_detector_,
putResult(Upstream::Outlier::Result::LOCAL_ORIGIN_CONNECT_FAILED, _));
EXPECT_CALL(callbacks1, onFailure()).WillOnce(Invoke([&]() { client_->close(); }));
EXPECT_CALL(*connect_or_op_timer_, disableTimer());
EXPECT_CALL(connection_callbacks, onEvent(Network::ConnectionEvent::RemoteClose));
upstream_connection_->raiseEvent(Network::ConnectionEvent::RemoteClose);
}

TEST(RedisClientFactoryImplTest, Basic) {
ClientFactoryImpl factory;
Upstream::MockHost::MockCreateConnectionData conn_info;
Expand Down