Skip to content

Commit 322940a

Browse files
committed
Sync to spec
1 parent fb7fef1 commit 322940a

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

pymongo/topology_description.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,26 @@ def apply_selector(
352352
if custom_selector:
353353
sds = custom_selector(sds)
354354
return sds
355+
# All primaries are deprioritized
356+
if deprioritized_servers:
357+
for sd in deprioritized_servers:
358+
if sd.server_type == SERVER_TYPE.RSPrimary:
359+
sds = [sd]
360+
if custom_selector:
361+
sds = custom_selector(sds)
362+
return sds
355363
# No primary found, return an empty list.
356364
return []
357365

358366
selection = Selection.from_topology_description(self)
359367
# Ignore read preference for sharded clusters.
360368
if self.topology_type != TOPOLOGY_TYPE.Sharded:
361369
selection = selector(selection)
370+
# No suitable servers found, apply preference again but include deprioritized servers.
371+
if not selection and deprioritized_servers:
372+
self._filter_servers(None)
373+
selection = Selection.from_topology_description(self)
374+
selection = selector(selection)
362375

363376
# Apply custom selector followed by localThresholdMS.
364377
if custom_selector is not None and selection:

test/server_selection/server_selection/ReplicaSetWithPrimary/read/DeprioritizedPrimary.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,23 @@
4343
}
4444
],
4545
"suitable_servers": [
46+
{
47+
"address": "a:27017",
48+
"avg_rtt_ms": 5,
49+
"type": "RSPrimary",
50+
"tags": {
51+
"data_center": "nyc"
52+
}
53+
}
4654
],
4755
"in_latency_window": [
56+
{
57+
"address": "a:27017",
58+
"avg_rtt_ms": 5,
59+
"type": "RSPrimary",
60+
"tags": {
61+
"data_center": "nyc"
62+
}
63+
}
4864
]
4965
}

test/server_selection/server_selection/ReplicaSetWithPrimary/read/DeprioritizedSecondary.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,31 @@
5656
}
5757
],
5858
"suitable_servers": [
59+
{
60+
"address": "b:27017",
61+
"avg_rtt_ms": 5,
62+
"type": "RSSecondary",
63+
"tags": {
64+
"data_center": "nyc"
65+
}
66+
},
67+
{
68+
"address": "c:27017",
69+
"avg_rtt_ms": 100,
70+
"type": "RSSecondary",
71+
"tags": {
72+
"data_center": "nyc"
73+
}
74+
}
5975
],
6076
"in_latency_window": [
77+
{
78+
"address": "b:27017",
79+
"avg_rtt_ms": 5,
80+
"type": "RSSecondary",
81+
"tags": {
82+
"data_center": "nyc"
83+
}
84+
}
6185
]
6286
}

test/server_selection/server_selection/ReplicaSetWithPrimary/write/DeprioritizedSecondaryPreferred.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,23 @@
4848
}
4949
],
5050
"suitable_servers": [
51+
{
52+
"address": "a:27017",
53+
"avg_rtt_ms": 26,
54+
"type": "RSPrimary",
55+
"tags": {
56+
"data_center": "nyc"
57+
}
58+
}
5159
],
5260
"in_latency_window": [
61+
{
62+
"address": "a:27017",
63+
"avg_rtt_ms": 26,
64+
"type": "RSPrimary",
65+
"tags": {
66+
"data_center": "nyc"
67+
}
68+
}
5369
]
5470
}

0 commit comments

Comments
 (0)