Skip to content

Commit

Permalink
fix: don't try to calculate sMax if there are no viable servers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbroadst committed Jun 10, 2020
1 parent ee451f2 commit dd24cee
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/sdam/server_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ function maxStalenessReducer(readPreference, topologyDescription, servers) {
if (staleness <= readPreference.maxStalenessSeconds) result.push(server);
return result;
}, []);
} else if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) {
}

if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) {
if (servers.length === 0) {
return servers;
}

const sMax = servers.reduce((max, s) => (s.lastWriteDate > max.lastWriteDate ? s : max));
return servers.reduce((result, server) => {
const stalenessMS =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"topology_description": {
"type": "ReplicaSetNoPrimary",
"servers": [
{
"address": "a:27017",
"type": "Unknown"
},
{
"address": "b:27017",
"type": "Unknown"
}
]
},
"read_preference": {
"mode": "Nearest",
"maxStalenessSeconds": 1
},
"error": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# maxStalenessSeconds must be at least 90 seconds, even with no known servers.
---
topology_description:
type: ReplicaSetNoPrimary
servers:
- &1
address: a:27017
type: Unknown
- &2
address: b:27017
type: Unknown
read_preference:
mode: Nearest
maxStalenessSeconds: 1 # Too small.
error: true
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"read_preference": {
"mode": "Nearest",
"maxStalenessSeconds": 1
"maxStalenessSeconds": 90
},
"error": true
"suitable_servers": [],
"in_latency_window": []
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# maxStalenessSeconds must be at least 90 seconds, even with no known servers.
# valid maxStalenessSeconds and no known servers results in an empty set of suitable servers
---
topology_description:
type: ReplicaSetNoPrimary
Expand All @@ -11,5 +11,6 @@ topology_description:
type: Unknown
read_preference:
mode: Nearest
maxStalenessSeconds: 1 # Too small.
error: true
maxStalenessSeconds: 90
suitable_servers: []
in_latency_window: []

0 comments on commit dd24cee

Please sign in to comment.