PYTHON-4780 Implement fast path for server selection with Primary#2416
PYTHON-4780 Implement fast path for server selection with Primary#2416blink1073 merged 11 commits intomongodb:masterfrom
Conversation
|
I didn't schedule a perf build because our perf tests only run on standalone. |
|
Minor note for the benchmark, thread dispatching could be dominating |
|
With your suggested benchmark changes: On With this PR: |
ShaneHarvey
left a comment
There was a problem hiding this comment.
Nice work! Could you add a changelog saying "Improved performance of selecting a server with the Primary selector."?
pymongo/topology_description.py
Outdated
|
|
||
| # Primary selection fast path. | ||
| if self.topology_type == TOPOLOGY_TYPE.ReplicaSetWithPrimary and isinstance( | ||
| selector, Primary |
There was a problem hiding this comment.
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 doing type(selector) is Primary rather than isinstance. Or we could document the potential breaking change.
There was a problem hiding this comment.
I went with the code modification option
pymongo/topology_description.py
Outdated
| if self.topology_type == TOPOLOGY_TYPE.ReplicaSetWithPrimary and type(selector) is Primary: | ||
| for sd in self._server_descriptions.values(): | ||
| if sd.server_type == SERVER_TYPE.RSPrimary: | ||
| return [sd] |
There was a problem hiding this comment.
Oh one more thing, don't we have to call the custom_selector here if one was provided?
There was a problem hiding this comment.
I'll have to defer for now, working on a HELP ticket
pymongo/topology_description.py
Outdated
|
|
||
| # Primary selection fast path. | ||
| if ( | ||
| custom_selector is None |
There was a problem hiding this comment.
Is the custom_selector is None check supposed to be here?
There was a problem hiding this comment.
No, I forgot to back that change out
|
|
||
| # If there is a custom selector, it should be applied. | ||
| def custom_selector(servers): | ||
| return [] |
Tested with the following script:
On
master:0.4806627919897437
1.5110677920019953
With this change:
0.17988054199668113
1.233099208009662