You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.2 KiB

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