Skip to content

Commit

Permalink
BREAKING CHANGE: SqlWaitForAG: Evaluate so that Availability Group is…
Browse files Browse the repository at this point in the history
… available (#1636)

- SqlWaitForAG
  - BREAKING CHANGE: Fix for issue (issue #1569)
    The resource now waits for the Availability Group to become Available.
  - Two parameters where added to test get and set resource at instance level.
  • Loading branch information
Fiander authored Nov 25, 2020
1 parent 87c50c0 commit ce78685
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- WaitForAG
- BREAKING CHANGE: Fix for issue ([issue #1569](https://github.com/dsccommunity/SqlServerDsc/issues/1569))
The resource now waits for the Availability Group to become Available.
- Two parameters where added to test get and set resource at instance level.
- SqlRole
- Major overhaul of resource.
- BREAKING CHANGE: Removed decision making from get-TargetResource; this
Expand Down
104 changes: 96 additions & 8 deletions source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
Returns the cluster role/group that is waiting to be created,
along with the time and number of times to wait.
.PARAMETER ServerName
Hostname of the SQL Server to be configured.
.PARAMETER InstanceName
Name of the SQL instance to be configured.
.PARAMETER Name
Name of the cluster role/group to look for (normally the same as the
Availability Group name).
.PARAMETER RetryIntervalSec
The interval, in seconds, to check for the presence of the cluster role/group.
Default value is 20 seconds. When the cluster role/group has been found the
resource will wait for this amount of time once more before returning.
resource will check if the AG group exist. When the availability group has
been found the resource will also wait this amount of time before returning.
.PARAMETER RetryCount
Maximum number of retries until the resource will timeout and throw an error.
Expand All @@ -30,6 +37,16 @@ function Get-TargetResource
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$ServerName = $env:COMPUTERNAME,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$InstanceName,

[Parameter(Mandatory = $true)]
[System.String]
$Name,
Expand All @@ -49,14 +66,43 @@ function Get-TargetResource

$clusterGroupFound = $false

# No ClusterName specified, so defaults to cluster on this node.
$clusterGroup = Get-ClusterGroup -Name $Name -ErrorAction SilentlyContinue

if ($null -ne $clusterGroup)
{
Write-Verbose -Message (
$script:localizedData.FoundClusterGroup -f $Name
)

$clusterGroupFound = $true
# Connect to the instance
$serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName

if ($serverObject)
{
# Determine if HADR is enabled on the instance. If not, AG group can not exist.
if ($serverObject.IsHadrEnabled )
{
$availabilityGroup = $serverObject.AvailabilityGroups[$Name]

if ( $availabilityGroup )
{
$clusterGroupFound = $true
}
else
{
Write-Verbose -Message (
$script:localizedData.AGNotFound -f $name, $InstanceName, $RetryIntervalSec
)
}
}
else
{
Write-Verbose -Message (
$script:localizedData.HadrNotEnabled -f $InstanceName
)
}
}
}
else
{
Expand All @@ -66,6 +112,8 @@ function Get-TargetResource
}

return @{
ServerName = $ServerName
InstanceName = $InstanceName
Name = $Name
RetryIntervalSec = $RetryIntervalSec
RetryCount = $RetryCount
Expand All @@ -77,14 +125,22 @@ function Get-TargetResource
.SYNOPSIS
Waits for a cluster role/group to be created
.PARAMETER ServerName
Hostname of the SQL Server to be configured.
.PARAMETER InstanceName
Name of the SQL instance to be configured.
.PARAMETER Name
Name of the cluster role/group to look for (normally the same as the Availability
Group name).
Name of the cluster role/group to look for (normally the same as the
Availability Group name).
.PARAMETER RetryIntervalSec
The interval, in seconds, to check for the presence of the cluster role/group.
Default value is 20 seconds. When the cluster role/group has been found the
resource will wait for this amount of time once more before returning.
resource will check if the AG group exist. When the availability group has
been found the resource will also wait this amount of time before returning.
.PARAMETER RetryCount
Maximum number of retries until the resource will timeout and throw an error.
Expand All @@ -95,6 +151,16 @@ function Set-TargetResource
[CmdletBinding()]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$ServerName = $env:COMPUTERNAME,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$InstanceName,

[Parameter(Mandatory = $true)]
[System.String]
$Name,
Expand All @@ -113,6 +179,8 @@ function Set-TargetResource
)

$getTargetResourceParameters = @{
ServerName = $ServerName
InstanceName = $InstanceName
Name = $Name
RetryIntervalSec = $RetryIntervalSec
RetryCount = $RetryCount
Expand Down Expand Up @@ -153,14 +221,22 @@ function Set-TargetResource
.SYNOPSIS
Tests if the cluster role/group has been created.
.PARAMETER ServerName
Hostname of the SQL Server to be configured.
.PARAMETER InstanceName
Name of the SQL instance to be configured.
.PARAMETER Name
Name of the cluster role/group to look for (normally the same as the Availability
Group name).
Name of the cluster role/group to look for (normally the same as the
Availability Group name).
.PARAMETER RetryIntervalSec
The interval, in seconds, to check for the presence of the cluster role/group.
Default value is 20 seconds. When the cluster role/group has been found the
resource will wait for this amount of time once more before returning.
resource will check if the AG group exist. When the availability group has
been found the resource will also wait this amount of time before returning.
.PARAMETER RetryCount
Maximum number of retries until the resource will timeout and throw an error.
Expand All @@ -172,6 +248,16 @@ function Test-TargetResource
[OutputType([System.Boolean])]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$ServerName = $env:COMPUTERNAME,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$InstanceName,

[Parameter(Mandatory = $true)]
[System.String]
$Name,
Expand All @@ -190,6 +276,8 @@ function Test-TargetResource
)

$getTargetResourceParameters = @{
ServerName = $ServerName
InstanceName = $InstanceName
Name = $Name
RetryIntervalSec = $RetryIntervalSec
RetryCount = $RetryCount
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[ClassVersion("1.0.0.0"), FriendlyName("SqlWaitForAG")]
class DSC_SqlWaitForAG : OMI_BaseResource
{
[Key, Description("The name of the _SQL Server_ instance to be configured.")] String InstanceName;
[Write, Description("The host name of the _SQL Server_ to be configured. Default value is `$env:COMPUTERNAME`.")] String ServerName;
[Key, Description("Name of the cluster role/group to look for (normally the same as the _Availability Group_ name).")] String Name;
[Write, Description("The interval, in seconds, to check for the presence of the cluster role/group. Default value is `20` seconds. When the cluster role/group has been found the resource will wait for this amount of time once more before returning.")] UInt64 RetryIntervalSec;
[Write, Description("Maximum number of retries until the resource will timeout and throw an error. Default values is `30` times.")] UInt32 RetryCount;
Expand Down
14 changes: 3 additions & 11 deletions source/DSCResources/DSC_SqlWaitForAG/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Description

The `SqlWaitForAG` DSC resource will wait for a cluster role/group to be
created. This is used to wait for an Availability Group to create the
cluster role/group in the cluster.
created. When the cluster group is found it will wait for the availability group to become available.
When the availability group has been found the resource will wait the amount of time specified
in the parameter RetryIntervalSec before returning.

## Requirements

Expand All @@ -17,13 +18,4 @@ cluster role/group in the cluster.

## Known issues

* This resource evaluates if the Windows Failover Cluster role/group
has been created. But the Windows Failover Cluster role/group is created
before the Availability Group is in a ready state. When the Windows Failover
Cluster role/group is found the resource will wait one more time
according to the value of `RetryIntervalSec` before returning. There is
currently no check to validate that the Availability Group was successfully
created and is in a ready state. A workaround is instead use [`WaitForAny`](https://docs.microsoft.com/en-us/powershell/scripting/dsc/reference/resources/windows/waitforanyresource?view=powershell-7)
resource. This is being tracked in [issue #1569](https://github.com/dsccommunity/SqlServerDsc/issues/1569).

All issues are not listed here, see [here for all open issues](https://github.com/dsccommunity/SqlServerDsc/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+SqlWaitForAG).
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ ConvertFrom-StringData @'
RetryMessage = Will retry again after {0} seconds.
FailedMessage = Did not find the cluster group '{0}' within the timeout period.
TestingConfiguration = Determines the current state of the Always On Availability Group with the cluster group name '{0}'.
HadrNotEnabled = Hadr is not enabled on the sql server instance '{0}'.
AGNotFound = Availibility Group '{0}' not found on instance '{1}'. Waiting an other {2} seconds.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Configuration Example
Name = 'AGTest1'
RetryIntervalSec = 20
RetryCount = 30
InstanceName = 'MSSQLSERVER'

PsDscRunAsCredential = $SqlAdministratorCredential
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Configuration Example
Name = 'AGTest1'
RetryIntervalSec = 20
RetryCount = 30
InstanceName = 'MSSQLSERVER'

PsDscRunAsCredential = $SqlAdministratorCredential
}
Expand All @@ -29,6 +30,7 @@ Configuration Example
Name = 'AGTest2'
RetryIntervalSec = 20
RetryCount = 30
InstanceName = 'MSSQLSERVER'

PsDscRunAsCredential = $SqlAdministratorCredential
}
Expand Down
Loading

0 comments on commit ce78685

Please sign in to comment.