Package org.apache.thrift.server
Class TThreadedSelectorServer
java.lang.Object
org.apache.thrift.server.TServer
org.apache.thrift.server.AbstractNonblockingServer
org.apache.thrift.server.TThreadedSelectorServer
A Half-Sync/Half-Async server with a separate pool of threads to handle non-blocking I/O. Accepts
are handled on a single thread, and a configurable number of nonblocking selector threads manage
reading and writing of client connections. A synchronous worker thread pool handles processing of
requests.
Performs better than TNonblockingServer/THsHaServer in multi-core environments when the bottleneck is CPU on the single selector thread handling I/O. In addition, because the accept handling is decoupled from reads/writes and invocation, the server has better ability to handle back-pressure from new connections (e.g. stop accepting when busy).
Like TNonblockingServer, it relies on the use of TFramedTransport.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
The thread that selects on the server transport (listen socket) and accepts new connections to hand off to the IO selector threadsstatic class
protected class
The SelectorThread(s) will be doing all the selecting on accepted active connections.protected static class
A round robin load balancer for choosing selector threads for new connections.Nested classes/interfaces inherited from class org.apache.thrift.server.AbstractNonblockingServer
AbstractNonblockingServer.AbstractNonblockingServerArgs<T extends AbstractNonblockingServer.AbstractNonblockingServerArgs<T>>, AbstractNonblockingServer.AbstractSelectThread, AbstractNonblockingServer.AsyncFrameBuffer, AbstractNonblockingServer.FrameBuffer
Nested classes/interfaces inherited from class org.apache.thrift.server.TServer
TServer.AbstractServerArgs<T extends TServer.AbstractServerArgs<T>>
-
Field Summary
Fields inherited from class org.apache.thrift.server.TServer
eventHandler_, inputProtocolFactory_, inputTransportFactory_, outputProtocolFactory_, outputTransportFactory_, processorFactory_, serverTransport_, stopped_
-
Constructor Summary
ConstructorsConstructorDescriptionCreate the server with the specified Args configuration -
Method Summary
Modifier and TypeMethodDescriptionprotected static ExecutorService
Helper to create the invoker if one is not specifiedcreateSelectorThreadLoadBalancer
(Collection<? extends TThreadedSelectorServer.SelectorThread> threads) Creates a SelectorThreadLoadBalancer to be used by the accept thread for assigning newly accepted connections across the threads.protected Runnable
getRunnable
(AbstractNonblockingServer.FrameBuffer frameBuffer) protected void
protected void
protected boolean
requestInvoke
(AbstractNonblockingServer.FrameBuffer frameBuffer) We override the standard invoke method here to queue the invocation for invoker service instead of immediately invoking.protected boolean
Start the accept and selector threads running to deal with clients.void
stop()
Stop serving and shut everything down.protected void
Joins the accept and selector threads and shuts down the executor service.Methods inherited from class org.apache.thrift.server.AbstractNonblockingServer
serve, startListening, stopListening
Methods inherited from class org.apache.thrift.server.TServer
getEventHandler, getShouldStop, isServing, setServerEventHandler, setServing, setShouldStop
-
Constructor Details
-
TThreadedSelectorServer
Create the server with the specified Args configuration
-
-
Method Details
-
startThreads
protected boolean startThreads()Start the accept and selector threads running to deal with clients.- Specified by:
startThreads
in classAbstractNonblockingServer
- Returns:
- true if everything went ok, false if we couldn't start for some reason.
-
waitForShutdown
protected void waitForShutdown()Joins the accept and selector threads and shuts down the executor service.- Specified by:
waitForShutdown
in classAbstractNonblockingServer
-
joinThreads
- Throws:
InterruptedException
-
stop
public void stop()Stop serving and shut everything down. -
gracefullyShutdownInvokerPool
protected void gracefullyShutdownInvokerPool() -
requestInvoke
We override the standard invoke method here to queue the invocation for invoker service instead of immediately invoking. If there is no thread pool, handle the invocation inline on this thread- Specified by:
requestInvoke
in classAbstractNonblockingServer
- Returns:
- true if invocation was successfully requested, which is not a guarantee that invocation has completed. False if the request failed.
-
getRunnable
-
createDefaultExecutor
Helper to create the invoker if one is not specified -
createSelectorThreadLoadBalancer
protected TThreadedSelectorServer.SelectorThreadLoadBalancer createSelectorThreadLoadBalancer(Collection<? extends TThreadedSelectorServer.SelectorThread> threads) Creates a SelectorThreadLoadBalancer to be used by the accept thread for assigning newly accepted connections across the threads.
-