Skip to content

Commit 46f39d5

Browse files
authored
CSHARP-4017: Fix flaky Aggregate_with__out_includes_read_preference_for_5.0__server error (#1391)
1 parent 41e352e commit 46f39d5

File tree

8 files changed

+128
-7
lines changed

8 files changed

+128
-7
lines changed

src/MongoDB.Driver.Core/Core/Clusters/ServerSelectors/WritableServerSelector.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public override string ToString()
9898

9999
private bool CanUseSecondaries(ClusterDescription cluster, List<ServerDescription> servers)
100100
{
101-
if (_mayUseSecondary?.ReadPreference == null || servers.Count == 0)
101+
if (_mayUseSecondary?.ReadPreference == null)
102102
{
103103
return false;
104104
}
@@ -107,6 +107,11 @@ private bool CanUseSecondaries(ClusterDescription cluster, List<ServerDescriptio
107107
{
108108
case ClusterType.ReplicaSet:
109109
case ClusterType.Sharded:
110+
if (servers.Count == 0)
111+
{
112+
return true;
113+
}
114+
110115
return servers.All(s => _mayUseSecondary.CanUseSecondary(s));
111116

112117
case ClusterType.LoadBalanced:

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ServerSelectors/CompositeServerSelectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ServerSelectors/DelegateServerSelectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ServerSelectors/EndPointServerSelectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ServerSelectors/LatencyLimitingServerSelectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ServerSelectors/RandomServerSelectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ServerSelectors/ReadPreferenceServerSelectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Net;
19+
using FluentAssertions;
20+
using MongoDB.Driver.Core.Bindings;
21+
using MongoDB.Driver.Core.Helpers;
22+
using MongoDB.Driver.Core.Misc;
23+
using MongoDB.Driver.Core.Operations;
24+
using MongoDB.Driver.Core.Servers;
25+
using Xunit;
26+
27+
namespace MongoDB.Driver.Core.Clusters.ServerSelectors
28+
{
29+
public class WritableServerSelectorTests
30+
{
31+
[Theory]
32+
[MemberData(nameof(ReplicaSetTestCases))]
33+
public void WritableServerSelector_should_work(
34+
ReadPreference readPreference,
35+
ClusterDescription cluster,
36+
ReadPreference expectedReadPreference,
37+
IEnumerable<ServerDescription> expectedServers)
38+
{
39+
IMayUseSecondaryCriteria mayUseSecondary = null;
40+
if (readPreference != null)
41+
{
42+
mayUseSecondary = new AggregateToCollectionOperation.MayUseSecondary(readPreference);
43+
}
44+
45+
var selector = new WritableServerSelector(mayUseSecondary);
46+
var results = selector.SelectServers(cluster, cluster.Servers);
47+
48+
results.Should().BeEquivalentTo(expectedServers);
49+
if (readPreference != null)
50+
{
51+
selector.MayUseSecondary.EffectiveReadPreference.Should().Be(expectedReadPreference);
52+
}
53+
}
54+
55+
public static IEnumerable<object[]> ReplicaSetTestCases()
56+
{
57+
var clusterId = new ClusterId();
58+
var primary = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), ServerType.ReplicaSetPrimary, wireVersionRange: new Range<int>(WireVersion.Server70, WireVersion.Server70));
59+
var secondary1Server70 = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), ServerType.ReplicaSetSecondary, wireVersionRange: new Range<int>(WireVersion.Server70, WireVersion.Server70));
60+
var secondary2Server70 = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019), ServerType.ReplicaSetSecondary, wireVersionRange: new Range<int>(WireVersion.Server70, WireVersion.Server70));
61+
var secondary3Server42 = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27020), ServerType.ReplicaSetSecondary, wireVersionRange: new Range<int>(WireVersion.Server42, WireVersion.Server42));
62+
63+
yield return new object[]
64+
{
65+
ReadPreference.SecondaryPreferred,
66+
new ClusterDescription(
67+
clusterId,
68+
false,
69+
null,
70+
ClusterType.ReplicaSet,
71+
Array.Empty<ServerDescription>()),
72+
ReadPreference.SecondaryPreferred,
73+
Array.Empty<ServerDescription>()
74+
};
75+
76+
yield return new object[]
77+
{
78+
ReadPreference.SecondaryPreferred,
79+
new ClusterDescription(
80+
clusterId,
81+
false,
82+
null,
83+
ClusterType.ReplicaSet,
84+
new[] { primary }),
85+
ReadPreference.SecondaryPreferred,
86+
new[] { primary }
87+
};
88+
89+
yield return new object[]
90+
{
91+
ReadPreference.SecondaryPreferred,
92+
new ClusterDescription(
93+
clusterId,
94+
false,
95+
null,
96+
ClusterType.ReplicaSet,
97+
new[] { primary, secondary1Server70, secondary2Server70 }),
98+
ReadPreference.SecondaryPreferred,
99+
new[] { secondary1Server70, secondary2Server70 }
100+
};
101+
102+
yield return new object[]
103+
{
104+
ReadPreference.SecondaryPreferred,
105+
new ClusterDescription(
106+
clusterId,
107+
false,
108+
null,
109+
ClusterType.ReplicaSet,
110+
new[] { primary, secondary1Server70, secondary2Server70, secondary3Server42 }),
111+
ReadPreference.Primary,
112+
new[] { primary }
113+
};
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)