Exception Message: The client could not finish the operation within specified timeout. Killing the lease on a file in Azure BLOB

Exception Message: The client could not finish the operation within specified timeout.

When backing up a database to the cloud if the network connectivity is slow or if the database backup operation was halted in between then the blob (backup file) remains locked in the Azure storage space since the operations didn’t complete. In such cases the DBA needs to clean up such unwanted and incomplete backups but won’t be able to since the lease is still active on the blob file. Very similar to how an Xclusive lock is acquired on the data page. Until this lock/ lease is released the file remains in the blob.

You can connect to the azure blob storage account in order to check if the database backup file has an active lease on it.

In this case we cannot delete the file until the lease is released. The solution is to run the power shell script below

Step 1 :- Open Notepad and copy paste the below content and save the file with a ps extension

param([Parameter(Mandatory=$true)] [string] $STORAGEACCOUNT, [Parameter(Mandatory=$true)] [string] $STORAGEKEY, [Parameter(Mandatory=$true)] [string] $BLOBCONTAINER, [Parameter(Mandatory=$true)] [string] $STORAGEASSEMBLYPATH) # Well known

RESTORE Lease ID $RESTORELEASEID = "BAC2BAC2BAC2BAC2BAC2BAC2BAC2BAC2" # LOAD the storage assembly without locking the FILE
FOR the duration OF the PowerShell session $BYTES = [System.IO.File]::ReadAllBytes($STORAGEASSEMBLYPATH) [System.Reflection.Assembly]::LOAD ($BYTES) $CRED = New - OBJECT 'Microsoft.WindowsAzure.Storage.Auth.StorageCredentials' $STORAGEACCOUNT
    ,$STORAGEKEY $CLIENT = New - OBJECT 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient' "https://$storageAccount.blob.core.windows.net"
    ,$CRED $CONTAINER = $CLIENT.GetContainerReference($BLOBCONTAINER) #list ALL the blobs $ALLBLOBS = $CONTAINER.ListBlobs() $LOCKEDBLOBS = @() # filter blobs that are have Lease STATUS AS "locked" foreach($BLOB IN $ALLBLOBS) { $BLOBPROPERTIES = $BLOB.Properties

IF ($BLOBPROPERTIES.LeaseStatus - eq "Locked") { $LOCKEDBLOBS += $BLOB } }
    IF ($LOCKEDBLOBS.Count - eq 0) { Write - Host " There are no blobs with locked lease status" }
        IF ($LOCKEDBLOBS.Count - gt 0) { write - host "Breaking leases" foreach($BLOB IN $LOCKEDBLOBS) { try { $BLOB.AcquireLease($NULL, $RESTORELEASEID, $NULL, $NULL, $NULL) Write - Host "The lease on $($blob.Uri) is a restore lease" } catch [Microsoft.WindowsAzure.Storage.StorageException] {
            IF ($_. Exception.RequestInformation.HttpStatusCode - eq 409) { Write - Host "The lease on $($blob.Uri) is not a restore lease" } } Write - Host "Breaking lease on $($blob.Uri)" $BLOB.BreakLease($( New - TimeSpan)
                ,$NULL ) | OUT - NULL } }

Step 2:- Run the powershell script in Powershell 3.0 via command prompt using the command below

powershell -noexit “& “”Path of the powerhsell file clearlease.ps1″””

Step 3:- Enter the details required which include the storageid , key , container name and the path for the azure Storage assembly path (usually found in Binn folder of SQL install path).

Step 4:- Wait until the lease is expired you should see messages like

Breaking lease on URL for strorage account container.

Please Consider Subscribing

Leave a Reply