Skip to content

Commit

Permalink
direct connections to a mongod ignore read preference, although the S…
Browse files Browse the repository at this point in the history
…laveOk query flag is still utilized.
  • Loading branch information
craiggwilson committed Aug 8, 2012
1 parent fa63af0 commit f81e9d9
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions Driver/Internal/DirectMongoServerProxy.cs
Expand Up @@ -118,17 +118,7 @@ public MongoServerInstance ChooseServerInstance(ReadPreference readPreference)

if (_instance.State == MongoServerState.Connected)
{
if (readPreference.MatchesInstance(_instance))
{
return _instance;
}
else
{
string message = string.Format("Server {0} does not meet the criteria of the specified read preference {1}.",
_instance.Address,
readPreference);
throw new MongoConnectionException(message);
}
return _instance;
}

throw new MongoConnectionException(string.Format("Unable to connect to the server {0}.", _instance.Address));
Expand All @@ -145,45 +135,44 @@ public void Connect(TimeSpan timeout, ReadPreference readPreference)
{
lock (_stateLock)
{
_connectionAttempt++;
var exceptions = new List<Exception>();
foreach (var address in _server.Settings.Servers)
if (_instance.State != MongoServerState.Connected)
{
try
_connectionAttempt++;
var exceptions = new List<Exception>();
foreach (var address in _server.Settings.Servers)
{
_instance.Address = address;
_instance.Connect(); // TODO: what about timeout?
if (!readPreference.MatchesInstance(_instance))
{
exceptions.Add(new MongoConnectionException(string.Format("The server '{0}' does not match the read preference {1}.", address, readPreference)));
continue;
}

if (_server.Settings.ReplicaSetName != null &&
(_instance.InstanceType != MongoServerInstanceType.ReplicaSetMember || _instance.ReplicaSetInformation.Name != _server.Settings.ReplicaSetName))
try
{
exceptions.Add(new MongoConnectionException(string.Format("The server '{0}' is not a member of replica set '{1}'.", address, _server.Settings.ReplicaSetName)));
continue;
_instance.Address = address;
_instance.Connect(); // TODO: what about timeout?

if (_server.Settings.ReplicaSetName != null &&
(_instance.InstanceType != MongoServerInstanceType.ReplicaSetMember || _instance.ReplicaSetInformation.Name != _server.Settings.ReplicaSetName))
{
exceptions.Add(new MongoConnectionException(string.Format("The server '{0}' is not a member of replica set '{1}'.", address, _server.Settings.ReplicaSetName)));
_instance.Disconnect();
continue;
}

if (_instance.IsMasterResult.MyAddress != null)
{
_instance.Address = _instance.IsMasterResult.MyAddress;
}
return;
}

if (_instance.IsMasterResult.MyAddress != null)
catch (Exception ex)
{
_instance.Address = _instance.IsMasterResult.MyAddress;
exceptions.Add(ex);
}
return;
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}

var firstAddress = _server.Settings.Servers.First();
var firstException = exceptions.First();
var message = string.Format("Unable to connect to server {0}: {1}.", firstAddress, firstException.Message);
var connectionException = new MongoConnectionException(message, firstException);
connectionException.Data.Add("InnerExceptions", exceptions); // useful when there is more than one
throw connectionException;
var firstAddress = _server.Settings.Servers.First();
var firstException = exceptions.First();
var message = string.Format("Unable to connect to server {0}: {1}.", firstAddress, firstException.Message);
var connectionException = new MongoConnectionException(message, firstException);
connectionException.Data.Add("InnerExceptions", exceptions); // useful when there is more than one
throw connectionException;
}
}
}
}
Expand Down

0 comments on commit f81e9d9

Please sign in to comment.