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

network: delegate file event creation to io handle #12720

Merged
merged 2 commits into from
Aug 20, 2020

Conversation

florincoras
Copy link
Member

Avoid exposing the fd and allow the io handle to build custom file events.

Signed-off-by: Florin Coras fcoras@cisco.com

Risk Level: Low
Testing: unit tests
Docs Changes: n/a
Release Notes: n/a

Avoid exposing the fd and allow the io handle to build custom file
events.

Signed-off-by: Florin Coras <fcoras@cisco.com>
@florincoras
Copy link
Member Author

cc @antoniovicente would be good to get your review on this!

@antoniovicente
Copy link
Contributor

/assign @antoniovicente

Signed-off-by: Florin Coras <fcoras@cisco.com>
Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

Thanks this is great. One high level comment.

* should initially listen on.
* @return @ref Event::FileEventPtr
*/
virtual Event::FileEventPtr createFileEvent(Event::Dispatcher& dispatcher, Event::FileReadyCb cb,
Copy link
Member

Choose a reason for hiding this comment

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

The long term plan continues to be to completely remove "fd" from all top level dispatcher interfaces, right? My main concern is there is nothing right now that prevents not calling the original FileEvent factory function with the IoHandle fd, right? Can we add relevant comments around usage, TODOs for eventual removal, warnings, etc.? My quick take would be that if we have a similar function on the filesystem interface we might be able to completely remove the public interface that allows file event creation from fd sooner rather than later (I'm not sure what else actually uses that factory currently)? wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's exactly right. The goal is to use IoHandles instead of fds whenever possible.

Apart from the sockets, others that use the dispatcher's original FileEvent factory function are:

  • WatcherImpl, which creates inotify events using raw fds
  • DnsImpl, which relies on raw fds from Ares
  • HotRestartingParent, which also uses raw fds

In theory, we can change all of these, including IoSocketHandleImpl to allocate FileEventPtrs directly and then completely remove the dispatcher's public interface. The only thing we'll miss out on is the isThreadSafe() check, but maybe we can make the function public? I can take care of this but should we do it as part of this PR or a separate one?

Finally, out of those 3, maybe we should update at least DnsImpl to use IoHandles (not sure if that's easy).

Copy link
Member

Choose a reason for hiding this comment

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

In theory, we can change all of these, including IoSocketHandleImpl to allocate FileEventPtrs directly and then completely remove the dispatcher's public interface. The only thing we'll miss out on is the isThreadSafe() check, but maybe we can make the function public? I can take care of this but should we do it as part of this PR or a separate one?

I would prefer we just do it now if you are up for it, but if you feel strongly we can do it as a fast follow up?

Finally, out of those 3, maybe we should update at least DnsImpl to use IoHandles (not sure if that's easy).

Yeah that would be nice. Maybe add a TODO there for follow up later?

Copy link
Member Author

@florincoras florincoras Aug 20, 2020

Choose a reason for hiding this comment

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

Was about to start work on it but it turns out FileEventImpl depends on DispatcherImpl and all is bundled in //source/common/event:dispatcher_lib. Trying to add the latter as dependency to the default_socket_interface_lib quickly runs into circular dependencies. I guess this is because we don't want to expose the scheduler's implementation, i.e., libevent's event_base, in the dispatcher's interface.

Unless there's a smarter way, we'll have have to separate FileEventImpl from DispatcherImpl and that might not be trivial (they'd have to exchange libevent specific objects over generic apis). The alternative would be to still use the dispatcher as a FileEvent factory and work towards removing the fd from IoHandle (more of that here and #12736). But that exercise is also not trivial because "higher level transports" like tls and quic do want to see the raw fds. So it might be some time until we cleanup everything.

What option would you prefer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Eliminating use of the IoHandle::fd() method seems like progress.

Direct creation of FileEvents instead of going through the dispatcher would reduce out ability to mock out FileEvent operations or inject test versions that can easily be marked read/write blocked, unless we add some factory interface similar to the one provided by the dispatchers today.

Copy link
Member

Choose a reason for hiding this comment

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

Yes potentially we can just have a format/lint checker while we do further cleanups.

Copy link
Member

Choose a reason for hiding this comment

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

and yes maybe also rename fdDoNotUse() or something until we fix it all.

Copy link
Member Author

Choose a reason for hiding this comment

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

Works for me! Can we do it after all the PRs are merged to limit impact, i.e., change name and then delete them?

Copy link
Member

Choose a reason for hiding this comment

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

Yup that's fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Added to my todo list :-)

file_event_ = dispatcher_.createFileEvent(
ConnectionImpl::ioHandle().fd(), [this](uint32_t events) -> void { onFileEvent(events); },
trigger, Event::FileReadyType::Read | Event::FileReadyType::Write);
file_event_ = socket_->ioHandle().createFileEvent(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The RELEASE_ASSERT in line 62 above may make more sense in IoSocketHandleImpl::createFileEvent, DispatcherImpl::createFileEvent or FileEventImpl so we can remove the call to the ioHandle fd method.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was actually planning a separate PR that removes all of those asserts! Should I do this one in particular here or is it fine if we postpone it to the next PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Next PR is fine.

Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

Thanks!

@mattklein123 mattklein123 self-assigned this Aug 20, 2020
@mattklein123 mattklein123 merged commit 2f6f697 into envoyproxy:master Aug 20, 2020
@florincoras florincoras deleted the iohandle_event branch August 28, 2020 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants