Introduction to MINA - Apache MINA — Apache...

83
The high-performance protocol construction toolkit. Apache Peter Royal <[email protected] > Originally presented at ApacheCon Europe 2007 in Amsterdam (if you can’t read this, move closer!)

Transcript of Introduction to MINA - Apache MINA — Apache...

Page 1: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

The high-performance protocol construction toolkit.

Apache

Peter Royal <[email protected]>Originally presented at ApacheCon Europe 2007

in Amsterdam

(if you can’t read this, move closer!)

Page 2: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Hi, I’m Peterhttp://fotap.org/~osi

Page 3: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

MINA hacker since Fall 2005

Page 4: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Radar Networkshttp://radarnetworks.com

Page 6: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Apache Memberhttp://apache.org

Page 7: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

What is MINA?

Page 8: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

MINA?

Page 9: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

MultipurposeInfrastructureNetworkedApplications

http://mina.apache.org

Page 10: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Built on Java NIO

Page 11: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Non-Blocking

Page 12: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Asynchronous

Page 13: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Event-Driven(SEDA!)

Page 14: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Multiple Transports(framework is really agnostic)

Page 15: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

TCP

Page 16: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

UDP(being re-written for 2.0)

Page 17: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

In-VM(great for testing)

Page 18: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

RS-232 (under development)

Page 19: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Smartly Designed

Page 20: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Follows Inversion of Control Pattern

(plays nicely with PicoContainer, Spring, etc)

Page 21: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Separation of Concerns

Page 22: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Application Logic

Wire Protocol

Rather than this...

Page 23: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Concerns are Separated

Wire Protocol

Application Logic

Page 24: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Stable and Production-Ready

• v1.0 released Fall 2006

• v1.1 released April 2007

• Same API as v1.0 but uses Java 5 Concurrency primitives

• v2.0 this year

• API simplification based on lessons learned

Page 25: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Many Users

Page 26: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Apache Directory

http://directory.apache.org

LDAPv3, NTP, DNS, DHCP and Kerberos

Page 27: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

AsyncWebhttp://asyncweb.safehaus.org

(joining MINA @ Apache very soon though!)

HTTP/HTTPS

Page 28: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Apache (incubating)

Qpidhttp://cwiki.apache.org/qpid/

Advanced Messaging Queuing Protocol (AMQP)

(from Wall Street!)

Page 29: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

QuickFIX/Jhttp://www.quickfixj.org/

Financial Information eXchange (FIX)

Page 31: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

red5http://www.osflash.org/red5

RTMP(talk to Flash player)

Page 32: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

...and more!(maybe you, next time!)

Page 33: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Key Concepts

Page 34: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

ByteBuffer

• Core NIO construct

• MINA version that wraps and provides additional convenience methods

• auto-expanding, string encoding

• MINA gives control...

• allocate from the Heap or Stack

• optional Pooling

• (in v2, will be non-pooled and heap-only, as it provides the best performance)

Page 35: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Future

• Represents a function call that completes asynchronously

• Provides blocking functions to retrieve the result

• MINA allows callbacks to be invoked upon completion, so invoking thread can “fire and forget”

• (unlike the Java 5 Future)

Page 36: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoService

IoHandler

IoFilter

IoFilter

IoFilter

IoProcessor

Reads

Writes

Page 37: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Two Versions

Page 38: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

IoAcceptor“act as server”

single thread for new connections

Page 39: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

IoConnector“act as client”

Page 40: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Connection instance is an IoSession

Page 41: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Handles reads and writes

Page 42: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Instance count scales with CPU/Load

Page 43: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Session fixed to an Instance(under review for v2)

Page 44: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Chain of IoFilter’s

Page 45: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Per Connection

Page 46: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Reusable

Page 47: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Hot Deployable

Page 48: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Filter all eventsRead / Write / Idle / etc

Page 49: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Application Logic Lives Here

Page 50: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Large Library of IoFilter’s

Page 51: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Protocol Conversion

• Framework to plug in your own codecs to handle conversion to/from a ByteBuffer

• Existing codecs

• Text-based

• Java Serialization

Page 52: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Blacklist

Page 53: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Logging(great for debugging!)

Page 54: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

SSL / TLS

Page 55: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Compression

Page 56: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Read Throttling(we’re working on a general solution for writes)

Page 57: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Thread Models(a necessary evil)

Page 58: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

“single threaded”

Page 59: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

One IoProcessor Thread

Page 60: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Scalability sucks

Page 61: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Add more IoProcessor Threads

(at least one per CPU core)

Page 62: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Lowest latencyScales nicely

(connection latency to be addressed in v2)

Page 63: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

“multi threaded”

Page 64: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

use ExecutorFilter

Page 65: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoProcessor threads only do reads & writes

(the intent)

Page 66: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Filters execute on a different thread.

(filter location is key!)

Page 67: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Work for a session is serialized(queued per session)

Page 68: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Work is delegated to an Executor

(generally a java.util.concurrent.ThreadPoolExecutor)

Page 69: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Size thread pool to “active” session count

(too much in queue == OOM!Use the Read Throttle filter)

Page 70: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

“recommended application pattern”

• use ExecutorFilter as first in chain

• unless you need really low latency

• use ProtocolCodecFilter

• convert the wire protocol into a Java representation

• put application logic into an IoHandler

• store state in the IoSession

• minimum of Java 5

• java.util.concurrent rocks!

Page 71: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Example Time!

Page 72: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Everybody loves a Haiku

Page 73: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

This is our Haiku validator

Page 74: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Simple Protocol

• Connect

• Send 3 lines of text

• Receive HAIKU! or NOT A HAIKU:

• (plus a little reason why not)

Page 75: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

ProtocolCodecFilter + TextLineCodecFactory

Bytes to Java String’s. For free!

Page 76: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

ToHaikuIoFilterThree String’s to a Haiku

Page 77: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Filter is very testable(mock objects rock!)

Page 78: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

IoHandler is very simpleValidate Haiku, send result

Page 79: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Also very testable

Page 80: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

Very easy to hook it all up

Page 83: Introduction to MINA - Apache MINA — Apache MINAmina.apache.org/mina-project/resources/ACEU2007.pdf · The high-performance protocol construction toolkit. Apache Peter Royal

http://mina.apache.org