Skip to content

PYTHON-3021 Send primaryPreferred when connected to standalone servers #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,8 @@ def _socket_for_reads(self, read_preference, session):
# for topology type Single."
# Thread safe: if the type is single it cannot change.
topology = self._get_topology()
single = topology.description.topology_type == TOPOLOGY_TYPE.Single
server = self._select_server(read_preference, session)
single = topology.description.topology_type == TOPOLOGY_TYPE.Single
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the bug. The problem is that before this _select_server call the topology_type could be Unknown in which case we would incorrectly set single to False.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense.


with self._get_socket(server, session) as sock_info:
secondary_ok = (single and not sock_info.is_mongos) or (
Expand Down
7 changes: 2 additions & 5 deletions test/mockupdb/test_op_msg_read_preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,12 @@ def test(self):
else:
self.fail('unrecognized op_type %r' % operation.op_type)
# For single mongod we send primaryPreferred instead of primary.
if (expected_pref == ReadPreference.PRIMARY and self.single_mongod
and operation.name != "command"):
if expected_pref == ReadPreference.PRIMARY and self.single_mongod:
expected_pref = ReadPreference.PRIMARY_PREFERRED
with going(operation.function, client) as future:
with going(operation.function, client):
request = expected_server.receive()
request.reply(operation.reply)

future() # No error.

self.assertEqual(expected_pref.document,
request.doc.get('$readPreference'))
self.assertNotIn('$query', request.doc)
Expand Down