-
Notifications
You must be signed in to change notification settings - Fork 0
Abstract Cursor Resource Manager #404
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
Conversation
Shared usage in sync and async Part of a wider Command Cursor refactoring JAVA-5159
static <T> List<T> convertAndProduceLastId(@Nullable final List<RawBsonDocument> rawDocuments, | ||
final Decoder<T> decoder, | ||
final Consumer<BsonDocument> lastIdConsumer) { | ||
List<T> results = null; | ||
List<T> results = new ArrayList<>(); |
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.
Note: This follows on from the previous AsyncSingleBatchCursor change where next()
no longer returns a null value.
driver-core/src/main/com/mongodb/internal/operation/ChangeStreamBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CommandBatchCursorHelper.java
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncOperationHelper.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
…onnection in tests
} | ||
} | ||
}).get((r, t) -> { | ||
if (resourceManager.getServerCursor() == null) { |
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.
I think we can remove the auto-closing nature of these AsyncBatchCursors now.
I propose adding a hasNext
method which just then returns resourceManager.getServerCursor() != null
for async.
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.
I commented on what I think is the same change in the sync version, where it's easier to understand.
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.
This is different to the sync issue (or I missed which one was commented on). AsyncBatchCursors automatically mark themselves as closed on consumption of all resources. That predates these refactorings.
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.
I think my comment on the sync version is about something different, so ignore my previous comment on this thread.
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.
I think what you're saying is that users of the batch cursors will now have to call close
explicitly in order to release resources (i.e the pinned connection and the connection source). If so, that is a behavioral change that we have to consider carefully, as it effects not just driver internals but users of the driver. Consider what we've allowed in the legacy API:
DBCursor cursor = dbCollection.find();
while (cursor.hasNext()) {
cursor.next();
}
// allow cursor to go out of scope and let the cursor cleaner call DBCursor#close, which in turn calls CommandBatchCursor#close
Currently, if the above loop exits normally (without throwing), all the resources will be released and the cursor cleaner is a no-op. With this proposed change, the releases won't be released until GC kicks in.
While the above is shoddy application coding, we have allowed it and I'm hesitant to change its behavior unless there is a compelling reason.
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.
Ah that makes more sense, let me capture that in a test and apply the fix to release.
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.
nm - there are tests that already cover this scenario for example:
should handle getMore when there are empty results but there is a cursor
So there is no change in behaviour here - because setting the server cursor to null resourceManger.setServerCursor
already releases the resources automatically if the cursor is null.
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.
I agree that the code
if (getServerCursor() == null) {
close();
}
is not needed for releasing resources (and there is no such code in CommandBatchCursor
), but it is needed to self-close the AsyncCommandBatchCursor
. As far as I understand, without this code the AsyncCommandCatchCursor.isClosed
will continue returning false
despite all resources having been released (CommandBatchCursor
does not have this issue because it does not have the isClosed
method).
Thus, I think we should live the code as is and add a comment explaining what's going on.
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.
Even better, instead of doing
if (getServerCursor() == null) {
close();
}
after endOperation
in AsyncCommandBatchCursor
, let's just call close
instead of releaseClientResources
in CursorResourceManager.setServerCursor
(the code change is suggested in #404 (comment)). This will have the same effect, and AsyncCommandBatchCursor
won't need the aforementioned additional code that we don't have in CommandBatchCursor
.
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.
Cannot close in CursorResourceManager.setServerCursor
as auto closing breaks the sync cursor.
The
|
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CommandBatchCursor.java
Show resolved
Hide resolved
} | ||
} | ||
}).get((r, t) -> { | ||
if (resourceManager.getServerCursor() == null) { |
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.
I commented on what I think is the same change in the sync version, where it's easier to understand.
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.
Reviewed CommandBatchCursor
and CursorResourceManager
with regard to how it is used in the synchronous code.
driver-core/src/main/com/mongodb/internal/operation/CommandBatchCursor.java
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Outdated
Show resolved
Hide resolved
…urceManager.java Co-authored-by: Valentin Kovalenko <[email protected]>
…urceManager.java Co-authored-by: Valentin Kovalenko <[email protected]>
…urceManager.java Co-authored-by: Valentin Kovalenko <[email protected]>
…urceManager.java Co-authored-by: Valentin Kovalenko <[email protected]>
…urceManager.java Co-authored-by: Valentin Kovalenko <[email protected]>
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.
Partially reviewed AsyncCommandBatchCursor
.
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java
Outdated
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Show resolved
Hide resolved
driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java
Show resolved
Hide resolved
…ndBatchCursor.java Co-authored-by: Valentin Kovalenko <[email protected]>
…urceManager.java Co-authored-by: Valentin Kovalenko <[email protected]>
…rsorResourceManager.java" This reverts commit 238cd21.
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.
There is an optional suggestion, otherwise approved.
Co-authored-by: Valentin Kovalenko <[email protected]>
Two commits:
You should be able to view both implementations side by side.
Note, the aim isn't to make the API's / behaviour exactly same - rather its to bring them to a point where they are similar enough, that it isn't such a mental leap to work on them.