Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Service fabric] Retry for remaining commands #7607

Merged
merged 3 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,7 @@

$ApplicationManifestPath = "$AppPkgPathToUse\ApplicationManifest.xml"

try
{
$global:operationId = $SF_Operations.TestClusterConnection
[void](Test-ServiceFabricClusterConnection)
}
catch
{
Write-Warning (Get-VstsLocString -Key SFSDK_UnableToVerifyClusterConnection)
throw
}
Test-ServiceFabricClusterConnectionAction

# If ApplicationName is not specified on command line get application name from Application Parameter file.
if (!$ApplicationName)
Expand Down Expand Up @@ -259,8 +250,7 @@

Write-Host (Get-VstsLocString -Key SFSDK_CopyingAppToImageStore)
# Get image store connection string
$global:operationId = $SF_Operations.GetClusterManifest
$clusterManifestText = Get-ServiceFabricClusterManifest
$clusterManifestText = Get-ServiceFabricClusterManifestAction
$imageStoreConnectionString = Get-ImageStoreConnectionStringFromClusterManifest ([xml] $clusterManifestText)

$applicationPackagePathInImageStore = $names.ApplicationTypeName
Expand Down Expand Up @@ -310,9 +300,8 @@
Write-Host (Get-VstsLocString -Key SFSDK_RegisterAppType)
Register-ServiceFabricApplicationTypeAction -RegisterParameters $registerParameters -ApplicationTypeName $names.ApplicationTypeName -ApplicationTypeVersion $names.ApplicationTypeVersion

$global:operationId = $SF_Operations.RemoveApplicationPackage
Write-Host (Get-VstsLocString -Key SFSDK_RemoveAppPackage)
Remove-ServiceFabricApplicationPackage -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore -ImageStoreConnectionString $imageStoreConnectionString
Remove-ServiceFabricApplicationPackageAction -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore -ImageStoreConnectionString $imageStoreConnectionString
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,7 @@ function Publish-UpgradedServiceFabricApplication
return
}


try
{
$global:operationId = $SF_Operations.TestClusterConnection
[void](Test-ServiceFabricClusterConnection)
}
catch
{
Write-Warning (Get-VstsLocString -Key SFSDK_UnableToVerifyClusterConnection)
throw
}
Test-ServiceFabricClusterConnectionAction

$ApplicationTypeAlreadyRegistered = $false
if ($Action.Equals('RegisterAndUpgrade') -or $Action.Equals('Register'))
Expand Down Expand Up @@ -235,14 +225,12 @@ function Publish-UpgradedServiceFabricApplication
if (!$reg -or !$ApplicationTypeAlreadyRegistered)
{
# Get image store connection string
$global:operationId = $SF_Operations.GetClusterManifest
$clusterManifestText = Get-ServiceFabricClusterManifest
$clusterManifestText = Get-ServiceFabricClusterManifestAction
$imageStoreConnectionString = Get-ImageStoreConnectionStringFromClusterManifest ([xml] $clusterManifestText)

if (!$SkipPackageValidation)
{
$global:operationId = $SF_Operations.TestApplicationPackage
$packageValidationSuccess = (Test-ServiceFabricApplicationPackage $AppPkgPathToUse -ImageStoreConnectionString $imageStoreConnectionString)
$packageValidationSuccess = (Test-ServiceFabricApplicationPackageAction -AppPkgPath $AppPkgPathToUse -ImageStoreConnectionString $imageStoreConnectionString)
if (!$packageValidationSuccess)
{
$errMsg = (Get-VstsLocString -Key SFSDK_PackageValidationFailed -ArgumentList $ApplicationPackagePath)
Expand Down Expand Up @@ -331,8 +319,7 @@ function Publish-UpgradedServiceFabricApplication
}

Write-Host (Get-VstsLocString -Key SFSDK_StartAppUpgrade)
$global:operationId = $SF_Operations.StartApplicationUpgrade
Start-ServiceFabricApplicationUpgrade @UpgradeParameters
Start-ServiceFabricApplicationUpgradeAction -UpgradeParameters $UpgradeParameters
}
catch
{
Expand Down Expand Up @@ -360,7 +347,11 @@ function Publish-UpgradedServiceFabricApplication
}

Write-Host (Get-VstsLocString -Key SFSDK_WaitingForUpgrade)
$upgradeStatusFetcher = { Get-ServiceFabricApplicationUpgradeAction -ApplicationName $ApplicationName }
$upgradeStatusFetcher = {
$global:operationId = $SF_Operations.GetApplicationUpgradeStatus
Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
}

$upgradeRetryEvaluator = { param($upgradeStatus) return ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted") }
$upgradeStatus = Invoke-ActionWithRetries -Action $upgradeStatusFetcher `
-ResultRetryEvaluator $upgradeRetryEvaluator `
Expand Down
105 changes: 101 additions & 4 deletions Tasks/ServiceFabricDeployV1/ServiceFabricSDK/Utilities.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ function Get-ServiceFabricApplicationUpgradeAction
)

$global:operationId = $SF_Operations.GetApplicationUpgradeStatus
return Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
$getUpgradeAction = { Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName }
return Invoke-ActionWithDefaultRetries -Action $getUpgradeAction `
-RetryMessage (Get-VstsLocString -Key SFSDK_RetryingGetApplicationUpgrade)
}

function Copy-ServiceFabricApplicationPackageAction
Expand Down Expand Up @@ -518,8 +520,6 @@ function New-ServiceFabricApplicationAction
)

$global:operationId = $SF_Operations.CreateNewApplication


$createAction = { New-ServiceFabricApplication -ApplicationName $ApplicationName -ApplicationTypeName $ApplicationTypeName -ApplicationTypeVersion $ApplicationTypeVersion -ApplicationParameter $ApplicationParameter }
$exceptionRetryEvaluator = {
param($ex)
Expand All @@ -544,11 +544,108 @@ function New-ServiceFabricApplicationAction
{
Write-Host (Get-VstsLocString -Key SFSDK_CreateApplicationFailed)
# print application health status if create did not succeed
Trace-ServiceFabricApplicationHealth
Trace-ServiceFabricApplicationHealth -ApplicationName $ApplicationName
throw
}
}

function Start-ServiceFabricApplicationUpgradeAction
{
Param (
[hashtable]
$UpgradeParameters
)

$global:operationId = $SF_Operations.StartApplicationUpgrade
$startAction = { Start-ServiceFabricApplicationUpgrade @UpgradeParameters }
$exceptionRetryEvaluator = {
param($ex)

# If upgrade already started, don't retry
$upgradeStatus = Get-ServiceFabricApplicationUpgradeAction -ApplicationName $($UpgradeParameters["ApplicationName"])
if ($upgradeStatus -and ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont need to put braces for the 2nd expression. However, I m not clear on the logic here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is checking if upgrade is already in progress (status should not be RollingBackCompleted or RollingForwardCompleted). if so, it will not retry starting upgrade.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you are returning false in those cases, wouldn't that stop the retry?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is intended. The action being tried here is Start-Upgrade.. If upgrade has already started even though the call failed due to timeout, we would not like to try further.
Only in case of upgrade not started, we want to retry

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we differentiate between the last upgrade vs the current upgrade

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if current is first upgrade for application, upgrade status will be null, otherwise it will be RollingForwardCompleted or RollingBackCompleted depending upon whether last upgrade was success or rolled back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same logic which we use while waiting for upgrade to finish

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance for us to not even attempt retry if the server didn't even accept our retry request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing like server will not accept request. It will accept and either fail or proceed with upgrade. In either of these cases we don't want to retry.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense

{
return $false
}

return $true
}

try
{
Invoke-ActionWithDefaultRetries -Action $startAction `
-RetryMessage (Get-VstsLocString -Key SFSDK_RetryingUpgradeApplication) `
-ExceptionRetryEvaluator $exceptionRetryEvaluator `
-RetryableExceptions @("System.Fabric.FabricTransientException", "System.TimeoutException")
}
catch
{
# print application health status if starting upgrade did not succeed
Trace-ServiceFabricApplicationHealth -ApplicationName $($UpgradeParameters["ApplicationName"])
throw
}
}

function Test-ServiceFabricClusterConnectionAction
{
try
{
$global:operationId = $SF_Operations.TestClusterConnection
$testAction = { [void](Test-ServiceFabricClusterConnection) }
Invoke-ActionWithDefaultRetries -Action $testAction `
-RetryMessage (Get-VstsLocString -Key SFSDK_RetryingTestClusterConnection) `
-RetryableExceptions @("System.Fabric.FabricTransientException", "System.TimeoutException")
}
catch
{
Write-Warning (Get-VstsLocString -Key SFSDK_UnableToVerifyClusterConnection)
throw
}
}

function Test-ServiceFabricApplicationPackageAction
{
Param (
[string]
$AppPkgPath,

[string]
$ImageStoreConnectionString
)

$global:operationId = $SF_Operations.TestApplicationPackage
$testAction = { Test-ServiceFabricApplicationPackage -ApplicationPackagePath $AppPkgPath -ImageStoreConnectionString $ImageStoreConnectionString }
return Invoke-ActionWithDefaultRetries -Action $testAction `
-RetryMessage (Get-VstsLocString -Key SFSDK_RetryingTestAppPackage) `
-RetryableExceptions @("System.Fabric.FabricTransientException", "System.TimeoutException")
}

function Get-ServiceFabricClusterManifestAction
{
$global:operationId = $SF_Operations.GetClusterManifest
$manifestAction = { Get-ServiceFabricClusterManifest }
return Invoke-ActionWithDefaultRetries -Action $manifestAction `
-RetryMessage (Get-VstsLocString -Key SFSDK_RetryingGetClusterManifest) `
-RetryableExceptions @("System.Fabric.FabricTransientException", "System.TimeoutException")
}

function Remove-ServiceFabricApplicationPackageAction
{
Param (
[string]
$ApplicationPackagePathInImageStore,

[string]
$ImageStoreConnectionString
)

$global:operationId = $SF_Operations.RemoveApplicationPackage
$removeAction = { Remove-ServiceFabricApplicationPackage -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore -ImageStoreConnectionString $ImageStoreConnectionString }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the casing in $application starts with lower case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#fixed

Invoke-ActionWithDefaultRetries -Action $removeAction `
-RetryMessage (Get-VstsLocString -Key SFSDK_RetryingRemoveApplicationPackage) `
-RetryableExceptions @("System.Fabric.FabricTransientException", "System.TimeoutException")
}

function Trace-ServiceFabricClusterHealth
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,11 @@
"loc.messages.SFSDK_RetryingUnregisterApplicationType": "Retrying unregister..",
"loc.messages.SFSDK_RetryingRemoveApplication": "Retrying remove application..",
"loc.messages.SFSDK_RetryingCreateApplication": "Retrying create application..",
"loc.messages.SFSDK_ApplicationHealth": "Getting application health:"
"loc.messages.SFSDK_ApplicationHealth": "Getting application health:",
"loc.messages.SFSDK_RetryingUpgradeApplication": "Retrying application upgrade..",
"loc.messages.SFSDK_RetryingGetApplicationUpgrade": "Getting application upgrade status...",
"loc.messages.SFSDK_RetryingTestClusterConnection": "Testing connection to cluster..",
"loc.messages.SFSDK_RetryingTestAppPackage": "Testing application package..",
"loc.messages.SFSDK_RetryingGetClusterManifest": "Getting cluster manifest..",
"loc.messages.SFSDK_RetryingRemoveApplicationPackage": "Retrying to remove application package.."
}
24 changes: 24 additions & 0 deletions Tasks/ServiceFabricDeployV1/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,29 @@ describe('ServiceFabricDeploy Suite', function () {
it('Register application type should retry', (done) => {
psr.run(path.join(__dirname, 'RegisterApplicationTypeShouldRetry.ps1'), done);
})
it('Unregister application type should retry till success', (done) => {
psr.run(path.join(__dirname, 'UnregisterApplicationTypeShouldRetryTillSuccess.ps1'), done);
})
it('Unregister application type should retry', (done) => {
psr.run(path.join(__dirname, 'UnregisterApplicationTypeShouldRetry.ps1'), done);
})
it('Create application type should retry till success', (done) => {
psr.run(path.join(__dirname, 'CreateApplicationShouldRetryTillSuccess.ps1'), done);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this file already present

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they were part of previous PR. However, during conflict resolution, these changes were reverted in that PR.

})
it('Create application type should retry', (done) => {
psr.run(path.join(__dirname, 'CreateApplicationShouldRetry.ps1'), done);
})
it('Remove application type should retry till success', (done) => {
psr.run(path.join(__dirname, 'RemoveApplicationShouldRetryTillSuccess.ps1'), done);
})
it('Remove application type should retry', (done) => {
psr.run(path.join(__dirname, 'RemoveApplicationShouldRetry.ps1'), done);
})
it('Start application upgrade should retry till success', (done) => {
psr.run(path.join(__dirname, 'StartApplicationUpgradeShouldRetryTillSuccess.ps1'), done);
})
it('Start application upgrade should retry', (done) => {
psr.run(path.join(__dirname, 'StartApplicationUpgradeShouldRetry.ps1'), done);
})
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[CmdletBinding()]
param()

Import-Module ServiceFabric
. $PSScriptRoot\..\..\..\Tests\lib\Initialize-Test.ps1

$ApplicationTypeName = "app type name"
$ApplicationTypeVersion = "app type version"
$applicationPackagePathInImageStore = "image path"

$UpgradeParameters = @{
'ApplicationTypeName' = $ApplicationTypeName;
'ApplicationTypeVersion' = $ApplicationTypeVersion
'ApplicationParameter' = {}
}

$upgradeStatus = @{
'UpgradeState' = 'RollingForwardCompleted'
}
$global:startUpgradeAttempted = 0
$global:getRetriesAttempted = 0
$global:appHealthPrinted = $false

Register-Mock Start-ServiceFabricApplicationUpgrade {
$global:startUpgradeAttempted++
throw [System.Fabric.FabricTransientException]::new("Could not ping!")
} -- @UpgradeParameters

Register-Mock Get-ServiceFabricApplicationUpgrade {
$global:getRetriesAttempted++

if ($global:getRetriesAttempted -eq 3)
{
$upgradeStatus.UpgradeState = 'RollingForwardCompleted'
return $upgradeStatus
}

if ($global:getRetriesAttempted -eq 2)
{
return $null
}

throw [System.Fabric.FabricTransientException]::new("Could not ping!")
} -- -ApplicationName $ApplicationName

Register-Mock Get-ServiceFabricApplicationHealth {
$global:appHealthPrinted = $true
}

Register-Mock Start-Sleep {}
Register-Mock Write-VstsTaskError

# Act
. $PSScriptRoot\..\..\..\Tasks\ServiceFabricDeployV1\ps_modules\PowershellHelpers\Helpers.ps1
. $PSScriptRoot\..\..\..\Tasks\ServiceFabricDeployV1\ServiceFabricSDK\Utilities.ps1

# Act/Assert
Assert-Throws {
Start-ServiceFabricApplicationUpgradeAction -UpgradeParameters $UpgradeParameters
}
Assert-AreEqual 3 $global:startUpgradeAttempted "Number of start upgrade retries not correct"
Assert-AreEqual $true $global:appHealthPrinted "cluster health not printed in case of error"
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[CmdletBinding()]
param()

Import-Module ServiceFabric
. $PSScriptRoot\..\..\..\Tests\lib\Initialize-Test.ps1

$ApplicationTypeName = "app type name"
$ApplicationTypeVersion = "app type version"
$applicationPackagePathInImageStore = "image path"

$UpgradeParameters = @{
'ApplicationTypeName' = $ApplicationTypeName;
'ApplicationTypeVersion' = $ApplicationTypeVersion
'ApplicationParameter' = {}
}

$upgradeStatus = @{
'UpgradeState' = 'RollingForwardCompleted'
}
$global:startUpgradeAttempted = 0
$global:getRetriesAttempted = 0
$global:appHealthPrinted = $false

Register-Mock Start-ServiceFabricApplicationUpgrade {
$global:startUpgradeAttempted++
throw [System.Fabric.FabricTransientException]::new("Could not ping!")
} -- @UpgradeParameters

Register-Mock Get-ServiceFabricApplicationUpgrade {
$global:getRetriesAttempted++

if ($global:getRetriesAttempted -eq 6)
{
$upgradeStatus.UpgradeState = 'RollingForwardInProgress'
return $upgradeStatus
}

if ($global:getRetriesAttempted -eq 3)
{
$upgradeStatus.UpgradeState = 'RollingForwardCompleted'
return $upgradeStatus
}

throw [System.Fabric.FabricTransientException]::new("Could not ping!")
} -- -ApplicationName $ApplicationName

Register-Mock Get-ServiceFabricApplicationHealth {
$global:appHealthPrinted = $true
}

Register-Mock Start-Sleep {}
Register-Mock Write-VstsTaskError

# Act
. $PSScriptRoot\..\..\..\Tasks\ServiceFabricDeployV1\ps_modules\PowershellHelpers\Helpers.ps1
. $PSScriptRoot\..\..\..\Tasks\ServiceFabricDeployV1\ServiceFabricSDK\Utilities.ps1

# Act/Assert
Start-ServiceFabricApplicationUpgradeAction -UpgradeParameters $UpgradeParameters
Assert-AreEqual 2 $global:startUpgradeAttempted "Number of start upgrade retries not correct"
Loading