-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathwal_manager_test.cc
401 lines (341 loc) · 12.3 KB
/
wal_manager_test.cc
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "treeline/options.h"
#include "treeline/status.h"
#include "wal/manager.h"
namespace {
using namespace tl;
namespace fs = std::filesystem;
class WALManagerTest : public testing::Test {
public:
WALManagerTest()
: kWALDir("/tmp/tl-wal-" + std::to_string(std::time(nullptr))) {}
void SetUp() override {
fs::remove_all(kWALDir);
fs::create_directory(kWALDir);
}
void TearDown() override { fs::remove_all(kWALDir); }
size_t NumFilesInDir() const {
size_t count = 0;
for (const auto& _ : fs::directory_iterator(kWALDir)) {
++count;
}
return count;
}
const fs::path kWALDir;
};
TEST_F(WALManagerTest, ReplayEmpty) {
wal::Manager manager(kWALDir);
Status s = manager.PrepareForReplay();
ASSERT_TRUE(s.ok());
size_t call_count = 0;
s = manager.ReplayLog([&call_count](const Slice& key, const Slice& value,
format::WriteType type) {
++call_count;
return Status::OK();
});
ASSERT_TRUE(s.ok());
ASSERT_EQ(call_count, 0);
}
TEST_F(WALManagerTest, WriteThenReadOneVersion) {
const std::vector<std::pair<std::string, std::string>> records = {
{"hello", "world"}, {"key", "value"}, {"foo", "bar"}};
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForWrite();
ASSERT_TRUE(s.ok());
// Log file is lazily created.
ASSERT_EQ(NumFilesInDir(), 0);
for (const auto& record : records) {
s = manager.LogWrite(WriteOptions(), record.first, record.second,
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
}
ASSERT_EQ(NumFilesInDir(), 1);
}
ASSERT_EQ(NumFilesInDir(), 1);
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForReplay();
ASSERT_TRUE(s.ok());
auto it = records.begin();
const auto rec_end = records.end();
s = manager.ReplayLog([&it, &rec_end](const Slice& key, const Slice& value,
format::WriteType type) {
EXPECT_TRUE(it != rec_end);
EXPECT_TRUE(key.compare(it->first) == 0);
EXPECT_TRUE(value.compare(it->second) == 0);
EXPECT_EQ(type, format::WriteType::kWrite);
++it;
return Status::OK();
});
ASSERT_TRUE(it == rec_end);
}
// Didn't delete the log - so it should still exist.
ASSERT_EQ(NumFilesInDir(), 1);
}
TEST_F(WALManagerTest, CleanShutdown) {
wal::Manager manager(kWALDir);
Status s = manager.PrepareForWrite();
ASSERT_TRUE(s.ok());
s = manager.LogWrite(WriteOptions(), "hello", "world",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 1);
uint64_t prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 0);
// The new log file is created lazily on the first write.
ASSERT_EQ(NumFilesInDir(), 1);
s = manager.LogWrite(WriteOptions(), "hello", "world",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 2);
// On a clean shutdown, all volatile data is persisted to disk. We then
// discard the write-ahead log(s).
s = manager.DiscardAllForCleanShutdown();
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 0);
}
TEST_F(WALManagerTest, WriteReplayMultiple) {
// Tests the scenario where we write to multiple logs, discard some logs
// (because they are no longer needed), and then "crash". Make sure we can
// replay the persisted logs.
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForWrite();
ASSERT_TRUE(s.ok());
s = manager.LogWrite(WriteOptions(), "hello-0", "world-0",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
s = manager.LogWrite(WriteOptions(), "hello-0-1", "world-0-1",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 1);
uint64_t prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 0);
// New log file created lazily.
ASSERT_EQ(NumFilesInDir(), 1);
s = manager.LogWrite(WriteOptions(), "hello-1", "world-1",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 2);
prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 1);
ASSERT_EQ(NumFilesInDir(), 2);
s = manager.LogWrite(WriteOptions(), "hello-2", "world-2",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 3);
prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 2);
ASSERT_EQ(NumFilesInDir(), 3);
// Discard the oldest log version
s = manager.DiscardOldest(0);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 2);
}
// No logs should have been lost.
ASSERT_EQ(NumFilesInDir(), 2);
// Start replaying the logs.
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForReplay();
ASSERT_TRUE(s.ok());
std::vector<std::pair<std::string, std::string>> records;
s = manager.ReplayLog([&records](const Slice& key, const Slice& value,
format::WriteType type) {
EXPECT_EQ(type, format::WriteType::kWrite);
records.emplace_back(std::string(key.data(), key.size()),
std::string(value.data(), value.size()));
return Status::OK();
});
ASSERT_EQ(records.size(), 2);
ASSERT_EQ(records[0].first, "hello-1");
ASSERT_EQ(records[0].second, "world-1");
ASSERT_EQ(records[1].first, "hello-2");
ASSERT_EQ(records[1].second, "world-2");
// Switch to writing mode (recovery has "finished")
s = manager.PrepareForWrite();
ASSERT_TRUE(s.ok());
// By default we don't delete the replayed logs. The new log file is lazily
// created, so there should be no change in the number of files.
ASSERT_EQ(NumFilesInDir(), 2);
}
// Ensure that if the user requests old logs to be discarded, they are.
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForReplay();
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 2);
s = manager.PrepareForWrite(/*discard_existing_logs=*/true);
ASSERT_TRUE(s.ok());
// No log files left.
ASSERT_EQ(NumFilesInDir(), 0);
// The new log's version was supposed to be 0.
const uint64_t prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 0);
// But since no log writes were made, no log files should exist.
ASSERT_EQ(NumFilesInDir(), 0);
}
}
TEST_F(WALManagerTest, ReplayCleanShutdown) {
// Replay after a clean shutdown should be a no-op.
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForWrite();
ASSERT_TRUE(s.ok());
s = manager.LogWrite(WriteOptions(), "hello", "world",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
s = manager.DiscardAllForCleanShutdown();
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 0);
}
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForReplay();
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 0);
size_t call_count = 0;
s = manager.ReplayLog([&call_count](const Slice& key, const Slice& value,
format::WriteType type) {
++call_count;
return Status::OK();
});
ASSERT_TRUE(s.ok());
ASSERT_EQ(call_count, 0);
}
}
TEST_F(WALManagerTest, DiscardUpToInclusive) {
wal::Manager manager(kWALDir);
Status s = manager.PrepareForWrite();
ASSERT_TRUE(s.ok());
s = manager.LogWrite(WriteOptions(), "hello-0", "world-0",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 1);
uint64_t prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 0);
// Log file is lazily created.
ASSERT_EQ(NumFilesInDir(), 1);
s = manager.LogWrite(WriteOptions(), "hello-1", "world-1",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 2);
prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 1);
ASSERT_EQ(NumFilesInDir(), 2);
s = manager.LogWrite(WriteOptions(), "hello-2", "world-2",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 3);
prev_version = manager.IncrementLogVersion();
ASSERT_EQ(prev_version, 2);
ASSERT_EQ(NumFilesInDir(), 3);
s = manager.LogWrite(WriteOptions(), "hello-3", "world-3",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 4);
// The current version is 3, but it is "active" and cannot be discarded.
// If we request to discard up to 3 (inclusive), we should only delete logs 0,
// 1, and 2.
s = manager.DiscardUpToInclusive(3);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 1);
ASSERT_TRUE(fs::is_regular_file(kWALDir / "3"));
}
TEST_F(WALManagerTest, HandleUnrelatedFilePresence) {
// Create a file in the WAL directory that is not related to the WAL.
// The manager should just ignore this file.
{
std::ofstream unrelated(kWALDir / "123-not-log");
unrelated << "Hello world!" << std::endl;
}
ASSERT_EQ(NumFilesInDir(), 1);
// Write some data to the log.
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForWrite(/*discard_existing_logs=*/true);
ASSERT_TRUE(s.ok());
// The unrelated file should not have been deleted.
ASSERT_EQ(NumFilesInDir(), 1);
s = manager.LogWrite(WriteOptions(), "hello-0", "world-0",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 2);
ASSERT_EQ(manager.IncrementLogVersion(), 0);
s = manager.LogWrite(WriteOptions(), "hello-1", "world-1",
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 3);
}
// Make sure we can read the written records.
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForReplay();
ASSERT_TRUE(s.ok());
ASSERT_EQ(NumFilesInDir(), 3);
std::vector<std::pair<std::string, std::string>> records;
s = manager.ReplayLog([&records](const Slice& key, const Slice& value,
format::WriteType type) {
EXPECT_EQ(type, format::WriteType::kWrite);
records.emplace_back(std::string(key.data(), key.size()),
std::string(value.data(), value.size()));
return Status::OK();
});
ASSERT_EQ(records.size(), 2);
ASSERT_EQ(records[0].first, "hello-0");
ASSERT_EQ(records[0].second, "world-0");
ASSERT_EQ(records[1].first, "hello-1");
ASSERT_EQ(records[1].second, "world-1");
s = manager.PrepareForWrite(/*discard_existing_logs=*/true);
ASSERT_TRUE(s.ok());
// Only the unrelated file should exist.
ASSERT_EQ(NumFilesInDir(), 1);
}
}
TEST_F(WALManagerTest, AbortReplayEarly) {
const std::vector<std::pair<std::string, std::string>> records = {
{"hello", "world"}, {"key", "value"}, {"foo", "bar"}};
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForWrite();
ASSERT_TRUE(s.ok());
// Log file is lazily created.
ASSERT_EQ(NumFilesInDir(), 0);
for (const auto& record : records) {
s = manager.LogWrite(WriteOptions(), record.first, record.second,
format::WriteType::kWrite);
ASSERT_TRUE(s.ok());
}
ASSERT_EQ(NumFilesInDir(), 1);
}
{
wal::Manager manager(kWALDir);
Status s = manager.PrepareForReplay();
ASSERT_TRUE(s.ok());
// Ensure we abort early if the callback returns a non-OK status.
size_t records_seen = 0;
s = manager.ReplayLog([&records_seen](const Slice& key, const Slice& value,
format::WriteType write_type) {
++records_seen;
return Status::NotFound("Expected error.");
});
ASSERT_TRUE(s.IsNotFound());
ASSERT_EQ(records_seen, 1);
ASSERT_TRUE(records_seen < records.size());
// Replaying the log should now go through all the records.
records_seen = 0;
s = manager.ReplayLog([&records_seen](const Slice& key, const Slice& value,
format::WriteType write_type) {
++records_seen;
return Status::OK();
});
ASSERT_TRUE(s.ok());
ASSERT_EQ(records_seen, records.size());
}
}
} // namespace