PYTHON-3020 Properly mark server unknown after "not master" errors without a code (#797)

Fix prefer-error-code SDAM test.
This commit is contained in:
Shane Harvey 2021-11-17 18:18:41 -08:00 committed by GitHub
parent 9cf88cfdc1
commit 5b8b09ac4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 15 deletions

View File

@ -8,7 +8,7 @@ set -o errexit
${PYTHON_BINARY} setup.py clean
createvirtualenv ${PYTHON_BINARY} mockuptests
trap "deactivate, rm -rf mockuptests" EXIT HUP
trap "deactivate; rm -rf mockuptests" EXIT HUP
# Install PyMongo from git clone so mockup-tests don't
# download it from pypi.

View File

@ -633,7 +633,9 @@ class Topology(object):
if hasattr(error, 'code'):
err_code = error.code
else:
err_code = error.details.get('code', -1)
# Default error code if one does not exist.
default = 10107 if isinstance(error, NotPrimaryError) else None
err_code = error.details.get('code', default)
if err_code in helpers._NOT_PRIMARY_CODES:
is_shutting_down = err_code in helpers._SHUTDOWN_CODES
# Mark server Unknown, clear the pool, and request check.

View File

@ -52,7 +52,7 @@
}
},
{
"description": "errmsg \"not writable primary\" gets ignored when error code exists",
"description": "errmsg \"not master\" gets ignored when error code exists",
"applicationErrors": [
{
"address": "a:27017",
@ -61,7 +61,7 @@
"type": "command",
"response": {
"ok": 0,
"errmsg": "not writable primary",
"errmsg": "not master",
"code": 1
}
}

View File

@ -52,11 +52,7 @@ secondaries in a replica set, or select a mongos for secondary reads in a
sharded cluster (PYTHON-868).
"""
not_master_reply_to_query = OpMsgReply(
{'$err': 'not master'},
flags=REPLY_FLAGS['QueryFailure'])
not_master_reply_to_command = OpMsgReply(ok=0, errmsg='not master')
not_master_reply = OpMsgReply(ok=0, errmsg='not master')
operations = [
Operation(
@ -64,31 +60,31 @@ operations = [
lambda client: client.db.collection.find_one(),
reply={'cursor': {'id': 0, 'firstBatch': []}},
op_type='may-use-secondary',
not_master=not_master_reply_to_query),
not_master=not_master_reply),
Operation(
'count',
lambda client: client.db.collection.count_documents({}),
reply={'n': 1},
op_type='may-use-secondary',
not_master=not_master_reply_to_command),
not_master=not_master_reply),
Operation(
'aggregate',
lambda client: client.db.collection.aggregate([]),
reply={'cursor': {'id': 0, 'firstBatch': []}},
op_type='may-use-secondary',
not_master=not_master_reply_to_command),
not_master=not_master_reply),
Operation(
'options',
lambda client: client.db.collection.options(),
reply={'cursor': {'id': 0, 'firstBatch': []}},
op_type='must-use-primary',
not_master=not_master_reply_to_command),
not_master=not_master_reply),
Operation(
'command',
lambda client: client.db.command('foo'),
reply={'ok': 1},
op_type='must-use-primary', # Ignores client's read preference.
not_master=not_master_reply_to_command),
not_master=not_master_reply),
Operation(
'secondary command',
lambda client:
@ -101,7 +97,7 @@ operations = [
lambda client: client.db.collection.index_information(),
reply={'cursor': {'id': 0, 'firstBatch': []}},
op_type='must-use-primary',
not_master=not_master_reply_to_command),
not_master=not_master_reply),
]