Monday, June 7, 2021

 One of the advantages of the method discussed on June  4th is that it converts all regions to write-regions if there were more than one of them to begin with. The justification is that the earlier configuration continues to operate in the same manner as new regions serve in write mode also. 

If the original configuration needs to be the same as the old one, then we do have the option of incrementally adding all the write regions first while the flag for EnableMultipleWriteLocations is set to true. Then, when adding the read regions, the flag can be set to false. This allows all the remaining regions of the old configuration to be correctly added back as the read region. The net effect is that we move the toggling of the EnableMultipleWriteLocations switch earlier than the last step.

The new script looks something like this:

param (

    [Parameter(Mandatory=$true)][string]$resourceGroupName,

    [Parameter(Mandatory=$true)][string]$accountName,

    [Parameter(Mandatory=$true)][string]$subscriptionId,

    [string]$environment = "AzureCloud"

)

 

function supportsZoneRedundancy {

    param (

        $regionName

    )

    $zonalRegions = @("West Central US", "Switzerland North", "UAE North", "UK West", "Norway East", "Korea Central", "East Asia", "Australia Central", "Australia Southeast", "Japan West", "Korea South", "West US", "Jio India West", "Central US EUAP", "Central US")

    $zoneRedundantRegions = @("East US", "East US 2", "North Europe", "Southeast Asia", "Japan East", "East US 2 EUAP", "Brazil South", "Canada Central", "West US 2", "France Central", "Korea Central", "Central India", "South Africa North", "West Europe", "Germany West Central", "UK South", "South Central US", "West US 3")

    $setZoneRedundancy = $False

    if ($zoneRedundantRegions.Where({$_ -eq $regionName}).Count -eq 1) {

        $setZoneRedundancy = $True

    } else {

        Write-Host $regionName does not support zone redundancy

    }

    return $setZoneRedundancy

}

 

Write-Host Multi-region account migration to AZ-Readiness begin

Connect-AzAccount -Environment "$environment"

Set-AzContext -SubscriptionId "$subscriptionId"

 

$dbs = Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName -ErrorAction Stop

 

foreach ($db in $dbs) {

 

if ($db.Locations.Count -eq 1) {

   Write-Host This script cannot be used for single-region account.

   exit

}

 

$oldWriteAll = $db.EnableMultipleWriteLocations

if ($db.EnableMultipleWriteLocations -eq $True) {

    Update-AzCosmosDBAccount `

      -ResourceGroupName $resourceGroupName `

      -Name $accountName `

      -EnableMultipleWriteLocations $False `

      -ErrorAction Stop

}

 

$locations = @()

$oldLocations = @()

$oldPriority = 0

$priority = 0

Write-Host Processing Write regions first for AZ readiness

Update-AzCosmosDBAccount `

  -ResourceGroupName $resourceGroupName `

  -Name $accountName `

  -EnableMultipleWriteLocations $True `

  -ErrorAction Stop

 

for ($i = 0; $i -lt $db.WriteLocations.Count; $i = $i + 1) {

    if ($i -eq 0) {

        $locations += New-AzCosmosDBLocationObject -LocationName $db.Locations[$i].LocationName -FailOverPriority $db.Locations[$i].FailoverPriority -IsZoneRedundant $db.Locations[$i].IsZoneRedundant

        $oldPriority = $db.Locations[$i].FailoverPriority

    }

    $oldLocations += New-AzCosmosDBLocationObject -LocationName $db.Locations[$i].LocationName -FailOverPriority $db.Locations[$i].FailoverPriority -IsZoneRedundant $db.Locations[$i].IsZoneRedundant

}

 

Write-Host before doing anything

Write-Host Incrementally adding back each region with AZ readiness on

Update-AzCosmosDBAccountRegion `

    -ResourceGroupName $resourceGroupName `

    -Name $accountName `

    -LocationObject $locations `

    -ErrorAction Stop

 

for ($i = 1; $i -lt $oldLocations.Count; $i = $i + 1) {

$setZoneRedundancy = supportsZoneRedundancy $oldLocations[$i].LocationName

$locations += New-AzCosmosDBLocationObject -LocationName $oldLocations[$i].LocationName -FailOverPriority $oldLocations[$i].FailoverPriority -IsZoneRedundant $setZoneRedundancy

Update-AzCosmosDBAccountRegion `

    -ResourceGroupName $resourceGroupName `

    -Name $accountName `

    -LocationObject $locations `

    -ErrorAction Stop

}

 

$failoverlocations = @()

$priority = 0

for ($i = 1; $i -lt $oldLocations.Count; $i = $i + 1) {

$priority = $i - 1

$setZoneRedundancy = supportsZoneRedundancy $oldLocations[$i].LocationName

$failoverlocations += New-AzCosmosDBLocationObject -LocationName $oldLocations[$i].LocationName -FailOverPriority $priority -IsZoneRedundant $setZoneRedundancy

}

 

Update-AzCosmosDBAccountRegion `

    -ResourceGroupName $resourceGroupName `

    -Name $accountName `

    -LocationObject $failoverlocations `

    -ErrorAction Stop

 

Write-Host Converting the default region to have AZ readiness

$setZoneRedundancy = supportsZoneRedundancy $oldLocations[0].LocationName

$priority = $failoverlocations.Count

$failoverlocations += New-AzCosmosDBLocationObject -LocationName $oldLocations[0].LocationName -FailOverPriority $priority -IsZoneRedundant $setZoneRedundancy

Update-AzCosmosDBAccountRegion `

    -ResourceGroupName $resourceGroupName `

    -Name $accountName `

    -LocationObject $failoverlocations `

    -ErrorAction Stop

 

Update-AzCosmosDBAccount `

  -ResourceGroupName $resourceGroupName `

  -Name $accountName `

  -EnableMultipleWriteLocations $False `

  -ErrorAction Stop

 

Write-Host Processing Read Regions next for AZ readiness

$oldLocations = @()

for ($i = $locations.Count; $i -lt $db.Locations.Count; $i = $i + 1) {

    $oldLocations += New-AzCosmosDBLocationObject -LocationName $db.Locations[$i].LocationName -FailOverPriority $db.Locations[$i].FailoverPriority -IsZoneRedundant $db.Locations[$i].IsZoneRedundant

}

$priority = $locations.Count

for ($i = 0; $i -lt $oldLocations.Count; $i = $i + 1) {

$setZoneRedundancy = supportsZoneRedundancy $oldLocations[$i].LocationName

$locations += New-AzCosmosDBLocationObject -LocationName $oldLocations[$i].LocationName -FailOverPriority $priority -IsZoneRedundant $setZoneRedundancy

$priority = $priority + 1

}

 

Write-Host all regions as per old configuration added

Write-Host retaining order of failover priority as per original configuration next

$oldLocations = @()

for ($i = 0; $i -lt $db.Locations.Count; $i = $i + 1) {

    $oldLocations += New-AzCosmosDBLocationObject -LocationName $db.Locations[$i].LocationName -FailOverPriority $db.Locations[$i].FailoverPriority -IsZoneRedundant $db.Locations[$i].IsZoneRedundant

}

 

$regions = @()

foreach ($l in $oldLocations){$regions += $l.LocationName}

Update-AzCosmosDBAccountFailoverPriority  `

    -ResourceGroupName $resourceGroupName `

    -Name $accountName `

    -FailoverPolicy $regions `

    -ErrorAction Stop

 

Write-Host Ensuring EnableMultipleWriteLocations Setting remains so

Update-AzCosmosDBAccount `

  -ResourceGroupName $resourceGroupName `

  -Name $accountName `

  -EnableMultipleWriteLocations $oldWriteAll `

  -ErrorAction Stop

 

}

Write-Host CosmosDB multi-region account is now AZ-Ready


/* Testing:

Multi-region account migration to AZ-Readiness begin

 

 

Id                : ravirajamani-test-zone-down-eastus2

LocationName      : East US 2

DocumentEndpoint  : https://ravirajamani-test-zone-down-eastus2.documents.azure.com:443/

ProvisioningState : Succeeded

FailoverPriority  : 0

IsZoneRedundant   : True

 

Id                : ravirajamani-test-zone-down-eastus

LocationName      : East US

DocumentEndpoint  : https://ravirajamani-test-zone-down-eastus.documents.azure.com:443/

ProvisioningState : Succeeded

FailoverPriority  : 1

IsZoneRedundant   : False

 

Processing Write regions first for AZ readiness

Update-AzCosmosDBAccount -ResourceGroupName ravirajamani_test_rg -Name ravirajamani-test-zone-down -EnableMultipleWriteLocations True

LocationName     : East US 2

FailoverPriority : 0

IsZoneRedundant  : True

 

before doing anything

Incrementally adding back each region with AZ readiness on

Update-AzCosmosDBAccountRegion -ResourceGroupName ravirajamani_test_rg -Name ravirajamani-test-zone-down -LocationObject Microsoft.Azure.Commands.CosmosDB.Models.PSLocation

Update-AzCosmosDBAccountRegion -ResourceGroupName ravirajamani_test_rg -Name ravirajamani-test-zone-down -LocationObject

Converting the default region to have AZ readiness

Update-AzCosmosDBAccountRegion -ResourceGroupName ravirajamani_test_rg -Name ravirajamani-test-zone-down -LocationObject Microsoft.Azure.Commands.CosmosDB.Models.PSLocation

Update-AzCosmosDBAccount -ResourceGroupName ravirajamani_test_rg -Name ravirajamani-test-zone-down -EnableMultipleWriteLocations False

LocationName     : East US 2

FailoverPriority : 0

IsZoneRedundant  : True

 

Processing Read Regions next for AZ readiness

LocationName     : East US 2

FailoverPriority : 0

IsZoneRedundant  : True

 

LocationName     : East US

FailoverPriority : 1

IsZoneRedundant  : True

 

all regions as per old configuration added

retaining order of failover priority as per original configuration next

LocationName     : East US 2

FailoverPriority : 0

IsZoneRedundant  : True

 

LocationName     : East US

FailoverPriority : 1

IsZoneRedundant  : False

 

Update-AzCosmosDBAccountFailoverPriority -ResourceGroupName ravirajamani_test_rg -Name ravirajamani-test-zone-down -FailoverPolicy East US 2 East US

Ensuring EnableMultipleWriteLocations Setting remains so

Update-AzCosmosDBAccount -ResourceGroupName ravirajamani_test_rg -Name ravirajamani-test-zone-down -EnableMultipleWriteLocations False

CosmosDB multi-region account is now AZ-Ready

*/


No comments:

Post a Comment