diff --git a/sdk/storage/Azure.Storage.DataMovement/src/CommitChunkHandler.cs b/sdk/storage/Azure.Storage.DataMovement/src/CommitChunkHandler.cs index 6c564f91d05f8..603b9c89579e1 100644 --- a/sdk/storage/Azure.Storage.DataMovement/src/CommitChunkHandler.cs +++ b/sdk/storage/Azure.Storage.DataMovement/src/CommitChunkHandler.cs @@ -92,11 +92,12 @@ public CommitChunkHandler( SingleReader = true, }); _cancellationToken = cancellationToken; - _processStageChunkEvents = Task.Run(() => NotifyOfPendingStageChunkEvents()); // Set bytes transferred to block size because we transferred the initial block _bytesTransferred = blockSize; + _processStageChunkEvents = Task.Run(() => NotifyOfPendingStageChunkEvents()); + _blockSize = blockSize; _transferOrder = transferOrder; if (_transferOrder == DataTransferOrder.Sequential) @@ -155,7 +156,10 @@ private async Task NotifyOfPendingStageChunkEvents() // Read one event argument at a time. StageChunkEventArgs args = await _stageChunkChannel.Reader.ReadAsync(_cancellationToken).ConfigureAwait(false); - Interlocked.Add(ref _bytesTransferred, args.BytesTransferred); + // don't need to use Interlocked.Add() as we are reading one event at a time + // and _bytesTransferred is not being read/updated from any other thread + _bytesTransferred += args.BytesTransferred; + // Report the incremental bytes transferred _reportProgressInBytes(args.BytesTransferred); diff --git a/sdk/storage/Azure.Storage.DataMovement/src/DownloadChunkHandler.cs b/sdk/storage/Azure.Storage.DataMovement/src/DownloadChunkHandler.cs index 856f151b8e296..7e7a7164ff15f 100644 --- a/sdk/storage/Azure.Storage.DataMovement/src/DownloadChunkHandler.cs +++ b/sdk/storage/Azure.Storage.DataMovement/src/DownloadChunkHandler.cs @@ -107,6 +107,11 @@ public DownloadChunkHandler( ClientDiagnostics clientDiagnostics, CancellationToken cancellationToken) { + // Set bytes transferred to the length of bytes we got back from the initial + // download request + _bytesTransferred = currentTransferred; + _currentRangeIndex = 0; + // Create channel of finished Stage Chunk Args to update the bytesTransferred // and for ending tasks like commit block. // The size of the channel should never exceed 50k (limit on blocks in a block blob). @@ -143,10 +148,6 @@ public DownloadChunkHandler( _queueCompleteFileDownload = behaviors.QueueCompleteFileDownload ?? throw Errors.ArgumentNull(nameof(behaviors.QueueCompleteFileDownload)); - // Set bytes transferred to the length of bytes we got back from the initial - // download request - _bytesTransferred = currentTransferred; - _currentRangeIndex = 0; _rangesCount = ranges.Count; // Set size of the list of null streams _rangesCompleted = new ConcurrentDictionary(); @@ -338,8 +339,9 @@ private async Task InvokeFailedEvent(Exception ex) /// private void UpdateBytesAndRange(long bytesDownloaded) { - Interlocked.Add(ref _bytesTransferred, bytesDownloaded); - Interlocked.Increment(ref _currentRangeIndex); + // don't need to use Interlocked since this is the only thread reading and updating these values + _bytesTransferred += bytesDownloaded; + _currentRangeIndex++; _reportProgressInBytes(bytesDownloaded); } }