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

[MetricsAdvisor] Added RetryOnTooManyRequestsPolicy to tests #20653

Merged
merged 3 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -5,6 +5,7 @@
using System.Collections.Generic;
using Azure.AI.MetricsAdvisor.Administration;
using Azure.AI.MetricsAdvisor.Models;
using Azure.Core;
using Azure.Core.TestFramework;
using NUnit.Framework;

Expand Down Expand Up @@ -38,7 +39,7 @@ public MetricsAdvisorLiveTestBase(bool isAsync) : base(isAsync)
public MetricsAdvisorAdministrationClient GetMetricsAdvisorAdministrationClient(bool useTokenCredential = false)
{
var endpoint = new Uri(TestEnvironment.MetricsAdvisorUri);
var instrumentedOptions = InstrumentClientOptions(new MetricsAdvisorClientsOptions());
var instrumentedOptions = GetInstrumentedOptions();

MetricsAdvisorAdministrationClient client = useTokenCredential
? new(endpoint, TestEnvironment.Credential, instrumentedOptions)
Expand All @@ -50,7 +51,7 @@ public MetricsAdvisorAdministrationClient GetMetricsAdvisorAdministrationClient(
public MetricsAdvisorClient GetMetricsAdvisorClient(bool useTokenCredential = false)
{
var endpoint = new Uri(TestEnvironment.MetricsAdvisorUri);
var instrumentedOptions = InstrumentClientOptions(new MetricsAdvisorClientsOptions());
var instrumentedOptions = GetInstrumentedOptions();

MetricsAdvisorClient client = useTokenCredential
? new(endpoint, TestEnvironment.Credential, instrumentedOptions)
Expand Down Expand Up @@ -88,5 +89,14 @@ protected void ValidateGroupKey(DimensionKey groupKey)
Assert.That(column.Value, Is.Not.Null.And.Not.Empty);
}
}

private MetricsAdvisorClientsOptions GetInstrumentedOptions()
{
var options = new MetricsAdvisorClientsOptions();

options.AddPolicy(new RetryOnTooManyRequestsPolicy(), HttpPipelinePosition.PerRetry);

return InstrumentClientOptions(options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;

namespace Azure.AI.MetricsAdvisor.Tests
{
public class RetryOnTooManyRequestsPolicy : HttpPipelinePolicy
{
private const int TooManyRequestsStatusCode = 429;

private const string RetryAfterHeaderName = "Retry-After";

public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) =>
ProcessAsync(message, pipeline, false).EnsureCompleted();

public override ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) =>
ProcessAsync(message, pipeline, true);

private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async)
{
bool gotResponse = false;

while (!gotResponse)
{
if (async)
{
await ProcessNextAsync(message, pipeline);
}
else
{
ProcessNext(message, pipeline);
}

if (message.Response.Status == TooManyRequestsStatusCode)
{
TimeSpan delay = message.Response.Headers.TryGetValue(RetryAfterHeaderName, out string retryAfterValue)
&& int.TryParse(retryAfterValue, out int delayInSeconds)
? TimeSpan.FromSeconds(delayInSeconds)
: TimeSpan.FromSeconds(1);

await WaitAsync(delay, async, message.CancellationToken);
}
else
{
gotResponse = true;
}
}
}

private async ValueTask WaitAsync(TimeSpan delay, bool async, CancellationToken cancellationToken)
{
if (async)
{
await Task.Delay(delay, cancellationToken);
}
else
{
cancellationToken.WaitHandle.WaitOne(delay);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public async Task UpdateHookAsync()
}

[Test]
[Ignore("https://github.com/Azure/azure-sdk-for-net/issues/20677")]
Copy link
Member Author

@kinelski kinelski Apr 26, 2021

Choose a reason for hiding this comment

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

Issue has been present for more than a week and is not related to these changes. This one is from Apr 19th: https://dev.azure.com/azure-sdk/internal/_build/results?buildId=849790&view=logs&j=68082d65-ebf7-5f3f-494d-d3fd09e80dd6&t=6f6444ca-c101-5878-5224-de85502bb056&l=89

public async Task GetHooksAsync()
{
string endpoint = MetricsAdvisorUri;
Expand Down