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.

171 lines
4.4 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. "fmt"
  4. "math"
  5. "os"
  6. "sort"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/storage/types"
  13. )
  14. type EcVolume struct {
  15. VolumeId needle.VolumeId
  16. Collection string
  17. dir string
  18. ecxFile *os.File
  19. ecxFileSize int64
  20. Shards []*EcVolumeShard
  21. ShardLocations map[ShardId][]string
  22. ShardLocationsRefreshTime time.Time
  23. ShardLocationsLock sync.RWMutex
  24. Version needle.Version
  25. }
  26. func NewEcVolume(dir string, collection string, vid needle.VolumeId) (ev *EcVolume, err error) {
  27. ev = &EcVolume{dir: dir, Collection: collection, VolumeId: vid}
  28. baseFileName := EcShardFileName(collection, dir, int(vid))
  29. // open ecx file
  30. if ev.ecxFile, err = os.OpenFile(baseFileName+".ecx", os.O_RDONLY, 0644); err != nil {
  31. return nil, fmt.Errorf("cannot read ec volume index %s.ecx: %v", baseFileName, err)
  32. }
  33. ecxFi, statErr := ev.ecxFile.Stat()
  34. if statErr != nil {
  35. return nil, fmt.Errorf("can not stat ec volume index %s.ecx: %v", baseFileName, statErr)
  36. }
  37. ev.ecxFileSize = ecxFi.Size()
  38. ev.ShardLocations = make(map[ShardId][]string)
  39. return
  40. }
  41. func (ev *EcVolume) AddEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
  42. for _, s := range ev.Shards {
  43. if s.ShardId == ecVolumeShard.ShardId {
  44. return false
  45. }
  46. }
  47. ev.Shards = append(ev.Shards, ecVolumeShard)
  48. sort.Slice(ev.Shards, func(i, j int) bool {
  49. return ev.Shards[i].VolumeId < ev.Shards[j].VolumeId ||
  50. ev.Shards[i].VolumeId == ev.Shards[j].VolumeId && ev.Shards[i].ShardId < ev.Shards[j].ShardId
  51. })
  52. return true
  53. }
  54. func (ev *EcVolume) DeleteEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, deleted bool) {
  55. foundPosition := -1
  56. for i, s := range ev.Shards {
  57. if s.ShardId == shardId {
  58. foundPosition = i
  59. }
  60. }
  61. if foundPosition < 0 {
  62. return nil, false
  63. }
  64. ecVolumeShard = ev.Shards[foundPosition]
  65. ev.Shards = append(ev.Shards[:foundPosition], ev.Shards[foundPosition+1:]...)
  66. return ecVolumeShard, true
  67. }
  68. func (ev *EcVolume) FindEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, found bool) {
  69. for _, s := range ev.Shards {
  70. if s.ShardId == shardId {
  71. return s, true
  72. }
  73. }
  74. return nil, false
  75. }
  76. func (ev *EcVolume) Close() {
  77. for _, s := range ev.Shards {
  78. s.Close()
  79. }
  80. if ev.ecxFile != nil {
  81. _ = ev.ecxFile.Close()
  82. ev.ecxFile = nil
  83. }
  84. }
  85. func (ev *EcVolume) Destroy() {
  86. ev.Close()
  87. for _, s := range ev.Shards {
  88. s.Destroy()
  89. }
  90. os.Remove(ev.FileName() + ".ecx")
  91. }
  92. func (ev *EcVolume) FileName() string {
  93. return EcShardFileName(ev.Collection, ev.dir, int(ev.VolumeId))
  94. }
  95. func (ev *EcVolume) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  96. prevVolumeId := needle.VolumeId(math.MaxUint32)
  97. var m *master_pb.VolumeEcShardInformationMessage
  98. for _, s := range ev.Shards {
  99. if s.VolumeId != prevVolumeId {
  100. m = &master_pb.VolumeEcShardInformationMessage{
  101. Id: uint32(s.VolumeId),
  102. Collection: s.Collection,
  103. }
  104. messages = append(messages, m)
  105. }
  106. prevVolumeId = s.VolumeId
  107. m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
  108. }
  109. return
  110. }
  111. func (ev *EcVolume) LocateEcShardNeedle(n *needle.Needle, version needle.Version) (offset types.Offset, size uint32, intervals []Interval, err error) {
  112. // find the needle from ecx file
  113. offset, size, err = ev.findNeedleFromEcx(n.Id)
  114. if err != nil {
  115. return types.Offset{}, 0, nil, err
  116. }
  117. shard := ev.Shards[0]
  118. // calculate the locations in the ec shards
  119. intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset.ToAcutalOffset(), uint32(needle.GetActualSize(size, version)))
  120. return
  121. }
  122. func (ev *EcVolume) findNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size uint32, err error) {
  123. var key types.NeedleId
  124. buf := make([]byte, types.NeedleMapEntrySize)
  125. l, h := int64(0), ev.ecxFileSize/types.NeedleMapEntrySize
  126. for l < h {
  127. m := (l + h) / 2
  128. if _, err := ev.ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  129. return types.Offset{}, 0, err
  130. }
  131. key, offset, size = idx.IdxFileEntry(buf)
  132. if key == needleId {
  133. return
  134. }
  135. if key < needleId {
  136. l = m + 1
  137. } else {
  138. h = m
  139. }
  140. }
  141. err = fmt.Errorf("needle id %d not found", needleId)
  142. return
  143. }