Commit Graph

305 Commits

Author SHA1 Message Date
Chris Vest
0272b1cf84
Merge pull request #36 from netty/rc-thread-safety
Allow slices to obtain ownership when parent is closed
2021-03-16 21:27:46 +01:00
Chris Vest
de305bd6b9 Align slice sendability of composite buffers with that of non-composite buffers
This means we no longer need to have tests that are parameterised over non-sliced buffers.
2021-03-16 17:22:41 +01:00
Chris Vest
d40989da78 Add toString implementations to all Drop implementations
This is helpful when debugging.
2021-03-16 17:20:35 +01:00
Chris Vest
0ccb34ca08 Fix failing ByteBufAdaptorTests and increase adaptor compatibility 2021-03-16 12:11:29 +01:00
Chris Vest
1f4234dfb5 Fix checkstyle line length 2021-03-15 16:59:42 +01:00
Chris Vest
253b6cb919 Allow slices to obtain ownership when parent is closed
Motivation:
It is kind of a weird internal and hidden state, that slices were special.
For instance, slices could not be sent, and they could never obtain ownership.
This means buffers from slices behaved differently from allocated buffers.
In doing so, they violated both the principle that magic should stay hidden, and the principle of consistent behaviour.

Modification:
- The special reference-counting drop implementation that was added to support bifurcation, has been renamed to ArcDrop (for atomic reference counting).
- The ArcDrop is then used throughout the MemSegBuffer implementation to account for every instance where multiple buffers reference the same memory, e.g. slices and the like.
- Borrows of a buffer is then the sum of borrows from the buffer itself, and its ArcDrop.
- Ownership is thus tied to both the buffer itself being owned, and the ArcDrop being in an owned state.
- SizeClassedMemoryPool is changed to pool recoverable memory instead of sends, because the sends could come from slices.
- We also take care to keep around a "base" memory segment, so that we don't return memory segment slices to the memory pool (doing so would leak the memory from the parent segment that is not part of the slice).
- CleanerPooledDrop now keeps a weak reference to itself, rather than the buffer, which is more correct anyway, but now also required because we cannot rely on the buffer reference the cleaner was created with.
- The CleanerPooledDrop now takes care to drop the buffer that is actually passed to it, rather than what it was referencing from some earlier point.
- MemoryManager can now disclose the size of recoverable memory, so that SizeClassedMemoryPool can pick the correct size pool to return memory to. It cannot rely on the passed down buffer instance for this, because that buffer might have been a slice.

Result:
It is now possible for slices to obtain ownership when their parent buffer is closed.
2021-03-15 16:42:56 +01:00
Chris Vest
374b0524d8
Merge pull request #35 from netty/more-examples
Port over more Netty examples
2021-03-10 17:18:49 +01:00
Chris Vest
d98e80b9fe Fix build by adding missing Fedora packages
These are required now that we build a complete distribution of Netty,
instead of just a small selection of Netty modules.
2021-03-10 15:06:23 +01:00
Chris Vest
f775e2cf97 Get the last ByteBufAdaptor tests passing 2021-03-09 16:48:33 +01:00
Chris Vest
2dee6f8516 Fix bounds checking bugs when setting bytes
These should not take the read offset into account.
2021-03-09 16:16:38 +01:00
Chris Vest
da70f29ff4 Fix numerous bugs in the ByteBufAdaptor 2021-03-09 12:04:57 +01:00
Chris Vest
45074e4749 Try to speed up BufferTest with more parameter memoization and parallel execution 2021-03-09 12:03:33 +01:00
Chris Vest
56bfa22d4a Align Buffer.get* bounds checks with their documented behaviour
The get* methods bounds checking accesses between 0 and the write offset, and the tests were confirming this behaviour.

This was wrong because it is not symmetric with the set* methods, which bounds check between 0 and the capacity, and does not modify the write offset.

The tests and methods have been updated so the get* methods now bounds check between 0 and the capacity.
2021-03-09 12:02:46 +01:00
Chris Vest
f460c732d0 Make LifecycleTracer thread-safe
The real world may expose the buffers to concurrent accesses even when this is not supposed to be supported.
2021-03-09 11:57:49 +01:00
Chris Vest
2f9aabc915 Create AbstractByteBufTest for ByteBufAdaptor
There are a number of test failures that needs to be looked at, still.
2021-03-06 11:22:25 +01:00
Chris Vest
8d31917bc6 The HttpUpload example *almost* works
There are some weird off-by-one artefacts where ASCII period and newline character bytes switch places.
This needs more investigation.
2021-03-06 11:21:17 +01:00
Chris Vest
602389712c Add working HttpSnoop example 2021-03-06 11:18:14 +01:00
Chris Vest
bf80061335 Also attach traces to ownership errors from compact() and bifurcate() 2021-03-06 11:14:00 +01:00
Chris Vest
264746d583
Merge pull request #34 from netty/tracer
Add LifecycleTracer to help debug lifecycle/ownership issues
2021-03-06 11:11:38 +01:00
Chris Vest
0867f99be1 Add LifecycleTracer to help debug lifecycle/ownership issues
Motivation:
With reference counting it can be difficult to keep track of ownership and references.
When bugs arise in this area, it's good to get help from some tooling.

Modification:
Add a LifecycleTracer which records lifecycle changes.
This information can be attached to any lifecycle/ownership exceptions as suppressed exceptions.
The tracing is off by default, unless assertions are enabled.

Result:
It's now easier to debug reference counting/lifecycle/ownership issues.
2021-03-05 16:32:10 +01:00
Chris Vest
25a84e0602 Include netty-handler module in Docker layer cache 2021-03-01 14:05:13 +01:00
Chris Vest
1d73f655bf
Merge pull request #32 from netty/bytebuf-integration
Make the incubating buffers exposable as ByteBuf
2021-03-01 11:32:16 +01:00
Chris Vest
78f04eeb49 Update docs and examples 2021-03-01 11:21:25 +01:00
Chris Vest
1b65bf9a23 Make the incubating buffers exposable as ByteBuf
Motivation:
This makes it possible to use the new buffer API in Netty as is.

Modification:
Make the MemSegBuffer implementation class implement AsByteBuf and ReferenceCounted.
The produced ByteBuf instance delegates all calls to the underlying Buffer instance as faithfully as possible.
One area where the two deviates, is that it's not possible to create non-retained duplicates and slices with the new buffer API.

Result:
It is now possible to use the new buffer API on both client and server side.
The Echo* examples demonstrate this, and the EchoIT proves it with a test.
The API is used more directly on the client side, since the server-side allocator in Netty does not know how to allocate buffers with the incubating API.
2021-03-01 10:49:09 +01:00
Chris Vest
f14a77961c
Merge pull request #33 from netty/read-comp-byte-cursor
Make ReadableComponent expose ByteCursors
2021-03-01 09:51:49 +01:00
Chris Vest
6175f8f4c5 Make ReadableComponent expose ByteCursors
Motivation:
Another way to process the readable data in a buffer.
This might be faster for composite buffers, since their byte cursors are a bit slower than the MemSegBuffer due to the indirections and more complicated logic.

Modification:
ReadableComponent now have openCursor method.

Note that we *don't* add an openReverseCursor method on ReadableComponent.
The reason is that forEachReadable iterates the components in the forward direction, and it's really confusing to then iterate the bytes in a backwards direction.
Working with both directions at the same time is very error prone.

Result:
It is now possible to process readable components with byte cursors.
2021-02-27 11:04:44 +01:00
Chris Vest
63df380d96
Merge pull request #31 from netty/composite
Move composite buffer methods to Buffer
2021-02-15 18:09:05 +01:00
Chris Vest
7185a59f7a Move composite buffer methods to Buffer
Motivation:
There is no reason for `compose()` to be an instance method on `BufferAllocator` since the allocator implementation should not influence how this method is implemented.

Modification:
Make `compose()` a static method and move it to the `Buffer` interface.
Also move its companion methods `extendComposite()` and `isComposite()` to the `Buffer` interface.

Result:
The composite buffer methods are now in a more sensible place.

Also: decided _against_ making `extendComposite()` and `isComposite()` instance methods, because the subtle behaviours of `extendComposite()` means it would behave quite differently for non-composite buffers.
Also: `isComposite()` is not an instance method because it relates to the hard-coded and concrete `CompositeBuffer` implementation.
2021-02-15 17:46:23 +01:00
Chris Vest
53ba97edbe
Merge pull request #29 from netty/renames
API Renames after feedback
2021-02-12 20:37:11 +01:00
Chris Vest
5434fa88b4 Explode ComponentProcessor into its contained interfaces 2021-02-12 18:28:29 +01:00
Chris Vest
eef97dd1fd Rename Buf to Buffer and Allocator to BufferAllocator 2021-02-12 18:22:07 +01:00
Chris Vest
5f1f0bae38
Merge pull request #27 from netty/send-deref
Introduce Deref abstraction
2021-02-12 17:22:34 +01:00
Chris Vest
492977d9be Introduce Deref abstraction
Motivation:
Sometimes, we wish to operate on both buffers and anything that can produce a buffer.
For instance, when making a composite buffer, we could compose either buffers or sends.

Modification:
Introduce a Deref interface, which is extended by both Rc and Send.
A Deref can be used to acquire an Rc instance, and in doing so will also acquire a reference to the Rc.
That is, dereferencing increases the reference count.
For Rc itself, this just delegates to Rc.acquire, while for Send it delegates to Send.receive, and can only be called once.
The Allocator.compose method has been changed to take Derefs.
This allows us to compose either Bufs or Sends of bufs.
Or a mix.
Extra care and caution has been added to the code, to make sure the reference counts are managed correctly when composing buffers, now that it's a more complicated operation.
A handful of convenience methods for working with Sends have also been added to the Send interface.

Result:
We can now build a composite buffer out of sends of buffers.
2021-02-11 14:26:57 +01:00
Chris Vest
1a741baaf0
Merge pull request #26 from netty/update-examples
Update examples
2021-01-25 13:57:35 +01:00
Chris Vest
2df000ad9a Update examples
Motivation:
The forEachReadable/Writable permit a cleaner FileCopyExample implementation.

Modification:
Simplify FileCopyExample.
Also add examples of various good and bad ways to transfer buffer ownership between threads.
Update the forEachReadable/Writable APIs to let exceptions pass through.

Result:
Cleaner code and more useful forEachReadable/Writable APIs.
2021-01-25 13:23:26 +01:00
Chris Vest
6e24f5d155
Merge pull request #25 from netty/composite-flatten
Avoid nesting composite buffers
2021-01-21 14:13:01 +01:00
Chris Vest
14d55c3e0b Avoid nesting composite buffers
Motivation:
There is no reason that composite buffers should nest when composed.
Instead, when composite buffers are used to compose or extend other composite buffers, we should unwrap them and copy the references to their constituent buffers.

Modification:
Composite buffers now always unwrap and flatten themselves when they participate in composition or extension of other composite buffers.

Result:
Composite buffers are now always guaranteed* to contain a single level of non-composed leaf buffers.

*assuming no other unknown buffer-wrapping buffer type is in the mix.
2021-01-18 16:06:53 +01:00
Chris Vest
202dd54ff2
Merge pull request #24 from netty/buffer-iterate
Add support for iterating underlying buffer components
2021-01-18 13:07:56 +01:00
Chris Vest
1ff8b4bf5a Remove work-around for slice-of-heap-segment-based-buffer JDK bug that got fixed 2021-01-18 12:11:03 +01:00
Chris Vest
e22b57ddcd Address PR review comments 2021-01-18 11:57:35 +01:00
Chris Vest
1c1149395b Add comments about how component count overflow is prevented
Also add TODOs for flattening composite buffers.
2021-01-18 10:56:33 +01:00
Chris Vest
1c10770e82 Implement segregated readable/writable component interfaces and processing 2021-01-15 21:32:24 +01:00
Chris Vest
46ed14577c Add Buf.forEachWritable
Pass iteration indexes through.
2021-01-15 16:11:21 +01:00
Chris Vest
d382017dc6 Add support for iterating underlying buffer components
Motivation:
It's desirable to be able to access the contents of a Buf via an array or a ByteBuffer.
However, we would also like to have a unified API that works for both composite and non-composite buffers.
Even for nested composite buffers.

Modification:
Add a forEachReadable method, which uses internal iteration to process all buffer components.
The internal iteration allows us to hide any nesting of composite buffers.
The consumer in the internal iteration is presented with a Component object, which exposes the contents in various ways.
The data is exposed from the Component via methods, such that anything that is expensive to create, will not have to be paid for unless it is used.
This mechanism also let us avoid any allocation unnecessary allocation; the ByteBuffers and arrays will necessarily have to be allocated, but the consumer may or may not need allocation depending on how it's implemented, and the component objects do not need to be allocated, because the non-composite buffers can directly implement the Component interface.

Result:
It's now possible to access the contents of Buf instances as arrays or ByteBuffers, without having to copy the data.
2021-01-11 16:31:36 +01:00
Chris Vest
8cdcfd53c9
Merge pull request #23 from netty/read-only
Add support for read-only buffers
2021-01-05 17:27:10 +01:00
Chris Vest
617d9ccef1 Add support for read-only buffers
Motivation:
There are cases where you want a buffer to be "constant."
Buffers are inherently mutable, but it's possible to block off write access to the buffer contents.
This doesn't make it completely safe to share the buffer across multiple threads, but it does catch most races that could occur.

Modification:
Add a method to Buf for toggling read-only mode.
When a buffer is read-only, the write accessors throw exceptions when called.
In the MemSegBuf, this is implemented by having separate read and write references to the underlying memory segment.
In a read-only buffer, the write reference is redirected to point to a closed memory segment, thus preventing all writes to the memory backing the buffer.

Result:
It is now possible to make buffers read-only.
Note, however, that it is also possible to toggle a read-only buffer back to writable.
We need that in order for buffer pools to be able to fully reset the state of a buffer, regardless of the buffer implementation.
2021-01-05 16:53:21 +01:00
Chris Vest
f862220c4a Readme file updates 2021-01-05 12:51:17 +01:00
Chris Vest
3b1743c7fd Javadoc corrections for buffer accessor methods
Fixes #22
2021-01-05 12:24:46 +01:00
Chris Vest
d72bdb7fc5
Merge pull request #21 from netty/no-confinement
Remove thread-confinement of Buffers
2021-01-05 12:18:31 +01:00
Chris Vest
41b3c02812 Remove thread-confinement of Buffers
Motivation:
Thread-confinement ends up being too confusing to code for, and also prevents some legitimate use cases.
Additionally, thread-confinement exposed implementation specific behavioural differences of buffers, where we would ideally like all buffers to always behave the same, regardless of implementation.

Modification:
All MemorySegment based buffers now always use shared segments.
For heap-based segments, we avoid the overhead associated with the closing of shared segments, by just not closing them, and instead just leave the whole thing for the GC to deal with.

Result:
Buffers can now always be accessed from multiple different threads at the same time.
2020-12-30 15:42:33 +01:00