Skip to content

Eliminate unnecessary killCursors command when batchSize == limit #1656

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

Merged
merged 4 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ public FindOptions maxAwaitTime(final long maxAwaitTime, final TimeUnit timeUnit
* @mongodb.driver.manual reference/method/cursor.batchSize/#cursor.batchSize Batch Size
*/
public int getBatchSize() {
return batchSize;
return batchSize > 0 && batchSize == limit
? batchSize + 1 // avoid an open cursor on server side when batchSize and limit are equal
: batchSize;
}

/**
Expand Down
62 changes: 62 additions & 0 deletions driver-core/src/test/resources/unified-test-format/crud/find.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,68 @@
]
}
]
},
{
"description": "Find with batchSize equal to limit",
"operations": [
{
"object": "collection0",
"name": "find",
"arguments": {
"filter": {
"_id": {
"$gt": 1
}
},
"sort": {
"_id": 1
},
"limit": 4,
"batchSize": 4
},
"expectResult": [
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
},
{
"_id": 4,
"x": 44
},
{
"_id": 5,
"x": 55
}
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"find": "coll0",
"filter": {
"_id": {
"$gt": 1
}
},
"limit": 4,
"batchSize": 5
},
"commandName": "find",
"databaseName": "find-tests"
}
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void shouldBuildTheExpectedOperation() {
.retryReads(true)
.filter(new BsonDocument())
.allowDiskUse(false)
.batchSize(100)
.batchSize(101)
.collation(COLLATION)
.comment(new BsonString("my comment"))
.cursorType(CursorType.NonTailable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class GridFSFindIterableSpecification extends Specification {
expect operation, isTheSameAs(new FindOperation<GridFSFile>(namespace, gridFSFileCodec)
.filter(new BsonDocument('filter', new BsonInt32(2)))
.sort(new BsonDocument('sort', new BsonInt32(2)))
.batchSize(99)
.batchSize(100)
.limit(99)
.skip(9)
.noCursorTimeout(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class FindIterableSpecification extends Specification {
.filter(new BsonDocument('filter', new BsonInt32(1)))
.sort(new BsonDocument('sort', new BsonInt32(1)))
.projection(new BsonDocument('projection', new BsonInt32(1)))
.batchSize(100)
.batchSize(101)
.limit(100)
.skip(10)
.cursorType(CursorType.NonTailable)
Expand Down Expand Up @@ -132,7 +132,7 @@ class FindIterableSpecification extends Specification {
.filter(new BsonDocument('filter', new BsonInt32(2)))
.sort(new BsonDocument('sort', new BsonInt32(2)))
.projection(new BsonDocument('projection', new BsonInt32(2)))
.batchSize(99)
.batchSize(100)
.limit(99)
.skip(9)
.cursorType(CursorType.Tailable)
Expand Down