-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Updated CompositeByteBuf to be responsible for retaining its ByteBufs #1825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d3361a9
b3d6b91
a6fb3f9
82fdb75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| package com.mongodb.internal.connection; | ||
|
|
||
| import com.mongodb.assertions.Assertions; | ||
| import org.bson.ByteBuf; | ||
|
|
||
| import java.nio.Buffer; | ||
|
|
@@ -50,7 +51,11 @@ class CompositeByteBuf implements ByteBuf { | |
| } | ||
|
|
||
| CompositeByteBuf(final CompositeByteBuf from) { | ||
| components = from.components; | ||
| notNull("from", from); | ||
| components = new ArrayList<>(from.components.size()); | ||
| from.components.forEach(component -> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: I will check the source code for other methods, but in general I believe if possible let's skip the allocation of iterator here and just go over the arraylist using fori |
||
| components.add(new Component(component.buffer.duplicate(), component.offset)) | ||
| ); | ||
| position = from.position(); | ||
| limit = from.limit(); | ||
| } | ||
|
|
@@ -306,6 +311,7 @@ public ByteBuf retain() { | |
| referenceCount.decrementAndGet(); | ||
| throw new IllegalStateException("Attempted to increment the reference count when it is already 0"); | ||
| } | ||
| components.forEach(c -> c.buffer.retain()); | ||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -315,6 +321,11 @@ public void release() { | |
| referenceCount.incrementAndGet(); | ||
| throw new IllegalStateException("Attempted to decrement the reference count below 0"); | ||
| } | ||
| components.forEach(c -> c.buffer.release()); | ||
| if (referenceCount.get() == 0) { | ||
| Assertions.assertTrue(components.stream().noneMatch(c -> c.buffer.getReferenceCount() > 0), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the assertFalse above is statistically imported, maybe we can do the same for assertTrue ? |
||
| "All component buffers should have reference count 0 when CompositeByteBuf is fully released, but some still have references."); | ||
| } | ||
| } | ||
|
|
||
| private Component findComponent(final int index) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rozza can we remove this line and replace it with if then exception ?
According to java se 8 documentation
I think we should not rely whether a client will enable assertions to throw an assertion error here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad, I assumed it's a standard jdk assert but it's coming from our custom mongo Assertions