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

Normalize Cancellation Handling for STOW #2877

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Microsoft.Health.Dicom.Core.Messages.Store;
using Microsoft.Health.Dicom.Tests.Common;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
using DicomValidationException = FellowOakDicom.DicomValidationException;

Expand Down Expand Up @@ -545,6 +546,48 @@ public async Task GivenRequiredStudyInstanceUid_WhenProcessed_ThenItShouldBePass
await ExecuteAndValidateAsync(dicomInstanceEntry);
}

[Fact]
public async Task GivenFetchCancellation_WhenProcessed_ThenItShouldHaveThrown()
{
using CancellationTokenSource tokenSource = new CancellationTokenSource();
IDicomInstanceEntry dicomInstanceEntry = Substitute.For<IDicomInstanceEntry>();
dicomInstanceEntry.GetDicomDatasetAsync(tokenSource.Token).Returns(ValueTask.FromException<DicomDataset>(new TaskCanceledException()));

await Assert.ThrowsAsync<TaskCanceledException>(() => _storeService.ProcessAsync(
new IDicomInstanceEntry[] { dicomInstanceEntry },
null,
cancellationToken: tokenSource.Token));
}

[Fact]
public async Task GivenValidationCancellation_WhenProcessed_ThenItShouldHaveThrown()
{
using CancellationTokenSource tokenSource = new CancellationTokenSource();
IDicomInstanceEntry dicomInstanceEntry = Substitute.For<IDicomInstanceEntry>();
dicomInstanceEntry.GetDicomDatasetAsync(tokenSource.Token).Returns(_dicomDataset1);
_dicomDatasetValidator.ValidateAsync(_dicomDataset1, null, tokenSource.Token).ThrowsAsync<TaskCanceledException>();

await Assert.ThrowsAsync<TaskCanceledException>(() => _storeService.ProcessAsync(
new IDicomInstanceEntry[] { dicomInstanceEntry },
null,
cancellationToken: tokenSource.Token));
}

[Fact]
public async Task GivenStowCancellation_WhenProcessed_ThenItShouldHaveThrown()
{
using CancellationTokenSource tokenSource = new CancellationTokenSource();
IDicomInstanceEntry dicomInstanceEntry = Substitute.For<IDicomInstanceEntry>();
dicomInstanceEntry.GetDicomDatasetAsync(tokenSource.Token).Returns(_dicomDataset1);
_storeOrchestrator.StoreDicomInstanceEntryAsync(dicomInstanceEntry, tokenSource.Token).ThrowsAsync(new DataStoreException(new TaskCanceledException()));

Exception actual = await Assert.ThrowsAsync<DataStoreException>(() => _storeService.ProcessAsync(
new IDicomInstanceEntry[] { dicomInstanceEntry },
null,
cancellationToken: tokenSource.Token));
Assert.IsType<TaskCanceledException>(actual.InnerException);
}

private Task ExecuteAndValidateAsync(params IDicomInstanceEntry[] dicomInstanceEntries)
=> ExecuteAndValidateAsync(requiredStudyInstanceUid: null, dicomInstanceEntries);

Expand Down
54 changes: 22 additions & 32 deletions src/Microsoft.Health.Dicom.Core/Features/Store/StoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,18 @@ public async Task<StoreResponse> ProcessAsync(
}
}
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
ushort failureCode = FailureReasonCodes.ProcessingFailure;

switch (ex)
ushort failureCode = ex switch
{
case DicomValidationException _:
failureCode = FailureReasonCodes.ValidationFailure;
break;

case DatasetValidationException dicomDatasetValidationException:
failureCode = dicomDatasetValidationException.FailureCode;
break;

case ValidationException _:
failureCode = FailureReasonCodes.ValidationFailure;
break;
}
DatasetValidationException dve => dve.FailureCode,
DicomValidationException or ValidationException => FailureReasonCodes.ValidationFailure,
_ => FailureReasonCodes.ProcessingFailure,
};

LogValidationFailedDelegate(_logger, index, failureCode, ex);

Expand All @@ -234,26 +228,22 @@ public async Task<StoreResponse> ProcessAsync(
);
return length;
}
catch (ConditionalExternalException cee) when (cee.IsExternal)
{
throw;
}
catch (DataStoreException dse) when (dse.InnerException is OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
ushort failureCode = FailureReasonCodes.ProcessingFailure;

switch (ex)
ushort failureCode = ex switch
{
case DataStoreException { IsExternal: true }:
throw;

case DataStoreRequestFailedException { IsExternal: true }:
throw;

case PendingInstanceException _:
failureCode = FailureReasonCodes.PendingSopInstance;
break;

case InstanceAlreadyExistsException _:
failureCode = FailureReasonCodes.SopInstanceAlreadyExists;
break;
}
PendingInstanceException => FailureReasonCodes.PendingSopInstance,
InstanceAlreadyExistsException => FailureReasonCodes.SopInstanceAlreadyExists,
_ => FailureReasonCodes.ProcessingFailure,
};

LogFailedToStoreDelegate(_logger, index, failureCode, ex);

Expand Down