Hi SQL Experts,
We have SQL 2014 Enterprise Edition instance in our environment and Alwayson availability groups configured.
Assume Server-1 and Server-2 are the two AG replicas.
In our environment (dev reporting enviornment), we have sql agent jobs configured only on "Server-1" and no jobs present on "Server-2".
Just incase if any reason(patching,unexpected sql shutdown, unexpected reboot) if it fails over to Server-2, I have create a sql agent job on Server-2 which runs every 5 mins, just to check if it became primary replica,
if so, I wanted to fail back to Server-1. Today, there was failover but the was continously failing with below error. Can anybody help me in fixing this.
Always my preferred node is Server-1 to run my jobs.
Job Name Failover AG group
Step Name step1
Duration 00:00:00
Sql Severity 16
Sql Message ID 41122
Operator Emailed
Operator Net sent
Operator Paged
Retries Attempted 0
Message
Executed as user: domain\user. Server-2 [SQLSTATE 01000] (Message 0) PRIMARY [SQLSTATE 01000] (Message 0) Cannot failover availability group 'MytestAG' to this instance of SQL Server. The local availability replica is already the primary replica of the availability group. To failover this availability group to another instance of SQL Server, run the failover command on that instance of SQL Server. If local instance of SQL Server is intended to host the primary replica of the availability group, then no action is required. [SQLSTATE 42000] (Error 41122). The step failed.
Job step logic :
===============
SET NOCOUNT ON
DECLARE @ServerName NVARCHAR(256) = @@SERVERNAME
DECLARE @RoleDesc NVARCHAR(60)
SELECT @RoleDesc = a.role_desc
FROM sys.dm_hadr_availability_replica_states AS a
JOIN sys.availability_replicas AS b
ON b.replica_id = a.replica_id
WHERE b.replica_server_name = @ServerName
PRINT @ServerName
PRINT @RoleDesc
IF @RoleDesc = 'PRIMARY'
BEGIN
ALTER AVAILABILITY GROUP [MytestAG] FAILOVER;
END
Thanks,
Sam