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

listener manager: implement dynamic add/remove #1215

Merged
merged 3 commits into from
Jul 6, 2017
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
6 changes: 6 additions & 0 deletions docs/configuration/listeners/listeners.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Each individual listener configuration has the following format:
.. code-block:: json

{
"name": "...",
"address": "...",
"filters": [],
"ssl_context": "{...}",
Expand All @@ -18,6 +19,11 @@ Each individual listener configuration has the following format:
"per_connection_buffer_limit_bytes": "..."
}

name
*(optional, string)* The unique name by which this listener is known. If no name is provided,
Envoy will allocate an internal UUID for the listener. If the listener is to be dynamically
updated or removed via LDS a unique name must be provided.

address
*(required, string)* The address that the listener should listen on. Currently only TCP
listeners are supported, e.g., "tcp://127.0.0.1:80". Note, "tcp://0.0.0.0:80" is the wild card
Expand Down
1 change: 1 addition & 0 deletions include/envoy/network/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class Instance {
virtual ~Instance() {}

virtual bool operator==(const Instance& rhs) const PURE;
bool operator!=(const Instance& rhs) const { return !operator==(rhs); }

/**
* @return a human readable string for the address.
Expand Down
1 change: 1 addition & 0 deletions include/envoy/network/listen_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ListenSocket {
};

typedef std::unique_ptr<ListenSocket> ListenSocketPtr;
typedef std::shared_ptr<ListenSocket> ListenSocketSharedPtr;

} // namespace Network
} // namespace Envoy
44 changes: 27 additions & 17 deletions include/envoy/server/listener_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class ListenerComponentFactory {
* Creates a socket.
* @param address supplies the socket's address.
* @param bind_to_port supplies whether to actually bind the socket.
* @return Network::ListenSocketPtr an initialized and potentially bound socket.
* @return Network::ListenSocketSharedPtr an initialized and potentially bound socket.
*/
virtual Network::ListenSocketPtr
virtual Network::ListenSocketSharedPtr
createListenSocket(Network::Address::InstanceConstSharedPtr address, bool bind_to_port) PURE;

/**
Expand Down Expand Up @@ -55,15 +55,9 @@ class Listener {
*/
virtual Network::FilterChainFactory& filterChainFactory() PURE;

/**
* @return Network::Address::InstanceConstSharedPtr the configured address for the listener. This
* may be distinct to the bound address, e.g. if the port is zero.
*/
virtual Network::Address::InstanceConstSharedPtr address() PURE;

/**
* @return Network::ListenSocket& the actual listen socket. The address of this socket may be
* different from address() if for example the configured address binds to port zero.
* different from configured if for example the configured address binds to port zero.
*/
virtual Network::ListenSocket& socket() PURE;

Expand Down Expand Up @@ -108,8 +102,6 @@ class Listener {
virtual uint64_t listenerTag() PURE;
};

typedef std::unique_ptr<Listener> ListenerPtr;

/**
* A manager for all listeners and all threaded connection handling workers.
*/
Expand All @@ -118,22 +110,40 @@ class ListenerManager {
virtual ~ListenerManager() {}

/**
* Add a listener to the manager.
* @param json supplies the configuration JSON. Will throw an EnvoyException if the listener can
* not be added.
* Add or update a listener. Listeners are referenced by a unique name. If no name is provided,
* the manager will allocate a UUID. Listeners that expect to be dynamically updated should
* provide a unique name. The manager will search by name to find the existing listener that
* should be updated. The new listener must have the same configured address. The old listener
* will be gracefully drained once the new listener is ready to take traffic (e.g. when RDS has
* been initialized).
* @param json supplies the configuration JSON.
* @return TRUE if a listener was added or FALSE if the listener was not updated because it is
* a duplicate of the existing listener. This routine will throw an EnvoyException if
* there is a fundamental error preventing the listener from being added or updated.
*/
virtual void addListener(const Json::Object& json) PURE;
virtual bool addOrUpdateListener(const Json::Object& json) PURE;

/**
* @return std::list<std::reference_wrapper<Listener>> a list of the currently loaded listeners.
* @return std::vector<std::reference_wrapper<Listener>> a list of the currently loaded listeners.
* Note that this routine returns references to the existing listeners. The references are only
* valid in the context of the current call stack and should not be stored.
*/
virtual std::list<std::reference_wrapper<Listener>> listeners() PURE;
virtual std::vector<std::reference_wrapper<Listener>> listeners() PURE;

/**
* @return uint64_t the total number of connections owned by all listeners across all workers.
*/
virtual uint64_t numConnections() PURE;

/**
* Remove a listener by name.
* @param name supplies the listener name to remove.
* @return TRUE if the listener was found and removed. Note that when this routine returns TRUE,
* the listener has not necessarily been actually deleted right away. The listener will be
* drained and fully removed at some later time.
*/
virtual bool removeListener(const std::string& name) PURE;

/**
* Start all workers accepting new connections on all added listeners.
* @param guard_dog supplies the guard dog to use for thread watching.
Expand Down
1 change: 1 addition & 0 deletions source/common/json/config_schemas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const std::string Json::Schema::LISTENER_SCHEMA(R"EOF(
},
"type" : "object",
"properties": {
"name": {"type": "string"},
Copy link
Member

Choose a reason for hiding this comment

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

doc update for this param?

Copy link
Member Author

Choose a reason for hiding this comment

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

oops, thanks. Good catch. Will add.

Copy link
Member

Choose a reason for hiding this comment

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

Do you also want to update this in envoy-api for v2? Seems like we'll need it there as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes will do

"address": {"type": "string"},
"filters" : {
"type" : "array",
Expand Down
2 changes: 1 addition & 1 deletion source/exe/hot_restart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void HotRestartImpl::drainParentListeners() {
}

int HotRestartImpl::duplicateParentListenSocket(const std::string& address) {
if (options_.restartEpoch() == 0) {
if (options_.restartEpoch() == 0 || parent_terminated_) {
return -1;
}

Expand Down
7 changes: 3 additions & 4 deletions source/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ envoy_cc_library(
"//source/common/common:logger_lib",
"//source/common/common:utility_lib",
"//source/common/json:config_schemas_lib",
"//source/common/json:json_loader_lib",
"//source/common/network:utility_lib",
"//source/common/ratelimit:ratelimit_lib",
"//source/common/tracing:http_tracer_lib",
"//source/common/tracing/zipkin:zipkin_lib",
"//source/common/upstream:cluster_manager_lib",
],
)

Expand Down Expand Up @@ -125,6 +122,7 @@ envoy_cc_library(
hdrs = ["listener_manager_impl.h"],
deps = [
":configuration_lib",
":init_manager_lib",
"//include/envoy/registry",
"//include/envoy/server:filter_config_interface",
"//include/envoy/server:listener_manager_interface",
Expand Down Expand Up @@ -159,7 +157,6 @@ envoy_cc_library(
"//include/envoy/server:instance_interface",
"//include/envoy/server:listener_manager_interface",
"//include/envoy/server:options_interface",
"//include/envoy/ssl:context_manager_interface",
"//include/envoy/stats:stats_macros",
"//include/envoy/tracing:http_tracer_interface",
"//include/envoy/upstream:cluster_manager_interface",
Expand All @@ -171,6 +168,7 @@ envoy_cc_library(
"//source/common/runtime:runtime_lib",
"//source/common/stats:statsd_lib",
"//source/common/thread_local:thread_local_lib",
"//source/common/upstream:cluster_manager_lib",
"//source/server/http:admin_lib",
],
)
Expand Down Expand Up @@ -198,6 +196,7 @@ envoy_cc_library(
hdrs = ["worker_impl.h"],
deps = [
":connection_handler_lib",
":test_hooks_lib",
"//include/envoy/api:api_interface",
"//include/envoy/event:dispatcher_interface",
"//include/envoy/event:timer_interface",
Expand Down
4 changes: 2 additions & 2 deletions source/server/config_validation/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class ValidationInstance : Logger::Loggable<Logger::Id::main>,
Configuration::FactoryContext& context) override {
return ProdListenerComponentFactory::createFilterFactoryList_(filters, *this, context);
}
Network::ListenSocketPtr createListenSocket(Network::Address::InstanceConstSharedPtr,
bool) override {
Network::ListenSocketSharedPtr createListenSocket(Network::Address::InstanceConstSharedPtr,
bool) override {
// Returned sockets are not currently used so we can return nothing here safely vs. a
// validation mock.
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions source/server/configuration_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "common/common/utility.h"
#include "common/json/config_schemas.h"
#include "common/ratelimit/ratelimit_impl.h"
#include "common/upstream/cluster_manager_impl.h"
#include "common/tracing/http_tracer_impl.h"

#include "spdlog/spdlog.h"

Expand All @@ -43,7 +43,7 @@ void MainImpl::initialize(const Json::Object& json, Instance& server,
ENVOY_LOG(info, "loading {} listener(s)", listeners.size());
for (size_t i = 0; i < listeners.size(); i++) {
ENVOY_LOG(info, "listener #{}:", i);
server.listenerManager().addListener(*listeners[i]);
server.listenerManager().addOrUpdateListener(*listeners[i]);
}

if (json.hasObject("statsd_local_udp_port") && json.hasObject("statsd_udp_ip_address")) {
Expand Down
Loading