-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
http: prefetch for upstreams #14143
Changes from 5 commits
377a655
a3f873e
03d25da
0de2a56
00d0506
7f58cb6
800fb46
280b655
8bc15af
963803b
a130c58
47b4b7e
f4619e3
84882f2
568ac1c
5f3ef8a
228f441
7f391a9
575636e
ff6aba8
dbb54a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -589,11 +589,10 @@ message Cluster { | |
google.protobuf.Duration max_interval = 2 [(validate.rules).duration = {gt {nanos: 1000000}}]; | ||
} | ||
|
||
// [#not-implemented-hide:] | ||
message PrefetchPolicy { | ||
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. And then one question for @mattklein123 , I assuming we do end up with at minimum lmk if you think it's worth having wrapper messages for per-upstream and per-cluster. |
||
// Indicates how many streams (rounded up) can be anticipated per-upstream for each | ||
// incoming stream. This is useful for high-QPS or latency-sensitive services. Prefetching | ||
// will only be done if the upstream is healthy. | ||
// will only be done if the upstream is healthy and the cluster has traffic. | ||
// | ||
// For example if this is 2, for an incoming HTTP/1.1 stream, 2 connections will be | ||
// established, one for the new incoming stream, and one for a presumed follow-up stream. For | ||
|
@@ -622,24 +621,24 @@ message Cluster { | |
// Indicates how many many streams (rounded up) can be anticipated across a cluster for each | ||
// stream, useful for low QPS services. This is currently supported for a subset of | ||
// deterministic non-hash-based load-balancing algorithms (weighted round robin, random). | ||
// Unlike per_upstream_prefetch_ratio this prefetches across the upstream instances in a | ||
// Unlike *per_upstream_prefetch_ratio* this prefetches across the upstream instances in a | ||
// cluster, doing best effort predictions of what upstream would be picked next and | ||
// pre-establishing a connection. | ||
// | ||
// Prefetching will be limited to one prefetch per configured upstream in the cluster and will | ||
// only be done if there are healthy upstreams and the cluster has traffic. | ||
// | ||
// For example if prefetching is set to 2 for a round robin HTTP/2 cluster, on the first | ||
// incoming stream, 2 connections will be prefetched - one to the first upstream for this | ||
// cluster, one to the second on the assumption there will be a follow-up stream. | ||
// | ||
// Prefetching will be limited to one prefetch per configured upstream in the cluster. | ||
// | ||
// If this value is not set, or set explicitly to one, Envoy will fetch as many connections | ||
// as needed to serve streams in flight, so during warm up and in steady state if a connection | ||
// is closed (and per_upstream_prefetch_ratio is not set), there will be a latency hit for | ||
// connection establishment. | ||
// | ||
// If both this and prefetch_ratio are set, Envoy will make sure both predicted needs are met, | ||
// basically prefetching max(predictive-prefetch, per-upstream-prefetch), for each upstream. | ||
// TODO(alyssawilk) per LB docs and LB overview docs when unhiding. | ||
google.protobuf.DoubleValue predictive_prefetch_ratio = 2 | ||
[(validate.rules).double = {lte: 3.0 gte: 1.0}]; | ||
} | ||
|
@@ -998,7 +997,6 @@ message Cluster { | |
// Configuration to track optional cluster stats. | ||
TrackClusterStats track_cluster_stats = 49; | ||
|
||
// [#not-implemented-hide:] | ||
// Prefetch configuration for this cluster. | ||
PrefetchPolicy prefetch_policy = 50; | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -855,25 +855,33 @@ ThreadLocalCluster* ClusterManagerImpl::get(absl::string_view cluster) { | |
|
||
void ClusterManagerImpl::maybePrefetch( | ||
ThreadLocalClusterManagerImpl::ClusterEntryPtr& cluster_entry, | ||
const ClusterConnectivityState& state, | ||
std::function<ConnectionPool::Instance*()> pick_prefetch_pool) { | ||
// TODO(alyssawilk) As currently implemented, this will always just prefetch | ||
// one connection ahead of actually needed connections. | ||
// | ||
// Instead we want to track the following metrics across the entire connection | ||
// pool and use the same algorithm we do for per-upstream prefetch: | ||
// ((pending_streams_ + num_active_streams_) * global_prefetch_ratio > | ||
// (connecting_stream_capacity_ + num_active_streams_))) | ||
// and allow multiple prefetches per pick. | ||
// Also cap prefetches such that | ||
// num_unused_prefetch < num hosts | ||
// since if we have more prefetches than hosts, we should consider kicking into | ||
// per-upstream prefetch. | ||
// | ||
// Once we do this, this should loop capped number of times while shouldPrefetch is true. | ||
if (cluster_entry->cluster_info_->peekaheadRatio() > 1.0) { | ||
auto peekahead_ratio = cluster_entry->cluster_info_->peekaheadRatio(); | ||
if (peekahead_ratio <= 1.0) { | ||
return; | ||
} | ||
|
||
// 3 here is arbitrary. Just as in ConnPoolImplBase::tryCreateNewConnections | ||
// we want to limit the work which can be done on any given prefetch attempt. | ||
for (int i = 0; i < 3; ++i) { | ||
// Just as in ConnPoolImplBase::shouldCreateNewConnection, see if adding this one new connection | ||
// would put the cluster over desired capacity. If so, stop prefetching. | ||
if ((state.pending_streams_ + 1 + state.active_streams_) * peekahead_ratio <= | ||
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. Why the +1 in the expression above? I see a similar +1 in ConnPoolImplBase::shouldCreateNewConnection when doing global prefetch but not when doing per-upstream prefetch It seems that this is mimcing the global prefetch logic in shouldCreateNewConnection Worth a comment that references shouldCreateNewConnection? 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. Added a comment - lmk if that doesn't clarify. 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. FWIW I've read this a few times and I'm still struggling a bit on what we are comparing. When I see <= my eye wants to not return but we do return. Perhaps invert? Up to you. |
||
(state.connecting_stream_capacity_ + state.active_streams_)) { | ||
return; | ||
} | ||
ConnectionPool::Instance* prefetch_pool = pick_prefetch_pool(); | ||
if (prefetch_pool) { | ||
prefetch_pool->maybePrefetch(cluster_entry->cluster_info_->peekaheadRatio()); | ||
if (!prefetch_pool->maybePrefetch(peekahead_ratio)) { | ||
// Given that the next prefetch pick may be entirely different, we could | ||
// opt to try again even if the first prefetch fails. Err on the side of | ||
// caution and wait for the next attempt. | ||
return; | ||
} | ||
} else { | ||
// If unable to find a prefetch pool, exit early. | ||
return; | ||
} | ||
} | ||
} | ||
|
@@ -898,9 +906,10 @@ ClusterManagerImpl::httpConnPoolForCluster(const std::string& cluster, ResourceP | |
// performed here in anticipation of the new stream. | ||
// TODO(alyssawilk) refactor to have one function call and return a pair, so this invariant is | ||
// code-enforced. | ||
maybePrefetch(entry->second, [&entry, &priority, &protocol, &context]() { | ||
return entry->second->connPool(priority, protocol, context, true); | ||
}); | ||
maybePrefetch(entry->second, cluster_manager.cluster_manager_state_, | ||
[&entry, &priority, &protocol, &context]() { | ||
return entry->second->connPool(priority, protocol, context, true); | ||
}); | ||
|
||
return ret; | ||
} | ||
|
@@ -924,9 +933,10 @@ ClusterManagerImpl::tcpConnPoolForCluster(const std::string& cluster, ResourcePr | |
// TODO(alyssawilk) refactor to have one function call and return a pair, so this invariant is | ||
// code-enforced. | ||
// Now see if another host should be prefetched. | ||
maybePrefetch(entry->second, [&entry, &priority, &context]() { | ||
return entry->second->tcpConnPool(priority, context, true); | ||
}); | ||
maybePrefetch(entry->second, cluster_manager.cluster_manager_state_, | ||
[&entry, &priority, &context]() { | ||
return entry->second->tcpConnPool(priority, context, true); | ||
}); | ||
|
||
return ret; | ||
} | ||
|
@@ -1405,8 +1415,10 @@ ClusterManagerImpl::ThreadLocalClusterManagerImpl::ClusterEntry::connPool( | |
LoadBalancerContext* context, bool peek) { | ||
HostConstSharedPtr host = (peek ? lb_->peekAnotherHost(context) : lb_->chooseHost(context)); | ||
if (!host) { | ||
ENVOY_LOG(debug, "no healthy host for HTTP connection pool"); | ||
cluster_info_->stats().upstream_cx_none_healthy_.inc(); | ||
if (!peek) { | ||
ENVOY_LOG(debug, "no healthy host for HTTP connection pool"); | ||
cluster_info_->stats().upstream_cx_none_healthy_.inc(); | ||
} | ||
return nullptr; | ||
} | ||
|
||
|
@@ -1466,8 +1478,10 @@ ClusterManagerImpl::ThreadLocalClusterManagerImpl::ClusterEntry::tcpConnPool( | |
ResourcePriority priority, LoadBalancerContext* context, bool peek) { | ||
HostConstSharedPtr host = (peek ? lb_->peekAnotherHost(context) : lb_->chooseHost(context)); | ||
if (!host) { | ||
ENVOY_LOG(debug, "no healthy host for TCP connection pool"); | ||
cluster_info_->stats().upstream_cx_none_healthy_.inc(); | ||
if (!peek) { | ||
ENVOY_LOG(debug, "no healthy host for TCP connection pool"); | ||
cluster_info_->stats().upstream_cx_none_healthy_.inc(); | ||
} | ||
return nullptr; | ||
} | ||
|
||
|
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.
So I know for mobile we're going to want to have fixed prefetch numbers (prefetch 6/8 connections), so I was thinking to make it a oneof (ratio, fixed_prefetch)
But I think the fixed_prefetch will end up being the moral equivalent of min_connections_picked, at which point we should leave this as-is and allow extending it. SG @antoniovicente