/// <summary> /// Fetches the current status. /// </summary> public static ReplicaSetStatus GetStatus() { if (!RoleEnvironment.IsAvailable) { return(GetDummyStatus()); } var settings = ConnectionUtilities.GetMongoClientSettings( ReadPreference.SecondaryPreferred); var client = new MongoClient(settings); var server = client.GetServer(); try { var result = server.GetDatabase("admin").RunCommand("replSetGetStatus"); return(ParseStatus(result.Response)); } catch (IOException ie) { return(new ReplicaSetStatus { Status = State.Error, Error = ie }); } catch (MongoException e) { return(new ReplicaSetStatus { Status = State.Error, Error = e }); } }
internal static int ReconfigReplicaSet(string replicaSetName, int port) { var replicaSetRoleCount = RoleEnvironment.Roles[currentRoleName].Instances.Count; var version = GetReplicaSetConfigVersion(port); var cfg = CreateReplicaSetConfiguration(replicaSetName, port, replicaSetRoleCount, false); cfg.Add("version", ++version); var reconfigCommand = new CommandDocument { { "replSetReconfig", cfg } }; var clientSettings = ConnectionUtilities.GetMongoClientSettings(); var client = new MongoClient(clientSettings); var server = client.GetServer(); try { var result = server.GetDatabase("admin").RunCommand(reconfigCommand); } catch (Exception e) { // no primary? DiagnosticsHelper.TraceWarning(e.Message); } return(replicaSetRoleCount); }
public static ReplicaSetStatus GetReplicaSetStatus() { ReplicaSetStatus status; var settings = ConnectionUtilities.GetMongoClientSettings(ReadPreference.SecondaryPreferred); var client = new MongoClient(settings); var server = client.GetServer(); try { var result = server.GetDatabase("admin").RunCommand("replSetGetStatus"); var response = result.Response; BsonValue startupStatus; if (response.TryGetValue("startupStatus", out startupStatus)) { status = new ReplicaSetStatus("Replica Set Initializing"); return(status); } status = new ReplicaSetStatus((string)response.GetValue("set")); var value = response.GetElement("members"); var members = value.Value.AsBsonArray; foreach (BsonDocument member in members) { var node = new ServerStatus(); foreach (BsonElement bsonElement in member.Elements) { switch (bsonElement.Name) { case "_id": node.id = bsonElement.Value.ToInt32(); break; case "name": node.name = bsonElement.Value.ToString(); break; case "health": node.health = bsonElement.Value.ToInt32() == 0 ? "DOWN" : "UP"; break; case "state": node.state = bsonElement.Value.ToInt32(); if (node.state == 1) { node.lastHeartbeat = "Not Applicable"; node.pingMS = "Not Applicable"; } break; case "stateStr": node.stateStr = bsonElement.Value.ToString(); break; case "uptime": break; case "lastHeartbeat": var hearbeat = bsonElement.Value.ToUniversalTime(); if (hearbeat != null) { node.lastHeartbeat = hearbeat.ToString("yyyy-MM-dd HH:mm tt"); } break; case "optimeDate": node.optimeDate = bsonElement.Value.ToUniversalTime(); break; case "pingMs": Double pingTime = bsonElement.Value.AsInt32; node.pingMS = pingTime.ToString(CultureInfo.InvariantCulture); break; } } status.servers.Add(node); } } catch (MongoException me) { status = new ReplicaSetStatus(string.Format( "Replica Set Unavailable due to {0}", me.Message)); } return(status); }