-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Make reference count check atomic with release #876
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import com.mongodb.connection.ClusterType; | ||
| import com.mongodb.connection.ServerDescription; | ||
| import com.mongodb.internal.async.SingleResultCallback; | ||
| import com.mongodb.internal.binding.AbstractReferenceCounted; | ||
| import com.mongodb.internal.binding.AsyncClusterAwareReadWriteBinding; | ||
| import com.mongodb.internal.binding.AsyncConnectionSource; | ||
| import com.mongodb.internal.binding.AsyncReadWriteBinding; | ||
|
|
@@ -36,15 +37,15 @@ | |
| import static com.mongodb.assertions.Assertions.notNull; | ||
| import static com.mongodb.connection.ClusterType.LOAD_BALANCED; | ||
|
|
||
| public class ClientSessionBinding implements AsyncReadWriteBinding { | ||
| public class ClientSessionBinding extends AbstractReferenceCounted implements AsyncReadWriteBinding { | ||
| private final AsyncClusterAwareReadWriteBinding wrapped; | ||
| private final AsyncClientSession session; | ||
| private final boolean ownsSession; | ||
| private final ClientSessionContext sessionContext; | ||
|
|
||
| public ClientSessionBinding(final AsyncClientSession session, final boolean ownsSession, | ||
| final AsyncClusterAwareReadWriteBinding wrapped) { | ||
| this.wrapped = notNull("wrapped", (wrapped)); | ||
| this.wrapped = notNull("wrapped", wrapped).retain(); | ||
| this.ownsSession = ownsSession; | ||
| this.session = notNull("session", session); | ||
| this.sessionContext = new AsyncClientSessionContext(session); | ||
|
|
@@ -113,14 +114,9 @@ private void getPinnedConnectionSource(final boolean isRead, final SingleResultC | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int getCount() { | ||
| return wrapped.getCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public AsyncReadWriteBinding retain() { | ||
| wrapped.retain(); | ||
| super.retain(); | ||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -131,15 +127,15 @@ public void getReadConnectionSource(final int minWireVersion, final ReadPreferen | |
| } | ||
|
|
||
| @Override | ||
| public void release() { | ||
| wrapped.release(); | ||
| closeSessionIfCountIsZero(); | ||
| } | ||
|
|
||
| private void closeSessionIfCountIsZero() { | ||
| if (getCount() == 0 && ownsSession) { | ||
|
Collaborator
Author
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. Stop using |
||
| session.close(); | ||
| public int release() { | ||
| int count = super.release(); | ||
| if (count == 0) { | ||
| wrapped.release(); | ||
| if (ownsSession) { | ||
| session.close(); | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| private boolean isConnectionSourcePinningRequired() { | ||
|
|
@@ -152,6 +148,7 @@ private class SessionBindingAsyncConnectionSource implements AsyncConnectionSour | |
|
|
||
| SessionBindingAsyncConnectionSource(final AsyncConnectionSource wrapped) { | ||
| this.wrapped = wrapped; | ||
| ClientSessionBinding.this.retain(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -214,9 +211,12 @@ public int getCount() { | |
| } | ||
|
|
||
| @Override | ||
| public void release() { | ||
| wrapped.release(); | ||
| closeSessionIfCountIsZero(); | ||
| public int release() { | ||
| int count = wrapped.release(); | ||
| if (count == 0) { | ||
| ClientSessionBinding.this.release(); | ||
| } | ||
| return count; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,6 @@ | |
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.mongodb.assertions.Assertions.isTrue; | ||
| import static com.mongodb.connection.ServerType.SHARD_ROUTER; | ||
| import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback; | ||
|
|
||
|
|
@@ -61,16 +60,16 @@ public DefaultServerConnection retain() { | |
| } | ||
|
|
||
| @Override | ||
| public void release() { | ||
| super.release(); | ||
| if (getCount() == 0) { | ||
| public int release() { | ||
| int count = super.release(); | ||
| if (count == 0) { | ||
| wrapped.close(); | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| @Override | ||
| public ConnectionDescription getDescription() { | ||
| isTrue("open", getCount() > 0); | ||
|
Collaborator
Author
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. Just get rid of superfluous use of |
||
| return wrapped.getDescription(); | ||
| } | ||
|
|
||
|
|
||
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.
It might have been overkill to change this class since it doesn't need to be thread safe, but once I marked
getCountasVisibleForTesting(otherwise=PRIVATE)I didn't want to use it any more in production code. But take a close look because my initial attempts here were buggy and caused unit tests to fail.