Skip to content

Commit d2eda40

Browse files
committed
CSHARP-2632: Don't send $readPreference to standalone servers
1 parent 4696ab8 commit d2eda40

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingCommandMessageWireProtocol.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using MongoDB.Driver.Core.Connections;
2828
using MongoDB.Driver.Core.Misc;
2929
using MongoDB.Driver.Core.Operations;
30+
using MongoDB.Driver.Core.Servers;
3031
using MongoDB.Driver.Core.WireProtocol.Messages;
3132
using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
3233

@@ -190,7 +191,9 @@ private Type0CommandMessageSection<BsonDocument> CreateType0Section(ConnectionDe
190191
var dbElement = new BsonElement("$db", _databaseNamespace.DatabaseName);
191192
extraElements.Add(dbElement);
192193

193-
if (_readPreference != null && _readPreference != ReadPreference.Primary)
194+
if (connectionDescription.IsMasterResult.ServerType != ServerType.Standalone
195+
&& _readPreference != null
196+
&& _readPreference != ReadPreference.Primary)
194197
{
195198
var readPreferenceDocument = QueryHelper.CreateReadPreferenceDocument(_readPreference);
196199
var readPreferenceElement = new BsonElement("$readPreference", readPreferenceDocument);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Copyright 2019-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 FluentAssertions;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.TestHelpers.XunitExtensions;
19+
using MongoDB.Driver.Core;
20+
using MongoDB.Driver.Core.Clusters;
21+
using MongoDB.Driver.Core.Events;
22+
using MongoDB.Driver.Core.Misc;
23+
using MongoDB.Driver.TestHelpers;
24+
using Xunit;
25+
26+
namespace MongoDB.Driver.Tests
27+
{
28+
public class ReadPreferenceOnStandaloneTests
29+
{
30+
[Theory]
31+
[ParameterAttributeData]
32+
public void ReadPreference_should_not_be_sent_to_standalone_server(
33+
[Values(false, true)] bool async)
34+
{
35+
var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>(e =>
36+
e.CommandName.Equals("find") || e.CommandName.Equals("$query"));
37+
using (var client = CreateDisposableClient(eventCapturer, ReadPreference.PrimaryPreferred))
38+
{
39+
var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
40+
var collection = database.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName);
41+
42+
if (async)
43+
{
44+
var _ = collection.FindAsync("{ x : 2 }").GetAwaiter().GetResult();
45+
}
46+
else
47+
{
48+
var _ = collection.FindSync("{ x : 2 }");
49+
}
50+
51+
CommandStartedEvent sentCommand = ((CommandStartedEvent) eventCapturer.Events[0]);
52+
var serverVersion = client.Cluster.Description.Servers[0].Version;
53+
var clusterType = client.Cluster.Description.Type;
54+
55+
var expectedContainsReadPreference = clusterType == ClusterType.Standalone ||
56+
(clusterType == ClusterType.ReplicaSet && serverVersion < Feature.CommandMessage.FirstSupportedVersion)
57+
? false
58+
: true;
59+
var readPreferenceFieldName = sentCommand.Command.Contains("$readPreference")
60+
? "$readPreference"
61+
: "readPreference";
62+
63+
sentCommand.Command.Contains(readPreferenceFieldName).Should().Be(expectedContainsReadPreference);
64+
}
65+
}
66+
67+
// private methods
68+
private DisposableMongoClient CreateDisposableClient(EventCapturer eventCapturer, ReadPreference readPreference)
69+
{
70+
return DriverTestConfiguration.CreateDisposableClient((MongoClientSettings settings) =>
71+
{
72+
settings.ClusterConfigurator = c => c.Subscribe(eventCapturer);
73+
settings.ReadPreference = readPreference;
74+
});
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)