Class NIOServerCnxnFactory.SelectorThread

  • All Implemented Interfaces:
    Runnable
    Enclosing class:
    NIOServerCnxnFactory

    public class NIOServerCnxnFactory.SelectorThread
    extends ZooKeeperThread
    The SelectorThread receives newly accepted connections from the AcceptThread and is responsible for selecting for I/O readiness across the connections. This thread is the only thread that performs any non-threadsafe or potentially blocking calls on the selector (registering new connections and reading/writing interest ops). Assignment of a connection to a SelectorThread is permanent and only one SelectorThread will ever interact with the connection. There are 1-N SelectorThreads, with connections evenly apportioned between the SelectorThreads. If there is a worker thread pool, when a connection has I/O to perform the SelectorThread removes it from selection by clearing its interest ops and schedules the I/O for processing by a worker thread. When the work is complete, the connection is placed on the ready queue to have its interest ops restored and resume selection. If there is no worker thread pool, the SelectorThread performs the I/O directly.
    • Method Detail

      • addAcceptedConnection

        public boolean addAcceptedConnection​(SocketChannel accepted)
        Place new accepted connection onto a queue for adding. Do this so only the selector thread modifies what keys are registered with the selector.
      • addInterestOpsUpdateRequest

        public boolean addInterestOpsUpdateRequest​(SelectionKey sk)
        Place interest op update requests onto a queue so that only the selector thread modifies interest ops, because interest ops reads/sets are potentially blocking operations if other select operations are happening.
      • run

        public void run()
        The main loop for the thread selects() on the connections and dispatches ready I/O work requests, then registers all pending newly accepted connections and updates any interest ops on the queue.
        Specified by:
        run in interface Runnable
        Overrides:
        run in class Thread