Skip to content

Commit 3083b64

Browse files
committed
Reuse the async connection in the getmore loop
1 parent e23f1f6 commit 3083b64

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

driver-core/src/main/com/mongodb/internal/operation/AsyncCommandBatchCursor.java

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
import static com.mongodb.internal.operation.CommandBatchCursorHelper.MESSAGE_IF_CONCURRENT_OPERATION;
5656
import static com.mongodb.internal.operation.CommandBatchCursorHelper.NEXT_BATCH;
5757
import static com.mongodb.internal.operation.CommandBatchCursorHelper.NO_OP_FIELD_NAME_VALIDATOR;
58-
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getKillCursorsCommand;
5958
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getCommandCursorResult;
59+
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getKillCursorsCommand;
6060
import static com.mongodb.internal.operation.CommandBatchCursorHelper.getMoreCommandDocument;
6161
import static com.mongodb.internal.operation.CommandBatchCursorHelper.translateCommandException;
6262
import static java.util.Collections.emptyList;
@@ -116,8 +116,6 @@ public void next(final SingleResultCallback<List<T>> callback) {
116116
return;
117117
}
118118

119-
120-
121119
resourceManager.execute((AsyncCallbackSupplier<List<T>>) funcCallback -> {
122120
ServerCursor localServerCursor = resourceManager.getServerCursor();
123121
boolean serverCursorIsNull = localServerCursor == null;
@@ -193,39 +191,44 @@ public int getMaxWireVersion() {
193191

194192
private void getMore(final ServerCursor cursor, final SingleResultCallback<List<T>> callback) {
195193
resourceManager.executeWithConnection((connection, wrappedCallback) ->
196-
assertNotNull(connection).commandAsync(namespace.getDatabaseName(),
197-
getMoreCommandDocument(cursor.getId(), connection.getDescription(), namespace,
198-
limit, batchSize, count.get(), maxTimeMS, comment),
199-
NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(),
200-
CommandResultDocumentCodec.create(decoder, NEXT_BATCH),
201-
assertNotNull(resourceManager.getConnectionSource()).getOperationContext(),
202-
(commandResult, t) -> {
203-
if (t != null) {
204-
Throwable translatedException =
205-
t instanceof MongoCommandException
206-
? translateCommandException((MongoCommandException) t, cursor)
207-
: t;
208-
wrappedCallback.onResult(null, translatedException);
209-
return;
210-
}
211-
CommandCursorResult<T> commandCursorResult = toCommandCursorResult(
212-
connection.getDescription().getServerAddress(), NEXT_BATCH, assertNotNull(commandResult));
213-
resourceManager.setServerCursor(commandCursorResult.getServerCursor());
214-
215-
if (!resourceManager.operable()) {
216-
// The cursor was closed
217-
resourceManager.releaseServerAndClientResources(connection);
218-
wrappedCallback.onResult(emptyList(), null);
219-
return;
220-
}
221-
222-
List<T> nextBatch = commandCursorResult.getResults();
223-
if (nextBatch.isEmpty() && commandCursorResult.getServerCursor() != null) {
224-
getMore(commandCursorResult.getServerCursor(), wrappedCallback);
225-
} else {
226-
wrappedCallback.onResult(nextBatch, null);
227-
}
228-
}), callback);
194+
getMoreLoop(assertNotNull(connection), cursor, wrappedCallback), callback);
195+
}
196+
197+
private void getMoreLoop(final AsyncConnection connection, final ServerCursor serverCursor,
198+
final SingleResultCallback<List<T>> callback) {
199+
connection.commandAsync(namespace.getDatabaseName(),
200+
getMoreCommandDocument(serverCursor.getId(), connection.getDescription(), namespace,
201+
limit, batchSize, count.get(), maxTimeMS, comment),
202+
NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(),
203+
CommandResultDocumentCodec.create(decoder, NEXT_BATCH),
204+
assertNotNull(resourceManager.getConnectionSource()).getOperationContext(),
205+
(commandResult, t) -> {
206+
if (t != null) {
207+
Throwable translatedException =
208+
t instanceof MongoCommandException
209+
? translateCommandException((MongoCommandException) t, serverCursor)
210+
: t;
211+
callback.onResult(null, translatedException);
212+
return;
213+
}
214+
CommandCursorResult<T> commandCursorResult = toCommandCursorResult(
215+
connection.getDescription().getServerAddress(), NEXT_BATCH, assertNotNull(commandResult));
216+
resourceManager.setServerCursor(commandCursorResult.getServerCursor());
217+
218+
if (!resourceManager.operable()) {
219+
// The cursor was closed
220+
resourceManager.releaseServerAndClientResources(connection);
221+
callback.onResult(emptyList(), null);
222+
return;
223+
}
224+
225+
List<T> nextBatch = commandCursorResult.getResults();
226+
if (nextBatch.isEmpty() && commandCursorResult.getServerCursor() != null) {
227+
getMoreLoop(connection, commandCursorResult.getServerCursor(), callback);
228+
} else {
229+
callback.onResult(nextBatch, null);
230+
}
231+
});
229232
}
230233

231234
private CommandCursorResult<T> toCommandCursorResult(final ServerAddress serverAddress, final String fieldNameContainingBatch,

driver-core/src/test/unit/com/mongodb/internal/operation/AsyncCommandBatchCursorSpecification.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ class AsyncCommandBatchCursorSpecification extends Specification {
233233
it.last().onResult(response, null)
234234
}
235235

236-
1 * connectionB.commandAsync(*_) >> {
237-
connectionB.getCount() == 1
236+
1 * connectionA.commandAsync(*_) >> {
237+
connectionA.getCount() == 1
238238
connectionSource.getCount() == 1
239239
it.last().onResult(response2, null)
240240
}

0 commit comments

Comments
 (0)