Skip to content

Commit a6897dc

Browse files
authored
DRIVERS-2972: add message requirement to ServerDescription.error (#1729)
1 parent d9b434d commit a6897dc

20 files changed

+90
-43
lines changed

source/server-discovery-and-monitoring/server-discovery-and-monitoring.md

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ Fields:
226226
field in the server's hello or legacy hello response, in the case that the server reports an address different from
227227
the address the client uses.
228228

229-
- (=) `error`: information about the last error related to this server. Default null.
229+
- (=) `error`: information about the last error related to this server. Default null. MUST contain or be able to produce
230+
a string describing the error.
230231

231232
- `roundTripTime`: the duration of the hello or legacy hello call. Default null.
232233

@@ -485,7 +486,13 @@ removed once the primary is checked.
485486
#### error
486487

487488
If the client experiences any error when checking a server, it stores error information in the ServerDescription's error
488-
field.
489+
field. The message contained in this field MUST contain the substrings detailed in the table below when the
490+
ServerDescription is changed to Unknown in the circumstances outlined.
491+
492+
| circumstance | error substring |
493+
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
494+
| RSPrimary with a stale electionId/setVersion is discovered | `'primary marked stale due to electionId/setVersion mismatch, <stale tuple> is stale compared to <max tuple>'` |
495+
| New primary is elected/discovered | `'primary marked stale due to discovery of newer primary'` |
489496

490497
#### roundTripTime
491498

@@ -871,7 +878,8 @@ if serverDescription.maxWireVersion >= 17: # MongoDB 6.0+
871878
topologyDescription.maxSetVersion = serverDescription.setVersion
872879
else:
873880
# Stale primary.
874-
# replace serverDescription with a default ServerDescription of type "Unknown"
881+
# The error field MUST include the substring "primary marked stale due to electionId/setVersion mismatch"
882+
replace serverDescription with a default ServerDescription of type "Unknown"
875883
checkIfHasPrimary()
876884
return
877885
else:
@@ -889,7 +897,8 @@ else:
889897
)
890898
):
891899
# Stale primary.
892-
# replace serverDescription with a default ServerDescription of type "Unknown"
900+
# The error field MUST include the substring "primary marked stale due to electionId/setVersion mismatch"
901+
replace serverDescription with a default ServerDescription of type "Unknown"
893902
checkIfHasPrimary()
894903
return
895904

@@ -906,6 +915,7 @@ for each server in topologyDescription.servers:
906915
if server.address != serverDescription.address:
907916
if server.type is RSPrimary:
908917
# See note below about invalidating an old primary.
918+
# the error field MUST include the substring "primary marked stale due to discovery of newer primary"
909919
replace the server with a default ServerDescription of type "Unknown"
910920

911921
for each address in serverDescription's "hosts", "passives", and "arbiters":
@@ -921,9 +931,10 @@ checkIfHasPrimary()
921931
```
922932

923933
A note on invalidating the old primary: when a new primary is discovered, the client finds the previous primary (there
924-
should be none or one) and replaces its description with a default ServerDescription of type "Unknown." A multi-threaded
925-
client MUST [request an immediate check](server-monitoring.md#requesting-an-immediate-check) for that server as soon as
926-
possible.
934+
should be none or one) and replaces its description with a default ServerDescription of type "Unknown". Additionally,
935+
the `error` field of the new `ServerDescription` object MUST include a descriptive error explaining that it was
936+
invalidated because the primary was determined to be stale. A multi-threaded client MUST
937+
[request an immediate check](server-monitoring.md#requesting-an-immediate-check) for that server as soon as possible.
927938

928939
If the old primary server version is 4.0 or earlier, the client MUST clear its connection pool for the old primary, too:
929940
the connections are all bad because the old primary has closed its sockets. If the old primary server version is 4.2 or
@@ -934,6 +945,8 @@ See [replica set monitoring with and without a primary](#replica-set-monitoring-
934945
If the server is primary with an obsolete electionId or setVersion, it is likely a stale primary that is going to step
935946
down. Mark it Unknown and let periodic monitoring detect when it becomes secondary. See
936947
[using electionId and setVersion to detect stale primaries](#using-electionid-and-setversion-to-detect-stale-primaries).
948+
Drivers MAY additionally specify whether this was due to an electionId or setVersion mismatch as described in the
949+
[ServerDescripion.error section](#error).
937950

938951
A note on checking "me": Unlike `updateRSWithPrimaryFromMember`, there is no need to remove the server if the address is
939952
not equal to "me": since the server address will not be a member of either "hosts", "passives", or "arbiters", the
@@ -1921,16 +1934,6 @@ oversaw the specification process.
19211934

19221935
## Changelog
19231936

1924-
- 2024-11-11: Removed references to `getLastError`
1925-
1926-
- 2024-11-04: Make the description of `TopologyDescription.servers` consistent with the spec tests.
1927-
1928-
- 2024-08-16: Updated host b wire versions in `too_new` and `too_old` tests
1929-
1930-
- 2024-08-09: Updated wire versions in tests to 4.0+.
1931-
1932-
- 2024-05-08: Migrated from reStructuredText to Markdown.
1933-
19341937
- 2015-12-17: Require clients to compare (setVersion, electionId) tuples.
19351938

19361939
- 2015-10-09: Specify electionID comparison method.
@@ -2012,6 +2015,19 @@ oversaw the specification process.
20122015

20132016
- 2024-01-17: Add section on expected client close behaviour
20142017

2018+
- 2024-05-08: Migrated from reStructuredText to Markdown.
2019+
2020+
- 2024-08-09: Updated wire versions in tests to 4.0+.
2021+
2022+
- 2024-08-16: Updated host b wire versions in `too_new` and `too_old` tests
2023+
2024+
- 2024-11-04: Make the description of `TopologyDescription.servers` consistent with the spec tests.
2025+
2026+
- 2024-11-11: Removed references to `getLastError`
2027+
2028+
- 2025-01-22: Add error messages when a new primary is elected or a primary with a stale electionId or setVersion is
2029+
discovered.
2030+
20152031
______________________________________________________________________
20162032

20172033
[^1]: "localThresholdMS" was called "secondaryAcceptableLatencyMS" in the Read Preferences Spec, before it was superseded

source/server-discovery-and-monitoring/tests/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ following keys:
7171

7272
- type: A ServerType name, like "RSSecondary". See [ServerType](../server-discovery-and-monitoring.md#servertype) for
7373
details pertaining to async and multi-threaded drivers.
74+
- error: An optional string that must be a substring of the message on the `ServerDescription.error` object
7475
- setName: A string with the expected replica set name, or null.
7576
- setVersion: absent or an integer.
7677
- electionId: absent, null, or an ObjectId.

source/server-discovery-and-monitoring/tests/rs/new_primary.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/new_primary.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ phases: [
6363
"a:27017": {
6464

6565
type: "Unknown",
66-
setName:
66+
setName:,
67+
error: "primary marked stale due to discovery of newer primary"
6768
},
6869

6970
"b:27017": {

source/server-discovery-and-monitoring/tests/rs/new_primary_new_electionid.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/new_primary_new_electionid.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ phases: [
6363
"a:27017": {
6464
type: "Unknown",
6565
setName: ,
66-
electionId:
66+
electionId: ,
67+
error: "primary marked stale due to discovery of newer primary"
6768
},
6869
"b:27017": {
6970
type: "RSPrimary",
@@ -100,7 +101,8 @@ phases: [
100101
"a:27017": {
101102
type: "Unknown",
102103
setName: ,
103-
electionId:
104+
electionId:,
105+
error: "primary marked stale due to electionId/setVersion mismatch"
104106
},
105107
"b:27017": {
106108
type: "RSPrimary",

source/server-discovery-and-monitoring/tests/rs/new_primary_new_setversion.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/new_primary_new_setversion.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ phases: [
6363
"a:27017": {
6464
type: "Unknown",
6565
setName: ,
66-
electionId:
66+
electionId:,
67+
error: "primary marked stale due to discovery of newer primary"
6768
},
6869
"b:27017": {
6970
type: "RSPrimary",
@@ -100,7 +101,8 @@ phases: [
100101
"a:27017": {
101102
type: "Unknown",
102103
setName: ,
103-
electionId:
104+
electionId:,
105+
error: "primary marked stale due to electionId/setVersion mismatch"
104106
},
105107
"b:27017": {
106108
type: "RSPrimary",

source/server-discovery-and-monitoring/tests/rs/primary_disconnect_electionid.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/primary_disconnect_electionid.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ phases: [
3636
"a:27017": {
3737
type: "Unknown",
3838
setName: ,
39-
electionId:
39+
electionId:,
40+
error: "primary marked stale due to discovery of newer primary"
4041
},
4142
"b:27017": {
4243
type: "RSPrimary",
@@ -99,6 +100,7 @@ phases: [
99100
"a:27017": {
100101
type: "Unknown",
101102
setName: ,
103+
error: "primary marked stale due to electionId/setVersion mismatch",
102104
electionId:
103105
},
104106
"b:27017": {

source/server-discovery-and-monitoring/tests/rs/primary_disconnect_setversion.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/primary_disconnect_setversion.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ phases: [
3636
"a:27017": {
3737
type: "Unknown",
3838
setName: ,
39-
electionId:
39+
electionId:,
40+
error: "primary marked stale due to discovery of newer primary"
4041
},
4142
"b:27017": {
4243
type: "RSPrimary",
@@ -99,6 +100,7 @@ phases: [
99100
"a:27017": {
100101
type: "Unknown",
101102
setName: ,
103+
error: "primary marked stale due to electionId/setVersion mismatch",
102104
electionId:
103105
},
104106
"b:27017": {

source/server-discovery-and-monitoring/tests/rs/setversion_greaterthan_max_without_electionid.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/setversion_greaterthan_max_without_electionid.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ phases: [
6161
"a:27017": {
6262
type: "Unknown",
6363
setName: ,
64-
electionId:
64+
electionId:,
65+
error: "primary marked stale due to discovery of newer primary"
6566
},
6667
"b:27017": {
6768
type: "RSPrimary",

source/server-discovery-and-monitoring/tests/rs/setversion_without_electionid-pre-6.0.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/setversion_without_electionid-pre-6.0.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ phases: [
6161
"a:27017": {
6262
type: "Unknown",
6363
setName: ,
64-
electionId:
64+
electionId:,
65+
error: "primary marked stale due to discovery of newer primary"
6566
},
6667
"b:27017": {
6768
type: "RSPrimary",

source/server-discovery-and-monitoring/tests/rs/use_setversion_without_electionid-pre-6.0.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/server-discovery-and-monitoring/tests/rs/use_setversion_without_electionid-pre-6.0.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ phases: [
6262
"a:27017": {
6363
type: "Unknown",
6464
setName: ,
65-
electionId:
65+
electionId:,
66+
error: "primary marked stale due to discovery of newer primary"
6667
},
6768
"b:27017": {
6869
type: "RSPrimary",
@@ -99,7 +100,8 @@ phases: [
99100
"a:27017": {
100101
type: "Unknown",
101102
setName: ,
102-
electionId:
103+
electionId:,
104+
error: "primary marked stale due to electionId/setVersion mismatch"
103105
},
104106
"b:27017": {
105107
type: "RSPrimary",

source/server-discovery-and-monitoring/tests/rs/use_setversion_without_electionid.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)