Skip to content

Commit 7e09d3e

Browse files
committed
minor: MongoServerInstance is now persisting certain server information between disconnections. MultipleInstanceMongoServerProxy reuses addresses specified with a different hostname than the replica recognizes. Primary disconnect no longers causes invalid extra instance.
1 parent 2647c3b commit 7e09d3e

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

Driver/Core/MongoServerInstance.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public sealed class MongoServerInstance
7272
private bool _inStateVerification;
7373
private ServerInformation _serverInfo;
7474
private IPEndPoint _ipEndPoint;
75+
private bool _permanentlyDisconnected;
7576
private int _sequentialId;
7677
private MongoServerState _state;
7778
private Timer _stateVerificationTimer;
@@ -96,6 +97,7 @@ internal MongoServerInstance(MongoServer server, MongoServerAddress address)
9697
};
9798
_connectionPool = new MongoConnectionPool(this);
9899
_pingTimeAggregator = new PingTimeAggregator(5);
100+
_permanentlyDisconnected = false;
99101
// Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
100102
}
101103

@@ -425,7 +427,7 @@ internal void Connect()
425427
// Console.WriteLine("MongoServerInstance[{0}]: Connect() called.", sequentialId);
426428
lock (_serverInstanceLock)
427429
{
428-
if (_state == MongoServerState.Connecting || _state == MongoServerState.Connected)
430+
if (_permanentlyDisconnected || _state == MongoServerState.Connecting || _state == MongoServerState.Connected)
429431
{
430432
return;
431433
}
@@ -513,6 +515,19 @@ internal void Disconnect()
513515
}
514516
}
515517

518+
/// <summary>
519+
/// Disconnects this instance permanently.
520+
/// </summary>
521+
internal void DisconnectPermanently()
522+
{
523+
lock (_serverInstanceLock)
524+
{
525+
_permanentlyDisconnected = true;
526+
}
527+
528+
Disconnect();
529+
}
530+
516531
/// <summary>
517532
/// Releases the connection.
518533
/// </summary>
@@ -618,18 +633,20 @@ private void LookupServerInformation(MongoConnection connection)
618633
currentServerInfo = _serverInfo;
619634
}
620635

636+
// keep the current instance type, build info, and replica set info
637+
// as these aren't relevent to state and are likely still correct.
621638
var newServerInfo = new ServerInformation
622639
{
623-
BuildInfo = null,
640+
BuildInfo = currentServerInfo.BuildInfo,
624641
InstanceType = currentServerInfo.InstanceType,
625642
IsArbiter = false,
626643
IsMasterResult = isMasterResult,
627644
IsPassive = false,
628645
IsPrimary = false,
629646
IsSecondary = false,
630-
MaxDocumentSize = MongoDefaults.MaxDocumentSize,
631-
MaxMessageLength = MongoDefaults.MaxMessageLength,
632-
ReplicaSetInformation = null
647+
MaxDocumentSize = currentServerInfo.MaxDocumentSize,
648+
MaxMessageLength = currentServerInfo.MaxMessageLength,
649+
ReplicaSetInformation = currentServerInfo.ReplicaSetInformation
633650
};
634651

635652
SetState(MongoServerState.Disconnected, newServerInfo);

Driver/Internal/MultipleInstanceMongoServerProxy.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ public void VerifyState()
323323
/// <param name="address">The address.</param>
324324
protected void EnsureInstanceWithAddress(MongoServerAddress address)
325325
{
326+
if (address == null)
327+
{
328+
throw new ArgumentNullException("address");
329+
}
330+
326331
lock (_lock)
327332
{
328333
if (!_instances.Any(x => x.Address == address))
@@ -365,7 +370,10 @@ protected void MakeInstancesMatchAddresses(IEnumerable<MongoServerAddress> addre
365370

366371
foreach (var address in addresses)
367372
{
368-
EnsureInstanceWithAddress(address);
373+
if (address != null)
374+
{
375+
EnsureInstanceWithAddress(address);
376+
}
369377
}
370378
}
371379
}
@@ -427,6 +435,20 @@ private void ProcessInstanceStateChange(MongoServerInstance instance)
427435
return;
428436
}
429437

438+
if (instance.Address != instance.IsMasterResult.MyAddress)
439+
{
440+
if (!_instances.Any(x => x.Address == instance.IsMasterResult.MyAddress))
441+
{
442+
instance.Address = instance.IsMasterResult.MyAddress;
443+
}
444+
else
445+
{
446+
// we need to get rid of the duplicate.
447+
RemoveInstance(instance);
448+
return;
449+
}
450+
}
451+
430452
if (_state != MongoServerState.Disconnecting && _state != MongoServerState.Disconnected)
431453
{
432454
_connectedInstances.EnsureContains(instance);
@@ -450,7 +472,7 @@ private void RemoveInstance(MongoServerInstance instance)
450472
{
451473
_instances.Remove(instance);
452474
instance.StateChanged -= InstanceStateChanged;
453-
instance.Disconnect();
475+
instance.DisconnectPermanently();
454476
ProcessInstanceStateChange(instance);
455477
}
456478
}

Driver/Internal/ReplicaSetMongoServerProxy.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ private void ProcessConnectedPrimaryStateChange(MongoServerInstance instance)
175175

176176
private void ProcessConnectedSecondaryStateChange(MongoServerInstance instance)
177177
{
178-
// make sure the primary exists in the instance list
179-
EnsureInstanceWithAddress(instance.ReplicaSetInformation.Primary);
178+
var address = instance.ReplicaSetInformation.Primary;
179+
if (address != null)
180+
{
181+
// make sure the primary exists in the instance list
182+
EnsureInstanceWithAddress(address);
183+
}
180184
}
181185
}
182186
}

0 commit comments

Comments
 (0)