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.

152 lines
4.0 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) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  84. prevVolumeId := needle.VolumeId(math.MaxUint32)
  85. var m *master_pb.VolumeEcShardInformationMessage
  86. for _, s := range ev.Shards {
  87. if s.VolumeId != prevVolumeId {
  88. m = &master_pb.VolumeEcShardInformationMessage{
  89. Id: uint32(s.VolumeId),
  90. Collection: s.Collection,
  91. }
  92. messages = append(messages, m)
  93. }
  94. prevVolumeId = s.VolumeId
  95. m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
  96. }
  97. return
  98. }
  99. func (ev *EcVolume) LocateEcShardNeedle(n *needle.Needle, version needle.Version) (offset types.Offset, size uint32, intervals []Interval, err error) {
  100. // find the needle from ecx file
  101. offset, size, err = ev.findNeedleFromEcx(n.Id)
  102. if err != nil {
  103. return types.Offset{}, 0, nil, err
  104. }
  105. shard := ev.Shards[0]
  106. // calculate the locations in the ec shards
  107. intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset.ToAcutalOffset(), uint32(needle.GetActualSize(size, version)))
  108. return
  109. }
  110. func (ev *EcVolume) findNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size uint32, err error) {
  111. var key types.NeedleId
  112. buf := make([]byte, types.NeedleMapEntrySize)
  113. l, h := int64(0), ev.ecxFileSize/types.NeedleMapEntrySize
  114. for l < h {
  115. m := (l + h) / 2
  116. if _, err := ev.ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  117. return types.Offset{}, 0, err
  118. }
  119. key, offset, size = idx.IdxFileEntry(buf)
  120. if key == needleId {
  121. return
  122. }
  123. if key < needleId {
  124. l = m + 1
  125. } else {
  126. h = m
  127. }
  128. }
  129. err = fmt.Errorf("needle id %d not found", needleId)
  130. return
  131. }