![Page 1: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/1.jpg)
MINAin Real Life
![Page 2: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/2.jpg)
Schedule• Introduction to MINA• Simple Use Cases• A more complex Use Case• Do's and Don'ts• Summary• Q&A
![Page 3: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/3.jpg)
Introduction
![Page 4: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/4.jpg)
• A framework on top of NIO 1.0– Asynchronous– Nonblocking– EventDriven– TCP, UDP, APR, Serial ...– Extensible through Filters– Comes with a protocol framework
![Page 5: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/5.jpg)
Built for ADS• ADS needed a SEDA based network
framework on top of NIO– Netty1 sound ok, but...– Needed a full rewrite– It became MINA 1.0– And later, a TLP !
![Page 6: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/6.jpg)
Key concepts
}MINA
Performs I/O
Filters incoming andoutgoing messages
Application layer
Session
![Page 7: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/7.jpg)
How it works...
![Page 8: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/8.jpg)
Simple use cases
![Page 9: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/9.jpg)
• A Simple TCP server : EchoServer– Based on TCP– MultiUsers– Should be fast– Returns what the users sent without
modification
• Ok, let's code it !
![Page 10: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/10.jpg)
The echo server
public static void main(String[] args) throws Exception {
SocketAcceptor acceptor = new NioSocketAcceptor();
// Bind
acceptor.setHandler(new EchoProtocolHandler());
acceptor.bind(new InetSocketAddress(PORT));
System.out.println("Listening on port " + PORT);
}
![Page 11: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/11.jpg)
The “Business” part
public class EchoProtocolHandler extends IoHandlerAdapter {
/**
* This is where we handle incoming messages
*/
public void messageReceived(IoSession session, Object message)
throws Exception {
// Write the received data back to remote peer
session.write(((IoBuffer) message).duplicate());
}
}
![Page 12: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/12.jpg)
And that's it !• We have created a SocketAcceptor• Then we associated a handler to it• And accepted incoming connections• Last, we implemented the logic in the
Handler, in the messageReceived() method.
![Page 13: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/13.jpg)
What do we have ?• A multithreaded server• Accepting many parallel clients• Roughly 4 lines of code !• We can extended the server easily
– For instance, adding a logger– Handling more messages– Or adding SSL support
![Page 14: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/14.jpg)
Adding a logging filterpublic static void main(String[] args) throws Exception {
SocketAcceptor acceptor = new NioSocketAcceptor();
// Add a logging filter
acceptor.getFilterChain().addLast( "Logger", new LoggingFilter() );
// Bind
acceptor.setHandler(new EchoProtocolHandler());
acceptor.bind(new InetSocketAddress(PORT));
System.out.println("Listening on port " + PORT);
}
![Page 15: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/15.jpg)
Another simple Use Case• NTP Server
– UDP (port 123)– Fixed Message size– Binary protocol– Stateless
• The code...
![Page 16: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/16.jpg)
A more complexuse case
![Page 17: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/17.jpg)
A more complex Use Case• Apache Directory Server
– TCP and UDP– Simple or Two levels protocols– Binary messages– Multiple handlers– Potentially hundred of thousands
connections– Has to be fast
![Page 18: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/18.jpg)
Handle many protocols• LDAP (TCP)• Kerberos (TCP and UDP)• NTP (UDP)• DHCP (UDP)• DNS (TCP and UDP)• ChangePassword
![Page 19: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/19.jpg)
LDAP protocol• Binary protocol
– Defined using ASN.1– BER encoded
• TCP based• Connected• More than one message type
![Page 20: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/20.jpg)
Constraints• Support LDAP and LDAPS• Session can last forever
– Small memory footprint
• Messages can be quite big– Images
• We can receive more than one message in an incoming buffer
• It should be Client and Server side
![Page 21: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/21.jpg)
Decoding• Problem : it's a 2 level protocol
– TLVs– Ldap
• TLV means Type/Length/Value– Each of those three elements can be
longer than one byte– A Value can contains other TLVs
![Page 22: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/22.jpg)
LDAP messages• 10 different requests
– Bind, Unbind, Abandon, Add, Compare, Delete, Modify, ModifyDN, Search, Extended
• 11 different responses– Bind, SearchResEntry, SearchResDone,
SearchResRef, Add, Compare, Delete, Modify, ModifyDN, Extended, Intermediate
![Page 23: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/23.jpg)
Server Side• The chain will contain the SSL filter,
plus an executor, and the Ldap protocol codec
• We may have expensive requests• We want more than one handler• Each session contains user's datas
![Page 24: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/24.jpg)
The chainSocketAcceptor acceptor = new NioSocketAcceptor( nbThreads );
IoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();chain.addLast( "sslFilter", new SslFilter( sslCtx ) );
chain.addLast( "codec", new ProtocolCodecFilter( getProtocolCodecFactory() ) );
chain.addLast( "executor", new ExecutorFilter( new OrderedThreadPoolExecutor( getNbThreads() ), IoEventType.WRITE ) );
acceptor.setFilterChainBuilder( chain );...
![Page 25: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/25.jpg)
Acceptor configuration...// Disable the disconnection of the clients on unbindacceptor.setCloseOnDeactivation( false ); // Allow the port to be reused even if the socket is in TIME_WAIT stateacceptor.setReuseAddress( true ); // No Nagle's algorithmacceptor.getSessionConfig().setTcpNoDelay( true ); // Inject the protocol handleracceptor.setHandler( getHandler() ); // Bind to the configured addressacceptor.bind();
![Page 26: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/26.jpg)
Handlersclass LdapProtocolHandler extends DemuxingIoHandler{... public void messageReceived( IoSession session, Object message ) { ... // SSL and controls Handling super.messageReceived( session, message ); }...}
public void messageReceived(IoSession session, Object message){ MessageHandler<Object> handler = findReceivedMessageHandler(message.getClass());
if (handler != null) { handler.handleMessage(session, message); } else { throw new UnknownMessageTypeException(...);; } }
![Page 27: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/27.jpg)
Back to basic...
![Page 28: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/28.jpg)
What about XML ?• Tagged language• Size is unknown• Parser are a bit a pain to use at this
point• Seems like XML is the Lingua Franca
those days...– “a language used by people of diverse speech to communicate with one another,
often a basic form of speech with simplified grammar.”
![Page 29: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/29.jpg)
Issues• We have to detect tags• We have to detect text between tags• We have to keep everything
somewhere until we are done with the closing tag
• Java XML decoders don't handle fragmented tags...
![Page 30: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/30.jpg)
An XML stripper server• We want to extract the message in an
XML message, and return it to the user• The message can be big• The decoder is the main concern...• We have to validate the data before
sending it to the handler.
![Page 31: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/31.jpg)
XML server public static void main( String[] args ) throws Exception {
IoHandler xmlStripperProtocolHandler = new XmlStripperProtocolHandler();
SocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setReuseAddress( true );
acceptor.setHandler( xmlStripperProtocolHandler );
// Add the codec filter
acceptor.getFilterChain().addLast( "codec",
new ProtocolCodecFilter( new XmlStripperProtocolCodecFactory() ) );
// Start the listener
acceptor.bind(new InetSocketAddress(IP_PORT_DEFAULT));
}
![Page 32: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/32.jpg)
XML handler
public void messageReceived( IoSession session, Object message )
{
Document document = (Document)message;
// Strip the XML from the <tags>
String result = getChildren( document.getFirstChild() );
session.write( result );
}
![Page 33: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/33.jpg)
XML codec factorypublic class XmlStripperProtocolCodecFactory implements ProtocolCodecFactory
{
public ProtocolEncoder getEncoder( IoSession session )
{
// Create a new encoder.
return new XmlStripperEncoder();
}
public ProtocolDecoder getDecoder( IoSession session )
{
// Create a new decoder.
return new XmlStripperDecoder();
}
}
![Page 34: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/34.jpg)
XML decoderprotected boolean doDecode( IoSession session, IoBuffer ioBuffer,
ProtocolDecoderOutput decoderOutput ) {
...
decoderOutput.write( parserXML( data ) );
...
}
public Object parserXML( IoBuffer xmlBuffer ) {
byte[] data = new byte[xmlBuffer.limit()];
xmlBuffer.get( data );
String xml = new String(data).trim();
Document document = DocumentBuilderFactory.newInstance().
newDocumentBuilder().parse(
new ByteArrayInputStream( xml.getBytes() ) );
return (document);
}
![Page 35: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/35.jpg)
Do's and Don'ts
![Page 36: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/36.jpg)
Do's !!!• Follow the KISS principle• Keep the chain short• Do not use an executor if not needed• Tune the number of IoProcessors• Use only one codec filter• If you have a problem, then your
codec/handler probably sucks...
![Page 37: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/37.jpg)
DON'Ts !!!• Don't use the logging filter. Use Log4j.• Your filter must be threadsafe• Don't expect that you will receive data
in one single block• Don't forget about the negative impact
Nagle's algorithm has on performance• Don't use Direct buffers unless
absolutely needed...
![Page 38: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/38.jpg)
Summary
![Page 39: MINA in Real Life - Apache MINA — Apache MINAmina.apache.org/assets/pdfs/Mina_in_real_life_ASEU-2009.pdf · • A framework on top of NIO 1.0 – Asynchronous ... • ADS needed](https://reader035.vdocument.in/reader035/viewer/2022071223/6085bdceb78a501105271afd/html5/thumbnails/39.jpg)
Q&A