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.

251 lines
7.0 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
6 years ago
  1. package erasure_coding
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb"
  6. "github.com/chrislusf/seaweedfs/weed/storage/volume_info"
  7. "math"
  8. "os"
  9. "sort"
  10. "sync"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  13. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  14. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  15. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  16. "github.com/chrislusf/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. sort.Slice(ev.Shards, func(i, j int) bool {
  74. return ev.Shards[i].VolumeId < ev.Shards[j].VolumeId ||
  75. ev.Shards[i].VolumeId == ev.Shards[j].VolumeId && ev.Shards[i].ShardId < ev.Shards[j].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.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. shard := ev.Shards[0]
  184. // calculate the locations in the ec shards
  185. intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset.ToActualOffset(), types.Size(needle.GetActualSize(size, version)))
  186. return
  187. }
  188. func (ev *EcVolume) FindNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size types.Size, err error) {
  189. return SearchNeedleFromSortedIndex(ev.ecxFile, ev.ecxFileSize, needleId, nil)
  190. }
  191. 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) {
  192. var key types.NeedleId
  193. buf := make([]byte, types.NeedleMapEntrySize)
  194. l, h := int64(0), ecxFileSize/types.NeedleMapEntrySize
  195. for l < h {
  196. m := (l + h) / 2
  197. if _, err := ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
  198. return types.Offset{}, types.TombstoneFileSize, fmt.Errorf("ecx file %d read at %d: %v", ecxFileSize, m*types.NeedleMapEntrySize, err)
  199. }
  200. key, offset, size = idx.IdxFileEntry(buf)
  201. if key == needleId {
  202. if processNeedleFn != nil {
  203. err = processNeedleFn(ecxFile, m*types.NeedleHeaderSize)
  204. }
  205. return
  206. }
  207. if key < needleId {
  208. l = m + 1
  209. } else {
  210. h = m
  211. }
  212. }
  213. err = NotFoundError
  214. return
  215. }