Repository navigation
Provide a listener to hot restart for udp packet forwarding - #29137
Conversation
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
|
/retest to check arm failure |
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
| } | ||
| const std::string addr = fmt::format("unix://{}", address->asString()); | ||
| const int fd = server_.hotRestart().duplicateParentListenSocket(addr, worker_index); | ||
| const int fd = server_.hotRestart().duplicateParentListenSocket(addr, worker_index, listener); |
There was a problem hiding this comment.
This listener is the ListenerConfig object not the active TCP/QUIC listener. I'm wondering which field of this object do you want to build the hash map upon, i.e. name or tag? Could you pass in that instead?
And please keep in mind that the address configured can be Any. When you do the mapping, this need special handling.
There was a problem hiding this comment.
Oh, this is the "no dynamic cast" problem again and I think I misread the chain of inheritance because of two completely different types of things being called listeners, and the class hierarchy in this area being a real spaghetti. :(
I definitely can't pass the keys in instead because the values of the map have to be the objects on which onData can be called. So I need, I guess, the UdpListenerCallbacks, as the base-level type that will do what's needed. (The key would be a tuple of the addr and worker index, which are already the arguments to duplicateParentListenSocket.)
Good point about the Any address, I'll add that to the notes.
There was a problem hiding this comment.
Turns out no I didn't misread the chain of things, I was thinking of dynamic_casting to UdpListenerConfig, which has listenerWorkerRouter() on it, which would allow us to deliver packets to the appropriate worker via deliver(). So my mistake was that dynamic_casting isn't an option so I'd need to instead make it an OptRef<UdpListenerConfig> that gets passed through all these layers.
Alternative option is to capture either a UdpListenerConfig or an ActiveUdpListener somewhere else in the process and pass it in via a different function, which makes less of a mess (see #29249 for example); but then the question is do we have a preference between:
- map[addr] -> udpListenerConfig.listenerWorkerRouter(addr).deliver(worker_id, packet)
or - map[addr+worker_index] -> ActiveUdpListener.post(packet)
Option 1 transforms addresses to strings in listenerWorkerRouter, and appears to not have a special case for 0.0.0.0 addresses, so maybe option 2 is necessarily the way.
There was a problem hiding this comment.
I think option 1 looks cleaner, given that you don't need to call registerUdpForwardingListener() in worker threads, but on main thread. But I'm still not very clear about the thread model in hot restart. Ideally we can build a map from listening address to listener config in main thread at start up, and use it against forwarded packets' destination address during hot restart by the hot restart thread (may be it's main thread?).
Either way you need special handling for 0.0.0.0 in the map and probably in listenerWorkerRouter() because it is originally passed in with configured listening address instead of packet destination address.
There was a problem hiding this comment.
Well option 1 turns out to be much cleaner now that I'm passing it from a bit further up the stack. The call is made from the main thread (a few calls up it's in a TRY_ASSERT_MAIN_THREAD block), which I think is also the thread hot restart mostly runs on. I assume tsan will yell at me at some point in this process if that's incorrect.
Included a comment about 0.0.0.0, implementation for it will be later, and I think can stay out of listenerWorkerRouter (I just have to be sure to pass 0.0.0.0 into listenerWorkerRouter if that's the listener the hot restart function chooses.)
|
/wait |
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
| virtual int duplicateParentListenSocket(const std::string& address, uint32_t worker_index) PURE; | ||
|
|
||
| /** | ||
| * Registers an ActiveUdpListener as a possible receiver of udp packets forwarded from the parent |
There was a problem hiding this comment.
s/ActiveUdpListener/UdpListenerCallbacks
| listener_config, quic_config, kernel_worker_routing, enabled, quic_stat_names, | ||
| packets_to_read_to_connection_count_ratio, crypto_server_stream_factory, proof_source_factory, | ||
| std::move(cid_generator), worker_selector_); | ||
| server_.hotRestart().registerUdpForwardingListener(std::move(addr), worker_index, *listener); |
There was a problem hiding this comment.
Is server_ shared across the workers? (I'm not very clear about which part is done in the main thread and which part is done on worker threads.) Should we post the registration to the main thread here?
There was a problem hiding this comment.
I'm going to stack this onto #29306 which will make hotRestart() part of quic_context_ rather than adding server_ for access to it, but yes, either way it's a reference to a common object.
It looks like these operations are indeed on a worker thread, which I didn't realize - how is posting to the main thread done? Does hot restart even run on the main thread? I think it would be okay to make any thread-resolving part of the work of registerUdpForwardingListener rather than doing it here, anyway. (Thinking if posting isn't easy, mutex-guarding a map would work for simplicity, since after the registration is complete there'll be no contention so it shouldn't be a significant performance burden.)
There was a problem hiding this comment.
(Holding off on the stacking until the other one lands because doing it now would probably be disruptive to review.)
There was a problem hiding this comment.
I don't know if hot restart runs on main thread or other stand-alone thread, but definitely not on worker threads.
Dispatcher has an interface post() to post callback function to the thread the Dispatcher object runs event loop on, which is thread safe.
There was a problem hiding this comment.
But the only dispatcher we have access to here is the worker thread's dispatcher, isn't it? So it wouldn't be useful for getting something onto the hot restarter's thread.
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
| const Network::Address::Instance& /* address */, | ||
| Network::UdpListenerConfig& /* listener_config */) { | ||
| ASSERT_IS_MAIN_OR_TEST_THREAD(); | ||
| // TODO(ravenblack): Actually register it in the next PR. |
There was a problem hiding this comment.
Thoughts on implementing it in the current PR, so that we don't need to guess how it would look like or questioning if this is gonna work during code review? At this point the PR doesn't look too big anyway.
There was a problem hiding this comment.
If this is a tacit approval of the thing so far then I'm okay with extending it in-place - just trying to avoid cascading hideous refactors by going one step at a time (which has already been a good idea three times in this PR!)
There was a problem hiding this comment.
To some extent I'd rather not though, because I have work in progress on one of the adjoining pieces, which already declares a structure into which this registration will occur. I'm trying to leave the joins until last so the work can be parallelized.
There was a problem hiding this comment.
I had a hard time to review this PR because none of us can confidently tell if the current implementation would work. So I'd prefer to as least have a proof of concept and then looking into how to cover the corner cases and cut it into smaller changes for review. For this PR to make sense, I think we would like to see at least the child process ends up with a map of listening address to the corresponding listener config's worker router during hot restart. It can be extending this PR in place, or implementing this method in a stand-alone PR before hooking it in this PR.
There was a problem hiding this comment.
Added that, and the retrieval-per-address part with handling for 'any' addresses since I know you're especially interested in reviewing that. :)
And some testing.
|
/wait |
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
|
/wait |
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Is this outdated? |
Updated. |
| // to support QUIC packet forwarding. | ||
| class UdpForwardingContext { | ||
| public: | ||
| using ForwardEntry = std::pair<const Network::Address::Instance*, Network::UdpListenerConfig*>; |
There was a problem hiding this comment.
It's scary to store address pointer here. Do you know its life time? I'm less concerned about UdpListenerConfig lifetime.
Or is it needed given it's the listener address in UdpListenerConfig?
There was a problem hiding this comment.
Sorry, UdpListenerConfig knows nothing about the listening socket. Could you clone the socket?
There was a problem hiding this comment.
It's needed because the call later is UdpListenerConfig::listenerWorkerRouter(addr).deliver(...) - the UdpListenerConfig instance contains multiple addresses.
| } | ||
| udp_listener_config_->listener_worker_routers_.emplace( | ||
| address.asString(), std::make_unique<Network::UdpListenerWorkerRouterImpl>(concurrency)); | ||
| parent_.server_.hotRestart().registerUdpForwardingListener(address, *udp_listener_config_); |
There was a problem hiding this comment.
Do think it make sense to call registerUdpForwardingListener() outside of this method where you have access to the address shared_ptr?
There was a problem hiding this comment.
I don't think it's necessary/useful since we have no choice but to index the map by string anyway (like the line above also does!)
(Rather, there is actually a choice, but if we were to index with an address and a custom hash function then the same treatment could be given to listener_worker_routers_ and it would turn into a big refactor that I'd very much rather not do at this time.)
There was a problem hiding this comment.
Ah, I see, you were thinking it could be a shared_ptr in the ForwardEntry rather than a bare pointer. Will take a moment to look into it.
Signed-off-by: Raven Black <[email protected]>
| class UdpForwardingContext { | ||
| public: | ||
| using ForwardEntry = std::pair<const Network::Address::Instance*, Network::UdpListenerConfig*>; | ||
| using ForwardEntry = std::pair<Network::Address::InstanceConstSharedPtr, |
There was a problem hiding this comment.
This looks safer now. Thanks!
Do you think we can key the map on Network::Address::InstanceConstSharedPtr by providing a customized Hash?
There was a problem hiding this comment.
I would prefer not to at this time since listener_worker_routers_ could also have benefited from doing that, and didn't do it - I'd tend to assume there's a reason for that.
It does look like Address::Instance has an equality operator that would mostly do the right thing, but given earlier conversations about dynamic_cast on the previous PR, there might be some issue around dynamic_cast e.g. here where if true dynamic_cast isn't available then this would provoke an error if you were to look up an ipv4 address in a hash_map where it shares a hash with an ipv6 addresses.
Maybe worth filing an issue to update both things to key on an InstanceConstSharedPtr though.
There was a problem hiding this comment.
I would prefer not to at this time since
listener_worker_routers_could also have benefited from doing that, and didn't do it - I'd tend to assume there's a reason for that.It does look like Address::Instance has an equality operator that would mostly do the right thing, but given earlier conversations about dynamic_cast on the previous PR, there might be some issue around dynamic_cast e.g. here where if true dynamic_cast isn't available then this would provoke an error if you were to look up an ipv4 address in a hash_map where it shares a hash with an ipv6 addresses.
This depends on how you implement Hash. But filing a issue for both and handle it when needed sounds good.
There was a problem hiding this comment.
Also would be awkward if you keyed on shared pointers, when you want to find() an address and what you have is a reference or local instance. I don't think there's a way to use alternative hash and equality operators for different candidate types.
Signed-off-by: Raven Black <[email protected]>
danzh2010
left a comment
There was a problem hiding this comment.
LGTM, @RyanTheOptimist over to you.
Signed-off-by: Raven Black <[email protected]>
RyanTheOptimist
left a comment
There was a problem hiding this comment.
Looks great, modulo one nit.
| SharedMemory* shmem_; | ||
| ProcessSharedMutex log_lock_; | ||
| ProcessSharedMutex access_log_lock_; | ||
| friend class HotRestartUdpForwardingTestHelper; |
There was a problem hiding this comment.
nit: friend declarations should come at the top of the section
There was a problem hiding this comment.
Is it envoy policy to obey all google style rules?
I would expect this one would be enforced by a linter if we really want it enforced, but okay, done anyway.
There was a problem hiding this comment.
Yes, that's the policy:
Beyond code formatting, for the most part Envoy uses the Google C++ style guidelines. The following section covers the major areas where we deviate from the Google guidelines.
The Style Guide go well beyond things which can be enforces with the tooling we have, annoyingly.
There was a problem hiding this comment.
Did you push a new commit? I'm not seeing a change, fwiw.
There was a problem hiding this comment.
Sorry about that, I had done the relevant git push but replied too quickly, before the git hooks had finished!
| std::shared_ptr<Network::UdpListenerConfig> listener_config) { | ||
| const bool inserted = | ||
| listener_map_.try_emplace(address->asString(), ForwardEntry{address, listener_config}).second; | ||
| ASSERT(inserted, "Two udp listeners on the same address shouldn't be possible"); |
There was a problem hiding this comment.
Out of curiosity, what enforces the invariant that there only is a single listener per interface?
There was a problem hiding this comment.
Per interface and port. I'm not sure if envoy enforces that you can't, and SO_REUSEPORT means the operating system won't enforce that you can't, but I hope envoy enforces that you can't because if you do have multiple listeners with different behaviors on the same interface and port you'd get horrific mixed delivery. (And if you have multiple listeners on the same interface+port with identical behavior then why?!)
Signed-off-by: Raven Black <[email protected]>
Commit Message: Provide a listener to hot restart for udp packet forwarding
Additional Description: A part of #19454 - in order to be able to route udp packets delivered via a back channel, the hot restart child instance needs sufficient access to the quic listeners to be able to deliver those packets. The data passed to
duplicateParentListenSocketis already sufficient to uniquely identify a socket; this adds a UdpListenerConfig reference such that the hot restart child class can build a hashmap from addresses to UdpListenerConfigs. Upon receiving a forwarded packet, it will thereby be able to calldeliveron the appropriate UdpListenerConfig to get that packet delivered.Risk Level: Low, essentially a no-op at this point.
Testing: Should be covered by existing tests.
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a