forked from microsoft/FASTER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFasterLogTests.cs
More file actions
335 lines (275 loc) · 13.2 KB
/
FasterLogTests.cs
File metadata and controls
335 lines (275 loc) · 13.2 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Threading;
using System.Threading.Tasks;
using FASTER.core;
using NUnit.Framework;
namespace FASTER.test
{
[TestFixture]
internal class FasterLogTests
{
const int entryLength = 100;
const int numEntries = 1000000;
private FasterLog log;
private IDevice device;
private string commitPath;
[SetUp]
public void Setup()
{
commitPath = new DefaultCheckpointNamingScheme().FasterLogCommitBasePath();
if (commitPath == "")
throw new Exception("Write log commits to separate folder for testing");
commitPath = TestContext.CurrentContext.TestDirectory + "\\" + commitPath;
if (Directory.Exists(commitPath))
DeleteDirectory(commitPath);
if (File.Exists(TestContext.CurrentContext.TestDirectory + "\\fasterlog.log.commit"))
File.Delete(TestContext.CurrentContext.TestDirectory + "\\fasterlog.log.commit");
device = Devices.CreateLogDevice(TestContext.CurrentContext.TestDirectory + "\\fasterlog.log", deleteOnClose: true);
}
[TearDown]
public void TearDown()
{
device.Close();
if (Directory.Exists(commitPath))
DeleteDirectory(commitPath);
if (File.Exists(TestContext.CurrentContext.TestDirectory + "\\fasterlog.log.commit"))
File.Delete(TestContext.CurrentContext.TestDirectory + "\\fasterlog.log.commit");
}
[Test]
public void FasterLogTest1([Values]LogChecksumType logChecksum)
{
log = new FasterLog(new FasterLogSettings { LogDevice = device, LogChecksum = logChecksum });
byte[] entry = new byte[entryLength];
for (int i = 0; i < entryLength; i++)
entry[i] = (byte)i;
for (int i = 0; i < numEntries; i++)
{
log.Enqueue(entry);
}
log.Commit(true);
using (var iter = log.Scan(0, long.MaxValue))
{
int count = 0;
while (iter.GetNext(out byte[] result, out int length, out long currentAddress))
{
count++;
Assert.IsTrue(result.SequenceEqual(entry));
if (count % 100 == 0)
log.TruncateUntil(iter.NextAddress);
}
Assert.IsTrue(count == numEntries);
}
log.Dispose();
}
[Test]
public async Task FasterLogTest2([Values]LogChecksumType logChecksum)
{
log = new FasterLog(new FasterLogSettings { LogDevice = device, LogChecksum = logChecksum });
byte[] data1 = new byte[10000];
for (int i = 0; i < 10000; i++) data1[i] = (byte)i;
using (var iter = log.Scan(0, long.MaxValue, scanBufferingMode: ScanBufferingMode.SinglePageBuffering))
{
int i = 0;
while (i++ < 500)
{
var waitingReader = iter.WaitAsync();
Assert.IsTrue(!waitingReader.IsCompleted);
while (!log.TryEnqueue(data1, out _)) ;
Assert.IsFalse(waitingReader.IsCompleted);
await log.CommitAsync();
while (!waitingReader.IsCompleted) ;
Assert.IsTrue(waitingReader.IsCompleted);
var curr = iter.GetNext(out byte[] result, out _, out _);
Assert.IsTrue(curr);
Assert.IsTrue(result.SequenceEqual(data1));
var next = iter.GetNext(out _, out _, out _);
Assert.IsFalse(next);
}
}
log.Dispose();
}
[Test]
public async Task FasterLogTest3([Values]LogChecksumType logChecksum)
{
log = new FasterLog(new FasterLogSettings { LogDevice = device, PageSizeBits = 14, LogChecksum = logChecksum });
byte[] data1 = new byte[10000];
for (int i = 0; i < 10000; i++) data1[i] = (byte)i;
using (var iter = log.Scan(0, long.MaxValue, scanBufferingMode: ScanBufferingMode.SinglePageBuffering))
{
var appendResult = log.TryEnqueue(data1, out _);
Assert.IsTrue(appendResult);
await log.CommitAsync();
await iter.WaitAsync();
var iterResult = iter.GetNext(out byte[] entry, out _, out _);
Assert.IsTrue(iterResult);
appendResult = log.TryEnqueue(data1, out _);
Assert.IsFalse(appendResult);
await iter.WaitAsync();
// Should read the "hole" and return false
iterResult = iter.GetNext(out entry, out _, out _);
Assert.IsFalse(iterResult);
// Should wait for next item
var task = iter.WaitAsync();
Assert.IsFalse(task.IsCompleted);
appendResult = log.TryEnqueue(data1, out _);
Assert.IsTrue(appendResult);
await log.CommitAsync();
await task;
iterResult = iter.GetNext(out entry, out _, out _);
Assert.IsTrue(iterResult);
}
log.Dispose();
}
[Test]
public async Task FasterLogTest4([Values]LogChecksumType logChecksum)
{
log = new FasterLog(new FasterLogSettings { LogDevice = device, PageSizeBits = 14, LogChecksum = logChecksum });
byte[] data1 = new byte[100];
for (int i = 0; i < 100; i++) data1[i] = (byte)i;
for (int i = 0; i < 100; i++)
{
log.Enqueue(data1);
}
Assert.IsTrue(log.CommittedUntilAddress == log.BeginAddress);
await log.CommitAsync();
Assert.IsTrue(log.CommittedUntilAddress == log.TailAddress);
Assert.IsTrue(log.CommittedBeginAddress == log.BeginAddress);
using (var iter = log.Scan(0, long.MaxValue))
{
// Should read the "hole" and return false
var iterResult = iter.GetNext(out byte[] entry, out _, out _);
log.TruncateUntil(iter.NextAddress);
Assert.IsTrue(log.CommittedUntilAddress == log.TailAddress);
Assert.IsTrue(log.CommittedBeginAddress < log.BeginAddress);
Assert.IsTrue(iter.NextAddress == log.BeginAddress);
await log.CommitAsync();
Assert.IsTrue(log.CommittedUntilAddress == log.TailAddress);
Assert.IsTrue(log.CommittedBeginAddress == log.BeginAddress);
}
log.Dispose();
}
[Test]
public async Task FasterLogTest5([Values]LogChecksumType logChecksum)
{
log = new FasterLog(new FasterLogSettings { LogDevice = device, PageSizeBits = 16, MemorySizeBits = 16, LogChecksum = logChecksum });
int headerSize = logChecksum == LogChecksumType.None ? 4 : 12;
bool _disposed = false;
var commit = new Thread(() => { while (!_disposed) { log.Commit(true); Thread.Sleep(1); } });
commit.Start();
// 65536=page size|headerSize|64=log header
await log.EnqueueAndWaitForCommitAsync(new byte[65536 - headerSize - 64]);
// 65536=page size|headerSize
await log.EnqueueAndWaitForCommitAsync(new byte[65536 - headerSize]);
// 65536=page size|headerSize
await log.EnqueueAndWaitForCommitAsync(new byte[65536 - headerSize]);
// 65536=page size|headerSize
await log.EnqueueAndWaitForCommitAsync(new byte[65536 - headerSize]);
// 65536=page size|headerSize
await log.EnqueueAndWaitForCommitAsync(new byte[65536 - headerSize]);
_disposed = true;
commit.Join();
log.Dispose();
}
[Test]
public async Task ResumePersistedReaderSpec([Values]LogChecksumType logChecksum)
{
var input1 = new byte[] { 0, 1, 2, 3 };
var input2 = new byte[] { 4, 5, 6, 7, 8, 9, 10 };
var input3 = new byte[] { 11, 12 };
string readerName = "abc";
string commitFilePath = TestContext.CurrentContext.TestDirectory + "\\fasterlog.log.commit";
using (var l = new FasterLog(new FasterLogSettings { LogDevice = device, PageSizeBits = 16, MemorySizeBits = 16, LogChecksum = logChecksum, LogCommitFile = commitFilePath }))
{
await l.EnqueueAsync(input1);
await l.EnqueueAsync(input2);
await l.EnqueueAsync(input3);
await l.CommitAsync();
long recoveryAddress;
using (var originalIterator = l.Scan(0, long.MaxValue, readerName))
{
originalIterator.GetNext(out _, out _, out _, out recoveryAddress);
originalIterator.CompleteUntil(recoveryAddress);
originalIterator.GetNext(out _, out _, out _, out _); // move the reader ahead
await l.CommitAsync();
}
}
using (var l = new FasterLog(new FasterLogSettings { LogDevice = device, PageSizeBits = 16, MemorySizeBits = 16, LogChecksum = logChecksum, LogCommitFile = commitFilePath }))
{
using (var recoveredIterator = l.Scan(0, long.MaxValue, readerName))
{
byte[] outBuf;
recoveredIterator.GetNext(out outBuf, out _, out _, out _);
Assert.True(input2.SequenceEqual(outBuf)); // we should have read in input2, not input1 or input3
}
}
}
[Test]
public async Task ResumePersistedReader2([Values] LogChecksumType logChecksum, [Values] bool overwriteLogCommits, [Values] bool removeOutdated)
{
var input1 = new byte[] { 0, 1, 2, 3 };
var input2 = new byte[] { 4, 5, 6, 7, 8, 9, 10 };
var input3 = new byte[] { 11, 12 };
string readerName = "abc";
var commitPath = TestContext.CurrentContext.TestDirectory + "\\ResumePersistedReader2";
if (Directory.Exists(commitPath))
DeleteDirectory(commitPath);
using (var logCommitManager = new DeviceLogCommitCheckpointManager(new LocalStorageNamedDeviceFactory(), new DefaultCheckpointNamingScheme(commitPath), overwriteLogCommits, removeOutdated))
{
long originalCompleted;
using (var l = new FasterLog(new FasterLogSettings { LogDevice = device, PageSizeBits = 16, MemorySizeBits = 16, LogChecksum = logChecksum, LogCommitManager = logCommitManager }))
{
await l.EnqueueAsync(input1);
await l.CommitAsync();
await l.EnqueueAsync(input2);
await l.CommitAsync();
await l.EnqueueAsync(input3);
await l.CommitAsync();
long recoveryAddress;
using (var originalIterator = l.Scan(0, long.MaxValue, readerName))
{
originalIterator.GetNext(out _, out _, out _, out recoveryAddress);
originalIterator.CompleteUntil(recoveryAddress);
originalIterator.GetNext(out _, out _, out _, out _); // move the reader ahead
await l.CommitAsync();
originalCompleted = originalIterator.CompletedUntilAddress;
}
}
using (var l = new FasterLog(new FasterLogSettings { LogDevice = device, PageSizeBits = 16, MemorySizeBits = 16, LogChecksum = logChecksum, LogCommitManager = logCommitManager }))
{
using (var recoveredIterator = l.Scan(0, long.MaxValue, readerName))
{
recoveredIterator.GetNext(out byte[] outBuf, out _, out _, out _);
// we should have read in input2, not input1 or input3
Assert.True(input2.SequenceEqual(outBuf), $"Original: {input2[0]}, Recovered: {outBuf[0]}, Original: {originalCompleted}, Recovered: {recoveredIterator.CompletedUntilAddress}");
// TestContext.Progress.WriteLine($"Original: {originalCompleted}, Recovered: {recoveredIterator.CompletedUntilAddress}");
}
}
}
DeleteDirectory(commitPath);
}
private static void DeleteDirectory(string path)
{
foreach (string directory in Directory.GetDirectories(path))
{
DeleteDirectory(directory);
}
try
{
Directory.Delete(path, true);
}
catch (IOException)
{
Directory.Delete(path, true);
}
catch (UnauthorizedAccessException)
{
Directory.Delete(path, true);
}
}
}
}