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

[Draft] Minimize noisy Storage exceptions via DPG methods #27645

Closed
wants to merge 2 commits into from
Closed
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
159 changes: 159 additions & 0 deletions sdk/core/Azure.Core/src/Shared/RequestContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading;

namespace Azure.Core
{
/// <summary>
/// Shared source helpers for <see cref="RequestContext"/>.
/// </summary>
internal static class RequestContextExtensions
{
/// <summary>
/// Singleton <see cref="RequestContext"/> to use when we don't have an
/// instance.
/// </summary>
public static RequestContext Empty { get; } = new RequestContext();

/// <summary>
/// Create a RequestContext instance from a CancellationToken.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A <see cref="RequestContext"/> wrapping the cancellation token.
/// </returns>
public static RequestContext ToRequestContext(this CancellationToken cancellationToken) =>
cancellationToken == CancellationToken.None ?
Empty :
new() { CancellationToken = cancellationToken };

/// <summary>
/// Link a CancellationToken with an existing RequestContext.
/// </summary>
/// <param name="context">
/// The <see cref="RequestContext"/> for the current operation.
/// </param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>
/// The <see cref="RequestContext"/> for the current operation.
/// </returns>
public static RequestContext Link(this RequestContext context, CancellationToken cancellationToken)
{
if (cancellationToken == CancellationToken.None)
{
// Don't link if there's nothing there
}
else if (context.CancellationToken == CancellationToken.None)
{
// Don't link if we can just replace
context.CancellationToken = cancellationToken;
}
else
{
// Otherwise link them together
CancellationTokenSource source = CancellationTokenSource.CreateLinkedTokenSource(
context.CancellationToken,
cancellationToken);
context.CancellationToken = source.Token;
}
return context;
}

/// <summary>
/// Customizes the <see cref="ResponseClassifier"/> for this operation
/// to change the default <see cref="Response"/> classification behavior
/// so that it evalutes the anonymous classifier to determine whether
/// an error occured or not.
///
/// Custom classifiers are applied after all
/// <see cref="ResponseClassificationHandler"/> classifiers. This is
/// useful for cases where you'd like to prevent specific situations
/// from being treated as errors by logging and distributed tracing
/// policies -- that is, if a response is not classified as an error,
/// it will not appear as an error in logs or distributed traces.
/// </summary>
/// <param name="context">
/// The <see cref="RequestContext"/> for the current operation.
/// </param>
/// <param name="classifier">
/// A function to classify the response. It should return true if the
/// message contains an error, false if not an error, and null if it
/// was unable to classify.
/// </param>
/// <returns>
/// The <see cref="RequestContext"/> for the current operation.
/// </returns>
public static RequestContext AddClassifier(this RequestContext context, Func<HttpMessage, bool?> classifier)
{
Argument.AssertNotNull(context, nameof(context));
Argument.AssertNotNull(classifier, nameof(classifier));
context.AddClassifier(new LambdaClassifier(classifier));
return context;
}

// Anonymous reponse classifier
private class LambdaClassifier : ResponseClassificationHandler
{
private Func<HttpMessage, bool?> _classifier;

public LambdaClassifier(Func<HttpMessage, bool?> classifier) =>
_classifier = classifier;

public override bool TryClassify(HttpMessage message, out bool isError)
{
if (message.HasResponse)
{
bool? result = _classifier(message);
if (result is not null)
{
isError = result.Value;
return true;
}
}

isError = false;
return false;
}
}

/// <summary>
/// Customizes the <see cref="ResponseClassifier"/> for this operation
/// to change the default <see cref="Response"/> classification behavior
/// so that it considers the passed-in status code and error code to be
/// an error or not, as specified.
///
/// Custom classifiers are applied after all
/// <see cref="ResponseClassificationHandler"/> classifiers. This is
/// useful for cases where you'd like to prevent specific response
/// status and error codes from being treated as errors by logging and
/// distributed tracing policies -- that is, if a response is not
/// classified as an error, it will not appear as an error in logs or
/// distributed traces.
/// </summary>
/// <param name="context">
/// The <see cref="RequestContext"/> for the current operation.
/// </param>
/// <param name="statusCode">
/// The status code to customize classification for.
/// </param>
/// <param name="errorCode">
/// The error code to customize classification for.
/// </param>
/// <param name="isError">
/// Whether the passed-in status code should be classified as an error.
/// </param>
/// <returns>
/// The <see cref="RequestContext"/> for the current operation.
/// </returns>
public static RequestContext AddClassifier(this RequestContext context, int statusCode, string errorCode, bool isError) =>
context.AddClassifier(
message =>
message.Response.Status == statusCode &&
message.Response.Headers.TryGetValue("x-ms-error-code", out string code) &&
code == errorCode ?
isError :
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,9 @@ public BlobLeaseClient(Azure.Storage.Blobs.Specialized.BlobBaseClient client, st
protected virtual Azure.Storage.Blobs.BlobContainerClient BlobContainerClient { get { throw null; } }
public virtual string LeaseId { get { throw null; } }
public System.Uri Uri { get { throw null; } }
public virtual Azure.Response<Azure.Storage.Blobs.Models.BlobLease> Acquire(System.TimeSpan duration, Azure.RequestConditions conditions, Azure.RequestContext context) { throw null; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we determine that we want to add RequestContext to HLC methods vs using it internally.
I.e. why this instead of AcquireIfNotYetAcuired API that uses RequestContext internally?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's not really doing AcquireIfNotYetAcquired. It's doing AcquireIfBlobNotFoundOrContainerNotFoundOrLeaseAlreadyAcquired and we don't want to add an overload for that specific, non-general scenario.

public virtual Azure.Response<Azure.Storage.Blobs.Models.BlobLease> Acquire(System.TimeSpan duration, Azure.RequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Storage.Blobs.Models.BlobLease>> AcquireAsync(System.TimeSpan duration, Azure.RequestConditions conditions, Azure.RequestContext context) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Storage.Blobs.Models.BlobLease>> AcquireAsync(System.TimeSpan duration, Azure.RequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Storage.Blobs.Models.BlobLease> Break(System.TimeSpan? breakPeriod = default(System.TimeSpan?), Azure.RequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Storage.Blobs.Models.BlobLease>> BreakAsync(System.TimeSpan? breakPeriod = default(System.TimeSpan?), Azure.RequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Compile Include="$(AzureCoreSharedSources)ForwardsClientCallsAttribute.cs" LinkBase="SharedCore" />
<Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" LinkBase="SharedCore" />
<Compile Include="$(AzureCoreSharedSources)NoBodyResponseOfT.cs" LinkBase="SharedCore" />
<Compile Include="$(AzureCoreSharedSources)RequestContextExtensions.cs" LinkBase="SharedCore" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(AzureStorageSharedSources)ClientsideEncryption\*.cs" LinkBase="Shared" />
Expand Down
62 changes: 41 additions & 21 deletions sdk/storage/Azure.Storage.Blobs/src/BlobBaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4442,39 +4442,59 @@ private async Task<Response<bool>> ExistsInternal(
nameof(BlobBaseClient),
message:
$"{nameof(Uri)}: {Uri}");

string operationName = $"{nameof(BlobBaseClient)}.{nameof(Exists)}";

DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(BlobBaseClient)}.{nameof(Exists)}");
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
try
{
Response<BlobProperties> response = await GetPropertiesInternal(
conditions: default,
async: async,
cancellationToken: cancellationToken,
operationName)
.ConfigureAwait(false);
scope.Start();

return Response.FromValue(true, response.GetRawResponse());
}
catch (RequestFailedException storageRequestFailedException)
when (storageRequestFailedException.ErrorCode == BlobErrorCode.BlobNotFound
|| storageRequestFailedException.ErrorCode == BlobErrorCode.ContainerNotFound)
{
return Response.FromValue(false, default);
}
catch (RequestFailedException storageRequestFailedException)
when (storageRequestFailedException.ErrorCode == BlobErrorCode.BlobUsesCustomerSpecifiedEncryption)
{
return Response.FromValue(true, default);
// Get the blob properties, but don't throw any exceptions
RequestContext context =
new RequestContext()
{
ErrorOptions = ErrorOptions.NoThrow,
CancellationToken = cancellationToken
}
.AddClassifier(404, BlobErrorCode.BlobNotFound.ToString(), false)
.AddClassifier(404, BlobErrorCode.ContainerNotFound.ToString(), false)
.AddClassifier(409, BlobErrorCode.BlobUsesCustomerSpecifiedEncryption.ToString(), false);
Response response = async ?
await BlobRestClient.GetPropertiesAsync(context: context).ConfigureAwait(false) :
BlobRestClient.GetProperties(context: context);

// If the response was any error other than a 404
// ContainerNotFound, 404 BlobNotFound, or 409
// BlobUsesCustomerSpecifiedEncryption we will throw
if (response.IsError)
{
RequestFailedException ex = async ?
await ClientConfiguration.ClientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) :
ClientConfiguration.ClientDiagnostics.CreateRequestFailedException(response);
// Can't simply do the following because of https://github.com/Azure/azure-sdk-for-net/issues/27614
// ex = new RequestFailedException(response);

throw ex;
}

return Response.FromValue(
response.Status switch
{
200 => true, // Blob found
409 => true, // GetProperties failed because of encryption so blob exists
404 => false, // Blob not found
_ => false // Otherwise consider it not found
},
response);
}
catch (Exception ex)
{
ClientConfiguration.Pipeline.LogException(ex);
scope.Failed(ex);
throw;
}
finally
{
ClientConfiguration.Pipeline.LogMethodExit(nameof(BlobBaseClient));
scope.Dispose();
}
}
}
Expand Down
49 changes: 31 additions & 18 deletions sdk/storage/Azure.Storage.Blobs/src/BlobContainerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1595,28 +1595,41 @@ private async Task<Response<bool>> ExistsInternal(
{
using (ClientConfiguration.Pipeline.BeginLoggingScope(nameof(BlobContainerClient)))
{
ClientConfiguration.Pipeline.LogMethodEnter(
nameof(BlobContainerClient),
message:
$"{nameof(Uri)}: {Uri}");

ClientConfiguration.Pipeline.LogMethodEnter(nameof(BlobContainerClient), message: $"{nameof(Uri)}: {Uri}");
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(BlobContainerClient)}.{nameof(Exists)}");
scope.Start();

try
{
Response<BlobContainerProperties> response = await GetPropertiesInternal(
conditions: null,
async: async,
cancellationToken: cancellationToken)
.ConfigureAwait(false);
scope.Start();

return Response.FromValue(true, response.GetRawResponse());
}
catch (RequestFailedException storageRequestFailedException)
when (storageRequestFailedException.ErrorCode == BlobErrorCode.ContainerNotFound)
{
return Response.FromValue(false, default);
// Get the container properties without throwing any exceptions
RequestContext context =
new RequestContext()
{
ErrorOptions = ErrorOptions.NoThrow,
CancellationToken = cancellationToken
}
.AddClassifier(404, BlobErrorCode.ContainerNotFound.ToString(), false);

Response response = async ?
await ContainerRestClient.GetPropertiesAsync(context: context).ConfigureAwait(false) :
ContainerRestClient.GetProperties(context: context);

// If the response was any error other than a 404
// ContainerNotFound we will throw.
if (response.IsError)
{
RequestFailedException ex = async ?
await ClientConfiguration.ClientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) :
ClientConfiguration.ClientDiagnostics.CreateRequestFailedException(response);
// Can't simply do the following because of https://github.com/Azure/azure-sdk-for-net/issues/27614
// ex = new RequestFailedException(response);

throw ex;
}

// The container exists if we returned 200, but a 404 means
// it doesn't
return Response.FromValue(response.Status == 200, response);
}
catch (Exception ex)
{
Expand Down
Loading