Skip to content

Commit 108be7e

Browse files
author
rstam
committed
CSHARP-724: Make MongoServer Primary property robust.
1 parent 1472375 commit 108be7e

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

MongoDB.Driver/Communication/Proxies/DiscoveringMongoServerProxy.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ public MongoServerState State
145145
}
146146
}
147147

148+
// internal properties
149+
internal IMongoServerProxy WrappedProxy
150+
{
151+
get { return _serverProxy; }
152+
}
153+
148154
// public methods
149155
/// <summary>
150156
/// Chooses the server instance.

MongoDB.Driver/Communication/Proxies/MultipleInstanceMongoServerProxy.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ protected void MakeInstancesMatchAddresses(IEnumerable<MongoServerAddress> addre
398398
protected virtual void ProcessConnectedInstanceStateChange(MongoServerInstance instance)
399399
{ }
400400

401+
/// <summary>
402+
/// Processes the disconnected instance state change.
403+
/// </summary>
404+
/// <param name="instance">The instance.</param>
405+
protected virtual void ProcessDisconnectedInstanceStateChange(MongoServerInstance instance)
406+
{ }
407+
401408
// private methods
402409
private void AddInstance(MongoServerInstance instance)
403410
{
@@ -472,6 +479,7 @@ private void ProcessInstanceStateChange(MongoServerInstance instance)
472479
else
473480
{
474481
_connectedInstances.Remove(instance);
482+
ProcessDisconnectedInstanceStateChange(instance);
475483
}
476484
}
477485

MongoDB.Driver/Communication/Proxies/ReplicaSetMongoServerProxy.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ internal sealed class ReplicaSetMongoServerProxy : MultipleInstanceMongoServerPr
2828
// private fields
2929
private readonly Random _random = new Random();
3030
private readonly object _randomLock = new object();
31+
private MongoServerInstance _primary;
3132
private string _replicaSetName;
3233

3334
// constructors
@@ -53,6 +54,17 @@ public ReplicaSetMongoServerProxy(MongoServerSettings serverSettings, IEnumerabl
5354
{ }
5455

5556
// public properties
57+
/// <summary>
58+
/// Gets the primary.
59+
/// </summary>
60+
/// <value>
61+
/// The primary.
62+
/// </value>
63+
public MongoServerInstance Primary
64+
{
65+
get { return _primary; }
66+
}
67+
5668
/// <summary>
5769
/// Gets the type of the proxy.
5870
/// </summary>
@@ -200,6 +212,15 @@ protected override void ProcessConnectedInstanceStateChange(MongoServerInstance
200212
}
201213
}
202214

215+
/// <summary>
216+
/// Processes the disconnected instance state change.
217+
/// </summary>
218+
/// <param name="instance">The instance.</param>
219+
protected override void ProcessDisconnectedInstanceStateChange(MongoServerInstance instance)
220+
{
221+
Interlocked.CompareExchange(ref _primary, null, instance);
222+
}
223+
203224
// private methods
204225
/// <summary>
205226
/// Gets a randomly selected matching instance.
@@ -254,6 +275,7 @@ private MongoServerInstance GetMatchingInstance(List<ConnectedInstanceCollection
254275

255276
private void ProcessConnectedPrimaryStateChange(MongoServerInstance instance)
256277
{
278+
Interlocked.Exchange(ref _primary, instance);
257279
Interlocked.CompareExchange(ref _replicaSetName, instance.ReplicaSetInformation.Name, null);
258280

259281
var members = instance.ReplicaSetInformation.Members;

MongoDB.Driver/MongoServer.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,36 @@ public virtual MongoServerInstance Primary
275275
{
276276
get
277277
{
278-
return _serverProxy.Instances.SingleOrDefault(x => x.IsPrimary);
278+
var serverProxy = _serverProxy;
279+
280+
var discoveringServerProxy = serverProxy as DiscoveringMongoServerProxy;
281+
if (discoveringServerProxy != null)
282+
{
283+
serverProxy = discoveringServerProxy.WrappedProxy;
284+
if (serverProxy == null)
285+
{
286+
return null;
287+
}
288+
}
289+
290+
var directProxy = serverProxy as DirectMongoServerProxy;
291+
if (directProxy != null)
292+
{
293+
var instance = directProxy.Instances[0];
294+
if (instance.IsPrimary)
295+
{
296+
return instance;
297+
}
298+
return null;
299+
}
300+
301+
var replicaSetProxy = serverProxy as ReplicaSetMongoServerProxy;
302+
if (replicaSetProxy != null)
303+
{
304+
return replicaSetProxy.Primary;
305+
}
306+
307+
return null;
279308
}
280309
}
281310

0 commit comments

Comments
 (0)