Repository navigation
Expand file tree
/
Copy pathRecoveryTests.cs
More file actions
352 lines (331 loc) · 15.6 KB
/
Copy pathRecoveryTests.cs
File metadata and controls
352 lines (331 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System.Diagnostics;
using System.Text.Json;
using KeyLoad.CrashHost;
using KeyLoad.RecoveryTests.Features.StorageRecovery;
using KeyLoad.Storage;
using KeyLoad.Storage.ZoneTree;
namespace KeyLoad.RecoveryTests;
internal sealed class StorageRecoveryProcessTests
{
public static IEnumerable<int> Batches => Enumerable.Range(0, 20);
[Test]
[MethodDataSource(nameof(Batches))]
public async Task FiftySeededRealProcessCrashesPreserveAtomicTransactions(int batch)
{
var random = new SeededCrashChoice(1701 + batch);
var repository = new DirectoryInfo(AppContext.BaseDirectory);
while (repository.Parent is not null && !File.Exists(Path.Combine(repository.FullName, "KeyLoad.slnx")))
{
repository = repository.Parent;
}
var reports = Path.Combine(File.Exists(Path.Combine(repository.FullName, "KeyLoad.slnx"))
? repository.FullName : AppContext.BaseDirectory, "artifacts", "qualification");
Directory.CreateDirectory(reports);
using var evidence = new StreamWriter(Path.Combine(reports, $"crash-trials-{batch:D2}.jsonl"), false);
for (var trial = 0; trial < 50; trial++)
{
await RunSeededCrashTrialAsync(batch, trial, random, evidence, TestContext.Current!.Execution.CancellationToken);
}
}
private static async Task RunSeededCrashTrialAsync(int batch, int trial, SeededCrashChoice random,
StreamWriter evidence, CancellationToken cancellationToken)
{
var root = Path.Combine(Path.GetTempPath(), $"keyload-crash-{batch}-{trial}-{Guid.NewGuid():N}");
var stage = (CommitStage)random.Next(5);
var mutationIndex = random.Next(3);
using var admission = await StorageTrialLease.AcquireAsync(cancellationToken);
using var process = Process.Start(CreateCrashHostStart(root, stage, mutationIndex))!;
using var timeoutTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(15), TimeProvider.System);
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTimeout.Token);
Exception? originalFailure = null;
int[]? values = null;
try
{
await Assert.That(await process.StandardOutput.ReadLineAsync(timeout.Token)).IsEqualTo("crash-point");
process.Kill();
await process.WaitForExitAsync(timeout.Token);
await WaitForKilledProcessFilesAsync(root, timeout.Token);
values = ReadRecoveredValues(root);
await AssertAtomicCutAsync(values, batch, trial, stage);
await RecordCrashTrialAsync(evidence, batch, trial, stage, mutationIndex, values!,
StorageTrialLease.ActiveStorageTrials, StorageTrialLease.MaximumObservedStorageTrials, timeout.Token);
}
catch (Exception exception)
{
originalFailure = exception;
Console.WriteLine($"Seed {1701 + batch}, trial {trial}, stage {stage}: {exception}");
throw;
}
finally
{
try
{
await CleanupCrashTrialAsync(process, root, cancellationToken);
}
catch (IOException error) when (originalFailure is not null)
{
Console.WriteLine($"Cleanup after the original failure: {error.Message}");
}
}
}
private static ProcessStartInfo CreateCrashHostStart(string root, CommitStage stage, int mutationIndex)
{
var start = new ProcessStartInfo("dotnet") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false };
start.ArgumentList.Add(typeof(CrashHostMarker).Assembly.Location);
start.ArgumentList.Add(root);
start.ArgumentList.Add(stage.ToString());
start.ArgumentList.Add(mutationIndex.ToString(System.Globalization.CultureInfo.InvariantCulture));
return start;
}
private static int[] ReadRecoveredValues(string root)
{
using var store = new ZoneTreeStore(new(root), RecoveryExecutionOptions.StorageExecution(), RecoveryExecutionOptions.PointCacheExecution());
return store.Read(view => Enumerable.Range(0, 3)
.Select(i => NativeSerialization.Deserialize<int>(view.ReadOwnedValue(KeyCodec.Encode("item", (long)i))!)).ToArray());
}
private static async Task RecordCrashTrialAsync(StreamWriter evidence, int batch, int trial,
CommitStage stage, int mutationIndex, int[] values, int activeStorageTrials,
int maximumObservedStorageTrials, CancellationToken cancellationToken)
{
var record = new
{
Seed = 1701 + batch,
Trial = trial,
Stage = stage.ToString(),
MutationIndex = mutationIndex,
Values = values,
OS = System.Runtime.InteropServices.RuntimeInformation.OSDescription,
Architecture = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString(),
ActiveStorageTrials = activeStorageTrials,
MaximumObservedStorageTrials = maximumObservedStorageTrials
};
await evidence.WriteLineAsync(JsonSerializer.Serialize(record).AsMemory(), cancellationToken);
await evidence.FlushAsync(cancellationToken);
}
private static async Task AssertAtomicCutAsync(int[] values, int batch, int trial, CommitStage stage)
{
await Assert.That(values.All(value => value == 0) || values.All(value => value == 1))
.IsTrue().Because($"Non-atomic recovery at seed {batch}, trial {trial}, stage {stage}.");
if (stage >= CommitStage.JournalFlushed)
{
foreach (var value in values)
{
await Assert.That(value).IsEqualTo(1);
}
}
}
private static async Task CleanupCrashTrialAsync(Process process, string root, CancellationToken cancellationToken)
{
if (!process.HasExited)
{
process.Kill();
await process.WaitForExitAsync(cancellationToken);
}
if (Directory.Exists(root))
{
await StoragePublicationRecoveryTests.DeleteTrialAsync(root, cancellationToken);
}
}
internal static Task WaitForKilledProcessFilesAsync(string root, CancellationToken cancellationToken) =>
KilledProcessFileReadiness.WaitAsync(root, cancellationToken);
private sealed class SeededCrashChoice(int seed)
{
private uint state = unchecked((uint)seed);
internal int Next(int exclusiveMaximum)
{
state += 0x9E3779B9;
var value = state;
value = (value ^ (value >> 16)) * 0x85EBCA6B;
value = (value ^ (value >> 13)) * 0xC2B2AE35;
value ^= value >> 16;
return (int)(value % exclusiveMaximum);
}
}
}
internal sealed class StoragePublicationRecoveryTests
{
[Test]
[Arguments("compact", CommitStage.SnapshotWritten)]
[Arguments("compact", CommitStage.SnapshotFlushed)]
[Arguments("compact", CommitStage.InstallPrepared)]
[Arguments("compact", CommitStage.JournalSwapped)]
[Arguments("install", CommitStage.SnapshotWritten)]
[Arguments("install", CommitStage.SnapshotFlushed)]
[Arguments("install", CommitStage.InstallPrepared)]
[Arguments("install", CommitStage.JournalSwapped)]
public async Task ProcessKillDuringCheckpointPublicationRecoversOneCompleteGeneration(string mode, CommitStage stage)
{
var root = Path.Combine(Path.GetTempPath(), "keyload-checkpoint-crash-" + Guid.NewGuid().ToString("N"));
var cancellationToken = TestContext.Current!.Execution.CancellationToken;
using var admission = await StorageTrialLease.AcquireAsync(cancellationToken);
try
{
await KillCheckpointPublicationProcessAsync(root, mode, stage, cancellationToken);
await AssertCheckpointRecoveryGenerationAsync(root, mode, stage);
}
finally
{
await DeleteCheckpointTrialAsync(root, cancellationToken);
}
}
private static async Task KillCheckpointPublicationProcessAsync(string root, string mode, CommitStage stage,
CancellationToken cancellationToken)
{
var start = new ProcessStartInfo("dotnet")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
foreach (var argument in new[] { typeof(CrashHostMarker).Assembly.Location, root, stage.ToString(), "0", mode })
{
start.ArgumentList.Add(argument);
}
using var process = Process.Start(start)!;
using var timeoutTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(20), TimeProvider.System);
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTimeout.Token);
try
{
await Assert.That(await process.StandardOutput.ReadLineAsync(timeout.Token)).IsEqualTo("crash-point");
process.Kill();
await process.WaitForExitAsync(timeout.Token);
await StorageRecoveryProcessTests.WaitForKilledProcessFilesAsync(root, timeout.Token);
}
finally
{
if (!process.HasExited)
{
process.Kill();
await process.WaitForExitAsync(cancellationToken);
}
}
}
private static async Task AssertCheckpointRecoveryGenerationAsync(string root, string mode, CommitStage stage)
{
using var store = new ZoneTreeStore(new(root), RecoveryExecutionOptions.StorageExecution(), RecoveryExecutionOptions.PointCacheExecution());
var values = ReadCheckpointValues(store);
var installed = IsCheckpointGenerationInstalled(mode, stage);
foreach (var value in values)
{
await Assert.That(value).IsEqualTo(installed ? 2 : 1);
}
await Assert.That(store.Position).IsEqualTo(2);
if (mode == "install")
{
await AssertInstalledCheckpointMetadataAsync(store, installed);
}
await AssertNextCheckpointCommitAsync(store);
}
private static int[] ReadCheckpointValues(ZoneTreeStore store) =>
store.Read(view => Enumerable.Range(0, 3).Select(index =>
NativeSerialization.Deserialize<int>(view.ReadOwnedValue(KeyCodec.Encode("item", (long)index))!)).ToArray());
private static bool IsCheckpointGenerationInstalled(string mode, CommitStage stage) =>
mode == "install" && stage == CommitStage.JournalSwapped;
private static async Task AssertInstalledCheckpointMetadataAsync(ZoneTreeStore store, bool installed)
{
var obsoleteExists = store.Read(view => view.ReadOwnedValue(KeyCodec.Encode("obsolete")) is not null);
await Assert.That(obsoleteExists).IsEqualTo(!installed);
var lastApplied = store.Read(view => view.ReadOwnedValue(KeyCodec.Encode("system", "last-applied")) is { } bytes
? NativeSerialization.Deserialize<long>(bytes) : 0);
await Assert.That(lastApplied).IsEqualTo(installed ? 9 : 0);
}
private static async Task AssertNextCheckpointCommitAsync(ZoneTreeStore store)
{
var next = store.Commit((transaction, position) =>
{
transaction.PutRecord(KeyCodec.Encode("after"), true);
return position;
});
await Assert.That(next).IsEqualTo(3);
}
private static async Task DeleteCheckpointTrialAsync(string root, CancellationToken cancellationToken)
{
if (Directory.Exists(root))
{
await DeleteTrialAsync(root, cancellationToken);
}
}
internal static async Task DeleteTrialAsync(string root, CancellationToken cancellationToken)
{
var clock = TimeProvider.System;
var started = clock.GetTimestamp();
while (true)
{
try
{ Directory.Delete(root, true); return; }
catch (IOException) when (clock.GetElapsedTime(started) < TimeSpan.FromSeconds(5))
{ await Task.Delay(TimeSpan.FromMilliseconds(25), clock, cancellationToken); }
}
}
[Test]
public async Task CorruptCompleteFrameFailsClosedInsteadOfBeingDiscardedAsATornTail()
{
var root = Path.Combine(Path.GetTempPath(), "keyload-corruption-" + Guid.NewGuid().ToString("N"));
try
{
using (var store = new ZoneTreeStore(new(root), RecoveryExecutionOptions.StorageExecution(), RecoveryExecutionOptions.PointCacheExecution()))
{
store.Commit((tx, _) => { tx.PutRecord(KeyCodec.Encode("key"), "value"); return true; });
}
var path = Path.Combine(root, "commands.wal");
var bytes = await File.ReadAllBytesAsync(path);
bytes[^1] ^= 1;
await File.WriteAllBytesAsync(path, bytes);
await Assert.That(Assert.ThrowsExactly<KeyLoadException>(() => OpenCorruptStore(root)).Code).IsEqualTo(ErrorCode.Corruption);
await Assert.That(new FileInfo(path).Length).IsEqualTo(bytes.Length);
}
finally
{
if (Directory.Exists(root))
{
Directory.Delete(root, true);
}
}
}
[Test]
public async Task VerifiedBackupRestoresDataWithNewIdentityAndPausedDispatch()
{
var root = Path.Combine(Path.GetTempPath(), "keyload-backup-" + Guid.NewGuid().ToString("N"));
var backup = root + "-backup";
var restored = root + "-restored";
try
{
Guid previous;
using (var store = new ZoneTreeStore(new(root), RecoveryExecutionOptions.StorageExecution(), RecoveryExecutionOptions.PointCacheExecution()))
{
previous = store.Identity.Incarnation;
store.Commit((tx, _) => { tx.PutRecord(KeyCodec.Encode("key"), "value"); return true; });
store.CreateBackup(backup);
}
var identity = ZoneTreeStore.Restore(backup, restored, RecoveryExecutionOptions.StorageExecution());
await Assert.That(identity.Incarnation).IsNotEqualTo(previous);
await Assert.That(identity.DispatchPaused).IsTrue();
if (!OperatingSystem.IsWindows())
{
var privateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute;
await Assert.That(File.GetUnixFileMode(backup)).IsEqualTo(privateMode);
await Assert.That(File.GetUnixFileMode(restored)).IsEqualTo(privateMode);
}
using var recovered = new ZoneTreeStore(new(restored), RecoveryExecutionOptions.StorageExecution(), RecoveryExecutionOptions.PointCacheExecution());
await Assert.That(recovered.Read(view => NativeSerialization.Deserialize<string>(view.ReadOwnedValue(KeyCodec.Encode("key"))!))).IsEqualTo("value");
var tampered = await File.ReadAllBytesAsync(Path.Combine(backup, "commands.wal"));
tampered[^1] ^= 1;
await File.WriteAllBytesAsync(Path.Combine(backup, "commands.wal"), tampered);
await Assert.That(Assert.ThrowsExactly<KeyLoadException>(() => ZoneTreeStore.Restore(backup, restored + "-bad", RecoveryExecutionOptions.StorageExecution())).Code).IsEqualTo(ErrorCode.Corruption);
}
finally
{
foreach (var path in new[] { root, backup, restored })
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
}
}
private static void OpenCorruptStore(string root)
{
using var rejected = new ZoneTreeStore(new(root), RecoveryExecutionOptions.StorageExecution(), RecoveryExecutionOptions.PointCacheExecution());
}
}