-
Notifications
You must be signed in to change notification settings - Fork 654
Gracefully handle IOException on concurrent cache writes #920
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
Merged
JakeGinnivan
merged 5 commits into
GitTools:master
from
simonejsing:simon/concurrentcachewrite
Jul 10, 2016
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e19687d
Gracefully handle IOException on concurrent cache writes
6f4c245
Isolate write operation
0ad410c
Created class to implement retry with exponential backoff with unit t…
15042a4
renamed class and wired up the retry operation to the cache write wit…
83becc0
Write to log when retrying
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 hidden or 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 hidden or 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,20 @@ | ||
using System; | ||
using GitVersion.Helpers; | ||
|
||
public class MockThreadSleep : IThreadSleep | ||
{ | ||
private Action<int> Validator; | ||
|
||
public MockThreadSleep(Action<int> validator = null) | ||
{ | ||
this.Validator = validator; | ||
} | ||
|
||
public void Sleep(int milliseconds) | ||
{ | ||
if (Validator != null) | ||
{ | ||
Validator(milliseconds); | ||
} | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
src/GitVersionCore.Tests/OperationWithExponentialBackoffTests.cs
This file contains hidden or 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,125 @@ | ||
using System; | ||
using System.IO; | ||
using GitVersion.Helpers; | ||
using NUnit.Framework; | ||
using Shouldly; | ||
|
||
[TestFixture] | ||
public class OperationWithExponentialBackoffTests | ||
{ | ||
[Test] | ||
public void RetryOperationThrowsWhenNegativeMaxRetries() | ||
{ | ||
Action action = () => new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), () => { }, -1); | ||
action.ShouldThrow<ArgumentOutOfRangeException>(); | ||
} | ||
|
||
[Test] | ||
public void RetryOperationThrowsWhenThreadSleepIsNull() | ||
{ | ||
Action action = () => new OperationWithExponentialBackoff<IOException>(null, () => { }); | ||
action.ShouldThrow<ArgumentNullException>(); | ||
} | ||
|
||
[Test] | ||
public void OperationIsNotRetriedOnInvalidException() | ||
{ | ||
Action operation = () => | ||
{ | ||
throw new Exception(); | ||
}; | ||
|
||
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation); | ||
Action action = () => retryOperation.Execute(); | ||
action.ShouldThrow<Exception>(); | ||
} | ||
|
||
[Test] | ||
public void OperationIsRetriedOnIOException() | ||
{ | ||
var operationCount = 0; | ||
|
||
Action operation = () => | ||
{ | ||
operationCount++; | ||
if (operationCount < 2) | ||
{ | ||
throw new IOException(); | ||
} | ||
}; | ||
|
||
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation); | ||
retryOperation.Execute(); | ||
|
||
operationCount.ShouldBe(2); | ||
} | ||
|
||
[Test] | ||
public void OperationIsRetriedAMaximumNumberOfTimes() | ||
{ | ||
const int numberOfRetries = 3; | ||
var operationCount = 0; | ||
|
||
Action operation = () => | ||
{ | ||
operationCount++; | ||
throw new IOException(); | ||
}; | ||
|
||
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation, numberOfRetries); | ||
Action action = () => retryOperation.Execute(); | ||
action.ShouldThrow<AggregateException>(); | ||
|
||
operationCount.ShouldBe(numberOfRetries + 1); | ||
} | ||
|
||
[Test] | ||
public void OperationDelayDoublesBetweenRetries() | ||
{ | ||
const int numberOfRetries = 3; | ||
var expectedSleepMSec = 500; | ||
var sleepCount = 0; | ||
|
||
Action operation = () => | ||
{ | ||
throw new IOException(); | ||
}; | ||
|
||
Action<int> validator = u => | ||
{ | ||
sleepCount++; | ||
u.ShouldBe(expectedSleepMSec); | ||
expectedSleepMSec *= 2; | ||
}; | ||
|
||
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries); | ||
Action action = () => retryOperation.Execute(); | ||
action.ShouldThrow<AggregateException>(); | ||
|
||
sleepCount.ShouldBe(numberOfRetries); | ||
} | ||
|
||
[Test] | ||
public void TotalSleepTimeForSixRetriesIsAboutThirtySeconds() | ||
{ | ||
const int numberOfRetries = 6; | ||
int totalSleep = 0; | ||
|
||
Action operation = () => | ||
{ | ||
throw new IOException(); | ||
}; | ||
|
||
Action<int> validator = u => | ||
{ | ||
totalSleep += u; | ||
}; | ||
|
||
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries); | ||
Action action = () => retryOperation.Execute(); | ||
action.ShouldThrow<AggregateException>(); | ||
|
||
// Exact number is 31,5 seconds | ||
totalSleep.ShouldBe(31500); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
namespace GitVersion.Helpers | ||
{ | ||
public interface IThreadSleep | ||
{ | ||
void Sleep(int milliseconds); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/GitVersionCore/Helpers/OperationWithExponentialBackoff.cs
This file contains hidden or 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,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace GitVersion.Helpers | ||
{ | ||
internal class OperationWithExponentialBackoff<T> where T : Exception | ||
{ | ||
private IThreadSleep ThreadSleep; | ||
private Action Operation; | ||
private int MaxRetries; | ||
|
||
public OperationWithExponentialBackoff(IThreadSleep threadSleep, Action operation, int maxRetries = 5) | ||
{ | ||
if (threadSleep == null) | ||
throw new ArgumentNullException("threadSleep"); | ||
if (maxRetries < 0) | ||
throw new ArgumentOutOfRangeException("maxRetries"); | ||
|
||
this.ThreadSleep = threadSleep; | ||
this.Operation = operation; | ||
this.MaxRetries = maxRetries; | ||
} | ||
|
||
public void Execute() | ||
{ | ||
var exceptions = new List<Exception>(); | ||
|
||
int tries = 0; | ||
int sleepMSec = 500; | ||
|
||
while (tries <= MaxRetries) | ||
{ | ||
tries++; | ||
|
||
try | ||
{ | ||
Operation(); | ||
break; | ||
} | ||
catch (T e) | ||
{ | ||
exceptions.Add(e); | ||
if (tries > MaxRetries) | ||
{ | ||
throw new AggregateException("Operation failed after maximum number of retries were exceeded.", exceptions); | ||
} | ||
} | ||
|
||
Logger.WriteInfo(string.Format("Operation failed, retrying in {0} milliseconds.", sleepMSec)); | ||
ThreadSleep.Sleep(sleepMSec); | ||
sleepMSec *= 2; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,12 @@ | ||
namespace GitVersion.Helpers | ||
{ | ||
using System.Threading; | ||
|
||
internal class ThreadSleep : IThreadSleep | ||
{ | ||
public void Sleep(int milliseconds) | ||
{ | ||
Thread.Sleep(milliseconds); | ||
} | ||
} | ||
} |
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.
It would probably be good to log here how long it is going to sleep for, that way if it is failing multiple times the user can see why it's taking so long.