Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-4780

Implement fast path for server selection with Primary()

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • 4.11
    • Affects Version/s: None
    • Component/s: None
    • None
    • Python Drivers

      A lot of the work for selecting a server (in TopologyDescription.apply_selector) is unneeded when using Primary() read preference on a replica set. We could implement a fast path for server selection with Primary():

      diff --git a/pymongo/topology_description.py b/pymongo/topology_description.py
      index ac296cac4..e8d34e120 100644
      --- a/pymongo/topology_description.py
      +++ b/pymongo/topology_description.py
      @@ -34,7 +34,7 @@ from bson.min_key import MinKey
       from bson.objectid import ObjectId
       from pymongo import common
       from pymongo.errors import ConfigurationError
      -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
      @@ -335,6 +335,14 @@ class TopologyDescription:
                   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]
      +
               selection = Selection.from_topology_description(self)
       

      Noticed while looking into PERF-5904. 

            Assignee:
            Unassigned Unassigned
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: