From 5b8b09ac4f925d0cbaa9532aba8b66a572ccbd03 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Wed, 17 Nov 2021 18:18:41 -0800 Subject: [PATCH] PYTHON-3020 Properly mark server unknown after "not master" errors without a code (#797) Fix prefer-error-code SDAM test. --- .evergreen/run-mockupdb-tests.sh | 2 +- pymongo/topology.py | 4 +++- .../errors/prefer-error-code.json | 4 ++-- test/mockupdb/operations.py | 18 +++++++----------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.evergreen/run-mockupdb-tests.sh b/.evergreen/run-mockupdb-tests.sh index a0b67302a..a76ed6316 100755 --- a/.evergreen/run-mockupdb-tests.sh +++ b/.evergreen/run-mockupdb-tests.sh @@ -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. diff --git a/pymongo/topology.py b/pymongo/topology.py index 9139c1492..6f26cff61 100644 --- a/pymongo/topology.py +++ b/pymongo/topology.py @@ -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. diff --git a/test/discovery_and_monitoring/errors/prefer-error-code.json b/test/discovery_and_monitoring/errors/prefer-error-code.json index 21d123f42..eb00b6961 100644 --- a/test/discovery_and_monitoring/errors/prefer-error-code.json +++ b/test/discovery_and_monitoring/errors/prefer-error-code.json @@ -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 } } diff --git a/test/mockupdb/operations.py b/test/mockupdb/operations.py index 9fb0ca16b..47890f80e 100644 --- a/test/mockupdb/operations.py +++ b/test/mockupdb/operations.py @@ -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), ]