Skip to content

Commit

Permalink
Verify and rewrite during PackfileMaintenanceStep
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickstolee committed Jan 3, 2019
1 parent 7942b9e commit 0772173
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
33 changes: 31 additions & 2 deletions GVFS/GVFS.Common/Maintenance/PackfileMaintenanceStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,36 @@ protected override void PerformMaintenance()

GitProcess.Result expireResult = this.RunGitCommand((process) => process.MultiPackIndexExpire(this.Context.Enlistment.GitObjectsRoot));
List<string> staleIdxFiles = this.CleanStaleIdxFiles(out int numDeletionBlocked);
this.GetPackFilesInfo(out int expireCount, out long expireSize, out hasKeep);
this.GetPackFilesInfo(out int expireCount, out long expireSize, out hasKeep);

GitProcess.Result verifyResult1 = this.RunGitCommand((process) => process.VerifyMultiPackIndex(this.Context.Enlistment.GitObjectsRoot));
if (verifyResult1.ExitCodeIsFailure)
{
EventMetadata errorMetadata = this.CreateEventMetadata();
errorMetadata["MultiPackIndexVerifyOutput"] = verifyResult1.Output;
errorMetadata["MultiPackIndexVerifyErrors"] = verifyResult1.Errors;
string multiPackIndexPath = Path.Combine(this.Context.Enlistment.GitPackRoot, "multi-pack-index");
errorMetadata["TryDeleteFileResult"] = this.Context.FileSystem.TryDeleteFile(multiPackIndexPath);
activity.RelatedError(errorMetadata, "multi-pack-index is corrupt after write. Deleting and rewriting.");

this.RunGitCommand((process) => process.WriteMultiPackIndex(this.Context.Enlistment.GitObjectsRoot));
}

GitProcess.Result repackResult = this.RunGitCommand((process) => process.MultiPackIndexRepack(this.Context.Enlistment.GitObjectsRoot, this.batchSize));
this.GetPackFilesInfo(out int afterCount, out long afterSize, out hasKeep);
this.GetPackFilesInfo(out int afterCount, out long afterSize, out hasKeep);

GitProcess.Result verifyResult2 = this.RunGitCommand((process) => process.VerifyMultiPackIndex(this.Context.Enlistment.GitObjectsRoot));
if (verifyResult1.ExitCodeIsFailure)
{
EventMetadata errorMetadata = this.CreateEventMetadata();
errorMetadata["MultiPackIndexVerifyOutput"] = verifyResult1.Output;
errorMetadata["MultiPackIndexVerifyErrors"] = verifyResult1.Errors;
string multiPackIndexPath = Path.Combine(this.Context.Enlistment.GitPackRoot, "multi-pack-index");
errorMetadata["TryDeleteFileResult"] = this.Context.FileSystem.TryDeleteFile(multiPackIndexPath);
activity.RelatedError(errorMetadata, "multi-pack-index is corrupt after write. Deleting and rewriting.");

this.RunGitCommand((process) => process.WriteMultiPackIndex(this.Context.Enlistment.GitObjectsRoot));
}

EventMetadata metadata = new EventMetadata();
metadata.Add("GitObjectsRoot", this.Context.Enlistment.GitObjectsRoot);
Expand All @@ -154,8 +181,10 @@ protected override void PerformMaintenance()
metadata.Add(nameof(afterSize), afterSize);
metadata.Add("ExpireOutput", expireResult.Output);
metadata.Add("ExpireErrors", expireResult.Errors);
metadata.Add("VerifyResult1", verifyResult1.ExitCode);
metadata.Add("RepackOutput", repackResult.Output);
metadata.Add("RepackErrors", repackResult.Errors);
metadata.Add("VerifyResult2", verifyResult2.ExitCode);
metadata.Add("NumStaleIdxFiles", staleIdxFiles.Count);
metadata.Add("NumIdxDeletionsBlocked", numDeletionBlocked);
activity.RelatedEvent(EventLevel.Informational, $"{this.Area}_{nameof(this.PerformMaintenance)}", metadata, Keywords.Telemetry);
Expand Down
44 changes: 39 additions & 5 deletions GVFS/GVFS.UnitTests/Maintenance/PackfileMaintenanceStepTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class PackfileMaintenanceStepTests
private GVFSContext context;

private string ExpireCommand => $"multi-pack-index expire --object-dir=\"{this.context.Enlistment.GitObjectsRoot}\"";
private string VerifyCommand => $"-c core.multiPackIndex=true multi-pack-index verify --object-dir=\"{this.context.Enlistment.GitObjectsRoot}\"";
private string WriteCommand => $"-c core.multiPackIndex=true multi-pack-index write --object-dir=\"{this.context.Enlistment.GitObjectsRoot}\"";
private string RepackCommand => $"-c pack.depth=0 -c pack.window=0 multi-pack-index repack --object-dir=\"{this.context.Enlistment.GitObjectsRoot}\" --batch-size=2g";

[TestCase]
Expand All @@ -36,9 +38,11 @@ public void PackfileMaintenanceIgnoreTimeRestriction()
this.tracer.StartActivityTracer.RelatedErrorEvents.Count.ShouldEqual(0);
this.tracer.StartActivityTracer.RelatedWarningEvents.Count.ShouldEqual(0);
List<string> commands = this.gitProcess.CommandsRun;
commands.Count.ShouldEqual(2);
commands.Count.ShouldEqual(4);
commands[0].ShouldEqual(this.ExpireCommand);
commands[1].ShouldEqual(this.RepackCommand);
commands[1].ShouldEqual(this.VerifyCommand);
commands[2].ShouldEqual(this.RepackCommand);
commands[3].ShouldEqual(this.VerifyCommand);
}

[TestCase]
Expand Down Expand Up @@ -66,9 +70,36 @@ public void PackfileMaintenancePassTimeRestriction()
this.tracer.StartActivityTracer.RelatedErrorEvents.Count.ShouldEqual(0);
this.tracer.StartActivityTracer.RelatedWarningEvents.Count.ShouldEqual(0);
List<string> commands = this.gitProcess.CommandsRun;
commands.Count.ShouldEqual(2);
commands.Count.ShouldEqual(4);
commands[0].ShouldEqual(this.ExpireCommand);
commands[1].ShouldEqual(this.RepackCommand);
commands[1].ShouldEqual(this.VerifyCommand);
commands[2].ShouldEqual(this.RepackCommand);
commands[3].ShouldEqual(this.VerifyCommand);
}

[TestCase]
public void PackfileMaintenanceRewriteOnBadVerify()
{
this.TestSetup(DateTime.UtcNow, failOnVerify: true);

this.gitProcess.SetExpectedCommandResult(
this.WriteCommand,
() => new GitProcess.Result(string.Empty, string.Empty, GitProcess.Result.SuccessCode));

PackfileMaintenanceStep step = new PackfileMaintenanceStep(this.context, requireObjectCacheLock: false, forceRun: true);
step.Execute();

this.tracer.StartActivityTracer.RelatedErrorEvents.Count.ShouldEqual(2);
this.tracer.StartActivityTracer.RelatedWarningEvents.Count.ShouldEqual(0);

List<string> commands = this.gitProcess.CommandsRun;
commands.Count.ShouldEqual(6);
commands[0].ShouldEqual(this.ExpireCommand);
commands[1].ShouldEqual(this.VerifyCommand);
commands[2].ShouldEqual(this.WriteCommand);
commands[3].ShouldEqual(this.RepackCommand);
commands[4].ShouldEqual(this.VerifyCommand);
commands[5].ShouldEqual(this.WriteCommand);
}

[TestCase]
Expand Down Expand Up @@ -109,7 +140,7 @@ public void CleanStaleIdxFiles()
.ShouldBeFalse();
}

private void TestSetup(DateTime lastRun)
private void TestSetup(DateTime lastRun, bool failOnVerify = false)
{
string lastRunTime = EpochConverter.ToUnixEpochSeconds(lastRun).ToString();

Expand Down Expand Up @@ -157,6 +188,9 @@ private void TestSetup(DateTime lastRun)
this.gitProcess.SetExpectedCommandResult(
this.ExpireCommand,
() => new GitProcess.Result(string.Empty, string.Empty, GitProcess.Result.SuccessCode));
this.gitProcess.SetExpectedCommandResult(
this.VerifyCommand,
() => new GitProcess.Result(string.Empty, string.Empty, failOnVerify ? GitProcess.Result.GenericFailureCode : GitProcess.Result.SuccessCode));
this.gitProcess.SetExpectedCommandResult(
this.RepackCommand,
() => new GitProcess.Result(string.Empty, string.Empty, GitProcess.Result.SuccessCode));
Expand Down

0 comments on commit 0772173

Please sign in to comment.