private void WaitForInitialization(Server server, AvailabilityGroup availabilityGroup) { var policy = Policy .Handle <TimeoutException>() .WaitAndRetry(4, retryAttempt => TimeSpan.FromMilliseconds(Math.Pow(10, retryAttempt))); policy.Execute(() => { if (availabilityGroup.IsInitializing(Name)) { throw new TimeoutException($"{server.Name} is initializing. Wait period expired."); } }); }
/// <summary> /// Relays an HTTP request to the specified instance. Should only be called if <see cref="Trinity.Network.Http.TrinityHttpServer.SetInstanceList"/> is properly called. /// </summary> /// <param name="instance_id">The server id.</param> /// <param name="context">The context of the request to relay.</param> public void RelayRequest(int instance_id, HttpListenerContext context) { if (instance_id < 0 || instance_id >= m_RelayInstanceList.Count) { Log.WriteLine(LogLevel.Error, "RelayRequest: relay target server index out of bound."); Log.WriteLine(LogLevel.Error, "RelayRequest: Index: {0}.", instance_id); Log.WriteLine(LogLevel.Error, "RelayRequest: Remote endpoint: {0}.", context.Request.RemoteEndPoint.ToString()); } AvailabilityGroup ag = m_RelayInstanceList[instance_id]; int idx = m_RelayRoundRobinTable[instance_id] = (m_RelayRoundRobinTable[instance_id] + 1) % ag.Instances.Count; _RelayRequest(context, ag, idx); }
public static AvailabilityReplica BuildAR(AvailabilityGroup ag, string replicaName, string endpointUrl) { //This will give a different combination of the possible configurations of an AR every time it runs. AvailabilityReplica ar = new AvailabilityReplica(ag, replicaName); ar.AvailabilityMode = AvailabilityReplicaAvailabilityMode.SynchronousCommit; ar.FailoverMode = AvailabilityReplicaFailoverMode.Automatic; ar.EndpointUrl = endpointUrl; ar.SessionTimeout = 30; ar.ConnectionModeInPrimaryRole = AvailabilityReplicaConnectionModeInPrimaryRole.AllowAllConnections; ar.ConnectionModeInSecondaryRole = AvailabilityReplicaConnectionModeInSecondaryRole.AllowAllConnections; ar.BackupPriority = 3; ar.SeedingMode = AvailabilityReplicaSeedingMode.Automatic; return(ar); }
private void FinalizePrimary(Server server, AvailabilityGroup availabilityGroup) { if (!availabilityGroup.IsPrimaryInstance) { return; } var database = server.Database(Name); if (!database.Restoring) { return; } database.RestoreWithRecovery(); }
private void WaitForInitialization(Server server, AvailabilityGroup availabilityGroup) { var wait = 100; var maxWait = 60000; var multiplier = 2; while (availabilityGroup.IsInitializing(Name)) { if (wait > maxWait) { throw new TimeoutException($"{server.Name} is initializing. Wait period expired."); } Thread.Sleep(wait); wait *= multiplier; } }
public static SMO.AvailabilityReplica GetPrimaryReplica(string agName, SMO.Server server) { AvailabilityGroup ag = server.AvailabilityGroups[agName]; ag.Refresh(); foreach (SMO.AvailabilityReplica replica in ag.AvailabilityReplicas) { if (replica.Role == AvailabilityReplicaRole.Primary) { return(replica); } } return(null); }
/// <summary> /// Sets database in required state. /// </summary> /// <param name="agName"></param> /// <param name="dbName"></param> /// <param name="suspend"></param> /// <param name="server"></param> private static void SetDbState(string agName, string dbName, bool suspend, SMO.Server server) { AvailabilityGroup ag = server.AvailabilityGroups[agName]; AvailabilityDatabase db = ag.AvailabilityDatabases[dbName]; db.Refresh(); // database state is server side property, so the SMO object needs to be refreshed to get the correct value. if (suspend) { db.SuspendDataMovement(); } else { db.ResumeDataMovement(); } }
public static List <SMO.AvailabilityReplica> GetSecondaryReplicas(string agName, SMO.Server server) { List <SMO.AvailabilityReplica> secondaryReplicaList = new List <AvailabilityReplica>(); AvailabilityGroup ag = server.AvailabilityGroups[agName]; ag.Refresh(); foreach (SMO.AvailabilityReplica replica in ag.AvailabilityReplicas) { if (replica.Role != AvailabilityReplicaRole.Primary) { secondaryReplicaList.Add(replica); } } return(secondaryReplicaList); }
/// <summary> /// On primary, removes database from availability group. On secondary, unjoins the secondary database from availability group. /// </summary> /// <param name="agName">Availability Group name</param> /// <param name="databaseName">Database name to be removed from AG</param> /// <param name="server">smo server</param> public static void RemoveDatabaseFromAG(string agName, string databaseName, Smo.Server server) { AvailabilityGroup ag = server.AvailabilityGroups[agName]; AvailabilityDatabase adb; if (ag.AvailabilityDatabases.Contains(databaseName)) { adb = ag.AvailabilityDatabases[databaseName]; if (ag.AvailabilityReplicas[server.Name].Role == AvailabilityReplicaRole.Primary) { adb.Drop(); } else { adb.LeaveAvailabilityGroup(); } } ag.AvailabilityDatabases.Refresh(); }
public void AddDatabasesIntoAG(List <string> listDbNames) { AvailabilityGroup ag = primary.AvailabilityGroups[agName]; if (ag == null) { return; } foreach (string dbname in listDbNames) { AvailabilityDatabase adb = new AvailabilityDatabase(ag, dbname); adb.Create(); foreach (Smo.Server srv in secondaries) { AGDBHelper.JoinAG(dbname, agName, srv); Thread.Sleep(1000); } // wait a bit to let adb join ag } }
private void CheckLoginExists(Server server, AvailabilityGroup availabilityGroup, string loginName) { var matchingLogins = server.Logins.Where(l => l.Name == loginName); if (matchingLogins.Count() == 0) { throw new MissingLoginException($"Login missing on {server.Name}, {_listener.AvailabilityGroup.Name}, {loginName}"); } if (matchingLogins.Count() > 1) { throw new MultipleLoginException($"Multiple logins exist on {server.Name}, {_listener.AvailabilityGroup.Name}, {loginName}"); } var sid = matchingLogins.First().Sid; if (sid == null || sid.Length == 0) { throw new MissingSidException($"Sid missing on {server.Name}, {_listener.AvailabilityGroup.Name}, {loginName}"); } }
// When we called this primary and secondary should being create public void TestCreateAGWithTwoReplicasWithoutDatabase() { Smo.Server primary = replicas[0]; AvailabilityGroup ag = new AvailabilityGroup(primary, agName); List <Smo.Server> secondaries = replicas.GetRange(1, replicas.Count - 1); for (int i = 0; i < replicas.Count; ++i) { AvailabilityReplica ar = ARHelper.BuildAR(ag, replicas[i].Name, replicaEndpointUrls[i]); ag.AvailabilityReplicas.Add(ar); } try { Console.WriteLine("Creating availability group '{0}' on server '{1}", ag.Name, primary.Name); ag.Create(); Thread.Sleep(1000); //Sleep a tick to let AG create take effect foreach (Smo.Server srv in secondaries) { srv.JoinAvailabilityGroup(agName); } CreateAGListener(); // enable autoseeding in secondary //secondary.GrantAvailabilityGroupCreateDatabasePrivilege(agName); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Failed to create AG {0}", ag.Name); AGHelper.DropAG(ag.Name, primary); } }
/// <summary> /// Fails over an AvailabilityGroup to the server specified in manner specified /// </summary> /// <param name="serverToFailoverTo">The server to failover the AG to</param> /// <param name="agName">The Ag to failover</param> /// <param name="action">The type of failover</param> public static void FailoverToServer(Smo.Server serverToFailoverTo, string agName, ActionType action) { AvailabilityGroup agJoined = serverToFailoverTo.AvailabilityGroups[agName]; if (agJoined == null) { } else { switch (action) { case ActionType.ForceFailover: agJoined.FailoverWithPotentialDataLoss(); break; case ActionType.ManualFailover: agJoined.Failover(); break; default: break; } } }
internal RemoteStorage(AvailabilityGroup trinityServer, int connPerServer, MemoryCloud mc, int serverId, bool nonblocking) { this.memory_cloud = mc; this.MyServerId = serverId; var connect_async_task = Task.Factory.StartNew(() => { for (int k = 0; k < connPerServer; k++) // make different server connections interleaved { for (int i = 0; i < trinityServer.Instances.Count; i++) { Connect(trinityServer.Instances[i]); } } BackgroundThread.AddBackgroundTask(new BackgroundTask(Heartbeat, TrinityConfig.HeartbeatInterval)); mc.ReportServerConnectedEvent(serverId); }); if (!nonblocking) { try { connect_async_task.Wait(); } catch (AggregateException ex) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } } }