Skip to content

event loop refactoring #588

Closed
Closed

Description

@Geal

Context

the current design of the event loop is holding us back a bit. First, a small history of how it evolved:

  • at the beginning, mio 0.5 required that we provide objects that implement a Handler trait, that mio's internal loop would call. It was a bit annoying to use, so mio 0.6 moved instead to a Poll object that we would call in a loop. So creating sessions was a bit easier, but the way sessions were working was still a bit based on that Handler trait
  • we had one thread(then one process) with one event loop per proxy type (HTTP, HTTPS or TCP) because it was a bit easier to write than everything in the same loop
  • with merge all the event loops #144 the event loops were merged, by separating the token space in 3. The event loop holds a http::Proxy, a tcp::Proxy, and a HttpsProvider, an enum that contains either a rustls based proxy, or a rustls proxy and an openssl proxy, depending on the use-openssl cargo feature. Each proxy was holding a map of token -> session (they were called "client" at the time)
  • in 45012b8 I introduced a ProxyClient trait that allowed storing all the clients in one slab. I could cast them to their real session type with methods like as_http() or as_tcp(), to be able to call the corresponding proxy's connect_to_backend method with the session as argument
  • in 25fcaf9 I added a way for a proxy to listen on multiple ports (before that, it was possible for TCP, but not for HTTP and HTTPS). A listen socket was stored in the same slab as the sessions, with protocol marked as HTTPListen, TCPListen, etc. That way we can call the corresponding proxy's accept() method

So the current design might seem awkward, because it evolved through multiple different ideas and needs.
I think it is time to change it again, because we might want to add more proxy types (like the peek-sni one I'm working on in a branch), and also because it is hard to modify and causes a lot of code duplication without reducing coupling.

So here's where we are now:

  • the Server struct holds the event loop. It has the following members:
    • sessions: Slab<Rc<RefCell<ProxySessionCast>>,SessionToken>, hiding sessions and listeners behind a ProxySessionCast
    • http: http::Proxy: HTTP proxy
    • tcp: tcp::Proxy: TCP proxy
    • https: HttpsProvider: holds either a openssl or rustls based HTTPS proxy
  • Server will call the ProxySessionCast::protocol() method to get the kind of session or listener, then cast it and pass it to the corresponding proxy (to call accept()or connect_to_backend)
  • a Proxy implements ProxyConfiguration (the remnant of the mio 0.5 trait), and holds a list of Listener, passes configuration information to them, and provides app_id to backend routing information
  • a Listener holds a listen socket, a request router (to get an app id from a request) and some proxy specific info (like certificates)

At some point I was worried about the cost of dynamic dispatch for sessions and listeners, but the current design is probably even slower than that, so I think we could get better results by abstracting things behind trait objects.

Proposal

  • a Proxy kind would only be able to create a Listener, and hold some configuration information common to all its listeners
  • a Listener would hold a link to its proxy. Either by making the Proxy an object with 'static lifetime (since all listeners would necessarily have a shorter life, and it can live for the whole application's run), or holding it in a Rc
  • a Listener is stored in the event loop's slab along other sessions, and has a process_events method that can be called (no special case for listeners). Potential problem: the accept queue and can_accept bool are both managed by the shared event loop. But it might be better to manage an accept queue per listener. And instead of an accept_ready queue, we would put the listeners in the same ready queue as sessions
  • a Listener holds an access to the mio::Poll object. Storing it into a Rc?
  • a Listener can create a Session in its process_events method
  • a Listener has a connect_to_backend method that access the Poll object and the Proxy's configuration information
  • a Session will hold a Rc of its Listener, and be able to call it directly, instead of returning an order that would be caught in the event loop's interpret_client_order method and then redispatched to the Proxy. That point will simplify the design a lot

So with the new design, here's how the event loop would work:

  • deref the Rc<RefCell<Poll>> to call its poll() method and get a list of tokens and events
  • get the Rc<RefCell<ProxySessionCast>> (maybe rename ProxySessionCast to something else) corresponding to the token, call its process_events through dynamic dispatch
  • the process_events could deref the Rc<RefCell<Poll>> if a Listener accepts a connection, or if a Session tries to connect to a backend, but there should be no conflict, since we do not hold it for long
  • this will add one more field per listener, but there will not be a large number of them (65535 at most), and one more field per session. Those are already pretty large, but they hold a Weak ref to the buffer pool, and we could replace that one (since the Listener could hold the buffer pool ref)

I'm still a bit worried that this will make a lot of pointer dereferences, but this is clearly not the biggest overhead there is in sozu right now, and a nicer design is really needed right now to ship more interesting features

Activity

  1. Geal commented on Jun 5, 2019

    @Geal
    MemberAuthor

    probably related to #444 and #448

  2. added this to the 0.12 milestone on Jun 5, 2019
  3. Geal commented on Jun 6, 2019

    @Geal
    MemberAuthor

    we cannot completely decouple the event loop from the proxys internal behaviour, because methods like create_session and connect_to_backend still need to reserve a slot in the sessions slab. Should that slab be accessible from proxy instances as well?

  4. Geal commented on Jun 13, 2019

    @Geal
    MemberAuthor

    the refactoring is now mostly done and apparently does not break anything (except certificates queries for now).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions