PYTHON-4921 Eliminate unnecessary killCursors command when batchSize == limit (#2004)

This commit is contained in:
Shane Harvey 2024-11-18 10:25:20 -08:00 committed by GitHub
parent d2c1e18cc2
commit 18940030f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 368 additions and 32 deletions

View File

@ -252,6 +252,10 @@ def _gen_find_command(
if limit < 0:
cmd["singleBatch"] = True
if batch_size:
# When limit and batchSize are equal we increase batchSize by 1 to
# avoid an unnecessary killCursors.
if limit == batch_size:
batch_size += 1
cmd["batchSize"] = batch_size
if read_concern.level and not (session and session.in_transaction):
cmd["readConcern"] = read_concern.document

View File

@ -3,7 +3,8 @@
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "8.0"
"minServerVersion": "8.0",
"serverless": "forbid"
}
],
"createEntities": [

View File

@ -3,7 +3,8 @@
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "8.0"
"minServerVersion": "8.0",
"serverless": "forbid"
}
],
"createEntities": [

View File

@ -0,0 +1,139 @@
{
"description": "distinct-hint",
"schemaVersion": "1.0",
"runOnRequirements": [
{
"minServerVersion": "7.1.0"
}
],
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "distinct-hint-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "coll0"
}
}
],
"initialData": [
{
"collectionName": "coll0",
"databaseName": "distinct-hint-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
],
"tests": [
{
"description": "distinct with hint string",
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {
"_id": 1
},
"hint": "_id_"
},
"expectResult": [
11
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {
"_id": 1
},
"hint": "_id_"
},
"commandName": "distinct",
"databaseName": "distinct-hint-tests"
}
}
]
}
]
},
{
"description": "distinct with hint document",
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {
"_id": 1
},
"hint": {
"_id": 1
}
},
"expectResult": [
11
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {
"_id": 1
},
"hint": {
"_id": 1
}
},
"commandName": "distinct",
"databaseName": "distinct-hint-tests"
}
}
]
}
]
}
]
}

View File

@ -249,7 +249,7 @@
"name": "estimatedDocumentCount",
"object": "collection0",
"expectError": {
"isError": true
"isClientError": true
}
}
],

View File

@ -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"
}
}
]
}
]
}
]
}

View File

@ -0,0 +1,158 @@
{
"description": "findOne",
"schemaVersion": "1.0",
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "find-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "coll0"
}
}
],
"initialData": [
{
"collectionName": "coll0",
"databaseName": "find-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
},
{
"_id": 4,
"x": 44
},
{
"_id": 5,
"x": 55
},
{
"_id": 6,
"x": 66
}
]
}
],
"tests": [
{
"description": "FindOne with filter",
"operations": [
{
"object": "collection0",
"name": "findOne",
"arguments": {
"filter": {
"_id": 1
}
},
"expectResult": {
"_id": 1,
"x": 11
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"find": "coll0",
"filter": {
"_id": 1
},
"batchSize": {
"$$exists": false
},
"limit": 1,
"singleBatch": true
},
"commandName": "find",
"databaseName": "find-tests"
}
}
]
}
]
},
{
"description": "FindOne with filter, sort, and skip",
"operations": [
{
"object": "collection0",
"name": "findOne",
"arguments": {
"filter": {
"_id": {
"$gt": 2
}
},
"sort": {
"_id": 1
},
"skip": 2
},
"expectResult": {
"_id": 5,
"x": 55
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"find": "coll0",
"filter": {
"_id": {
"$gt": 2
}
},
"sort": {
"_id": 1
},
"skip": 2,
"batchSize": {
"$$exists": false
},
"limit": 1,
"singleBatch": true
},
"commandName": "find",
"databaseName": "find-tests"
}
}
]
}
]
}
]
}

View File

@ -925,35 +925,6 @@ def parse_spec_options(opts):
if "maxCommitTimeMS" in opts:
opts["max_commit_time_ms"] = opts.pop("maxCommitTimeMS")
if "hint" in opts:
hint = opts.pop("hint")
if not isinstance(hint, str):
hint = list(hint.items())
opts["hint"] = hint
# Properly format 'hint' arguments for the Bulk API tests.
if "requests" in opts:
reqs = opts.pop("requests")
for req in reqs:
if "name" in req:
# CRUD v2 format
args = req.pop("arguments", {})
if "hint" in args:
hint = args.pop("hint")
if not isinstance(hint, str):
hint = list(hint.items())
args["hint"] = hint
req["arguments"] = args
else:
# Unified test format
bulk_model, spec = next(iter(req.items()))
if "hint" in spec:
hint = spec.pop("hint")
if not isinstance(hint, str):
hint = list(hint.items())
spec["hint"] = hint
opts["requests"] = reqs
return dict(opts)