Skip to content

Commit 65c0c81

Browse files
author
rstam
committed
CSHARP-735: When connected directly to a secondary do not coerce ReadPreference to Primary for commands.
1 parent 77bf751 commit 65c0c81

13 files changed

+114
-10
lines changed

MongoDB.Driver/Communication/Proxies/DirectMongoServerProxy.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public ReadOnlyCollection<MongoServerInstance> Instances
8686
get { return new List<MongoServerInstance> { _instance }.AsReadOnly(); }
8787
}
8888

89+
/// <summary>
90+
/// Gets the type of the proxy.
91+
/// </summary>
92+
public MongoServerProxyType ProxyType
93+
{
94+
get { return MongoServerProxyType.Direct; }
95+
}
96+
8997
/// <summary>
9098
/// Gets the state.
9199
/// </summary>

MongoDB.Driver/Communication/Proxies/DiscoveringMongoServerProxy.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ public ReadOnlyCollection<MongoServerInstance> Instances
9696
}
9797
}
9898

99+
/// <summary>
100+
/// Gets the type of the proxy.
101+
/// </summary>
102+
public MongoServerProxyType ProxyType
103+
{
104+
get
105+
{
106+
if (_serverProxy == null)
107+
{
108+
return MongoServerProxyType.Unknown;
109+
}
110+
111+
return _serverProxy.ProxyType;
112+
}
113+
}
114+
99115
/// <summary>
100116
/// Gets the name of the replica set (null if not connected to a replica set).
101117
/// </summary>

MongoDB.Driver/Communication/Proxies/IMongoServerProxy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ internal interface IMongoServerProxy
3838
/// </summary>
3939
ReadOnlyCollection<MongoServerInstance> Instances { get; }
4040

41+
/// <summary>
42+
/// Gets the type of the proxy.
43+
/// </summary>
44+
MongoServerProxyType ProxyType { get; }
45+
4146
/// <summary>
4247
/// Gets the state.
4348
/// </summary>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright 2010-2013 10gen 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.ObjectModel;
18+
19+
namespace MongoDB.Driver.Internal
20+
{
21+
/// <summary>
22+
/// Represents the type of an IMongoServerProxy.
23+
/// </summary>
24+
internal enum MongoServerProxyType
25+
{
26+
/// <summary>
27+
/// The type of the proxy is not yet known.
28+
/// </summary>
29+
Unknown,
30+
/// <summary>
31+
/// A proxy to a single node.
32+
/// </summary>
33+
Direct,
34+
/// <summary>
35+
/// A proxy to a replica set.
36+
/// </summary>
37+
ReplicaSet,
38+
/// <summary>
39+
/// A proxy to a sharded router.
40+
/// </summary>
41+
Sharded
42+
}
43+
}

MongoDB.Driver/Communication/Proxies/MultipleInstanceMongoServerProxy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public ReadOnlyCollection<MongoServerInstance> Instances
125125
}
126126
}
127127

128+
/// <summary>
129+
/// Gets the type of the proxy.
130+
/// </summary>
131+
public abstract MongoServerProxyType ProxyType { get; }
132+
128133
/// <summary>
129134
/// Gets the state.
130135
/// </summary>

MongoDB.Driver/Communication/Proxies/ReplicaSetMongoServerProxy.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public ReplicaSetMongoServerProxy(MongoServerSettings serverSettings, IEnumerabl
5353
{ }
5454

5555
// public properties
56+
/// <summary>
57+
/// Gets the type of the proxy.
58+
/// </summary>
59+
public override MongoServerProxyType ProxyType
60+
{
61+
get { return MongoServerProxyType.ReplicaSet; }
62+
}
63+
5664
/// <summary>
5765
/// Gets the name of the replica set.
5866
/// </summary>

MongoDB.Driver/Communication/Proxies/ShardedMongoServerProxy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public ShardedMongoServerProxy(MongoServerSettings settings, IEnumerable<MongoSe
4848
: base(settings, instances, stateChangedQueue, connectionAttempt)
4949
{ }
5050

51+
// public properties
52+
/// <summary>
53+
/// Gets the type of the proxy.
54+
/// </summary>
55+
public override MongoServerProxyType ProxyType
56+
{
57+
get { return MongoServerProxyType.Sharded; }
58+
}
59+
5160
// protected methods
5261
/// <summary>
5362
/// Chooses the server instance.

MongoDB.Driver/MongoCollection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,9 +1741,12 @@ private TCommandResult RunCommandAs<TCommandResult>(
17411741
GuidRepresentation = _settings.GuidRepresentation
17421742
};
17431743
var readPreference = _settings.ReadPreference;
1744-
if (readPreference != ReadPreference.Primary && !CanCommandBeSentToSecondary.Delegate(command.ToBsonDocument()))
1744+
if (readPreference != ReadPreference.Primary)
17451745
{
1746-
readPreference = ReadPreference.Primary;
1746+
if (_server.ProxyType == MongoServerProxyType.ReplicaSet && !CanCommandBeSentToSecondary.Delegate(command.ToBsonDocument()))
1747+
{
1748+
readPreference = ReadPreference.Primary;
1749+
}
17471750
}
17481751
var flags = (readPreference == ReadPreference.Primary) ? QueryFlags.None : QueryFlags.SlaveOk;
17491752

MongoDB.Driver/MongoCursor.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,7 @@ public virtual IEnumerator<TDocument> GetEnumerator()
732732

733733
if (readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary && Collection.Name == "$cmd")
734734
{
735-
var queryDocument = Query.ToBsonDocument();
736-
var isSecondaryOk = CanCommandBeSentToSecondary.Delegate(queryDocument);
737-
if (!isSecondaryOk)
735+
if (Server.ProxyType == MongoServerProxyType.ReplicaSet && !CanCommandBeSentToSecondary.Delegate(Query.ToBsonDocument()))
738736
{
739737
// if the command can't be sent to a secondary, then we use primary here
740738
// regardless of the user's choice.

MongoDB.Driver/MongoCursorEnumerator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public MongoCursorEnumerator(MongoCursor<TDocument> cursor)
5757

5858
if (_readPreference.ReadPreferenceMode != ReadPreferenceMode.Primary && _cursor.Collection.Name == "$cmd")
5959
{
60-
var queryDocument = _cursor.Query.ToBsonDocument();
61-
var isSecondaryOk = CanCommandBeSentToSecondary.Delegate(queryDocument);
62-
if (!isSecondaryOk)
60+
if (cursor.Server.ProxyType == MongoServerProxyType.ReplicaSet && !CanCommandBeSentToSecondary.Delegate(_cursor.Query.ToBsonDocument()))
6361
{
6462
// if the command can't be sent to a secondary, then we use primary here
6563
// regardless of the user's choice.

0 commit comments

Comments
 (0)