Skip to content

Commit f636938

Browse files
committed
Adjust the AbortRunner so it asks the storage if there's something to abort
(cherry picked from commit 587b42b)
1 parent 8cde7bd commit f636938

8 files changed

Lines changed: 145 additions & 4 deletions

File tree

src/TimeoutMigrationTool.Raven.IntegrationTests/RavenAbortsTheMigration.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,47 @@ public async Task WhenAbortingWithoutAToolStateToolWillStillCleanupBatches()
7777
Assert.That(hiddenTimeouts.Count, Is.EqualTo(0));
7878
}
7979

80+
[Test]
81+
public async Task WhenCheckingIfThereIsSomethingToAbortAndThereIsNoToolStateButWeHaveBatches()
82+
{
83+
var cutOffTime = DateTime.Now.AddDays(-1);
84+
await testSuite.InitTimeouts(nrOfTimeouts);
85+
86+
var storage = new RavenDBTimeoutStorage(testSuite.Logger, testSuite.ServerName, testSuite.DatabaseName, "TimeoutDatas", testSuite.RavenVersion, false);
87+
await storage.Prepare(cutOffTime, testSuite.EndpointName, new Dictionary<string, string>());
88+
89+
await testSuite.RavenAdapter.DeleteDocument(RavenConstants.ToolStateId);
90+
91+
var sut = new RavenDBTimeoutStorage(testSuite.Logger,testSuite.ServerName, testSuite.DatabaseName, "TimeoutDatas", testSuite.RavenVersion, false);
92+
var migrationIsInProgress = await sut.CheckIfAMigrationIsInProgress();
93+
94+
Assert.That(migrationIsInProgress, Is.True);
95+
}
96+
97+
[Test]
98+
public async Task WhenCheckingIfThereIsSomethingToAbortAndThereIsAToolState()
99+
{
100+
var cutOffTime = DateTime.Now.AddDays(-1);
101+
await testSuite.InitTimeouts(nrOfTimeouts);
102+
103+
var storage = new RavenDBTimeoutStorage(testSuite.Logger, testSuite.ServerName, testSuite.DatabaseName, "TimeoutDatas", testSuite.RavenVersion, false);
104+
await storage.Prepare(cutOffTime, testSuite.EndpointName, new Dictionary<string, string>());
105+
106+
var sut = new RavenDBTimeoutStorage(testSuite.Logger,testSuite.ServerName, testSuite.DatabaseName, "TimeoutDatas", testSuite.RavenVersion, false);
107+
var migrationIsInProgress = await sut.CheckIfAMigrationIsInProgress();
108+
109+
Assert.That(migrationIsInProgress, Is.True);
110+
}
111+
112+
[Test]
113+
public async Task WhenCheckingIfThereIsSomethingToAbortInACleanSystem()
114+
{
115+
var sut = new RavenDBTimeoutStorage(testSuite.Logger,testSuite.ServerName, testSuite.DatabaseName, "TimeoutDatas", testSuite.RavenVersion, false);
116+
var migrationIsInProgress = await sut.CheckIfAMigrationIsInProgress();
117+
118+
Assert.That(migrationIsInProgress, Is.False);
119+
}
120+
80121
[Test]
81122
public async Task WhenCleaningUpBatchesThenTimeoutsInIncompleteBatchesAreReset()
82123
{
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace TimeoutMigrationTool.Tests
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using NUnit.Framework;
8+
using Particular.TimeoutMigrationTool;
9+
10+
[TestFixture]
11+
public class AbortRunnerTests
12+
{
13+
[SetUp]
14+
public void Setup()
15+
{
16+
timeoutStorage = new FakeTimeoutStorage();
17+
logger = new ConsoleLogger(false);
18+
19+
runner = new AbortRunner(logger, timeoutStorage);
20+
21+
endpoints = new List<EndpointInfo>
22+
{
23+
new EndpointInfo
24+
{
25+
EndpointName = "Sales",
26+
NrOfTimeouts = 500
27+
}
28+
};
29+
testEndpoint = endpoints.First().EndpointName;
30+
timeoutStorage.SetupEndpoints(endpoints);
31+
}
32+
33+
[Test]
34+
public void WhenAbortingAndTimeoutStorageFoundNothingToAbortThrowsException()
35+
{
36+
Assert.ThrowsAsync<Exception>(async () => { await runner.Run(); });
37+
}
38+
39+
[Test]
40+
public async Task WhenAbortingAndTimeoutStorageFoundToolStateItIsAborted()
41+
{
42+
var batches = GetBatches();
43+
var toolState = new FakeToolState
44+
{
45+
EndpointName = testEndpoint,
46+
Batches = batches,
47+
RunParameters = new Dictionary<string, string>()
48+
};
49+
timeoutStorage.SetupToolStateToReturn(toolState);
50+
51+
await runner.Run();
52+
53+
Assert.That(timeoutStorage.ToolStateWasAborted, Is.True);
54+
}
55+
56+
static List<BatchInfo> GetBatches()
57+
{
58+
var batches = new List<BatchInfo>
59+
{
60+
new BatchInfo(1, BatchState.Pending, 1)
61+
};
62+
return batches;
63+
}
64+
65+
FakeTimeoutStorage timeoutStorage;
66+
AbortRunner runner;
67+
List<EndpointInfo> endpoints;
68+
string testEndpoint;
69+
Microsoft.Extensions.Logging.ILogger logger;
70+
}
71+
}

src/TimeoutMigrationTool.Tests/FakeTimeoutStorage.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,10 @@ public Task Complete()
9999
ToolStateMovedToCompleted = true;
100100
return Task.CompletedTask;
101101
}
102+
103+
public Task<bool> CheckIfAMigrationIsInProgress()
104+
{
105+
return Task.FromResult(this.existingToolState != null);
106+
}
102107
}
103108
}

src/TimeoutMigrationTool.Tests/MigrationRunnerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace TimeoutMigrationTool.Tests
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Dynamic;
65
using System.Linq;
76
using System.Threading.Tasks;
87
using NUnit.Framework;

src/TimeoutMigrationTool/AbortRunner.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,25 @@ public AbortRunner(ILogger logger, ITimeoutStorage timeoutStorage)
1414

1515
public async Task Run()
1616
{
17-
var toolState = await timeoutStorage.TryLoadOngoingMigration();
18-
if (toolState == null)
17+
var shouldAbort = await timeoutStorage.CheckIfAMigrationIsInProgress();
18+
if (!shouldAbort)
1919
{
2020
throw new Exception("Could not find a previous migration to abort.");
2121
}
2222

23-
logger.LogInformation($"Aborting ongoing migration for {toolState.EndpointName}");
23+
var toolState = await timeoutStorage.TryLoadOngoingMigration();
24+
if (toolState != null)
25+
{
26+
logger.LogInformation($"Aborting ongoing migration for {toolState.EndpointName}");
27+
}
28+
else
29+
{
30+
logger.LogInformation("Cleaning up changes made in the previous interrupted migration");
31+
}
2432

2533
await timeoutStorage.Abort();
34+
35+
logger.LogInformation("Previous migration was successfully aborted. That means that the timeouts hidden away from the TimeoutManager, have been made available again");
2636
}
2737

2838
readonly ILogger logger;

src/TimeoutMigrationTool/ITimeoutStorage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public interface ITimeoutStorage
1414
Task Abort();
1515
Task<List<EndpointInfo>> ListEndpoints(DateTime cutOffTime);
1616
Task Complete();
17+
Task<bool> CheckIfAMigrationIsInProgress();
1718
}
1819
}

src/TimeoutMigrationTool/RavenDB/RavenDBTimeoutStorage.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ public async Task Complete()
282282
await ravenAdapter.ArchiveDocument(GetArchivedToolStateId(ravenToolState.Endpoint), ravenToolState);
283283
}
284284

285+
public async Task<bool> CheckIfAMigrationIsInProgress()
286+
{
287+
var ravenToolState = await ravenAdapter.GetDocument<RavenToolStateDto>(RavenConstants.ToolStateId, (doc, id) => { });
288+
var anyBatches = await ravenAdapter.GetDocuments<RavenBatch>(batch => batch.State == BatchState.Pending, RavenConstants.BatchPrefix, (batch, id) => { });
289+
290+
return ravenToolState != null || anyBatches.Any();
291+
}
292+
285293
internal async Task CleanupExistingBatchesAndResetTimeouts(List<RavenBatch> batchesToRemove, List<RavenBatch> batchesForWhichToResetTimeouts)
286294
{
287295
foreach (var batch in batchesToRemove)

src/TimeoutMigrationTool/SqlP/SqlTimeoutStorage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public async Task Complete()
147147
await command.ExecuteNonQueryAsync();
148148
}
149149

150+
public async Task<bool> CheckIfAMigrationIsInProgress()
151+
{
152+
var toolState = await TryLoadOngoingMigration();
153+
return toolState != null;
154+
}
155+
150156
public async Task Abort()
151157
{
152158
var toolState = await TryLoadOngoingMigration();

0 commit comments

Comments
 (0)