asynchronous i/o tricks and tips -...
TRANSCRIPT
![Page 1: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/1.jpg)
Async hronous I /O Tr ic k s and T ips
Alan BatemanSun Microsystems Inc.
Jeanfrancois ArcandSun Microsystems Inc.
![Page 2: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/2.jpg)
2
Agenda
> Part 1– Overview of Asynchronous I/O API– Demultiplexing I/O events and thread pools– Usage notes and other topics
> Part 2– Grizzly Architecture– Thread Pool Strategies– Tricks
> Conclusion
![Page 3: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/3.jpg)
3
Concept> Initiate non-blocking I/O operation> Notification when I/O completes
![Page 4: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/4.jpg)
4
Concept> Initiate non-blocking I/O operation> Notification when I/O completes> Compare with non-blocking synchronous I/O
– notification when channel ready for I/O (Selector)– perform non-blocking I/O operation– Reactor vs. Proactor pattern
![Page 5: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/5.jpg)
5
Two forms> Initiate non-blocking I/O operation
– Return j.u.c.Future representing pending result
![Page 6: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/6.jpg)
6
Two forms> Initiate non-blocking I/O operation
– Return j.u.c.Future representing pending result
> Initiate non-blocking I/O operation specifying CompletionHandler
– CompletionHandler invoked when I/O completes
![Page 7: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/7.jpg)
7
Using FutureAsynchronousSocketChannel ch = ...ByteBuffer buf = ...Future<Integer> result = ch.read(buf);
![Page 8: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/8.jpg)
8
Using FutureAsynchronousSocketChannel ch = ...ByteBuffer buf = ...Future<Integer> result = ch.read(buf);// check if I/O operation has completedboolean isDone = result.isDone();
![Page 9: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/9.jpg)
9
Using FutureAsynchronousSocketChannel ch = ...ByteBuffer buf = ...Future<Integer> result = ch.read(buf);// wait for I/O operation to completeint nread = result.get();
![Page 10: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/10.jpg)
10
Using FutureAsynchronousSocketChannel ch = ...ByteBuffer buf = ...Future<Integer> result = ch.read(buf);// wait for I/O operation to complete with timeoutint nread = result.get(5, TimeUnit.SECONDS);
![Page 11: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/11.jpg)
11
CompletionHandler
> V = type of result value> A = type of object attached to I/O operation
● Used to pass context● Typically encapsulates connection context
> completed method invoked if success> failed method invoked if I/O operations fails
interface CompletionHandler<V,A> { void completed(V result, A attachment); void failed(Throwable exc, A attachment);}
![Page 12: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/12.jpg)
12
Using CompletionHandlerclass Connection { … }class Handler implements CompletionHandler<Integer,Connection> { public void completed(Integer result, Connection conn) { int nread = result; // handle result } public void failed(Throwable exc, Connection conn) { // error handling }}
![Page 13: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/13.jpg)
13
Using CompletionHandlerclass Connection { … }class Handler implements CompletionHandler<Integer,Connection> { public void completed(Integer result, Connection conn) { // handle result } public void failed(Throwable exc, Connection conn) { // error handling }}AsynchronousSocketChannel ch = ...ByteBuffer buf = ...Connection conn = ...Handler handler = ...ch.read(buf, conn, handler);
![Page 14: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/14.jpg)
14
AsynchronousSocketChannel> Asynchronous connect> Asynchronous read/write> Asynchronous scatter/gather (multiple buffers)> Read/write operations support timeout
– failed method invoked with timeout exception> Implements NetworkChannel
– for binding, setting socket options, etc.
![Page 15: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/15.jpg)
15
AsynchronousServerSocketChannel> Asynchronous accept
– handler invoked when connection accepted– Result is AsynchronousSocketConnection
> Implements NetworkChannel – for binding, setting socket options, etc.
![Page 16: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/16.jpg)
16
AsynchronousDatagramChannel> Asynchronous read/write (connected)> Asynchronous receive/send (unconnected)
– Result of receive is sender address> Implements NetworkChannel
– for binding, setting socket options, etc.> Implements MulticastChannel
– to join multicast groups
![Page 17: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/17.jpg)
17
> Asynchronous read/write > No global file position/offset
– Each read/write specifies position in file– Access different parts of file concurrently
AsynchronousFileChannel
![Page 18: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/18.jpg)
18
> Asynchronous read/write > No global file position/offset
– Each read/write specifies position in file– Access different parts of file concurrently
AsynchronousFileChannel
Future<Integer> result = channel.write(buf, position);doSomethingElse();int nwrote = result.get();
![Page 19: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/19.jpg)
19
> Open method specifies options● READ, WRITE, TRUNCATE_EXISTING, …● No APPEND● Can specify initial attributes when creating file
> Also supports file locking, size, truncate, ...
AsynchronousFileChannel
![Page 20: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/20.jpg)
20
Groups> What threads invoke the completion handlers?> Network oriented channels bound to a group
– AsynchronousChannelGroup> Group encapsulates thread pool and other
shared resources> Create group with thread pool> Default group for simpler applications> Completion handlers invoked by pooled threads> AsynchronousFileChannel can be created with
its own thread pool (group of one)
![Page 21: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/21.jpg)
21
Creating a group
// fixed thread poolThreadFactory myThreadFactory = …int nthreads = …AsynchronousChannelGroup group = AsynchronousChannelGroup .withFixedThreadPool(nThreads, threadFactory);
![Page 22: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/22.jpg)
22
Creating a group
// custom thread poolExecutorService pool = ...AsynchronousChannelGroup group = AsynchronousChannelGroup .withThreadPool(pool);
![Page 23: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/23.jpg)
23
Creating a group
// custom thread poolExecutorService pool = ...AsynchronousChannelGroup group = AsynchronousChannelGroup .withThreadPool(pool);AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group);
![Page 24: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/24.jpg)
24
Thread pools> Fixed thread pool
– Each thread waits on I/O event– do I/O completion– invoke completion handler– go back to waiting for I/O events
> Cached or custom thread pool– Internal threads wait on I/O events– Submit tasks to thread pool to dispatch to
completion handler
![Page 25: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/25.jpg)
25
Fixed thread pool
Thread
I/O Port
Complete I/O operationInvoke completion handler
Thread Thread
![Page 26: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/26.jpg)
26
Cached and custom thread pools
Thread
Thread
I/O Port
Internal threads
Invoke completion handler
![Page 27: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/27.jpg)
27
More on CompletionHandlers> Should complete in a timely manner
● Avoid blocking indefinitely● Important for fixed thread pools
> May be invoked directly by initiating thread – when I/O operation completes immediately, and– initiating thread is pooled thread– may have several handler frames on thread stack– implementation limit to avoid stack overflow
> Termination due to uncaught error or runtime exception causes pooled thread to exit
![Page 28: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/28.jpg)
28
ByteBuffers> Not safe for use by multiple concurrent threads> When I/O operation is initiated then must take
great care not to access buffer until I/O operation completes
> Memory requirements for buffers depends on number of outstanding I/O operations
> Heap buffers incur additional copy per I/O● As per SocketChannel API, compare
performance ● Copy performance and temporary direct buffer
usage improved
![Page 29: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/29.jpg)
29
Other topics> Queuing not supported on stream connections
● A short-write would corrupt the stream● Handlers not guaranteed to be invoked in order● Read/WritePendingException to catch bugs
> Asynchronous close● Causes all outstanding I/O operations to fail
> Cancellation● Future interface defines cancel method● Forceful cancel allows to close channel
![Page 30: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/30.jpg)
Grizzly Architecture
Grizzly Comet FrameworkGrizzly Comet Framework
HTTP (Sync/Async) HTTP (Sync/Async)
Grizzly NIO FrameworkGrizzly NIO Framework
JDK Kernel NIOJDK Kernel NIO JDK Kernel AIOJDK Kernel AIO
HTTP (Sync/Async) HTTP (Sync/Async)
![Page 31: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/31.jpg)
31
> With AIO, you can configure the thread pool (ExecutorService) used by both the AIO kernel and your application
AsynchronousChannelGroup.withCachedThreadPool (ExecutorService, initialSize)AsynchronousChannelGroup.withThreadPool (ExecutorService)AsynchronousChannelGroup.withFixedThreadPool (nThread, ThreadFactory)
…or use the preconfigured/built in Thread Pool that comes by default…
Which Thread Pool strategy?
![Page 32: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/32.jpg)
32
> An asynchronous channel group associated with a fixed thread pool of size N creates N threads that are waiting for already processed I/O events.
> The kernel dispatches events directly to those threads:
> Thread first completes the I/O operation (like filling a ByteBuffer during a read operation).
> Next invoke the CompletionHandler.completed() that consumes the result.
> When the CompletionHandler terminates normally then the thread returns to the thread pool and wait on a next event.
FixedThreadPool
![Page 33: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/33.jpg)
33
Brrr…It’s freezing here!
![Page 34: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/34.jpg)
34
Tip # 1 - FixedThreadPool!
![Page 35: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/35.jpg)
35
> An asynchronous channel group associated with a cached thread pool submits events to the thread pool that simply invoke the user's completion handler.
> Internal kernel's I/O operations are handled by one or more internal threads that are not visible to the user application.
> That means you have one hidden thread pool that dispatches events to a cached thread pool, which in turn invokes completion handler
> Wait! you just win a prize: a thread's context switch for free!!
CachedThreadPool
![Page 36: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/36.jpg)
36
> Probability of suffering the hang problem compared with the FixedThreadPool is lower.
> Still might grow infinitely…> At least you guarantee that the kernel will be able
to complete its I/O operations (like reading bytes).> Oops…CachedThreadPool must support
unbounded queuing to works properly.> So you can possibly lock all the threads and feed
the queue forever until OOM happens. Of course, nobody will ever do that!
OOM, here we come!
![Page 37: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/37.jpg)
37
Avoid blocking/long lived operations inside a completion handler.
Possibility of OOM if the queue grow indefinitively => monitor the queue
Tip # 2 - CachedThreadPool!
![Page 38: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/38.jpg)
38
> Hybrid of the above configurations:> Cached thread pool that creates threads on demand> N threads that dequeue events and dispatch directly to
CompletionHandler
> N defaults to the number of hardware threads.
> In addition to N threads, there is one additional internal thread that dequeues events and submits tasks to the thread pool to invoke completion handlers.
Kernel/default thread pool.
![Page 39: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/39.jpg)
39
Tip # 3 – Kernel Thread Pool
![Page 40: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/40.jpg)
40
> AIOHandler> Thread Pool are configurable> An application can test which one gives the best
scalability/throughput.
Grizzly’s implementation
![Page 41: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/41.jpg)
41
> Once a connection has been accepted, it is now time to read some bytes:
AsynchronousSocketChannel.read(ByteBuffer b, Attachement a, CompletionHandler<> c);
> Hey Hey You see the problem, right?> Who remember when I was scared by the
SelectionKey.attach()?
AsynchronousSocketChannel.read()
![Page 42: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/42.jpg)
42
> Trouble trouble trouble:– Let’s say you get 10 000 accepted connections– Hence 10 000 ByteBuffer created, and the read
operations get invoked– Now we are waiting, waiting, waiting, waiting for
the remote client(s) to send us bytes (slow clients/network)
– Another 10 000 requests comes in, and we are again creating 10 000 ByteBuffer and invoke the read() operations.
> BOOM!
AsynchronousSocketChannel.read()
![Page 43: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/43.jpg)
43
> Let’s not be too negative here. So far we have tested with more than 20 000 clients without any issues
> But this is still something you have to keep in mind!!> Might want to throttle the read() operation to avoid the
creation of too many ByteBuffer> We strongly recommend the use of a ByteBuffer pool,
specially if you are using Heap ByteBuffer (more on this later).
> Get a ByteBuffer before invoking the read() method, and return it to the pool once the read operations complete.
Tip #4: Use ByteBuffer pool & Throttle
![Page 44: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/44.jpg)
44
> Hey? Blocking?> When invoking the read operation, the returned
value is a Future:> Future readOp = AsynchronousSocketChannel.read(…);> readOp.get(30, TimeUnit.SECONDS);
> The Thread will block until the read operation complete or times out.
> Be careful as you might lock your ThreadPool (specially with FixedThreadPool)
BlockingAsynchronousSocketChannel.read()
![Page 45: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/45.jpg)
45
> AIOContext - InputReader> Use a ByteBuffer Pool> Throttle Read Operations.> Use blocking for short read operations.
Grizzly’s implementation
![Page 46: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/46.jpg)
46
> Now let’s execute some write operations:AsynchronousSocketChannel.write(ByteBuffer b, Attachement a, CompletionHandler<> c);> Wait wait wait. Since we are asynchronous, invoking
write(..) will not block, so the calling thread can continue its execution.
> What happens when the calling thread invokes the write method again and the CompletionHandler has not yet been invoked by the previous write call?
AsynchronousSocketChannel.write()
![Page 47: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/47.jpg)
47
> Aille!! You get a WritePendingException> Hence when invoking the write operation, make sure the
CompletionHandler.complete() has been invoked before initiating another write.
> Better, store ByteBuffer inside a queue and execute write operations only when the previous one has completed (will show code soon)
> As for read, we strongly recommend the use of a ByteBuffer pool for executing write operations. Get one before writing, put it back to the pool after.
AsynchronousSocketChannel.write()
![Page 48: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/48.jpg)
48
> OutputWriter> Use a ByteBuffer Pool> FIFO Queue ByteBuffer> Allow blocking for write operations.
Grizzly’s implementation
![Page 49: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/49.jpg)
49
Damned ByteBuffer!> If you are using Heap ByteBuffer, be aware the
kernel will copy the bytes into a direct ByteBuffer during every write operation:
>Free byte copy > Direct ByteBuffer performance have significantly
improved with JDK 7, so use them all the time.
> Scattered ByteBuffer write operations still offer you free copy, using direct ByteBuffer or not!
![Page 50: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/50.jpg)
50
> Before, the nightmare:
File f = new File();
FileOutputStream fis = new FileOutputStream(f);
FileChannel fc = fis.getChannel();
fc.write(…);…… typing so many lines hurts
AsynchronousFileChannel.open()
![Page 51: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/51.jpg)
51
> Now, the paradise
afc = AsynchronousFileChannel open(Path file, OpenOption... options);
afc.write(…);
AsynchronousFileChannel.open()
![Page 52: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/52.jpg)
52
> NIO.2 brings asynchronous I/O to the masses> You can try it now!> Try it using Project Grizzly, or look at the
implementation to get started.
Conclusion
![Page 53: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/53.jpg)
53
TS-5052: Hacking the File System with JDK™ Release 7, Thursday @ 10:50, Gateway 102-103.
BOF-5087: All Things I/O with JDK™ Release 7, Thursday @ 6:30pm, Gateway 102-103.
BOF-4611: Grizzly 2.0: Monster Reloaded! Wednesday @ 6:45pm, Hall E 134.
Companion Session
![Page 54: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/54.jpg)
54
More Information – NIO.2
Open JDK NIO.2 page: http://openjdk.java.net/projects/nio/NIO.2 docs http://openjdk.java.net/projects/nio/javadoc/NIO.2 mailing list [email protected] Alan’s blog http://blogs.sun.com/alanb/
![Page 55: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/55.jpg)
55
More Information - Grizzly
Project Grizzly: http://grizzly.dev.java.net Join the Grizzly’s buzz [email protected] http://twitter.com/project_grizzlyJeanfrancois’ blog http://weblogs.java.net/blog/jfarcand/ http://twitter.com/jfarcand
![Page 56: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/56.jpg)
56
> FileWriter (http link will be added later, will use an IDE to show the code during the talk)
Grizzly’s implementation
![Page 57: Asynchronous I/O Tricks and Tips - OpenJDKopenjdk.java.net/projects/nio/presentations/TS-4222.pdf · Asynchronous I/O Tricks and Tips Alan Bateman Sun Microsystems Inc. Jeanfrancois](https://reader034.vdocument.in/reader034/viewer/2022051405/5a72f6b77f8b9a93538e5168/html5/thumbnails/57.jpg)
Alan BatemanJeanfrancois ArcandSun Microsystems Inc.