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 5 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
37 changes: 1 addition & 36 deletions docs/root/version_history/current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,8 @@ Removed Config or Runtime

New Features
------------
* compression: the :ref:`compressor <envoy_v3_api_msg_extensions.filters.http.compressor.v3.Compressor>` filter adds support for compressing request payloads. Its configuration is unified with the :ref:`decompressor <envoy_v3_api_msg_extensions.filters.http.decompressor.v3.Decompressor>` filter with two new fields for different directions - :ref:`requests <envoy_v3_api_field_extensions.filters.http.compressor.v3.Compressor.request_direction_config>` and :ref:`responses <envoy_v3_api_field_extensions.filters.http.compressor.v3.Compressor.response_direction_config>`. The latter deprecates the old response-specific fields and, if used, roots the response-specific stats in `<stat_prefix>.compressor.<compressor_library.name>.<compressor_library_stat_prefix>.response.*` instead of `<stat_prefix>.compressor.<compressor_library.name>.<compressor_library_stat_prefix>.*`.
* config: added ability to flush stats when the admin's :ref:`/stats endpoint <operations_admin_interface_stats>` is hit instead of on a timer via :ref:`stats_flush_on_admin <envoy_v3_api_field_config.bootstrap.v3.Bootstrap.stats_flush_on_admin>`.
* config: added new runtime feature `envoy.features.enable_all_deprecated_features` that allows the use of all deprecated features.
* formatter: added new :ref:`text_format_source <envoy_v3_api_field_config.core.v3.SubstitutionFormatString.text_format_source>` field to support format strings both inline and from a file.
* grpc: implemented header value syntax support when defining :ref:`initial metadata <envoy_v3_api_field_config.core.v3.GrpcService.initial_metadata>` for gRPC-based `ext_authz` :ref:`HTTP <envoy_v3_api_field_extensions.filters.http.ext_authz.v3.ExtAuthz.grpc_service>` and :ref:`network <envoy_v3_api_field_extensions.filters.network.ext_authz.v3.ExtAuthz.grpc_service>` filters, and :ref:`ratelimit <envoy_v3_api_field_config.ratelimit.v3.RateLimitServiceConfig.grpc_service>` filters.
* grpc-json: added support for configuring :ref:`unescaping behavior <envoy_v3_api_field_extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder.url_unescape_spec>` for path components.
* hds: added support for delta updates in the :ref:`HealthCheckSpecifier <envoy_v3_api_msg_service.health.v3.HealthCheckSpecifier>`, making only the Endpoints and Health Checkers that changed be reconstructed on receiving a new message, rather than the entire HDS.
* health_check: added option to use :ref:`no_traffic_healthy_interval <envoy_v3_api_field_config.core.v3.HealthCheck.no_traffic_healthy_interval>` which allows a different no traffic interval when the host is healthy.
* http: added HCM :ref:`timeout config field <envoy_v3_api_field_extensions.filters.network.http_connection_manager.v3.HttpConnectionManager.request_headers_timeout>` to control how long a downstream has to finish sending headers before the stream is cancelled.
* http: added frame flood and abuse checks to the upstream HTTP/2 codec. This check is off by default and can be enabled by setting the `envoy.reloadable_features.upstream_http2_flood_checks` runtime key to true.
* http: added support for :ref:`:ref:`preconnecting <envoy_v3_api_msg_config.cluster.v3.Cluster.PreconnectPolicy>`. Preconnecting is off by default, but recommended for clusters serving latency-sensitive traffic, especially if using HTTP/1.1.
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth talking about this in the arch overview docs somewhere? Seems pretty important. Feel free to do this in a follow up if you want.

* http: clusters now support selecting HTTP/1 or HTTP/2 based on ALPN, configurable via :ref:`alpn_config <envoy_v3_api_field_extensions.upstreams.http.v3.HttpProtocolOptions.auto_config>` in the :ref:`http_protocol_options <envoy_v3_api_msg_extensions.upstreams.http.v3.HttpProtocolOptions>` message.
* jwt_authn: added support for :ref:`per-route config <envoy_v3_api_msg_extensions.filters.http.jwt_authn.v3.PerRouteConfig>`.
* kill_request: added new :ref:`HTTP kill request filter <config_http_filters_kill_request>`.
* listener: added an optional :ref:`default filter chain <envoy_v3_api_field_config.listener.v3.Listener.default_filter_chain>`. If this field is supplied, and none of the :ref:`filter_chains <envoy_v3_api_field_config.listener.v3.Listener.filter_chains>` matches, this default filter chain is used to serve the connection.
* listener: added back the :ref:`use_original_dst field <envoy_v3_api_field_config.listener.v3.Listener.use_original_dst>`.
* log: added a new custom flag ``%_`` to the log pattern to print the actual message to log, but with escaped newlines.
* lua: added `downstreamDirectRemoteAddress()` and `downstreamLocalAddress()` APIs to :ref:`streamInfo() <config_http_filters_lua_stream_info_wrapper>`.
* mongo_proxy: the list of commands to produce metrics for is now :ref:`configurable <envoy_v3_api_field_extensions.filters.network.mongo_proxy.v3.MongoProxy.commands>`.
* network: added a :ref:`timeout <envoy_v3_api_field_config.listener.v3.FilterChain.transport_socket_connect_timeout>` for incoming connections completing transport-level negotiation, including TLS and ALTS hanshakes.
* overload: add :ref:`envoy.overload_actions.reduce_timeouts <config_overload_manager_overload_actions>` overload action to enable scaling timeouts down with load. Scaling support :ref:`is limited <envoy_v3_api_enum_config.overload.v3.ScaleTimersOverloadActionConfig.TimerType>` to the HTTP connection and stream idle timeouts.
* ratelimit: added support for use of various :ref:`metadata <envoy_v3_api_field_config.route.v3.RateLimit.Action.metadata>` as a ratelimit action.
* ratelimit: added :ref:`disable_x_envoy_ratelimited_header <envoy_v3_api_msg_extensions.filters.http.ratelimit.v3.RateLimit>` option to disable `X-Envoy-RateLimited` header.
* ratelimit: added :ref:`body <envoy_v3_api_field_service.ratelimit.v3.RateLimitResponse.raw_body>` field to support custom response bodies for non-OK responses from the external ratelimit service.
* router: added support for regex rewrites during HTTP redirects using :ref:`regex_rewrite <envoy_v3_api_field_config.route.v3.RedirectAction.regex_rewrite>`.
* sds: improved support for atomic :ref:`key rotations <xds_certificate_rotation>` and added configurable rotation triggers for
:ref:`TlsCertificate <envoy_v3_api_field_extensions.transport_sockets.tls.v3.TlsCertificate.watched_directory>` and
:ref:`CertificateValidationContext <envoy_v3_api_field_extensions.transport_sockets.tls.v3.CertificateValidationContext.watched_directory>`.
* signal: added an extension point for custom actions to run on the thread that has encountered a fatal error. Actions are configurable via :ref:`fatal_actions <envoy_v3_api_field_config.bootstrap.v3.Bootstrap.fatal_actions>`.
* start_tls: :ref:`transport socket<envoy_v3_api_msg_extensions.transport_sockets.starttls.v3.StartTlsConfig>` which starts in clear-text but may programatically be converted to use tls.
* tcp: added a new :ref:`envoy.overload_actions.reject_incoming_connections <config_overload_manager_overload_actions>` action to reject incoming TCP connections.
* thrift_proxy: added a new :ref: `payload_passthrough <envoy_v3_api_field_extensions.filters.network.thrift_proxy.v3.ThriftProxy.payload_passthrough>` option to skip decoding body in the Thrift message.
* tls: added support for RSA certificates with 4096-bit keys in FIPS mode.
* tracing: added SkyWalking tracer.
* tracing: added support for setting the hostname used when sending spans to a Zipkin collector using the :ref:`collector_hostname <envoy_v3_api_field_config.trace.v3.ZipkinConfig.collector_hostname>` field.
* xds: added support for resource TTLs. A TTL is specified on the :ref:`Resource <envoy_api_msg_Resource>`. For SotW, a :ref:`Resource <envoy_api_msg_Resource>` can be embedded
in the list of resources to specify the TTL.
* tcp_proxy: add support for converting raw TCP streams into HTTP/1.1 CONNECT requests. See :ref:`upgrade documentation <tunneling-tcp-over-http>` for details.

Deprecated
----------
31 changes: 26 additions & 5 deletions source/common/conn_pool/conn_pool_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ void ConnPoolImplBase::destructAllConnections() {
dispatcher_.clearDeferredDeleteList();
}

bool ConnPoolImplBase::shouldConnect(size_t pending_streams, size_t active_streams,
uint32_t connecting_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.
//
// Also without this, we would never pre-establish a connection as the first
// connection in a pool because pending/active streams could both be 0.
int anticipated_streams = anticipate_incoming_stream ? 1 : 0;

// The number of streams we want to be provisioned for is the number of
// pending, active, and anticipated 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 (pending_streams + active_streams + anticipated_streams) * preconnect_ratio >
connecting_capacity + active_streams;
}

bool ConnPoolImplBase::shouldCreateNewConnection(float global_preconnect_ratio) const {
// If the host is not healthy, don't make it do extra work, especially as
// upstream selection logic may result in bypassing this upstream entirely.
Expand All @@ -48,9 +70,8 @@ bool ConnPoolImplBase::shouldCreateNewConnection(float global_preconnect_ratio)
// preconnect limit, preconnect.
// We may eventually want to track preconnect_attempts to allow more preconnecting for
// heavily weighted upstreams or sticky picks.
if (global_preconnect_ratio > 1.0 &&
((pending_streams_.size() + 1 + num_active_streams_) * global_preconnect_ratio >
(connecting_stream_capacity_ + num_active_streams_))) {
if (shouldConnect(pending_streams_.size(), num_active_streams_, connecting_stream_capacity_,
global_preconnect_ratio, true)) {
return true;
}

Expand All @@ -61,8 +82,8 @@ bool ConnPoolImplBase::shouldCreateNewConnection(float global_preconnect_ratio)
//
// 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_.size() + num_active_streams_) * perUpstreamPreconnectRatio() >
(connecting_stream_capacity_ + num_active_streams_);
return shouldConnect(pending_streams_.size(), num_active_streams_, connecting_stream_capacity_,
perUpstreamPreconnectRatio());
Copy link
Member

@mattklein123 mattklein123 Jan 12, 2021

Choose a reason for hiding this comment

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

Is the idea here basically to use either the global ratio or the per-upstream ratio to decide on whether to make a pre-connect for this host only? If so would it be more clear to call this once and take the max of the global ratio and the per-upstream ratio? I'm confused why we would pass true for anticipate above but not here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fundamentally there's two types of prefetching, local and global. local is done every time there's a connection change, so will be adequately prefetched. It doesn't need to anticipate as it's called after new streams are assigned. global does need to anticipate, or incoming traffic to upstream A could never prefetch a connection for B (given the streams in B would be zero, it'd zero out the function). So this function will only meaningfully call one of the two blocks. I'll make it more clear with an if-else and add some more comments about the +1.

}

float ConnPoolImplBase::perUpstreamPreconnectRatio() const {
Expand Down
9 changes: 9 additions & 0 deletions source/common/conn_pool/conn_pool_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ class ConnPoolImplBase : protected Logger::Loggable<Logger::Id::pool> {
return *static_cast<T*>(&context);
}

// Determines if prefetching is warranted based on the number of streams in
// use, pending streams, anticipated capacity, and preconnect configuration.
//
// 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,
Copy link
Member

Choose a reason for hiding this comment

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

Pedantically for this to make sense this should be connecting_and_connected_capacity, right? Since it's not just about active streams, it's about all excess capacity and all pending or existing connections? If that is correct maybe update so it's more clear?

bool anticipate_incoming_stream = false);

void addDrainedCallbackImpl(Instance::DrainedCb cb);
void drainConnectionsImpl();

Expand Down
10 changes: 7 additions & 3 deletions source/common/upstream/cluster_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,14 @@ void ClusterManagerImpl::maybePreconnect(
// 3 here is arbitrary. Just as in ConnPoolImplBase::tryCreateNewConnections
// we want to limit the work which can be done on any given preconnect attempt.
for (int i = 0; i < 3; ++i) {
// Just as in ConnPoolImplBase::shouldCreateNewConnection, see if adding this one new connection
// See if adding this one new connection
// would put the cluster over desired capacity. If so, stop preconnecting.
if ((state.connecting_stream_capacity_ + state.active_streams_) >
(state.pending_streams_ + 1 + state.active_streams_) * peekahead_ratio) {
//
// We anticipate the incoming stream here, because maybePreconnect is called
// before a new stream is established.
if (!ConnectionPool::ConnPoolImplBase::shouldConnect(
state.pending_streams_, state.active_streams_, state.connecting_stream_capacity_,
peekahead_ratio, true)) {
return;
}
ConnectionPool::Instance* preconnect_pool = pick_preconnect_pool();
Expand Down