Class NIOServerCnxnFactory.SelectorThread

  • All Implemented Interfaces:
    java.lang.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.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      SelectorThread​(int id)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addAcceptedConnection​(java.nio.channels.SocketChannel accepted)
      Place new accepted connection onto a queue for adding.
      boolean addInterestOpsUpdateRequest​(java.nio.channels.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.
      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.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • SelectorThread

        public SelectorThread​(int id)
                       throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • addAcceptedConnection

        public boolean addAcceptedConnection​(java.nio.channels.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​(java.nio.channels.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 java.lang.Runnable
        Overrides:
        run in class java.lang.Thread