/// <summary> /// Renews the lease /// </summary> /// <param name="lease">The lease to renew</param> /// <returns>True if the lease is renewed successfully</returns> private async Task <bool> RenewLeaseCoreAsync(AzureBlobLease lease) { CloudBlockBlob leaseBlob = lease.Blob; string partitionId = lease.PartitionId; using (_logger.BeginScope("Renew Lease Core")) { try { _logger.LogDebug("Core renewal for partition {partitionId}", lease.PartitionId); using (_storagePerformanceSummary.Time()) { await leaseBlob.RenewLeaseAsync(AccessCondition.GenerateLeaseCondition(lease.Token), _defaultRequestOptions, _operationContext).ConfigureAwait(false); } _logger.LogDebug("Core renewal completed for partition {partitionId}", lease.PartitionId); } catch (StorageException e) { var se = AzureBlobCommon.CheckForLeaseLostException(partitionId, e, _logger); _logger.LogError(se, "Renewing lease failed for partition {partitionId}", lease.PartitionId); throw se; } } return(true); }
/// <inheritdoc /> public async Task <bool> ReleaseLeaseAsync(Lease lease) { using (_logger.BeginScope("Renew Lease")) { _logger.LogInformation("Releasing lease: Partition Id: {partitionId}", lease.PartitionId); AzureBlobLease azureLease = lease as AzureBlobLease; CloudBlockBlob leaseBlob = azureLease.Blob; string partitionId = azureLease.PartitionId; try { string leaseId = lease.Token; AzureBlobLease releasedCopy = new AzureBlobLease(azureLease, leaseBlob) { Token = string.Empty, Owner = string.Empty }; leaseBlob.Metadata.Remove(MetaDataOwnerName); _logger.LogDebug("Setting metadata for partition {partitionId}", partitionId); using (_storagePerformanceSummary.Time()) { await leaseBlob.SetMetadataAsync(AccessCondition.GenerateLeaseCondition(leaseId), _defaultRequestOptions, _operationContext).ConfigureAwait(false); } _logger.LogDebug("Uploading lease data for partition {partitionId}", partitionId); using (_storagePerformanceSummary.Time()) { await leaseBlob.UploadTextAsync(JsonConvert.SerializeObject(releasedCopy), _leaseEncoding, AccessCondition.GenerateLeaseCondition(leaseId), _defaultRequestOptions, _operationContext).ConfigureAwait(false); } _logger.LogDebug("Release the lease for for partition {partitionId}", partitionId); using (_storagePerformanceSummary.Time()) { await leaseBlob.ReleaseLeaseAsync(AccessCondition.GenerateLeaseCondition(leaseId), _defaultRequestOptions, _operationContext).ConfigureAwait(false); } } catch (StorageException e) { var se = AzureBlobCommon.CheckForLeaseLostException(partitionId, e, _logger); _logger.LogError(se, "Error releasing lease for partition {partitionId}", lease.PartitionId); throw se; } } return(true); }
private async Task UploadAsync(CancellationToken ct, string item, string uploadPath, SemaphoreSlim clientThrottle, bool isLeaseRequired) { if (!File.Exists(item)) { throw new Exception(string.Format("The file '{0}' does not exist.", item)); } await clientThrottle.WaitAsync(); string leaseId = string.Empty; AzureBlobLease blobLease = new AzureBlobLease(feed.AccountName, feed.AccountKey, string.Empty, feed.ContainerName, uploadPath, Log, "60", "10"); if (isLeaseRequired) { try { leaseId = blobLease.Acquire(); } catch (Exception) { Log.LogError($"Unable to obtain lease on {uploadPath}"); } } try { Log.LogMessage($"Uploading {item} to {uploadPath}."); UploadClient uploadClient = new UploadClient(Log); await uploadClient.UploadBlockBlobAsync( ct, feed.AccountName, feed.AccountKey, feed.ContainerName, item, uploadPath, leaseId); } catch (Exception) { Log.LogError($"Unable to upload to {uploadPath}"); } finally { if (isLeaseRequired) { blobLease.Release(); } clientThrottle.Release(); } }
/// <summary> /// Updates the lease /// </summary> /// <param name="lease">The lease to update</param> /// <returns>True if updated successfully</returns> private async Task <bool> UpdateLeaseCoreAsync(AzureBlobLease lease) { var success = false; if (lease != null && !string.IsNullOrEmpty(lease.Token)) { } else { success = true; _logger.LogInformation("Updating lease: Partition Id: {partitionId}", lease.PartitionId); string partitionId = lease.PartitionId; string token = lease.Token; CloudBlockBlob leaseBlob = lease.Blob; try { string jsonToUpload = JsonConvert.SerializeObject(lease); _logger.LogInformation("Updating lease: Partition Id: {partitionId}, RawJson: {json}", lease.PartitionId, jsonToUpload); using (_storagePerformanceSummary.Time()) { await leaseBlob.UploadTextAsync(jsonToUpload, _leaseEncoding, AccessCondition.GenerateLeaseCondition(token), _defaultRequestOptions, _operationContext).ConfigureAwait(false); } _logger.LogDebug("Updated lease: Partition Id: {partitionId}", lease.PartitionId); } catch (StorageException e) { var se = AzureBlobCommon.CheckForLeaseLostException(partitionId, e, _logger); _logger.LogError(se, "Updating lease for partition {partitionId}", lease.PartitionId); throw se; } } return(success); }
/// <inheritdoc /> public async Task <Lease> CreateLeaseIfNotExistsAsync(string partitionId) { AzureBlobLease returnLease; using (_logger.BeginScope("Create Lease")) { _logger.LogInformation("Creating lease for partition {partitionId}", partitionId); try { CloudBlockBlob leaseBlob = _consumerGroupDirectory.GetBlockBlobReference(partitionId); returnLease = new AzureBlobLease(partitionId, leaseBlob); string jsonLease = JsonConvert.SerializeObject(returnLease); _logger.LogDebug("Creating Lease - Partition: {partition}, consumerGroup: {consumerGroup}", partitionId, _consumerGroupName); using (_storagePerformanceSummary.Time()) { await leaseBlob.UploadTextAsync(jsonLease, _leaseEncoding, _overwriteAccessCondition, _defaultRequestOptions, _operationContext).ConfigureAwait(false); } _logger.LogDebug("Created Lease - Partition: {partition}, consumerGroup: {consumerGroup}", partitionId, _consumerGroupName); } catch (StorageException se) { if (se.RequestInformation.ErrorCode == BlobErrorCodeStrings.BlobAlreadyExists || se.RequestInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMissing) { _logger.LogInformation(se, "Lease already exists, Partition {patitionId}", partitionId); returnLease = (AzureBlobLease) await GetLeaseAsync(partitionId).ConfigureAwait(false); } else { _leaseErrorCounter.Increment(); _logger.LogError(se, "CreateLeaseIfNotExist StorageException, Partition {patitionId}", partitionId); throw; } } } return(returnLease); }