Skip to content

Commit be51347

Browse files
committed
fix: don't try to calculate sMax if there are no viable servers
When the primary is unknown the `maxStalenessReducer` attempts to determine the server with the greatest `lastWriteDate` in order to calculate each server's staleness. There is a bug where the reducer currently assumes it will always be able to find such a server, which is not possible when working with an empty set of servers. NODE-2641
1 parent 9c4288c commit be51347

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

lib/core/sdam/server_selection.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ function maxStalenessReducer(readPreference, topologyDescription, servers) {
6060
if (staleness <= readPreference.maxStalenessSeconds) result.push(server);
6161
return result;
6262
}, []);
63-
} else if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) {
63+
}
64+
65+
if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) {
66+
if (servers.length === 0) {
67+
return servers;
68+
}
69+
6470
const sMax = servers.reduce((max, s) => (s.lastWriteDate > max.lastWriteDate ? s : max));
6571
return servers.reduce((result, server) => {
6672
const stalenessMS =
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"topology_description": {
3+
"type": "ReplicaSetNoPrimary",
4+
"servers": [
5+
{
6+
"address": "a:27017",
7+
"type": "Unknown"
8+
},
9+
{
10+
"address": "b:27017",
11+
"type": "Unknown"
12+
}
13+
]
14+
},
15+
"read_preference": {
16+
"mode": "Nearest",
17+
"maxStalenessSeconds": 1
18+
},
19+
"error": true
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# maxStalenessSeconds must be at least 90 seconds, even with no known servers.
2+
---
3+
topology_description:
4+
type: ReplicaSetNoPrimary
5+
servers:
6+
- &1
7+
address: a:27017
8+
type: Unknown
9+
- &2
10+
address: b:27017
11+
type: Unknown
12+
read_preference:
13+
mode: Nearest
14+
maxStalenessSeconds: 1 # Too small.
15+
error: true

test/spec/max-staleness/ReplicaSetNoPrimary/NoKnownServers.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"read_preference": {
1616
"mode": "Nearest",
17-
"maxStalenessSeconds": 1
17+
"maxStalenessSeconds": 90
1818
},
19-
"error": true
19+
"suitable_servers": [],
20+
"in_latency_window": []
2021
}

test/spec/max-staleness/ReplicaSetNoPrimary/NoKnownServers.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# maxStalenessSeconds must be at least 90 seconds, even with no known servers.
1+
# valid maxStalenessSeconds and no known servers results in an empty set of suitable servers
22
---
33
topology_description:
44
type: ReplicaSetNoPrimary
@@ -11,5 +11,6 @@ topology_description:
1111
type: Unknown
1212
read_preference:
1313
mode: Nearest
14-
maxStalenessSeconds: 1 # Too small.
15-
error: true
14+
maxStalenessSeconds: 90
15+
suitable_servers: []
16+
in_latency_window: []

0 commit comments

Comments
 (0)