forked from NebulousLabs/writeaheadlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeaheadlog_test.go
394 lines (348 loc) · 11.8 KB
/
writeaheadlog_test.go
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
package wal
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/persist"
"github.com/NebulousLabs/fastrand"
)
// walTester holds a WAL along with some other fields
// useful for testing, and has methods implemented on it that can assist
// testing.
type walTester struct {
wal *WAL
updates []Update
logpath string
}
// newContractManagerTester returns a ready-to-rock contract manager tester.
func newWALTester(name string, cancel <-chan struct{}, walStopped chan struct{}, settings map[string]bool) (*walTester, error) {
if testing.Short() {
panic("use of newContractManagerTester during short testing")
}
// Create temp dir
testdir := build.TempDir("wal", name)
err := os.MkdirAll(testdir, 0700)
if err != nil {
return nil, err
}
// Create logger
var buf bytes.Buffer
log := persist.NewLogger(&buf)
logpath := filepath.Join(testdir, "log.wal")
updates, wal, err := New(logpath, log, cancel, walStopped, settings)
if err != nil {
return nil, err
}
cmt := &walTester{
wal: wal,
logpath: logpath,
updates: updates,
}
return cmt, nil
}
// getTransactionPages is
func TransactionPages(txn *Transaction) (pages []page) {
page := txn.firstPage
for page != nil {
pages = append(pages, *page)
page = page.nextPage
}
return
}
// TestTransactionInterrupted checks if an interrupt between committing and releasing a
// transaction is handled correctly upon reboot
func TestTransactionInterrupted(t *testing.T) {
cancel := make(chan struct{})
walStopped := make(chan struct{})
settings := make(map[string]bool)
settings["cleanWALFile"] = true
wt, err := newWALTester(t.Name(), cancel, walStopped, settings)
if err != nil {
t.Error(err)
}
// Create a transaction with 1 update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Version: "1.0",
Instructions: fastrand.Bytes(1234),
})
// Create one transaction which will be committed and one that will be applied
txn := wt.wal.NewTransaction(updates)
txn2 := wt.wal.NewTransaction(updates)
// wait for the transactions to be committed
wait := txn.SignalSetupComplete()
if err := <-wait; err != nil {
t.Errorf("SignalSetupComplete for the first transaction failed %v", err)
}
wait2 := txn2.SignalSetupComplete()
if err := <-wait2; err != nil {
t.Errorf("SignalSetupComplete for the second transaction failed")
}
// release the changes of the second transaction
wait2 = txn2.SignalApplyComplete()
if err := <-wait2; err != nil {
t.Errorf("SignalApplyComplete for the second transaction failed")
}
// Shutdown the wal without releasing the changes
cancel <- struct{}{}
select {
case <-walStopped:
}
// make sure the wal is still there
if _, err := os.Stat(wt.logpath); os.IsNotExist(err) {
t.Errorf("wal was deleted at %v", wt.logpath)
}
// Restart it and check if exactly 1 unfinished transaction is reported
cancel2 := make(chan struct{})
updates2, _, err := New(wt.logpath, wt.wal.log, cancel2, make(chan struct{}), make(map[string]bool))
if err != nil {
t.Error(err)
}
if len(updates2) != len(updates) {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
len(updates), len(updates2))
}
}
// TestWalParallel checks if the wal still works without errors under a high load parallel work
// The wal won't be deleted but reloaded instead to check if the amount of returned failed updates
// equals 0
func TestWalParallel(t *testing.T) {
cancel := make(chan struct{})
settings := make(map[string]bool)
settings["cleanWALFile"] = true
walStopped := make(chan struct{})
wt, err := newWALTester(t.Name(), cancel, walStopped, settings)
if err != nil {
t.Error(err)
}
// Prepare a random update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Version: "1.0",
Instructions: fastrand.Bytes(1234),
})
// Define a function that creates a transaction from this update and applies it
done := make(chan error)
f := func() {
// Create txn
txn := wt.wal.NewTransaction(updates)
// Wait for the txn to be committed
if err := <-txn.SignalSetupComplete(); err != nil {
done <- err
return
}
if err := <-txn.SignalApplyComplete(); err != nil {
done <- err
return
}
done <- nil
}
// Create numThreads instances of the function and wait for it to complete without error
numThreads := 10000
for i := 0; i < numThreads; i++ {
go f()
}
for i := 0; i < numThreads; i++ {
err := <-done
if err != nil {
t.Errorf("Thread %v failed: %v", i, err)
}
}
// The number of available pages should equal the number of created pages
if wt.wal.filePageCount != uint64(len(wt.wal.availablePages)) {
t.Errorf("number of available pages doesn't match the number of created ones. Expected %v, but was %v",
wt.wal.availablePages, wt.wal.filePageCount)
}
// shutdown the wal
close(cancel)
<-walStopped
// Get the fileinfo
fi, err := os.Stat(wt.logpath)
if os.IsNotExist(err) {
t.Errorf("wal was deleted but shouldn't have")
}
// Log some stats about the file
t.Logf("filesize: %v bytes", fi.Size())
t.Logf("used pages: %v", wt.wal.filePageCount)
// Restart it and check that no unfinished transactions are reported
cancel2 := make(chan struct{})
updates2, _, err := New(wt.logpath, wt.wal.log, cancel2, make(chan struct{}), make(map[string]bool))
if err != nil {
t.Error(err)
}
if len(updates2) != 0 {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
0, len(updates2))
}
}
// TestPageRecycling checks if pages are actually freed and used again after a transaction was applied
func TestPageRecycling(t *testing.T) {
cancel := make(chan struct{})
walStopped := make(chan struct{})
wt, err := newWALTester(t.Name(), cancel, walStopped, make(map[string]bool))
if err != nil {
t.Error(err)
}
// Prepare a random update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Version: "1.0",
Instructions: fastrand.Bytes(1234),
})
// Create txn
txn := wt.wal.NewTransaction(updates)
// Wait for the txn to be committed
if err := <-txn.SignalSetupComplete(); err != nil {
t.Errorf("SignalSetupComplete failed: %v", err)
}
// There should be no available pages before the transaction was applied
if len(wt.wal.availablePages) != 0 {
t.Errorf("Number of available pages should be 0 but was %v", len(wt.wal.availablePages))
}
if err := <-txn.SignalApplyComplete(); err != nil {
t.Errorf("SignalApplyComplete failed: %v", err)
}
usedPages := wt.wal.filePageCount
availablePages := len(wt.wal.availablePages)
// The number of used pages should be greater than 0
if usedPages == 0 {
t.Errorf("The number of used pages should be greater than 0")
}
// Make sure usedPages equals availablePages and remember the values
if usedPages != uint64(availablePages) {
t.Errorf("number of used pages doesn't match number of available pages")
}
// Create second txn
txn2 := wt.wal.NewTransaction(updates)
// Wait for the txn to be committed
if err := <-txn2.SignalSetupComplete(); err != nil {
t.Errorf("SignalSetupComplete failed: %v", err)
}
// There should be no available pages before the transaction was applied
if len(wt.wal.availablePages) != 0 {
t.Errorf("Number of available pages should be 0 but was %v", len(wt.wal.availablePages))
}
if err := <-txn2.SignalApplyComplete(); err != nil {
t.Errorf("SignalApplyComplete failed: %v", err)
}
// The number of used pages shouldn't have increased and still be equal to the number of available ones
if wt.wal.filePageCount != usedPages || len(wt.wal.availablePages) != availablePages {
t.Errorf("expected used pages %v, was %v", usedPages, wt.wal.filePageCount)
t.Errorf("expected available pages %v, was %v", availablePages, len(wt.wal.availablePages))
}
}
// TestRestoreTransactions checks that restoring transactions from a WAL works correctly
func TestRestoreTransactions(t *testing.T) {
cancel := make(chan struct{})
wt, err := newWALTester(t.Name(), cancel, make(chan struct{}), make(map[string]bool))
if err != nil {
t.Error(err)
}
// Create 10 transactions with 1 update each
txns := []Transaction{}
totalPages := []page{}
totalUpdates := []Update{}
for i := 0; i < 10; i++ {
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Version: "1.0",
Instructions: fastrand.Bytes(5000), // ensures that 2 pages will be created
})
totalUpdates = append(totalUpdates, updates...)
// Create a new transaction
txn := wt.wal.NewTransaction(updates)
wait := txn.SignalSetupComplete()
if err := <-wait; err != nil {
t.Errorf("SignalSetupComplete failed %v", err)
}
// Check that 2 pages were created
pages := TransactionPages(txn)
if len(pages) != 2 {
t.Errorf("Txn has wrong size. Expected %v but was %v", 2, len(pages))
}
totalPages = append(totalPages, pages...)
txns = append(txns, *txn)
}
// create a dictionary that takes a page offset and maps it to the page that points to that offset
previousPages := make(map[uint64]page)
for _, page := range totalPages {
if page.nextPage != nil {
previousPages[page.nextPage.offset] = page
}
}
// restore the transactions
recoveredTxns, err := wt.wal.restoreTransactions(totalPages, previousPages)
// check if the recovered transactions have the same length as before
if len(recoveredTxns) != len(txns) {
t.Errorf("Recovered txns don't have same length as before. Expected %v but was %v", len(txns),
len(recoveredTxns))
}
// check that all txns point to valid pages
for i, txn := range recoveredTxns {
if txn.firstPage == nil {
t.Errorf("%v: The firstPage of the txn is nil", i)
}
if txn.finalPage == nil {
t.Errorf("%v: The finalPage of the txn is nil", i)
}
if txn.firstPage.pageStatus != txns[i].firstPage.pageStatus {
t.Errorf("%v: The pageStatus of the txn is %v but should be",
i, txn.firstPage.pageStatus, txns[i].firstPage.pageStatus)
}
if txn.finalPage.transactionNumber != txns[i].finalPage.transactionNumber {
t.Errorf("%v: The transactionNumber of the txn is %v but should be",
i, txn.finalPage.transactionNumber, txns[i].finalPage.transactionNumber)
}
if txn.finalPage.transactionChecksum != txns[i].finalPage.transactionChecksum {
t.Errorf("%v: The transactionChecksum of the txn is %v but should be",
i, txn.finalPage.transactionChecksum, txns[i].finalPage.transactionChecksum)
}
}
// Decode the updates
recoveredUpdates := []Update{}
for _, txn := range recoveredTxns {
// loop over all the pages of the transaction, retrieve the payloads and decode them
page := txn.firstPage
var updateBytes []byte
for page != nil {
updateBytes = append(updateBytes, page.payload...)
page = page.nextPage
}
// Unmarshal the updates of the current transaction
var currentUpdates []Update
err := json.Unmarshal(updateBytes, ¤tUpdates)
if err != nil {
t.Errorf("Unmarshal of updates failed %v", err)
}
recoveredUpdates = append(recoveredUpdates, currentUpdates...)
}
// Check if the number of recovered updates matches the total number of original updates
if len(totalUpdates) != len(recoveredUpdates) {
t.Errorf("The number of recovered updates doesn't match the number of original updates."+
" expected %v but was %v", len(totalUpdates), len(recoveredUpdates))
}
// Check if the recovered updates match the original updates
originalData, err1 := json.Marshal(totalUpdates)
recoveredData, err2 := json.Marshal(recoveredUpdates)
if err1 != nil || err2 != nil {
t.Errorf("Failed to marshall data for comparison")
}
if bytes.Compare(originalData, recoveredData) != 0 {
t.Errorf("The recovered data doesn't match the original data")
}
// shutdown the wal
close(cancel)
time.Sleep(time.Second)
// make sure the wal is gone
if _, err := os.Stat(wt.logpath); !os.IsNotExist(err) {
t.Error("wal was not deleted after clean shutdown")
}
}