Skip to content

Commit d25db9c

Browse files
authored
Ensure getMore comments only sent if server >= 4.4.0 (#899)
JAVA-4507 DRIVERS-2214 DRIVERS-2243
1 parent 331bb3e commit d25db9c

File tree

4 files changed

+230
-8
lines changed

4 files changed

+230
-8
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.mongodb.MongoNamespace;
2222
import com.mongodb.ReadPreference;
2323
import com.mongodb.ServerCursor;
24+
import com.mongodb.connection.ConnectionDescription;
2425
import com.mongodb.connection.ServerType;
2526
import com.mongodb.diagnostics.logging.Logger;
2627
import com.mongodb.diagnostics.logging.Loggers;
@@ -55,6 +56,7 @@
5556
import static com.mongodb.internal.operation.DocumentHelper.putIfNotNull;
5657
import static com.mongodb.internal.operation.OperationHelper.getMoreCursorDocumentToQueryResult;
5758
import static com.mongodb.internal.operation.QueryHelper.translateCommandException;
59+
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionFourDotFour;
5860
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo;
5961
import static java.lang.String.format;
6062
import static java.util.Collections.singletonList;
@@ -251,8 +253,8 @@ public void onResult(final AsyncConnection connection, final Throwable t) {
251253

252254
private void getMore(final AsyncConnection connection, final ServerCursor cursor, final SingleResultCallback<List<T>> callback) {
253255
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) {
254-
connection.commandAsync(namespace.getDatabaseName(), asGetMoreCommandDocument(cursor.getId()), NO_OP_FIELD_NAME_VALIDATOR,
255-
ReadPreference.primary(), CommandResultDocumentCodec.create(decoder, "nextBatch"),
256+
connection.commandAsync(namespace.getDatabaseName(), asGetMoreCommandDocument(cursor.getId(), connection.getDescription()),
257+
NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), CommandResultDocumentCodec.create(decoder, "nextBatch"),
256258
connectionSource.getSessionContext(), connectionSource.getServerApi(), connectionSource.getRequestContext(),
257259
new CommandResultSingleResultCallback(connection, cursor, callback));
258260

@@ -262,7 +264,7 @@ private void getMore(final AsyncConnection connection, final ServerCursor cursor
262264
}
263265
}
264266

265-
private BsonDocument asGetMoreCommandDocument(final long cursorId) {
267+
private BsonDocument asGetMoreCommandDocument(final long cursorId, final ConnectionDescription connectionDescription) {
266268
BsonDocument document = new BsonDocument("getMore", new BsonInt64(cursorId))
267269
.append("collection", new BsonString(namespace.getCollectionName()));
268270

@@ -273,7 +275,9 @@ private BsonDocument asGetMoreCommandDocument(final long cursorId) {
273275
if (maxTimeMS != 0) {
274276
document.append("maxTimeMS", new BsonInt64(maxTimeMS));
275277
}
276-
putIfNotNull(document, "comment", comment);
278+
if (serverIsAtLeastVersionFourDotFour(connectionDescription)) {
279+
putIfNotNull(document, "comment", comment);
280+
}
277281
return document;
278282
}
279283

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.mongodb.ServerApi;
2727
import com.mongodb.ServerCursor;
2828
import com.mongodb.annotations.ThreadSafe;
29+
import com.mongodb.connection.ConnectionDescription;
2930
import com.mongodb.connection.ServerType;
3031
import com.mongodb.diagnostics.logging.Logger;
3132
import com.mongodb.diagnostics.logging.Loggers;
@@ -63,6 +64,7 @@
6364
import static com.mongodb.internal.operation.DocumentHelper.putIfNotNull;
6465
import static com.mongodb.internal.operation.OperationHelper.getMoreCursorDocumentToQueryResult;
6566
import static com.mongodb.internal.operation.QueryHelper.translateCommandException;
67+
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionFourDotFour;
6668
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo;
6769
import static java.lang.String.format;
6870
import static java.util.Collections.singletonList;
@@ -286,7 +288,7 @@ private void getMore() {
286288
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) {
287289
try {
288290
nextServerCursor = initFromCommandResult(connection.command(namespace.getDatabaseName(),
289-
asGetMoreCommandDocument(serverCursor),
291+
asGetMoreCommandDocument(serverCursor.getId(), connection.getDescription()),
290292
NO_OP_FIELD_NAME_VALIDATOR,
291293
ReadPreference.primary(),
292294
CommandResultDocumentCodec.create(decoder, "nextBatch"),
@@ -307,8 +309,8 @@ private void getMore() {
307309
});
308310
}
309311

310-
private BsonDocument asGetMoreCommandDocument(final ServerCursor serverCursor) {
311-
BsonDocument document = new BsonDocument("getMore", new BsonInt64(serverCursor.getId()))
312+
private BsonDocument asGetMoreCommandDocument(final long cursorId, final ConnectionDescription connectionDescription) {
313+
BsonDocument document = new BsonDocument("getMore", new BsonInt64(cursorId))
312314
.append("collection", new BsonString(namespace.getCollectionName()));
313315

314316
int batchSizeForGetMoreCommand = Math.abs(getNumberToReturn(limit, this.batchSize, count));
@@ -318,7 +320,9 @@ private BsonDocument asGetMoreCommandDocument(final ServerCursor serverCursor) {
318320
if (maxTimeMS != 0) {
319321
document.append("maxTimeMS", new BsonInt64(maxTimeMS));
320322
}
321-
putIfNotNull(document, "comment", comment);
323+
if (serverIsAtLeastVersionFourDotFour(connectionDescription)) {
324+
putIfNotNull(document, "comment", comment);
325+
}
322326
return document;
323327
}
324328

driver-core/src/test/resources/unified-test-format/crud/aggregate.json

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,121 @@
441441
]
442442
}
443443
]
444+
},
445+
{
446+
"description": "aggregate with comment sets comment on getMore - pre 4.4",
447+
"runOnRequirements": [
448+
{
449+
"minServerVersion": "3.6.0",
450+
"maxServerVersion": "4.4.0",
451+
"topologies": [
452+
"single",
453+
"replicaset"
454+
]
455+
}
456+
],
457+
"operations": [
458+
{
459+
"name": "aggregate",
460+
"arguments": {
461+
"pipeline": [
462+
{
463+
"$match": {
464+
"_id": {
465+
"$gt": 1
466+
}
467+
}
468+
}
469+
],
470+
"batchSize": 2,
471+
"comment": "comment"
472+
},
473+
"object": "collection0",
474+
"expectResult": [
475+
{
476+
"_id": 2,
477+
"x": 22
478+
},
479+
{
480+
"_id": 3,
481+
"x": 33
482+
},
483+
{
484+
"_id": 4,
485+
"x": 44
486+
},
487+
{
488+
"_id": 5,
489+
"x": 55
490+
},
491+
{
492+
"_id": 6,
493+
"x": 66
494+
}
495+
]
496+
}
497+
],
498+
"expectEvents": [
499+
{
500+
"client": "client0",
501+
"events": [
502+
{
503+
"commandStartedEvent": {
504+
"command": {
505+
"aggregate": "coll0",
506+
"pipeline": [
507+
{
508+
"$match": {
509+
"_id": {
510+
"$gt": 1
511+
}
512+
}
513+
}
514+
],
515+
"cursor": {
516+
"batchSize": 2
517+
},
518+
"comment": "comment"
519+
},
520+
"commandName": "aggregate",
521+
"databaseName": "aggregate-tests"
522+
}
523+
},
524+
{
525+
"commandStartedEvent": {
526+
"command": {
527+
"getMore": {
528+
"$$type": [
529+
"int",
530+
"long"
531+
]
532+
},
533+
"collection": "coll0",
534+
"batchSize": 2
535+
},
536+
"commandName": "getMore",
537+
"databaseName": "aggregate-tests"
538+
}
539+
},
540+
{
541+
"commandStartedEvent": {
542+
"command": {
543+
"getMore": {
544+
"$$type": [
545+
"int",
546+
"long"
547+
]
548+
},
549+
"collection": "coll0",
550+
"batchSize": 2
551+
},
552+
"commandName": "getMore",
553+
"databaseName": "aggregate-tests"
554+
}
555+
}
556+
]
557+
}
558+
]
444559
}
445560
]
446561
}

driver-core/src/test/resources/unified-test-format/crud/find-comment.json

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,105 @@
194194
}
195195
]
196196
},
197+
{
198+
"description": "find with comment sets comment on getMore - pre 4.4",
199+
"runOnRequirements": [
200+
{
201+
"minServerVersion": "3.6.0",
202+
"maxServerVersion": "4.4.0",
203+
"topologies": [
204+
"single",
205+
"replicaset"
206+
]
207+
}
208+
],
209+
"operations": [
210+
{
211+
"name": "find",
212+
"object": "collection0",
213+
"arguments": {
214+
"filter": {
215+
"_id": {
216+
"$gt": 1
217+
}
218+
},
219+
"batchSize": 2,
220+
"comment": "comment"
221+
},
222+
"expectResult": [
223+
{
224+
"_id": 2,
225+
"x": 22
226+
},
227+
{
228+
"_id": 3,
229+
"x": 33
230+
},
231+
{
232+
"_id": 4,
233+
"x": 44
234+
},
235+
{
236+
"_id": 5,
237+
"x": 55
238+
},
239+
{
240+
"_id": 6,
241+
"x": 66
242+
}
243+
]
244+
}
245+
],
246+
"expectEvents": [
247+
{
248+
"client": "client0",
249+
"events": [
250+
{
251+
"commandStartedEvent": {
252+
"command": {
253+
"find": "coll0",
254+
"filter": {
255+
"_id": {
256+
"$gt": 1
257+
}
258+
},
259+
"batchSize": 2,
260+
"comment": "comment"
261+
}
262+
}
263+
},
264+
{
265+
"commandStartedEvent": {
266+
"command": {
267+
"getMore": {
268+
"$$type": [
269+
"int",
270+
"long"
271+
]
272+
},
273+
"collection": "coll0",
274+
"batchSize": 2
275+
}
276+
}
277+
},
278+
{
279+
"commandStartedEvent": {
280+
"command": {
281+
"getMore": {
282+
"$$type": [
283+
"int",
284+
"long"
285+
]
286+
},
287+
"collection": "coll0",
288+
"batchSize": 2
289+
}
290+
}
291+
}
292+
]
293+
}
294+
]
295+
},
197296
{
198297
"description": "find with comment sets comment on getMore",
199298
"runOnRequirements": [

0 commit comments

Comments
 (0)