Commit Graph

50 Commits

Author SHA1 Message Date
Trustin Lee
a0e74ff984 Use thread local direct buffer for I/O when the current allocator is unpooled
- Allocating and deallocating a direct buffer for I/O is an expensive
  operation, so we have to at least have a pool of direct buffers if the
  current allocator is not pooled
2014-02-15 11:24:01 -08:00
Trustin Lee
2b84314fdd Add Recycler.Handle.recycle() so that it's possible to recycle an object without an explicit reference to Recycler 2014-02-13 17:24:37 -08:00
Norman Maurer
7041a9238e Allow to cancel non-flushed writes 2014-02-11 19:42:49 +01:00
Trustin Lee
d52dc3b740 Do not warn if failed to mark a void promise as success
- it's always supposed to fail.
2014-02-10 15:03:46 -08:00
Trustin Lee
96b0a949e1 Make most outbound operations cancellable / More robust promise update
- Inspired by #2214 by @normanmaurer
- Call setUncancellable() before performing an outbound operation
- Add safeSetSuccess/Failure() and use them wherever
2014-02-10 14:57:18 -08:00
Norman Maurer
9bee78f91c Provide an optimized AtomicIntegerFieldUpdater, AtomicLongFieldUpdater and AtomicReferenceFieldUpdater 2014-02-06 20:08:45 +01:00
milenkovicm
393f7b2306 ChannelOutboundBuffer returns total pending write size
total pending write size may be used to optimize write batching
2014-01-07 06:52:07 +01:00
Norman Maurer
c0936fc8e7 [#1890] Correctly expand ByteBuffer array in all cases
The problem was that we did not handle the case correctly when doubling the array was not enough. We need to keep doubling until everything fits in.
2013-10-06 15:29:35 +02:00
Bill Gallagher
013ac44d3a [#1832] - Channel writability change notifications sometimes fail to fire 2013-09-30 20:01:39 +02:00
Norman Maurer
b4fa8af079 Cache underlying ByteBuffers and count in ChannelOutboundBuffer.Entry to reduce object creation and so GC pressure
Beside this it also helps to reduce CPU usage as nioBufferCount() is quite expensive when used on CompositeByteBuf which are
nested and contains a lot of components
2013-09-26 20:37:39 +02:00
Norman Maurer
92cec8d2ac Split up the nioBuffers() method to allow for inline. Related to #1812
This move less common method patterns to extra methods and so make the nioBuffers() method with most common pattern (backed by one ByteBuffer) small enough for inlining.
2013-09-05 09:22:13 +02:00
Norman Maurer
25c226a835 Make sure only direct ByteBuffer are passed to the underlying jdk Channel.
This is needed because of otherwise the JDK itself will do an extra ByteBuffer copy with it's own pool implementation. Even worth it will be done
multiple times if the ByteBuffer is always only partial written. With this change the copy is done inside of netty using it's own allocator and
only be done one time in all cases.
2013-09-02 20:17:53 +02:00
Norman Maurer
80d30c3dd8 Small code improvements 2013-08-26 07:52:47 +02:00
Norman Maurer
f76c01c3aa [#1782] Fix IndexOutOfBoundException with direct buffer and gathering writes 2013-08-24 18:16:05 +02:00
Norman Maurer
206dc2a391 [#1772] Make sure ChannelOutboundBuffer.recycle() does also reset unflushed, flushed and tail.
This fix a IndexOutOfBoundsException which as triggered if recycle() did cut down the buffer[] to the initial size.
2013-08-23 09:01:16 +02:00
bgallagher
6a2f340ec0 trim buffers before recycling 2013-08-21 20:28:54 +02:00
bgallagher
fb619f2394 fix writability callback 2013-08-21 16:39:50 +02:00
Norman Maurer
217b8e255c [#1763] Fill ChannelOutboundBuffer.nioBuffers with null on close to allow the content to be GC'ed 2013-08-20 21:15:05 +02:00
bgallagher
a383988cdb add struct to replace parallel arrays consolidate flushed & unflushed buffers 2013-08-18 19:19:57 +02:00
bgallagher
06e250e493 remove unused initialCapacity 2013-08-17 14:10:33 +02:00
Norman Maurer
fc6213604d Make sure ChannelOutboundBuffer not throw NPE during increment/decrement pending writes 2013-08-08 13:42:25 +02:00
Norman Maurer
5b6e762bf4 Allow to have the Channel GC'ed ASAP 2013-08-07 10:32:03 +02:00
Norman Maurer
48a7a21541 Correctly update Channel.isWritable() when the write happens from outside the EventLoop in a fast fashion. Fixes [#1697]
Introduce a new interface called MessageSizeEstimator. This can be specific per Channel (via ChannelConfig). The MessageSizeEstimator will be used to estimate for a message that should be written. The default implementation handles ByteBuf, ByteBufHolder and FileRegion. A user is free to plug-in his/her own implementation for different behaviour.
2013-08-07 10:10:43 +02:00
Trustin Lee
5e4169802f Fix potential reentrance issue in ChannelOutboundBuffer 2013-07-24 11:26:03 +09:00
Trustin Lee
7903697c36 Remove Arrays.fill(..., null)
.. because we can just set each element to null while looping
2013-07-23 15:09:14 +09:00
Trustin Lee
a89b17fa94 Merge ChannelOutboundBuffer.failUnflushed() and recycle() into a single method and make sure it is run later on reentrance
- Previously, failUnflushed() did not run when inFail is true, which made unflushed writes are not released on reentrance.   This has been fixed by this commit.
- Also, AbstractUnsafe.outboundBuffer is set to null as early as possible to remove the chance of any write attempts made after the closure.
2013-07-23 14:34:10 +09:00
Norman Maurer
0f6cc0cc7b [#1630] Fix re-entrance bug in ChannelOutboundBuffer.remove(...) 2013-07-22 10:44:33 +02:00
Norman Maurer
7f86550ef8 [#1619] Fix bug in ChannelOutboundBuffer which could lead to invalid write order when fireChannelWritabilityChanged() write another message 2013-07-20 10:03:53 +02:00
Jeff Pinner
6c9c151d66 minor documentation cleanup 2013-07-20 08:01:14 +02:00
Trustin Lee
19c92ceb59 Separate 'progress total' and 'pending number of bytes'
- Reverted the recent changes in AbstractChannel.calculateMessageSize()
2013-07-19 14:09:08 +09:00
Trustin Lee
762adfcb69 Update HttpStaticFileServer example / Fix bugs related with progress notification
- Fix a bug in DefaultProgressivePromise.tryProgress() where the notification is dropped
 - Fix a bug in AbstractChannel.calculateMessageSize() where FileRegion is not counted
 - HttpStaticFileServer example now uses zero copy file transfer if possible.
2013-07-19 13:21:32 +09:00
Trustin Lee
8178322c5c A little bit of Javadoc for ChannelOutboundBuffer 2013-07-19 01:17:00 +09:00
Trustin Lee
4cd7e62555 Make ChannelOutboundBuffer recycled 2013-07-18 23:26:45 +09:00
Trustin Lee
46ea0d4e7b Implement gathering writes in NioSocketChannel
- Add some support methods in ChannelOutputBuffer
2013-07-18 23:14:39 +09:00
Trustin Lee
bcef796dc7 Rewrite ChannelOutboundBuffer
- Merge MessageList into ChannelOutboundBuffer
- Make ChannelOutboundBuffer a queue-like data structure so that it is nearly impossible to leak a message
- Make ChannelOutboundBuffer public so that AbstractChannel can expose it to its subclasses.
- TODO: Re-enable gathering write in NioSocketChannel
2013-07-18 20:59:14 +09:00
Trustin Lee
568166d554 Cleanup 2013-07-18 10:29:34 +09:00
Trustin Lee
e28594952b Make the cleanup logic in ChannelOutboundBuffer more robust
- Fixes #1601
2013-07-18 10:23:26 +09:00
Trustin Lee
88cbdb50d2 Fail unflushed writes with ClosedChannelException
... instead of cryptic exception message.
2013-07-17 21:25:51 +09:00
Trustin Lee
a8d67b0282 Fix data structure corruption and resource leak in ChannelOutboundBuffer 2013-07-17 21:02:56 +09:00
Norman Maurer
2af7db361b [#1573] Fix NPE which could be triggered one failed promises that trigger a close89 2013-07-12 22:57:03 +02:00
Trustin Lee
b7a7c33fe3 Fix a bug in ChannelOutboundBuffer.addFlush()
It should not advance the tail if no write() was issued so far.
2013-07-11 01:05:26 +09:00
Norman Maurer
b57d9f307f Allow per-write promises and disallow promises on flush()
- write() now accepts a ChannelPromise and returns ChannelFuture as most
  users expected.  It makes the user's life much easier because it is
  now much easier to get notified when a specific message has been
  written.
- flush() does not create a ChannelPromise nor returns ChannelFuture.
  It is now similar to what read() looks like.
2013-07-11 00:49:48 +09:00
Trustin Lee
26e9d70457 Remove channelReadSuspended / Rename messageReceived(Last) to channelRead(Complete)
- Remove channelReadSuspended because it's actually same with messageReceivedLast
- Rename messageReceived to channelRead
- Rename messageReceivedLast to channelReadComplete

We renamed messageReceivedLast to channelReadComplete because it
reflects what it really is for.  Also, we renamed messageReceived to
channelRead for consistency in method names.
2013-07-09 23:58:51 +09:00
Trustin Lee
cbd8817905 Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass.  Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:

- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)

When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message.  Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.

Similarly, for outbound, write(ctx, list) has been split into two:

- write(ctx, msg)
- flush(ctx, promise)

Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.

Please note that write() doesn't have a promise with it.  You must call
flush() to get notified on completion. (or you can use writeAndFlush())

Other changes:

- Because MessageList is completely hidden, codec framework uses
  List<Object> instead of MessageList as an output parameter.
2013-07-09 23:51:48 +09:00
Trustin Lee
956c0f8b90 Better explanation of reentrance issue of ChannelOutboundBuffer 2013-07-02 17:22:56 +09:00
Norman Maurer
5437835832 [#1501] Fix NPE correctly which could accour if ChannelOutboundBuffer.fail(..) triggered another call to ChannelOutboundBuffer.fail(...) 2013-07-02 09:58:41 +02:00
Norman Maurer
8dfbcbda29 [#1501] Fix NPE which could accour if ChannelOutboundBuffer.fail(..) triggered another call to ChannelOutboundBuffer.fail(...) 2013-07-02 09:32:34 +02:00
Trustin Lee
734ec51ac9 Allow specifying 0 as the default number of threads when instantiating an EventLoopGroup
- Fixes #1426
- We already allow a user instantiate an EventLoopGroup with the default number of threads via the default constructor, so I think it's OK although it's not always optimal.
2013-06-27 10:39:39 +09:00
Norman Maurer
cce74efded [#1448] Don't print failure if VoidChannelPromise is used 2013-06-16 14:25:02 +02:00
Trustin Lee
14158070bf Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.

Memory consumption issue:

When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user.  In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.

Memory footprint issue:

The old Netty 4 API decreased overall memory footprint by a great deal
in many cases.  It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read.  Instead, it
created a new buffer for each handler in a pipeline.  This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.

Changes:

All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.

To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:

- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
  - Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
  - Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
  - Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
  ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
  - Similar to ReceiveBufferSizePredictor in Netty 3
  - Some existing configuration properties such as
    DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf

This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-06-10 16:10:39 +09:00