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.

217 lines
5.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package erasure_coding
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "os"
  7. "sort"
  8. "sync"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "github.com/chrislusf/seaweedfs/weed/storage/types"
  14. )
  15. var (
  16. NotFoundError = errors.New("needle not found")
  17. )
  18. type EcVolume struct {
  19. VolumeId needle.VolumeId
  20. Collection string
  21. dir string
  22. ecxFile *os.File
  23. ecxFileSize int64
  24. ecxCreatedAt time.Time
  25. Shards []*EcVolumeShard
  26. ShardLocations map[ShardId][]string
  27. ShardLocationsRefreshTime time.Time
  28. ShardLocationsLock sync.RWMutex
  29. Version needle.Version
  30. ecjFile *os.File
  31. ecjFileAccessLock sync.Mutex
  32. }
  33. func NewEcVolume(dir string, collection string, vid needle.VolumeId) (ev *EcVolume, err error) {
  34. ev = &EcVolume{dir: dir, Collection: collection, VolumeId: vid}
  35. baseFileName := EcShardFileName(collection, dir, int(vid))
  36. // open ecx file
  37. if ev.ecxFile, err = os.OpenFile(baseFileName+".ecx", os.O_RDWR, 0644); err != nil {
  38. return nil, fmt.Errorf("cannot open ec volume index %s.ecx: %v", baseFileName, err)
  39. }
  40. ecxFi, statErr := ev.ecxFile.Stat()
  41. if statErr != nil {
  42. return nil, fmt.Errorf("can not stat ec volume index %s.ecx: %v", baseFileName, statErr)
  43. }
  44. ev.ecxFileSize = ecxFi.Size()
  45. ev.ecxCreatedAt = ecxFi.ModTime()
  46. // open ecj file
  47. if ev.ecjFile, err = os.OpenFile(baseFileName+".ecj", os.O_RDWR|os.O_CREATE, 0644); err != nil {
  48. return nil, fmt.Errorf("cannot open ec volume journal %s.ecj: %v", baseFileName, err)
  49. }
  50. ev.ShardLocations = make(map[ShardId][]string)
  51. return
  52. }
  53. func (ev *EcVolume) AddEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
  54. for _, s := range ev.Shards {
  55. if s.ShardId == ecVolumeShard.ShardId {
  56. return false
  57. }
  58. }
  59. ev.Shards = append(ev.Shards, ecVolumeShard)
  60. sort.Slice(ev.Shards, func(i, j int) bool {
  61. return ev.Shards[i].VolumeId < ev.Shards[j].VolumeId ||
  62. ev.Shards[i].VolumeId == ev.Shards[j].VolumeId && ev.Shards[i].ShardId < ev.Shards[j].ShardId
  63. })
  64. return true
  65. }
  66. func (ev *EcVolume) DeleteEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, deleted bool) {
  67. foundPosition := -1
  68. for i, s := range ev.Shards {
  69. if s.ShardId == shardId {
  70. foundPosition = i
  71. }
  72. }
  73. if foundPosition < 0 {
  74. return nil, false
  75. }
  76. ecVolumeShard = ev.Shards[foundPosition]
  77. ev.Shards = append(ev.Shards[:foundPosition], ev.Shards[foundPosition+1:]...)
  78. return ecVolumeShard, true
  79. }
  80. func (ev *EcVolume) FindEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, found bool) {
  81. for _, s := range ev.Shards {
  82. if s.ShardId == shardId {
  83. return s, true
  84. }
  85. }
  86. return nil, false
  87. }
  88. func (ev *EcVolume) Close() {
  89. for _, s := range ev.Shards {
  90. s.Close()
  91. }
  92. if ev.ecjFile != nil {
  93. ev.ecjFileAccessLock.Lock()
  94. _ = ev.ecjFile.Close()
  95. ev.ecjFile = nil
  96. ev.ecjFileAccessLock.Unlock()
  97. }
  98. if ev.ecxFile != nil {
  99. _ = ev.ecxFile.Close()
  100. ev.ecxFile = nil
  101. }
  102. }
  103. func (ev *EcVolume) Destroy() {
  104. ev.Close()
  105. for _, s := range ev.Shards {
  106. s.Destroy()
  107. }
  108. os.Remove(ev.FileName() + ".ecx")
  109. os.Remove(ev.FileName() + ".ecj")
  110. }
  111. func (ev *EcVolume) FileName() string {
  112. return EcShardFileName(ev.Collection, ev.dir, int(ev.VolumeId))
  113. }
  114. func (ev *EcVolume) ShardSize() int64 {
  115. if len(ev.Shards) > 0 {
  116. return ev.Shards[0].Size()
  117. }
  118. return 0
  119. }
  120. func (ev *EcVolume) CreatedAt() time.Time {
  121. return ev.ecxCreatedAt
  122. }
  123. func (ev *EcVolume) ShardIdList() (shardIds []ShardId) {
  124. for _, s := range ev.Shards {
  125. shardIds = append(shardIds, s.ShardId)
  126. }
  127. return
  128. }
  129. func (ev *EcVolume) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  130. prevVolumeId := needle.VolumeId(math.MaxUint32)
  131. var m *master_pb.VolumeEcShardInformationMessage
  132. for _, s := range ev.Shards {
  133. if s.VolumeId != prevVolumeId {
  134. m = &master_pb.VolumeEcShardInformationMessage{
  135. Id: uint32(s.VolumeId),
  136. Collection: s.Collection,
  137. }
  138. messages = append(messages, m)
  139. }
  140. prevVolumeId = s.VolumeId
  141. m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
  142. }
  143. return
  144. }
  145. func (ev *EcVolume) LocateEcShardNeedle(needleId types.NeedleId, version needle.Version) (offset types.Offset, size uint32, intervals []Interval, err error) {
  146. // find the needle from ecx file
  147. offset, size, err = ev.FindNeedleFromEcx(needleId)
  148. if err != nil {
  149. return types.Offset{}, 0, nil, fmt.Errorf("FindNeedleFromEcx: %v", err)
  150. }
  151. shard := ev.Shards[0]
  152. // calculate the locations in the ec shards
  153. intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset.ToAcutalOffset(), uint32(needle.GetActualSize(size, version)))
  154. return
  155. }
  156. func (ev *EcVolume) FindNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size uint32, err error) {
  157. return searchNeedleFromEcx(ev.ecxFile, ev.ecxFileSize, needleId, nil)
  158. }
  159. func searchNeedleFromEcx(ecxFile *os.File, ecxFileSize int64, needleId types.NeedleId, processNeedleFn func(file *os.File, offset int64) error) (offset types.Offset, size uint32, err error) {
  160. var key types.NeedleId
  161. buf := make([]byte, types.NeedleMapEntrySize)
  162. l, h := int64(0), ecxFileSize/types.NeedleMapEntrySize
  163. for l < h {
  164. m := (l + h) / 2
  165. if _, err := ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  166. return types.Offset{}, types.TombstoneFileSize, fmt.Errorf("ecx file %d read at %d: %v", ecxFileSize, m*types.NeedleMapEntrySize, err)
  167. }
  168. key, offset, size = idx.IdxFileEntry(buf)
  169. if key == needleId {
  170. if processNeedleFn != nil {
  171. err = processNeedleFn(ecxFile, m*types.NeedleHeaderSize)
  172. }
  173. return
  174. }
  175. if key < needleId {
  176. l = m + 1
  177. } else {
  178. h = m
  179. }
  180. }
  181. err = NotFoundError
  182. return
  183. }