of 40
8/3/2019 20070712 Grizzly Architecture
1/40
Jeanfrancois ArcandSenior Staff EngineerSun Microsystems
Grizzly 1.5 ArchitectureOverview
8/3/2019 20070712 Grizzly Architecture
2/40
2
JGDAgenda
Introduction
Project Grizzly> What is Project Grizzly
> Classes Diagram and description. HTTP extension
> Aynschronous Request processing> Comet
Performance
Q&A
8/3/2019 20070712 Grizzly Architecture
3/40
3
JGD
What is Project Grizzly
Uses Java technology based NIO primitives and hidesthe complexity of programming with Java technologybased NIO
Easy-to-use high performance APIs for TCP,UDP and SSL communications
Brings non-blocking sockets to the protocolprocessing layer Utilizes high performance buffers and buffermanagement
Choice of several different high performancethread pools
8/3/2019 20070712 Grizzly Architecture
4/404
JGDProject Grizzly and its Extensions
Grizzly Framework[1]Grizzly Framework
HTTP
Adapter
AsyncWeb Phobos JRuby
Jetty Restlet
Comet
Asynchronous Processing
RealTime
Jersey GlassFishv2
Cometd
Synchronous Processing
GlassFishV3
Netbeans6Derby
8/3/2019 20070712 Grizzly Architecture
5/405
JGDFramework Classes Diagram
8/3/2019 20070712 Grizzly Architecture
6/406
JGDController
Main entry point when using the GrizzlyFramework. A Controller is composed of> Handlers
> SelectorHandler> SelectionKeyHandler
> ProtocolChainInstanceHandler> ConnectorHandler
> ProtocolChain> Pipeline.
All of those components are configured byclient using the Grizzly Framework.
8/3/2019 20070712 Grizzly Architecture
7/407
JGDSelectorHandler
A SelectorHandler handles alljava.nio.channels.Selector operations. Oneor more instance of a Selector are handledby SelectorHandler.
The logic for processing of SelectionKeyinterest (OP_ACCEPT,OP_READ, etc.) isusually defined using an instance ofSelectorHandler.
This is where the decision of attaching an
object to SelectionKey.
8/3/2019 20070712 Grizzly Architecture
8/40
8
JGDSelectorHandler (Cont.)
/**
* This method is garantee to always be called before operation
* Selector.select().
*/
public void preSelect(Context controllerCtx) throws IOException;;
/*** Invoke the Selector.select() method.
*/
public Set select(Context controllerCtx) throws IOException;
/**
* This method is garantee to always be called after operation* Selector.select().
*/
public void postSelect(Context controllerCtx) throws IOException;
8/3/2019 20070712 Grizzly Architecture
9/40
9
JGDSelectionKeyHandler
A SelectionKeyHandler is used to handlethe life life cycle of a SelectionKey. Operations likes cancelling, registering or
closing are handled bySelectionKeyHandler.
8/3/2019 20070712 Grizzly Architecture
10/40
10
JGDSelectionKeyHandler (Cont.)
/**
* Expire a SelectionKey.
*/
public void expire(SelectionKey key);
/**
* Cancel a SelectionKey and close its Channel.*/
public void cancel(SelectionKey key);
/**
* Close the SelectionKey's channel input or output, but keep alive
* the SelectionKey.*/
public void close(SelectionKey key);
8/3/2019 20070712 Grizzly Architecture
11/40
11
JGDProtocolChainInstanceHandler
An ProtocolChainInstanceHandler is whereone or several ProtocolChain are createdand cached.
An InstanceHandler decide if a stateless orstatefull ProtocolChain needs to becreated.
8/3/2019 20070712 Grizzly Architecture
12/40
12
JGDInstanceHandler (Cont.)
/*** Return an instance of ProtocolChain.
*/
public ProtocolChain poll();
/**
* Pool an instance of ProtocolChain.*/
public boolean offer(ProtocolChain instance);
8/3/2019 20070712 Grizzly Architecture
13/40
13
JGDPipeline
An interface used as a wrapper aroundany kind of thread pool.
8/3/2019 20070712 Grizzly Architecture
14/40
14
JGDPipeline (Cont.)
/**
* Add an E to be processed by thisPipeline
*/
public void execute(E task) throws PipelineFullException;
/**
* Return a E object available in the pipeline.
*/
public E waitForIoTask() ;
8/3/2019 20070712 Grizzly Architecture
15/40
15
JGDProtocolChain
A ProtocolChain implements the "Chainof Responsibility" pattern (for more info,
take a look at the classic "Gang of Four"
design patterns book). The ProtocolChain API models a
computation as a series of "protocol
filter" that can be combined into a
"protocol chain".
8/3/2019 20070712 Grizzly Architecture
16/40
16
JGDProtocolChain (Cont.)
/**
* Add a ProtocolFilter to the list.ProtocolFilter
* will be invoked in the order they have been added.
*/
public boolean addFilter(ProtocolFilter protocolFilter);
/**
* Remove the ProtocolFilter from this chain.
*/
public boolean removeFilter(ProtocolFilter theFilter);
public void addFilter(int pos, ProtocolFilter protocolFilter);
8/3/2019 20070712 Grizzly Architecture
17/40
17
JGDProtocolFilter
A ProtocolFilter encapsulates a unit ofprocessing work to be performed, whosepurpose is to examine and/or modify thestate of a transaction that is representedby a ProtocolContext.
Individual ProtocolFilter(s) can beassembled into a ProtocolChain.
8/3/2019 20070712 Grizzly Architecture
18/40
18
JGDProtocolFilter (Cont.)
The API for ProtocolFilter consists of atwo methods:
execute(Context)
postExecute(Context)
which are passed a "protocol context"
containing the dynamic state of the
computation
8/3/2019 20070712 Grizzly Architecture
19/40
19
JGDProtocolFilter (Cont.)
/*** Execute a unit of processing work to be performed. ThisProtocolFilter
* may either complete the required processing and return false,
* or delegate remaining processing to the next ProtocolFilter in a
* ProtocolChain containing this ProtocolFilter by returning true.
*/
public boolean execute(Context ctx) throws IOException;
/**
* Execute any cleanup activities, such as releasing resources thatwere
* acquired during the execute() method of this ProtocolFilterinstance.
*/
public boolean postExecute(Context ctx) throws IOException;
8/3/2019 20070712 Grizzly Architecture
20/40
20
JGDConnectorHandler
Non blocking IO for client communication Supports UDP, TCP and TLS Can be combined with the Server NIO side
to implement two ways communication ex: the Ericsson/Sun project Sailfin uses it
for supporting the SIP protocol
8/3/2019 20070712 Grizzly Architecture
21/40
21
JGD
Creating a Controller
8/3/2019 20070712 Grizzly Architecture
22/40
22
JGD
Request Handling
8/3/2019 20070712 Grizzly Architecture
23/40
23
JGD
Worker Thread execution
8/3/2019 20070712 Grizzly Architecture
24/40
24
JGDExample 1 - TCP
By default, the Grizzly Framework bundledefault implementation for TCP and UPDtransport. The TCPSelectorHandler isinstanciated by default.
As an example, supporting the TCPprotocol should only consist of adding theappropriate ProtocolFilter like:
8/3/2019 20070712 Grizzly Architecture
25/40
25
JGDExample 1 TCP (Cont.)
Controller con = new Controller();
con.setInstanceHandler(new DefaultInstanceHandler(){
public ProtocolChain poll() {
ProtocolChain protocolChain = protocolChains.poll();
if (protocolChain == null){protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(new ReadFilter());
protocolChain.addFilter(new MyProtocolFilter());}
return protocolChain;
}
});
con.addSelectorHandler(new TCPSelectorHandler());
8/3/2019 20070712 Grizzly Architecture
26/40
26
JGDExample 2 UDP
Controller con = new Controller();
con.setInstanceHandler(new DefaultInstanceHandler(){
public ProtocolChain poll() {
ProtocolChain protocolChain = protocolChains.poll();
if (protocolChain == null){protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(new ReadFilter());
protocolChain.addFilter(new MyProtocolFilter());
}return protocolChain;
} });
con.addSelectorHandler(new UDPSelectorHandler());
8/3/2019 20070712 Grizzly Architecture
27/40
Sun Microsystems, Inc 27
Architecture HTTP layer
Grizzly Framework[1]Grizzly Framework
HTTP
Adapter
AsyncWeb Phobos JRuby
Jetty Restlet
Comet
Asynchronous Processing
RealTime
SWDPRest
GlassFishv2
Cometd
Synchronous Processing
GlassfishV3
Netbeans6Derby
8/3/2019 20070712 Grizzly Architecture
28/40
28
JGD
Grizzly HTTP layer
Lightweight HTTP 1.0/1.1 based server Extremely easy to embed. Small footprint.
Performance is extremely good Good performance apply to bothSynchronous processing and AsynchronousProcessing
8/3/2019 20070712 Grizzly Architecture
29/40
29
JGD
Example: Grizzly Web Server
SelectorThread
Grizzly Framework
ProtocolParser
HTTPRequests
StaticResourceAdapter
HTTPProtocolFilter
8/3/2019 20070712 Grizzly Architecture
30/40
30
JGD
Grizzly HTTP layer
Easy to embedded. Only have to interactwith one object: SelectorThread
Write an implementation ofcom.sun.grizzly.tcp.Adapterclass.
The Adapter is the glue code between theHTTP layer and the program that embedGrizzly.
In the following example, the defaultStaticResourcesAdapter is used
8/3/2019 20070712 Grizzly Architecture
31/40
31
JGD
Example 1 Static Resource Web
ServerSelectorThread selectorThread = new SelectorThread();
selectorThread.setPort(port);
selectorThread.setAdapter(
new StaticResourcesAdapter());selectorThread.setWebAppRootPath(folder);
selectorThread.initEndpoint();
selectorThread.startEndpoint();
8/3/2019 20070712 Grizzly Architecture
32/40
32
JGD
Asynchronous Request Processing
Allow for parking a request; a type ofcontinuation at the request processing level
The goal is to be able to build, on top of Grizzly, a
scalable ARP implementation that doesn't holdone thread per connection, and achieve as closeras possible the performance of synchronousrequest processing (SRP).
8/3/2019 20070712 Grizzly Architecture
33/40
33
JGD
Example Asynchonous Request
ProcessingSelectorThread selectorThread = new SelectorThread();
selectorThread.setPort(port);
selectorThread.setWebAppRootPath(folder);
selectorThread.setAdapter(new StaticResourcesAdapter());
AsyncHandler asyncHandler = new DefaultAsyncHandler();
asyncHandler.addAsyncFilter(new CometAsyncFilter());
selectorThread.setAsyncHandler(asyncHandler);selectorThread.initEndpoint();
selectorThread.startEndpoint();
8/3/2019 20070712 Grizzly Architecture
34/40
2007 JavaOneSM Conference | Session XXXX | 34
Comparing Project Grizzly to Apache MINA both running AsyncWebProject Grizzly Performance
What is MINA? Apache MINA (Multipurpose Infrastructure for Network
Applications) is a network application framework which helpsusers develop high performance and high scalability networkapplications easily.
What is AsyncWeb? AsyncWeb is a high-throughput, non blocking Java platform
HTTP engine - designed throughout to support asynchronousrequest processing. AsyncWeb is build on top of MINA.
8/3/2019 20070712 Grizzly Architecture
35/40
35
JGDProject Grizzly Performance
Client load generated via faban> faban.sunsource.net
Two modes of measurement
> Throughput> Limited # of clients> No Think Time
> Scalability> Max # of clients with 90% response time metric
> Think time
How did we performance test?
8/3/2019 20070712 Grizzly Architecture
36/40
36
JGDProject Grizzly versus Apache MINA bothrunning AsyncWeb
Source: Internal Benchmark Tests
Total Throughput Scalability0.00%
10.00%
20.00%
30.00%
40.00%
50.00%
60.00%
70.00%
80.00%
90.00%
100.00%
Grizzly
Mina
Higher is better, normalized to Grizzly score
8/3/2019 20070712 Grizzly Architecture
37/40
37
JGDProject Grizzly Performance
Tested against a benchmark designed to: Measure scalability, specifically to measure how many concurrent
clients can be supported with: Average client think time of 8 seconds 90% response time within 3 seconds Error rate < 0.1%
8/3/2019 20070712 Grizzly Architecture
38/40
2007 JavaOneSM Conference | Session XXXX | 38
Project Grizzly HTTP vs other HTTPServers
2 CPU 6 CPU 16 CPU0
250
500750
1000
1250
1500
1750
2000
2250
2500
2750
3000 Traditional I/O
C-Based Server
C-Based Server
Grizzly
%o
fTraditionalI/O
Score
Higher is better
8/3/2019 20070712 Grizzly Architecture
39/40
39
JGDQ&A
8/3/2019 20070712 Grizzly Architecture
40/40
Grizzly 1.5 Architecture
Overview