ec_volume.go 7.3 KB

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