Skip to content

Commit 5cb476e

Browse files
committed
PYTHON-2629 Use hello command when API Version is declared (#610)
PYTHON-2697 Update CMAP runner to ignore extra events (cherry picked from commit f64b563)
1 parent c3c62ad commit 5cb476e

14 files changed

+80
-32
lines changed

pymongo/auth.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,8 @@ def _authenticate_default(credentials, sock_info):
579579
mechs = sock_info.negotiated_mechanisms[credentials]
580580
else:
581581
source = credentials.source
582-
cmd = SON([
583-
('ismaster', 1),
584-
('saslSupportedMechs', source + '.' + credentials.username)])
582+
cmd = sock_info.hello_cmd()
583+
cmd['saslSupportedMechs'] = source + '.' + credentials.username
585584
mechs = sock_info.command(
586585
source, cmd, publish_events=False).get(
587586
'saslSupportedMechs', [])

pymongo/compression_support.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
except ImportError:
3535
_HAVE_ZSTD = False
3636

37+
from pymongo.hello import HelloCompat
3738
from pymongo.monitoring import _SENSITIVE_COMMANDS
3839

3940
_SUPPORTED_COMPRESSORS = set(["snappy", "zlib", "zstd"])
40-
_NO_COMPRESSION = set(['ismaster'])
41+
_NO_COMPRESSION = set([HelloCompat.CMD, HelloCompat.LEGACY_CMD])
4142
_NO_COMPRESSION.update(_SENSITIVE_COMMANDS)
4243

4344

pymongo/hello.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2021-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helpers for the 'hello' and legacy hello commands."""
16+
17+
18+
class HelloCompat:
19+
CMD = 'hello'
20+
LEGACY_CMD = 'ismaster'
21+
PRIMARY = 'isWritablePrimary'
22+
LEGACY_PRIMARY = 'ismaster'

pymongo/ismaster.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from bson.py3compat import imap
2020
from pymongo import common
21+
from pymongo.hello import HelloCompat
2122
from pymongo.server_type import SERVER_TYPE
2223

2324

@@ -31,7 +32,9 @@ def _get_server_type(doc):
3132
elif doc.get('setName'):
3233
if doc.get('hidden'):
3334
return SERVER_TYPE.RSOther
34-
elif doc.get('ismaster'):
35+
elif doc.get(HelloCompat.PRIMARY):
36+
return SERVER_TYPE.RSPrimary
37+
elif doc.get(HelloCompat.LEGACY_PRIMARY):
3538
return SERVER_TYPE.RSPrimary
3639
elif doc.get('secondary'):
3740
return SERVER_TYPE.RSSecondary

pymongo/pool.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
NotMasterError,
5252
OperationFailure,
5353
PyMongoError)
54+
from pymongo.hello import HelloCompat
5455
from pymongo.ismaster import IsMaster
5556
from pymongo.monotonic import time as _time
5657
from pymongo.monitoring import (ConnectionCheckOutFailedReason,
@@ -549,12 +550,18 @@ def __init__(self, sock, pool, address, id):
549550
self.opts = pool.opts
550551
self.more_to_come = False
551552

553+
def hello_cmd(self):
554+
if self.opts.server_api:
555+
return SON([(HelloCompat.CMD, 1)])
556+
else:
557+
return SON([(HelloCompat.LEGACY_CMD, 1)])
558+
552559
def ismaster(self, all_credentials=None):
553560
return self._ismaster(None, None, None, all_credentials)
554561

555562
def _ismaster(self, cluster_time, topology_version,
556563
heartbeat_frequency, all_credentials):
557-
cmd = SON([('ismaster', 1)])
564+
cmd = self.hello_cmd()
558565
performing_handshake = not self.performed_handshake
559566
awaitable = False
560567
if performing_handshake:

test/cmap/pool-checkout-no-idle.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
},
2121
{
2222
"name": "checkOut"
23+
},
24+
{
25+
"name": "waitForEvent",
26+
"event": "ConnectionClosed",
27+
"count": 1
28+
},
29+
{
30+
"name": "waitForEvent",
31+
"event": "ConnectionCheckedOut",
32+
"count": 2
2333
}
2434
],
2535
"events": [
@@ -43,11 +53,6 @@
4353
"connectionId": 1,
4454
"reason": "idle",
4555
"address": 42
46-
},
47-
{
48-
"type": "ConnectionCheckedOut",
49-
"connectionId": 2,
50-
"address": 42
5156
}
5257
],
5358
"ignore": [

test/cmap/pool-checkout-no-stale.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
},
1717
{
1818
"name": "checkOut"
19+
},
20+
{
21+
"name": "waitForEvent",
22+
"event": "ConnectionClosed",
23+
"count": 1
24+
},
25+
{
26+
"name": "waitForEvent",
27+
"event": "ConnectionCheckedOut",
28+
"count": 2
1929
}
2030
],
2131
"events": [
@@ -43,11 +53,6 @@
4353
"connectionId": 1,
4454
"reason": "stale",
4555
"address": 42
46-
},
47-
{
48-
"type": "ConnectionCheckedOut",
49-
"connectionId": 2,
50-
"address": 42
5156
}
5257
],
5358
"ignore": [

test/discovery_and_monitoring/errors/write_errors_ignored.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"writeErrors": [
6464
{
6565
"errmsg": "NotMasterNoSlaveOk",
66-
"code": 13435
66+
"code": 13435,
67+
"index": 0
6768
}
6869
]
6970
}

test/discovery_and_monitoring_integration/connectTimeoutMS.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
},
4343
"data": {
4444
"failCommands": [
45-
"isMaster"
45+
"isMaster",
46+
"hello"
4647
],
4748
"appName": "connectTimeoutMS=0",
4849
"blockConnection": true,

test/discovery_and_monitoring_integration/isMaster-command-error.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"runOn": [
33
{
4-
"minServerVersion": "4.4"
4+
"minServerVersion": "4.9"
55
}
66
],
77
"database_name": "sdam-tests",
@@ -17,7 +17,8 @@
1717
},
1818
"data": {
1919
"failCommands": [
20-
"isMaster"
20+
"isMaster",
21+
"hello"
2122
],
2223
"appName": "commandErrorHandshakeTest",
2324
"closeConnection": false,
@@ -128,7 +129,8 @@
128129
},
129130
"data": {
130131
"failCommands": [
131-
"isMaster"
132+
"isMaster",
133+
"hello"
132134
],
133135
"appName": "commandErrorCheckTest",
134136
"closeConnection": false,

0 commit comments

Comments
 (0)