This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
writeaheadlog_test.go
1079 lines (937 loc) · 27.9 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package writeaheadlog
import (
"os"
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/NebulousLabs/fastrand"
)
// retry will call 'fn' 'tries' times, waiting 'durationBetweenAttempts'
// between each attempt, returning 'nil' the first time that 'fn' returns nil.
// If 'nil' is never returned, then the final error returned by 'fn' is
// returned.
func retry(tries int, durationBetweenAttempts time.Duration, fn func() error) (err error) {
for i := 1; i < tries; i++ {
err = fn()
if err == nil {
return nil
}
time.Sleep(durationBetweenAttempts)
}
return fn()
}
// tempDir joins the provided directories and prefixes them with the testing
// directory.
func tempDir(dirs ...string) string {
path := filepath.Join(os.TempDir(), "wal", filepath.Join(dirs...))
os.RemoveAll(path) // remove old test data
return path
}
// 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
recoveredTxns []*Transaction
path string
}
// Close is a helper function for a clean tester shutdown
func (wt *walTester) Close() error {
// Close wal
return wt.wal.Close()
}
// newWalTester returns a ready-to-rock walTester.
func newWALTester(name string, deps dependencies) (*walTester, error) {
// Create temp dir
testdir := tempDir(name)
err := os.MkdirAll(testdir, 0700)
if err != nil {
return nil, err
}
path := filepath.Join(testdir, "test.wal")
recoveredTxns, wal, err := newWal(path, deps)
if err != nil {
return nil, err
}
for _, txn := range recoveredTxns {
if err := txn.SignalUpdatesApplied(); err != nil {
wal.Close()
return nil, err
}
}
cmt := &walTester{
wal: wal,
recoveredTxns: recoveredTxns,
path: path,
}
return cmt, nil
}
// transactionPages returns all of the pages associated with a transaction.
func transactionPages(txn *Transaction) (pages []page) {
page := txn.firstPage
for page != nil {
pages = append(pages, *page)
page = page.nextPage
}
return
}
// TestCommitFailed checks if a corruption of the first page of the
// transaction during the commit is handled correctly
func TestCommitFailed(t *testing.T) {
wt, err := newWALTester(t.Name(), &dependencyCommitFail{})
if err != nil {
t.Fatal(err)
}
// Create a transaction with 1 update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(1234),
})
// Create the transaction
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// Committing the txn should fail on purpose
wait := txn.SignalSetupComplete()
if err := <-wait; err == nil {
t.Error("SignalSetupComplete should have failed but didn't")
}
// shutdown the wal
wt.Close()
// make sure the wal is still there
if _, err := os.Stat(wt.path); os.IsNotExist(err) {
t.Errorf("wal was deleted at %v", wt.path)
}
// Restart it. No unfinished updates should be reported since they were
// never committed
updates2, w, err := New(wt.path)
if err != nil {
t.Fatal(err)
}
defer w.Close()
if len(updates2) != 0 {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
0, len(updates2))
}
}
// TestMisleadingWrite tests the scenario where Write returns nil, but Sync
// later returns an error.
func TestMisleadingWrite(t *testing.T) {
t.Skip("not implemented yet")
}
func BenchmarkMarshalUpdates(b *testing.B) {
updates := make([]Update, 100)
for i := range updates {
updates[i] = Update{
Name: "test",
Instructions: fastrand.Bytes(1234),
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
marshalUpdates(updates)
}
}
func BenchmarkUnmarshalUpdates(b *testing.B) {
updates := make([]Update, 100)
for i := range updates {
updates[i] = Update{
Name: "test",
Instructions: fastrand.Bytes(1234),
}
}
data := marshalUpdates(updates)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := unmarshalUpdates(data)
if err != nil {
b.Fatal(err)
}
}
}
// TestReleaseFailed checks if a corruption of the first page of the
// transaction during the commit is handled correctly
func TestReleaseFailed(t *testing.T) {
wt, err := newWALTester(t.Name(), &dependencyReleaseFail{})
if err != nil {
t.Fatal(err)
}
// Create a transaction with 1 update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(1234),
})
// Create the transaction
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// Committing the txn should fail on purpose
wait := txn.SignalSetupComplete()
if err := <-wait; err != nil {
t.Errorf("SignalSetupComplete failed %v", err)
}
// Committing the txn should fail on purpose
if err := txn.SignalUpdatesApplied(); err == nil {
t.Error("SignalUpdatesApplies should have failed but didn't")
}
// shutdown the wal
wt.Close()
// make sure the wal is still there
if _, err := os.Stat(wt.path); os.IsNotExist(err) {
t.Errorf("wal was deleted at %v", wt.path)
}
// Restart it. There should be 1 unfinished update since it was committed
// but never released
updates2, w, err := New(wt.path)
if err != nil {
t.Fatal(err)
}
defer w.Close()
if len(updates2) != 1 {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
1, len(updates2))
}
}
// TestReleaseNotCalled checks if an interrupt between committing and releasing a
// transaction is handled correctly upon reboot
func TestReleaseNotCalled(t *testing.T) {
wt, err := newWALTester(t.Name(), &dependencyUncleanShutdown{})
if err != nil {
t.Fatal(err)
}
// Create a transaction with 1 update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(1234),
})
// Create one transaction which will be committed and one that will be applied
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
txn2, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// 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
if err := txn2.SignalUpdatesApplied(); err != nil {
t.Errorf("SignalApplyComplete for the second transaction failed")
}
// shutdown the wal
wt.Close()
// make sure the wal is still there
if _, err := os.Stat(wt.path); os.IsNotExist(err) {
t.Errorf("wal was deleted at %v", wt.path)
}
// Restart it and check if exactly 1 unfinished transaction is reported
updates2, w, err := New(wt.path)
if err != nil {
t.Fatal(err)
}
defer w.Close()
if len(updates2) != len(updates) {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
len(updates), len(updates2))
}
}
// TestPayloadCorrupted creates 2 update and corrupts the first one. Therefore
// no unfinished transactions should be reported because the second one is
// assumed to be corrupted too
func TestPayloadCorrupted(t *testing.T) {
wt, err := newWALTester(t.Name(), &dependencyUncleanShutdown{})
if err != nil {
t.Fatal(err)
}
// Create a transaction with 1 update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(1234),
})
// Create 2 txns
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
txn2, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// Committing the txns but don't release them
wait := txn.SignalSetupComplete()
if err := <-wait; err != nil {
t.Errorf("SignalSetupComplete failed %v", err)
}
wait = txn2.SignalSetupComplete()
if err := <-wait; err != nil {
t.Errorf("SignalSetupComplete failed %v", err)
}
// Corrupt the payload of the first txn
txn.firstPage.payload = fastrand.Bytes(2000)
_, err = txn.wal.logFile.WriteAt(txn.firstPage.appendTo(nil), int64(txn.firstPage.offset))
if err != nil {
t.Errorf("Corrupting the page failed %v", err)
}
// shutdown the wal
wt.Close()
// make sure the wal is still there
if _, err := os.Stat(wt.path); os.IsNotExist(err) {
t.Errorf("wal was deleted at %v", wt.path)
}
// Restart it. 1 unfinished transaction should be reported
updates2, w, err := New(wt.path)
if err != nil {
t.Fatal(err)
}
defer w.Close()
if len(updates2) != 1 {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
1, len(updates2))
}
}
// TestPayloadCorrupted2 creates 2 update and corrupts the second one. Therefore
// one unfinished transaction should be reported
func TestPayloadCorrupted2(t *testing.T) {
wt, err := newWALTester(t.Name(), &dependencyUncleanShutdown{})
if err != nil {
t.Fatal(err)
}
// Create a transaction with 1 update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(1234),
})
// Create 2 txns
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
txn2, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// Committing the txns but don't release them
wait := txn.SignalSetupComplete()
if err := <-wait; err != nil {
t.Errorf("SignalSetupComplete failed %v", err)
}
wait = txn2.SignalSetupComplete()
if err := <-wait; err != nil {
t.Errorf("SignalSetupComplete failed %v", err)
}
// Corrupt the payload of the second txn
txn2.firstPage.payload = fastrand.Bytes(2000)
_, err = txn2.wal.logFile.WriteAt(txn2.firstPage.appendTo(nil), int64(txn2.firstPage.offset))
if err != nil {
t.Errorf("Corrupting the page failed %v", err)
}
// shutdown the wal
wt.Close()
// make sure the wal is still there
if _, err := os.Stat(wt.path); os.IsNotExist(err) {
t.Errorf("wal was deleted at %v", wt.path)
}
// Restart it. 1 Unfinished transaction should be reported.
updates2, w, err := New(wt.path)
if err != nil {
t.Fatal(err)
}
defer w.Close()
if len(updates2) != 1 {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
0, 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) {
wt, err := newWALTester(t.Name(), &dependencyProduction{})
if err != nil {
t.Fatal(err)
}
// Prepare a random update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
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, err := wt.wal.NewTransaction(updates)
if err != nil {
done <- err
return
}
// Wait for the txn to be committed
if err := <-txn.SignalSetupComplete(); err != nil {
done <- err
return
}
if err := txn.SignalUpdatesApplied(); err != nil {
done <- err
return
}
done <- nil
}
// Create numThreads instances of the function and wait for it to complete without error
numThreads := 1000
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
wt.Close()
// Get the fileinfo
fi, err := os.Stat(wt.path)
if os.IsNotExist(err) {
t.Fatalf("wal was deleted but shouldn't have")
}
// Log some stats about the file
t.Logf("filesize: %v mb", float64(fi.Size())/float64(1e+6))
t.Logf("used pages: %v", wt.wal.filePageCount)
// Restart it and check that no unfinished transactions are reported
updates2, w, err := New(wt.path)
if err != nil {
t.Error(err)
}
defer w.Close()
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) {
wt, err := newWALTester(t.Name(), &dependencyProduction{})
if err != nil {
t.Error(err)
}
defer wt.Close()
// Prepare a random update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(5000),
})
// Create txn
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// 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.SignalUpdatesApplied(); 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, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// 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.SignalUpdatesApplied(); 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) {
t.Skip("broken")
// wt, err := newWALTester(t.Name(), dependencyUncleanShutdown{})
// if err != nil {
// t.Error(err)
// }
// defer wt.Close()
// // Create 10 transactions with 1 update each
// txns := []Transaction{}
// totalPages := []page{}
// totalUpdates := []Update{}
// for i := 0; i < 2; 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, err := wt.wal.NewTransaction(updates)
// if err != nil {
// t.Fatal(err)
// }
// 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)
// }
// // restore the transactions
// recoveredTxns := []Transaction{}
// logData, err := ioutil.ReadFile(wt.path)
// if err != nil {
// t.Fatal(err)
// }
// for _, txn := range txns {
// var restoredTxn Transaction
// err := unmarshalTransaction(&restoredTxn, txn.firstPage, txn.firstPage.nextPage.offset, logData)
// if err != nil {
// t.Error(err)
// }
// recoveredTxns = append(recoveredTxns, restoredTxn)
// }
// // 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.firstPage.pageStatus != txns[i].firstPage.pageStatus {
// t.Errorf("%v: The pageStatus of the txn is %v but should be",
// txn.firstPage.pageStatus, txns[i].firstPage.pageStatus)
// }
// }
// // 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
// currentUpdates, err := unmarshalUpdates(updateBytes)
// 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")
// }
}
// TestRecoveryFailed checks if the WAL behave correctly if a crash occurs
// during a call to RecoveryComplete
func TestRecoveryFailed(t *testing.T) {
wt, err := newWALTester(t.Name(), &dependencyUncleanShutdown{})
if err != nil {
t.Error(err)
}
// Prepare random updates
numUpdates := 10
updates := []Update{}
for i := 0; i < numUpdates; i++ {
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(10000),
})
}
// Create txn
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
return
}
// Wait for the txn to be committed
if err = <-txn.SignalSetupComplete(); err != nil {
return
}
// Close and restart the wal.
if err := wt.Close(); err == nil {
t.Error("There should have been an error but there wasn't")
}
recoveredTxns2, w, err := newWal(wt.path, &dependencyRecoveryFail{})
if err != nil {
t.Fatal(err)
}
// New should return numUpdates updates
numRecoveredUpdates := 0
for _, txn := range recoveredTxns2 {
numRecoveredUpdates += len(txn.Updates)
}
if numRecoveredUpdates != numUpdates {
t.Errorf("There should be %v updates but there were %v", numUpdates, numRecoveredUpdates)
}
// Signal that the recovery is complete
for _, txn := range recoveredTxns2 {
if err := txn.SignalUpdatesApplied(); err != nil {
t.Errorf("Failed to signal applied updates: %v", err)
}
}
// Restart the wal again
if err := w.Close(); err != nil {
t.Errorf("Failed to close wal: %v", err)
}
recoveredTxns3, w, err := New(wt.path)
if err != nil {
t.Fatal(err)
}
// There should be 0 updates this time
if len(recoveredTxns3) != 0 {
t.Errorf("There should be %v updates but there were %v", 0, len(recoveredTxns3))
}
// The metadata should say "unclean"
mdData := make([]byte, pageSize)
if _, err := w.logFile.ReadAt(mdData, 0); err != nil {
t.Fatal(err)
}
recoveryState, err := readWALMetadata(mdData)
if err != nil {
t.Fatal(err)
}
if recoveryState != recoveryStateUnclean {
t.Errorf("recoveryState should be %v but was %v",
recoveryStateUnclean, recoveryState)
}
// Close the wal again and check that the file still exists on disk
if err := w.Close(); err != nil {
t.Errorf("Failed to close wal: %v", err)
}
_, err = os.Stat(wt.path)
if os.IsNotExist(err) {
t.Errorf("wal was deleted but shouldn't have")
}
}
// TestTransactionAppend tests the functionality of the Transaction's append
// call
func TestTransactionAppend(t *testing.T) {
wt, err := newWALTester(t.Name(), &dependencyProduction{})
if err != nil {
t.Fatal(err)
}
// Create a transaction with 1 update
updates := []Update{{
Name: "test",
Instructions: fastrand.Bytes(3000),
}}
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
t.Fatal(err)
}
// Append another update
if err := <-txn.Append(updates); err != nil {
t.Errorf("Append failed: %v", err)
}
// 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)
}
// shutdown the wal
wt.Close()
// Restart it and check if exactly 2 unfinished transactions are reported
recoveredTxns2, w, err := New(wt.path)
if err != nil {
t.Fatal(err)
}
defer w.Close()
if len(recoveredTxns2[0].Updates) != len(updates)*2 {
t.Errorf("Number of updates after restart didn't match. Expected %v, but was %v",
len(updates)*2, len(recoveredTxns2[0].Updates))
}
}
// benchmarkTransactionSpeed is a helper function to create benchmarks that run
// for 1 min to find out how many transactions can be applied to the wal and
// how large the wal grows during that time using a certain number of threads.
// When appendUpdate is set to 'true', a second update will be appended to the
// transaction before it is committed.
func benchmarkTransactionSpeed(b *testing.B, numThreads int, appendUpdate bool) {
b.Logf("Running benchmark with %v threads", numThreads)
wt, err := newWALTester(b.Name(), &dependencyProduction{})
if err != nil {
b.Error(err)
}
defer wt.Close()
// Prepare a random update
updates := []Update{}
updates = append(updates, Update{
Name: "test",
Instructions: fastrand.Bytes(4000), // 1 page / txn
})
// Define a function that creates a transaction from this update and
// applies it. It returns the duration it took to commit the transaction.
f := func() (latency time.Duration, err error) {
// Get start time
startTime := time.Now()
// Create txn
txn, err := wt.wal.NewTransaction(updates)
if err != nil {
return
}
// Append second update
if appendUpdate {
if err = <-txn.Append(updates); err != nil {
return
}
}
// Wait for the txn to be committed
if err = <-txn.SignalSetupComplete(); err != nil {
return
}
// Calculate latency after committing
latency = time.Since(startTime)
if err = txn.SignalUpdatesApplied(); err != nil {
return
}
return latency, nil
}
// Create a channel to stop threads
stop := make(chan struct{})
// Create atomic variables to count transactions and errors
var atomicNumTxns uint64
var atomicNumErr uint64
// Create waitgroup to wait for threads before reading the counters
var wg sync.WaitGroup
// Save latencies
latencies := make([]time.Duration, numThreads, numThreads)
// Start threads
for i := 0; i < numThreads; i++ {
wg.Add(1)
go func(j int) {
defer wg.Done()
for {
// Check for stop signal
select {
case <-stop:
return
default:
}
// Execute the function
latency, err := f()
if err != nil {
// Abort thread on error
atomic.AddUint64(&atomicNumErr, 1)
return
}
atomic.AddUint64(&atomicNumTxns, 1)
// Remember highest latency
if latency > latencies[j] {
latencies[j] = latency
}
}
}(i)
}
// Kill threads after 1 minute
select {
case <-time.After(time.Minute):
close(stop)
}
// Wait for each thread to finish
wg.Wait()
// Check if any errors happened
if atomicNumErr > 0 {
b.Fatalf("%v errors happened during execution", atomicNumErr)
}
// Get the fileinfo
fi, err := os.Stat(wt.path)
if os.IsNotExist(err) {
b.Errorf("wal was deleted but shouldn't have")
}
// Find the maximum latency it took to commit a transaction
var maxLatency time.Duration
for i := 0; i < numThreads; i++ {
if latencies[i] > maxLatency {
maxLatency = latencies[i]
}
}
// Log results
b.Logf("filesize: %v mb", float64(fi.Size())/float64(1e+6))
b.Logf("used pages: %v", wt.wal.filePageCount)
b.Logf("total transactions: %v", atomicNumTxns)
b.Logf("txn/s: %v", float64(atomicNumTxns)/60.0)
b.Logf("maxLatency: %v", maxLatency)
}
// BenchmarkTransactionSpeedAppend runs benchmarkTransactionSpeed with append =
// false
func BenchmarkTransactionSpeed(b *testing.B) {
numThreads := []int{1, 10, 50, 100}
for _, n := range numThreads {
b.Run(strconv.Itoa(n), func(b *testing.B) {
benchmarkTransactionSpeed(b, n, false)
})
}
}
// BenchmarkTransactionSpeedAppend runs benchmarkTransactionSpeed with append =
// true
func BenchmarkTransactionSpeedAppend(b *testing.B) {
numThreads := []int{1, 10, 50, 100}
for _, n := range numThreads {
b.Run(strconv.Itoa(n), func(b *testing.B) {
benchmarkTransactionSpeed(b, n, true)
})
}
}
// benchmarkDiskWrites writes numThreads pages of pageSize size and spins up 1
// goroutine for each page that overwrites it numWrites times
func benchmarkDiskWrites(b *testing.B, numWrites int, pageSize int, numThreads int) {
b.Logf("Starting benchmark with %v writes and %v threads for pages of size %v",
numWrites, numThreads, pageSize)
// Get a tmp dir path
tmpdir := tempDir(b.Name())
// Create dir
err := os.MkdirAll(tmpdir, 0700)
if err != nil {
b.Fatal(err)
}
// Create a tmp file
f, err := os.Create(filepath.Join(tmpdir, "wal.dat"))
if err != nil {
b.Fatal(err)
}
// Close it after test
defer f.Close()
// Write numThreads pages to file
_, err = f.Write(fastrand.Bytes(pageSize * numThreads))
if err != nil {
b.Fatal(err)
}