SlideShare une entreprise Scribd logo
1  sur  48
Télécharger pour lire hors ligne
4
5
Building an NIO Server
Understanding the problem
Defining a solution
An NIO implementation
6
What Does a Server Do?
A server processes requests:
Receive a client request
Perform some request-specific task
Return a response
Multiple requests run concurrently
Client requests correspond to connections
Sequential requests may be sent on a single socket
Requests may contend for resources
Must tolerate slow, misbehaving or
unresponsive clients
7
Multiplexing Strategies
Poll each socket in turn
Impractical without non-blocking sockets
Inefficient, not fair and scales poorly
Thread-per-socket
Only practical solution with blocking sockets
Stresses the thread scheduler, which limits scalability
Thread scheduler does readiness selection—inefficiently
Readiness selection
Efficient, but requires OS and Java VM support
Scales well, especially for many slow clients
8
Other Considerations
Multi-threading issues are magnified
Access controls may become a bottleneck
Non-obvious example: formatting text messages for logging
Potential for deadlocks
Per-thread overhead
Diminishing returns as threads/CPU ratio increases
Quality-of-service policy under load
Define acceptable performance criteria
Define what happens when threshold(s) are reached
Do nothing different, prioritize requests, queue new requests, reject new
requests, redirect to another server, and so on and so on...
Client profile
Ratio of connected clients to running requests
Can (or must) you tolerate malfunctioning or malicious clients?
AKA: Dispatcher, Notifier
9
The Reactor Pattern
Published in Pattern Languages of Program
Design, 1995, ISBN 0-201-6073-4
Paper by Prof. Douglas C. Schmidt *
Google for: Reactor Pattern
Describes the basic elements of a server
Gives us a vocabulary for this discussion
* http://www.cs.wustl.edu/~schmidt/patterns-ace.html
10
Reactor Pattern UML
11
Reactor Pattern Participants
Handle
A reference to an event source, such as a socket
Event
A state change that can occur on a Handle
Demultiplexer
Reacts to and interprets Events on Handles
Dispatcher
Invokes Handlers for Events on Handles
Handler
Invoked to process an Event on a Handle
12
Dispatcher Flow (Single Threaded)
Register handler(s) for event(s)
...
Do forever
Ask demultiplexer for current events on registered
handles (may block indefinitely)
For each current event*
Invoke handler for event
Clear the event for the handle
*Events should “latch on” until handled
Dispatch stops
while handler
is running
13
Dispatcher Flow (Multi-Threaded)
Do forever
Ask demultiplexer for current events on registered handles
(may block indefinitely)
For each current event
Ask demultiplexer to stop notification of the event
Schedule handler for execution
Clear the event for the handle
<some time later, in some other (handler) thread>
Tell dispatcher the handler has finished running
Tell demultiplexer to resume notification of the event
Synchronize perfectly, don’t clobber or miss anything
This is the
tricky bit
14
A Quick Diversion…
Network connections are streams
If your code assumes structured reads, it’s broken
When reading:
You may only get some (or none) of the data
Structured messages will be fragmented
You must buffer bytes and reconstitute the structure
When writing:
The channel may not accept all you want to send
You must queue output data
Don’t spin in a handler waiting for output to drain
Perhaps it never will
Don’t Forget—The Channels Are Non-Blocking
15
Observations
Handling ready-to-write is just buffer draining
Handlers should enqueue their output
Generic code can drain it
Reads are non-blocking and may fragment
Generic code can fill input queues
Client handlers process “messages”
Handler decides what constitutes a message
Handler threads interact with the Dispatcher
Dispatcher needs to know when handlers finish
Handler may want to disable reads, unregister, etc.
Yep. Inversion of Control—it’s all the rage
It’s mostly generic, common to any server
Make it once, make it solid, make it reusable
16
Yet Another Framework?
17
Assumptions
Our server will be multi-threaded
Use the java.util.concurrent package (Java SE 5)
One select loop (Dispatcher)
Accepting new sockets is done elsewhere
We only care about input handlers
One input Handler per channel
No handler chains
Input and output processing are not directly coupled
Queuing is done by the framework
Input handlers do not enforce queuing policies
18
Let’s Quickly Review
Selector (Demultiplexer)
Holds a Set of keys representing ready channels
This is the “selected set” of keys
Events are added to but never removed from a key in this set
SelectionKey (Handle)
Associates a Selector with a SelectableChannel
Holds set of events of interest for the channel
Events not in the interest set are ignored
Holds a set of triggered events as-of last select() call
Events persist until key is removed from the selected set
May hold an opaque Object reference for your use
Readiness Selection with NIO
19
Reactor Pattern Mapped to NIO
Handle
SelectionKey
Event
SelectionKey.OP_READ, etc
Demultiplexer
Selector
Dispatcher
Selector.select() + iterate Selector.selectedKeys()
Handler
An instance of Runnable or Callable
20
NIO Reactor UML
dispatch()
registerChannel (ch, handler)
unRegisterChannel (ch)
Dispatcher
select()
selectedKeys()
Selector
call()
Concrete Handler
attachment()
SelectionKey
uses
1
*
notifies
1 1
stores
1 *
spawns
Callable
<<interface>>
call()
*
1
notifies
21
Dispatcher Framework Architecture
Client code registers an InputHandler for a channel
Dispatcher wraps handler in an internal adapter class
Adapter instances manage the channel and its queues
When new data arrives, adapter asks InputHandler to
determine if a full message has arrived
If so, the message is dequeued and passed to the handler
The client handler is passed a ChannelFacade interface
through which it may interact with the channel and/or queues
The client InputHandler is decoupled from NIO and
Dispatcher implementation details
The Dispatcher framework is decoupled from any
semantics of the data it processes
Decouple I/O Grunt Work From the Client Handler Logic
22
NIO Reactor as a Framework
dispatch()
registerChannel (ch, handler)
unRegisterChannel (ch)
Dispatcher
select()
selectedKeys()
Selector
call()
Callable
<<interface>>
call()
inputQueue()
outputQueue()
...
HandlerAdapter
attachment()
SelectionKey
uses
1
*
notfies
1 *
spawns
inputQueue()
outputQueue()
getInterestOps()
...
ChannelFacade
<<interface>>
nextMessage (facade)
handleInput (msg, facade)
InputHandler
<<interface>>
uses
uses
*
1
notifies
1 1
stores
23
An Even Better NIO Framework
dispatch()
registerChannel (ch, handler)
unRegisterChannel (ch)
Dispatcher
select()
selectedKeys()
Selector
call()
Callable
<<interface>>
call()
inputQueue()
outputQueue()
...
HandlerAdapter
attachment()
SelectionKey
uses
1
*
notfies
1 *
spawns
inputQueue()
outputQueue()
getInterestOps()
...
ChannelFacade
<<interface>>
nextMessage (facade)
handleInput (msg, facade)
InputHandler
<<interface>>
uses
uses
*
1
notifies
1 1
stores
done()
HandlerFutureTask
uses
done()
FutureTask
JDK Framework Client Code
24
Dispatcher Interface
public interface Dispatcher
{
void dispatch() throws IOException;
ChannelFacade registerChannel (
SelectableChannel channel,
InputHandler handler)throws IOException;
void unregisterChannel (ChannelFacade key);
}
25
Wrangling Threads
java.util.concurrent.Executor
Backport to 1.4 is available
Executor takes a Callable
Callable takes no arguments but has a return type and may
throw exceptions
The framework’s HandlerAdapter class will:
Serve as the Callable that Executor will run
Encapsulate Event state for the worker thread
Coordinate hand-off and rendezvous with the Dispatcher
Contain the input and output queues
Present a Façade through which the InputHandler may
interact with the framework
Don’t Even Think About Writing Your Own Thread Pool
26
Core Dispatcher Loop
public void dispatch()
{
while (true) {
selectorGuardBarrier();
selector.select();
checkStatusChangeQueue (statusChangeQueue);
Set<SelectionKey> keys = selector.selectedKeys();
for (SelectionKey key : keys) {
HandlerAdapter adapter = (HandlerAdapter)key.attachment();
invokeHandler (adapter);
}
keys.clear();
}
}
27
Another Quick Diversion…
While a thread is sleeping in select(), many
Selector and SelectionKey methods can block
indefinitely if invoked from a different thread
Use a guard object to handshake
Selection thread grabs then releases the guard
Other threads wishing to change Selector state
Lock the guard object
Wakeup the selector
Do whatever (eg: key.interestOps())
Release the guard lock
The Selector class is kind of cranky about threads
Reader/Writer Lock Barrier
ReadWriteLock: Improvement over synchronized
Worker threads acquire read locks
Multiple may be granted at once
Selection thread acquires write lock
Must wait for all read locks to be released
Lets multiple handler threads complete in one
Selector wakeup cycle
Handler threads hold their lock for a very short time
Re-pooling threads quickly pool improves efficiency
Need to take greater care managing lock state
Locks must be explicitly released
28
Selection Guard Implementation
import java.util.concurrent.locks.ReadWriteLock;
private final ReadWriteLock selectorGuard =
new ReentrantReadWriteLock();
private void selectorGuardBarrier()
{
selectorGuard.writeLock().lock(); // may wait here for readers
selectorGuard.writeLock().unlock(); // allow readers
}
private void acquireSelectorGuard()
{
selectorGuard.readLock().lock(); // close Selector barrier
selector.wakeup(); // wake Selector if sleeping
}
private void releaseSelectorGuard()
{
selectorGuard.readLock().unlock(); // release my hold on barrier
}
29
From NioDispatcher.java
30
Registering an InputHandler
public ChannelFacade registerChannel (
SelectableChannel channel, InputHandler handler)
{
HandlerAdapter adapter = new HandlerAdapter (handler, this,
bufferFactory);
acquireSelectorGuard();
try {
SelectionKey key = channel.register (selector,
SelectionKey.OP_READ, adapter);
adapter.setKey (key);
return adapter;
} finally {
releaseSelectorGuard();
}
}
class HandlerAdapter implements Callable<HandlerAdapter>,
ChannelFacade
{ . . . }
31
Unregistering an InputHandler
public void unregisterChannel (ChannelFacade token)
{
if ( ! (token instanceof HandlerAdapter)) {
throw new IllegalArgumentException (”bad registration…");
}
HandlerAdapter adapter = (HandlerAdapter) token;
SelectionKey selectionKey = adapter.key();
acquireSelectorGuard();
try {
selectionKey.cancel();
} finally {
releaseSelectorGuard();
}
}
32
While a Worker Thread Is Running
Channel’s interest ops are all disabled
Handler cannot be allowed to re-enable them
Selector would fire and spawn another handler thread
HandlerAdapter class mediates and buffers changes
Other threads must not change key’s interest ops
Always use ChannelFacade, it buffers if needed
Handler could block if it accesses channel or key
Handler is never passed a real channel or key
Event information is buffered in adapter
Interest op changes are buffered for later
33
Invoking a Handler in Another Thread
private void invokeHandler (HandlerAdapter adapter)
{
adapter.prepareToRun (key);
adapter.key().interestOps (0); // stop selection on channel
executor.execute (new HandlerFutureTask (adapter));
}
More about
this shortly
34
Preparing a Handler to Run
class HandlerAdapter implements Callable<HandlerAdapter>,
ChannelFacade
{
private volatile boolean running = false;
private final Object stateChangeLock = new Object();
. . .
void prepareToRun() // package local
{
synchronized (stateChangeLock) {
interestOps = key.interestOps();
readyOps = key.readyOps();
running = true;
}
}
35
Handler Thread Life-Cycle
public HandlerAdapter call() throws IOException
{
try {
drainOutput();
fillInput();
ByteBuffer msg;
while ((msg = clientHandler.nextMessage (this)) != null) {
clientHandler.handleInput (msg, this);
}
} finally {
synchronized (stateChangeLock) { running = false; }
}
return this;
}
36
First: Manage The Queues
private void drainOutput() throws IOException
{
if (((readyOps & SelectionKey.OP_WRITE) != 0)
&& ( ! outputQueue.isEmpty()))
{
outputQueue.drainTo ((ByteChannel) channel);
}
if (outputQueue.isEmpty()) {
disableWriteSelection();
if (shuttingDown) { // set by fillInput on EOS
channel.close();
}
}
}
Similar logic for
fillInput(),
see example code
37
Second: Invoke Client InputHandler
public HandlerAdapter call() throws IOException
{
try {
drainOutput();
fillInput();
ByteBuffer msg;
while ((msg = clientHandler.nextMessage (this)) != null) {
clientHandler.handleInput (msg, this);
}
} finally {
synchronized (stateChangeLock) { running = false; }
}
return this;
}
38
A Handler’s View of the World
interface InputHandler
{
ByteBuffer nextMessage (ChannelFacade channelFacade);
void handleInput (ByteBuffer message, ChannelFacade
channelFacade);
}
interface ChannelFacade
{
InputQueue inputQueue();
OutputQueue outputQueue();
void setHandler (InputHandler handler);
int getInterestOps();
void modifyInterestOps (int opsToSet, int opsToReset);
}
Handler Is Wrapped In FutureTask
Overrides done() method
Called after return from call() in HandlerAdapter
Appends itself to a BlockingQueue
Wakes the selection thread
Worker thread returns to the Executor pool
Selection thread drains the queue each time around
For each queued HandlerAdapter
If the connection has terminated, unregister handler
Otherwise, the interest set is updated with the new value
buffered in the adapter object
39
java.util.concurrent.FutureTask
HandlerFutureTask Class
private class HandlerFutureTask extends FutureTask<HandlerAdapter>
{
private final HandlerAdapter adapter; // Stored by constructor
protected void done()
{
enqueueStatusChange (adapter); // notify selection thread
try {
get(); // Get result or throw deferred exception
} catch (ExecutionException e) {
adapter.die(); // selection thread will drop it
}
}
}
40
Finally: Reap Completed Handlers
public void dispatch()
{
while (true) {
selectorGuardBarrier();
selector.select();
checkStatusChangeQueue();
Set<SelectionKey> keys = selector.selectedKeys();
for (SelectionKey key : keys) {
HandlerAdapter adapter = (HandlerAdapter)key.attachment();
invokeHandler (adapter);
}
keys.clear();
}
}
41
Cleanup Completed Handlers
private void checkStatusChangeQueue()
{
HandlerAdapter adapter;
while ((adapter = statusChangeQueue.poll()) != null) {
if (adapter.isDead())
unregisterChannel (adapter);
else
resumeSelection (adapter);
}
}
private void resumeSelection (HandlerAdapter adapter)
{
SelectionKey key = adapter.key();
if (key.isValid()) key.interestOps (adapter.getInterestOps());
}
42
Running in the
selection thread,
no need for the
guard lock
43
A Few Words About Queues
The framework need only see trivial interfaces
Handlers will need more, and perhaps
specialized, API methods
Use Abstract Factory or Builder pattern to
decouple queue creation (dependency injection)
Output queues (usually) must be thread-safe
A handler for one channel may want to add data to
a different channels queue
Input queues usually don’t need to be
Buffer factories need to be thread-safe
44
Basic Queue Interfaces
interface InputQueue
{
int fillFrom (ReadableByteChannel channel);
boolean isEmpty();
int indexOf (byte b);
ByteBuffer dequeueBytes (int count);
void discardBytes (int count);
}
interface OutputQueue
{
boolean isEmpty();
int drainTo (WriteableByteChannel channel);
boolean enqueue (ByteBuffer byteBuffer);
// enqueue() is not referenced by the framework, but
// it must enable write selection when data is queued
// write selection is disabled when the queue becomes empty
}
Only methods in
blue are used by
the framework
45
Trivial Echo-Back Handler Example
public ByteBuffer nextMessage (ChannelFacade channelFacade)
{
InputQueue inputQueue = channelFacade.inputQueue();
int nlPos = inputQueue.indexOf ((byte) 'n');
if (nlPos == -1) return (null);
return (inputQueue.dequeueBytes (nlPos));
}
public void handleInput (ByteBuffer message, ChannelFacade channelFacade)
{
channelFacade.outputQueue().enqueue (message);
}
Implementation of InputHandler
46
Less Trivial Chat Server Example
public ByteBuffer nextMessage (ChannelFacade channelFacade)
{
InputQueue inputQueue = channelFacade.inputQueue();
int nlPos = inputQueue.indexOf ((byte) 'n');
if (nlPos == -1) return null;
if ((nlPos == 1) && (inputQueue.indexOf ((byte) 'r') == 0)) {
inputQueue.discardBytes (2); // eat CR/NL by itself
return null;
}
return (inputQueue.dequeueBytes (nlPos + 1));
}
public void handleInput (ByteBuffer message, ChannelFacade facade)
{
protocol.handleMessage (channelFacade, message);
}
47
Chat Server Continued
public void handleMessage (ChannelFacade facade, ByteBuffer message)
{
broadcast (users.get (facade), message);
}
private void broadcast (NadaUser sender, ByteBuffer message)
{
synchronized (users) {
for (NadaUser user : users.values()) {
if (user != sender) {
sender.sendTo (user, message);
}
}
}
}
48
Danger! Danger, Will Robinson!
Never let the Dispatcher thread die
Everything will go very quiet
Be sure to catch and handle all possible throwables
This is done by the HandlerFutureTask class
Beware Executor thread pool policies
If “caller runs” is enabled, the Dispatcher thread can
execute the handler code—that’s not good
Put sensible limits on queue sizes
Not too small, especially for output
Don’t statically allocate per-channel, use factories
Don’t over-use direct buffers
49
Tuning
Too big a subject to cover here
Use the knobs and levers in java.util.concurrent
Optimal thread count is dependent on CPU/core count
Backlog vs. caller runs vs. discard, etc.
Use buffer factories to obtain space for queues
Pool direct buffers, if used, they’re expensive to create
Heap buffers probably shouldn’t be pooled
Limit policies may be different for input vs. output
queues
Output limits are typically higher
Input limits can be small, the network layer queues too
50
A Picture Is Worth…
dispatch()
registerChannel (ch, handler)
unRegisterChannel (ch)
Dispatcher
select()
selectedKeys()
Selector
call()
Callable
<<interface>>
call()
inputQueue()
outputQueue()
...
HandlerAdapter
attachment()
SelectionKey
uses
1
*
notfies
1 *
spawns
inputQueue()
outputQueue()
getInterestOps()
...
ChannelFacade
<<interface>>
nextMessage (facade)
handleInput (msg, facade)
InputHandler
<<interface>>
uses
uses
*
1
notifies
1 1
stores
done()
HandlerFutureTask
uses
done()
FutureTask
JDK Framework Client Code
51
Summary
The core of the problem is generic boilerplate
Decouple application-specific code from generic
Keep the critical parts lean, efficient and robust
Lock appropriately, but sparingly
Delegate work to handler threads
Protect the framework from alien handler code
Use good object design and leverage patterns
Keep it simple

Contenu connexe

Similaire à NIO MULTIPLEXER.pdf

A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2ppd1961
 
Programming for Performance
Programming for PerformanceProgramming for Performance
Programming for PerformanceCris Holdorph
 
How to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check TuneupHow to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check TuneupBala Subra
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examplesPeter Lawrey
 
002 hbase clientapi
002 hbase clientapi002 hbase clientapi
002 hbase clientapiScott Miao
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stalMichael Stal
 
Play framework : A Walkthrough
Play framework : A WalkthroughPlay framework : A Walkthrough
Play framework : A Walkthroughmitesh_sharma
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaYardena Meymann
 
DataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep DiveDataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep DiveMorag Hughson
 

Similaire à NIO MULTIPLEXER.pdf (20)

Coding style for good synthesis
Coding style for good synthesisCoding style for good synthesis
Coding style for good synthesis
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Akka (1)
Akka (1)Akka (1)
Akka (1)
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
Programming for Performance
Programming for PerformanceProgramming for Performance
Programming for Performance
 
How to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check TuneupHow to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check Tuneup
 
Curator intro
Curator introCurator intro
Curator intro
 
Introduction to reactive programming
Introduction to reactive programmingIntroduction to reactive programming
Introduction to reactive programming
 
Reactive server with netty
Reactive server with nettyReactive server with netty
Reactive server with netty
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examples
 
002 hbase clientapi
002 hbase clientapi002 hbase clientapi
002 hbase clientapi
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
 
Mule overview
Mule overviewMule overview
Mule overview
 
Hands on: Hystrix
Hands on: HystrixHands on: Hystrix
Hands on: Hystrix
 
Java Programming - 08 java threading
Java Programming - 08 java threadingJava Programming - 08 java threading
Java Programming - 08 java threading
 
Play framework : A Walkthrough
Play framework : A WalkthroughPlay framework : A Walkthrough
Play framework : A Walkthrough
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
 
Java one2013
Java one2013Java one2013
Java one2013
 
DataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep DiveDataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep Dive
 

Dernier

Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Naicy mandal
 
Thane Escorts, (Pooja 09892124323), Thane Call Girls
Thane Escorts, (Pooja 09892124323), Thane Call GirlsThane Escorts, (Pooja 09892124323), Thane Call Girls
Thane Escorts, (Pooja 09892124323), Thane Call GirlsPooja Nehwal
 
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...motiram463
 
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...
NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...
NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...Amil baba
 
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls in Vashi Escorts Services - 7738631006
Call Girls in Vashi Escorts Services - 7738631006Call Girls in Vashi Escorts Services - 7738631006
Call Girls in Vashi Escorts Services - 7738631006Pooja Nehwal
 
9004554577, Get Adorable Call Girls service. Book call girls & escort service...
9004554577, Get Adorable Call Girls service. Book call girls & escort service...9004554577, Get Adorable Call Girls service. Book call girls & escort service...
9004554577, Get Adorable Call Girls service. Book call girls & escort service...Pooja Nehwal
 
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...Pooja Nehwal
 
Call Girls Kothrud Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Kothrud Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Kothrud Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Kothrud Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...
Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...
Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...ranjana rawat
 
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...ranjana rawat
 
Shikrapur Call Girls Most Awaited Fun 6297143586 High Profiles young Beautie...
Shikrapur Call Girls Most Awaited Fun  6297143586 High Profiles young Beautie...Shikrapur Call Girls Most Awaited Fun  6297143586 High Profiles young Beautie...
Shikrapur Call Girls Most Awaited Fun 6297143586 High Profiles young Beautie...tanu pandey
 
Call Girls Chikhali Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Chikhali Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Chikhali Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Chikhali Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Top Rated Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Call Girls in Nagpur High Profile
 
FULL ENJOY - 8264348440 Call Girls in Hauz Khas | Delhi
FULL ENJOY - 8264348440 Call Girls in Hauz Khas | DelhiFULL ENJOY - 8264348440 Call Girls in Hauz Khas | Delhi
FULL ENJOY - 8264348440 Call Girls in Hauz Khas | Delhisoniya singh
 
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Dubai Call Girls O528786472 Call Girls In Dubai Wisteria
Dubai Call Girls O528786472 Call Girls In Dubai WisteriaDubai Call Girls O528786472 Call Girls In Dubai Wisteria
Dubai Call Girls O528786472 Call Girls In Dubai WisteriaUnited Arab Emirates
 

Dernier (20)

Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
Thane Escorts, (Pooja 09892124323), Thane Call Girls
Thane Escorts, (Pooja 09892124323), Thane Call GirlsThane Escorts, (Pooja 09892124323), Thane Call Girls
Thane Escorts, (Pooja 09892124323), Thane Call Girls
 
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
 
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
 
NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...
NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...
NO1 Verified Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi A...
 
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
(SANA) Call Girls Landewadi ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls in Vashi Escorts Services - 7738631006
Call Girls in Vashi Escorts Services - 7738631006Call Girls in Vashi Escorts Services - 7738631006
Call Girls in Vashi Escorts Services - 7738631006
 
9004554577, Get Adorable Call Girls service. Book call girls & escort service...
9004554577, Get Adorable Call Girls service. Book call girls & escort service...9004554577, Get Adorable Call Girls service. Book call girls & escort service...
9004554577, Get Adorable Call Girls service. Book call girls & escort service...
 
Call Girls In Vaishali 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Vaishali 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In Vaishali 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Vaishali 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...
 
Call Girls Kothrud Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Kothrud Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Kothrud Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Kothrud Call Me 7737669865 Budget Friendly No Advance Booking
 
Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...
Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...
Book Paid Lohegaon Call Girls Pune 8250192130Low Budget Full Independent High...
 
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(ANIKA) Wanwadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(MEGHA) Hinjewadi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
 
Shikrapur Call Girls Most Awaited Fun 6297143586 High Profiles young Beautie...
Shikrapur Call Girls Most Awaited Fun  6297143586 High Profiles young Beautie...Shikrapur Call Girls Most Awaited Fun  6297143586 High Profiles young Beautie...
Shikrapur Call Girls Most Awaited Fun 6297143586 High Profiles young Beautie...
 
Call Girls Chikhali Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Chikhali Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Chikhali Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Chikhali Call Me 7737669865 Budget Friendly No Advance Booking
 
Top Rated Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
FULL ENJOY - 8264348440 Call Girls in Hauz Khas | Delhi
FULL ENJOY - 8264348440 Call Girls in Hauz Khas | DelhiFULL ENJOY - 8264348440 Call Girls in Hauz Khas | Delhi
FULL ENJOY - 8264348440 Call Girls in Hauz Khas | Delhi
 
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
 
Dubai Call Girls O528786472 Call Girls In Dubai Wisteria
Dubai Call Girls O528786472 Call Girls In Dubai WisteriaDubai Call Girls O528786472 Call Girls In Dubai Wisteria
Dubai Call Girls O528786472 Call Girls In Dubai Wisteria
 

NIO MULTIPLEXER.pdf

  • 1. 4
  • 2. 5 Building an NIO Server Understanding the problem Defining a solution An NIO implementation
  • 3. 6 What Does a Server Do? A server processes requests: Receive a client request Perform some request-specific task Return a response Multiple requests run concurrently Client requests correspond to connections Sequential requests may be sent on a single socket Requests may contend for resources Must tolerate slow, misbehaving or unresponsive clients
  • 4. 7 Multiplexing Strategies Poll each socket in turn Impractical without non-blocking sockets Inefficient, not fair and scales poorly Thread-per-socket Only practical solution with blocking sockets Stresses the thread scheduler, which limits scalability Thread scheduler does readiness selection—inefficiently Readiness selection Efficient, but requires OS and Java VM support Scales well, especially for many slow clients
  • 5. 8 Other Considerations Multi-threading issues are magnified Access controls may become a bottleneck Non-obvious example: formatting text messages for logging Potential for deadlocks Per-thread overhead Diminishing returns as threads/CPU ratio increases Quality-of-service policy under load Define acceptable performance criteria Define what happens when threshold(s) are reached Do nothing different, prioritize requests, queue new requests, reject new requests, redirect to another server, and so on and so on... Client profile Ratio of connected clients to running requests Can (or must) you tolerate malfunctioning or malicious clients?
  • 6. AKA: Dispatcher, Notifier 9 The Reactor Pattern Published in Pattern Languages of Program Design, 1995, ISBN 0-201-6073-4 Paper by Prof. Douglas C. Schmidt * Google for: Reactor Pattern Describes the basic elements of a server Gives us a vocabulary for this discussion * http://www.cs.wustl.edu/~schmidt/patterns-ace.html
  • 8. 11 Reactor Pattern Participants Handle A reference to an event source, such as a socket Event A state change that can occur on a Handle Demultiplexer Reacts to and interprets Events on Handles Dispatcher Invokes Handlers for Events on Handles Handler Invoked to process an Event on a Handle
  • 9. 12 Dispatcher Flow (Single Threaded) Register handler(s) for event(s) ... Do forever Ask demultiplexer for current events on registered handles (may block indefinitely) For each current event* Invoke handler for event Clear the event for the handle *Events should “latch on” until handled Dispatch stops while handler is running
  • 10. 13 Dispatcher Flow (Multi-Threaded) Do forever Ask demultiplexer for current events on registered handles (may block indefinitely) For each current event Ask demultiplexer to stop notification of the event Schedule handler for execution Clear the event for the handle <some time later, in some other (handler) thread> Tell dispatcher the handler has finished running Tell demultiplexer to resume notification of the event Synchronize perfectly, don’t clobber or miss anything This is the tricky bit
  • 11. 14 A Quick Diversion… Network connections are streams If your code assumes structured reads, it’s broken When reading: You may only get some (or none) of the data Structured messages will be fragmented You must buffer bytes and reconstitute the structure When writing: The channel may not accept all you want to send You must queue output data Don’t spin in a handler waiting for output to drain Perhaps it never will Don’t Forget—The Channels Are Non-Blocking
  • 12. 15 Observations Handling ready-to-write is just buffer draining Handlers should enqueue their output Generic code can drain it Reads are non-blocking and may fragment Generic code can fill input queues Client handlers process “messages” Handler decides what constitutes a message Handler threads interact with the Dispatcher Dispatcher needs to know when handlers finish Handler may want to disable reads, unregister, etc.
  • 13. Yep. Inversion of Control—it’s all the rage It’s mostly generic, common to any server Make it once, make it solid, make it reusable 16 Yet Another Framework?
  • 14. 17 Assumptions Our server will be multi-threaded Use the java.util.concurrent package (Java SE 5) One select loop (Dispatcher) Accepting new sockets is done elsewhere We only care about input handlers One input Handler per channel No handler chains Input and output processing are not directly coupled Queuing is done by the framework Input handlers do not enforce queuing policies
  • 15. 18 Let’s Quickly Review Selector (Demultiplexer) Holds a Set of keys representing ready channels This is the “selected set” of keys Events are added to but never removed from a key in this set SelectionKey (Handle) Associates a Selector with a SelectableChannel Holds set of events of interest for the channel Events not in the interest set are ignored Holds a set of triggered events as-of last select() call Events persist until key is removed from the selected set May hold an opaque Object reference for your use Readiness Selection with NIO
  • 16. 19 Reactor Pattern Mapped to NIO Handle SelectionKey Event SelectionKey.OP_READ, etc Demultiplexer Selector Dispatcher Selector.select() + iterate Selector.selectedKeys() Handler An instance of Runnable or Callable
  • 17. 20 NIO Reactor UML dispatch() registerChannel (ch, handler) unRegisterChannel (ch) Dispatcher select() selectedKeys() Selector call() Concrete Handler attachment() SelectionKey uses 1 * notifies 1 1 stores 1 * spawns Callable <<interface>> call() * 1 notifies
  • 18. 21 Dispatcher Framework Architecture Client code registers an InputHandler for a channel Dispatcher wraps handler in an internal adapter class Adapter instances manage the channel and its queues When new data arrives, adapter asks InputHandler to determine if a full message has arrived If so, the message is dequeued and passed to the handler The client handler is passed a ChannelFacade interface through which it may interact with the channel and/or queues The client InputHandler is decoupled from NIO and Dispatcher implementation details The Dispatcher framework is decoupled from any semantics of the data it processes Decouple I/O Grunt Work From the Client Handler Logic
  • 19. 22 NIO Reactor as a Framework dispatch() registerChannel (ch, handler) unRegisterChannel (ch) Dispatcher select() selectedKeys() Selector call() Callable <<interface>> call() inputQueue() outputQueue() ... HandlerAdapter attachment() SelectionKey uses 1 * notfies 1 * spawns inputQueue() outputQueue() getInterestOps() ... ChannelFacade <<interface>> nextMessage (facade) handleInput (msg, facade) InputHandler <<interface>> uses uses * 1 notifies 1 1 stores
  • 20. 23 An Even Better NIO Framework dispatch() registerChannel (ch, handler) unRegisterChannel (ch) Dispatcher select() selectedKeys() Selector call() Callable <<interface>> call() inputQueue() outputQueue() ... HandlerAdapter attachment() SelectionKey uses 1 * notfies 1 * spawns inputQueue() outputQueue() getInterestOps() ... ChannelFacade <<interface>> nextMessage (facade) handleInput (msg, facade) InputHandler <<interface>> uses uses * 1 notifies 1 1 stores done() HandlerFutureTask uses done() FutureTask JDK Framework Client Code
  • 21. 24 Dispatcher Interface public interface Dispatcher { void dispatch() throws IOException; ChannelFacade registerChannel ( SelectableChannel channel, InputHandler handler)throws IOException; void unregisterChannel (ChannelFacade key); }
  • 22. 25 Wrangling Threads java.util.concurrent.Executor Backport to 1.4 is available Executor takes a Callable Callable takes no arguments but has a return type and may throw exceptions The framework’s HandlerAdapter class will: Serve as the Callable that Executor will run Encapsulate Event state for the worker thread Coordinate hand-off and rendezvous with the Dispatcher Contain the input and output queues Present a Façade through which the InputHandler may interact with the framework Don’t Even Think About Writing Your Own Thread Pool
  • 23. 26 Core Dispatcher Loop public void dispatch() { while (true) { selectorGuardBarrier(); selector.select(); checkStatusChangeQueue (statusChangeQueue); Set<SelectionKey> keys = selector.selectedKeys(); for (SelectionKey key : keys) { HandlerAdapter adapter = (HandlerAdapter)key.attachment(); invokeHandler (adapter); } keys.clear(); } }
  • 24. 27 Another Quick Diversion… While a thread is sleeping in select(), many Selector and SelectionKey methods can block indefinitely if invoked from a different thread Use a guard object to handshake Selection thread grabs then releases the guard Other threads wishing to change Selector state Lock the guard object Wakeup the selector Do whatever (eg: key.interestOps()) Release the guard lock The Selector class is kind of cranky about threads
  • 25. Reader/Writer Lock Barrier ReadWriteLock: Improvement over synchronized Worker threads acquire read locks Multiple may be granted at once Selection thread acquires write lock Must wait for all read locks to be released Lets multiple handler threads complete in one Selector wakeup cycle Handler threads hold their lock for a very short time Re-pooling threads quickly pool improves efficiency Need to take greater care managing lock state Locks must be explicitly released 28
  • 26. Selection Guard Implementation import java.util.concurrent.locks.ReadWriteLock; private final ReadWriteLock selectorGuard = new ReentrantReadWriteLock(); private void selectorGuardBarrier() { selectorGuard.writeLock().lock(); // may wait here for readers selectorGuard.writeLock().unlock(); // allow readers } private void acquireSelectorGuard() { selectorGuard.readLock().lock(); // close Selector barrier selector.wakeup(); // wake Selector if sleeping } private void releaseSelectorGuard() { selectorGuard.readLock().unlock(); // release my hold on barrier } 29 From NioDispatcher.java
  • 27. 30 Registering an InputHandler public ChannelFacade registerChannel ( SelectableChannel channel, InputHandler handler) { HandlerAdapter adapter = new HandlerAdapter (handler, this, bufferFactory); acquireSelectorGuard(); try { SelectionKey key = channel.register (selector, SelectionKey.OP_READ, adapter); adapter.setKey (key); return adapter; } finally { releaseSelectorGuard(); } } class HandlerAdapter implements Callable<HandlerAdapter>, ChannelFacade { . . . }
  • 28. 31 Unregistering an InputHandler public void unregisterChannel (ChannelFacade token) { if ( ! (token instanceof HandlerAdapter)) { throw new IllegalArgumentException (”bad registration…"); } HandlerAdapter adapter = (HandlerAdapter) token; SelectionKey selectionKey = adapter.key(); acquireSelectorGuard(); try { selectionKey.cancel(); } finally { releaseSelectorGuard(); } }
  • 29. 32 While a Worker Thread Is Running Channel’s interest ops are all disabled Handler cannot be allowed to re-enable them Selector would fire and spawn another handler thread HandlerAdapter class mediates and buffers changes Other threads must not change key’s interest ops Always use ChannelFacade, it buffers if needed Handler could block if it accesses channel or key Handler is never passed a real channel or key Event information is buffered in adapter Interest op changes are buffered for later
  • 30. 33 Invoking a Handler in Another Thread private void invokeHandler (HandlerAdapter adapter) { adapter.prepareToRun (key); adapter.key().interestOps (0); // stop selection on channel executor.execute (new HandlerFutureTask (adapter)); } More about this shortly
  • 31. 34 Preparing a Handler to Run class HandlerAdapter implements Callable<HandlerAdapter>, ChannelFacade { private volatile boolean running = false; private final Object stateChangeLock = new Object(); . . . void prepareToRun() // package local { synchronized (stateChangeLock) { interestOps = key.interestOps(); readyOps = key.readyOps(); running = true; } }
  • 32. 35 Handler Thread Life-Cycle public HandlerAdapter call() throws IOException { try { drainOutput(); fillInput(); ByteBuffer msg; while ((msg = clientHandler.nextMessage (this)) != null) { clientHandler.handleInput (msg, this); } } finally { synchronized (stateChangeLock) { running = false; } } return this; }
  • 33. 36 First: Manage The Queues private void drainOutput() throws IOException { if (((readyOps & SelectionKey.OP_WRITE) != 0) && ( ! outputQueue.isEmpty())) { outputQueue.drainTo ((ByteChannel) channel); } if (outputQueue.isEmpty()) { disableWriteSelection(); if (shuttingDown) { // set by fillInput on EOS channel.close(); } } } Similar logic for fillInput(), see example code
  • 34. 37 Second: Invoke Client InputHandler public HandlerAdapter call() throws IOException { try { drainOutput(); fillInput(); ByteBuffer msg; while ((msg = clientHandler.nextMessage (this)) != null) { clientHandler.handleInput (msg, this); } } finally { synchronized (stateChangeLock) { running = false; } } return this; }
  • 35. 38 A Handler’s View of the World interface InputHandler { ByteBuffer nextMessage (ChannelFacade channelFacade); void handleInput (ByteBuffer message, ChannelFacade channelFacade); } interface ChannelFacade { InputQueue inputQueue(); OutputQueue outputQueue(); void setHandler (InputHandler handler); int getInterestOps(); void modifyInterestOps (int opsToSet, int opsToReset); }
  • 36. Handler Is Wrapped In FutureTask Overrides done() method Called after return from call() in HandlerAdapter Appends itself to a BlockingQueue Wakes the selection thread Worker thread returns to the Executor pool Selection thread drains the queue each time around For each queued HandlerAdapter If the connection has terminated, unregister handler Otherwise, the interest set is updated with the new value buffered in the adapter object 39 java.util.concurrent.FutureTask
  • 37. HandlerFutureTask Class private class HandlerFutureTask extends FutureTask<HandlerAdapter> { private final HandlerAdapter adapter; // Stored by constructor protected void done() { enqueueStatusChange (adapter); // notify selection thread try { get(); // Get result or throw deferred exception } catch (ExecutionException e) { adapter.die(); // selection thread will drop it } } } 40
  • 38. Finally: Reap Completed Handlers public void dispatch() { while (true) { selectorGuardBarrier(); selector.select(); checkStatusChangeQueue(); Set<SelectionKey> keys = selector.selectedKeys(); for (SelectionKey key : keys) { HandlerAdapter adapter = (HandlerAdapter)key.attachment(); invokeHandler (adapter); } keys.clear(); } } 41
  • 39. Cleanup Completed Handlers private void checkStatusChangeQueue() { HandlerAdapter adapter; while ((adapter = statusChangeQueue.poll()) != null) { if (adapter.isDead()) unregisterChannel (adapter); else resumeSelection (adapter); } } private void resumeSelection (HandlerAdapter adapter) { SelectionKey key = adapter.key(); if (key.isValid()) key.interestOps (adapter.getInterestOps()); } 42 Running in the selection thread, no need for the guard lock
  • 40. 43 A Few Words About Queues The framework need only see trivial interfaces Handlers will need more, and perhaps specialized, API methods Use Abstract Factory or Builder pattern to decouple queue creation (dependency injection) Output queues (usually) must be thread-safe A handler for one channel may want to add data to a different channels queue Input queues usually don’t need to be Buffer factories need to be thread-safe
  • 41. 44 Basic Queue Interfaces interface InputQueue { int fillFrom (ReadableByteChannel channel); boolean isEmpty(); int indexOf (byte b); ByteBuffer dequeueBytes (int count); void discardBytes (int count); } interface OutputQueue { boolean isEmpty(); int drainTo (WriteableByteChannel channel); boolean enqueue (ByteBuffer byteBuffer); // enqueue() is not referenced by the framework, but // it must enable write selection when data is queued // write selection is disabled when the queue becomes empty } Only methods in blue are used by the framework
  • 42. 45 Trivial Echo-Back Handler Example public ByteBuffer nextMessage (ChannelFacade channelFacade) { InputQueue inputQueue = channelFacade.inputQueue(); int nlPos = inputQueue.indexOf ((byte) 'n'); if (nlPos == -1) return (null); return (inputQueue.dequeueBytes (nlPos)); } public void handleInput (ByteBuffer message, ChannelFacade channelFacade) { channelFacade.outputQueue().enqueue (message); } Implementation of InputHandler
  • 43. 46 Less Trivial Chat Server Example public ByteBuffer nextMessage (ChannelFacade channelFacade) { InputQueue inputQueue = channelFacade.inputQueue(); int nlPos = inputQueue.indexOf ((byte) 'n'); if (nlPos == -1) return null; if ((nlPos == 1) && (inputQueue.indexOf ((byte) 'r') == 0)) { inputQueue.discardBytes (2); // eat CR/NL by itself return null; } return (inputQueue.dequeueBytes (nlPos + 1)); } public void handleInput (ByteBuffer message, ChannelFacade facade) { protocol.handleMessage (channelFacade, message); }
  • 44. 47 Chat Server Continued public void handleMessage (ChannelFacade facade, ByteBuffer message) { broadcast (users.get (facade), message); } private void broadcast (NadaUser sender, ByteBuffer message) { synchronized (users) { for (NadaUser user : users.values()) { if (user != sender) { sender.sendTo (user, message); } } } }
  • 45. 48 Danger! Danger, Will Robinson! Never let the Dispatcher thread die Everything will go very quiet Be sure to catch and handle all possible throwables This is done by the HandlerFutureTask class Beware Executor thread pool policies If “caller runs” is enabled, the Dispatcher thread can execute the handler code—that’s not good Put sensible limits on queue sizes Not too small, especially for output Don’t statically allocate per-channel, use factories Don’t over-use direct buffers
  • 46. 49 Tuning Too big a subject to cover here Use the knobs and levers in java.util.concurrent Optimal thread count is dependent on CPU/core count Backlog vs. caller runs vs. discard, etc. Use buffer factories to obtain space for queues Pool direct buffers, if used, they’re expensive to create Heap buffers probably shouldn’t be pooled Limit policies may be different for input vs. output queues Output limits are typically higher Input limits can be small, the network layer queues too
  • 47. 50 A Picture Is Worth… dispatch() registerChannel (ch, handler) unRegisterChannel (ch) Dispatcher select() selectedKeys() Selector call() Callable <<interface>> call() inputQueue() outputQueue() ... HandlerAdapter attachment() SelectionKey uses 1 * notfies 1 * spawns inputQueue() outputQueue() getInterestOps() ... ChannelFacade <<interface>> nextMessage (facade) handleInput (msg, facade) InputHandler <<interface>> uses uses * 1 notifies 1 1 stores done() HandlerFutureTask uses done() FutureTask JDK Framework Client Code
  • 48. 51 Summary The core of the problem is generic boilerplate Decouple application-specific code from generic Keep the critical parts lean, efficient and robust Lock appropriately, but sparingly Delegate work to handler threads Protect the framework from alien handler code Use good object design and leverage patterns Keep it simple