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.

203 lines
5.5 KiB

  1. package erasure_coding
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  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. DataShardsCount = 10
  14. ParityShardsCount = 4
  15. TotalShardsCount = DataShardsCount + ParityShardsCount
  16. ErasureCodingLargeBlockSize = 1024 * 1024 * 1024 // 1GB
  17. ErasureCodingSmallBlockSize = 1024 * 1024 // 1MB
  18. )
  19. // WriteSortedEcxFile generates .ecx file from existing .idx file
  20. // all keys are sorted in ascending order
  21. func WriteSortedEcxFile(baseFileName string) (e error) {
  22. cm, err := readCompactMap(baseFileName)
  23. if err != nil {
  24. return fmt.Errorf("readCompactMap: %v", err)
  25. }
  26. ecxFile, err := os.OpenFile(baseFileName+".ecx", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
  27. if err != nil {
  28. return fmt.Errorf("failed to open dat file: %v", err)
  29. }
  30. defer ecxFile.Close()
  31. err = cm.AscendingVisit(func(value needle_map.NeedleValue) error {
  32. bytes := value.ToBytes()
  33. _, writeErr := ecxFile.Write(bytes)
  34. return writeErr
  35. })
  36. if err != nil {
  37. return fmt.Errorf("failed to open dat file: %v", err)
  38. }
  39. return nil
  40. }
  41. // WriteEcFiles generates .ec01 ~ .ec14 files
  42. func WriteEcFiles(baseFileName string) error {
  43. return generateEcFiles(baseFileName, 256*1024, ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize)
  44. }
  45. func ToExt(ecIndex int) string {
  46. return fmt.Sprintf(".ec%02d", ecIndex)
  47. }
  48. func generateEcFiles(baseFileName string, bufferSize int, largeBlockSize int64, smallBlockSize int64) error {
  49. file, err := os.OpenFile(baseFileName+".dat", os.O_RDONLY, 0)
  50. if err != nil {
  51. return fmt.Errorf("failed to open dat file: %v", err)
  52. }
  53. defer file.Close()
  54. fi, err := file.Stat()
  55. if err != nil {
  56. return fmt.Errorf("failed to stat dat file: %v", err)
  57. }
  58. err = encodeDatFile(fi.Size(), err, baseFileName, bufferSize, largeBlockSize, file, smallBlockSize)
  59. if err != nil {
  60. return fmt.Errorf("encodeDatFile: %v", err)
  61. }
  62. return nil
  63. }
  64. func encodeData(file *os.File, enc reedsolomon.Encoder, startOffset, blockSize int64, buffers [][]byte, outputs []*os.File) error {
  65. bufferSize := int64(len(buffers[0]))
  66. batchCount := blockSize / bufferSize
  67. if blockSize%bufferSize != 0 {
  68. glog.Fatalf("unexpected block size %d buffer size %d", blockSize, bufferSize)
  69. }
  70. for b := int64(0); b < batchCount; b++ {
  71. err := encodeDataOneBatch(file, enc, startOffset+b*bufferSize, blockSize, buffers, outputs)
  72. if err != nil {
  73. return err
  74. }
  75. }
  76. return nil
  77. }
  78. func openEcFiles(baseFileName string, forRead bool) (files []*os.File, err error) {
  79. for i := 0; i < TotalShardsCount; i++ {
  80. fname := baseFileName + ToExt(i)
  81. openOption := os.O_TRUNC | os.O_CREATE | os.O_WRONLY
  82. if forRead {
  83. openOption = os.O_RDONLY
  84. }
  85. f, err := os.OpenFile(fname, openOption, 0644)
  86. if err != nil {
  87. return files, fmt.Errorf("failed to open file %s: %v", fname, err)
  88. }
  89. files = append(files, f)
  90. }
  91. return
  92. }
  93. func closeEcFiles(files []*os.File) {
  94. for _, f := range files {
  95. if f != nil {
  96. f.Close()
  97. }
  98. }
  99. }
  100. func encodeDataOneBatch(file *os.File, enc reedsolomon.Encoder, startOffset, blockSize int64, buffers [][]byte, outputs []*os.File) error {
  101. // read data into buffers
  102. for i := 0; i < DataShardsCount; i++ {
  103. n, err := file.ReadAt(buffers[i], startOffset+blockSize*int64(i))
  104. if err != nil {
  105. if err != io.EOF {
  106. return err
  107. }
  108. }
  109. if n < len(buffers[i]) {
  110. for t := len(buffers[i]) - 1; t >= n; t-- {
  111. buffers[i][t] = 0
  112. }
  113. }
  114. }
  115. err := enc.Encode(buffers)
  116. if err != nil {
  117. return err
  118. }
  119. for i := 0; i < TotalShardsCount; i++ {
  120. _, err := outputs[i].Write(buffers[i])
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }
  127. func encodeDatFile(remainingSize int64, err error, baseFileName string, bufferSize int, largeBlockSize int64, file *os.File, smallBlockSize int64) error {
  128. var processedSize int64
  129. enc, err := reedsolomon.New(DataShardsCount, ParityShardsCount)
  130. if err != nil {
  131. return fmt.Errorf("failed to create encoder: %v", err)
  132. }
  133. buffers := make([][]byte, TotalShardsCount)
  134. outputs, err := openEcFiles(baseFileName, false)
  135. defer closeEcFiles(outputs)
  136. if err != nil {
  137. return fmt.Errorf("failed to open dat file: %v", err)
  138. }
  139. for i, _ := range buffers {
  140. buffers[i] = make([]byte, bufferSize)
  141. }
  142. for remainingSize > largeBlockSize*DataShardsCount {
  143. err = encodeData(file, enc, processedSize, largeBlockSize, buffers, outputs)
  144. if err != nil {
  145. return fmt.Errorf("failed to encode large chunk data: %v", err)
  146. }
  147. remainingSize -= largeBlockSize * DataShardsCount
  148. processedSize += largeBlockSize * DataShardsCount
  149. }
  150. for remainingSize > 0 {
  151. encodeData(file, enc, processedSize, smallBlockSize, buffers, outputs)
  152. if err != nil {
  153. return fmt.Errorf("failed to encode small chunk data: %v", err)
  154. }
  155. remainingSize -= smallBlockSize * DataShardsCount
  156. processedSize += smallBlockSize * DataShardsCount
  157. }
  158. return nil
  159. }
  160. func readCompactMap(baseFileName string) (*needle_map.CompactMap, error) {
  161. indexFile, err := os.OpenFile(baseFileName+".idx", os.O_RDONLY, 0644)
  162. if err != nil {
  163. return nil, fmt.Errorf("cannot read Volume Index %s.idx: %v", baseFileName, err)
  164. }
  165. defer indexFile.Close()
  166. cm := needle_map.NewCompactMap()
  167. err = idx.WalkIndexFile(indexFile, func(key types.NeedleId, offset types.Offset, size uint32) error {
  168. if !offset.IsZero() && size != types.TombstoneFileSize {
  169. cm.Set(key, offset, size)
  170. } else {
  171. cm.Delete(key)
  172. }
  173. return nil
  174. })
  175. return cm, err
  176. }