forked from tipabu/erasurecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_test.go
1054 lines (945 loc) · 28.5 KB
/
backend_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 erasurecode
import (
"bytes"
"fmt"
"math/rand"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
var validParams = []Params{
{Name: "liberasurecode_rs_vand", K: 2, M: 1},
{Name: "liberasurecode_rs_vand", K: 10, M: 4},
{Name: "liberasurecode_rs_vand", K: 4, M: 3},
{Name: "liberasurecode_rs_vand", K: 8, M: 4},
{Name: "liberasurecode_rs_vand", K: 15, M: 4},
{Name: "isa_l_rs_vand", K: 2, M: 1},
{Name: "isa_l_rs_vand", K: 2, M: 1, MaxBlockSize: 1},
{Name: "isa_l_rs_vand", K: 2, M: 1, MaxBlockSize: maxBuffer * 2},
{Name: "isa_l_rs_vand", K: 10, M: 4},
{Name: "isa_l_rs_vand", K: 4, M: 3},
{Name: "isa_l_rs_vand", K: 8, M: 4},
{Name: "isa_l_rs_vand", K: 15, M: 4},
{Name: "isa_l_rs_cauchy", K: 2, M: 1},
{Name: "isa_l_rs_cauchy", K: 10, M: 4},
{Name: "isa_l_rs_cauchy", K: 4, M: 3},
{Name: "isa_l_rs_cauchy", K: 8, M: 4},
{Name: "isa_l_rs_cauchy", K: 15, M: 4},
{Name: "jerasure_rs_vand", K: 2, M: 1},
{Name: "jerasure_rs_vand", K: 10, M: 4},
{Name: "jerasure_rs_vand", K: 4, M: 3},
{Name: "jerasure_rs_vand", K: 8, M: 4},
{Name: "jerasure_rs_vand", K: 15, M: 4},
{Name: "jerasure_rs_cauchy", K: 2, M: 1},
{Name: "jerasure_rs_cauchy", K: 10, M: 4},
{Name: "jerasure_rs_cauchy", K: 4, M: 3},
{Name: "jerasure_rs_cauchy", K: 8, M: 4},
{Name: "jerasure_rs_cauchy", K: 15, M: 4, W: 5},
}
var testPatterns = [][]byte{
bytes.Repeat([]byte{0x00}, 1),
bytes.Repeat([]byte{0xff}, 1),
bytes.Repeat([]byte{0x00}, 1<<10),
bytes.Repeat([]byte{0xff}, 1<<10),
bytes.Repeat([]byte{0x00}, 1<<20),
bytes.Repeat([]byte{0xff}, 1<<20),
bytes.Repeat([]byte{0xf0, 0x0f}, 512),
bytes.Repeat([]byte{0xde, 0xad, 0xbe, 0xef}, 256),
bytes.Repeat([]byte{0xaa}, 1024),
bytes.Repeat([]byte{0x55}, 1024),
bytes.Repeat([]byte{0x55}, 2234345),
}
func shuf(src [][]byte) [][]byte {
dest := make([][]byte, len(src))
perm := rand.Perm(len(src))
for i, v := range perm {
dest[v] = src[i]
}
return dest
}
func TestGetVersion(t *testing.T) {
v := GetVersion()
t.Logf("INFO: Using liberasurecode version %s", v)
if v.Major != 1 {
t.Errorf("Expected major version number 1, not %d", v.Major)
}
if v.Less(Version{1, 4, 0}) {
t.Errorf("liberasurecode_get_version was introduced in 1.4.0; got %v", v)
}
}
func TestInitBackend(t *testing.T) {
for _, params := range validParams {
backend, err := InitBackend(params)
if !BackendIsAvailable(params.Name) {
if err == nil {
t.Errorf("Expected EBACKENDNOTAVAIL")
}
continue
}
if err != nil {
t.Errorf("%q", err)
continue
}
if backend.libecDesc <= 0 {
t.Errorf("Expected backend descriptor > 0, got %d", backend.libecDesc)
}
if err = backend.Close(); err != nil {
t.Errorf("%q", err)
}
if err = backend.Close(); err == nil {
t.Errorf("Expected error when closing an already-closed backend.")
}
}
}
func TestInitBackendFailure(t *testing.T) {
cases := []struct {
params Params
want string
}{
{Params{Name: "liberasurecode_rs_vand", K: -1, M: 1},
"instance_create() returned EINVALIDPARAMS"},
{Params{Name: "liberasurecode_rs_vand", K: 10, M: -1},
"instance_create() returned EINVALIDPARAMS"},
{Params{Name: "non-existent-backend", K: 10, M: 4},
"unsupported backend \"non-existent-backend\""},
{Params{Name: "", K: 10, M: 4},
"unsupported backend \"\""},
{Params{Name: "liberasurecode_rs_vand", K: 20, M: 20},
"instance_create() returned EINVALIDPARAMS"},
{Params{Name: "flat_xor_hd", K: 4, M: 4, HD: 3},
"instance_create() returned EBACKENDINITERR"},
}
for _, args := range cases {
backend, err := InitBackend(args.params)
if err == nil {
t.Errorf("Expected error when calling InitBackend(%v)",
args.params)
_ = backend.Close()
continue
}
if err.Error() != args.want {
t.Errorf("InitBackend(%v) produced error %q, want %q",
args.params, err, args.want)
}
if backend.libecDesc != 0 {
t.Errorf("InitBackend(%v) produced backend with descriptor %v, want 0",
args.params, backend.libecDesc)
_ = backend.Close()
}
}
}
func TestEncodeDecode(t *testing.T) {
for _, params := range validParams {
if !BackendIsAvailable(params.Name) {
continue
}
backend, err := InitBackend(params)
if err != nil {
t.Errorf("Error creating backend %v: %q", params, err)
continue
}
for patternIndex, pattern := range testPatterns {
data, err := backend.Encode(pattern)
if err != nil {
t.Errorf("Error encoding %v: %q", params, err)
break
}
expectedVersion := GetVersion()
frags := data.Data
for index, frag := range frags {
info := GetFragmentInfo(frag)
if info.Index != index {
t.Errorf("Expected frag %v to have index %v; got %v", index, index, info.Index)
}
if info.Size != len(frag)-80 { // 80 == sizeof (struct fragment_header_s)
t.Errorf("Expected frag %v to have size %v; got %v", index, len(frag)-80, info.Size)
}
if info.OrigDataSize != uint64(len(pattern)) {
t.Errorf("Expected frag %v to have orig_data_size %v; got %v", index, len(pattern), info.OrigDataSize)
}
if info.BackendName != params.Name {
t.Errorf("Expected frag %v to have backend %v; got %v", index, params.Name, info.BackendName)
}
if info.ErasureCodeVersion != expectedVersion {
t.Errorf("Expected frag %v to have EC version %v; got %v", index, expectedVersion, info.ErasureCodeVersion)
}
if !info.IsValid {
t.Errorf("Expected frag %v to be valid", index)
}
}
decode := func(frags [][]byte, description string) bool {
decoded, err := backend.Decode(frags)
if err != nil {
t.Errorf("%v: %v: %q for pattern %d", description, backend, err, patternIndex)
return false
} else if !bytes.Equal(decoded.Data, pattern) {
t.Errorf("%v: Expected %v to roundtrip pattern %d, got %q", description, backend, patternIndex, decoded.Data)
return false
}
decoded.Free()
return true
}
var good bool
good = decode(frags, "all frags")
good = good && decode(shuf(frags), "all frags, shuffled")
good = good && decode(frags[:params.K], "data frags")
good = good && decode(shuf(frags[:params.K]), "shuffled data frags")
good = good && decode(frags[params.M:], "with parity frags")
good = good && decode(shuf(frags[params.M:]), "shuffled parity frags")
if !good {
break
}
data.Free()
}
if _, err := backend.Decode([][]byte{}); err == nil {
t.Errorf("Expected error when decoding from empty fragment array")
}
err = backend.Close()
if err != nil {
t.Errorf("Error closing backend %v: %q", backend, err)
}
}
}
func TestReconstruct(t *testing.T) {
for _, params := range validParams {
if !BackendIsAvailable(params.Name) {
continue
}
backend, err := InitBackend(params)
if err != nil {
t.Errorf("Error creating backend %v: %q", params, err)
_ = backend.Close()
continue
}
for patternIndex, pattern := range testPatterns {
data, err := backend.Encode(pattern)
frags := data.Data
if err != nil {
t.Errorf("Error encoding %v: %q", params, err)
}
reconstruct := func(recon_frags [][]byte, frag_index int, description string) bool {
data, err := backend.Reconstruct(recon_frags, frag_index)
if err != nil {
t.Errorf("%v: %v: %q for pattern %d", description, backend, err, patternIndex)
return false
} else if !bytes.Equal(data, frags[frag_index]) {
t.Errorf("%v: Expected %v to roundtrip pattern %d, got %q", description, backend, patternIndex, data)
return false
}
return true
}
var good bool
good = reconstruct(shuf(frags[:params.K]), params.K+params.M-1, "last frag from data frags")
good = good && reconstruct(shuf(frags[params.M:]), 0, "first frag with parity frags")
if !good {
break
}
data.Free()
}
if _, err := backend.Reconstruct([][]byte{}, 0); err == nil {
t.Errorf("Expected error when reconstructing from empty fragment array")
}
err = backend.Close()
if err != nil {
t.Errorf("Error closing backend %v: %q", backend, err)
}
}
}
func TestIsInvalidFragment(t *testing.T) {
for _, params := range validParams {
if !BackendIsAvailable(params.Name) {
continue
}
backend, err := InitBackend(params)
if err != nil {
t.Errorf("Error creating backend %v: %q", params, err)
_ = backend.Close()
continue
}
for patternIndex, pattern := range testPatterns {
data, err := backend.Encode(pattern)
if err != nil {
t.Errorf("Error encoding %v: %q", params, err)
continue
}
frags := data.Data
for index, frag := range frags {
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid for pattern %d", backend, index, patternIndex)
}
fragCopy := make([]byte, len(frag))
copy(fragCopy, frag)
// corrupt the frag
corruptedByte := rand.Intn(len(frag))
for 71 <= corruptedByte && corruptedByte < 80 {
// in the alignment padding -- try again
corruptedByte = rand.Intn(len(frag))
}
frag[corruptedByte] ^= 0xff
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly valid after inverting byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
if corruptedByte < 4 || 8 <= corruptedByte && corruptedByte <= 59 {
/** corruption is in metadata; claim we were created by a version of
* libec that predates metadata checksums. Note that
* Note that a corrupted fragment size (bytes 4-7) will lead to a
* segfault when we try to verify the fragment -- there's a reason
* we added metadata checksums!
*/
copy(frag[63:67], []byte{9, 1, 1, 0})
if 20 <= corruptedByte && corruptedByte <= 53 {
/** Corrupted data checksum type or data checksum
* We may or may not detect this type of error; in particular,
* - if data checksum type is not in ec_checksum_type_t,
* it is ignored
* - if data checksum is mangled, we may still be valid
* under the "alternative" CRC32; this seems more likely
* with the byte inversion when the data is short
* Either way, though, clearing the checksum type should make
* it pass.
*/
frag[20] = 0
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after clearing metadata crc and disabling data crc", backend, index)
}
} else if corruptedByte >= 54 || 0 <= corruptedByte && corruptedByte < 4 {
/** Some corruptions of some bytes are still detectable. Since we're
* inverting the byte, we can detect:
* - frag index -- bytes 0-3
* - data checksum type -- byte 20
* - data checksum mismatch -- byte 54
* - backend id -- byte 55
* - backend version -- bytes 56-59
*/
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly still valid after clearing metadata crc", backend, index)
}
} else {
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after clearing metadata crc", backend, index)
}
}
} else if corruptedByte >= 67 {
copy(frag[20:25], []byte{1, 0, 0, 0, 0})
// And since we've changed the metadata, roll back version as above...
copy(frag[63:67], []byte{9, 1, 1, 0})
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after clearing data crc", backend, index)
t.FailNow()
}
}
frag[corruptedByte] ^= 0xff
copy(frag[63:67], fragCopy[63:67])
copy(frag[20:25], fragCopy[20:25])
if !bytes.Equal(frag, fragCopy) {
for i, orig := range fragCopy {
if frag[i] != orig {
t.Logf("%v != %v at index %v", frag[i], orig, i)
}
}
t.Fatal(corruptedByte, frag, fragCopy)
}
frag[corruptedByte]++
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly valid after incrementing byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
frag[corruptedByte] -= 2
if corruptedByte >= 63 && corruptedByte < 67 && frag[corruptedByte] != 0xff {
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after decrementing version byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
} else {
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly valid after decrementing byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
}
}
data.Free()
}
err = backend.Close()
if err != nil {
t.Errorf("Error closing backend %v: %q", backend, err)
}
}
}
func TestBackendIsAvailable(t *testing.T) {
requiredBackends := []string{
"null",
"flat_xor_hd",
"liberasurecode_rs_vand",
}
optionalBackends := []string{
"isa_l_rs_vand",
"isa_l_rs_cauchy",
"jerasure_rs_vand",
"jerasure_rs_cauchy",
"shss",
"libphazr",
}
for _, name := range requiredBackends {
if !BackendIsAvailable(name) {
t.Fatalf("%v is not available", name)
}
}
for _, name := range optionalBackends {
if !BackendIsAvailable(name) {
t.Logf("INFO: backend not available: %v", name)
}
}
}
// TestGC do multiple decode / reconstruct in concurrent goroutine.
// When enough data has been allocated and freed, the GC will wakeup
// and may destroy a block in-use. If this block is re-allocated,
// the content is most likely no longer valid and it should
// trigger an error or a crash.
func TestGC(t *testing.T) {
input := bytes.Repeat([]byte("X"), 1000000)
backend, err := InitBackend(
Params{
Name: "liberasurecode_rs_vand",
K: 2,
M: 1,
})
if err != nil {
t.Logf("Cannot run test because %s", err)
return
}
tests := []struct {
name string
testFunc func()
}{{
"Reconstruct",
func() {
encoded, err := backend.Encode(input)
if err != nil {
t.Fatal("cannot encode data")
return
}
vect := encoded.Data
defer encoded.Free()
oldData := vect[0][:] // force a copy
data, err := backend.Reconstruct(vect[1:3], 0)
if err != nil {
t.Fatalf("cannot reconstruct data, %s", err)
return
}
if len(data) != len(oldData) {
t.Fatal("reconstructing failed")
return
}
},
},
{
"Decode",
func() {
encoded, err := backend.Encode(input)
if err != nil {
t.Fatal("cannot encode data")
return
}
defer encoded.Free()
vect := encoded.Data
decoded, err := backend.Decode(vect[0:2])
if err != nil {
t.Fatalf("cannot decode data: %v", err)
return
}
defer decoded.Free()
data := decoded.Data
if len(data) != len(input) {
t.Fatal("decoding failed")
return
}
},
},
}
nbRoutines := 500
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(nbRoutines)
for i := 0; i < nbRoutines; i++ {
go func() {
test.testFunc()
wg.Done()
}()
}
wg.Wait()
})
}
backend.Close()
}
func TestAvailableBackends(t *testing.T) {
for _, name := range AvailableBackends() {
backend, err := InitBackend(Params{Name: name, K: 3, M: 3, HD: 3})
if err != nil {
t.Errorf("Error creating backend %v: %q", name, err)
}
_ = backend.Close()
}
t.Logf("INFO: found %v/%v available backends", len(AvailableBackends()), len(KnownBackends))
}
func BenchmarkEncode(b *testing.B) {
backend, _ := InitBackend(Params{Name: "isa_l_rs_vand", K: 4, M: 2, W: 8, HD: 5})
buf := bytes.Repeat([]byte("A"), 1024*1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
encoded, err := backend.Encode(buf)
if err != nil {
b.Fatal(err)
}
encoded.Free()
}
backend.Close()
}
const DefaultChunkSize = 32768
const DefaultFragSize = 1048576
type decodeTest struct {
size int
p Params
}
func (d decodeTest) String() string {
return fmt.Sprintf("%s-%d+%d-%db", d.p.Name, d.p.K, d.p.M, d.size)
}
var decodeTests = []decodeTest{
{1000 * 1000, Params{Name: "isa_l_rs_vand", K: 5, M: 1}},
{1024 * 1024, Params{Name: "isa_l_rs_vand", K: 4, M: 2, W: 8, HD: 5}},
{5 * 100000, Params{Name: "isa_l_rs_vand", K: 5, M: 7}},
// Will force an allocation of a new (dedicated) pool
{10000000, Params{Name: "isa_l_rs_vand", K: 2, M: 1, MaxBlockSize: 10000000 + maxBuffer}},
// Will force an allocation at every encoding (but should work)
{maxBuffer * 2, Params{Name: "isa_l_rs_vand", K: 2, M: 1, MaxBlockSize: maxBuffer / 2}},
}
func BenchmarkDecodeM(b *testing.B) {
for _, test := range decodeTests {
b.Run(test.String(), func(b *testing.B) {
backend, err := InitBackend(test.p)
if err != nil {
b.Fatal("cannot create backend", err)
}
defer backend.Close()
buf := bytes.Repeat([]byte("A"), test.size)
encoded, err := backend.EncodeMatrix(buf, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
defer encoded.Free()
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoded, err := backend.DecodeMatrix(encoded.Data, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
if decoded != nil {
if decoded.Free != nil {
decoded.Free()
}
} else {
b.Fatal("decoded is nil")
}
}
})
}
}
func BenchmarkDecodeMissingM(b *testing.B) {
for _, test := range decodeTests {
b.Run(test.String(), func(b *testing.B) {
backend, err := InitBackend(test.p)
if err != nil {
b.Fatal("cannot create backend", err)
}
defer backend.Close()
buf := bytes.Repeat([]byte("A"), test.size)
encoded, err := backend.EncodeMatrix(buf, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
defer encoded.Free()
data := encoded.Data[1:]
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoded, err := backend.DecodeMatrix(data, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
if decoded != nil {
if decoded.Free != nil {
decoded.Free()
}
} else {
b.Fatal("decoded is nil")
}
}
})
}
}
func BenchmarkReconstruct(b *testing.B) {
for _, test := range decodeTests {
b.Run(test.String(), func(b *testing.B) {
backend, err := InitBackend(test.p)
if err != nil {
b.Fatal("cannot create backend", err)
}
defer backend.Close()
buf := bytes.Repeat([]byte("A"), test.size)
encoded, err := backend.Encode(buf)
if err != nil {
b.Fatal(err)
}
defer encoded.Free()
flags := encoded.Data[1:]
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := backend.Reconstruct(flags, 0)
if err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkReconstructM(b *testing.B) {
for _, test := range decodeTests {
b.Run(test.String(), func(b *testing.B) {
backend, err := InitBackend(test.p)
if err != nil {
b.Fatal("cannot create backend", err)
}
defer backend.Close()
buf := bytes.Repeat([]byte("A"), test.size)
encoded, err := backend.EncodeMatrix(buf, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
defer encoded.Free()
flags := encoded.Data[1:]
b.ResetTimer()
for i := 0; i < b.N; i++ {
ddata, err := backend.ReconstructMatrix(flags, 0, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
ddata.Free()
}
})
}
}
func BenchmarkDecodeMSlow(b *testing.B) {
for _, test := range decodeTests {
b.Run(test.String(), func(b *testing.B) {
backend, err := InitBackend(test.p)
if err != nil {
b.Fatal("cannot create backend", err)
}
buf := bytes.Repeat([]byte("A"), test.size)
encoded, err := backend.EncodeMatrix(buf, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
defer encoded.Free()
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoded, err := backend.decodeMatrixSlow(encoded.Data, DefaultChunkSize)
if err != nil {
b.Fatal(err)
}
decoded.Free()
}
})
}
}
func BenchmarkMatrix(b *testing.B) {
for _, dtest := range decodeTests {
blockSize := 32768
// If you want 1 single block per fragment use:
// blockSize := (dtest.size + dtest.p.K - 1) / dtest.p.K
buf := bytes.Repeat([]byte("X"), dtest.size)
b.Run(fmt.Sprintf("%s:%d+%d,size=%d", dtest.p.Name, dtest.p.K, dtest.p.M, dtest.size),
func(b *testing.B) {
for _, crc := range []int{ /* ChecksumNone, */ ChecksumCrc32, ChecksumXxhash} {
b.Run(fmt.Sprintf("crc=%v", crc),
func(b *testing.B) {
dtest.p.Checksum = crc
backend, _ := InitBackend(dtest.p)
b.Run("Encode", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
encoded, err := backend.EncodeMatrix(buf, blockSize)
if err != nil {
b.Fatal(err)
}
encoded.Free()
}
})
b.Run("Decode", func(b *testing.B) {
encoded, _ := backend.EncodeMatrix(buf, blockSize)
defer encoded.Free()
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoded, err := backend.DecodeMatrix(encoded.Data, blockSize)
if err != nil {
b.Fatal(err)
}
decoded.Free()
}
})
b.Run("Reconstruct", func(b *testing.B) {
encoded, _ := backend.EncodeMatrix(buf, blockSize)
defer encoded.Free()
b.ResetTimer()
for i := 0; i < b.N; i++ {
decoded, err := backend.ReconstructMatrix(encoded.Data[1:], 0, blockSize)
if err != nil {
b.Fatal(err)
}
decoded.Free()
}
})
backend.Close()
})
}
})
}
}
func BenchmarkDecode(b *testing.B) {
backend, _ := InitBackend(Params{Name: "isa_l_rs_vand", K: 4, M: 2, W: 8, HD: 5})
buf := bytes.Repeat([]byte("A"), 1024*1024)
res, _ := backend.Encode(buf)
defer res.Free()
for i := 0; i < b.N; i++ {
decoded, err := backend.Decode(res.Data)
if err != nil {
b.Fatal(err)
}
decoded.Free()
}
backend.Close()
}
func TestEncodeM(t *testing.T) {
backend, err := InitBackend(Params{Name: "isa_l_rs_vand", K: 4, M: 2, W: 8, HD: 5})
if err != nil {
t.Fatalf("cannot init backend: (%v)", err)
}
buf := make([]byte, 1024*1024)
for i := 0; i < len(buf); i++ {
buf[i] = byte('A' + i%26)
}
testParams := []struct {
chunkUnit int
lenToDecode int
}{
{chunkUnit: 4096, lenToDecode: 4097},
{chunkUnit: 4097, lenToDecode: 4096},
{chunkUnit: 4096, lenToDecode: len(buf)},
{chunkUnit: 4096, lenToDecode: 4096},
}
for _, param := range testParams {
p := param
testName := fmt.Sprintf("TestEncodeB-%d-%d", p.chunkUnit, p.lenToDecode)
t.Run(testName, func(t *testing.T) {
// Do the matrix encoding
result, err := backend.EncodeMatrix(buf, p.chunkUnit)
if err != nil {
t.Errorf("failed to encode %+v", err)
}
// Do the matrix decoding. It should work fastly because we have
// all data fragments. After, we check that our linearized buffer
// contains expected data
ddata, err := backend.DecodeMatrix(result.Data, p.chunkUnit)
assert.NoError(t, err)
if ok := checkData(ddata.Data); ok == false {
t.Errorf("bad matrix decoding")
}
ddata.Free()
/* now do the same but with the slow path*/
/* we will run a matrix decoding but withtout some data part, to enforce repairing*/
var vect [][]byte
vect = append(vect, result.Data[2])
vect = append(vect, result.Data[3])
vect = append(vect, result.Data[4])
vect = append(vect, result.Data[5])
ddata2, _ := backend.decodeMatrixSlow(vect, p.chunkUnit)
if ok := checkData(ddata2.Data); ok == false {
t.Errorf("bad matrix repairing")
}
ddata2.Free()
/*
* now we will do the same but we should failed because the chunksize provided
* to decode the data is not the same used by encode function
*/
_, err = backend.decodeMatrixSlow(result.Data, p.chunkUnit+1)
if err == nil {
t.Errorf("no error during decoding whereas bad params were provided")
}
result.Free()
})
}
backend.Close()
}
// checkData reads a buffer and check that we have a round robin sequence of [A-Z] characters
func checkData(data []byte) bool {
for i := 0; i < len(data)-1; i++ {
if data[i] != 'Z' {
if data[i] != data[i+1]-1 {
return false
}
} else if data[i+1] != 'A' {
return false
}
}
return true
}
func TestMatrixBounds(t *testing.T) {
backend, err := InitBackend(Params{Name: "isa_l_rs_vand", K: 2, M: 1, W: 8, HD: 5})
if err != nil {
t.Fatalf("cannot init backend: (%v)", err)
}
testParams := []struct {
rangeStart int
rangeEnd int
chunkUnit int
expectedStart int
expectedEnd int
}{
{rangeStart: 1023999, rangeEnd: 1048575, chunkUnit: DefaultChunkSize, expectedStart: 492720, expectedEnd: 525568},
}
for _, param := range testParams {
p := param
testName := fmt.Sprintf("TestEMatrixBounds-%d-%d", p.rangeStart, p.rangeEnd)
t.Run(testName, func(t *testing.T) {
rmatrix := backend.GetRangeMatrix(p.rangeStart, p.rangeEnd, p.chunkUnit, DefaultFragSize)
if rmatrix.FragRangeStart != p.expectedStart || rmatrix.FragRangeEnd != p.expectedEnd {
t.Errorf("error : %+v but got %+v", p, rmatrix)
}
})
}
}
func TestReconstructM(t *testing.T) {
backend, err := InitBackend(Params{Name: "isa_l_rs_vand", K: 4, M: 2, W: 8, HD: 5})
if err != nil {
t.Fatalf("cannot init backend: (%v)", err)
}
buf := make([]byte, 1024*1024)
for i := 0; i < len(buf); i++ {
buf[i] = byte('A' + i%26)
}
// All our sub tests case. Each {X,Y} represents respectively the chunking unit (size of each subpart)
// and the fragment number we want to have to reconstruct
testParams := []struct {
chunkUnit int
fragNumber int
}{
{chunkUnit: 4096, fragNumber: 0},
{chunkUnit: 4096, fragNumber: backend.K},
{chunkUnit: DefaultChunkSize, fragNumber: 1},
}
for _, param := range testParams {
p := param
testName := fmt.Sprintf("TestReconstruct-%d-%d", p.chunkUnit, p.fragNumber)
t.Run(testName, func(t *testing.T) {
// Do the matrix encoding
result, err := backend.EncodeMatrix(buf, p.chunkUnit)
defer result.Free()
if err != nil {
t.Errorf("failed to encode %+v", err)
}
var vect [][]byte
for i := 0; i < backend.K+backend.M; i++ {
if i != p.fragNumber {
vect = append(vect, result.Data[i])
}
}
ddata, err := backend.ReconstructMatrix(vect, p.fragNumber, p.chunkUnit)
if err != nil {
t.Errorf("cannot reconstruct fragment %d cause=%v", p.fragNumber, err)
return
}
if ddata == nil {
t.Fatal("unexpected error / fragment rebuilt is nil")
}
res := bytes.Compare(ddata.Data, result.Data[p.fragNumber])
ddata.Free()
if res != 0 {
t.Errorf("Error, fragment rebuilt is different from the original one")
}
})
}
backend.Close()
}
func TestEncodeDecodeMatrix(t *testing.T) {
for _, params := range validParams {
if strings.Contains(params.Name, "jerasure_rs_cauchy") {
t.Logf("Skipping %s, not working with matrix", params.Name)
continue
}
if !BackendIsAvailable(params.Name) {
continue
}
backend, err := InitBackend(params)
if err != nil {
t.Errorf("Error creating backend %v: %q", params, err)
continue
}
for patternIndex, pattern := range testPatterns {
t.Run(fmt.Sprintf("%s_%d_%d-%d-%d",
params.Name, params.K, params.M,
patternIndex,
len(pattern)),
func(t *testing.T) {
data, err := backend.EncodeMatrix(pattern, 32768)
if err != nil {
t.Errorf("Error encoding %v: %q", params, err)
return
}
defer data.Free()
frags := data.Data
decode := func(frags [][]byte, description string) bool {