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.

190 lines
6.0 KiB

  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  5. "io"
  6. "time"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  11. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  12. )
  13. const PagedReadLimit = 1024 * 1024
  14. // read fills in Needle content by looking up n.Id from NeedleMapper
  15. func (v *Volume) readNeedle(n *needle.Needle, readOption *ReadOption, onReadSizeFn func(size Size)) (count int, err error) {
  16. v.dataFileAccessLock.RLock()
  17. defer v.dataFileAccessLock.RUnlock()
  18. nv, ok := v.nm.Get(n.Id)
  19. if !ok || nv.Offset.IsZero() {
  20. return -1, ErrorNotFound
  21. }
  22. readSize := nv.Size
  23. if readSize.IsDeleted() {
  24. if readOption != nil && readOption.ReadDeleted && readSize != TombstoneFileSize {
  25. glog.V(3).Infof("reading deleted %s", n.String())
  26. readSize = -readSize
  27. } else {
  28. return -1, ErrorDeleted
  29. }
  30. }
  31. if readSize == 0 {
  32. return 0, nil
  33. }
  34. if onReadSizeFn != nil {
  35. onReadSizeFn(readSize)
  36. }
  37. if readOption != nil && readOption.AttemptMetaOnly && readSize > PagedReadLimit {
  38. readOption.VolumeRevision = v.SuperBlock.CompactionRevision
  39. err = n.ReadNeedleMeta(v.DataBackend, nv.Offset.ToActualOffset(), readSize, v.Version())
  40. if err == needle.ErrorSizeMismatch && OffsetSize == 4 {
  41. readOption.IsOutOfRange = true
  42. err = n.ReadNeedleMeta(v.DataBackend, nv.Offset.ToActualOffset()+int64(MaxPossibleVolumeSize), readSize, v.Version())
  43. }
  44. if err != nil {
  45. return 0, err
  46. }
  47. if !n.IsCompressed() && !n.IsChunkedManifest() {
  48. readOption.IsMetaOnly = true
  49. }
  50. }
  51. if readOption == nil || !readOption.IsMetaOnly {
  52. err = n.ReadData(v.DataBackend, nv.Offset.ToActualOffset(), readSize, v.Version())
  53. if err == needle.ErrorSizeMismatch && OffsetSize == 4 {
  54. err = n.ReadData(v.DataBackend, nv.Offset.ToActualOffset()+int64(MaxPossibleVolumeSize), readSize, v.Version())
  55. }
  56. v.checkReadWriteError(err)
  57. if err != nil {
  58. return 0, err
  59. }
  60. }
  61. count = int(n.DataSize)
  62. if !n.HasTtl() {
  63. return
  64. }
  65. ttlMinutes := n.Ttl.Minutes()
  66. if ttlMinutes == 0 {
  67. return
  68. }
  69. if !n.HasLastModifiedDate() {
  70. return
  71. }
  72. if time.Now().Before(time.Unix(0, int64(n.AppendAtNs)).Add(time.Duration(ttlMinutes) * time.Minute)) {
  73. return
  74. }
  75. return -1, ErrorNotFound
  76. }
  77. // read fills in Needle content by looking up n.Id from NeedleMapper
  78. func (v *Volume) readNeedleDataInto(n *needle.Needle, readOption *ReadOption, writer io.Writer, offset int64, size int64) (err error) {
  79. v.dataFileAccessLock.RLock()
  80. defer v.dataFileAccessLock.RUnlock()
  81. nv, ok := v.nm.Get(n.Id)
  82. if !ok || nv.Offset.IsZero() {
  83. return ErrorNotFound
  84. }
  85. readSize := nv.Size
  86. if readSize.IsDeleted() {
  87. if readOption != nil && readOption.ReadDeleted && readSize != TombstoneFileSize {
  88. glog.V(3).Infof("reading deleted %s", n.String())
  89. readSize = -readSize
  90. } else {
  91. return ErrorDeleted
  92. }
  93. }
  94. if readSize == 0 {
  95. return nil
  96. }
  97. if readOption.VolumeRevision != v.SuperBlock.CompactionRevision {
  98. // the volume is compacted
  99. readOption.IsOutOfRange = false
  100. err = n.ReadNeedleMeta(v.DataBackend, nv.Offset.ToActualOffset(), readSize, v.Version())
  101. }
  102. buf := mem.Allocate(1024 * 1024)
  103. defer mem.Free(buf)
  104. actualOffset := nv.Offset.ToActualOffset()
  105. if readOption.IsOutOfRange {
  106. actualOffset += int64(MaxPossibleVolumeSize)
  107. }
  108. return n.ReadNeedleDataInto(v.DataBackend, actualOffset, buf, writer, offset, size)
  109. }
  110. // read fills in Needle content by looking up n.Id from NeedleMapper
  111. func (v *Volume) ReadNeedleBlob(offset int64, size Size) ([]byte, error) {
  112. v.dataFileAccessLock.RLock()
  113. defer v.dataFileAccessLock.RUnlock()
  114. return needle.ReadNeedleBlob(v.DataBackend, offset, size, v.Version())
  115. }
  116. type VolumeFileScanner interface {
  117. VisitSuperBlock(super_block.SuperBlock) error
  118. ReadNeedleBody() bool
  119. VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error
  120. }
  121. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  122. needleMapKind NeedleMapKind,
  123. volumeFileScanner VolumeFileScanner) (err error) {
  124. var v *Volume
  125. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  126. return fmt.Errorf("failed to load volume %d: %v", id, err)
  127. }
  128. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  129. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  130. }
  131. defer v.Close()
  132. version := v.Version()
  133. offset := int64(v.SuperBlock.BlockSize())
  134. return ScanVolumeFileFrom(version, v.DataBackend, offset, volumeFileScanner)
  135. }
  136. func ScanVolumeFileFrom(version needle.Version, datBackend backend.BackendStorageFile, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  137. n, nh, rest, e := needle.ReadNeedleHeader(datBackend, version, offset)
  138. if e != nil {
  139. if e == io.EOF {
  140. return nil
  141. }
  142. return fmt.Errorf("cannot read %s at offset %d: %v", datBackend.Name(), offset, e)
  143. }
  144. for n != nil {
  145. var needleBody []byte
  146. if volumeFileScanner.ReadNeedleBody() {
  147. // println("needle", n.Id.String(), "offset", offset, "size", n.Size, "rest", rest)
  148. if needleBody, err = n.ReadNeedleBody(datBackend, version, offset+NeedleHeaderSize, rest); err != nil {
  149. glog.V(0).Infof("cannot read needle head [%d, %d) body [%d, %d) body length %d: %v", offset, offset+NeedleHeaderSize, offset+NeedleHeaderSize, offset+NeedleHeaderSize+rest, rest, err)
  150. // err = fmt.Errorf("cannot read needle body: %v", err)
  151. // return
  152. }
  153. }
  154. err := volumeFileScanner.VisitNeedle(n, offset, nh, needleBody)
  155. if err == io.EOF {
  156. return nil
  157. }
  158. if err != nil {
  159. glog.V(0).Infof("visit needle error: %v", err)
  160. return fmt.Errorf("visit needle error: %v", err)
  161. }
  162. offset += NeedleHeaderSize + rest
  163. glog.V(4).Infof("==> new entry offset %d", offset)
  164. if n, nh, rest, err = needle.ReadNeedleHeader(datBackend, version, offset); err != nil {
  165. if err == io.EOF {
  166. return nil
  167. }
  168. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  169. }
  170. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  171. }
  172. return nil
  173. }