-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-4780 Implement fast path for server selection with Primary #2416
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
Changes from 7 commits
ac12b55
f32a3a6
07e0dca
7dfc3f1
746e8d5
25fd1f2
e5a0c84
e478cf7
40794fe
ee955f1
fd194f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
from bson.objectid import ObjectId | ||
from pymongo import common | ||
from pymongo.errors import ConfigurationError, PyMongoError | ||
from pymongo.read_preferences import ReadPreference, _AggWritePref, _ServerMode | ||
from pymongo.read_preferences import Primary, ReadPreference, _AggWritePref, _ServerMode | ||
from pymongo.server_description import ServerDescription | ||
from pymongo.server_selectors import Selection | ||
from pymongo.server_type import SERVER_TYPE | ||
|
@@ -324,6 +324,14 @@ def apply_selector( | |
description = self.server_descriptions().get(address) | ||
return [description] if description else [] | ||
|
||
# Primary selection fast path. | ||
if self.topology_type == TOPOLOGY_TYPE.ReplicaSetWithPrimary and isinstance( | ||
selector, Primary | ||
): | ||
for sd in self._server_descriptions.values(): | ||
if sd.server_type == SERVER_TYPE.RSPrimary: | ||
return [sd] | ||
ShaneHarvey marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh one more thing, don't we have to call the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And add a test for it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have to defer for now, working on a HELP ticket There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
selection = Selection.from_topology_description(self) | ||
# Ignore read preference for sharded clusters. | ||
if self.topology_type != TOPOLOGY_TYPE.Sharded: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My one concern is that technically this could be a breaking change if a user is subclassing Primary and overriding
__call__
to do something special during server selection. One simple way to avoid that risk is doingtype(selector) is Primary
rather than isinstance. Or we could document the potential breaking change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with the code modification option