ec_volume.go 6.9 KB

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