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.

281 lines
8.3 KiB

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