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

Apache MINA: The high-performance protocol construction toolkit

  • Upload
    osi

  • View
    15.459

  • Download
    2

Embed Size (px)

DESCRIPTION

An introduction to Apache MINA, a toolkit for building scalable clients and servers in Java leveraging NIO.

Citation preview

Page 1: Apache MINA: The high-performance protocol construction toolkit

The high-performance protocol construction toolkit.

Apache

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

Atlanta, GA

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

1Wednesday, November 14, 2007

Page 2: Apache MINA: The high-performance protocol construction toolkit

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

2Wednesday, November 14, 2007

Page 3: Apache MINA: The high-performance protocol construction toolkit

MINA hacker since Fall 2005

3Wednesday, November 14, 2007

Page 4: Apache MINA: The high-performance protocol construction toolkit

Radar Networkshttp://radarnetworks.com

4Wednesday, November 14, 2007

Page 6: Apache MINA: The high-performance protocol construction toolkit

Apache Memberhttp://apache.org

6Wednesday, November 14, 2007

Page 7: Apache MINA: The high-performance protocol construction toolkit

What is MINA?

7Wednesday, November 14, 2007

Page 8: Apache MINA: The high-performance protocol construction toolkit

MINA?8Wednesday, November 14, 2007

Page 9: Apache MINA: The high-performance protocol construction toolkit

MultipurposeInfrastructureNetworkedApplications

http://mina.apache.org

9Wednesday, November 14, 2007

Page 10: Apache MINA: The high-performance protocol construction toolkit

Built on Java NIO

10Wednesday, November 14, 2007

Page 11: Apache MINA: The high-performance protocol construction toolkit

Non-Blocking

11Wednesday, November 14, 2007

Page 12: Apache MINA: The high-performance protocol construction toolkit

Asynchronous

12Wednesday, November 14, 2007

Page 13: Apache MINA: The high-performance protocol construction toolkit

Event-Driven(SEDA!)

13Wednesday, November 14, 2007

Page 14: Apache MINA: The high-performance protocol construction toolkit

Multiple Transports(framework is really agnostic)

14Wednesday, November 14, 2007

Page 15: Apache MINA: The high-performance protocol construction toolkit

TCP

15Wednesday, November 14, 2007

Page 16: Apache MINA: The high-performance protocol construction toolkit

UDP(being re-written for 2.0)

16Wednesday, November 14, 2007

Page 17: Apache MINA: The high-performance protocol construction toolkit

In-VM(great for testing)

17Wednesday, November 14, 2007

Page 18: Apache MINA: The high-performance protocol construction toolkit

Apache Portable Runtime (APR)

(under development, alternate TCP/UDP implementation)

18Wednesday, November 14, 2007

Page 19: Apache MINA: The high-performance protocol construction toolkit

(and more..)

19Wednesday, November 14, 2007

Page 20: Apache MINA: The high-performance protocol construction toolkit

Smartly Designed

20Wednesday, November 14, 2007

Page 21: Apache MINA: The high-performance protocol construction toolkit

Follows Inversion of Control Pattern

(plays nicely with PicoContainer, Spring, etc)

21Wednesday, November 14, 2007

Page 22: Apache MINA: The high-performance protocol construction toolkit

Separation of Concerns

22Wednesday, November 14, 2007

Page 23: Apache MINA: The high-performance protocol construction toolkit

Application Logic

Wire Protocol

Rather than this...23Wednesday, November 14, 2007

Page 24: Apache MINA: The high-performance protocol construction toolkit

Concerns are Separated

Wire Protocol

Application Logic

24Wednesday, November 14, 2007

Page 25: Apache MINA: The high-performance protocol construction toolkit

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

• Performance boosts too!

25Wednesday, November 14, 2007

Page 26: Apache MINA: The high-performance protocol construction toolkit

Many Protocols & Users(here are a few...)

26Wednesday, November 14, 2007

Page 27: Apache MINA: The high-performance protocol construction toolkit

Apache Directory

http://directory.apache.org

LDAPv3, NTP, DNS, DHCP and Kerberos

27Wednesday, November 14, 2007

Page 28: Apache MINA: The high-performance protocol construction toolkit

SubEthaSTMPhttp://subethasmtp.tigris.org/

SMTP

28Wednesday, November 14, 2007

Page 29: Apache MINA: The high-performance protocol construction toolkit

Apache (incubating)

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

Advanced Messaging Queuing Protocol (AMQP)

(from Wall Street!)

29Wednesday, November 14, 2007

Page 31: Apache MINA: The high-performance protocol construction toolkit

red5http://www.osflash.org/red5

RTMP(talk to Flash player)

31Wednesday, November 14, 2007

Page 32: Apache MINA: The high-performance protocol construction toolkit

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

32Wednesday, November 14, 2007

Page 33: Apache MINA: The high-performance protocol construction toolkit

Key Concepts

33Wednesday, November 14, 2007

Page 34: Apache MINA: The high-performance protocol construction toolkit

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)

34Wednesday, November 14, 2007

Page 35: Apache MINA: The high-performance protocol construction toolkit

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)

35Wednesday, November 14, 2007

Page 36: Apache MINA: The high-performance protocol construction toolkit

IoService

IoHandler

IoFilter

IoFilter

IoFilter

IoProcessor

Reads

Writes

36Wednesday, November 14, 2007

Page 37: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Two Versions

37Wednesday, November 14, 2007

Page 38: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

IoAcceptor“act as server”

single thread for new connections

38Wednesday, November 14, 2007

Page 39: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

IoConnector“act as client”

39Wednesday, November 14, 2007

Page 40: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Connection instance is an IoSession

40Wednesday, November 14, 2007

Page 41: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Handles reads and writes

41Wednesday, November 14, 2007

Page 42: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Instance count scales with CPU/Load

42Wednesday, November 14, 2007

Page 43: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Session fixed to an Instance(under review for v2)

43Wednesday, November 14, 2007

Page 44: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Chain of IoFilter’s

44Wednesday, November 14, 2007

Page 45: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Per Connection

45Wednesday, November 14, 2007

Page 46: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Reusable

46Wednesday, November 14, 2007

Page 47: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Hot Deployable

47Wednesday, November 14, 2007

Page 48: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Filter all eventsRead / Write / Idle / etc

48Wednesday, November 14, 2007

Page 49: Apache MINA: The high-performance protocol construction toolkit

IoHandler

IoProcessor

IoFilter

IoFilter

IoFilter

IoService

Reads

Writes

Application Logic Lives Here

49Wednesday, November 14, 2007

Page 50: Apache MINA: The high-performance protocol construction toolkit

Large Library of IoFilter’s

50Wednesday, November 14, 2007

Page 51: Apache MINA: The high-performance protocol construction toolkit

Protocol Conversion

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

• Existing codecs

• Text-based

• Java Serialization

51Wednesday, November 14, 2007

Page 52: Apache MINA: The high-performance protocol construction toolkit

Blacklist

52Wednesday, November 14, 2007

Page 53: Apache MINA: The high-performance protocol construction toolkit

Logging(great for debugging!)

53Wednesday, November 14, 2007

Page 54: Apache MINA: The high-performance protocol construction toolkit

SSL / TLS

54Wednesday, November 14, 2007

Page 55: Apache MINA: The high-performance protocol construction toolkit

Compression

55Wednesday, November 14, 2007

Page 56: Apache MINA: The high-performance protocol construction toolkit

Read Throttling(write throttling is in 2.0)

56Wednesday, November 14, 2007

Page 57: Apache MINA: The high-performance protocol construction toolkit

Thread Models(a necessary evil)

57Wednesday, November 14, 2007

Page 58: Apache MINA: The high-performance protocol construction toolkit

“single threaded”

58Wednesday, November 14, 2007

Page 59: Apache MINA: The high-performance protocol construction toolkit

One IoProcessor Thread

59Wednesday, November 14, 2007

Page 60: Apache MINA: The high-performance protocol construction toolkit

Scalability sucks

60Wednesday, November 14, 2007

Page 61: Apache MINA: The high-performance protocol construction toolkit

Add more IoProcessor Threads

(at least one per CPU core)

61Wednesday, November 14, 2007

Page 62: Apache MINA: The high-performance protocol construction toolkit

Lowest latencyScales nicely

(connection latency to be addressed in v2)

62Wednesday, November 14, 2007

Page 63: Apache MINA: The high-performance protocol construction toolkit

“multi threaded”

63Wednesday, November 14, 2007

Page 64: Apache MINA: The high-performance protocol construction toolkit

use ExecutorFilter

64Wednesday, November 14, 2007

Page 65: Apache MINA: The high-performance protocol construction toolkit

IoProcessor threads only do reads & writes

(the intent)

65Wednesday, November 14, 2007

Page 66: Apache MINA: The high-performance protocol construction toolkit

Filters execute on a different thread.

(filter location is key!)

66Wednesday, November 14, 2007

Page 67: Apache MINA: The high-performance protocol construction toolkit

Work for a session is serialized(queued per session)

67Wednesday, November 14, 2007

Page 68: Apache MINA: The high-performance protocol construction toolkit

Work is delegated to an Executor

(generally a java.util.concurrent.ThreadPoolExecutor)

68Wednesday, November 14, 2007

Page 69: Apache MINA: The high-performance protocol construction toolkit

Size thread pool to “active” session count

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

69Wednesday, November 14, 2007

Page 70: Apache MINA: The high-performance protocol construction toolkit

“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!

70Wednesday, November 14, 2007

Page 71: Apache MINA: The high-performance protocol construction toolkit

Example Time!

71Wednesday, November 14, 2007

Page 72: Apache MINA: The high-performance protocol construction toolkit

Everybody loves a Haiku

72Wednesday, November 14, 2007

Page 73: Apache MINA: The high-performance protocol construction toolkit

This is our Haiku validator73Wednesday, November 14, 2007

Page 74: Apache MINA: The high-performance protocol construction toolkit

Simple Protocol

• Connect

• Send 3 lines of text

• Receive HAIKU! or NOT A HAIKU:

• (plus a little reason why not)

74Wednesday, November 14, 2007

Page 75: Apache MINA: The high-performance protocol construction toolkit

ProtocolCodecFilter + TextLineCodecFactory

Bytes to Java String’s. For free!

75Wednesday, November 14, 2007

Page 76: Apache MINA: The high-performance protocol construction toolkit

ToHaikuIoFilterThree String’s to a Haiku

76Wednesday, November 14, 2007

Page 77: Apache MINA: The high-performance protocol construction toolkit

Filter is very testable(mock objects rock!)

77Wednesday, November 14, 2007

Page 78: Apache MINA: The high-performance protocol construction toolkit

IoHandler is very simpleValidate Haiku, send result

78Wednesday, November 14, 2007

Page 79: Apache MINA: The high-performance protocol construction toolkit

Also very testable79Wednesday, November 14, 2007

Page 80: Apache MINA: The high-performance protocol construction toolkit

Very easy to hook it all up80Wednesday, November 14, 2007

Page 81: Apache MINA: The high-performance protocol construction toolkit

Questions?(I’m here all week!)

81Wednesday, November 14, 2007

Page 83: Apache MINA: The high-performance protocol construction toolkit

http://mina.apache.org

83Wednesday, November 14, 2007