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.

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