Repository navigation
Add capability for ActiveQuicListener to custom handle undeliverable packets - #28664
Conversation
Signed-off-by: Raven Black <[email protected]>
|
Assigning Ryan for HTTP/3 expertise. /assign @RyanTheOptimist |
Signed-off-by: Raven Black <[email protected]>
| * Inject optional custom handling of UDP packets during hot restart. | ||
| * It is up to the implementation to capture and call the provided function. | ||
| */ | ||
| virtual void onHotRestarting(HotRestartPacketForwardingFunction) {} |
There was a problem hiding this comment.
Does hot restart procedure call into shutdownListener()? Can the callback be part of that interface instead?
There was a problem hiding this comment.
Maybe, if we're doing forwarding only parent->child (and child not actually listening directly until drain is complete), as discussed in the thread. I'll have to look into it a bit, I think that it might not be great to complicate the common interface for the rare case.
There was a problem hiding this comment.
I guess that could be okay if shutdownListener takes some sort of abstract shared object parameter, then individual listeners could try to pull out dynamic_pointer_cast<SomethingThisListenerCaresAbout>.
Like shutdownListener(std::shared_ptr<AdditionalShutdownParameters>)
and class HotRestartShutdownParameters : public AdditionalShutdownParameters, public HotRestartPacketForwardingProvider
Putting specifically a hot restart parameter in shutdownListener, that only Quic listeners actually care about, seems like it would be more confusing even than this tangled dynamic_cast object (and much more horrible for the risk of future custom arguments that some other listener might want - the dynamic object at least makes it flexible for delivering whatever sort of arguments any listener might be interested in).
Is that better than a new virtual function that for most listeners is a no-op? I can see how maybe because it saves on piping a new call to the virtual function through all the various layers of controllers. But changing shutdownListener is still going to require a change to all those same layers, this time, at least.
There was a problem hiding this comment.
Where is onHotRestarting() going to be called? Can the call site distinguish what listener it is calling into?
There was a problem hiding this comment.
I was planning to call it from somewhere around the hot restarting functions where the socket ids are collected. The idea is the call site wouldn't need to distinguish which listeners need it, because all the other listeners have no-op implementations.
There was a problem hiding this comment.
Made a try with a parameter to shutdownListeners, to see what you think.
There was a problem hiding this comment.
Thanks for the code pointer! It seems that it's seperate RPC calls to PassListenSocket and DrainListeners: https://sourcegraph.com/github.com/envoyproxy/envoy/-/blob/source/server/hot_restarting_parent.cc?L52. Though it makes more sense to tell the listener whether/how to forward packets while telling it to drain, but it's not very good to collect socket ids twice once. So I don't strong opinion on this.
But we do want to make sure the sequence is right so that the parent instance knows how to forward the packets once it starts draining. Can we enforce the sequence on child side?
There was a problem hiding this comment.
I'm still working on this aspect in the next PR. Since I've changed it to passing in shutdownListeners like you asked, there's now no choice about where to provoke the change, it has to be drainListeners. It's also pretty awkward on the child side now, because with this "only parent to child" forwarding (rather than the original plan of two-way), hitting the right moment for the child to start listening is tricky - every other socket starts listening immediately, but this one will now have to hold off on listening until a moment before the parent drain completes. (On the up side, the location of this awkwardness is the same as the location where we'll need to do something awkward for the eBPF version, so at least the effort is dual-purpose.)
This part of the conversation should probably move to slack or the relevant issue though, since it's not important to this PR. :)
There was a problem hiding this comment.
I'm still working on this aspect in the next PR. Since I've changed it to passing in shutdownListeners like you asked, there's now no choice about where to provoke the change, it has to be drainListeners.
Actually doing it in drainListener() can enforce that.
It's also pretty awkward on the child side now, because with this "only parent to child" forwarding (rather than the original plan of two-way), hitting the right moment for the child to start listening is tricky - every other socket starts listening immediately, but this one will now have to hold off on listening until a moment before the parent drain completes. (On the up side, the location of this awkwardness is the same as the location where we'll need to do something awkward for the eBPF version, so at least the effort is dual-purpose.)
Yes, I would guess this needs an extra RPC call from parent to child saying "I'm done" if there hasn't been one. But would forwarding both ways should have the same need about knowing parent has been drained.
This part of the conversation should probably move to slack or the relevant issue though, since it's not important to this PR. :)
This sounds like a more comprehensive design doc!
There was a problem hiding this comment.
Having the parent say when it's done would be a dangerous choice, in that if the parent exited prematurely the child will never start listening, plus there's no existing channel for this form of communication (though it could piggyback on the packet forwarding channel which is also new).
My current thought is that the child starting listening immediately before or after it tells the parent to terminate would make sense, and be much simpler. (Doesn't matter if it's before or after because the socket exists, so it can still buffer up some content after the other instance closes, and if it picks up a few packets that were intended for connections associated with the parent it will reject them, which is okay because the parent is about to terminate and reject all packets anyway.)
|
Can you take a look @RyanTheOptimist , and @danzh2010? Thank you |
|
@danzh2010 I'm happy for you to do the first pass on this. Let me know when you're happy. |
| Network::UdpRecvData packet; | ||
| packet.addresses_.local_ = quicAddressToEnvoyAddressInstance(received_packet_info.self_address); | ||
| packet.addresses_.peer_ = quicAddressToEnvoyAddressInstance(received_packet_info.peer_address); | ||
| packet.buffer_ = std::make_unique<Buffer::OwnedImpl>(received_packet_info.packet.AsStringPiece()); | ||
| packet.receive_time_ = MonotonicTime( | ||
| std::chrono::microseconds{received_packet_info.packet.receipt_time().ToDebuggingValue()}); |
There was a problem hiding this comment.
Instead of converting a Network::UdpRecvData object in ActiveQuicListener::onDataWorker() to a quic::QuicReceivedPacket and then to quic::ReceivedPacketInfo and then convert it back to UdpRecvData, can we move packet forwarding logic to the listener? I.e. we can keep some states in the EnvoyQuicDispatcher about whether the Envoy is during hot restart and whether the current packet should forwarded. Then at end of onDataWorker() check the states and move the UdpRecvData if forwarding is needed.
There was a problem hiding this comment.
That is an option, but doing it that way adds extra checks in the common path (for every normal successfully delivered packet, not during hot restart). I think a small cost for packets that need to be forwarded is better than a tiny cost in the common path, yes/no?
There was a problem hiding this comment.
(Especially bearing in mind that ideally this whole thing is not in the flow at all if an eBPF implementation takes over, at which point the comparison would be a tiny cost in the common path if we check state in onDataWorker, versus zero cost of any kind.)
There was a problem hiding this comment.
I won't think checking a bool followed by no-op is a cost at all with eBPF. But forwarding packets in hot restarting has substantial cost, and if we copy every packets to be forwarded, that's even more cost.
There was a problem hiding this comment.
The forwarding is already much more expensive than one copy though, and, again, ideally in the performance-sensitive situation this will be completely moot with the eBPF version.
IMO a tiny cost (it's not zero, and it's a bit more than checking a bool, it's clearing and checking a bool every time in the common path, though at least the branch behavior will be mostly predictable) and additional complication in the common path, is a much worse compromise than almost any amount of complexity or slowdown in the occasional, rare, and eventually unused forwarding-during-hot-restart path. It's like a reference implementation of a graphics driver, it's important that it does the right thing, with performance as a distant secondary concern.
That said, if you feel really strongly about this, I can change it to how you want it. Having something that works is more important than having something that works the way I would prefer.
There was a problem hiding this comment.
If draining period would be long, the Envoy will be forwarding most of the incoming packets at the end. Reducing copy is definitely worth doing. It's up to you to decide if your system want this or not, since you know better about the traffic pattern.
| * Inject optional custom handling of UDP packets during hot restart. | ||
| * It is up to the implementation to capture and call the provided function. | ||
| */ | ||
| virtual void onHotRestarting(HotRestartPacketForwardingFunction) {} |
There was a problem hiding this comment.
Where is onHotRestarting() going to be called? Can the call site distinguish what listener it is calling into?
|
/wait |
…. Cancel support for unsetting, as the intent now is to only forward from the old to the new instance. Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
|
I think UDP proxy would also be affect by hot restarting. So ActiveUdpListener probably also need to handle onHotRestarting(forwarding_func) appropriately. |
I don't think regular UDP cares that much for the most part, because it makes no difference which instance handles the packet (unless the set of filters has changed). It's only the connection context of QUIC that makes it instance-dependent. (I definitely don't want to be also changing the behavior of regular UDP, so if it is relevant, it can be relevant later.) |
It also keeps a state based on downstream 4 tuple and upstream 4 tuple. So I think it is affect. But Lyft might not need to use it? @mattklein123
You don't need to provide a solution for that. But it's worth to add a ENVOY_BUG or comment along the code path to indicate there is a gap. |
| namespace Envoy { | ||
| namespace Quic { | ||
|
|
||
| class HotRestartPacketForwardingOptions : virtual public Network::ExtraShutdownListenerOptions { |
There was a problem hiding this comment.
If you are not doing to distinguish QUIC listener from TCP ones, how would you know which drainListener() call should pass down this object instead of the place holder? Would it make more sense to pass down more general information, i.e. child process UNIX domain socket address, in ExtraShutdownListenerOptions for all types of listeners instead of passing down a QUIC specific one?
There was a problem hiding this comment.
This is what the dynamic typing is for - you can pass in an object containing all the special-case information for all types of listener (which right now is just the one), and the listeners can pick out the information they care about and ignore any information they don't care about. So the answer is the drainListener call that gets started by the hot restart classes can pass the same set of options down to all the resulting shutdownListeners calls.
Passing a domain socket in would mean the quic stuff would need to know about the whole hot restart RPC protocol, which would also make it super unwieldy to unit test.
Trying to pick out listeners by type in advance would be a large unwieldy change because the listener manager barely allows access to individual listeners at all, and would be putting a bunch of decision logic in a place where it doesn't really need to be.
The lambda part acts as an abstract interface that makes it easy to mock.
There was a problem hiding this comment.
Passing a domain socket in would mean the quic stuff would need to know about the whole hot restart RPC protocol, which would also make it super unwieldy to unit test.
Trying to pick out listeners by type in advance would be a large unwieldy change because the listener manager barely allows access to individual listeners at all, and would be putting a bunch of decision logic in a place where it doesn't really need to be.
In light of these 2 statements, how do you plan to call shutdownListener() with the right ExtraShutdownListenerOptions implementation?
There was a problem hiding this comment.
Construct and pass in the options here. The options parameter needs to be expanded out another few layers, but not yet in this PR. Trying to keep it in small pieces.
There was a problem hiding this comment.
Okay, my suggestion is instead of instantiate a QUIC specific object there, removing the abstraction and adding an interface in ExtraShutdownListenerOptions and name it getUdpPacketForwardingFunction(). I assume as long as the underlying UDP listener want to forward packets during draining, either QUIC or UDP proxy, they should use the same way.
There was a problem hiding this comment.
The problem with getting rid of the abstraction is it's inflexible for any future custom behaviors we might want to support. If it's just an interface and we override it for this use-case, and then we have a different use-case later that wants to pass different things in from a different caller for a different listener, when we add another function to Options, the existing implementation now needs to override that function too, and doesn't have anything to override it with, and the new caller needs to provide an override for getUdpPacketForwardingFunction that it doesn't care about, and the normal caller passing nullptr means now we have to check if the options passed was nullptr or the function-getter returns nullptr, where with the dynamic_cast the existence of the interface implies the existence of the function.
OR you end up adding another parameter to all the shutdown functions for the new purpose of interface, which is even messier.
Compromise change, I've made it HotRestartUdpPacketForwarding, moved it out of the quic context, and made the interface part of the class so there's no lambdas involved now.
| Network::UdpRecvData packet; | ||
| packet.addresses_.local_ = quicAddressToEnvoyAddressInstance(received_packet_info.self_address); | ||
| packet.addresses_.peer_ = quicAddressToEnvoyAddressInstance(received_packet_info.peer_address); | ||
| packet.buffer_ = std::make_unique<Buffer::OwnedImpl>(received_packet_info.packet.AsStringPiece()); | ||
| packet.receive_time_ = MonotonicTime( | ||
| std::chrono::microseconds{received_packet_info.packet.receipt_time().ToDebuggingValue()}); |
There was a problem hiding this comment.
If draining period would be long, the Envoy will be forwarding most of the incoming packets at the end. Reducing copy is definitely worth doing. It's up to you to decide if your system want this or not, since you know better about the traffic pattern.
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
danzh2010
left a comment
There was a problem hiding this comment.
LGTM other than a few nits
| virtual void forwardUdpPacket(uint32_t worker_index, const Network::UdpRecvData& packet) PURE; | ||
| }; | ||
|
|
||
| using ExtraShutdownListenerOptionsPtr = std::shared_ptr<ExtraShutdownListenerOptions>; |
There was a problem hiding this comment.
nits: ExtraShutdownListenerOptionsSharedPtr. *Ptr is usually used in alias for unique_ptr.
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]>
danzh2010
left a comment
There was a problem hiding this comment.
Hand over to @RyanTheOptimist
RyanTheOptimist
left a comment
There was a problem hiding this comment.
Overall this looks great. One question, though, about dyanmic_cast which I think is not supported everywhere.
| virtual ~ExtraShutdownListenerOptions() = default; | ||
| }; | ||
|
|
||
| // An interface for ExtraShutdownListenerOptions enabling us to forward UDP packets from |
There was a problem hiding this comment.
nit: Avoid first person in comments since it can be a bit ambiguous who this refers to. In this case, How about: "enabling Envoy" or "enabling the Listener".
| * to configure packet forwarding during hot restart. | ||
| */ | ||
| virtual void shutdownListener() PURE; | ||
| virtual void shutdownListener(ExtraShutdownListenerOptionsSharedPtr options = nullptr) PURE; |
There was a problem hiding this comment.
Instead of having to do a dynamic_cast (which is not supported on all Envoy build environments) could we define a method that all Options are required to implement (onPacket(...) or some such) and just call that?
There was a problem hiding this comment.
Hm, it seems like dynamic_cast is pretty much already a requirement (crypto.h and conn_pool.h for example use dynamic_casts), but okay, I changed it to a way that doesn't use dynamic cast and still keeps it flexible for future behaviors.
There was a problem hiding this comment.
If I understand things correctly, in some environments, dynamic_cast is transformed to (essentially) static_cast. So it will still cast from type A to type B, but it does not have the "return null if the classes are incompatible" logic. That means code like:
ASSERT(dynamic_cast<T*>(&context) != nullptr);
"works", but is basically a no-op.
I think.
There was a problem hiding this comment.
Ah, I see, and with no RTTI dynamic_cast becomes a compile-time error if you're casting between types that don't have a known direct relation (but then why do we use dynamic_cast rather than static_cast in those contexts!)
There was a problem hiding this comment.
I think it turns into undefined behavior at runtime! Let's say we have 3 classes, Base, DerivedA and DerivedB. Code will compile just fine which does:
dyanmic_cast<DerivedA*>(base_ptr); or static_cast<DerivedA*>(base_ptr);
However, if base_ptr is actually a pointer to a DerivedB object then we get a behavior change. dynamic_cast will return nullptr (because the RTTI lets dynamic_cast know that the object in question is NOT DerivedA.). However, static cast will just blindly return a totally-bogus DerivedA* pointer to a DerivedB object. This is ... bad :)
| void ActiveQuicListener::shutdownListener() { | ||
| void ActiveQuicListener::shutdownListener(Network::ExtraShutdownListenerOptionsSharedPtr options) { | ||
| hot_restart_udp_packet_forwarding_ = | ||
| std::dynamic_pointer_cast<Network::HotRestartUdpPacketForwarding>(options); |
There was a problem hiding this comment.
In some Envoy build environments (where RTTI is disabled) I think this will not work :/
| iter->second->invokeListenerMethod([](Network::ConnectionHandler::ActiveListener& listener) { | ||
| if (listener.listener() != nullptr) { | ||
| listener.shutdownListener(); | ||
| listener.shutdownListener(nullptr); |
There was a problem hiding this comment.
nit: do we need the nullptr here since I think that's the default? (I'm also happy to not have default arguments and instead be explicit everywhere)
There was a problem hiding this comment.
Removed the default, and now it's {} because the parameter isn't a pointer.
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
RyanTheOptimist
left a comment
There was a problem hiding this comment.
Looks great! One small naming nit.
| class HotRestartUdpPacketForwarding : virtual public ExtraShutdownListenerOptions { | ||
| public: | ||
| virtual void forwardUdpPacket(uint32_t worker_index, const Network::UdpRecvData& packet) PURE; | ||
| struct ExtraShutdownListenerOptions { |
There was a problem hiding this comment.
nit: Brief comment on this struct.
| * to configure packet forwarding during hot restart. | ||
| */ | ||
| virtual void shutdownListener() PURE; | ||
| virtual void shutdownListener(ExtraShutdownListenerOptionsSharedPtr options = nullptr) PURE; |
There was a problem hiding this comment.
If I understand things correctly, in some environments, dynamic_cast is transformed to (essentially) static_cast. So it will still cast from type A to type B, but it does not have the "return null if the classes are incompatible" logic. That means code like:
ASSERT(dynamic_cast<T*>(&context) != nullptr);
"works", but is basically a no-op.
I think.
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
|
/retest |
Commit Message: Add capability for special UDP packet handling during hot restart
Additional Description: Adds a flexible argument for shutdownListeners, making it possible to pass an additional argument to QUIC listeners (or others); uses that to pass a callback to
ActiveQuicListenerandEnvoyQuicDispatcher. This is a no-op in production since the parameter is currently always nullptr, but is a first step towards allowing us to catch new connections on a QUIC listener and forward them to a new instance during hot restart. An important part of #19454Risk Level: Low, essentially a no-op. Adds one nullptr check in the common path per nonroutable quic packet; zero touch to the common routable packet path.
Testing: Added unit test in both EnvoyQuicDispatcher and ActiveQuicListener.
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a