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.

257 lines
7.3 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
  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) bool {
  75. return a.VolumeId < b.VolumeId || a.VolumeId == b.VolumeId && a.ShardId < b.ShardId
  76. })
  77. return true
  78. }
  79. func (ev *EcVolume) DeleteEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, deleted bool) {
  80. foundPosition := -1
  81. for i, s := range ev.Shards {
  82. if s.ShardId == shardId {
  83. foundPosition = i
  84. }
  85. }
  86. if foundPosition < 0 {
  87. return nil, false
  88. }
  89. ecVolumeShard = ev.Shards[foundPosition]
  90. ev.Shards = append(ev.Shards[:foundPosition], ev.Shards[foundPosition+1:]...)
  91. return ecVolumeShard, true
  92. }
  93. func (ev *EcVolume) FindEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, found bool) {
  94. for _, s := range ev.Shards {
  95. if s.ShardId == shardId {
  96. return s, true
  97. }
  98. }
  99. return nil, false
  100. }
  101. func (ev *EcVolume) Close() {
  102. for _, s := range ev.Shards {
  103. s.Close()
  104. }
  105. if ev.ecjFile != nil {
  106. ev.ecjFileAccessLock.Lock()
  107. _ = ev.ecjFile.Close()
  108. ev.ecjFile = nil
  109. ev.ecjFileAccessLock.Unlock()
  110. }
  111. if ev.ecxFile != nil {
  112. _ = ev.ecxFile.Sync()
  113. _ = ev.ecxFile.Close()
  114. ev.ecxFile = nil
  115. }
  116. }
  117. func (ev *EcVolume) Destroy() {
  118. ev.Close()
  119. for _, s := range ev.Shards {
  120. s.Destroy()
  121. }
  122. os.Remove(ev.FileName(".ecx"))
  123. os.Remove(ev.FileName(".ecj"))
  124. os.Remove(ev.FileName(".vif"))
  125. }
  126. func (ev *EcVolume) FileName(ext string) string {
  127. switch ext {
  128. case ".ecx", ".ecj":
  129. return ev.IndexBaseFileName() + ext
  130. }
  131. // .vif
  132. return ev.DataBaseFileName() + ext
  133. }
  134. func (ev *EcVolume) DataBaseFileName() string {
  135. return EcShardFileName(ev.Collection, ev.dir, int(ev.VolumeId))
  136. }
  137. func (ev *EcVolume) IndexBaseFileName() string {
  138. return EcShardFileName(ev.Collection, ev.dirIdx, int(ev.VolumeId))
  139. }
  140. func (ev *EcVolume) ShardSize() uint64 {
  141. if len(ev.Shards) > 0 {
  142. return uint64(ev.Shards[0].Size())
  143. }
  144. return 0
  145. }
  146. func (ev *EcVolume) Size() (size int64) {
  147. for _, shard := range ev.Shards {
  148. size += shard.Size()
  149. }
  150. return
  151. }
  152. func (ev *EcVolume) CreatedAt() time.Time {
  153. return ev.ecxCreatedAt
  154. }
  155. func (ev *EcVolume) ShardIdList() (shardIds []ShardId) {
  156. for _, s := range ev.Shards {
  157. shardIds = append(shardIds, s.ShardId)
  158. }
  159. return
  160. }
  161. func (ev *EcVolume) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  162. prevVolumeId := needle.VolumeId(math.MaxUint32)
  163. var m *master_pb.VolumeEcShardInformationMessage
  164. for _, s := range ev.Shards {
  165. if s.VolumeId != prevVolumeId {
  166. m = &master_pb.VolumeEcShardInformationMessage{
  167. Id: uint32(s.VolumeId),
  168. Collection: s.Collection,
  169. DiskType: string(ev.diskType),
  170. }
  171. messages = append(messages, m)
  172. }
  173. prevVolumeId = s.VolumeId
  174. m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
  175. }
  176. return
  177. }
  178. func (ev *EcVolume) LocateEcShardNeedle(needleId types.NeedleId, version needle.Version) (offset types.Offset, size types.Size, intervals []Interval, err error) {
  179. // find the needle from ecx file
  180. offset, size, err = ev.FindNeedleFromEcx(needleId)
  181. if err != nil {
  182. return types.Offset{}, 0, nil, fmt.Errorf("FindNeedleFromEcx: %v", err)
  183. }
  184. intervals = ev.LocateEcShardNeedleInterval(version, offset.ToActualOffset(), types.Size(needle.GetActualSize(size, version)))
  185. return
  186. }
  187. func (ev *EcVolume) LocateEcShardNeedleInterval(version needle.Version, offset int64, size types.Size) (intervals []Interval) {
  188. shard := ev.Shards[0]
  189. // calculate the locations in the ec shards
  190. intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset, types.Size(needle.GetActualSize(size, version)))
  191. return
  192. }
  193. func (ev *EcVolume) FindNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size types.Size, err error) {
  194. return SearchNeedleFromSortedIndex(ev.ecxFile, ev.ecxFileSize, needleId, nil)
  195. }
  196. 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) {
  197. var key types.NeedleId
  198. buf := make([]byte, types.NeedleMapEntrySize)
  199. l, h := int64(0), ecxFileSize/types.NeedleMapEntrySize
  200. for l < h {
  201. m := (l + h) / 2
  202. if _, err := ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  203. return types.Offset{}, types.TombstoneFileSize, fmt.Errorf("ecx file %d read at %d: %v", ecxFileSize, m*types.NeedleMapEntrySize, err)
  204. }
  205. key, offset, size = idx.IdxFileEntry(buf)
  206. if key == needleId {
  207. if processNeedleFn != nil {
  208. err = processNeedleFn(ecxFile, m*types.NeedleHeaderSize)
  209. }
  210. return
  211. }
  212. if key < needleId {
  213. l = m + 1
  214. } else {
  215. h = m
  216. }
  217. }
  218. err = NotFoundError
  219. return
  220. }