ec_volume.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package erasure_coding
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/volume_info"
  7. "golang.org/x/exp/slices"
  8. "math"
  9. "os"
  10. "sync"
  11. "time"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/idx"
  15. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  16. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  17. )
  18. var (
  19. NotFoundError = errors.New("needle not found")
  20. )
  21. type EcVolume struct {
  22. VolumeId needle.VolumeId
  23. Collection string
  24. dir string
  25. dirIdx string
  26. ecxFile *os.File
  27. ecxFileSize int64
  28. ecxCreatedAt time.Time
  29. Shards []*EcVolumeShard
  30. ShardLocations map[ShardId][]pb.ServerAddress
  31. ShardLocationsRefreshTime time.Time
  32. ShardLocationsLock sync.RWMutex
  33. Version needle.Version
  34. ecjFile *os.File
  35. ecjFileAccessLock sync.Mutex
  36. diskType types.DiskType
  37. }
  38. func NewEcVolume(diskType types.DiskType, dir string, dirIdx string, collection string, vid needle.VolumeId) (ev *EcVolume, err error) {
  39. ev = &EcVolume{dir: dir, dirIdx: dirIdx, Collection: collection, VolumeId: vid, diskType: diskType}
  40. dataBaseFileName := EcShardFileName(collection, dir, int(vid))
  41. indexBaseFileName := EcShardFileName(collection, dirIdx, int(vid))
  42. // open ecx file
  43. if ev.ecxFile, err = os.OpenFile(indexBaseFileName+".ecx", os.O_RDWR, 0644); err != nil {
  44. return nil, fmt.Errorf("cannot open ec volume index %s.ecx: %v", indexBaseFileName, err)
  45. }
  46. ecxFi, statErr := ev.ecxFile.Stat()
  47. if statErr != nil {
  48. return nil, fmt.Errorf("can not stat ec volume index %s.ecx: %v", indexBaseFileName, statErr)
  49. }
  50. ev.ecxFileSize = ecxFi.Size()
  51. ev.ecxCreatedAt = ecxFi.ModTime()
  52. // open ecj file
  53. if ev.ecjFile, err = os.OpenFile(indexBaseFileName+".ecj", os.O_RDWR|os.O_CREATE, 0644); err != nil {
  54. return nil, fmt.Errorf("cannot open ec volume journal %s.ecj: %v", indexBaseFileName, err)
  55. }
  56. // read volume info
  57. ev.Version = needle.Version3
  58. if volumeInfo, _, found, _ := volume_info.MaybeLoadVolumeInfo(dataBaseFileName + ".vif"); found {
  59. ev.Version = needle.Version(volumeInfo.Version)
  60. } else {
  61. volume_info.SaveVolumeInfo(dataBaseFileName+".vif", &volume_server_pb.VolumeInfo{Version: uint32(ev.Version)})
  62. }
  63. ev.ShardLocations = make(map[ShardId][]pb.ServerAddress)
  64. return
  65. }
  66. func (ev *EcVolume) AddEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
  67. for _, s := range ev.Shards {
  68. if s.ShardId == ecVolumeShard.ShardId {
  69. return false
  70. }
  71. }
  72. ev.Shards = append(ev.Shards, ecVolumeShard)
  73. slices.SortFunc(ev.Shards, func(a, b *EcVolumeShard) bool {
  74. return a.VolumeId < b.VolumeId || a.VolumeId == b.VolumeId && a.ShardId < b.ShardId
  75. })
  76. return true
  77. }
  78. func (ev *EcVolume) DeleteEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, deleted bool) {
  79. foundPosition := -1
  80. for i, s := range ev.Shards {
  81. if s.ShardId == shardId {
  82. foundPosition = i
  83. }
  84. }
  85. if foundPosition < 0 {
  86. return nil, false
  87. }
  88. ecVolumeShard = ev.Shards[foundPosition]
  89. ev.Shards = append(ev.Shards[:foundPosition], ev.Shards[foundPosition+1:]...)
  90. return ecVolumeShard, true
  91. }
  92. func (ev *EcVolume) FindEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, found bool) {
  93. for _, s := range ev.Shards {
  94. if s.ShardId == shardId {
  95. return s, true
  96. }
  97. }
  98. return nil, false
  99. }
  100. func (ev *EcVolume) Close() {
  101. for _, s := range ev.Shards {
  102. s.Close()
  103. }
  104. if ev.ecjFile != nil {
  105. ev.ecjFileAccessLock.Lock()
  106. _ = ev.ecjFile.Close()
  107. ev.ecjFile = nil
  108. ev.ecjFileAccessLock.Unlock()
  109. }
  110. if ev.ecxFile != nil {
  111. _ = ev.ecxFile.Sync()
  112. _ = ev.ecxFile.Close()
  113. ev.ecxFile = nil
  114. }
  115. }
  116. func (ev *EcVolume) Destroy() {
  117. ev.Close()
  118. for _, s := range ev.Shards {
  119. s.Destroy()
  120. }
  121. os.Remove(ev.FileName(".ecx"))
  122. os.Remove(ev.FileName(".ecj"))
  123. os.Remove(ev.FileName(".vif"))
  124. }
  125. func (ev *EcVolume) FileName(ext string) string {
  126. switch ext {
  127. case ".ecx", ".ecj":
  128. return ev.IndexBaseFileName() + ext
  129. }
  130. // .vif
  131. return ev.DataBaseFileName() + ext
  132. }
  133. func (ev *EcVolume) DataBaseFileName() string {
  134. return EcShardFileName(ev.Collection, ev.dir, int(ev.VolumeId))
  135. }
  136. func (ev *EcVolume) IndexBaseFileName() string {
  137. return EcShardFileName(ev.Collection, ev.dirIdx, int(ev.VolumeId))
  138. }
  139. func (ev *EcVolume) ShardSize() uint64 {
  140. if len(ev.Shards) > 0 {
  141. return uint64(ev.Shards[0].Size())
  142. }
  143. return 0
  144. }
  145. func (ev *EcVolume) Size() (size int64) {
  146. for _, shard := range ev.Shards {
  147. size += shard.Size()
  148. }
  149. return
  150. }
  151. func (ev *EcVolume) CreatedAt() time.Time {
  152. return ev.ecxCreatedAt
  153. }
  154. func (ev *EcVolume) ShardIdList() (shardIds []ShardId) {
  155. for _, s := range ev.Shards {
  156. shardIds = append(shardIds, s.ShardId)
  157. }
  158. return
  159. }
  160. func (ev *EcVolume) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  161. prevVolumeId := needle.VolumeId(math.MaxUint32)
  162. var m *master_pb.VolumeEcShardInformationMessage
  163. for _, s := range ev.Shards {
  164. if s.VolumeId != prevVolumeId {
  165. m = &master_pb.VolumeEcShardInformationMessage{
  166. Id: uint32(s.VolumeId),
  167. Collection: s.Collection,
  168. DiskType: string(ev.diskType),
  169. }
  170. messages = append(messages, m)
  171. }
  172. prevVolumeId = s.VolumeId
  173. m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
  174. }
  175. return
  176. }
  177. func (ev *EcVolume) LocateEcShardNeedle(needleId types.NeedleId, version needle.Version) (offset types.Offset, size types.Size, intervals []Interval, err error) {
  178. // find the needle from ecx file
  179. offset, size, err = ev.FindNeedleFromEcx(needleId)
  180. if err != nil {
  181. return types.Offset{}, 0, nil, fmt.Errorf("FindNeedleFromEcx: %v", err)
  182. }
  183. intervals = ev.LocateEcShardNeedleInterval(version, offset.ToActualOffset(), types.Size(needle.GetActualSize(size, version)))
  184. return
  185. }
  186. func (ev *EcVolume) LocateEcShardNeedleInterval(version needle.Version, offset int64, size types.Size) (intervals []Interval) {
  187. shard := ev.Shards[0]
  188. // calculate the locations in the ec shards
  189. intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset, types.Size(needle.GetActualSize(size, version)))
  190. return
  191. }
  192. func (ev *EcVolume) FindNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size types.Size, err error) {
  193. return SearchNeedleFromSortedIndex(ev.ecxFile, ev.ecxFileSize, needleId, nil)
  194. }
  195. func SearchNeedleFromSortedIndex(ecxFile *os.File, ecxFileSize int64, needleId types.NeedleId, processNeedleFn func(file *os.File, offset int64) error) (offset types.Offset, size types.Size, err error) {
  196. var key types.NeedleId
  197. buf := make([]byte, types.NeedleMapEntrySize)
  198. l, h := int64(0), ecxFileSize/types.NeedleMapEntrySize
  199. for l < h {
  200. m := (l + h) / 2
  201. if _, err := ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  202. return types.Offset{}, types.TombstoneFileSize, fmt.Errorf("ecx file %d read at %d: %v", ecxFileSize, m*types.NeedleMapEntrySize, err)
  203. }
  204. key, offset, size = idx.IdxFileEntry(buf)
  205. if key == needleId {
  206. if processNeedleFn != nil {
  207. err = processNeedleFn(ecxFile, m*types.NeedleHeaderSize)
  208. }
  209. return
  210. }
  211. if key < needleId {
  212. l = m + 1
  213. } else {
  214. h = m
  215. }
  216. }
  217. err = NotFoundError
  218. return
  219. }