-
Notifications
You must be signed in to change notification settings - Fork 959
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
Backoff to avoid excessive retries to Run Service in a duration #3354
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GitHub.Runner.Common; | ||
using GitHub.Services.Common; | ||
|
||
namespace GitHub.Runner.Listener | ||
{ | ||
[ServiceLocator(Default = typeof(ErrorThrottler))] | ||
public interface IErrorThrottler : IRunnerService | ||
{ | ||
void Reset(); | ||
Task IncrementAndWaitAsync(CancellationToken token); | ||
} | ||
|
||
public sealed class ErrorThrottler : RunnerService, IErrorThrottler | ||
{ | ||
internal static readonly TimeSpan MinBackoff = TimeSpan.FromSeconds(1); | ||
internal static readonly TimeSpan MaxBackoff = TimeSpan.FromMinutes(1); | ||
internal static readonly TimeSpan BackoffCoefficient = TimeSpan.FromSeconds(1); | ||
private int _count = 0; | ||
|
||
public void Reset() | ||
{ | ||
_count = 0; | ||
} | ||
|
||
public async Task IncrementAndWaitAsync(CancellationToken token) | ||
{ | ||
if (++_count <= 1) | ||
{ | ||
return; | ||
} | ||
|
||
TimeSpan backoff = BackoffTimerHelper.GetExponentialBackoff( | ||
attempt: _count - 2, // 0-based attempt | ||
minBackoff: MinBackoff, | ||
maxBackoff: MaxBackoff, | ||
deltaBackoff: BackoffCoefficient); | ||
Trace.Warning($"Back off {backoff.TotalSeconds} seconds before next attempt. Current consecutive error count: {_count}"); | ||
await HostContext.Delay(backoff, token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GitHub.DistributedTask.WebApi; | ||
using GitHub.Runner.Listener; | ||
using GitHub.Runner.Listener.Configuration; | ||
using GitHub.Runner.Common.Tests; | ||
using System.Runtime.CompilerServices; | ||
using GitHub.Services.WebApi; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace GitHub.Runner.Common.Tests.Listener | ||
{ | ||
public sealed class ErrorThrottlerL0 | ||
{ | ||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(2)] | ||
[InlineData(3)] | ||
[InlineData(4)] | ||
[InlineData(5)] | ||
[InlineData(6)] | ||
[InlineData(7)] | ||
[InlineData(8)] | ||
public async void TestIncrementAndWait(int totalAttempts) | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var eventArgs = new List<DelayEventArgs>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
eventArgs.Add(args); | ||
}; | ||
|
||
// Act | ||
for (int attempt = 1; attempt <= totalAttempts; attempt++) | ||
{ | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
} | ||
|
||
// Assert | ||
Assert.Equal(totalAttempts - 1, eventArgs.Count); | ||
for (int i = 0; i < eventArgs.Count; i++) | ||
{ | ||
// Expected milliseconds | ||
int expectedMin; | ||
int expectedMax; | ||
|
||
switch (i) | ||
{ | ||
case 0: | ||
expectedMin = 1000; // Min backoff | ||
expectedMax = 1000; | ||
break; | ||
case 1: | ||
expectedMin = 1800; // Min + 0.8 * Coefficient | ||
expectedMax = 2200; // Min + 1.2 * Coefficient | ||
break; | ||
case 2: | ||
expectedMin = 3400; // Min + 0.8 * Coefficient * 3 | ||
expectedMax = 4600; // Min + 1.2 * Coefficient * 3 | ||
break; | ||
case 3: | ||
expectedMin = 6600; // Min + 0.8 * Coefficient * 7 | ||
expectedMax = 9400; // Min + 1.2 * Coefficient * 7 | ||
break; | ||
case 4: | ||
expectedMin = 13000; // Min + 0.8 * Coefficient * 15 | ||
expectedMax = 19000; // Min + 1.2 * Coefficient * 15 | ||
break; | ||
case 5: | ||
expectedMin = 25800; // Min + 0.8 * Coefficient * 31 | ||
expectedMax = 38200; // Min + 1.2 * Coefficient * 31 | ||
break; | ||
case 6: | ||
expectedMin = 51400; // Min + 0.8 * Coefficient * 63 | ||
expectedMax = 60000; // Max backoff | ||
break; | ||
case 7: | ||
expectedMin = 60000; | ||
expectedMax = 60000; | ||
break; | ||
default: | ||
throw new NotSupportedException("Unexpected eventArgs count"); | ||
} | ||
|
||
var actualMilliseconds = eventArgs[i].Delay.TotalMilliseconds; | ||
Assert.True(expectedMin <= actualMilliseconds, $"Unexpected min delay for eventArgs[{i}]. Expected min {expectedMin}, actual {actualMilliseconds}"); | ||
Assert.True(expectedMax >= actualMilliseconds, $"Unexpected max delay for eventArgs[{i}]. Expected max {expectedMax}, actual {actualMilliseconds}"); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void TestReset() | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var eventArgs = new List<DelayEventArgs>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
eventArgs.Add(args); | ||
}; | ||
|
||
// Act | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
errorThrottler.Reset(); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
|
||
// Assert | ||
Assert.Equal(4, eventArgs.Count); | ||
for (int i = 0; i < eventArgs.Count; i++) | ||
{ | ||
// Expected milliseconds | ||
int expectedMin; | ||
int expectedMax; | ||
|
||
switch (i) | ||
{ | ||
case 0: | ||
case 2: | ||
expectedMin = 1000; // Min backoff | ||
expectedMax = 1000; | ||
break; | ||
case 1: | ||
case 3: | ||
expectedMin = 1800; // Min + 0.8 * Coefficient | ||
expectedMax = 2200; // Min + 1.2 * Coefficient | ||
break; | ||
default: | ||
throw new NotSupportedException("Unexpected eventArgs count"); | ||
} | ||
|
||
var actualMilliseconds = eventArgs[i].Delay.TotalMilliseconds; | ||
Assert.True(expectedMin <= actualMilliseconds, $"Unexpected min delay for eventArgs[{i}]. Expected min {expectedMin}, actual {actualMilliseconds}"); | ||
Assert.True(expectedMax >= actualMilliseconds, $"Unexpected max delay for eventArgs[{i}]. Expected max {expectedMax}, actual {actualMilliseconds}"); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void TestReceivesCancellationToken() | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var eventArgs = new List<DelayEventArgs>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
eventArgs.Add(args); | ||
}; | ||
var cancellationTokenSource1 = new CancellationTokenSource(); | ||
var cancellationTokenSource2 = new CancellationTokenSource(); | ||
var cancellationTokenSource3 = new CancellationTokenSource(); | ||
|
||
// Act | ||
await errorThrottler.IncrementAndWaitAsync(cancellationTokenSource1.Token); | ||
await errorThrottler.IncrementAndWaitAsync(cancellationTokenSource2.Token); | ||
await errorThrottler.IncrementAndWaitAsync(cancellationTokenSource3.Token); | ||
|
||
// Assert | ||
Assert.Equal(2, eventArgs.Count); | ||
Assert.Equal(cancellationTokenSource2.Token, eventArgs[0].Token); | ||
Assert.Equal(cancellationTokenSource3.Token, eventArgs[1].Token); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void TestReceivesSender() | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var senders = new List<object>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
senders.Add(sender); | ||
}; | ||
|
||
// Act | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
|
||
// Assert | ||
Assert.Equal(2, senders.Count); | ||
Assert.Equal(hc, senders[0]); | ||
Assert.Equal(hc, senders[1]); | ||
} | ||
} | ||
|
||
private TestHostContext CreateTestContext([CallerMemberName] String testName = "") | ||
{ | ||
return new TestHostContext(this, testName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with not retrying these errors, means we immediately call to Broker to get the next message, which might be the same job message.
When one of these errors is encountered from Run Service, an async message is sent to Broker. We don't want to keep hammering the service in a tight loop if there are delays processing the message queue.