Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] [DataMovement] Improve checking on part completion for pause/stop/completion status #45038

Merged
merged 7 commits into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions sdk/storage/Azure.Storage.DataMovement/src/JobPartInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ internal abstract class JobPartInternal
/// </summary>
public SyncAsyncEventHandler<TransferItemCompletedEventArgs> SingleTransferCompletedEventHandler { get; internal set; }

private List<Task<bool>> _chunkTasks;
private List<TaskCompletionSource<bool>> _chunkTaskSources;
/// <summary>
/// Represents the current state of the job part.
/// </summary>
private int _currentChunkCount;
private int _completedChunkCount;
protected bool _queueingTasks = false;

/// <summary>
Expand Down Expand Up @@ -195,8 +198,8 @@ internal JobPartInternal(
StorageResourceCreationPreference.FailIfExists : createMode;

Length = length;
_chunkTasks = new List<Task<bool>>();
_chunkTaskSources = new List<TaskCompletionSource<bool>>();
_currentChunkCount = 0;
_completedChunkCount = 0;
}

public void SetQueueChunkDelegate(QueueChunkDelegate chunkDelegate)
Expand All @@ -212,26 +215,20 @@ public void SetQueueChunkDelegate(QueueChunkDelegate chunkDelegate)
/// <returns></returns>
public async Task QueueChunkToChannelAsync(Func<Task> chunkTask)
{
// Attach TaskCompletionSource
TaskCompletionSource<bool> chunkCompleted = new TaskCompletionSource<bool>(
false,
TaskCreationOptions.RunContinuationsAsynchronously);
_chunkTaskSources.Add(chunkCompleted);
_chunkTasks.Add(chunkCompleted.Task);

Interlocked.Increment(ref _currentChunkCount);
await QueueChunk(
async () =>
{
try
{
await Task.Run(chunkTask).ConfigureAwait(false);
chunkCompleted.SetResult(true);
await CheckAndUpdateCancellationStateAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
await InvokeFailedArg(ex).ConfigureAwait(false);
}
Interlocked.Increment(ref _completedChunkCount);
await CheckAndUpdateCancellationStateAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
}

Expand Down Expand Up @@ -570,7 +567,7 @@ internal async Task CheckAndUpdateCancellationStateAsync()
if (JobPartStatus.State == DataTransferState.Pausing ||
JobPartStatus.State == DataTransferState.Stopping)
{
if (!_queueingTasks && _chunkTasks.All((Task task) => (task.IsCompleted)))
if (!_queueingTasks && _currentChunkCount == _completedChunkCount)
{
DataTransferState newState = JobPartStatus.State == DataTransferState.Pausing ?
DataTransferState.Paused :
Expand Down
Loading