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.

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