ec_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package erasure_coding
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "testing"
  8. "github.com/klauspost/reedsolomon"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  11. )
  12. const (
  13. largeBlockSize = 10000
  14. smallBlockSize = 100
  15. )
  16. func TestEncodingDecoding(t *testing.T) {
  17. bufferSize := 50
  18. baseFileName := "1"
  19. err := generateEcFiles(baseFileName, bufferSize, largeBlockSize, smallBlockSize)
  20. if err != nil {
  21. t.Logf("generateEcFiles: %v", err)
  22. }
  23. err = WriteSortedFileFromIdx(baseFileName, ".ecx")
  24. if err != nil {
  25. t.Logf("WriteSortedFileFromIdx: %v", err)
  26. }
  27. err = validateFiles(baseFileName)
  28. if err != nil {
  29. t.Logf("WriteSortedFileFromIdx: %v", err)
  30. }
  31. removeGeneratedFiles(baseFileName)
  32. }
  33. func validateFiles(baseFileName string) error {
  34. nm, err := readNeedleMap(baseFileName)
  35. if err != nil {
  36. return fmt.Errorf("readNeedleMap: %v", err)
  37. }
  38. defer nm.Close()
  39. datFile, err := os.OpenFile(baseFileName+".dat", os.O_RDONLY, 0)
  40. if err != nil {
  41. return fmt.Errorf("failed to open dat file: %v", err)
  42. }
  43. defer datFile.Close()
  44. fi, err := datFile.Stat()
  45. if err != nil {
  46. return fmt.Errorf("failed to stat dat file: %v", err)
  47. }
  48. ecFiles, err := openEcFiles(baseFileName, true)
  49. if err != nil {
  50. return fmt.Errorf("error opening ec files: %w", err)
  51. }
  52. defer closeEcFiles(ecFiles)
  53. err = nm.AscendingVisit(func(value needle_map.NeedleValue) error {
  54. return assertSame(datFile, fi.Size(), ecFiles, value.Offset, value.Size)
  55. })
  56. if err != nil {
  57. return fmt.Errorf("failed to check ec files: %v", err)
  58. }
  59. return nil
  60. }
  61. func assertSame(datFile *os.File, datSize int64, ecFiles []*os.File, offset types.Offset, size types.Size) error {
  62. data, err := readDatFile(datFile, offset, size)
  63. if err != nil {
  64. return fmt.Errorf("failed to read dat file: %v", err)
  65. }
  66. ecData, err := readEcFile(datSize, ecFiles, offset, size)
  67. if err != nil {
  68. return fmt.Errorf("failed to read ec file: %v", err)
  69. }
  70. if bytes.Compare(data, ecData) != 0 {
  71. return fmt.Errorf("unexpected data read")
  72. }
  73. return nil
  74. }
  75. func readDatFile(datFile *os.File, offset types.Offset, size types.Size) ([]byte, error) {
  76. data := make([]byte, size)
  77. n, err := datFile.ReadAt(data, offset.ToActualOffset())
  78. if err != nil {
  79. return nil, fmt.Errorf("failed to ReadAt dat file: %v", err)
  80. }
  81. if n != int(size) {
  82. return nil, fmt.Errorf("unexpected read size %d, expected %d", n, size)
  83. }
  84. return data, nil
  85. }
  86. func readEcFile(datSize int64, ecFiles []*os.File, offset types.Offset, size types.Size) (data []byte, err error) {
  87. intervals := LocateData(largeBlockSize, smallBlockSize, datSize, offset.ToActualOffset(), size)
  88. for i, interval := range intervals {
  89. if d, e := readOneInterval(interval, ecFiles); e != nil {
  90. return nil, e
  91. } else {
  92. if i == 0 {
  93. data = d
  94. } else {
  95. data = append(data, d...)
  96. }
  97. }
  98. }
  99. return data, nil
  100. }
  101. func readOneInterval(interval Interval, ecFiles []*os.File) (data []byte, err error) {
  102. ecFileIndex, ecFileOffset := interval.ToShardIdAndOffset(largeBlockSize, smallBlockSize)
  103. data = make([]byte, interval.Size)
  104. err = readFromFile(ecFiles[ecFileIndex], data, ecFileOffset)
  105. { // do some ec testing
  106. ecData, err := readFromOtherEcFiles(ecFiles, int(ecFileIndex), ecFileOffset, interval.Size)
  107. if err != nil {
  108. return nil, fmt.Errorf("ec reconstruct error: %v", err)
  109. }
  110. if bytes.Compare(data, ecData) != 0 {
  111. return nil, fmt.Errorf("ec compare error")
  112. }
  113. }
  114. return
  115. }
  116. func readFromOtherEcFiles(ecFiles []*os.File, ecFileIndex int, ecFileOffset int64, size types.Size) (data []byte, err error) {
  117. enc, err := reedsolomon.New(DataShardsCount, ParityShardsCount)
  118. if err != nil {
  119. return nil, fmt.Errorf("failed to create encoder: %v", err)
  120. }
  121. bufs := make([][]byte, TotalShardsCount)
  122. for i := 0; i < DataShardsCount; {
  123. n := int(rand.Int31n(TotalShardsCount))
  124. if n == ecFileIndex || bufs[n] != nil {
  125. continue
  126. }
  127. bufs[n] = make([]byte, size)
  128. i++
  129. }
  130. for i, buf := range bufs {
  131. if buf == nil {
  132. continue
  133. }
  134. err = readFromFile(ecFiles[i], buf, ecFileOffset)
  135. if err != nil {
  136. return
  137. }
  138. }
  139. if err = enc.ReconstructData(bufs); err != nil {
  140. return nil, err
  141. }
  142. return bufs[ecFileIndex], nil
  143. }
  144. func readFromFile(file *os.File, data []byte, ecFileOffset int64) (err error) {
  145. _, err = file.ReadAt(data, ecFileOffset)
  146. return
  147. }
  148. func removeGeneratedFiles(baseFileName string) {
  149. for i := 0; i < DataShardsCount+ParityShardsCount; i++ {
  150. fname := fmt.Sprintf("%s.ec%02d", baseFileName, i)
  151. os.Remove(fname)
  152. }
  153. os.Remove(baseFileName + ".ecx")
  154. }
  155. func TestLocateData(t *testing.T) {
  156. intervals := LocateData(largeBlockSize, smallBlockSize, DataShardsCount*largeBlockSize+1, DataShardsCount*largeBlockSize, 1)
  157. if len(intervals) != 1 {
  158. t.Errorf("unexpected interval size %d", len(intervals))
  159. }
  160. if !intervals[0].sameAs(Interval{0, 0, 1, false, 1}) {
  161. t.Errorf("unexpected interval %+v", intervals[0])
  162. }
  163. intervals = LocateData(largeBlockSize, smallBlockSize, DataShardsCount*largeBlockSize+1, DataShardsCount*largeBlockSize/2+100, DataShardsCount*largeBlockSize+1-DataShardsCount*largeBlockSize/2-100)
  164. fmt.Printf("%+v\n", intervals)
  165. }
  166. func (this Interval) sameAs(that Interval) bool {
  167. return this.IsLargeBlock == that.IsLargeBlock &&
  168. this.InnerBlockOffset == that.InnerBlockOffset &&
  169. this.BlockIndex == that.BlockIndex &&
  170. this.Size == that.Size
  171. }