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.

266 lines
7.7 KiB

2 years ago
  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. v.checkReadWriteError(err)
  54. if err != nil {
  55. return 0, err
  56. }
  57. }
  58. count = int(n.DataSize)
  59. if !n.HasTtl() {
  60. return
  61. }
  62. ttlMinutes := n.Ttl.Minutes()
  63. if ttlMinutes == 0 {
  64. return
  65. }
  66. if !n.HasLastModifiedDate() {
  67. return
  68. }
  69. if time.Now().Before(time.Unix(0, int64(n.AppendAtNs)).Add(time.Duration(ttlMinutes) * time.Minute)) {
  70. return
  71. }
  72. return -1, ErrorNotFound
  73. }
  74. // read needle at a specific offset
  75. func (v *Volume) readNeedleMetaAt(n *needle.Needle, offset int64, size int32) (err error) {
  76. v.dataFileAccessLock.RLock()
  77. defer v.dataFileAccessLock.RUnlock()
  78. // read deleted needle meta data
  79. if size < 0 {
  80. size = 0
  81. }
  82. err = n.ReadNeedleMeta(v.DataBackend, offset, Size(size), v.Version())
  83. if err == needle.ErrorSizeMismatch && OffsetSize == 4 {
  84. err = n.ReadNeedleMeta(v.DataBackend, offset+int64(MaxPossibleVolumeSize), Size(size), v.Version())
  85. }
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. // read fills in Needle content by looking up n.Id from NeedleMapper
  92. func (v *Volume) readNeedleDataInto(n *needle.Needle, readOption *ReadOption, writer io.Writer, offset int64, size int64) (err error) {
  93. if !readOption.HasSlowRead {
  94. v.dataFileAccessLock.RLock()
  95. defer v.dataFileAccessLock.RUnlock()
  96. }
  97. if readOption.HasSlowRead {
  98. v.dataFileAccessLock.RLock()
  99. }
  100. nv, ok := v.nm.Get(n.Id)
  101. if readOption.HasSlowRead {
  102. v.dataFileAccessLock.RUnlock()
  103. }
  104. if !ok || nv.Offset.IsZero() {
  105. return ErrorNotFound
  106. }
  107. readSize := nv.Size
  108. if readSize.IsDeleted() {
  109. if readOption != nil && readOption.ReadDeleted && readSize != TombstoneFileSize {
  110. glog.V(3).Infof("reading deleted %s", n.String())
  111. readSize = -readSize
  112. } else {
  113. return ErrorDeleted
  114. }
  115. }
  116. if readSize == 0 {
  117. return nil
  118. }
  119. actualOffset := nv.Offset.ToActualOffset()
  120. if readOption.IsOutOfRange {
  121. actualOffset += int64(MaxPossibleVolumeSize)
  122. }
  123. buf := mem.Allocate(min(readOption.ReadBufferSize, int(size)))
  124. defer mem.Free(buf)
  125. // read needle data
  126. crc := needle.CRC(0)
  127. for x := offset; x < offset+size; x += int64(len(buf)) {
  128. if readOption.HasSlowRead {
  129. v.dataFileAccessLock.RLock()
  130. }
  131. // possibly re-read needle offset if volume is compacted
  132. if readOption.VolumeRevision != v.SuperBlock.CompactionRevision {
  133. // the volume is compacted
  134. nv, ok = v.nm.Get(n.Id)
  135. if !ok || nv.Offset.IsZero() {
  136. if readOption.HasSlowRead {
  137. v.dataFileAccessLock.RUnlock()
  138. }
  139. return ErrorNotFound
  140. }
  141. actualOffset = nv.Offset.ToActualOffset()
  142. readOption.VolumeRevision = v.SuperBlock.CompactionRevision
  143. }
  144. count, err := n.ReadNeedleData(v.DataBackend, actualOffset, buf, x)
  145. if readOption.HasSlowRead {
  146. v.dataFileAccessLock.RUnlock()
  147. }
  148. toWrite := min(count, int(offset+size-x))
  149. if toWrite > 0 {
  150. crc = crc.Update(buf[0:toWrite])
  151. if _, err = writer.Write(buf[0:toWrite]); err != nil {
  152. return fmt.Errorf("ReadNeedleData write: %v", err)
  153. }
  154. }
  155. if err != nil {
  156. if err == io.EOF {
  157. err = nil
  158. break
  159. }
  160. return fmt.Errorf("ReadNeedleData: %v", err)
  161. }
  162. if count <= 0 {
  163. break
  164. }
  165. }
  166. if offset == 0 && size == int64(n.DataSize) && (n.Checksum != crc && uint32(n.Checksum) != crc.Value()) {
  167. // the crc.Value() function is to be deprecated. this double checking is for backward compatible.
  168. return fmt.Errorf("ReadNeedleData checksum %v expected %v", crc, n.Checksum)
  169. }
  170. return nil
  171. }
  172. func min(x, y int) int {
  173. if x < y {
  174. return x
  175. }
  176. return y
  177. }
  178. // read fills in Needle content by looking up n.Id from NeedleMapper
  179. func (v *Volume) ReadNeedleBlob(offset int64, size Size) ([]byte, error) {
  180. v.dataFileAccessLock.RLock()
  181. defer v.dataFileAccessLock.RUnlock()
  182. return needle.ReadNeedleBlob(v.DataBackend, offset, size, v.Version())
  183. }
  184. type VolumeFileScanner interface {
  185. VisitSuperBlock(super_block.SuperBlock) error
  186. ReadNeedleBody() bool
  187. VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error
  188. }
  189. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  190. needleMapKind NeedleMapKind,
  191. volumeFileScanner VolumeFileScanner) (err error) {
  192. var v *Volume
  193. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  194. return fmt.Errorf("failed to load volume %d: %v", id, err)
  195. }
  196. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  197. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  198. }
  199. defer v.Close()
  200. version := v.Version()
  201. offset := int64(v.SuperBlock.BlockSize())
  202. return ScanVolumeFileFrom(version, v.DataBackend, offset, volumeFileScanner)
  203. }
  204. func ScanVolumeFileFrom(version needle.Version, datBackend backend.BackendStorageFile, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  205. n, nh, rest, e := needle.ReadNeedleHeader(datBackend, version, offset)
  206. if e != nil {
  207. if e == io.EOF {
  208. return nil
  209. }
  210. return fmt.Errorf("cannot read %s at offset %d: %v", datBackend.Name(), offset, e)
  211. }
  212. for n != nil {
  213. var needleBody []byte
  214. if volumeFileScanner.ReadNeedleBody() {
  215. // println("needle", n.Id.String(), "offset", offset, "size", n.Size, "rest", rest)
  216. if needleBody, err = n.ReadNeedleBody(datBackend, version, offset+NeedleHeaderSize, rest); err != nil {
  217. 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)
  218. // err = fmt.Errorf("cannot read needle body: %v", err)
  219. // return
  220. }
  221. }
  222. err := volumeFileScanner.VisitNeedle(n, offset, nh, needleBody)
  223. if err == io.EOF {
  224. return nil
  225. }
  226. if err != nil {
  227. glog.V(0).Infof("visit needle error: %v", err)
  228. return fmt.Errorf("visit needle error: %v", err)
  229. }
  230. offset += NeedleHeaderSize + rest
  231. glog.V(4).Infof("==> new entry offset %d", offset)
  232. if n, nh, rest, err = needle.ReadNeedleHeader(datBackend, version, offset); err != nil {
  233. if err == io.EOF {
  234. return nil
  235. }
  236. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  237. }
  238. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  239. }
  240. return nil
  241. }