internal static void EnsureMongodIsListening(string rsName, int instanceId, int mongodPort) { var alias = ConnectionUtilities.GetNodeAlias(rsName, instanceId); var commandSucceeded = false; while (!commandSucceeded) { try { MongoServer conn; if (RoleEnvironment.IsEmulated) { conn = DatabaseHelper.GetLocalSlaveOkConnection(mongodPort + instanceId); } else { conn = DatabaseHelper.GetSlaveOkConnection(alias, mongodPort); } conn.Connect(new TimeSpan(0, 0, 5)); commandSucceeded = true; } catch (Exception e) { DiagnosticsHelper.TraceInformation(e.Message); commandSucceeded = false; } } }
internal static void RunInitializeCommandLocally( string rsName, int port) { var replicaSetRoleCount = RoleEnvironment.Roles[currentRoleName].Instances.Count; var membersDocument = new BsonArray(); for (int i = 0; i < replicaSetRoleCount; i++) { EnsureMongodIsListening(rsName, i, port); membersDocument.Add(new BsonDocument { { "_id", i }, { "host", RoleEnvironment.IsEmulated ? string.Format(Settings.LocalHostString, (port + i)) : ConnectionUtilities.GetNodeAlias(rsName, i) } }); } var cfg = new BsonDocument { { "_id", rsName }, { "members", membersDocument } }; var initCommand = new CommandDocument { { "replSetInitiate", cfg } }; var server = GetLocalSlaveOkConnection(port); var result = server["admin"].RunCommand(initCommand); }
private static BsonDocument CreateReplicaSetConfiguration( string rsName, int port, int replicaSetRoleCount, bool ensureNodesAreListening) { var membersDocument = new BsonArray(); for (int i = 0; i < replicaSetRoleCount; i++) { if (ensureNodesAreListening) { EnsureMongodIsListening(rsName, i, port); } membersDocument.Add(new BsonDocument { { "_id", i }, { "host", RoleEnvironment.IsEmulated ? string.Format(Settings.LocalHostString, (port + i)) : ConnectionUtilities.GetNodeAlias(rsName, i) } }); } var cfg = new BsonDocument { { "_id", rsName }, { "members", membersDocument } }; return(cfg); }
private static AvailableNodes GetAvailableNodes(string rsName) { var nodes = new AvailableNodes(); foreach (var instance in RoleEnvironment.Roles[Constants.MongoDBWorkerRoleName].Instances) { nodes.Add(new NodeAlias() { Alias = ConnectionUtilities.GetNodeAlias(rsName, ConnectionUtilities.ParseNodeInstanceId(instance.Id)), IpAddress = instance.InstanceEndpoints[Constants.MongodPortSetting].IPEndpoint.Address.ToString() }); } return(nodes); }