Skip to content

Commit 5b8b09a

Browse files
authored
PYTHON-3020 Properly mark server unknown after "not master" errors without a code (#797)
Fix prefer-error-code SDAM test.
1 parent 9cf88cf commit 5b8b09a

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

.evergreen/run-mockupdb-tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -o errexit
88
${PYTHON_BINARY} setup.py clean
99

1010
createvirtualenv ${PYTHON_BINARY} mockuptests
11-
trap "deactivate, rm -rf mockuptests" EXIT HUP
11+
trap "deactivate; rm -rf mockuptests" EXIT HUP
1212

1313
# Install PyMongo from git clone so mockup-tests don't
1414
# download it from pypi.

pymongo/topology.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,9 @@ def _handle_error(self, address, err_ctx):
633633
if hasattr(error, 'code'):
634634
err_code = error.code
635635
else:
636-
err_code = error.details.get('code', -1)
636+
# Default error code if one does not exist.
637+
default = 10107 if isinstance(error, NotPrimaryError) else None
638+
err_code = error.details.get('code', default)
637639
if err_code in helpers._NOT_PRIMARY_CODES:
638640
is_shutting_down = err_code in helpers._SHUTDOWN_CODES
639641
# Mark server Unknown, clear the pool, and request check.

test/discovery_and_monitoring/errors/prefer-error-code.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
}
5353
},
5454
{
55-
"description": "errmsg \"not writable primary\" gets ignored when error code exists",
55+
"description": "errmsg \"not master\" gets ignored when error code exists",
5656
"applicationErrors": [
5757
{
5858
"address": "a:27017",
@@ -61,7 +61,7 @@
6161
"type": "command",
6262
"response": {
6363
"ok": 0,
64-
"errmsg": "not writable primary",
64+
"errmsg": "not master",
6565
"code": 1
6666
}
6767
}

test/mockupdb/operations.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -52,43 +52,39 @@
5252
sharded cluster (PYTHON-868).
5353
"""
5454

55-
not_master_reply_to_query = OpMsgReply(
56-
{'$err': 'not master'},
57-
flags=REPLY_FLAGS['QueryFailure'])
58-
59-
not_master_reply_to_command = OpMsgReply(ok=0, errmsg='not master')
55+
not_master_reply = OpMsgReply(ok=0, errmsg='not master')
6056

6157
operations = [
6258
Operation(
6359
'find_one',
6460
lambda client: client.db.collection.find_one(),
6561
reply={'cursor': {'id': 0, 'firstBatch': []}},
6662
op_type='may-use-secondary',
67-
not_master=not_master_reply_to_query),
63+
not_master=not_master_reply),
6864
Operation(
6965
'count',
7066
lambda client: client.db.collection.count_documents({}),
7167
reply={'n': 1},
7268
op_type='may-use-secondary',
73-
not_master=not_master_reply_to_command),
69+
not_master=not_master_reply),
7470
Operation(
7571
'aggregate',
7672
lambda client: client.db.collection.aggregate([]),
7773
reply={'cursor': {'id': 0, 'firstBatch': []}},
7874
op_type='may-use-secondary',
79-
not_master=not_master_reply_to_command),
75+
not_master=not_master_reply),
8076
Operation(
8177
'options',
8278
lambda client: client.db.collection.options(),
8379
reply={'cursor': {'id': 0, 'firstBatch': []}},
8480
op_type='must-use-primary',
85-
not_master=not_master_reply_to_command),
81+
not_master=not_master_reply),
8682
Operation(
8783
'command',
8884
lambda client: client.db.command('foo'),
8985
reply={'ok': 1},
9086
op_type='must-use-primary', # Ignores client's read preference.
91-
not_master=not_master_reply_to_command),
87+
not_master=not_master_reply),
9288
Operation(
9389
'secondary command',
9490
lambda client:
@@ -101,7 +97,7 @@
10197
lambda client: client.db.collection.index_information(),
10298
reply={'cursor': {'id': 0, 'firstBatch': []}},
10399
op_type='must-use-primary',
104-
not_master=not_master_reply_to_command),
100+
not_master=not_master_reply),
105101
]
106102

107103

0 commit comments

Comments
 (0)