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

Expose current retry count in the context of the function (Issue #2595) #2658

Merged
merged 10 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<!-- Packages can have independent versions and only increment when released -->
<Version>3.0.26$(VersionSuffix)</Version>
<Version>3.0.27$(VersionSuffix)</Version>
<ExtensionsStorageVersion>4.0.3$(VersionSuffix)</ExtensionsStorageVersion>
<HostStorageVersion>4.0.1$(VersionSuffix)</HostStorageVersion>
<LoggingVersion>4.0.1$(VersionSuffix)</LoggingVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.WebJobs.Host;

namespace Microsoft.Azure.WebJobs
{
Expand Down Expand Up @@ -32,5 +33,10 @@ public class ExecutionContext
/// A host can set this via <see cref="CoreJobHostConfigurationExtensions.UseCore(JobHostConfiguration, string)"/>
/// </remarks>
public string FunctionAppDirectory { get; set; }

/// <summary>
/// Gets or sets value for retry context.
/// </summary>
alrod marked this conversation as resolved.
Show resolved Hide resolved
public RetryContext RetryContext { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ private ExecutionContext CreateContext()
InvocationId = _context.FunctionInstanceId,
FunctionName = _context.FunctionContext.MethodName,
FunctionDirectory = Environment.CurrentDirectory,
FunctionAppDirectory = _options.Value.AppDirectory
FunctionAppDirectory = _options.Value.AppDirectory,
RetryContext = _context.FunctionContext.RetryContext
};

if (result.FunctionAppDirectory != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Threading;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Protocols;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -18,6 +19,7 @@ public class FunctionBindingContext
private readonly CancellationToken _functionCancellationToken;
private readonly IServiceProvider _functionInvocationServices;
private static Lazy<IServiceProvider> _emptyServiceProvider = new Lazy<IServiceProvider>(CreateEmptyServiceProvider);
private readonly RetryContext _retryContext;

/// <summary>
/// Creates a new instance.
Expand Down Expand Up @@ -52,6 +54,23 @@ public FunctionBindingContext(
MethodName = functionDescriptor?.LogName;
}

/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="functionInstance">The function instance being bound to.</param>
/// <param name="functionCancellationToken">The <see cref="CancellationToken"/> to use.</param>
public FunctionBindingContext(IFunctionInstanceEx functionInstance, CancellationToken functionCancellationToken)
{
_functionInstanceId = functionInstance.Id;
_functionCancellationToken = functionCancellationToken;
_functionInvocationServices = functionInstance.InstanceServices ?? _emptyServiceProvider.Value;
if (functionInstance is FunctionInstance fi)
{
_retryContext = fi.RetryContext;
}
MethodName = functionInstance.FunctionDescriptor?.LogName;
}

/// <summary>
/// Gets the instance ID of the function being bound to.
/// </summary>
Expand All @@ -72,6 +91,11 @@ public FunctionBindingContext(
/// </summary>
public IServiceProvider InstanceServices { get; set; }

/// <summary>
/// Gets the retry context if this invocation is being retried.
/// </summary>
public RetryContext RetryContext => _retryContext;
mathewc marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Creates an object instance with constructor arguments provided in the
/// method call and from the current function context scoped services.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,7 @@ internal async Task<string> ExecuteWithLoggingAsync(IFunctionInstanceEx instance
FunctionOutputLogger.SetOutput(outputLog);

// Must bind before logging (bound invoke string is included in log message).
var functionContext = new FunctionBindingContext(
instance.Id,
functionCancellationTokenSource.Token,
instance.InstanceServices,
instance.FunctionDescriptor);
var functionContext = new FunctionBindingContext(instance, functionCancellationTokenSource.Token);
var valueBindingContext = new ValueBindingContext(functionContext, cancellationToken);

using (logger.BeginScope(_inputBindingScope))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static async Task<IDelayedException> TryExecuteAsync(this IFunctionExecut
var attempt = 0;
IDelayedException functionResult = null;
ILogger logger = null;
RetryContext retryContext = null;
Exception exceptionFromLastExecution = null;

while (true)
{
Expand All @@ -31,6 +33,17 @@ public static async Task<IDelayedException> TryExecuteAsync(this IFunctionExecut
logger = loggerFactory.CreateLogger(LogCategories.CreateFunctionCategory(functionInstance.FunctionDescriptor.LogName));
}

// Set retry context only if retry strategy exists and this is not first attempt
if (functionInstance.FunctionDescriptor.RetryStrategy != null && attempt > 0 && functionInstance is FunctionInstance instance)
{
retryContext = retryContext ?? new RetryContext();
retryContext.RetryCount = attempt;
retryContext.Instance = instance;
mathewc marked this conversation as resolved.
Show resolved Hide resolved
retryContext.MaxRetryCount = functionInstance.FunctionDescriptor.RetryStrategy.MaxRetryCount;
retryContext.Exception = exceptionFromLastExecution;

instance.RetryContext = retryContext;
}
functionResult = await executor.TryExecuteAsync(functionInstance, cancellationToken);

if (functionResult == null)
Expand All @@ -44,21 +57,15 @@ public static async Task<IDelayedException> TryExecuteAsync(this IFunctionExecut
break;
}

exceptionFromLastExecution = functionResult?.Exception;

IRetryStrategy retryStrategy = functionInstance.FunctionDescriptor.RetryStrategy;
if (retryStrategy.MaxRetryCount != -1 && ++attempt > retryStrategy.MaxRetryCount)
{
// retry count exceeded
break;
}

// Build retry context
var retryContext = new RetryContext
{
RetryCount = attempt,
Exception = functionResult.Exception,
Instance = functionInstance
};

TimeSpan nextDelay = retryStrategy.GetNextDelay(retryContext);
logger.LogFunctionRetryAttempt(nextDelay, attempt, retryStrategy.MaxRetryCount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public FunctionInstance(Guid id, IDictionary<string, string> triggerDetails, Gui

public FunctionDescriptor FunctionDescriptor { get; }

public RetryContext RetryContext { get; set; }

public IServiceProvider InstanceServices
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace Microsoft.Azure.WebJobs
/// </summary>
public class ExponentialBackoffRetryAttribute : RetryAttribute
{
private IDelayStrategy _delayStrategy;
private TimeSpan _parsedMinimumInterval;
private TimeSpan _parsedMaximumInterval;
private static string _delayStrategyKeyName = "MS_DelayStratagy";
pragnagopa marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Constructs a new instance.
Expand All @@ -23,18 +25,19 @@ public class ExponentialBackoffRetryAttribute : RetryAttribute
/// <param name="maximumInterval">The maximum delay interval.</param>
public ExponentialBackoffRetryAttribute(int maxRetryCount, string minimumInterval, string maximumInterval) : base(maxRetryCount)
{
if (!TimeSpan.TryParse(minimumInterval, out TimeSpan parsedMinimumInterval))
if (!TimeSpan.TryParse(minimumInterval, out _parsedMinimumInterval))
{
throw new ArgumentOutOfRangeException(nameof(minimumInterval));
}
if (!TimeSpan.TryParse(maximumInterval, out TimeSpan parsedMaximumInterval))
if (!TimeSpan.TryParse(maximumInterval, out _parsedMaximumInterval))
{
throw new ArgumentOutOfRangeException(nameof(maximumInterval));
}

RandomizedExponentialBackoffStrategy.ValidateIntervals(_parsedMinimumInterval, _parsedMaximumInterval);

MinimumInterval = minimumInterval;
MaximumInterval = maximumInterval;

_delayStrategy = new RandomizedExponentialBackoffStrategy(parsedMinimumInterval, parsedMaximumInterval);
}

/// <summary>
Expand All @@ -49,9 +52,21 @@ public ExponentialBackoffRetryAttribute(int maxRetryCount, string minimumInterva

public override TimeSpan GetNextDelay(RetryContext context)
{
IDelayStrategy delayStrategy;

if (context.State.TryGetValue(_delayStrategyKeyName, out object stateObject))
{
delayStrategy = stateObject as IDelayStrategy;
}
else
{
delayStrategy = new RandomizedExponentialBackoffStrategy(_parsedMinimumInterval, _parsedMaximumInterval);
context.State.Add(_delayStrategyKeyName, delayStrategy);
}

if (MaxRetryCount == -1 || context.RetryCount < MaxRetryCount)
{
return _delayStrategy.GetNextDelay(false);
return delayStrategy.GetNextDelay(false);
}
return TimeSpan.Zero;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Microsoft.Azure.WebJobs.Host/RetryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.WebJobs.Host.Executors;

namespace Microsoft.Azure.WebJobs.Host
Expand All @@ -11,11 +13,18 @@ namespace Microsoft.Azure.WebJobs.Host
/// </summary>
public class RetryContext
{
private Lazy<Dictionary<string, object>> _stateDictionary = new Lazy<Dictionary<string, object>>(() => new Dictionary<string, object>());

/// <summary>
/// Gets or sets the current retry count.
/// </summary>
public int RetryCount { get; set; }

/// <summary>
/// Gets or sets the max retry count.
/// </summary>
pragnagopa marked this conversation as resolved.
Show resolved Hide resolved
public int MaxRetryCount { get; set; }

/// <summary>
/// Gets or sets the <see cref="Exception"/> for the failed invocation.
/// </summary>
Expand All @@ -25,5 +34,10 @@ public class RetryContext
/// Gets or sets the <see cref="IFunctionInstance"/> for the failed invocation.
/// </summary>
public IFunctionInstance Instance { get; set; }

/// <summary>
/// Gets the state dictionary. This property can be used by retry strategies to store arbitrary state for a retried invocation.
/// </summary>
public IDictionary<string, object> State => _stateDictionary.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public RandomizedExponentialBackoffStrategy(TimeSpan minimumInterval, TimeSpan m

public RandomizedExponentialBackoffStrategy(TimeSpan minimumInterval, TimeSpan maximumInterval,
TimeSpan deltaBackoff)
{
ValidateIntervals(minimumInterval, maximumInterval);

_minimumInterval = minimumInterval;
_maximumInterval = maximumInterval;
_deltaBackoff = deltaBackoff;
}

public static void ValidateIntervals(TimeSpan minimumInterval, TimeSpan maximumInterval)
{
if (minimumInterval.Ticks < 0)
{
Expand All @@ -41,10 +50,6 @@ public RandomizedExponentialBackoffStrategy(TimeSpan minimumInterval, TimeSpan m
throw new ArgumentException("The minimumInterval must not be greater than the maximumInterval.",
"minimumInterval");
}

_minimumInterval = minimumInterval;
_maximumInterval = maximumInterval;
_deltaBackoff = deltaBackoff;
}

public TimeSpan GetNextDelay(bool executionSucceeded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public async Task SetAppDirectory()
Assert.Equal(@"z:\home", result.FunctionAppDirectory);
}

[Fact]
public async Task SetRetryContext()
{
IHost host = new HostBuilder()
.ConfigureDefaultTestHost<RetryTestJob>(b =>
{
b.AddExecutionContextBinding(o =>
{
o.AppDirectory = @"z:\home";
});
})
.Build();

var jobHost = host.GetJobHost<RetryTestJob>();

await jobHost.CallAsync("myfunc");
ExecutionContext result = RetryTestJob.Context;
Assert.NotNull(result);
Assert.Equal(result.RetryContext.RetryCount, 3);
}

public class CoreTestJobs
{
public static ExecutionContext Context { get; set; }
Expand All @@ -77,5 +98,30 @@ public static void ExecutionContext2(ExecutionContext context)
Context = context;
}
}

public class RetryTestJob
{
public static ExecutionContext Context { get; set; }

[FixedDelayRetry(3, "00:00:01")]
[NoAutomaticTrigger]
[FunctionName("myfunc")]
public static void ExecutionContext(ExecutionContext context)
{
Context = context;

Assert.Equal(context.RetryContext.MaxRetryCount, 3);

if (context.RetryContext.RetryCount != 0)
{
Assert.Equal(context.RetryContext.RetryCount - 1, int.Parse(context.RetryContext.Exception.InnerException.Message));
}

if (context.RetryContext.RetryCount != 3)
{
throw new Exception(context.RetryContext.RetryCount.ToString());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,33 @@ public void ExponentialBackOffDelay_NextDelay_ReturnsExpected()
};
Assert.Equal(TimeSpan.Zero, retry.GetNextDelay(lastRetryContext));
}

[Fact]
public void ExponentialBackOffDelay_GetNextDelay_Is_Stateless()
{
var retry = new ExponentialBackoffRetryAttribute(5, "01:02:25", "02:00:10");
RetryContext retryContext1 = new RetryContext
{
RetryCount = 1
};
RetryContext retryContext2 = new RetryContext
{
RetryCount = 1
};
Assert.Equal(retry.GetNextDelay(retryContext1), retry.GetNextDelay(retryContext2));
}

[Fact]
public void ExponentialBackOffDelay_GetNextDelay_Is_Increased()
{
var retry = new ExponentialBackoffRetryAttribute(5, "01:02:25", "02:00:10");
RetryContext retryContext = new RetryContext
{
RetryCount = 1
};
var delay1 = retry.GetNextDelay(retryContext);
var delay2 = retry.GetNextDelay(retryContext);
Assert.True(delay2 > delay1);
}
}
}