forked from microsoft/FASTER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectRecoveryTest2.cs
More file actions
277 lines (233 loc) · 9.72 KB
/
ObjectRecoveryTest2.cs
File metadata and controls
277 lines (233 loc) · 9.72 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using FASTER.core;
using System;
using System.IO;
using NUnit.Framework;
namespace FASTER.test.recovery.objects
{
[TestFixture]
public class ObjectRecoveryTest
{
int iterations;
string FasterFolderPath { get; set; }
[SetUp]
public void Setup()
{
FasterFolderPath = TestContext.CurrentContext.TestDirectory + "\\" + Path.GetRandomFileName();
if (!Directory.Exists(FasterFolderPath))
Directory.CreateDirectory(FasterFolderPath);
}
[TearDown]
public void TearDown()
{
DeleteDirectory(FasterFolderPath);
}
public 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);
}
}
[Test]
public void ObjectRecoveryTest1(
[Values]CheckpointType checkpointType,
[Range(100, 1500, 600)] int iterations)
{
this.iterations = iterations;
Prepare(checkpointType, out _, out _, out IDevice log, out IDevice objlog, out FasterKV<MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions> h, out MyContext context);
var session1 = h.NewSession();
Write(session1, context, h);
Read(session1, context, false);
session1.Dispose();
h.TakeFullCheckpoint(out _);
h.CompleteCheckpointAsync().GetAwaiter().GetResult();
Destroy(log, objlog, h);
Prepare(checkpointType, out _, out _, out log, out objlog, out h, out context);
h.Recover();
var session2 = h.NewSession();
Read(session2, context, true);
session2.Dispose();
Destroy(log, objlog, h);
}
private void Prepare(CheckpointType checkpointType, out string logPath, out string objPath, out IDevice log, out IDevice objlog, out FasterKV<MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions> h, out MyContext context)
{
logPath = Path.Combine(FasterFolderPath, $"FasterRecoverTests.log");
objPath = Path.Combine(FasterFolderPath, $"FasterRecoverTests_HEAP.log");
log = Devices.CreateLogDevice(logPath);
objlog = Devices.CreateLogDevice(objPath);
h = new FasterKV
<MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions>
(1L << 20, new MyFunctions(),
new LogSettings
{
LogDevice = log,
ObjectLogDevice = objlog,
SegmentSizeBits = 12,
MemorySizeBits = 12,
PageSizeBits = 9
},
new CheckpointSettings()
{
CheckpointDir = Path.Combine(FasterFolderPath, "check-points"),
CheckPointType = checkpointType
},
new SerializerSettings<MyKey, MyValue> { keySerializer = () => new MyKeySerializer(), valueSerializer = () => new MyValueSerializer() }
);
context = new MyContext();
}
private static void Destroy(IDevice log, IDevice objlog, FasterKV<MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions> h)
{
// Dispose FASTER instance and log
h.Dispose();
log.Close();
objlog.Close();
}
private void Write(ClientSession<MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions> session, MyContext context, FasterKV<MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions> fht)
{
for (int i = 0; i < iterations; i++)
{
var _key = new MyKey { key = i, name = i.ToString() };
var value = new MyValue { value = i.ToString() };
session.Upsert(ref _key, ref value, context, 0);
if (i % 100 == 0)
{
fht.TakeFullCheckpoint(out _);
fht.CompleteCheckpointAsync().GetAwaiter().GetResult();
}
}
}
private void Read(ClientSession<MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions> session, MyContext context, bool delete)
{
for (int i = 0; i < iterations; i++)
{
var key = new MyKey { key = i, name = i.ToString() };
var input = default(MyInput);
MyOutput g1 = new MyOutput();
var status = session.Read(ref key, ref input, ref g1, context, 0);
if (status == Status.PENDING)
{
session.CompletePending(true);
context.FinalizeRead(ref status, ref g1);
}
Assert.IsTrue(status == Status.OK);
Assert.IsTrue(g1.value.value == i.ToString());
}
if (delete)
{
var key = new MyKey { key = 1, name = "1" };
var input = default(MyInput);
var output = new MyOutput();
session.Delete(ref key, context, 0);
var status = session.Read(ref key, ref input, ref output, context, 0);
if (status == Status.PENDING)
{
session.CompletePending(true);
context.FinalizeRead(ref status, ref output);
}
Assert.IsTrue(status == Status.NOTFOUND);
}
}
}
public class MyKeySerializer : BinaryObjectSerializer<MyKey>
{
public override void Serialize(ref MyKey key)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(key.name);
writer.Write(4 + bytes.Length);
writer.Write(key.key);
writer.Write(bytes);
}
public override void Deserialize(ref MyKey key)
{
var size = reader.ReadInt32();
key.key = reader.ReadInt32();
var bytes = new byte[size - 4];
reader.Read(bytes, 0, size - 4);
key.name = System.Text.Encoding.UTF8.GetString(bytes);
}
}
public class MyValueSerializer : BinaryObjectSerializer<MyValue>
{
public override void Serialize(ref MyValue value)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(value.value);
writer.Write(bytes.Length);
writer.Write(bytes);
}
public override void Deserialize(ref MyValue value)
{
var size = reader.ReadInt32();
var bytes = new byte[size];
reader.Read(bytes, 0, size);
value.value = System.Text.Encoding.UTF8.GetString(bytes);
}
}
public class MyKey : IFasterEqualityComparer<MyKey>
{
public int key;
public string name;
public long GetHashCode64(ref MyKey key) => Utility.GetHashCode(key.key);
public bool Equals(ref MyKey key1, ref MyKey key2) => key1.key == key2.key && key1.name == key2.name;
}
public class MyValue { public string value; }
public class MyInput { public string value; }
public class MyOutput { public MyValue value; }
public class MyContext
{
private Status _status;
private MyOutput _g1;
internal void Populate(ref Status status, ref MyOutput g1)
{
_status = status;
_g1 = g1;
}
internal void FinalizeRead(ref Status status, ref MyOutput g1)
{
status = _status;
g1 = _g1;
}
}
public class MyFunctions : IFunctions<MyKey, MyValue, MyInput, MyOutput, MyContext>
{
public void InitialUpdater(ref MyKey key, ref MyInput input, ref MyValue value) => value.value = input.value;
public void CopyUpdater(ref MyKey key, ref MyInput input, ref MyValue oldValue, ref MyValue newValue) => newValue = oldValue;
public bool InPlaceUpdater(ref MyKey key, ref MyInput input, ref MyValue value)
{
if (value.value.Length < input.value.Length)
return false;
value.value = input.value;
return true;
}
public void SingleReader(ref MyKey key, ref MyInput input, ref MyValue value, ref MyOutput dst) => dst.value = value;
public void SingleWriter(ref MyKey key, ref MyValue src, ref MyValue dst) => dst = src;
public void ConcurrentReader(ref MyKey key, ref MyInput input, ref MyValue value, ref MyOutput dst) => dst.value = value;
public bool ConcurrentWriter(ref MyKey key, ref MyValue src, ref MyValue dst)
{
if (src == null)
return false;
if (dst.value.Length != src.value.Length)
return false;
dst = src;
return true;
}
public void ReadCompletionCallback(ref MyKey key, ref MyInput input, ref MyOutput output, MyContext ctx, Status status) => ctx.Populate(ref status, ref output);
public void UpsertCompletionCallback(ref MyKey key, ref MyValue value, MyContext ctx) { }
public void RMWCompletionCallback(ref MyKey key, ref MyInput input, MyContext ctx, Status status) { }
public void DeleteCompletionCallback(ref MyKey key, MyContext ctx) { }
public void CheckpointCompletionCallback(string sessionId, CommitPoint commitPoint) { }
}
}