// private methods
private void LookupServerInformation(MongoConnection connection)
{
IsMasterResult isMasterResult = null;
bool ok = false;
try
{
var isMasterCommand = new CommandDocument("ismaster", 1);
var tempResult = connection.RunCommand("admin", QueryFlags.SlaveOk, isMasterCommand, false);
isMasterResult = new IsMasterResult();
isMasterResult.Initialize(isMasterCommand, tempResult.Response);
if (!isMasterResult.Ok)
{
throw new MongoCommandException(isMasterResult);
}
MongoServerBuildInfo buildInfo;
var buildInfoCommand = new CommandDocument("buildinfo", 1);
var buildInfoResult = connection.RunCommand("admin", QueryFlags.SlaveOk, buildInfoCommand, false);
if (buildInfoResult.Ok)
{
buildInfo = MongoServerBuildInfo.FromCommandResult(buildInfoResult);
}
else
{
// short term fix: if buildInfo fails due to auth we don't know the server version; see CSHARP-324
if (buildInfoResult.ErrorMessage != "need to login")
{
throw new MongoCommandException(buildInfoResult);
}
buildInfo = null;
}
ReplicaSetInformation replicaSetInformation = null;
MongoServerInstanceType instanceType = MongoServerInstanceType.StandAlone;
if (isMasterResult.ReplicaSetName != null)
{
var peers = isMasterResult.Hosts.Concat(isMasterResult.Passives).Concat(isMasterResult.Arbiters).ToList();
replicaSetInformation = new ReplicaSetInformation(isMasterResult.ReplicaSetName, isMasterResult.Primary, peers, isMasterResult.Tags);
instanceType = MongoServerInstanceType.ReplicaSetMember;
}
else if (isMasterResult.Message != null && isMasterResult.Message == "isdbgrid")
{
instanceType = MongoServerInstanceType.ShardRouter;
}
var newServerInfo = new ServerInformation
{
BuildInfo = buildInfo,
InstanceType = instanceType,
IsArbiter = isMasterResult.IsArbiterOnly,
IsMasterResult = isMasterResult,
IsPassive = isMasterResult.IsPassive,
IsPrimary = isMasterResult.IsPrimary,
IsSecondary = isMasterResult.IsSecondary,
MaxDocumentSize = isMasterResult.MaxBsonObjectSize,
MaxMessageLength = isMasterResult.MaxMessageLength,
ReplicaSetInformation = replicaSetInformation
};
MongoServerState currentState;
lock (_serverInstanceLock)
{
currentState = _state;
}
SetState(currentState, newServerInfo);
ok = true;
}
finally
{
if (!ok)
{
ServerInformation currentServerInfo;
lock (_serverInstanceLock)
{
currentServerInfo = _serverInfo;
}
// keep the current instance type, build info, and replica set info
// as these aren't relevent to state and are likely still correct.
var newServerInfo = new ServerInformation
{
BuildInfo = currentServerInfo.BuildInfo,
InstanceType = currentServerInfo.InstanceType,
IsArbiter = false,
IsMasterResult = isMasterResult,
IsPassive = false,
IsPrimary = false,
IsSecondary = false,
MaxDocumentSize = currentServerInfo.MaxDocumentSize,
MaxMessageLength = currentServerInfo.MaxMessageLength,
ReplicaSetInformation = currentServerInfo.ReplicaSetInformation
};
SetState(MongoServerState.Disconnected, newServerInfo);
}
}
}