2019-05-15 08:02:00 +00:00
|
|
|
package erasure_coding
|
|
|
|
|
|
|
|
import (
|
2019-05-19 10:01:58 +00:00
|
|
|
"bytes"
|
2019-05-19 05:46:24 +00:00
|
|
|
"fmt"
|
2019-05-19 21:24:33 +00:00
|
|
|
"math/rand"
|
2019-05-15 08:02:00 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-10 05:29:02 +00:00
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2019-05-15 08:02:00 +00:00
|
|
|
)
|
|
|
|
|
2019-05-19 10:01:58 +00:00
|
|
|
const (
|
|
|
|
largeBlockSize = 10000
|
|
|
|
smallBlockSize = 100
|
|
|
|
)
|
|
|
|
|
2019-05-15 08:02:00 +00:00
|
|
|
func TestEncodingDecoding(t *testing.T) {
|
|
|
|
bufferSize := 50
|
2019-05-19 05:46:24 +00:00
|
|
|
baseFileName := "1"
|
2019-05-15 08:02:00 +00:00
|
|
|
|
2019-05-19 10:01:58 +00:00
|
|
|
err := generateEcFiles(baseFileName, bufferSize, largeBlockSize, smallBlockSize)
|
2019-05-15 08:02:00 +00:00
|
|
|
if err != nil {
|
2019-05-19 10:01:58 +00:00
|
|
|
t.Logf("generateEcFiles: %v", err)
|
2019-05-15 08:02:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 09:21:21 +00:00
|
|
|
err = WriteSortedFileFromIdx(baseFileName, ".ecx")
|
2019-05-15 08:02:00 +00:00
|
|
|
if err != nil {
|
2019-12-18 09:21:21 +00:00
|
|
|
t.Logf("WriteSortedFileFromIdx: %v", err)
|
2019-05-15 08:02:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:01:58 +00:00
|
|
|
err = validateFiles(baseFileName)
|
2019-05-19 05:46:24 +00:00
|
|
|
if err != nil {
|
2019-12-18 09:21:21 +00:00
|
|
|
t.Logf("WriteSortedFileFromIdx: %v", err)
|
2019-05-19 05:46:24 +00:00
|
|
|
}
|
2019-05-15 08:02:00 +00:00
|
|
|
|
2019-05-20 04:16:01 +00:00
|
|
|
removeGeneratedFiles(baseFileName)
|
|
|
|
|
2019-05-19 10:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 05:46:24 +00:00
|
|
|
func validateFiles(baseFileName string) error {
|
2020-02-05 05:16:34 +00:00
|
|
|
nm, err := readNeedleMap(baseFileName)
|
2020-03-10 05:29:02 +00:00
|
|
|
defer nm.Close()
|
2019-05-19 10:01:58 +00:00
|
|
|
if err != nil {
|
2020-02-05 05:16:34 +00:00
|
|
|
return fmt.Errorf("readNeedleMap: %v", err)
|
2019-05-19 10:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
datFile, err := os.OpenFile(baseFileName+".dat", os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open dat file: %v", err)
|
|
|
|
}
|
|
|
|
defer datFile.Close()
|
|
|
|
|
|
|
|
fi, err := datFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to stat dat file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ecFiles, err := openEcFiles(baseFileName, true)
|
2023-09-06 14:20:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error opening ec files: %w", err)
|
|
|
|
}
|
2019-05-19 10:01:58 +00:00
|
|
|
defer closeEcFiles(ecFiles)
|
|
|
|
|
2020-02-05 05:16:34 +00:00
|
|
|
err = nm.AscendingVisit(func(value needle_map.NeedleValue) error {
|
2019-05-19 10:01:58 +00:00
|
|
|
return assertSame(datFile, fi.Size(), ecFiles, value.Offset, value.Size)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check ec files: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func assertSame(datFile *os.File, datSize int64, ecFiles []*os.File, offset types.Offset, size types.Size) error {
|
2019-05-19 10:01:58 +00:00
|
|
|
|
|
|
|
data, err := readDatFile(datFile, offset, size)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read dat file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ecData, err := readEcFile(datSize, ecFiles, offset, size)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read ec file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Compare(data, ecData) != 0 {
|
|
|
|
return fmt.Errorf("unexpected data read")
|
|
|
|
}
|
|
|
|
|
2019-05-19 05:46:24 +00:00
|
|
|
return nil
|
2019-05-19 10:01:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func readDatFile(datFile *os.File, offset types.Offset, size types.Size) ([]byte, error) {
|
2019-05-19 10:01:58 +00:00
|
|
|
|
|
|
|
data := make([]byte, size)
|
2021-02-07 04:11:51 +00:00
|
|
|
n, err := datFile.ReadAt(data, offset.ToActualOffset())
|
2019-05-19 10:01:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to ReadAt dat file: %v", err)
|
|
|
|
}
|
|
|
|
if n != int(size) {
|
|
|
|
return nil, fmt.Errorf("unexpected read size %d, expected %d", n, size)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func readEcFile(datSize int64, ecFiles []*os.File, offset types.Offset, size types.Size) (data []byte, err error) {
|
2019-05-19 10:01:58 +00:00
|
|
|
|
2021-02-07 04:11:51 +00:00
|
|
|
intervals := LocateData(largeBlockSize, smallBlockSize, datSize, offset.ToActualOffset(), size)
|
2019-05-19 21:24:33 +00:00
|
|
|
|
|
|
|
for i, interval := range intervals {
|
2019-05-27 18:59:03 +00:00
|
|
|
if d, e := readOneInterval(interval, ecFiles); e != nil {
|
2019-05-19 21:24:33 +00:00
|
|
|
return nil, e
|
|
|
|
} else {
|
|
|
|
if i == 0 {
|
|
|
|
data = d
|
|
|
|
} else {
|
|
|
|
data = append(data, d...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2019-05-27 18:59:03 +00:00
|
|
|
func readOneInterval(interval Interval, ecFiles []*os.File) (data []byte, err error) {
|
2019-05-19 21:24:33 +00:00
|
|
|
|
2019-05-29 06:48:39 +00:00
|
|
|
ecFileIndex, ecFileOffset := interval.ToShardIdAndOffset(largeBlockSize, smallBlockSize)
|
2019-05-19 21:24:33 +00:00
|
|
|
|
2019-05-27 08:29:46 +00:00
|
|
|
data = make([]byte, interval.Size)
|
2019-05-19 21:24:33 +00:00
|
|
|
err = readFromFile(ecFiles[ecFileIndex], data, ecFileOffset)
|
|
|
|
{ // do some ec testing
|
2019-05-27 18:59:03 +00:00
|
|
|
ecData, err := readFromOtherEcFiles(ecFiles, int(ecFileIndex), ecFileOffset, interval.Size)
|
2019-05-19 21:24:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ec reconstruct error: %v", err)
|
|
|
|
}
|
|
|
|
if bytes.Compare(data, ecData) != 0 {
|
|
|
|
return nil, fmt.Errorf("ec compare error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func readFromOtherEcFiles(ecFiles []*os.File, ecFileIndex int, ecFileOffset int64, size types.Size) (data []byte, err error) {
|
2019-05-19 21:24:33 +00:00
|
|
|
enc, err := reedsolomon.New(DataShardsCount, ParityShardsCount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create encoder: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-25 09:02:44 +00:00
|
|
|
bufs := make([][]byte, TotalShardsCount)
|
2019-05-19 21:24:33 +00:00
|
|
|
for i := 0; i < DataShardsCount; {
|
2019-05-25 09:02:44 +00:00
|
|
|
n := int(rand.Int31n(TotalShardsCount))
|
2019-05-19 21:24:33 +00:00
|
|
|
if n == ecFileIndex || bufs[n] != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bufs[n] = make([]byte, size)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, buf := range bufs {
|
|
|
|
if buf == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = readFromFile(ecFiles[i], buf, ecFileOffset)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = enc.ReconstructData(bufs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufs[ecFileIndex], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFromFile(file *os.File, data []byte, ecFileOffset int64) (err error) {
|
|
|
|
_, err = file.ReadAt(data, ecFileOffset)
|
|
|
|
return
|
2019-05-19 10:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 04:16:01 +00:00
|
|
|
func removeGeneratedFiles(baseFileName string) {
|
|
|
|
for i := 0; i < DataShardsCount+ParityShardsCount; i++ {
|
2019-05-29 07:34:30 +00:00
|
|
|
fname := fmt.Sprintf("%s.ec%02d", baseFileName, i)
|
2019-05-20 04:16:01 +00:00
|
|
|
os.Remove(fname)
|
|
|
|
}
|
2019-05-20 07:53:17 +00:00
|
|
|
os.Remove(baseFileName + ".ecx")
|
2019-05-20 04:16:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:01:58 +00:00
|
|
|
func TestLocateData(t *testing.T) {
|
2019-05-27 08:29:46 +00:00
|
|
|
intervals := LocateData(largeBlockSize, smallBlockSize, DataShardsCount*largeBlockSize+1, DataShardsCount*largeBlockSize, 1)
|
2019-05-19 10:01:58 +00:00
|
|
|
if len(intervals) != 1 {
|
|
|
|
t.Errorf("unexpected interval size %d", len(intervals))
|
|
|
|
}
|
2019-05-27 18:59:03 +00:00
|
|
|
if !intervals[0].sameAs(Interval{0, 0, 1, false, 1}) {
|
2019-05-19 10:01:58 +00:00
|
|
|
t.Errorf("unexpected interval %+v", intervals[0])
|
|
|
|
}
|
|
|
|
|
2019-05-27 08:29:46 +00:00
|
|
|
intervals = LocateData(largeBlockSize, smallBlockSize, DataShardsCount*largeBlockSize+1, DataShardsCount*largeBlockSize/2+100, DataShardsCount*largeBlockSize+1-DataShardsCount*largeBlockSize/2-100)
|
2019-05-19 10:01:58 +00:00
|
|
|
fmt.Printf("%+v\n", intervals)
|
|
|
|
}
|
2019-05-19 05:46:24 +00:00
|
|
|
|
2019-05-19 10:01:58 +00:00
|
|
|
func (this Interval) sameAs(that Interval) bool {
|
2019-05-27 08:29:46 +00:00
|
|
|
return this.IsLargeBlock == that.IsLargeBlock &&
|
|
|
|
this.InnerBlockOffset == that.InnerBlockOffset &&
|
|
|
|
this.BlockIndex == that.BlockIndex &&
|
|
|
|
this.Size == that.Size
|
2019-05-19 05:46:24 +00:00
|
|
|
}
|