Skip to content

Commit c8b7e01

Browse files
author
rstam
committed
CSHARP-724: Now that we have more robust primary tracking use that information in more places. Also, reset state verification timer on old primary whenever we discover a new primary so that the new state of the old primary will be determined as soon as possible.
1 parent 108be7e commit c8b7e01

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

MongoDB.Driver/Communication/MongoServerInstance.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ internal void UnsetPrimary()
538538
lock (_serverInstanceLock)
539539
{
540540
_serverInfo.IsPrimary = false;
541+
_stateVerificationTimer.Change(TimeSpan.Zero, TimeSpan.FromSeconds(10)); // verify state as soon as possible
541542
}
542543
}
543544

MongoDB.Driver/Communication/Proxies/ConnectedInstanceCollection.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,18 @@ public List<InstanceWithPingTime> GetAllInstances()
6868
}
6969
}
7070

71-
/// <summary>
72-
/// Gets the primary instance.
73-
/// </summary>
74-
/// <returns>The primary instance (or null if there is none).</returns>
75-
public MongoServerInstance GetPrimary()
76-
{
77-
lock (_connectedInstancesLock)
78-
{
79-
return _instances.Select(x => x.Instance).FirstOrDefault(i => i.IsPrimary);
80-
}
81-
}
82-
8371
/// <summary>
8472
/// Gets a list of primary and secondary instances.
8573
/// </summary>
74+
/// <param name="primary">The current primary.</param>
8675
/// <returns>The list of primary and secondary instances.</returns>
87-
public List<InstanceWithPingTime> GetPrimaryAndSecondaries()
76+
public List<InstanceWithPingTime> GetPrimaryAndSecondaries(MongoServerInstance primary)
8877
{
8978
lock (_connectedInstancesLock)
9079
{
9180
// note: make copies of InstanceWithPingTime values because they can change after we return
9281
return _instances
93-
.Where(x => x.Instance.IsPrimary || x.Instance.IsSecondary)
82+
.Where(x => x.Instance == primary || x.Instance.IsSecondary)
9483
.Select(x => new InstanceWithPingTime { Instance = x.Instance, CachedAveragePingTime = x.CachedAveragePingTime })
9584
.ToList();
9685
}

MongoDB.Driver/Communication/Proxies/ReplicaSetMongoServerProxy.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,12 @@ protected override MongoServerInstance ChooseServerInstance(ConnectedInstanceCol
9696
switch (readPreference.ReadPreferenceMode)
9797
{
9898
case ReadPreferenceMode.Primary:
99-
return connectedInstances.GetPrimary();
99+
return _primary;
100100

101101
case ReadPreferenceMode.PrimaryPreferred:
102-
var primary = connectedInstances.GetPrimary();
103-
if (primary != null)
102+
if (_primary != null)
104103
{
105-
return primary;
104+
return _primary;
106105
}
107106
else
108107
{
@@ -120,11 +119,11 @@ protected override MongoServerInstance ChooseServerInstance(ConnectedInstanceCol
120119
}
121120
else
122121
{
123-
return connectedInstances.GetPrimary();
122+
return _primary;
124123
}
125124

126125
case ReadPreferenceMode.Nearest:
127-
return GetMatchingInstance(connectedInstances.GetPrimaryAndSecondaries(), readPreference, secondaryAcceptableLatency);
126+
return GetMatchingInstance(connectedInstances.GetPrimaryAndSecondaries(_primary), readPreference, secondaryAcceptableLatency);
128127

129128
default:
130129
throw new MongoInternalException("Invalid ReadPreferenceMode.");

0 commit comments

Comments
 (0)