Skip to content

Commit 32b085a

Browse files
CSHARP-2881: Verify max set version and max election id on topologies in SDAM spec tests
1 parent aae2823 commit 32b085a

18 files changed

+228
-32
lines changed

src/MongoDB.Driver.Core/Core/Clusters/MultiServerCluster.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,16 @@ private ClusterDescription ProcessReplicaSetChange(ClusterDescription clusterDes
417417
args.NewServerDescription.ReplicaSetConfig.Name,
418418
args.NewServerDescription.EndPoint,
419419
args.NewServerDescription.ReplicaSetConfig.Name)));
420+
421+
_maxElectionInfo = new ElectionInfo(
422+
args.NewServerDescription.ReplicaSetConfig.Version.Value,
423+
args.NewServerDescription.ElectionId);
420424
}
421425
else
422426
{
423427
if (_maxElectionInfo.SetVersion < args.NewServerDescription.ReplicaSetConfig.Version.Value)
424428
{
429+
var electionId = args.NewServerDescription.ElectionId ?? _maxElectionInfo.ElectionId;
425430
_sdamInformationEventHandler?.Invoke(new SdamInformationEvent(() =>
426431
string.Format(
427432
@"Updating stale setVersion: Updating the current " +
@@ -431,11 +436,15 @@ private ClusterDescription ProcessReplicaSetChange(ClusterDescription clusterDes
431436
_maxElectionInfo.SetVersion,
432437
_maxElectionInfo.ElectionId,
433438
args.NewServerDescription.ReplicaSetConfig.Version,
434-
args.NewServerDescription.ElectionId,
439+
electionId,
435440
args.NewServerDescription.ReplicaSetConfig.Name,
436441
args.NewServerDescription.EndPoint,
437442
args.NewServerDescription.ReplicaSetConfig.Version,
438443
args.NewServerDescription.ElectionId)));
444+
445+
_maxElectionInfo = new ElectionInfo(
446+
args.NewServerDescription.ReplicaSetConfig.Version.Value,
447+
electionId);
439448
}
440449
else // current primary is stale & setVersion is not stale ⇒ the electionId must be stale
441450
{
@@ -453,12 +462,12 @@ private ClusterDescription ProcessReplicaSetChange(ClusterDescription clusterDes
453462
args.NewServerDescription.EndPoint,
454463
args.NewServerDescription.ReplicaSetConfig.Version,
455464
args.NewServerDescription.ElectionId)));
465+
466+
_maxElectionInfo = new ElectionInfo(
467+
args.NewServerDescription.ReplicaSetConfig.Version.Value,
468+
args.NewServerDescription.ElectionId);
456469
}
457470
}
458-
459-
_maxElectionInfo = new ElectionInfo(
460-
args.NewServerDescription.ReplicaSetConfig.Version.Value,
461-
args.NewServerDescription.ElectionId);
462471
}
463472
}
464473

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/TestRunner.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private void VerifyTopology(ICluster cluster, string expectedType)
130130

131131
private void VerifyOutcome(BsonDocument outcome)
132132
{
133-
JsonDrivenHelper.EnsureAllFieldsAreValid(outcome, "compatible", "logicalSessionTimeoutMinutes", "servers", "setName", "topologyType");
133+
JsonDrivenHelper.EnsureAllFieldsAreValid(outcome, "compatible", "logicalSessionTimeoutMinutes", "servers", "setName", "topologyType", "maxSetVersion", "maxElectionId");
134134

135135
var expectedTopologyType = (string)outcome["topologyType"];
136136
VerifyTopology(_cluster, expectedTopologyType);
@@ -151,6 +151,29 @@ private void VerifyOutcome(BsonDocument outcome)
151151
VerifyServerDescription(actualServer, expectedServer.Description);
152152
}
153153

154+
if (outcome.TryGetValue("maxSetVersion", out var maxSetVersion))
155+
{
156+
if (_cluster is MultiServerCluster multiServerCluster)
157+
{
158+
multiServerCluster._maxElectionInfo_setVersion().Should().Be(maxSetVersion.AsInt32);
159+
}
160+
else
161+
{
162+
throw new Exception($"Expected MultiServerCluster but got {_cluster.GetType()}");
163+
}
164+
}
165+
if (outcome.TryGetValue("maxElectionId", out var maxElectionId))
166+
{
167+
if (_cluster is MultiServerCluster multiServerCluster)
168+
{
169+
multiServerCluster._maxElectionInfo_electionId().Should().Be(new ElectionId((ObjectId)maxElectionId));
170+
}
171+
else
172+
{
173+
throw new Exception($"Expected MultiServerCluster but got {_cluster.GetType()}");
174+
}
175+
}
176+
154177
if (outcome.Contains("setName"))
155178
{
156179
// TODO: assert something against setName
@@ -290,4 +313,24 @@ protected override bool ShouldReadJsonDocument(string path)
290313
}
291314
}
292315
}
316+
317+
internal static class MultiServerClusterReflector
318+
{
319+
public static int _maxElectionInfo_setVersion(this MultiServerCluster obj)
320+
{
321+
var maxElectionInfo = _maxElectionInfo(obj);
322+
return (int)Reflector.GetFieldValue(maxElectionInfo, "_setVersion");
323+
}
324+
325+
public static ElectionId _maxElectionInfo_electionId(this MultiServerCluster obj)
326+
{
327+
var maxElectionInfo = _maxElectionInfo(obj);
328+
return (ElectionId)Reflector.GetFieldValue(maxElectionInfo, "_electionId");
329+
}
330+
331+
private static object _maxElectionInfo(MultiServerCluster obj)
332+
{
333+
return Reflector.GetFieldValue(obj, nameof(_maxElectionInfo));
334+
}
335+
}
293336
}

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/equal_electionids.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@
6060
},
6161
"topologyType": "ReplicaSetWithPrimary",
6262
"logicalSessionTimeoutMinutes": null,
63-
"setName": "rs"
63+
"setName": "rs",
64+
"maxSetVersion": 1,
65+
"maxElectionId": {
66+
"$oid": "000000000000000000000001"
67+
}
6468
}
6569
}
6670
]

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/equal_electionids.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ phases: [
4848
topologyType: "ReplicaSetWithPrimary",
4949
logicalSessionTimeoutMinutes: null,
5050
setName: "rs",
51+
maxSetVersion: 1,
52+
maxElectionId: {"$oid": "000000000000000000000001"},
5153
}
5254
}
5355
]

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/new_primary_new_electionid.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141
},
4242
"topologyType": "ReplicaSetWithPrimary",
4343
"logicalSessionTimeoutMinutes": null,
44-
"setName": "rs"
44+
"setName": "rs",
45+
"maxSetVersion": 1,
46+
"maxElectionId": {
47+
"$oid": "000000000000000000000001"
48+
}
4549
}
4650
},
4751
{
@@ -83,7 +87,11 @@
8387
},
8488
"topologyType": "ReplicaSetWithPrimary",
8589
"logicalSessionTimeoutMinutes": null,
86-
"setName": "rs"
90+
"setName": "rs",
91+
"maxSetVersion": 1,
92+
"maxElectionId": {
93+
"$oid": "000000000000000000000002"
94+
}
8795
}
8896
},
8997
{
@@ -125,7 +133,11 @@
125133
},
126134
"topologyType": "ReplicaSetWithPrimary",
127135
"logicalSessionTimeoutMinutes": null,
128-
"setName": "rs"
136+
"setName": "rs",
137+
"maxSetVersion": 1,
138+
"maxElectionId": {
139+
"$oid": "000000000000000000000002"
140+
}
129141
}
130142
}
131143
]

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/new_primary_new_electionid.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ phases: [
3636
topologyType: "ReplicaSetWithPrimary",
3737
logicalSessionTimeoutMinutes: null,
3838
setName: "rs",
39+
maxSetVersion: 1,
40+
maxElectionId: {"$oid": "000000000000000000000001"},
3941
}
4042
},
4143

@@ -71,6 +73,8 @@ phases: [
7173
topologyType: "ReplicaSetWithPrimary",
7274
logicalSessionTimeoutMinutes: null,
7375
setName: "rs",
76+
maxSetVersion: 1,
77+
maxElectionId: {"$oid": "000000000000000000000002"},
7478
}
7579
},
7680

@@ -105,6 +109,8 @@ phases: [
105109
topologyType: "ReplicaSetWithPrimary",
106110
logicalSessionTimeoutMinutes: null,
107111
setName: "rs",
112+
maxSetVersion: 1,
113+
maxElectionId: {"$oid": "000000000000000000000002"},
108114
}
109115
}
110116
]

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/new_primary_new_setversion.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141
},
4242
"topologyType": "ReplicaSetWithPrimary",
4343
"logicalSessionTimeoutMinutes": null,
44-
"setName": "rs"
44+
"setName": "rs",
45+
"maxSetVersion": 1,
46+
"maxElectionId": {
47+
"$oid": "000000000000000000000001"
48+
}
4549
}
4650
},
4751
{
@@ -83,7 +87,11 @@
8387
},
8488
"topologyType": "ReplicaSetWithPrimary",
8589
"logicalSessionTimeoutMinutes": null,
86-
"setName": "rs"
90+
"setName": "rs",
91+
"maxSetVersion": 2,
92+
"maxElectionId": {
93+
"$oid": "000000000000000000000001"
94+
}
8795
}
8896
},
8997
{
@@ -125,7 +133,11 @@
125133
},
126134
"topologyType": "ReplicaSetWithPrimary",
127135
"logicalSessionTimeoutMinutes": null,
128-
"setName": "rs"
136+
"setName": "rs",
137+
"maxSetVersion": 2,
138+
"maxElectionId": {
139+
"$oid": "000000000000000000000001"
140+
}
129141
}
130142
}
131143
]

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/new_primary_new_setversion.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ phases: [
3636
topologyType: "ReplicaSetWithPrimary",
3737
logicalSessionTimeoutMinutes: null,
3838
setName: "rs",
39+
maxSetVersion: 1,
40+
maxElectionId: {"$oid": "000000000000000000000001"},
3941
}
4042
},
4143

@@ -71,6 +73,8 @@ phases: [
7173
topologyType: "ReplicaSetWithPrimary",
7274
logicalSessionTimeoutMinutes: null,
7375
setName: "rs",
76+
maxSetVersion: 2,
77+
maxElectionId: {"$oid": "000000000000000000000001"},
7478
}
7579
},
7680

@@ -105,6 +109,8 @@ phases: [
105109
topologyType: "ReplicaSetWithPrimary",
106110
logicalSessionTimeoutMinutes: null,
107111
setName: "rs",
112+
maxSetVersion: 2,
113+
maxElectionId: {"$oid": "000000000000000000000001"},
108114
}
109115
}
110116
]

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/null_election_id.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
},
4343
"topologyType": "ReplicaSetWithPrimary",
4444
"logicalSessionTimeoutMinutes": null,
45-
"setName": "rs"
45+
"setName": "rs",
46+
"maxSetVersion": 1
4647
}
4748
},
4849
{
@@ -90,7 +91,11 @@
9091
},
9192
"topologyType": "ReplicaSetWithPrimary",
9293
"logicalSessionTimeoutMinutes": null,
93-
"setName": "rs"
94+
"setName": "rs",
95+
"maxSetVersion": 1,
96+
"maxElectionId": {
97+
"$oid": "000000000000000000000002"
98+
}
9499
}
95100
},
96101
{
@@ -133,7 +138,11 @@
133138
},
134139
"topologyType": "ReplicaSetWithPrimary",
135140
"logicalSessionTimeoutMinutes": null,
136-
"setName": "rs"
141+
"setName": "rs",
142+
"maxSetVersion": 1,
143+
"maxElectionId": {
144+
"$oid": "000000000000000000000002"
145+
}
137146
}
138147
},
139148
{
@@ -179,7 +188,11 @@
179188
},
180189
"topologyType": "ReplicaSetWithPrimary",
181190
"logicalSessionTimeoutMinutes": null,
182-
"setName": "rs"
191+
"setName": "rs",
192+
"maxSetVersion": 1,
193+
"maxElectionId": {
194+
"$oid": "000000000000000000000002"
195+
}
183196
}
184197
}
185198
]

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/tests/rs/null_election_id.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ phases: [
4040
topologyType: "ReplicaSetWithPrimary",
4141
logicalSessionTimeoutMinutes: null,
4242
setName: "rs",
43+
maxSetVersion: 1,
4344
}
4445
},
4546

@@ -80,6 +81,8 @@ phases: [
8081
topologyType: "ReplicaSetWithPrimary",
8182
logicalSessionTimeoutMinutes: null,
8283
setName: "rs",
84+
maxSetVersion: 1,
85+
maxElectionId: {"$oid": "000000000000000000000002"},
8386
}
8487
},
8588

@@ -118,6 +121,8 @@ phases: [
118121
topologyType: "ReplicaSetWithPrimary",
119122
logicalSessionTimeoutMinutes: null,
120123
setName: "rs",
124+
maxSetVersion: 1,
125+
maxElectionId: {"$oid": "000000000000000000000002"},
121126
}
122127
},
123128

@@ -159,6 +164,8 @@ phases: [
159164
topologyType: "ReplicaSetWithPrimary",
160165
logicalSessionTimeoutMinutes: null,
161166
setName: "rs",
167+
maxSetVersion: 1,
168+
maxElectionId: {"$oid": "000000000000000000000002"},
162169
}
163170
}
164171
]

0 commit comments

Comments
 (0)