Skip to content

Commit

Permalink
Use getMore command in tests, when available
Browse files Browse the repository at this point in the history
In 5.1+, the legacy wire protocol is no longer supported, which includew
OP_GET_MORE

Although this message type will not be invoked in 5.1+ under normal usage,
there are integration tests in driver-core that use it directly,
and those have been modified so that they use the getMore command instead,
when available.

JAVA-4228
  • Loading branch information
jyemin authored and stIncMale committed Aug 3, 2021
1 parent 587d574 commit 9002b1e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
Expand Up @@ -21,7 +21,6 @@ import com.mongodb.MongoException
import com.mongodb.MongoTimeoutException
import com.mongodb.OperationFunctionalSpecification
import com.mongodb.ReadPreference
import com.mongodb.ServerCursor
import com.mongodb.WriteConcern
import com.mongodb.async.FutureResultCallback
import com.mongodb.client.model.CreateCollectionOptions
Expand Down Expand Up @@ -61,6 +60,7 @@ import static com.mongodb.ClusterFixture.serverVersionLessThan
import static com.mongodb.internal.connection.ServerHelper.waitForLastRelease
import static com.mongodb.internal.connection.ServerHelper.waitForRelease
import static com.mongodb.internal.operation.OperationHelper.cursorDocumentToQueryResult
import static com.mongodb.internal.operation.QueryOperationHelper.makeAdditionalGetMoreCall
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo
import static java.util.Arrays.asList
import static java.util.Collections.singletonList
Expand Down Expand Up @@ -357,7 +357,7 @@ class AsyncQueryBatchCursorFunctionalSpecification extends OperationFunctionalSp
while (connection.getCount() > 1) {
Thread.sleep(5)
}
makeAdditionalGetMoreCall(firstBatch.cursor, connection)
makeAdditionalGetMoreCall(getNamespace(), firstBatch.cursor, connection as Connection)

then:
thrown(MongoCursorNotFoundException)
Expand Down Expand Up @@ -447,8 +447,4 @@ class AsyncQueryBatchCursorFunctionalSpecification extends OperationFunctionalSp
futureResultCallback.get();
}
}

private void makeAdditionalGetMoreCall(ServerCursor serverCursor, Connection connection) {
connection.getMore(getNamespace(), serverCursor.getId(), 1, new DocumentCodec())
}
}
Expand Up @@ -52,6 +52,7 @@ import static com.mongodb.ClusterFixture.isSharded
import static com.mongodb.ClusterFixture.serverVersionAtLeast
import static com.mongodb.ClusterFixture.serverVersionLessThan
import static com.mongodb.internal.operation.OperationHelper.cursorDocumentToQueryResult
import static com.mongodb.internal.operation.QueryOperationHelper.makeAdditionalGetMoreCall
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo
import static java.util.Arrays.asList
import static java.util.Collections.singletonList
Expand Down Expand Up @@ -363,7 +364,7 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
cursor = new QueryBatchCursor<Document>(firstBatch, 5, 0, 0, new DocumentCodec(), connectionSource, connection)

when:
makeAdditionalGetMoreCall(firstBatch.cursor, connection)
makeAdditionalGetMoreCall(getNamespace(), firstBatch.cursor, connection)

then:
thrown(MongoCursorNotFoundException)
Expand All @@ -386,7 +387,7 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi

Thread.sleep(1000) //Note: waiting for some time for killCursor operation to be performed on a server.
when:
makeAdditionalGetMoreCall(serverCursor)
makeAdditionalGetMoreCall(getNamespace(), serverCursor, connectionSource)

then:
thrown(MongoCursorNotFoundException)
Expand Down Expand Up @@ -608,17 +609,4 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
connection.release();
}
}

private void makeAdditionalGetMoreCall(ServerCursor serverCursor) {
def connection = connectionSource.getConnection()
try {
makeAdditionalGetMoreCall(serverCursor, connection)
} finally {
connection.release()
}
}

private void makeAdditionalGetMoreCall(ServerCursor serverCursor, Connection connection) {
connection.getMore(getNamespace(), serverCursor.getId(), 1, new DocumentCodec())
}
}
Expand Up @@ -16,7 +16,24 @@

package com.mongodb.internal.operation

import com.mongodb.MongoCommandException
import com.mongodb.MongoCursorNotFoundException
import com.mongodb.MongoNamespace
import com.mongodb.MongoQueryException
import com.mongodb.ReadPreference
import com.mongodb.ServerCursor
import com.mongodb.internal.binding.ConnectionSource
import com.mongodb.internal.connection.Connection
import com.mongodb.internal.connection.NoOpSessionContext
import com.mongodb.internal.validator.NoOpFieldNameValidator
import org.bson.BsonDocument
import org.bson.BsonInt64
import org.bson.BsonString
import org.bson.codecs.BsonDocumentCodec
import org.bson.codecs.DocumentCodec

import static com.mongodb.ClusterFixture.getServerApi
import static com.mongodb.ClusterFixture.serverVersionLessThan

class QueryOperationHelper {
static BsonDocument sanitizeExplainResult(BsonDocument document) {
Expand Down Expand Up @@ -44,4 +61,34 @@ class QueryOperationHelper {
return getKeyPattern(new BsonDocument('queryPlanner', winningPlan.getArray('shards')[0].asDocument()))
}
}

static void makeAdditionalGetMoreCall(MongoNamespace namespace, ServerCursor serverCursor,
ConnectionSource connectionSource) {
def connection = connectionSource.getConnection()
try {
makeAdditionalGetMoreCall(namespace, serverCursor, connection)
} finally {
connection.release()
}
}

static void makeAdditionalGetMoreCall(MongoNamespace namespace, ServerCursor serverCursor, Connection connection) {
if (serverVersionLessThan(3, 6)) {
connection.getMore(namespace, serverCursor.getId(), 1, new DocumentCodec())
} else {
try {
connection.command(namespace.databaseName,
new BsonDocument('getMore', new BsonInt64(serverCursor.getId()))
.append('collection', new BsonString(namespace.getCollectionName())),
new NoOpFieldNameValidator(), ReadPreference.primary(),
new BsonDocumentCodec(), new NoOpSessionContext(), getServerApi())
} catch (MongoCommandException e) {
if (e.getErrorCode() == 43) {
throw new MongoCursorNotFoundException(serverCursor.getId(), serverCursor.getAddress())
} else {
throw new MongoQueryException(e)
}
}
}
}
}

0 comments on commit 9002b1e

Please sign in to comment.