Determine Which Server is Currently Active for Availability Groups

  • I want two write a small script to determine which is the currently active (primary) server in the AG. Right now, I see that using SELECT * FROM SYS.dm_hadr_availability_replica_states I can determine the role. However, when the server goes down and switches to the secondary node, I don't believe that the role changes (or does it?). How do I determine which is the active node?

  • Yes, the role changes. When you fail over from one database to another within your availability group, the active node switches. If you're scheduling activities on the databases, you'll want to have identical SQL Agent jobs on each server and the check to validate whether or not it's currently the active process.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • to check the currently active primary for a group use the following

    selectgroup_id

    , primary_replica

    , primary_recovery_health

    , primary_recovery_health_desc

    , secondary_recovery_health

    , secondary_recovery_health_desc

    , synchronization_health

    , synchronization_health_desc

    from sys.dm_hadr_availability_group_states

    Looking at the replica level will only show replica info, looking at the group level shows meta data aboyut that group including the current Primary

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • So will this also work,

    if (select ars.role

    from sys.dm_hadr_availability_replica_states ars

    where ars.is_local = 1) = 2

    begin

    raiserror ('This is not the primary replica.',2,1)

    end

  • Sorry for the bump guys. Do you happen to know if the script I listed above will work? I want to implement this within the next day or two.

  • I don't currently have an availability group set up in order to validate your script.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • JoshDBGuy (11/26/2014)


    So will this also work,

    if (select ars.role

    from sys.dm_hadr_availability_replica_states ars

    where ars.is_local = 1) = 2

    begin

    raiserror ('This is not the primary replica.',2,1)

    end

    Work from where, where will this be running from? Did you read my post above, if you query at the replica level you'll need to query each replica. Querying the group will show the current replica name

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Perry Whittle (12/9/2014)


    JoshDBGuy (11/26/2014)


    So will this also work,

    if (select ars.role

    from sys.dm_hadr_availability_replica_states ars

    where ars.is_local = 1) = 2

    begin

    raiserror ('This is not the primary replica.',2,1)

    end

    Work from where, where will this be running from? Did you read my post above, if you query at the replica level you'll need to query each replica. Querying the group will show the current replica name

    If this runs on the secondary, it will work currently. However, say the primary goes down and now the AG switches over to the secondary. Will this script work so that it thinks it's the primary?

  • JoshDBGuy (12/9/2014)


    Perry Whittle (12/9/2014)


    JoshDBGuy (11/26/2014)


    So will this also work,

    if (select ars.role

    from sys.dm_hadr_availability_replica_states ars

    where ars.is_local = 1) = 2

    begin

    raiserror ('This is not the primary replica.',2,1)

    end

    Work from where, where will this be running from? Did you read my post above, if you query at the replica level you'll need to query each replica. Querying the group will show the current replica name

    If this runs on the secondary, it will work currently. However, say the primary goes down and now the AG switches over to the secondary. Will this script work so that it thinks it's the primary?

    What are you expecting that script to do??

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Perry Whittle (12/10/2014)


    JoshDBGuy (12/9/2014)


    Perry Whittle (12/9/2014)


    JoshDBGuy (11/26/2014)


    So will this also work,

    if (select ars.role

    from sys.dm_hadr_availability_replica_states ars

    where ars.is_local = 1) = 2

    begin

    raiserror ('This is not the primary replica.',2,1)

    end

    Work from where, where will this be running from? Did you read my post above, if you query at the replica level you'll need to query each replica. Querying the group will show the current replica name

    If this runs on the secondary, it will work currently. However, say the primary goes down and now the AG switches over to the secondary. Will this script work so that it thinks it's the primary?

    What are you expecting that script to do??

    I'm going to stick it in a job as the first step. If it returns an error because it's not the primary node, I want the job to quit.

  • easy to test, but, affirm. The raiserror will be passed to the job step and fail, if this step is told to quit on failure the job will terminate

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • I've done the job test, but I'd like to failover to the secondary node and test it out. Unfortunately I can't do that on my box as I have windows 8.1 and I need 2012 server.

  • build yourself a virtual test system on your 8.1 machine 😉

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Unfortunately because I don't have server 2012, I can't. I did find through a Microsoft technet page that when it switches over, the replicated server should become the primary. So it should work.

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply