Esempio n. 1
0
        /// <inheritdoc />
        public async Task <bool> AcquireLeaseAsync(Lease lease)
        {
            var azureLease = (AzureBlobLease)lease;

            CloudBlockBlob leaseBlob   = azureLease.Blob;
            bool           retval      = true;
            string         newLeaseId  = Guid.NewGuid().ToString();
            string         partitionId = lease.PartitionId;

            _leaseUpdateCounter.Increment();

            using (_logger.BeginScope("Acquire Lease"))
            {
                _logger.LogInformation("Acquiring lease for partition {partitionId}", lease.PartitionId);

                try
                {
                    bool   renewLease = false;
                    string newToken;

                    using (_storagePerformanceSummary.Time())
                    {
                        await leaseBlob.FetchAttributesAsync(_defaultAccessCondition, _defaultRequestOptions, _operationContext).ConfigureAwait(false);
                    }

                    if (leaseBlob.Properties.LeaseState == LeaseState.Leased)
                    {
                        if (string.IsNullOrEmpty(lease.Token))
                        {
                            return(false);
                        }

                        _logger.LogInformation("Need to ChangeLease: Partition Id: {partitionId}", lease.PartitionId);
                        renewLease = true;

                        using (_storagePerformanceSummary.Time())
                        {
                            newToken = await leaseBlob.ChangeLeaseAsync(newLeaseId, AccessCondition.GenerateLeaseCondition(lease.Token), _defaultRequestOptions, _operationContext).ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        _logger.LogInformation("Need to AcquireLease: Partition Id: {partitionId}", lease.PartitionId);

                        using (_storagePerformanceSummary.Time())
                        {
                            newToken = await leaseBlob.AcquireLeaseAsync(LeaseDuration, newLeaseId, _defaultAccessCondition, _defaultRequestOptions, _operationContext).ConfigureAwait(false);
                        }
                    }

                    lease.Token = newToken;
                    lease.Owner = _eventProcessorHostName;
                    lease.IncrementEpoch(); // Increment epoch each time lease is acquired or stolen by a new host

                    if (renewLease)
                    {
                        await RenewLeaseCoreAsync(azureLease).ConfigureAwait(false);
                    }
                    leaseBlob.Metadata[MetaDataOwnerName] = lease.Owner;

                    using (_storagePerformanceSummary.Time())
                    {
                        await leaseBlob.SetMetadataAsync(AccessCondition.GenerateLeaseCondition(lease.Token), _defaultRequestOptions, _operationContext).ConfigureAwait(false);
                    }

                    using (_storagePerformanceSummary.Time())
                    {
                        await leaseBlob.UploadTextAsync(JsonConvert.SerializeObject(lease), _leaseEncoding, AccessCondition.GenerateLeaseCondition(lease.Token), _defaultRequestOptions, _operationContext).ConfigureAwait(false);
                    }
                }
                catch (StorageException e)
                {
                    _leaseErrorCounter.Increment();
                    var se = AzureBlobCommon.CheckForLeaseLostException(partitionId, e, _logger);

                    _logger.LogError(se, "Error acquiring lease for partition {partitionId}", lease.PartitionId);
                    throw se;
                }
            }

            return(retval);
        }