Skip to content

Commit 6f5b3d2

Browse files
authored
CSHARP-4808: "Updating stale electionId and setVersion" even when electionId/setVersion match (#1201)
1 parent dc92a39 commit 6f5b3d2

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,12 @@ public ElectionInfo(int? setVersion, ElectionId electionId)
658658
public int? SetVersion => _setVersion;
659659
public ElectionId ElectionId => _electionId;
660660

661-
public ElectionInfo Compare(int? setVersion, ElectionId electionId, int maxWireVerion)
661+
public ElectionInfo Compare(int? setVersion, ElectionId electionId, int maxWireVersion)
662662
{
663663
var newElectionId = _electionId;
664664
var newSetVersion = _setVersion;
665665

666-
if (Feature.ElectionIdPriorityInSDAM.IsSupported(maxWireVerion))
666+
if (Feature.ElectionIdPriorityInSDAM.IsSupported(maxWireVersion))
667667
{
668668
var electionIdOrder = Compare(electionId);
669669
var isGreaterOrEqual = electionIdOrder < 0 || electionIdOrder == 0 && IsGreaterOrEqual(setVersion, _setVersion);
@@ -707,7 +707,9 @@ public override bool Equals(object obj)
707707
{
708708
var other = obj as ElectionInfo;
709709

710-
return other != null && _setVersion == other.SetVersion && other.ElectionId == _electionId;
710+
return other != null &&
711+
_setVersion == other.SetVersion &&
712+
object.Equals(_electionId, other.ElectionId);
711713
}
712714

713715
private int Compare(ElectionId electionId)

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ElectionIdTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void Equals_should_return_false_if_any_field_is_not_equal()
4848
}
4949

5050
[Fact]
51-
public void Equals_should_return_true_if_all_fiels_are_equal()
51+
public void Equals_should_return_true_if_all_fields_are_equal()
5252
{
5353
var subject1 = new ElectionId(ObjectId.Empty);
5454
var subject2 = new ElectionId(ObjectId.Empty);

tests/MongoDB.Driver.Core.Tests/Core/Clusters/MultiServerClusterTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,31 @@ public void Should_remove_a_server_whose_canonical_end_point_does_not_match_its_
685685
_capturedEvents.Any().Should().BeFalse();
686686
}
687687

688+
[Fact]
689+
public void Should_not_invalidate_when_set_version_and_electionid_not_changed()
690+
{
691+
_settings = _settings.With(endPoints: new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint });
692+
var setVersion = 1;
693+
var objectId = ObjectId.GenerateNewId().ToString();
694+
695+
var subject = CreateSubject();
696+
subject.Initialize();
697+
_capturedEvents.Clear();
698+
699+
PublishDescription(subject, _firstEndPoint, ServerType.ReplicaSetPrimary, setVersion: setVersion, electionId: new ElectionId(ObjectId.Parse(objectId)));
700+
701+
var description = subject.Description;
702+
description.State.Should().Be(ClusterState.Connected);
703+
description.Type.Should().Be(ClusterType.ReplicaSet);
704+
description.Servers.Should().BeEquivalentToWithComparer(GetDescriptions(_firstEndPoint, _secondEndPoint, _thirdEndPoint), _serverDescriptionComparer);
705+
706+
PublishDescription(subject, _firstEndPoint, ServerType.ReplicaSetPrimary, setVersion: setVersion, electionId: new ElectionId(ObjectId.Parse(objectId)));
707+
708+
_capturedEvents.Next().Should().BeOfType<SdamInformationEvent>();
709+
_capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
710+
_capturedEvents.Any().Should().BeFalse();
711+
}
712+
688713
[Fact]
689714
public void Should_not_remove_a_server_that_is_no_longer_in_a_secondaries_host_list()
690715
{

0 commit comments

Comments
 (0)