Skip to content

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
merged 5 commits into from
Jul 10, 2016
Merged
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
2 changes: 2 additions & 0 deletions src/GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
<Compile Include="ConfigProviderTests.cs" />
<Compile Include="GitVersionContextTests.cs" />
<Compile Include="Helpers\DirectoryHelper.cs" />
<Compile Include="Mocks\MockThreadSleep.cs" />
<Compile Include="OperationWithExponentialBackoffTests.cs" />
<Compile Include="Init\InitScenarios.cs" />
<Compile Include="Init\InitStepsDefaultResponsesDoNotThrow.cs" />
<Compile Include="Init\TestConsole.cs" />
Expand Down
20 changes: 20 additions & 0 deletions src/GitVersionCore.Tests/Mocks/MockThreadSleep.cs
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 src/GitVersionCore.Tests/OperationWithExponentialBackoffTests.cs
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);
}
}
10 changes: 9 additions & 1 deletion src/GitVersionCore/ExecuteCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace GitVersion
{
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using GitVersion.Helpers;

Expand Down Expand Up @@ -57,7 +58,14 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
if (versionVariables == null)
{
versionVariables = ExecuteInternal(targetBranch, commitId, repo, gitPreparer, projectRoot, buildServer, overrideConfig: overrideConfig);
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
try
{
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
}
catch (AggregateException e)
{
Logger.WriteWarning(string.Format("One or more exceptions during cache write:{0}{1}", Environment.NewLine, e));
}
}

return versionVariables;
Expand Down
30 changes: 18 additions & 12 deletions src/GitVersionCore/GitVersionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,29 @@ public void WriteVariablesToDiskCache(IRepository repo, string gitDir, VersionVa
var cacheFileName = GetCacheFileName(GetKey(repo, gitDir), GetCacheDir(gitDir));
variablesFromCache.FileName = cacheFileName;

using (var stream = fileSystem.OpenWrite(cacheFileName))
Dictionary<string, string> dictionary;
using (Logger.IndentLog("Creating dictionary"))
{
using (var sw = new StreamWriter(stream))
{
Dictionary<string, string> dictionary;
using (Logger.IndentLog("Creating dictionary"))
{
dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
}
dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
}

using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
Action writeCacheOperation = () =>
{
using (var stream = fileSystem.OpenWrite(cacheFileName))
{
using (var sw = new StreamWriter(stream))
{
var serializer = new Serializer();
serializer.Serialize(sw, dictionary);
using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
{
var serializer = new Serializer();
serializer.Serialize(sw, dictionary);
}
}
}
}
};

var retryOperation = new OperationWithExponentialBackoff<IOException>(new ThreadSleep(), writeCacheOperation, maxRetries: 6);
retryOperation.Execute();
}

public VersionVariables LoadVersionVariablesFromDiskCache(IRepository repo, string gitDir)
Expand Down
3 changes: 3 additions & 0 deletions src/GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@
<Compile Include="GitVersionCache.cs" />
<Compile Include="Helpers\FileSystem.cs" />
<Compile Include="Helpers\IFileSystem.cs" />
<Compile Include="Helpers\IThreadSleep.cs" />
<Compile Include="Helpers\OperationWithExponentialBackoff.cs" />
<Compile Include="Helpers\ServiceMessageEscapeHelper.cs" />
<Compile Include="Helpers\ThreadSleep.cs" />
<Compile Include="IncrementStrategyFinder.cs" />
<Compile Include="OutputVariables\VersionVariables.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
Expand Down
7 changes: 7 additions & 0 deletions src/GitVersionCore/Helpers/IThreadSleep.cs
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 src/GitVersionCore/Helpers/OperationWithExponentialBackoff.cs
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);
Copy link
Contributor

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.

sleepMSec *= 2;
}
}
}
}
12 changes: 12 additions & 0 deletions src/GitVersionCore/Helpers/ThreadSleep.cs
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);
}
}
}