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

http: prefetch for upstreams #14143

Merged
merged 21 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 21 additions & 20 deletions source/common/conn_pool/conn_pool_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void ConnPoolImplBase::destructAllConnections() {
}

bool ConnPoolImplBase::shouldConnect(size_t pending_streams, size_t active_streams,
uint32_t connecting_capacity, float preconnect_ratio,
bool anticipate_incoming_stream) {
uint32_t connecting_and_connected_capacity,
float preconnect_ratio, bool anticipate_incoming_stream) {
// This is set to true any time global preconnect is being calculated.
// ClusterManagerImpl::maybePreconnect is called directly before a stream is created, so the
// stream must be anticipated.
Expand All @@ -54,7 +54,7 @@ bool ConnPoolImplBase::shouldConnect(size_t pending_streams, size_t active_strea
// If preconnect ratio is not set, it defaults to 1, and this simplifies to the
// legacy value of pending_streams_.size() > connecting_stream_capacity_
return (pending_streams + active_streams + anticipated_streams) * preconnect_ratio >
connecting_capacity + active_streams;
connecting_and_connected_capacity + active_streams;
}

bool ConnPoolImplBase::shouldCreateNewConnection(float global_preconnect_ratio) const {
Expand All @@ -66,24 +66,25 @@ bool ConnPoolImplBase::shouldCreateNewConnection(float global_preconnect_ratio)
return pending_streams_.size() > connecting_stream_capacity_;
}

// If global preconnecting is on, and this connection is within the global
// preconnect limit, preconnect.
// We may eventually want to track preconnect_attempts to allow more preconnecting for
// heavily weighted upstreams or sticky picks.
if (shouldConnect(pending_streams_.size(), num_active_streams_, connecting_stream_capacity_,
global_preconnect_ratio, true)) {
return true;
// Determine if we are trying to prefetch for global preconnect or local preconnect.
if (global_preconnect_ratio != 0) {
// If global preconnecting is on, and this connection is within the global
// preconnect limit, preconnect.
// For global preconnect, we anticipate an incoming stream to this pool, since it is
// prefetching for the next upcoming stream, which will likely be assigned to this pool.
// We may eventually want to track preconnect_attempts to allow more preconnecting for
// heavily weighted upstreams or sticky picks.
return shouldConnect(pending_streams_.size(), num_active_streams_, connecting_stream_capacity_,
Copy link
Member

Choose a reason for hiding this comment

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

In a future change you might consider renaming connecting_stream_capacity_ also to make it match connecting and connected but no need to block this change on that.

global_preconnect_ratio, true);
} else {
// Ensure this local pool has adequate connections for the given load.
//
// Local preconnect does not need to anticipate a stream. It is called as
// new streams are established or torn down and simply attempts to maintain
// the correct ratio of streams and anticipated capacity.
return shouldConnect(pending_streams_.size(), num_active_streams_, connecting_stream_capacity_,
perUpstreamPreconnectRatio());
}

// The number of streams we want to be provisioned for is the number of
// pending and active streams times the preconnect ratio.
// The number of streams we are (theoretically) provisioned for is the
// connecting stream capacity plus the number of active streams.
//
// If preconnect ratio is not set, it defaults to 1, and this simplifies to the
// legacy value of pending_streams_.size() > connecting_stream_capacity_
return shouldConnect(pending_streams_.size(), num_active_streams_, connecting_stream_capacity_,
perUpstreamPreconnectRatio());
}

float ConnPoolImplBase::perUpstreamPreconnectRatio() const {
Expand Down
2 changes: 1 addition & 1 deletion source/common/conn_pool/conn_pool_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ConnPoolImplBase : protected Logger::Loggable<Logger::Id::pool> {
// If anticipate_incoming_stream is true this assumes a call to newStream is
// pending, which is true for global preconnect.
static bool shouldConnect(size_t pending_streams, size_t active_streams,
uint32_t connecting_capacity, float preconnect_ratio,
uint32_t connecting_and_connected_capacity, float preconnect_ratio,
bool anticipate_incoming_stream = false);

void addDrainedCallbackImpl(Instance::DrainedCb cb);
Expand Down