From bf935d2bf9dbc84a4a760dfdb0938150101c5e94 Mon Sep 17 00:00:00 2001 From: Serkant Karaca Date: Fri, 28 Jul 2017 16:26:38 -0700 Subject: [PATCH] Fix lease-lost decision logic (#179) * Provide inner exception's message as LeaseLostException's message. * Fix the lease-lost decision logic. --- .../AzureStorageCheckpointLeaseManager.cs | 59 ++++++------------- .../LeaseLostException.cs | 2 +- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/src/Microsoft.Azure.EventHubs.Processor/AzureStorageCheckpointLeaseManager.cs b/src/Microsoft.Azure.EventHubs.Processor/AzureStorageCheckpointLeaseManager.cs index 243cf39..b41b9dd 100644 --- a/src/Microsoft.Azure.EventHubs.Processor/AzureStorageCheckpointLeaseManager.cs +++ b/src/Microsoft.Azure.EventHubs.Processor/AzureStorageCheckpointLeaseManager.cs @@ -333,14 +333,7 @@ async Task AcquireLeaseCoreAsync(AzureBlobLease lease) } catch (StorageException se) { - if (WasLeaseLost(partitionId, se)) - { - retval = false; - } - else - { - throw; - } + throw HandleStorageException(partitionId, se); } return retval; @@ -362,12 +355,7 @@ async Task RenewLeaseCoreAsync(AzureBlobLease lease) } catch (StorageException se) { - if (WasLeaseLost(partitionId, se)) - { - throw new LeaseLostException(partitionId, se); - } - - throw; + throw HandleStorageException(partitionId, se); } return true; @@ -398,12 +386,7 @@ async Task ReleaseLeaseCoreAsync(AzureBlobLease lease) } catch (StorageException se) { - if (WasLeaseLost(partitionId, se)) - { - throw new LeaseLostException(partitionId, se); - } - - throw; + throw HandleStorageException(partitionId, se); } return true; @@ -442,12 +425,7 @@ async Task UpdateLeaseCoreAsync(AzureBlobLease lease) } catch (StorageException se) { - if (WasLeaseLost(partitionId, se)) - { - throw new LeaseLostException(partitionId, se); - } - - throw; + throw HandleStorageException(partitionId, se); } return true; @@ -462,31 +440,32 @@ async Task DownloadLeaseAsync(string partitionId, CloudBlockBlob AzureBlobLease blobLease = new AzureBlobLease(rehydrated, blob); return blobLease; } - - bool WasLeaseLost(string partitionId, StorageException se) + + Exception HandleStorageException(string partitionId, StorageException se) { - bool retval = false; - ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.Id, partitionId, "WAS LEASE LOST?"); - ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.Id, partitionId, "HttpStatusCode " + se.RequestInformation.HttpStatusCode); + ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.Id, partitionId, "HandleStorageException - HttpStatusCode " + se.RequestInformation.HttpStatusCode); if (se.RequestInformation.HttpStatusCode == 409 || // conflict se.RequestInformation.HttpStatusCode == 412) // precondition failed { StorageExtendedErrorInformation extendedErrorInfo = se.RequestInformation.ExtendedErrorInformation; + if (extendedErrorInfo != null) { string errorCode = extendedErrorInfo.ErrorCode; - ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.Id, partitionId, "Error code: " + errorCode); - ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.Id, partitionId, "Error message: " + extendedErrorInfo.ErrorMessage); - if (errorCode == BlobErrorCodeStrings.LeaseLost || - errorCode == BlobErrorCodeStrings.LeaseIdMismatchWithLeaseOperation || - errorCode == BlobErrorCodeStrings.LeaseIdMismatchWithBlobOperation) - { - retval = true; - } + ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.Id, partitionId, "HandleStorageException - Error code: " + errorCode); + ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.Id, partitionId, "HandleStorageException - Error message: " + extendedErrorInfo.ErrorMessage); + } + + if (extendedErrorInfo == null || + extendedErrorInfo.ErrorCode == BlobErrorCodeStrings.LeaseLost || + extendedErrorInfo.ErrorCode == BlobErrorCodeStrings.LeaseIdMismatchWithLeaseOperation || + extendedErrorInfo.ErrorCode == BlobErrorCodeStrings.LeaseIdMismatchWithBlobOperation) + { + return new LeaseLostException(partitionId, se); } } - return retval; + return se; } CloudBlockBlob GetBlockBlobReference(string partitionId) diff --git a/src/Microsoft.Azure.EventHubs.Processor/LeaseLostException.cs b/src/Microsoft.Azure.EventHubs.Processor/LeaseLostException.cs index 3ecfc24..fbfc0a3 100644 --- a/src/Microsoft.Azure.EventHubs.Processor/LeaseLostException.cs +++ b/src/Microsoft.Azure.EventHubs.Processor/LeaseLostException.cs @@ -13,7 +13,7 @@ public class LeaseLostException : Exception readonly string partitionId; internal LeaseLostException(string partitionId, Exception innerException) - : base(string.Empty, innerException) + : base(innerException.Message, innerException) { if (partitionId == null) {