Implement fast path for server selection with Primary()

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Fixed
    • Priority: Unknown
    • 4.14
    • Affects Version/s: None
    • Component/s: None
    • None
    • Python Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      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:
              Steve Silvester
              Reporter:
              Shane Harvey
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: