apache mina

29
Building High Performance Scalable TCP/IP Servers with Apache MINA Originally presented at ApacheCon Europe 2006 in Dublin Latest slides and code samples at http://people.apache.org/~proyal Presented by Peter Royal, <[email protected] > 1

Upload: n467889

Post on 15-Oct-2014

1.343 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Apache MINA

Building High Performance Scalable TCP/IP Servers with Apache MINAOriginally presented at ApacheCon Europe 2006 in Dublin

Latest slides and code samples at http://people.apache.org/~proyal

Presented by Peter Royal, <[email protected]>

1

Page 2: Apache MINA

•Introduction to MINA

•Demonstration of what it can do

•Converting blocking-IO code to MINA

•Hopefully inspire you to use it :)

Goals of this presentation

2

Page 3: Apache MINA

•Multipurpose Infrastructure for Networked Applications

•A framework (the F word!) for building networked clients and servers based on non-blocking IO

•http://directory.apache.org/subprojects/mina/

What is MINA

3

Page 4: Apache MINA

•Started out as Netty2 from Trustin Lee

•Joined the Directory Project as the SEDA-based directory needed an asynchronous I/O layer.

Brief history of MINA

4

Page 5: Apache MINA

Architectural overview

IoFilter

IoFilter

IoFilter

IoHandler

IoProcessor

IoAcceptor

Inbound

Outbound

Network

5

Page 6: Apache MINA

•Holder of state for a connection (either client-side or server-side)

•Passed along with every event

•Important Methods

•write

•close

•get/setAttribute

IoSession

6

Page 7: Apache MINA

•Akin to a Servlet

•Endpoint of a filter chain

•Important Methods

•sessionOpened

•messageReceived

•sessionClosed

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoAcceptor

IoHandler

7

Page 8: Apache MINA

•Chain of IoFilter's for each IoSession

•Can setup template chains per IoConnector/IoAcceptor

•Dynamic addition/removal

IoFilterChain

8

Page 9: Apache MINA

•Akin to a ServletFilter

•View/Hack/Slash the event stream

•Important Methods

•sessionOpened

•messageReceived

•filterWrite

•sessionClosed

IoFilters

IoProcessor

IoAcceptor

IoHandler

IoFilter

IoFilter

IoFilter

9

Page 10: Apache MINA

•Server-side entry point.

•Accepts incoming connections and fires events to an IoHandler

•Important Methods

•bind

IoAcceptor

IoProcessor

IoFilter

IoFilter

IoFilter

IoHandler

IoAcceptor

10

Page 11: Apache MINA

•Client-side entry point

•Initiate connections to a remote service, and fires events to an IoHandler

•Important Methods

•connect

IoConnector

11

Page 12: Apache MINA

•Internal component

•Handles reading and writing data to an underlying connection

•Each connection is associated with a single IoProcessor (shared amongst multiple connections)

IoProcessor

IoAcceptor

IoHandler

IoFilter

IoFilter

IoFilter

IoProcessor

12

Page 13: Apache MINA

•Persistent connections from clients

•Serialize java objects across the wire

•Clients connect and are given a unit of work, which in this case, is just an instruction for how long to wait until getting their next instruction.

Our Sample Application

13

Page 14: Apache MINA

•Thread activity via jconsole

•CPU Activity via Activity Monitor

•(or your favorite tool)

Monitoring Performance

14

Page 16: Apache MINA

•JVM limit on number of threads

•The lovely java.lang.OutOfMemoryError: unable to create new native thread

Scalability

16

Page 17: Apache MINA

•Server side first

•(Client to come soon)

Lets convert to MINA!

17

Page 19: Apache MINA

•Java Serialization takes up CPU time

•(a profiler would reveal this)

•OS limit of per-process open files

•(consult the documentation for your OS)

•sysctl / ulimit to view/change on unix-like systems

New Limitations?

19

Page 20: Apache MINA

•Since we will be using MINA's built-in support for building protocols, the ProtocolCodecFilter

•Any socket client can talk to MINA

•We’re using MINA on both sides for simplicity in our examples.

MINA on the client

20

Page 21: Apache MINA

•IoHandler and IoFilter's

•Can re-use filters on both client and server sides.

Client is just like the server

21

Page 24: Apache MINA

•Logging

•Compression

•Blacklist

•SSL

•Requires Java 5

Filters that ship today

24

Page 25: Apache MINA

•Traffic Shaping

•Rate Limiting

•Performance Monitoring

Filters we are working on

25

Page 27: Apache MINA

Performance Tips

•Set the number of IoProcessor’s to be equal to the number of CPU cores.

•Benchmark! Users have found both heap and direct buffers, pooled and not pooled, to be beneficial depending on their workloads.

•For ultra-low latency with small message sizes on a local lan, disable Nagle’s algorithm; the TCP_NODELAY flag.

27