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.

164 lines
4.3 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
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) 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 false
  63. }
  64. ev.Shards = append(ev.Shards[:foundPosition], ev.Shards[foundPosition+1:]...)
  65. return true
  66. }
  67. func (ev *EcVolume) FindEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, found bool) {
  68. for _, s := range ev.Shards {
  69. if s.ShardId == shardId {
  70. return s, true
  71. }
  72. }
  73. return nil, false
  74. }
  75. func (ev *EcVolume) Close() {
  76. for _, s := range ev.Shards {
  77. s.Close()
  78. }
  79. if ev.ecxFile != nil {
  80. _ = ev.ecxFile.Close()
  81. ev.ecxFile = nil
  82. }
  83. }
  84. func (ev *EcVolume) Destroy() {
  85. ev.Close()
  86. baseFileName := EcShardFileName(ev.Collection, ev.dir, int(ev.VolumeId))
  87. for _, s := range ev.Shards {
  88. s.Destroy()
  89. }
  90. os.Remove(baseFileName + ".ecx")
  91. }
  92. func (ev *EcVolume) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  93. prevVolumeId := needle.VolumeId(math.MaxUint32)
  94. var m *master_pb.VolumeEcShardInformationMessage
  95. for _, s := range ev.Shards {
  96. if s.VolumeId != prevVolumeId {
  97. m = &master_pb.VolumeEcShardInformationMessage{
  98. Id: uint32(s.VolumeId),
  99. Collection: s.Collection,
  100. }
  101. messages = append(messages, m)
  102. }
  103. prevVolumeId = s.VolumeId
  104. m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
  105. }
  106. return
  107. }
  108. func (ev *EcVolume) LocateEcShardNeedle(n *needle.Needle, version needle.Version) (offset types.Offset, size uint32, intervals []Interval, err error) {
  109. // find the needle from ecx file
  110. offset, size, err = ev.findNeedleFromEcx(n.Id)
  111. if err != nil {
  112. return types.Offset{}, 0, nil, err
  113. }
  114. shard := ev.Shards[0]
  115. // calculate the locations in the ec shards
  116. intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset.ToAcutalOffset(), uint32(needle.GetActualSize(size, version)))
  117. return
  118. }
  119. func (ev *EcVolume) findNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size uint32, err error) {
  120. var key types.NeedleId
  121. buf := make([]byte, types.NeedleMapEntrySize)
  122. l, h := int64(0), ev.ecxFileSize/types.NeedleMapEntrySize
  123. for l < h {
  124. m := (l + h) / 2
  125. if _, err := ev.ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  126. return types.Offset{}, 0, err
  127. }
  128. key, offset, size = idx.IdxFileEntry(buf)
  129. if key == needleId {
  130. return
  131. }
  132. if key < needleId {
  133. l = m + 1
  134. } else {
  135. h = m
  136. }
  137. }
  138. err = fmt.Errorf("needle id %d not found", needleId)
  139. return
  140. }